How to only preprocess and link some of the files in cmake

I want some of my files only to be preprocessed and linked(.ld files) and others(.c) has to be fully compiled. How to do this in cmake ?

I think the best way is something involving target_link_options. CMake doesn’t have an abstraction for linker scripts at all and doesn’t have a way to preprocess them. You’ll probably want something like this:

add_custom_command(
  DEPENDS linker.ld
  OUTPUT  "${output}"
  COMMAND
    cpp # probably coming from `find_program`
    $<COMPILE_DEFINITIONS:mytgt> # might need string manip to add `-D` prefixes
    linker.ld
    -o "${output}")
target_linker_options(mytgt
  PRIVATE
    "LINKER:--script=${output}")