将新创建的文件设置为makefile中的变量 [英] Set newly created file as variable in makefile

查看:145
本文介绍了将新创建的文件设置为makefile中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建文件并将文件内容设置为变量,但该变量始终被视为空.

I am trying to create a file and set the contents of a file to a variable but the variable is always seen as empty.

一个例子:

define FILE_VAR
    cat /output/1.txt >> /output/2.txt
    $(eval s := $(shell cat /output/2.txt)) 
    $(eval FILENAME_BIN=$(word 1, $(s)).$(word 2, $(s)).$(word 3, $(s)).bin) 
    $(eval FILENAME_JFFS2=$(word 1, $(s)).$(word 2, $(s)).$(word 3, $(s)).jffs2)        
endef

如果2.txt在运行此命令之前存在,则变量将设置为运行make之前的数据(不是新的重定向数据),如果2.txt不存在,则不设置变量.似乎正在评估第一次运行make时文件的状态,这不是我想要的...

If 2.txt exists before running this the variables will be set to the data prior to running make (not the new redirected data), if 2.txt doesn't exist then the variables are not set. It looks like it is evaluating what the file is like when make is first run, which is not what I want...

推荐答案

您不清楚GNU make所做的工作,何时以及由Shell进行的工作, 以及何时执行配方.

You are unclear as to what is done by GNU make, and when, and what is done by the shell, and when, to execute a recipe.

$(...)形式的makefile中任何地方的任何内容均由make求值 除非它在shell命令中以$$(...)的形式转义.

Anything anywhere in a makefile of the form $(...) is evaluated by make unless it escaped as $$(...) in a shell command.

shell命令,除非在make函数$(shell ...)的上下文中或 反勾号只能是配方中的命令:

A shell command, unless in the context of the make-function $(shell ...) or back-ticks can only be a command within a recipe:

target: [prerequisite...]
    command
    ...

组成配方的命令按顺序执行,每个命令以不同的方式执行 外壳,执行配方.

The commands composing a recipe are executed in sequence, each in a distinct shell, to execute the recipe.

任何未转义形式$(...)都不是命令序列中的命令 配方的内容,除非它是您定义的将扩展扩展为命令的变量或宏.

Nothing of the unescaped form $(...) is a command in the command-sequence of a recipe unless it is the expansion of a variable or macro you have defined that expands to a command.

目标范围内的行不必是命令,也不必扩展为命令.它 可能还包含一个$(...)表达式,该表达式扩展为空, 并简单地指示make做某事,例如

A line in the scope of a target need not be a command or expand to a command. It may also consist of an $(...) expression that expands to nothing, and simply instructs make to do something, e.g.

$(info ...)

扩展为空,并告诉make在标准输出上打印....

expands to nothing and tells make to print ... on the standard output.

$(eval ...)

扩展为零,并告诉make评估...

expands to nothing and tells make to evaluate ...

此食谱只有两个命令,而不是四个:

This recipe has just two commands, not four:

target: prereq...
    $(eval TARGET=$@)
    command1
    $(info $(TARGET))
    command2

在配方范围内的任何make-expressions,$(...)都将在 当make决定运行配方时的顺序,命令顺序是什么 都经过评估后剩下.然后 命令序列运行.例如:

Any make-expressions, $(...) in the scope of a recipe are evaluated in sequence when make decides to run the recipe and the command-sequence is what is left after they have all been evaluated. Then the command-sequence is run. For example:

Makefile

target:
    echo Hello > hello.txt
    $(info Begin {)
    $(eval s := $(shell cat hello.txt)) 
    $(eval WORD=$(word 1, $(s)))
    $(info [$(WORD)])
    $(info } End)

运行:

$ make
Begin {
cat: hello.txt: No such file or directory
[]
} End
echo Hello > hello.txt

请注意,所有make表达式均在之前进行评估 运行目标命令.

Observe that all of the make-expressions are evaluated before the commands of the target are run.

您的FILE_VAR宏显然打算在配方范围内进行扩展,因为第一行是shell命令. 因此,如果其余行依赖于 运行第一行的效果.他们都没有这样做.剩下的3行 如果2.txt在创建时尚不存在,则在2.txt存在之前进行评估.

Your FILE_VAR macro is evidently intended to be expanded in recipe scope, since the first line is a shell command. The remaining lines must therefore achieve their purposes by shell commands if they depend upon the the effects of running the first line. None of them does so. The remaining 3 lines are evaluated before 2.txt exists, if it doesn't already exist at make-time.

这篇关于将新创建的文件设置为makefile中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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