Makefile 中的变量扩展
Variable expansion in Makefiles
Make 似乎只在规则激活后扩展变量。
make ok
显示已解析的名称(即“test”)
make bad
声称依赖于“grep”(而不是“test”)
如何让 make
展开 NAME ,然后 它检查“坏”规则的依赖关系?
CMakeLists.txt:
add_executable(test testA.c testB.c) # compiler (output input input ...)
生成文件(完整):
#========================================
# Shell cannot handle parentheses:
# *** unterminated call to function 'shell': missing ')'. Stop.
# Have also tried putting {"(", ")", "\(", "\)"} in variables
# ...but it just created a different problem
#
#NAME := $(shell grep "add_executable(" CMakeLists.txt | sed 's/[^(]*( *\([^ ]*\) .*//')
#========================================
# Curious results (same with or without the colon)
#
NAME := `grep "add_executable(" CMakeLists.txt | sed 's/[^(]*( *\([^ ]*\) .*//'`
#========================================
# As expected, displays:
# NAME=¦test¦"
#
PHONY: ok
ok:
@echo "NAME=¦${NAME}¦"
#========================================
# Dies with:
# *** No rule to make target '`grep', needed by 'bad'. Stop."
#
.PHONY: bad
bad: ${NAME}
@echo "NAME=¦${NAME}¦"
Makefile(相同,但经过压缩):
NAME := `grep "add_executable(" CMakeLists.txt | sed 's/[^(]*( *\([^ ]*\) .*//'`
ok:
@echo "NAME=¦${NAME}¦"
bad: ${NAME}
@echo "NAME=¦${NAME}¦"
而不是反引号
NAME := ${shell sed -n '/add_executable/{s/add_executable( *//;s/ .*//;p;}' CMakeLists.txt}
(在 shell
命令周围使用大括号而不是圆括号是一种允许在 sed
命令中使用圆括号的做法,我不知道如何优雅地转义。)
Make 似乎只在规则激活后扩展变量。
make ok
显示已解析的名称(即“test”)
make bad
声称依赖于“grep”(而不是“test”)
如何让 make
展开 NAME ,然后 它检查“坏”规则的依赖关系?
CMakeLists.txt:
add_executable(test testA.c testB.c) # compiler (output input input ...)
生成文件(完整):
#========================================
# Shell cannot handle parentheses:
# *** unterminated call to function 'shell': missing ')'. Stop.
# Have also tried putting {"(", ")", "\(", "\)"} in variables
# ...but it just created a different problem
#
#NAME := $(shell grep "add_executable(" CMakeLists.txt | sed 's/[^(]*( *\([^ ]*\) .*//')
#========================================
# Curious results (same with or without the colon)
#
NAME := `grep "add_executable(" CMakeLists.txt | sed 's/[^(]*( *\([^ ]*\) .*//'`
#========================================
# As expected, displays:
# NAME=¦test¦"
#
PHONY: ok
ok:
@echo "NAME=¦${NAME}¦"
#========================================
# Dies with:
# *** No rule to make target '`grep', needed by 'bad'. Stop."
#
.PHONY: bad
bad: ${NAME}
@echo "NAME=¦${NAME}¦"
Makefile(相同,但经过压缩):
NAME := `grep "add_executable(" CMakeLists.txt | sed 's/[^(]*( *\([^ ]*\) .*//'`
ok:
@echo "NAME=¦${NAME}¦"
bad: ${NAME}
@echo "NAME=¦${NAME}¦"
NAME := ${shell sed -n '/add_executable/{s/add_executable( *//;s/ .*//;p;}' CMakeLists.txt}
(在 shell
命令周围使用大括号而不是圆括号是一种允许在 sed
命令中使用圆括号的做法,我不知道如何优雅地转义。)