How to keep build time when changing configuration?
Consider a project with hundreds of C files that can be built in different configurations.
The config can be highlighted by an additional -D
one sent to the compiler, i.e. -DHARDWARE_FOO
or -DHARDWARE_BAR
. When changing configuration, I usually need to clear all object files by doing the following:
make clean all
Otherwise will make
not rebuild the project because none of the dependencies have changed.
A trivial solution to avoid manually cleaning up the project is to use a named text file configuration
that contains the name of the current configuration. In Makefile
we will find:
%o:%c $(CONFIGURATION)
$(cc) -c $< -o $@ -D$(shell cat $(CONFIGURATION))
Unfortunately, this will still require rebuilding the entire project every time the configuration changes.
I believe this solution can be optimized if I can say that it only needs to rebuild the files that depend on the $(CONFIGURATION)
ie declaration HARDWARE_xxx
.
This can save a lot of time, especially when the number of different hardware is large. In my particular case, I'm talking about embedded firmware that can run on different hardware platforms. The hardware platform is identified using a definition sent to the compiler. Therefore, in the sources we will find many directives#ifdef ... #elif ... #endif
My initial thoughts on this matter was to use a script getdependencies
that will check all C files (including them) to determine which ones depend on the configuration.
So in Makefile
I will find:
-import configurations.d
configurations.d: configuration
getdependencies --configuration=$< $(SRCS)
I did some research on this to see if a similar solution exists because I don't want to reinvent the wheel. Unfortunately, I haven't found anything like this.
Any idea?
source to share
I think the standard way in this case is to define in a makefile
target for each configuration and configure the tools (compiler, linker, librarian, collector, resource linker / compilers, etc.), create the files they produce in a separate directory for each configuration (use the configuration name as the directory name).
This change leads to better side effects for your build routine: you can create a configuration by simply running make
with the appropriate object on the command line (there will be no more file changes). However, the main improvement is that the compiler only recompiles files that have changed (since configurations no longer exchange objects and executables).
source to share