Saturday, October 30, 2021

[SOLVED] CMake file(GENERATE OUTPUT <output-file> [...]) how to write CONTENT on multiple lines

Issue

Given the following CMakeLists.txt:

project(test_package_lib)
add_library(${PROJECT_NAME})
foo(${PROJECT_NAME})

where foo function is defined like this:

function(foo TARGET)
    set(FILENAME_RELEASE $<TARGET_FILE_BASE_NAME:${TARGET}>.dll)

    # This works!
    file(GENERATE OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/text.log CONTENT "$<TARGET_FILE_NAME:${TARGET}> ${FILENAME_RELEASE}")

    # This produces CMake errors
    file(GENERATE OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/text.log CONTENT [==[
$<TARGET_FILE_NAME:${TARGET}>
${FILENAME_RELEASE}]==])

endfunction()

The first file(GENERATE...) produces the output: test_package_lib.dll test_package_lib.dll

The second file(GENERATE...) call results into the following error:

  Error evaluating generator expression:

    $<TARGET_FILE_NAME:${TARGET}>

  Expression syntax not recognized.

How is it possible to write the CONTENT on multiple lines?


Solution

Inside bracket-argument variable's dereference is not performed. This is why you get line $<TARGET_FILE_NAME:${TARGET}> in the error message: it has unexpanded ${TARGET}.

Instead bracket-argument use "normal" quoted argument splitted over several lines:

file(GENERATE OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/text.log CONTENT
  "$<TARGET_FILE_NAME:${TARGET}>
${FILENAME_RELEASE}"
)


Answered By - Tsyvarev