I am trying to build a Makefile that can build both release and debug targets by specifying a target rather than a variable (such as make debug=1
, not great) I have a condensed simplified example here that emulates what I am trying to achieve:
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
This example would have me use make debug=1
but I don't really like it and feel this implementation could be better. One way is to use target specific variables , but I am not entirely sure how to use them when the dependencies also need to change their names when a debug target is built. Something like:
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 > $@
Here the target will work alright as the bin/$(BINARY).bin
target wont need to be built when debug
is being built.But I am not sure how to handle the dependencies module_1/$(MODULE_1_BIN).bin
and module_2/$(MODULE_2_BIN).bin
in the recipe when building debug