检查由家长设定的变量是否存在 [英] Checking whether a variable set by the parent exists

查看:128
本文介绍了检查由家长设定的变量是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设以下的Makefile (见 的就是它的好为):

 目标1,原料:
    靶-1-体
目标2 - 原料:
    靶-2-体
目标3原材料:
    靶-3-体%-生的:
    回声错误:目标$ @不存在!
%:
    @ $(MAKE)$ @ - 原料3 GT;和2 2 - ;&放大器; 1 1 GT;及3

我想确保用户调用它不带后缀 -raw ,即是这样的:

 使目标1

不可以是这样的:

 使目标1,原材料

所以,我想设置一个变量在目标的规则并从所有其他目标,这个变量被设置检查:

 检查:
    #?如果没有设置标志,则用一个错误信息中止。目标1 - 原料:检查
    靶-1-体
目标2 - 原料:检查
    靶-2-体
目标3 - 原料:检查
    靶-3-体%-生的:
    回声错误:目标$ @不存在!
%:
    #?设置标志
    @ $(MAKE)$ @ - 原料3 GT;和2 2 - ;&放大器; 1 1 GT;及3

我试图实施开始#??? 没有成功行的各种方法。如果我使用制作(如 IFNDEF IFEQ ,那么事情似乎得到不计算时,我希望他们)。我试图用shell的如果[] 也有,但是没有成功。所以,如果这是在所有可能实现的,那么什么工具是我的朋友在这里?


解决方案

  

我想确保用户调用它不带后缀 -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.

这篇关于检查由家长设定的变量是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