CMaking fails due to add_custom_command( ... POST_BUILD ...) not properly handling $<CONFIG> in (generated) source-files' names
I found another problem with generated source-files whose names depend on $<CONFIG>
.
The problem occurs when accessing a target in a build-event (add_custom_command(... POST_BUILD ...)
).
CMakeLists.txt
file to reproduce.
cmake_minimum_required(VERSION 3.19)
project( GeneratorTest_with_POST_BUILD VERSION 1.0.0)
file( GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/Generated_$<CONFIG>.cpp" CONTENT "// Test" )
add_library( lib SHARED )
target_sources( lib PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/Generated_$<CONFIG>.cpp" )
add_custom_command( TARGET lib POST_BUILD
COMMAND "${CMAKE_COMMAND}" "-E" "echo" "$<TARGET_FILE:lib>"
)
When trying to run CMake on the above CMakeLists.txt
file it fails with multi-config generators or with single-config generators that use any other configuration than ""
.
E.g.:
cmake -G "Ninja" -DCMAKE_BUILD_TYPE=Debug /path/to/source/
-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is GNU 9.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
CMake Error at CMakeLists.txt:7 (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
-- Generating done
CMake Generate step failed. Build files cannot be regenerated correctly.
@brad.king The fix should probably also go into CMake 3.19 together with all the other fixes for handling generated source-files.
Edited by Deniz Bahadir