4 ms·
What I do is split the makefile in two. There's a top level makefile with targets like "make debug" and "make release". Those invoke a secondary makefile that
by munificent 10y ago
What I do is split the makefile in two.
There's a top level makefile with targets like "make debug" and "make release". Those invoke a secondary makefile that actually builds things and takes options on the command line. That way I can build different configurations to different directories without having to copy/paste all of the rules for each configuration of each file.
For example, the main makefile for Wren[1] has rules like:
debug:
@ $(MAKE) -f util/wren.mk MODE=debug
That util/wren.mk [2] then uses "MODE" and a few other options, like:
# Mode configuration.
ifeq ($(MODE),debug)
WREN := wrend
C_OPTIONS += -O0 -DDEBUG -g
BUILD_DIR := $(BUILD_DIR)/debug
else
WREN += wren
C_OPTIONS += -O3
BUILD_DIR := $(BUILD_DIR)/release
endif
It has the real rules for compiling files:
# VM object files.
$(BUILD_DIR)/vm/%.o: src/vm/%.c $(VM_HEADERS)
@ printf "%10s %-30s %s\n" $(CC) $< "$(C_OPTIONS)"
@ mkdir -p $(BUILD_DIR)/vm
@ $(CC) -c $(CFLAGS) -Isrc/include -Isrc/optional -Isrc/vm -o $@ $(FILE_FLAG) $<
Took me a while to come up with this and quite some time to get it working, but I've been pretty happy with it so far.
[1]: https://github.com/munificent/wren/blob/master/Makefile https://github.com/munificent/wren/blob/master/Makefile
[2]: https://github.com/munificent/wren/blob/master/util/wren.mk https://github.com/munificent/wren/blob/master/util/wren.mk