3

We have some source processing tools that generate things like raw assembly files from multiple 'snippets'.

When using these tools from make we can ensure up-to-date builds by having the source processing tools emit 'dependency files', just like gcc would with its -MD flag.

For example, let's say I have a assembly template file Frob.asmtmpl, and a templating engine called asm_templater that acts like a beefed up C pre-processor.

We run asm_templater -d Frob.asmtmpl which produces Frob.s and Frob.d. The latter is a makefile dependency snippet like the following:

Frob.s: Frob.asmtmpl ThingIncludedByFrob.asmincl OtherThingIncluded.asmincl

In a makefile based build system, we would include(Frob.d) if it existed, to tell make what the actual deps of Frob.asmtmpl are.

How do we do something similar for CMake? It seems that the two-pass nature of CMake (Generate then Build), and needing to support multiple generation engines leads to the conclusion that there must be some sort of native CMake way to do it that I haven't figure out yet.

I need to indicate to CMake somehow that there's a dependency between Frob.s. and ThingIncludedByFrob.asmincl, but that's all determined by the contents of Frob.asmtmpl and so need to be extracted by a tool.

Matthew G.
  • 1,298
  • 10
  • 24
  • The key is that you have to know the outputs of your "source processing tools". If the output names do vary (e.g. depending on the input name), it get's difficult (you have to implement the input to output name conversion in CMake). I haven't searched for similar questions, but I'm pretty sure have seen this before. Can you please give more details of things you have already tried and why they didn't meet your needs? – Florian Jul 18 '17 at 14:22
  • The outputs are easy enough to describe, but it's really the computed deps. I'm going to edit the question for clarity: – Matthew G. Jul 18 '17 at 14:46
  • Edited to clarify, I hope. – Matthew G. Jul 18 '17 at 14:52
  • Is the point of the question is to avoid manually maintaining the dependencies in a CMakeLists.txt file? – starball Jan 01 '23 at 07:46
  • I'm in the exact situation. My current solution is for the tool to generate a cmakefile with an `add_custom_command` that contains all the dependencies and outputs of the command. However, I still need to manual step (i.e. run the tool outside the build) to produce the generated cmakefile. – Lindydancer Jan 01 '23 at 09:51

1 Answers1

0

You may be interested in / looking for CMake's add_custom_command command. It has two signatures:

The first signature is for adding a custom command to produce an output. A target created in the same directory (CMakeLists.txt file) that specifies any output of the custom command as a source file is given a rule to generate the file using the command at build time.

The second signature adds a custom command to a target such as a library or executable. This is useful for performing an operation before or after building the target. The command becomes part of the target and will only execute when the target itself is built. If the target is already built, the command will not execute.

So you can create a custom command that has COMMAND asm_templater -d Frob.asmtmpl, OUTPUTS or BYPRODUCTS set to Frob.s, and DEPENDS ThingIncludedByFrob.asmincl OtherThingIncluded.asmincl.

I'm aware that a simple such approach could still involve maintaining the dependencies in a CMakeLists.txt file. I just thought it would be better to mention this than to not. I'm not aware of depfile processing at the CMake level. Usage of add_custom_command can form the basis for a more complicated setup that generates the custom command by parsing a depfile.

starball
  • 20,030
  • 7
  • 43
  • 238