Avoid quoting in CMake add_custom_command - cmake

I'm trying to create a simple batcher, which runs a script over a set of files.
So, after globbed files, I've created the following custom command to execute the script:
add_custom_command(OUTPUT ${RESOURCE_GFX} COMMAND ${EXE_GFX_EXPORT} ${GFX_EXPORT_PARAMETERS} ${RESOURCE_SWF})
where EXE_GFX_EXPORT is the script program, something like C:\Program Files (x86)\Scaleform\GFx SDK 3.1\Bin\gfxexport.exe; RESOURCE_SWF is the file that the script runs on; and GFX_EXPORT_PARAMETERS are script's parameters, something in the form of -i DDS -share_images -qp.
CMake "translates" this custom command in:
"C:\Program Files (x86)\Scaleform\GFx SDK 3.1\Bin\gfxexport.exe" "-i DDS -share_images -qp" "C:\path\to\file.swf"
but gfxexport.exe can't handle parameters surrounded by double quotes. Is there a way to avoid CMake automatically puts them around GFX_EXPORT_PARAMETERS variable?
Thanks guys,
Raffaele.

Try using the separate_arguments function on the parameter GFX_EXPORT_PARAMETERS before invoking add_custom_command:
separate_arguments(GFX_EXPORT_PARAMETERS_LIST WINDOWS_COMMAND "${GFX_EXPORT_PARAMETERS}")
add_custom_command(OUTPUT ${RESOURCE_GFX} COMMAND ${EXE_GFX_EXPORT} ${GFX_EXPORT_PARAMETERS_LIST} ${RESOURCE_SWF})

Related

CMake call add_subdirectory within custom command

I'm working with a code generator that produces C++ and a CMakeLists.txt file, unfortunately I cannot use this in my main CMakeLists.txt file for testing purposes.
For example you have the following CMakeLists.txt file:
project(SomeProject CXX C)
add_custom_command(OUTPUT ${SRCS}
COMMAND ${CODEGEN_CLI_PATH} -i "${INPUT}" -o "${OUT}"
COMMENT "Generating sources"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
VERBATIM
)
add_custom_target(CODEGEN
DEPENDS
${SRCS}
)
# Needs to be executed after the custom command
add_subdirectory(${GENERATED_CMAKE_LISTS_LOCATION})
Is it possible to use functions such as add_subdirectory only after you execute custom commands for a particular target, such as CODEGEN?
I've already tried to execute it by adding an extra line to the existing custom command:
COMMAND ${CMAKE_COMMAND} -D DIR=${GENERATED_CMAKE_LISTS_LOCATION} -P add_subdirectories.cmake
Unfortuantly this doesn't work because it isn't allowed to execute functions like add_subdirectory in script mode.
Neither I can manage to call custom made functions (that are executing add_subdirectory) from add_custom_command that are located in the same file.
Nope, it is not possible. The add_subdirectory command is run during configuration step, while CODEGEN is a target that runs during build.
You seem to be doing something wrong, so the only advice I can give you is to use execute_process command to run commands you need. The execute_process command is executed during configuration stage, so it will be able to generate files you need before add_subdirectory.
But again, please describe your problem, why do you want CMake to do that.
I have a huge fixed unsigned char array that I compiled into a static library. The way I work around it is by:
if(NOT EXISTS ${PATH_TO_FOLDER}/smeagol.a)
add_subdirectory(smeagol)
endif()
I'm still looking for a nicer kung-fu way to do it using cmake. I feel that its out there, and I will update this answer once i find it.

cmake run script for install target?

