makefile中的变量扩展
Make似乎仅在激活规则后才扩展变量makefile中的变量扩展,makefile,variable-expansion,Makefile,Variable Expansion,Make似乎仅在激活规则后才扩展变量 make ok显示已解析的名称(即“test”) makebad声称依赖于“grep”(而不是“test”) 在检查“坏”规则的依赖项之前,如何让make展开NAME CMakeLists.txt: add_executable(test testA.c testB.c) # compiler (output input input ...) 生成文件(完整): #======================================== # Sh
make ok
显示已解析的名称(即“test”)
makebad
声称依赖于“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/[^(]*( *\([^ ]*\) .*/\1/')
#========================================
# Curious results (same with or without the colon)
#
NAME := `grep "add_executable(" CMakeLists.txt | sed 's/[^(]*( *\([^ ]*\) .*/\1/'`
#========================================
# 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}¦"
NAME := `grep "add_executable(" CMakeLists.txt | sed 's/[^(]*( *\([^ ]*\) .*/\1/'`
ok:
@echo "NAME=¦${NAME}¦"
bad: ${NAME}
@echo "NAME=¦${NAME}¦"
生成文件(相同,但压缩):#========================================
# 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/[^(]*( *\([^ ]*\) .*/\1/')
#========================================
# Curious results (same with or without the colon)
#
NAME := `grep "add_executable(" CMakeLists.txt | sed 's/[^(]*( *\([^ ]*\) .*/\1/'`
#========================================
# 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}¦"
NAME := `grep "add_executable(" CMakeLists.txt | sed 's/[^(]*( *\([^ ]*\) .*/\1/'`
ok:
@echo "NAME=¦${NAME}¦"
bad: ${NAME}
@echo "NAME=¦${NAME}¦"
使用以下命令代替反引号:
(在
shell
命令中使用大括号而不是圆括号是一个很难在sed
命令中使用圆括号的地方,我不知道如何优雅地转义。)这里有一个粗略的解决方案,在我(或其他人)想出一个更优雅的解决方案之前,它将一直有效:NAME:=$(shell sed-n)/add\u/{s/add|u executable.*/;s/*/;p;}“CMakeLists.txt)
非常感谢您向我展示了如何清理grep | sed
:)谢谢。