Skip to content

CMaking fails due to install(EXPORT) not properly handling $<CONFIG> in (generated) source-files' names

When trying to install(EXPORT) an export-set for a (shared) library that is linked against another OBJECT library which itself compiles a generated source-file (whose name contains the $<CONFIG> generator expression), then CMake fails and complains that it cannot find the generated source-file.
However, it is ignoring the value of $<CONFIG> and therefore looking for the wrong file.

As always, examples are simpler to understand:

cmake_minimum_required(VERSION 3.18)
project(Test VERSION 1.0.0)

file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/Generated_$<CONFIG>.cpp" CONTENT "// content")
add_library(test-obj OBJECT)
target_sources(test-obj PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/Generated_$<CONFIG>.cpp")

add_library(test-lib SHARED)
target_link_libraries(test-lib PRIVATE test-obj)

install(TARGETS test-lib EXPORT MyExport)
install(EXPORT MyExport DESTINATION /tmp)  # This triggers the error!

Try building that with the following command:

mkdir build && cd build
cmake -DCMAKE_BUILD_TYPE=Debug ..

And cmake will configure fine but complain when generating with:

CMake Error at CMakeLists.txt:5 (add_library):
  Cannot find source file:

    .../build/Generated_.cpp

  Tried extensions .c .C .c++ .cc .cpp .cxx .cu .m .M .mm .h .hh .h++ .hm
  .hpp .hxx .in .txx .f .F .for .f77 .f90 .f95 .f03 .ispc

Of course, if you do not set CMAKE_BUILD_TYPE it succeeds. But that is not what I want.

Note: This error is not specific to OBJECT libraries. You could make test-obj a STATIC library (and give test-lib an additional source-file) and you will trigger the same error.

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