Add hint about INTERFACE_INCLUDE_DIRECTORIES and $<BUILD_INTERFACE>/$<INSTALL_INTERFACE> generator expressions

Hello! I would like to propose the small UX addition about CMake hints then the user has an error like this:

CMake Error in src/library/utils/CMakeLists.txt:
  Target "lib_utils" INTERFACE_INCLUDE_DIRECTORIES property contains
  path:

    "/home/user/proj/library/src/utils/include"

  which is prefixed in the source directory.

This error means that user exports that target and forget to add the $<INSTALL_INTERFACE> generator expression to the target_include_directories(). We could add a hint how to fix this error, and it could helps users to fix this error:

CMake Error in src/library/utils/CMakeLists.txt:
  Target "lib_utils" INTERFACE_INCLUDE_DIRECTORIES property contains
  path:

    "/home/user/proj/library/src/utils/include"

  which is prefixed in the source directory.

Hint: Replace the relative path "include" to $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>
Hint: Add the $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDE_DIR}> generator expression to adjust the includes path on install stage.

I also think that hint could accurately trim off the similar parts of include dirs (e.g. include/libname should be replaced by ${CMAKE_INSTALL_INCLUDE_DIR}/libname), and this hint could mention the GNUInstallDirs CMake module.

Thanks for your attention!

2 Likes

I could also make a PoC patch to CMake upstream, if you like this idea :blush:

2 Likes

The proposed error message LGTM, though the hints should still be indented and wrapped like the rest of the message. Please see CONTRIBUTING.rst for instructions to propose a change in a merge request.

1 Like