Generating linker command line arguments to a file

A link_commands.json file would solve my problem. I could create a dummy target that doesn’t actually get built at the CMake level (EXCLUDE_FROM_ALL), but is configured with all the correct dependencies and options. Then I could grab the options from compile_commands.json and link_commands.json based on the dummy source file and taget/output file names.

That said, my ideal would be a set of generator expressions (which expand to cmake lists, to avoid space safety issues, which appear to be a minor problem with compile_commands.json):

  • $<TARGET_COMPILER_COMMAND:target,source_file>: the compiler command with no options, e.g., gcc or ccache;gcc
  • $<TARGET_COMPILER_OPTIONS:target,source_file>: the command-line arguments to the compiler, including -D, -o, and input file names.
  • $<TARGET_LINKER_COMMAND:target>: the linker command with no options, e.g., gcc or gcc;-shared.
  • $<TARGET_LINKER_OPTIONS:target>: the command line arguments to the linker.

I would love to split the _OPTIONS expressions into FLAGS, INPUTS, and OUTPUTS, but I suspect that may not be possible in all cases since flags and inputs/outputs may need to be interspersed because argument order matters.

EDIT: My assumption is that the context you mention (shell and linker) can be extracted from the main generator in use and the target specified in the generator expressions. This information must be available at generation time. Clearly it’s not possible to compute this stuff at main script execution time in the cmake architecture.