我正在尝试通过指定目标而不是变量(例如make debug=1
,不是很好)来构建可以同时构建发行版和调试目标的Makefile,我在这里有一个简化的简化示例,它模仿了我要实现的目标:
ifdef debug
BINARY=my_binary_debug
MODULE_1_BIN=abc_debug
MODULE_2_BIN=xyz_debug
export DBG=1
else
BINARY=my_binary
MODULE_1_BIN=abc
MODULE_2_BIN=xyz
endif
FLAG=something
.PHONY: all debug clean
all: bin/$(BINARY).bin
bin/$(BINARY).bin: module_1/$(MODULE_1_BIN).bin module_2/$(MODULE_2_BIN).bin
cat module_1/$(MODULE_1_BIN).bin $(FLAG) module_2/$(MODULE_2_BIN).bin > $@
module_1/$(MODULE_1_BIN).bin:
$(MAKE) -C module_1
module_2/$(MODULE_2_BIN).bin:
$(MAKE) -C module_2
clean:
rm bin/*.bin
$(MAKE) -C module_1 clean
$(MAKE) -C module_2 clean
我可以使用该示例,make debug=1
但我并不真正喜欢它,并认为此实现可能会更好。一种方法是使用目标特定的变量,但是我不确定在构建调试目标时依赖项还需要更改其名称时如何使用它们。就像是:
debug: export DBG:=1
debug: bin/$(BINARY)_debug.bin
debug: module_1/$(MODULE_1_BIN)_debug.bin
debug: module_2/$(MODULE_2_BIN)_debug.bin
bin/$(BINARY).bin bin/$(BINARY)_debug.bin: module_1/$(MODULE_1_BIN).bin module_2/$(MODULE_2_BIN).bin
cat module_1/$(MODULE_1_BIN).bin module_2/$(MODULE_2_BIN).bin > $@
在这里目标可以正常工作,因为在bin/$(BINARY).bin
构建时不需要构建目标。debug
但是我不确定在构建时如何处理依赖关系module_1/$(MODULE_1_BIN).bin
和module_2/$(MODULE_2_BIN).bin
配方debug
此答案的先前版本尝试使用特定于目标的变量,但是它们用途有限,因为它们在配方中使用,但是在指定目标和前提条件时将被忽略。
隐式规则使用了模式,但是捕获两个模式my_binary
和my_binary_debug
使用隐式目标my_binary%
都行不通,因为模式与空字符串不匹配。
但是,在这种情况下,隐式规则会起作用,因为您的所有目标和依赖项都有一个公共结尾.bin
,即非空。匹配的字符串%
在自动变量中可用$*
。所以这是我的解决方案:
.PHONY: all debug clean
FLAG=something
# Maybe still needed by subdirectory makefiles
debug: export DBG:=1
all: bin/my_binary.bin
debug: bin/my_binary_debug.bin
# % will match both ".bin" and "_debug.bin"
# It won’t match the empty string.
bin/my_binary%: module_1/abc% module_2/xyz%
cat module_1/abc$* $(FLAG) module_2/xyz$* > $@
# Maybe, by specifying the target in the sub-make commandline,
# you can get rid of the DBG variable altogether.
module_1/abc%:
$(MAKE) -C module_1 $@
module_2/xyz%:
$(MAKE) -C module_2 $@
clean:
rm bin/*.bin
$(MAKE) -C module_1 clean
$(MAKE) -C module_2 clean
如果要构建的配方bin/my_binary.bin
可以在所有模块之间一个接一个地重写,并且$(FLAG)
在开始时可以进行一些简化:
.PHONY: all debug clean
MODULES = module_1/abc module_2/xyz
FLAG = something
# Maybe still needed by subdirectory makefiles
debug: export DBG:=1
all: bin/my_binary.bin
debug: bin/my_binary_debug.bin
bin/my_binary%: $(addsuffix %,$(MODULES))
cat $(FLAG) $^ > $@
module_1/abc%:
$(MAKE) -C module_1 $@
module_2/xyz%:
$(MAKE) -C module_2 $@
clean:
rm bin/*.bin
for m in $(MODULES); do $(MAKE) -C $$(dirname $$m) clean; done
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句