Invoking compiler in custom command with properties from a target

Hi folks,

What I’m trying to do is to run the C or C++ preprocessor with -E for each source file associated with a target (using the ninja generator). While I could potentially do this via compile_commands.json I’d prefer to do everything entirely within CMake as a custom target so that it can be parallelised and the build products used by other targets. I’d like each source file to use the target- and source-file-specific target properties so it exactly matches how it was compiled.

What I’ve come up with so to express my intent is:

set(deps)
foreach (file IN LISTS sources)
    set(out "${file}.e")
    add_custom_command(OUTPUT "${out}"
            COMMAND ${CMAKE_C_COMPILER} $<TARGET_PROPERTY:tgt,INCLUDE_DIRECTORIES>
            $<TARGET_PROPERTY:tgt,COMPILE_DEFINITIONS> $<TARGET_PROPERTY:tgt,COMPILE_OPTIONS> -o ${out} -E ${CMAKE_CURRENT_SOURCE_DIR}/${file}
            DEPENDS ${file})
    list(APPEND deps ${out})
endforeach ()
add_custom_target(gendeps DEPENDS ${deps})

But the properties aren’t directly usable as is–they don’t have the necessary compiler flags.

Is there a way to accomplish invoking the compiler with the target-specific properties that would enable this to work portably?

Many thanks,

Roger

I am also doing more or less similar thing in my project. and this is how I am doing.

add_custom_command (OUTPUT ${KTPP_Output}
COMMAND ${CMAKE_CXX_COMPILER} -E -x c -P -C -CC ${pLibIncludes} ${CMAKE_CXX_FLAGS} “$<$CONFIG:Debug:${CMAKE_CXX_FLAGS_DEBUG}>” “$<$CONFIG:MemDebug:${CMAKE_CXX_FLAGS_MEMDEBUG}>” “$<$CONFIG:Release:${CMAKE_CXX_FLAGS_RELEASE}>” “$<$CONFIG:LogRelease:${CMAKE_CXX_FLAGS_LOGRELEASE}>” “$<$CONFIG:Simulation:${CMAKE_CXX_FLAGS_SIMULATION}>” “$<$CONFIG:Metrics:${CMAKE_CXX_FLAGS_METRICS}>” -DTW_PP_KOTLIN=1 ${file} -o ${KTPP_Output}
VERBATIM
DEPENDS ${file}
COMMENT “[KTPP][${CMAKE_CXX_COMPILER}] Building parser with ktpp for ${file}”
COMMAND_EXPAND_LISTS
)

Here I am getting pLibIncludes from target using INCLUDE_DIRECTORIES/INTERFACE_INCLUDE_DIRECTORIES from itself. And CMAKE_CXX_COMPILER would anyway would be available as part of build system generate by cmake.

Does it help ?