Add quotation mark in CMake string - cmake

I am using CMake to create and build my project solution. i am using the following command to add a post build event to copy a .tlb from the local bin to the program bin.
ADD_CUSTOM_COMMAND(TARGET ${PROJECT_NAME} POST_BUILD COMMAND xcopy /D /Y "${CMAKE_SOURCE_DIR}LocalBin\\example.tlb" "${CMAKE_SOURCE_DIR}ProgramBin\\$<CONFIGURATION>\\")
When this adds the command to the project properties , it is added as
xcopy /D /Y LocalBin\example.tlb ProgramBin\Debug\
However this gives me an error. Exited with Code 4.
If i go into the project properties and hack the command line and change it to add " "
xcopy /D /Y "LocalBin\example.tlb " "ProgramBin\Debug\"
It works.
Is there a way i can change the CMake add custom command to include the " " in the actual command line so it will work and there is no need to manually change the project properties.

You have to escape the quotation mark inside the string. Escaping is done in CMake with a back slash. So adding a quotation mark to your string, add \".
Quoting the CMake documentation https://cmake.org/cmake/help/v3.4/manual/cmake-language.7.html#escape-sequences
A \ followed by a non-alphanumeric character simply encodes the literal character without interpreting it as syntax
In your case you'll end up with
"\"${CMAKE_SOURCE_DIR}LocalBin\\example.tlb\""
instead of
"${CMAKE_SOURCE_DIR}LocalBin\\example.tlb"

After struggling with this for some time, I found a workable solution using generator expressions:
ADD_CUSTOM_COMMAND(TARGET ${PROJECT_NAME} POST_BUILD COMMAND xcopy /D /Y $<1:"${CMAKE_SOURCE_DIR}LocalBin\\example.tlb"> $<1:"${CMAKE_SOURCE_DIR}ProgramBin\\$<CONFIGURATION>\\">)
This is an open bug in CMake.

Sadly this works only for the command arguments, but not for the command itself. This is really no fun when you have to call for example shader compiler that is in the Program Files (path with space in it) and prebuild rule fails. To work around this problem I created batch file on the fly with cmake file(WRITE ...) command and then call this batch file with correctly escaped path to the actual shader compiler I want to execute. In the batch I have a line with %1
Solution for my problem
file(WRITE Shaders/compile_shaders.bat "\
%1 /ESolveVMF /Tcs_5_0 /DTGSize_=16 /Fh \"${CMAKE_CURRENT_LIST_DIR}/Shaders/GenerateRoughnessMaps.inc\" \"${CMAKE_CURRENT_LIST_DIR}/Shaders/GenerateRoughnessMaps.hlsl\"\n\
%1 /ESolveVMFReconstructNormal /Tcs_5_0 /DTGSize_=16 /Fh \"${CMAKE_CURRENT_LIST_DIR}/Shaders/GenerateReconstructNormalRoughnessMaps.inc\" \"${CMAKE_CURRENT_LIST_DIR}/Shaders/GenerateRoughnessMaps.hlsl\"")
add_custom_command(TARGET ${CURRENT_MODULE_NAME} PRE_BUILD
COMMAND "${CMAKE_CURRENT_LIST_DIR}\\Shaders\\compile_shaders.bat" "\"$(WindowsSdkDir)bin\\x86\\fxc.exe\""
COMMENT "Compiling shaders")

Related

CMake MSVC: How to add a custom command for a single configuration? (Debug)

I would like to add the following command only for the Debug configuration:
if (MSVC)
add_custom_command(TARGET my_target PRE_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${src} ${dst})
endif()
src and dst are just path strings.
I tried to adapt the code using examples I've seen in related answers here, here and here.
But non of them worked. Either the specific case is slightly different or they simply execute the same command using a different argument for other configurations.
All of my adaptations attempts caused the command to be malformed or simply did not do the trick.
I would like other configurations to not have to add a command at all.
How can this simple task be achieved?
I like to nullify command using true
add_custom_command(TARGET ${target_name} PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E $<IF:$<CONFIG:Debug>,copy,true> ${src} ${dst}
COMMAND_EXPAND_LISTS)
if not in Debug you should have cmake -E true ... i.e. the true will turn the command as no op.
true Do nothing, with an exit code of 0.
src: https://cmake.org/cmake/help/latest/manual/cmake.1.html#run-a-command-line-tool
note: available since 3.16
src: https://cmake.org/cmake/help/latest/release/3.16.html#command-line
I managed to get it working using a dummy empty string for configuration other than Debug:
string(
APPEND copy_cmd
"$<IF:$<CONFIG:Debug>,"
"${CMAKE_COMMAND};-E;copy;${pdb_src};${pdb_dst},"
""
">"
)
add_custom_command(TARGET ${target_name} PRE_BUILD COMMAND "${copy_cmd}" COMMAND_EXPAND_LISTS)
Note that the "" string is basically the 'else' of the if. it nullifies the command.

What cmake command will copy a directory of files to a directory in a post build step

