Ninja Multi-Config: add_custom_command target dependency missing with CMAKE_CROSS_CONFIGS=all
Consider the attached project cross_configs_missing_dep.tar.gz
cmake_minimum_required(VERSION 3.21)
project(blubb)
add_executable(generator generator.cpp)
add_custom_command(
OUTPUT generated.cpp
COMMAND "NUMBER_TO_RETURN=1" "$<TARGET_FILE:generator>"
VERBATIM)
add_executable(example example.cpp ${CMAKE_CURRENT_BINARY_DIR}/generated.cpp)
We have a simple code generator that is referenced in a custom command with $<TARGET_FILE:>
.
Configure the project like this
cmake -G"Ninja Multi-Config" -DCMAKE_CONFIGURATION_TYPES=Release\;Debug <source-dir>
and look at the dependencies of generated.cpp
$ grep "^build generated.cpp" < CMakeFiles/impl-Release.ninja
build generated.cpp | ${cmake_ninja_workdir}generated.cpp: CUSTOM_COMMAND || Release/generator
As expected, we have a dependency to the generator executable.
Now, we configure with CMAKE_CROSS_CONFIGS=all
(in a clean build dir):
cmake -G"Ninja Multi-Config" -DCMAKE_CONFIGURATION_TYPES=Release\;Debug -DCMAKE_CROSS_CONFIGS=all <source-dir>
and look at the dependencies of generated.cpp
:
$ grep "^build generated.cpp" < CMakeFiles/impl-Release.ninja
build generated.cpp | ${cmake_ninja_workdir}generated.cpp: CUSTOM_COMMAND
The dependency to the generator executable is now missing, and the build is extremely likely to fail:
FAILED: generated.cpp /home/knolf/bugreports/cmake/cross_configs_missing_dep/build/generated.cpp
cd /home/knolf/bugreports/cmake/cross_configs_missing_dep/build && NUMBER_TO_RETURN=1 /home/knolf/bugreports/cmake/cross_configs_missing_dep/build/Release/generator
/bin/sh: 1: /home/knolf/bugreports/cmake/cross_configs_missing_dep/build/Release/generator: not found
[2/6] Building CXX object CMakeFiles/generator.dir/Release/generator.cpp.o
ninja: build stopped: subcommand failed.
After removing the environment variable assignment, the dependency is back again, even with CMAKE_CROSS_CONFIGS=all
.
Tested with CMake 3.21.2 and 3.21.4.