# Invoking compiler in custom command with properties from a target

**URL:** <https://discourse.cmake.org/t/invoking-compiler-in-custom-command-with-properties-from-a-target/15145>\
**Category:** Code\
**Created:** [September 2, 2025, 6:36am UTC](https://discourse.cmake.org/t/invoking-compiler-in-custom-command-with-properties-from-a-target/15145 "2025-09-02T06:36:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rleigh](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/r/9de053/32.png) [@rleigh](https://discourse.cmake.org/u/rleigh)\
**Post date:** [September 2, 2025, 6:36am UTC](https://discourse.cmake.org/t/invoking-compiler-in-custom-command-with-properties-from-a-target/15145/1 "2025-09-02T06:36:04Z")

</div>

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:

```auto
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

---

<div class="post-metadata">

**Author:** ![siddmohsin](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/d07c76/32.png) [@siddmohsin](https://discourse.cmake.org/u/siddmohsin)\
**Post date:** [September 4, 2025, 5:18am UTC](https://discourse.cmake.org/t/invoking-compiler-in-custom-command-with-properties-from-a-target/15145/2 "2025-09-04T05:18:21Z")

</div>

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 ?
