views:

64

answers:

2

I have a makefile that takes options at the command line

make OPTION_1=1

Based on the value it will add additional compiler definitions to a subset of objects.

ifeq ($(OPTION_1), 1)
CC_FLAGS += -DOPTION_1_ON
endif

The change in the definition affects the included header file content - a stub or an implementation is exposed to the object files.

How can I get make to rebuild the files 'affected' by this option changing?

A: 

put them in a target then call touch on each file.

onof
+3  A: 

I use a file to remember the last value of such options, like this:

.PHONY: force
compiler_flags: force
    echo '$(CC_FLAGS)' | cmp -s - $@ || echo '$(CC_FLAGS)' > $@

The cmp || echo bit means the file compiler_flags is only touched when the setting changes, so now you can write something like

$(OBJECTS): compiler_flags

to cause a rebuild of $(OBJECTS) whenever the compiler flags change. The rule for compiler_flags will be executed every time you run make, but a rebuild of $(OBJECTS) will be triggered only if the compiler_flags file was actually modified.

slowdog
Thanks for the example. Shame it doesn't appear possible to do this without a temp file.
Oliver
Edited to avoid creating `compiler_flags.tmp`, makes it look a little tidier. I suspect that's not what you meant, though. There's no way around creating *some* file to remember the setting, since make has no other way to preserve state across multiple invocations.
slowdog