I have a program, set up for experimental purposes, that can be build in different variants, controlled by preprocessor switches. At the moment, my makefile is setup such that it generates the binaries build/[flags]
, where flags
is a letter-encoding of the different variants to be build.
build/[flags]:
linker commands
build/[flags]_objects/[filename].o: [filename].cpp
compiler commands
I use secondary expansion to resolve all this, i.p., to decode the flag string and turn it into command line switches to the compiler. This is a bit hard to maintain, and I would like to know if there are better alternatives to that approach.
Edit. I admit that I wasn't very clear. Here is an example. My program is supposed to be compiled with and without -DFLAG
. My binary will be called d
(like default) for the version without and f
for the version with the flag. My makefile goes roughly like this:
.SECONDEXPANSION:
# Used for using $% in secondary expansion
PERCENT=%
# Decode the flag in the target name
decode_flag=$(if $(findstring f,$(1)),-DFLAG,)
# Build the executable
build/%.out: $$(patsubst $$(PERCENT).cpp,build/$$*/$$(PERCENT).o,$$(SRC))
$(CXX) $(LDFLAGS) -o $@ $^
# Build the object files; the objects for binary build/x* in build/x/
build/%.o: $$(*F).cpp
@mkdir -p $(@D)
$(CXX) -c $(CXXFLAGS) $(call decode_flag, $(*D)) -o $@ $<
I haven't specified CXX
, CXXFLAGS
, LDFLAGS
and SRC
; they have the usual meaning.
I guess you'll admit that all this patsubst
-stuff is hard to read. In reality, the decode_flag
is slightly more complicated, but the principle stays the same.