Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force gnu make to rebuild objects affected by compiler definition

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?

like image 421
Oliver Avatar asked Jul 13 '10 10:07

Oliver


People also ask

What is $@ in makefile?

The file name of the target of the rule. If the target is an archive member, then ' $@ ' is the name of the archive file. In a pattern rule that has multiple targets (see Introduction to Pattern Rules), ' $@ ' is the name of whichever target caused the rule's recipe to be run.

How do I recompile with makefile?

Use the command `make' to recompile the source files that really need recompilation. Make the changes in the header files. Use the command `make -t' to mark all the object files as up to date. The next time you run make, the changes in the header files do not cause any recompilation.

How does GNU Make work?

GNU Make is a program that automates the running of shell commands and helps with repetitive tasks. It is typically used to transform files into some other form, e.g. compiling source code files into programs or libraries. It does this by tracking prerequisites and executing a hierarchy of commands to produce targets.

What does force mean in makefile?

The force target will touch a file that your default target depends on. In the example below, I have added touch myprogram. cpp. I also added a recursive call to make. This will cause the default target to get made every time you type make force.


1 Answers

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.

like image 61
slowdog Avatar answered Sep 20 '22 17:09

slowdog