Implicit include path and changing value of CPATH
If the value of CPATH changes between the first call of cmake in a build directory and subsequent builds, the ignored include directories are incorrect.
Given:
==> CMakeLists.txt <==
cmake_minimum_required(VERSION 3.16.3)
project(test_cpath)
add_executable(test test.cxx)
target_include_directories(test PRIVATE include)
==> test.cxx <==
#include <some.h>
int main() {}
==> include/some.h <==
Building with:
mkdir build
CPATH=`realpath ../include/` cmake ..
cmake ..
make
Fails with:
[ 50%] Building CXX object CMakeFiles/test.dir/test.cxx.o
.../test_cpath/test.cxx:1:10: fatal error: some.h: No such file or directory
1 | #include <some.h>
| ^~~~~~~~
If one runs cmake with the same value for CPATH both times (either with or without), it works correctly. But if the first run of cmake is with CPATH the include directory is captured as implicit include path and then later runs of cmake without CPATH will fail to generate correct build scripts because the include path is excluded.
There is an earlier very closely related issue #19291 (closed). And !3395 (merged) was merged to fix the regression.
The problem is that how the fix decides whether an include file should be excluded. It removes those directories from the exclusion list which are currently in CPATH. But the implicit include paths are captures once the first time cmake runs. I believe the correct solution is to record the value of CPATH when the implicit include path is recorded and remove those from the list of ignored include directories.