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