8

I have an application that is written in C++ that I use CMake to build and release binaries.

I'd like to have the CMakeLists.txt script compile and run a CPP file that is used to timestamp and encrypt a license file after it has built the binaries for the application. I've seen examples of running the execute_process command such as this:

execute_process(COMMAND "gcc -o foo foo.cpp"
                WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
execute_process(COMMAND "./foo"
                WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
                RESULT_VARIABLE MY_FOO_VAR)

I am using Visual Studio 2010 to do the build on Windows. The problem is that I'm not sure how to get CMake to run a program during VS's build process. I want the last thing in the build operation to be the license file timestamp, but I don't really know enough about VS or CMake to get this set up.

TemplateRex
  • 69,038
  • 19
  • 164
  • 304
burtmacklin16
  • 715
  • 2
  • 13
  • 31

1 Answers1

16

You say you want to run the file after your build. execute_process() runs at CMake-time, not at build time. What you're looking for would be add_custom_command():

add_executable(LicenseStamper stamper.cpp)

add_custom_command(
  OUTPUT stamped_file.lic
  COMMAND LicenseStamper any other arguments
  DEPENDS any/dependency.file
  COMMENT "Stamping the license"
  VERBATIM
)

add_custom_target(
  StampTheLicense ALL
  DEPENDS stamped_file.lic
)

The custom command will run the executable (and build it first, if necessary). The custom target will drive the custom command - it depends on the command's output, so when the target is built, it will require its dependency to be built, causing the custom command to run.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • 1
    Both above commands may be replaced by a single `add_custom_target()` which takes a `COMMAND` keyword and can issue any task, being both a "driving target" and a task caller itself (likewise LicenceStamper will be a required dependency target). `add_custom_command()` is redundant in this scenario – bloody Apr 22 '20 at 13:24