Issue
I found both this question and href="https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html#debugging" rel="nofollow noreferrer">this documentation entry advising users to "debug" generator expressions by either using the add_custom_target()
command or the file(GENERATE)
command.
However, say you have a CMakeLists.txt
like this:
cmake_minimum_required(VERSION 3.15)
project(Toy VERSION 1.0)
set(gcc_like_cxx "$<COMPILE_LANG_AND_ID:CXX,ARMClang,AppleClang,Clang,GNU,LCC>")
set(msvc_like_cxx "$<COMPILE_LANG_AND_ID:CXX,MSVC>")
and you now want to check for the value of gcc_like_cxx
. Well, according to the documentation, something like
file(GENERATE OUTPUT hahaha CONTENT ${gcc_like_cxx})
or
add_custom_target(genexdebug COMMAND ${CMAKE_COMMAND} -E echo "${gcc_like_cxx}")
could be done, but either of the two produces the same error:
$<COMPILE_LANG_AND_ID:lang,id> may only be used with binary targets to
specify include directories, compile definitions, and compile options. It
may not be used with the add_custom_command, add_custom_target, or
file(GENERATE) commands.
Is there a way to get the value of the generator expression if the expression may not be used with the "standard" debugging options?
Solution
- You need to use the
target
argument infile(GENERATE)
in CMake version3.19
. - You need to add variable prefix to the output file or there will be following error:
CMake Error in CMakeLists.txt:
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:
/home/user/build/hahaha
I run the following CMake file on my server and it works:
cmake_minimum_required(VERSION 3.19)
project(Toy VERSION 1.0)
set(gcc_like_cxx "$<COMPILE_LANG_AND_ID:CXX,ARMClang,AppleClang,Clang,GNU,LCC>")
set(msvc_like_cxx "$<COMPILE_LANG_AND_ID:CXX,MSVC>")
file(GENERATE OUTPUT "${gcc_like_cxx}/log.txt" CONTENT "${gcc_like_cxx}\n" TARGET ${PROJECT_NAME})
add_executable(Toy toy.c)
CMake Gitlab -- file(GENERATE): Add TARGET argument
GitHub issue -- As a result, I added the TARGET argument to file(GENERATE) in CMake 3.19
Answered By - J. Doe Answer Checked By - Katrina (WPSolving Volunteer)