I work on a project comprised of a couple dozen shared libraries, each of which has many associated unit tests. Many libs are also dependent on other libs because a lib for some specific functionality will use the code from one of the more common libs. And finally of course there are the production executables which depend on the libs.
There is no question that a change in the API (a header file) of some core common lib should trigger a major recompilation of nearly the entire system. But often there is only a change in the implementation, and the only file compiled is the modified .cxx, and in theory just the modified lib would need to be linked - thanks to dynamic linking there should be no need to relink anything else. But CMake goes ahead and does it anyway: after relinking the lib it relinks all the unit tests associated with that lib. Then it relinks all the libs in that lib's dependency tree and all their unit tests. Finally it relinks the production executables. Due to the scale of the project this takes a lot of precious time.
I have reproduced this behavior with a simple project based on this minimal example (comments removed for brevity and lib changed to shared). My system is Ubuntu 16 on an Intel PC and my CMake version is 3.5.1.
Start with an empty directory and create these files:
CMakeLists.txt
cmake_minimum_required (VERSION 2.8.11)
project (HELLO)
add_subdirectory (Hello)
add_subdirectory (Demo)
Demo/CMakeLists.txt
add_executable (helloDemo demo.cxx)
target_link_libraries (helloDemo LINK_PUBLIC Hello)
Demo/demo.cxx
#include "hello.h"
int main() { hello(); }
Hello/CMakeLists.txt
add_library (Hello SHARED hello.cxx)
target_include_directories (Hello PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
Hello/hello.h
void hello();
Hello/hello.cxx
#include <stdio.h>
void hello() { printf("hello!\n"); }
now run the commands:
mkdir build
cd build
cmake ../
make
You may now execute Demo/helloDemo
and see hello!
.
Now, touch
Hello/hello.cxx and make
again. You will see that the helloDemo
executable is relinked ("Linking CXX executable helloDemo
"). Even if hello.cxx is modified to print a different string the relinked executable remains binary identical so really the relinking was unnecessary.
Is there a way to prevent these redundant build actions?