Calling CMake commands within CMake with execute_process

Hypothetically, you’re using CMake and want to do something that CMake documents as one of its list of commands it provides that can be used across different systems:

-E: CMake command mode.

For true platform independence, CMake provides a list of commands that can be used on all systems. Run with -E help for the usage information. Commands available are: chdir, compare_files, copy, copy_directory, copy_if_different, echo, echo_append, environment, make_directory, md5sum, remove, remove_directory, rename, tar, time, touch, touch_nocreate. In addition, some platform specific commands are available. On Windows: comspec, delete_regv, write_regv. On UNIX: create_symlink.

Ok, so you know that you can use the -E command line argument, but you’re not running this command from the command line. You’re trying to run it as part of the configuration step of your build.

That is, you have a CMakeLists.txt file, and within it, you want to add a command to do something at configuration time, such as create a symlink.

So, how do you do so?

Well, it’s not clear from the documentation, but it turns out that the way you are expected to do so is by using execute_process. Here is an example in which I link to my project’s resources directory from my Android project’s assets directory:

EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E create_symlink “${PROJECT_SOURCE_DIR}/resources” “${PROJECT_BINARY_DIR}/android-project/assets/resources”)

Hopefully this tip helps you save some of your hair from being pulled out.

Also, if you would prefer it to happen at build time instead, use add_custom_target.

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>




Follow GBGames on Google Plus and Facebook!

Twitter: gbgames