I have a set of resource files that have nothing to do with the build steps of GCC or some other compiler's output. I need them copied from a folder in my project to the cmake build output folder. The goal is for the executable, when run from the build output folder, can see the resources.
How do people typically copy and install resources in cmake builds? Additionally, I want them copied regardless of changes in the build and I want it executed every time I run some cmake command, like build. See below for what I tried to solve this issue.
For example:
I have a bunch of shader files that I want copied. All shaders/* files should be copied into a directory in the build output called "shaders", because that's where the executable for the program lives.
file(GLOB out shaders/*)
foreach (o ${out})
message("${o} was copied to shaders")
file(COPY ${o} DESTINATION shaders)
endforeach ()
This only works sometimes, like when I reload the CMake project, e.g.:
/opt/clion-2021.2.3/bin/cmake/linux/bin/cmake \
-DCMAKE_BUILD_TYPE=Debug -DCMAKE_DEPENDS_USE_COMPILER=FALSE \
-G "CodeBlocks - Unix Makefiles" \
/home/hack/glad
Also, it doesn't execute "POST_BUILD", so the lastest version of the shaders/a.vert file doesn't get copied to the shaders/ directory in the output.
I tried using this, too, but it gave me some headaches:
add_custom_command(TARGET my-executable POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy shaders/* shaders)
I think there's something incorrect with that above, because it wasn't run every POST_BUILD if the build's code didn't change. I don't care if the build's code doesn't change because the files in shaders/* could have changed and should be copied regardless of cmake determining if there was a change in my-executable.
This gist on github was very helpful, here but the gist that applies to my question is included below.
add_custom_target(bar
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/shaders
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/shaders ${CMAKE_BINARY_DIR}/shaders
COMMENT "copying ${CMAKE_SOURCE_DIR}/shaders to ${CMAKE_BINARY_DIR}/shaders"
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
The above code creates a bar cmake target that can be run separately with Make bar; then, if you add another target that requires those resources (the shaders above are resources for this other executable) then you can tie the dependency together using add_dependencies like this:
add_executable(a
main.c
opengl.c)
add_dependencies(a bar)
Now, every time the a target is run, the bar target is run as well, which has the effect of creating the directory and copying the files.
This was good enough for me, but to finish up, you can use this to create the files in a post build step after the other dependency is finished running:
add_custom_command(TARGET bar
# Run after all other rules within the target have been executed
POST_BUILD
COMMAND echo "executing a POST_BUILD command"
COMMENT "This command will be executed after building bar"
VERBATIM
)
Note that ${CMAKE_COMMAND} in the above examples of add_custom_command is a variable that points to the cmake executable, and you can run cmake -E to see the very helpful list of commands that come with cmake.
YIKES the post build step is only running after bar's target is built, not a's target. Hopefully, somebody can help me answer this better. I would still like to know how to copy files after a target is built, unless that's completely unnecessary and nobody should ever want to do that.

CMake add_custom_command() POST_BUILD generator expression expansion not working

I want to run a POST_BUILD action after the build (but only in the Debug configuration).
After reading add_custom_command docs and a possible solution I understood that I can "wrap" my COMMAND into $<CONFIG:Debug> generator expression (to be sure it's "empty" in Release mode).
I tried the following:
cmake_minimum_required(VERSION 3.18)
project(post-build CXX)
file(WRITE main.cxx "int main() {}")
add_executable(foo main.cxx)
add_custom_command(
TARGET foo POST_BUILD
COMMAND $<$<CONFIG:Debug>:${CMAKE_COMMAND} -E echo "hi there from debug build">
)
But this gives me the CMake configure-time warnings and a hard failure during a build-time (using Ninja generator):
(...) && "$<1:C:\Program Files\CMake\bin\cmake.exe" -E echo "hi there from debug build" >""
[build] The system cannot find the path specified.
[build] ninja: build stopped: subcommand failed.
[build] Build finished with exit code 1
I tried many possible quotes combinations (including escaped quotes):
COMMAND $<$<CONFIG:Debug>:"${CMAKE_COMMAND} -E echo \"hi there from debug build\"">
and
COMMAND "$<$<CONFIG:Debug>:${CMAKE_COMMAND} -E echo \"hi there from debug build\">"
etc.
But even though it removed the configure-time warning, it still yields a hard error during the build-time.
Question: What would be the correct way to achieve what I want? Is it possible like this or there is a CMake limitation here?
(Note: if possible I'd like to keep the whole command be executed in one place. I am also aware of other workaround possible)
Following the answer of Ben Boeckel here:
Spaces generally aren’t well-formed inside of genexes. You’ll need to replace the spaces with ; to make it parse properly (which is why you’re seeing half-expanded remnants in the build command).
And some discussion in the CMake mailing list (here), what finally worked for me was:
add_custom_command(
TARGET foo POST_BUILD
COMMAND "$<$<CONFIG:Debug>:${CMAKE_COMMAND};-E;echo;\"hi there from debug build\">"
COMMAND_EXPAND_LISTS
)
(Notice the quotes aroung the whole genex, separation with semicolons, backquoting the string, and COMMAND_EXPAND_LISTS to get rid of semicolons in the output -- all-in-all definitely not the most pleasing thing to read)
Edit:
This also works:
set(HELLO_FROM_DEBUG ${CMAKE_COMMAND} -E echo "hi there")
add_custom_command(
TARGET foo POST_BUILD
COMMAND "$<$<CONFIG:Debug>:${HELLO_FROM_DEBUG}>"
COMMAND_EXPAND_LISTS
)

how to run compound script statements from cmake?

I have a complex/compound command that I want to execute as patch command during execution of my ExternProject_Add using this macro
macro(SET_PATCH_CMD arg)
message(STATUS "Patch command : ${arg};${ARGN}")
set(${PROJECT_NAME}_patch_cmd ${arg};${ARGN})
endmacro(SET_PATCH_CMD)
I dont remeber or am not sure why I added a ; between ${arg} and ${ARGN}, but patch commands like
mkdir -p <SOURCE_DIR>/install
are working as expected in my cmake project.
When I set a simple command like
SET(PATCH_CMD <SOURCE_DIR>/scripts/my_schript.sh)
patch command executes as expected, but the moment I start adding arguments to script with spaces in between
SET(PATCH_CMD <SOURCE_DIR>/scripts/my_schript.sh arg1 arg2 arg3)
SET_PATCH_CMD(${PATCH_CMD})
I get Command failed: No such file or directory
clearly spaces in my command are messing up the way cmake runs the shell script/command. Any help on making it work is appreciated.

How do you make add_custom_command configuration-specific on Windows?

In order to run the unit tests in one of my projects, I have a custom command which copies the executable, libraries, and other related files to somewhere else so that they can be run with a specific setup rather than running them where they're built. On Linux, this is quite straightforward. But on Windows, I've hit a bit of a snag due to the fact that cmake appends the configuration name to the ouput directories (which I like in general, but it screws up what I'm doing in this case). It makes it hard to determine the paths to the generated libraries or executables. For instance, if I had a custom command which just copied the executable to another directory
set(EXE_PATH "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/exeName${CMAKE_EXECUTABLE_SUFFIX}")
set(NEW_EXE_PATH "${RUN_UNITTESTS_DIR}/exeName${CMAKE_EXECUTABLE_SUFFIX}")
add_custom_command(TARGET unitTests POST_BUILD
COMMAND ${CMAKE_COMMAND} ARGS -E copy "${EXE_PATH}" "${NEW_EXE_PATH}")
it's going to choke on Windows, because the executable isn't really in CMAKE_RUNTIME_OUTPUT_DIRECTORY. Depending on the configuration type, it's in either ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/Release or ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/Debug. On Linux, that could be trivially fixed by using CMAKE_BUILD_TYPE and adding it to the path, but that doesn't work with Windows, because on Windows, cmake generates multiple configurations rather than just one. So, what I'd like to be able to do is something like
add_custom_command(TARGET unitTests POST_BUILD
debug
COMMAND ${CMAKE_COMMAND} ARGS -E copy "${DEBUG_EXE_PATH}" "${DEBUG_NEW_EXE}")
add_custom_command(TARGET unitTests POST_BUILD
release
COMMAND ${CMAKE_COMMAND} ARGS -E copy "${RELEASE_EXE_PATH}" "${RELEASE_NEW_EXE}")
and some cmake commands see to be able to do that (e.g. target_link_libraries), but as far as I can tell, add_custom_target doesn't provide that capability. So, the question is how I would do that? How can I make a custom command be configuration-specific on Windows?
It could be solved with the help of the next "generator expressions" (CMake 2.8.10):
$<0:...> = empty string (ignores "...")
$<1:...> = content of "..."
$<CONFIG:cfg> = '1' if config is "cfg", else '0'
You can combine them to reach behaviour that you need (pseudocode):
if debug then ${DEBUG_EXE_PATH} elseif release then ${RELEASE_EXE_PATH}
which translates to:
$<$<CONFIG:debug>:${DEBUG_EXE_PATH}>$<$<CONFIG:release>:${RELEASE_EXE_PATH}>
So your string will look like:
add_custom_command(TARGET unitTests POST_BUILD
COMMAND ${CMAKE_COMMAND} ARGS -E copy "$<$<CONFIG:debug>:${DEBUG_EXE_PATH}>$<$<CONFIG:release>:${RELEASE_EXE_PATH}>" "$<$<CONFIG:debug>:${DEBUG_NEW_EXE}>$<$<CONFIG:release>:${RELEASE_NEW_EXE}>")
Details: CMake:add_custom_command
This is a case for the generator expressions provided for use with add_custom_command.
In your case you want the full path to your compiled exe, and also its filename to append to your destination directory. These are $<TARGET_FILE:unitTests> and $<TARGET_FILE_NAME:unitTests> respectively.
Your full command would be:
add_custom_command(TARGET unitTests POST_BUILD
COMMAND ${CMAKE_COMMAND} -E
copy $<TARGET_FILE:unitTests>
${RUN_UNITTESTS_DIR}/$<TARGET_FILE_NAME:unitTests>)