将环境变量导出到Makefile Shell [英] Exporting environment variables to Makefile shell

查看:126
本文介绍了将环境变量导出到Makefile Shell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想立即扩展Makefile中的shell命令,但是我希望shell命令可以访问Makefile中的环境变量.如果我使用$(shell ...),它将立即扩展,但是无法访问变量.如果使用反引号,则扩展不是立即进行的,稍后会在Makefile中为我带来问题.我想知道是否有任何方法可以使反引号立即扩展,或将当前环境传递给$(shell)命令.

I want to do immediate expansion of a shell command within a Makefile, but I want the shell command to have access to the environment variables within the Makefile. If I use the $(shell ...), it expands immediately, but there is no access to the variables. If I use the backquotes, the expansion is not immediate, and it causes problems for me later in the Makefile. I'm wondering if there is any way to make the backquotes expand immediately, or to pass the current environment to a $(shell) command.

例如,以下makefile:

For example, the following makefile:

SOME_VAR := some_val
export SOME_VAR

VAR1 := `echo $$SOME_VAR`
export VAR1
VAR2 := `echo $$VAR1`

all:
      @echo VAR1=$(VAR1)
      @echo VAR2=$(VAR2)

将输出:

~/tmp/t2> make
VAR1=some_val
VAR2=`echo $SOME_VAR`

我要在其中打印"VAR2 = some_val"的地方.实际的示例要复杂一些(环境变量是从父makefile继承的,而我正尝试使用perl脚本来编辑变量),但是原理是相同的.

Where I want it to print "VAR2=some_val". The real example is a bit more complicated (environment variables are inherited from parent makefiles, and I'm trying to use a perl script to edit the variables), but the principle is the same.

感谢您的帮助.

推荐答案

正如我在一些评论中提到的,我的实际目标是使脚本根据编译对象时使用的设置来生成文件名.然后,我需要另一个脚本来生成所有生成的文件名的特殊格式列表(目标是没有JIT编译器的嵌入式系统).在任何给定时间,都有超过30种可能会影响二进制文件的设置,并且将来可能会在多个模块上使用该设置,因此我希望具有可扩展性.

As I mentioned in some of the comments, my actual goal was to make the script generate filenames based on the settings the object was being compiled with. I then need another script to generate a specially formatted list of all the filenames generated (the target is an embedded system which doesn't have a JIT compiler on it). At any given time, there are over thirty settings which can potentially effect the binary, and this may be used on more than one module in the future, so I'd like something scalable.

我的解决方法如下.我没有传递变量,而是修改了脚本以根据以下设置输出一个makefile可解析的字符串:

My solution is as follows. Instead of passing the variables in, I modified my script to output a makefile-parsable string based on the settings:

-include $(SOME_MK_FILE)

$(SOME_MK_FILE) : .phony
    script.pl $(SETTINGS_OF_INTEREST_LIST) > $(SOME_MK_FILE)

someFilename := $(shell script2.pl $(VAR1))

script.pl输出一个类似于以下内容的字符串:

script.pl outputs a string that looks something like:

VAR1 := CONFIG_X1=$(CONFIG_X1) CONFIG_X2=$(CONFIG_X2) CONFIG_X33=$(CONFIG_X33)

and script2输出的文件名看起来像"someFilename.X1_y.X2_n.elf"

and script2 outputs a filename that looks something like 'someFilename.X1_y.X2_n.elf'

然后,在另一条规则中,我有:

and then, later on, in another rule, I have:

someobj: somedep
    script3.pl $(someFilename) >> builtfiles.txt

可以正确构建builtfiles.txt(这又是另一个脚本的输入...).最后,这是一个解决方法,可以解决make无法将其环境传递给$(shell)的事实.它不是很漂亮,但是可以.

which properly builds builtfiles.txt (which in turn is the input for yet another script...). In the end this is a workaround to the fact that make cannot pass its environement to $(shell). It's not overly pretty but it works.

约翰

这篇关于将环境变量导出到Makefile Shell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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