且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检查由家长设定的变量是否存在

更新时间:2023-11-25 22:18:28


  

我想确保用户调用它不带后缀 -raw


块引用>

我看不出有什么目的是通过这个服务。 制作和Makefile文件是针对个人,其中一个常依赖于有他们在做什么了工具。还有就是要积极$你的Makefile的对$ pvent用户无需的流交换进行构建的没有特别的理由,如果这是他们真正想要的东西。


  

所以,我想设置一个变量的目标%的规则,并从这一变量设置的所有其他目标进行检查。


块引用>

不可能的。在每个逻辑行让配方是由一个单独的shell执行。该命令是shell命令,而不是制作命令,没有您通过此类命令设置变量在命令完成生存的shell的退出。


  

所以,如果这是在所有可能实现的,那么什么工具是我的朋友在这里?


块引用>

由一个制作配方可以记录的持久性信息的后续命令或规则的主要机制是通过写文件。这样的文件或甚至它们仅仅存在的内容可以被用来影响后续命令。对于工作,如你的,但是,这似乎很可能是极其脆弱的。然而,它可能是这样的:

 目标1,原料:
    TEMP ='$ @';测试-e $$ {TEMP%-raw} -check \\
      ||回声警告:目标'$ @'直接进行
    靶-1-体%-生的:
    回声错误:目标$ @不存在!
%:
    触摸$ @ - 检查
    @ $(MAKE)$ @ - 原料3 GT;和2 2 - ;&放大器; 1 1 GT;及3
    RM $ @ - 检查

我向你保证,但是,如果我收到了的Makefile 包含这样一个结构,然后我就不会在IM pressed,我的第一个冲动是撕裂它。我建议你​​停止观看用户的***,来解决问题。

Suppose the following Makefile (see here what it's good for):

target-1-raw:
    target-1-body
target-2-raw:
    target-2-body
target-3-raw:
    target-3-body

%-raw:
    echo "Error: Target $@ does not exist!"    
%:
    @$(MAKE) $@-raw 3>&2 2>&1 1>&3

I am trying to make sure that the user invoked it without the suffix -raw, i.e. like this:

make target-1

and not like this:

make target-1-raw

So, I would like to set a variable in the rule of the target % and check from all other targets that this variable was set:

check:
    #??? If FLAG is not set, then abort with an error message. 

target-1-raw: check
    target-1-body
target-2-raw: check
    target-2-body
target-3-raw: check
    target-3-body

%-raw:
    echo "Error: Target $@ does not exist!"    
%:
    #??? set FLAG
    @$(MAKE) $@-raw 3>&2 2>&1 1>&3

I tried various ways of implementing the lines starting with #??? without success. If I use the facilities of make (such as ifndef and ifeq, then things seem to get evaluated not when I want them to). I tried to use the shell's if [] also, but without success. So, if this is at all possible to achieve, then what tool is my friend here?

I am trying to make sure that the user invoked it without the suffix -raw

I don't see what purpose is served by this. make and Makefiles are tools aimed at people whom one normally relies upon to have some idea of what they are doing. There is no particular reason to actively prevent users of your Makefile from performing a build without stream swapping, if that's what they really want.

So, I would like to set a variable in the rule of the target % and check from all other targets that this variable was set.

Not possible. Each logical line in a make recipe is executed by a separate shell. The commands are shell commands, not make commands, and no variables you set via such commands survive the exit of the shell at the completion of the command.

So, if this is at all possible to achieve, then what tool is my friend here?

The main mechanism by which a make recipe can record persistent information for subsequent commands or rules is by writing files. The content of such files or even their mere existence can be used to influence subsequent commands. For a job such as yours, however, that seems likely to be extremely fragile. Nevertheless, it might look like this:

target-1-raw:
    temp='$@'; test -e $${temp%-raw}-check \
      || echo warning: target '$@' made directly
    target-1-body

%-raw:
    echo "Error: Target $@ does not exist!"    
%:
    touch $@-check
    @$(MAKE) $@-raw 3>&2 2>&1 1>&3
    rm $@-check

I assure you, however, that if I ever received a Makefile containing such a construct then I would not be impressed, and my first impulse would be to rip it out. I suggest that you stop viewing your users' freedom as a problem to solve.