add_custom_command: workaround for lack of genex support in OUTPUTS
I am aware of #12877 (closed)
I was trying to see if it's possible to circumvent the restriction of genexes in OUTPUT
by using them in COMMAND
instead.
Here's my project.
It uses 2 commands one to create a regular non genex-named file (listed in OUTPUT
), and then another command to copy it to a different file name that contains genexes (without listing it in OUTPUT
).
cmake_minimum_required(VERSION 3.16)
project(app LANGUAGES CXX)
set(main_path "${CMAKE_CURRENT_BINARY_DIR}/main.cpp")
set(in_file_path "${CMAKE_CURRENT_BINARY_DIR}/in.txt")
set(preliminary_file_path "${CMAKE_CURRENT_BINARY_DIR}/preliminary.txt")
set(out_file_path "${CMAKE_CURRENT_BINARY_DIR}/$<TARGET_FILE_BASE_NAME:app>.txt")
file(WRITE "${main_path}" "int main() {return 0;}")
file(WRITE "${in_file_path}" "in_content")
add_executable(app "${main_path}")
add_custom_command(
OUTPUT "${preliminary_file_path}"
DEPENDS "${in_file_path}"
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"${in_file_path}"
"${preliminary_file_path}"
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"${preliminary_file_path}"
"${out_file_path}"
VERBATIM
)
set_source_files_properties("${out_file_path}" PROPERTIES GENERATED TRUE)
target_sources(app PRIVATE "${out_file_path}")
and the configure output
-- Configuring done
CMake Error at CMakeLists.txt:25 (target_sources):
Error evaluating generator expression:
$<TARGET_FILE_BASE_NAME:app>
Expressions which require the linker language may not be used while
evaluating link libraries
CMake Error at CMakeLists.txt:25 (target_sources):
Error evaluating generator expression:
$<TARGET_FILE_BASE_NAME:app>
Expressions which require the linker language may not be used while
evaluating link libraries
-- Generating done
CMake Generate step failed. Build files cannot be regenerated correctly.
Is the error just another manifestation of #12877 (closed) ? Is this expected to work without fixing the previously mentioned issue?
At least to me the error message seems confusing and non-descriptive. There are no link libraries involved.
Edited by Brad King