简体   繁体   English

Shell命令作为Makefile中的变量

[英]Shell command as a variable in Makefile

I have the following (very simplified) Makefile: 我有以下(非常简化的)Makefile:

# Makefile

MAKESH=shell
USER=$($MAKESH logname)

which aims to store my login name in the USER variable as if the $(shell logname) command was invoked. 它旨在将我的登录名存储在USER变量中,就像调用$(shell logname)命令一样。 However this doesn't happen. 但是,这不会发生。 I suspect that it's due to the way make evaluates variables. 我怀疑这是由于make评估变量的方式引起的。

Does anyone have some more insight to this or a possible workaround ie explicitly tell make that $MAKESH is actually the shell command? 有没有人有更深入的了解这还是一个可能的解决方法,即明确告诉make的是$MAKESH实际上是shell命令?

Thanks. 谢谢。

Edit 编辑

Consider the following example: 考虑以下示例:

#parent.mk

...
include child.mk
...

-------------------
#child.mk

export $(shell logname)/path1
export $(shell logname)/path2

-------------------
#bash_script.sh

...
source child.mk
...

(in this example, sourcing child.mk fails) (在此示例中,采购child.mk失败)

Essentially, what I wanted to do is to create a file that exports pathnames depending on the current user. 本质上,我想做的是创建一个文件,该文件根据当前用户导出路径名。 The syntax should be valid both for makefiles and bash scripts, since it can be included/sourced from both places. 该语法对makefiles和bash脚本均应有效,因为它可以在两个地方都包含/获取。

I also wanted to do subtle changes to these files, since they are part of a larger project and the interconnections are a bit confusing. 我还想对这些文件进行细微的更改,因为它们是一个较大项目的一部分,并且相互之间的连接有些混乱。

I have finally decided to go with this: 我终于决定继续这样做:

#parent.mk

...
export logname=$(shell logname)
include child.mk
...

-------------------
#child.mk

export $(logname)/path1
export $(logname)/path2

-------------------
#bash_script.sh

...
source child.mk
...

which is quite hacky, a tiny bit dangerous, but works. 这很hacky,有点危险,但是可以用。 If you know a more elegant way/aprroach, let me know. 如果您知道更优雅的方式/方法,请告诉我。

Finally, thank you for your solutions (and your warnings), which are valid for the pre-editted question. 最后,感谢您的解决方案(和警告),该解决方案对预先编辑的问题有效。

The syntax should be 语法应为

USER = $(shell logname)

I don't know why you want to put "shell" in a variable and screw up the function syntax, but if you really insist, try this: 我不知道为什么要在变量中放入“ shell”并弄乱函数语法,但是如果您真的坚持,请尝试以下操作:

$(eval USER=$$($MAKESH logname))

The $(eval ...) function will just turn it into the first form above, though, so it's wasted effort. 不过,$(eval ...)函数会将其转换为上面的第一种形式,因此浪费了精力。 (Unless your really trying to do something trickier than your example suggests.) (除非您确实试图做一些比您的示例所建议的更棘手的事情。)

MAKESH=shell
$(eval USER=$$($(MAKESH) logname))
$(info USER is $(USER))

Avoid if possible. 尽可能避免。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM