`file(GENERATE)` fails to resolve `$<CONFIG:...>` in OUTPUT file path with multi-config generators
Reproduced in CMake 3.23, 3.24, and 3.25. Using 3.25 rc2 binary. I did not try earlier versions.
Reproduced with Visual Studio 17 2022 and Ninja Multi-Config generators. The regular Ninja generator does not have this issue. This is an issue with multi-config generators.
If you have
file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/my_generated_file_$<$<CONFIG:Debug>:Debug>$<$<CONFIG:Release>:Release>.txt" CONTENT "Test $<$<CONFIG:Release>:Test>")
in a project that does not define manually set CMAKE_CONFIGURATION_TYPES
, the following warning will be produced:
Evaluation file to be written multiple times with different content. This is generally caused by the content evaluating the configuration type, language, or location of object files.
This is because the $<CONFIG:...>
fails to evaluate, and it tries to create "${CMAKE_CURRENT_BINARY_DIR}/my_generated_file_.txt" for each configuration. However, $<CONFIG:...>
in the content seems to evaluate successfully, given that the warning says it produced different content for different configurations.
This can be fixed by putting
set(CMAKE_CONFIGURATION_TYPES Release Debug)
before the first call to project()
.
This wasn't immediately obvious from the error, and a user that did not have knowledge of this variable may have a difficult time figuring out what to do.
In the documentation for CMAKE_CONFIGURATION_TYPES, it states
This variable is initialized by the first project() or enable_language() command called in a project when a new build tree is first created. If the CMAKE_CONFIGURATION_TYPES environment variable is set, its value is used. Otherwise, the default value is generator-specific.
If each generator defines default values, it sounds like it shouldn't be necessary to define CMAKE_CONFIGURATION_TYPES in order for file(GENERATE)
to use $<CONFIG:...>
in its OUTPUT file path. Plus, CONTENT seems to evaluate successfully.
This seems like a bug.