Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use CMake to run a C++ program post-build

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.

like image 525
burtmacklin16 Avatar asked Sep 23 '13 14:09

burtmacklin16


1 Answers

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.

like image 148
Angew is no longer proud of SO Avatar answered Sep 22 '22 10:09

Angew is no longer proud of SO