1

I need to run a few commands after I build my project with cmake. The commands will do some transformations to the source files in order to produce a new binary. The whole process would be like this:

  1. compile the target (executable or library) and link it (as it is now)
  2. run tool A on the compiled binary and produce an intermediary output
  3. run tool B on the source files using the intermediary output
  4. compile the target (executable or library) and link it (as it is now)

So, I need to compile twice with the same command, but between the compilations I need to run tool A and B.

All parameters that were passed to the cxx compiler should be passed to the tools A and B. Namely, those commands would look pretty much the same as if we were invoking the compiler, just instead of running the g++/clang++/etc, we would run another tool.

Ex, if the compilation step looks like this (simplified):

g++ source1.cpp source2.cpp -I/some/include -I/some/other/include -fPIC -o target 

then, we should just invoke this:

aaa source1.cpp source2.cpp -I/some/include -I/some/other/include -fPIC -o target bbb source1.cpp source2.cpp -I/some/include -I/some/other/include -fPIC -o target 
4
  • 1
    I do not understand the points. Does compiling the targets depends on output from these tools? If so, what is the point of compiling targets in point 1 if you didn't generate output from these tools? Does point 1 even matter? Why bother to do it at all? Commented Jun 18, 2020 at 10:11
  • Yes, the output from each step is used as input in the next step. Hence, the 1st compilation is also needed. Then in the next steps the source code is modified, and that's why the last compilation is needed as well Commented Jun 18, 2020 at 14:28
  • So you are actually building it twice, ie. you are building 4 targets? What about doing add_library(target1 ...) then add_custom_command(OUTPUTS semething_from_target1 DEPENDS target1 COMMAND aaa $<TARGET_POPERTY ... (get command line options )> $<TARGET_FILE:target1> ) and then add_library(the_real_target1 ....) add_dependencies(the_real_target1 something_from_target1). It would get easier if you would create an minimal reproducible example or at least explain what is that "intermediary output" you are generating and with what purpose. You could also generate compile_commands.json and parse it for options. Commented Jun 18, 2020 at 14:32
  • Thanks. I did it as @KamilCuk suggested. You could write it as an answer so I would mark it as such. BTW, the intermediary steps are modifying the source code based on the compiled binary. That's why they need to be executed in sequence. Commented Jun 29, 2020 at 23:19

1 Answer 1

1

Because it looks like you are compiling the library twice anyway, I would go with two targets.

set(srcs <list of source files> add_library(target1 ${srcs}) add_custom_command(OUTPUTS semething_from_target1 DEPENDS target1 COMMAND aaa $<TARGET_POPERTY ... (get command line options )> $<TARGET_FILE:target1> ) add_library(the_real_target1 ${srcs}) add_dependencies(the_real_target1 something_from_target1) 

If any of the sources are modified or generated, I would recommend writing it that way that they are generated within current binary directory. It's typically done if you want to integrate m4 preprocessor or such into pipeline. Like:

set(srcs <list of source files> add_library(target1 ${srcs}) # Generate files in BINARY_DIR for each source foreach(file IN LISTS srcs) add_custom_command( OUTPUTS ${CMAKE_CURRENT_BINARY_DIR}/${file_name}.c DEPENDS target1 COMMAND aaa $<TARGET_POPERTY ... (get command line options )> $<TARGET_FILE:target1> ${CMAKE_CURRENT_BINARY_DIR}/${file_name}.c ) list(APPEND the_real_target1_srcs ${CMAKE_CURRENT_BINARY_DIR}/${srcs}) endforeach() add_library(the_real_target1 ${the_real_target1_srcs}) 

Ie. keep source directory clean and fresh, and all changed keep in binary directory, that way it will be easy for you to switch between configurations and you will not be so much surprised by auto-modifying files.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.