Makefile: Dependency re-scanning misses some dependencies
With this example:
$ cat ../CMakeLists.txt
cmake_minimum_required(VERSION 3.11)
project(Deps C)
add_library(a a/a.c)
add_library(b b/b.c)
if(INCLUDES)
target_include_directories(a PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_include_directories(b PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
endif()
$ cat ../a/a.c
#include "c.h"
int a(void) { return 0; }
$ cat ../b/b.c
#include "c.h"
int b(void) { return 0; }
$ cat ../c.h
#define C
Run CMake without activating the include directories and then run make
:
$ cmake .. -DINCLUDES=0
$ make
...
Scanning dependencies of target b
[ 25%] Building C object CMakeFiles/b.dir/b/b.c.o
/.../b/b.c:1:10: fatal error: c.h: No such file or directory
As expected it fails to build.
Then run CMake again with the include directories activated and run make
:
$ cmake .. -DINCLUDES=1
$ make
Scanning dependencies of target b
[ 25%] Building C object CMakeFiles/b.dir/b/b.c.o
[ 50%] Linking C static library libb.a
[ 50%] Built target b
Scanning dependencies of target a
[ 75%] Building C object CMakeFiles/a.dir/a/a.c.o
[100%] Linking C static library liba.a
[100%] Built target a
This time the build works. However, if we touch the header only one target rebuilds:
$ touch ../c.h
$ make
[ 50%] Built target b
Scanning dependencies of target a
[ 75%] Building C object CMakeFiles/a.dir/a/a.c.o
[100%] Linking C static library liba.a
[100%] Built target a
The preprocessing dependency of b.c
on c.h
is missing.