iter
iter

Reputation: 4303

Multiple build variant targets

I'm building firmware binaries that need multiple variants. There are #ifdefs in the code for different generations of the board, different peripherals and different parameters. So I have filenames like foo-REVC-LCD-EUR.elf for the European version of the board that has an LCD and is rev. C of the hardware.

I'm trying to reduce boilerplate in the makefile. Right now, it looks something like this:

all: foo.elf \
  foo-DEBUG.elf \
  foo-LCD.elf \
  foo-LCD-DEBUG.elf \
  target_unit_tests.elf

foo%elf: \
  bar.ao \
  baz.ao \
  banana.ao

target_unit_tests.elf:  \
  bar.ao \
  bar_tests.ao \
  baz.ao \
  baz_tests.ao\
  banana.ao \
  banana_tests.ao

# linker
%.elf: %.ao startup_stm32f0xx.ao system_stm32f0xx.ao
    $(ARM_CC) $(ARM_FLAGS) $other_arguments -o $@ $^

# compiler, general case
%.ao: %.c
    $(ARM_CC) $(ARM_CPPFLAGS) $(ARM_FLAGS) $(CFLAGS) -c -o $@ $<

# compiler, build variants for `foo'
foo-%.ao: foo.c
    $(ARM_CC) $(ARM_CPPFLAGS) $(ARM_FLAGS) $(CFLAGS) -c -o $@ $<

# combinatorial explosion
foo-DEBUG.ao: ARM_CPPFLAGS += -DDEBUG
foo-LCD.ao: ARM_CPPFLAGS += -DLCD
foo-LCD-DEBUG: ARM_CPPFLAGS += -DLCD -DDEBUG

The targets contain flags in their names. How can I replace the last three rules (these are multiplying fast) with a single one that extracts flags from the target name?

Or am I going down an insane path and need to rethink my strategy?

Upvotes: 1

Views: 274

Answers (1)

MadScientist
MadScientist

Reputation: 100781

This is a pretty open-ended question, which is not usually approved of on StackOverflow. However to get you started:

First, it's best to build your makefile to place all the generated per-target files into a separate directory for each target. Generated common files (if any) can be placed into a common directory.

Second, consider as much as possible defining your builds by setting variables, then writing generic rules that use these variables. This will allow you to create and modify variants by changing the values of variables, without having to mess with complicated rules.

To accomplish this you might take a look at the Metaprogramming Make blog post series (start with the last one listed, which is the first one written) and consider how capabilities such as constructed macro names can help your situation.

Cheers!

Upvotes: 1

Related Questions