Skip to content

target_include_directories ignores compiler include paths

I'm investigating a TODO in the libc++ build. It appears that target_include_directories ignores compiler include paths.

A minimal example is as follows (I'm assuming Linux but it should be possible to adapt this for other platforms).

First download the latest release of Clang toolchain and extract it to clang+llvm-14.0.0-x86_64-linux-gnu-ubuntu-18.04.

Next, create a helper script:

$ cat >clang++ <<EOF
#!/bin/sh
${PWD}/clang+llvm-14.0.0-x86_64-linux-gnu-ubuntu-18.04/bin/clang++ -stdlib=libc++ \$@
EOF
$ chmod +x clang++

Then, create a minimal CMakeLists.txt with the following content:

cmake_minimum_required(VERSION 3.13.4)
project(Test CXX)

add_library(B INTERFACE)
target_include_directories(B INTERFACE ${CMAKE_CURRENT_LIST_DIR}/clang+llvm-14.0.0-x86_64-linux-gnu-ubuntu-18.04/include/c++/v1 ${CMAKE_CURRENT_LIST_DIR}/clang+llvm-14.0.0-x86_64-linux-gnu-ubuntu-18.04/include/x86_64-unknown-linux-gnu/c++/v1)

add_library(A STATIC a.cpp)
target_link_libraries(A PUBLIC B)

The include directories are the default directories used by Clang, which can you verify by running:

$ touch a.cpp
$ ./clang++ -c -v a.cpp -o a.o
...
#include <...> search starts here:
 /path/to/clang+llvm-14.0.0-x86_64-linux-gnu-ubuntu-18.04/bin/../include/x86_64-unknown-linux-gnu/c++/v1
 /path/to/clang+llvm-14.0.0-x86_64-linux-gnu-ubuntu-18.04/bin/../include/c++/v1
 /path/to/clang+llvm-14.0.0-x86_64-linux-gnu-ubuntu-18.04/lib/clang/14.0.0/include
 /usr/local/include /usr/include/x86_64-linux-gnu /usr/include
...

Now, when you generate the build files using:

$ cmake -GNinja -DCMAKE_CXX_COMPILER=${PWD}/clang++ .

You'll see that inside the generated build.ninja file, the build node for a.cpp doesn't contain either of the two include directories:

...
build CMakeFiles/A.dir/a.cpp.o: CXX_COMPILER__A_ /path/to/a.cpp || cmake_object_order_depends_target_A
  DEP_FILE = CMakeFiles/A.dir/a.cpp.o.d
  OBJECT_DIR = CMakeFiles/A.dir
  OBJECT_FILE_DIR = CMakeFiles/A.dir
  TARGET_COMPILE_PDB = CMakeFiles/A.dir/A.pdb
  TARGET_PDB = libA.pdb
...

Is this behavior intentional? Is there any way to disable this behavior?

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information