CMake missing include directories in install interface

Summary

The CMake reorganization in 9700fc84 breaks the include directories when using the installed CMake project.

Environment

  • Operating System : Ubuntu 22.04
  • Architecture : x64
  • Eigen Version : master
  • Compiler Version : GCC (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
  • Compile Flags : N/A
  • Vector Extension : N/A

Minimal Example

I'm using the standard cmake; cmake --build; cmake --install workflow with a minimal C++ CMake project:

git clone https://gitlab.com/libeigen/eigen.git
pushd eigen
cmake -Bbuild -S. \
    -D CMAKE_INSTALL_PREFIX="$PWD/../install-eigen" \
    -D BUILD_TESTING=Off \
    -D CMAKE_Fortran_COMPILER=""
cmake --build build
cmake --install build
popd
cat > CMakeLists.txt << 'EOF'
cmake_minimum_required(VERSION 3.17)
project(test-eigen LANGUAGES CXX)
find_package(Eigen3 CONFIG REQUIRED)
add_executable(main main.cpp)
target_link_libraries(main PRIVATE Eigen3::Eigen)
EOF
cat > main.cpp << 'EOF'
#include <Eigen/Core>
int main() {}
EOF
cmake -Bbuild -S. \
    -D Eigen3_DIR="$PWD/install-eigen/share/eigen3/cmake"
cmake --build build --verbose
/usr/bin/c++    -MD -MT CMakeFiles/main.dir/main.cpp.o -MF CMakeFiles/main.dir/main.cpp.o.d -o CMakeFiles/main.dir/main.cpp.o -c /tmp/test-eigen/main.cpp
/tmp/test-eigen/main.cpp:1:10: fatal error: Eigen/Core: No such file or directory
    1 | #include <Eigen/Core>
      |          ^~~~~~~~~~~~
compilation terminated.

The same workflow works without issues using c1d63743.

Steps to reproduce

See shell script above.

What is the current bug behavior?

Build failure because of missing include path.

What is the expected correct behavior?

Include path is correctly included in the Eigen3::Eigen CMake target.

Anything else that might help

Possible solution

The specific issue here is that on this line,

https://gitlab.com/libeigen/eigen/-/blob/9700fc847a39e98dfb7bd85331b5206641050e04/CMakeLists.txt#L140

the INCLUDE_INSTALL_DIR variable is not yet initialized. Changing the order around seems to fix at least this issue:

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 21f054656..c281acef8 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -132,17 +132,6 @@ set ( EIGEN_VERSION_MAJOR  ${EIGEN_WORLD_VERSION} )
 set ( EIGEN_VERSION_MINOR  ${EIGEN_MAJOR_VERSION} )
 set ( EIGEN_VERSION_PATCH  ${EIGEN_MINOR_VERSION} )
 
-# Imported target support
-add_library (eigen INTERFACE)
-add_library (Eigen3::Eigen ALIAS eigen)
-target_include_directories (eigen INTERFACE
-  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
-  $<INSTALL_INTERFACE:${INCLUDE_INSTALL_DIR}>
-)
-
-# Export as title case Eigen
-set_target_properties (eigen PROPERTIES EXPORT_NAME Eigen)
-
 # Alias Eigen_*_DIR to Eigen3_*_DIR:
 set(Eigen_SOURCE_DIR ${Eigen3_SOURCE_DIR})
 set(Eigen_BINARY_DIR ${Eigen3_BINARY_DIR})
@@ -203,6 +192,17 @@ endif()
 
 install(DIRECTORY Eigen DESTINATION ${INCLUDE_INSTALL_DIR} COMPONENT Devel)
 
+# Imported target support
+add_library (eigen INTERFACE)
+add_library (Eigen3::Eigen ALIAS eigen)
+target_include_directories (eigen INTERFACE
+  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
+  $<INSTALL_INTERFACE:${INCLUDE_INSTALL_DIR}>
+)
+
+# Export as title case Eigen
+set_target_properties (eigen PROPERTIES EXPORT_NAME Eigen)
+
 install(TARGETS eigen EXPORT Eigen3Targets)
 
 if(EIGEN_BUILD_CMAKE_PACKAGE)
Edited by tttapa