I have a project where "installing" the code is not quite as simple as just copying some files. With a traditional Makefile, I would just create a make install target that runs a series of shell commands to do what I need.
But googling around has resulting in no examples of this (some things close, but not quite... i think). So basically, I want a custom command, that depends on the target executables, but produces nothing and runs a script that need not be portable to accomplish the "install"
Anyone have any examples of something like this?
CMake's install command allows for custom scripts. See the official documentation: install - Custom Installation Logic:
install([[SCRIPT <file>] [CODE <code>]]
[COMPONENT <component>] [...])
The SCRIPT form will invoke the given CMake script files during installation. If the script file name is a relative path it will be interpreted with respect to the current source directory. The CODE form will invoke the given CMake code during installation. Code is specified as a single argument inside a double-quoted string. For example, the code
install(CODE "MESSAGE(\"Sample install message.\")")
will print a message during installation.
To run custom shell script (or whatever program), combine install(CODE ...) with execute_process:
install(CODE "execute_process(COMMAND my_script.sh)")
This worked for me: use add_custom_target, then add the main target as a dependency to the custom target target.
# create custom target for setcap to be executed
add_custom_target(setcap ALL
WORKING_DIRECTORY ${OUTPUT_DIR}/bin
COMMAND ${CMAKE_COMMAND} -E 'sudo setcap cap_net_raw,cap_net_admin+eip ${}/bin/<executable name>)
# create a dependency on the custom target for main target, setcap depends on ${proj_name}
add_dependencies(setcap ${proj_name})

Run custom shell script with CMake

I am having the following directory structure:
/CMakeLists.txt
/component-a/CMakeLists.txt
/...
/component-b/CMakeLists.txt
/...
/doc/CMakeLists.txt
/create-doc.sh
The shell script create-doc.sh creates a documentation file (doc.pdf). How can I use CMake to execute this shell script at build time and copy the file doc.pdf to the build directory?
I tried it by using add_custom_command in the CMakeLists.txt file inside the directory doc:
add_custom_command ( OUTPUT doc.pdf
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/create-doc.sh
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/)
Unfortunately the command is never run.
I also tried execute_process:
execute_process ( COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/create-doc.sh
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/ )
Now the script is executed during the configuration phase, but not at build time.
You got almost there with add_custom_command. This is indeed the correct way to tell CMake how to generate a file. However, CMake will only run that when something depends on that file.
When, as in your case, the file itself is the end product and is not used any further by subsequent build steps, the usual approach is to create a custom target to drive the relevant custom command(s):
add_custom_target(
BuildDocs ALL
DEPENDS doc.pdf
)
This (custom target driver for custom commands) is a very common idiom in CMake.
You can of course play around with arguments for add_custom_target (e.g. ALL, COMMENT) as it suits you.

CMake: execute a macro/function as the command of add_custom_command

I'm using an external library which provides a CMake function for automatic code generation, to be used in my CMakeLists. The problem is that whenever I modify a CMakeLists then the function is run again, triggering the recompilation of the newly generated but unchanged sources. I'd need something like add_custom_command with the possibility to specify the CMake function as COMMAND instead of an executable, so that the function is run only if the automatically generated files are not already present.
Is this feasible? If not, does it exist another way to obtain the same result?
Thanks.
Take a look to this SO post.
You can call your function in a separate CMake script, call this script with add_custom_target and cmake -P then add a dependency to your binary :
add_custom_target(run_script COMMAND ${CMAKE_COMMAND} -P separate_script.cmake)
add_executable(your_binary ...)
# or add_library(your_binary ...)
add_dependencies(your_binary run_script)
Is there a way to pass a parameter to the separate_script.cmake?
You can use the cmake variables to pass values when you call the script e.g.
"COMMAND ${CMAKE_COMMAND} -DPARAM=value -P separate_script.cmake"
To prevent that function to run, just wrap it into if:
if(NOT EXISTS ${CMAKE_BINARY_DIR}/blah-blah/generated.cpp)
run_your_provided_command(BLAH_BLAH)
endif()
Easy!
Update: To run it when config file has changed just use little more complicated condition:
if(
NOT EXISTS ${CMAKE_BINARY_DIR}/blah-blah/generated.cpp OR
${CMAKE_SOURCE_DIR}/blah-blah.config IS_NEWER_THAN ${CMAKE_BINARY_DIR}/blah-blah/generated.cpp
)
...
and use add_dependencies command to make sure your binary will be rebuild in case of config file modifications:
add_executable(
YourBinary
...
${CMAKE_BINARY_DIR}/blah-blah/generated.cpp
)
add_dependencies(YourBinary ${CMAKE_SOURCE_DIR}/blah-blah.config)

Access CMake cache variable from CTest script

My project links to a third-party library that comes with a valgrind suppression file, as well as a CMake script. The script stores the location of the suppression file in a CMake cache variable.
I have written a CTest script that runs continuous tests on my project and submit to a dashboard. I would like to use the suppression file during the memory-checking stage. Unfortunately, the CTest script does not seem to know anything about the CMake cache. How can I access the CMake cache variable from my CTest script?
You can't directly access CMake cache variables from the ctest -S script.
However, you could possibly:
include the third party CMake script in the ctest -S script with
"include" (after the update step, so the source tree is up to date)
read the CMakeCache.txt file after the configure step to pull out the
cache variable of interest
add code to your CMakeLists.txt file to write out a mini-script that
contains just the information you're looking for
For (1), the code would be something like:
include(${CTEST_SOURCE_DIRECTORY}/path/to/3rdParty/script.cmake)
This would only be realistically possible if the script does only simple things like set variable values that you can then reference. If it does any CMake-configure-time things like find_library or add_executable, then you shouldn't do this.
For (2):
file(STRINGS ${CTEST_BINARY_DIRECTORY}/CMakeCache.txt result
REGEX "^CURSES_LIBRARY:FILEPATH=(.*)$")
message("result='${result}'")
string(REGEX REPLACE "^CURSES_LIBRARY:FILEPATH=(.*)$" "\\1"
filename "${result}")
message("filename='${filename}'")
For (3):
In the CMakeLists.txt file:
file(WRITE "${CMAKE_BINARY_DIR}/mini-script.cmake" "
set(supp_file \"${supp_file_location}\")
")
In your ctest -S script, after the ctest_configure call:
include("${CTEST_BINARY_DIRECTORY}/mini-script.cmake")
message("supp_file='${supp_file}'")
# use supp_file as needed in the rest of your script
The tests should be configured in your CMakeLists.txt file via the command ADD_TEST(). The CTest script then simply calls ctest_test() to run all the configured tests. By doing it this way, you get access to the cache variables, plus you can just run make test at any point to run the tests. Save the CTest script for the nightly testing and/or Dashboard submissions.