Reputation: 3957
I can't figure out the right syntax to run a shell command in a post-build step in Cmake on Linux. I can make a simple echo
work, but when I want to e.g. iterate over all files and echo
those, I'm getting an error.
The following works:
add_custom_command(TARGET ${MY_LIBRARY_NAME}
POST_BUILD
COMMAND echo Hello world!
USES_TERMINAL)
This correctly prints Hello world!
.
But now I would like to iterate over all .obj
files and print those. I thought I should do:
add_custom_command(TARGET ${MY_LIBRARY_NAME}
POST_BUILD
COMMAND for file in *.obj; do echo @file ; done
VERBATIM
USES_TERMINAL)
But that gives the following error:
/bin/sh: 1: Syntax error: end of file unexpected
I've tried all sorts of combinations with quotation marks or starting with sh
, but none of that seems to work. How can I do this correctly?
Upvotes: 0
Views: 5070
Reputation: 5510
add_custom_command
(and all the other CMake functions that execute a COMMAND
) don't run shell scripts, but only allow the execution of a single command. The USES_TERMINAL
doesn't cause the command to be run in an actual terminal or allow the use of shell builtins like the for
loop.
From the documentation:
To run a full script, use the configure_file() command or the file(GENERATE) command to create it, and then specify a COMMAND to launch it.
Or, alternatively, for very simple scripts you can do what @lojza suggested in the comment to your question and run the bash
command with the actual script content as an argument.
add_custom_command(
TARGET ${MY_LIBRARY_NAME}
POST_BUILD
COMMAND bash -c [[for file in *.obj; do echo ${file}; done]]
VERBATIM
)
Note that I deliberately used a CMake raw string literal here so that ${file}
is not expanded as a CMake variable. You could also use bash -c "for file in *obj; do echo $file; done"
with a regular string literal, in which case $file
also isn't expanded due to the lack of curly braces. Having copied and pasted bash code from other sources into CMake before I know how difficult it is to track down bugs caused by an unexpected expansions of CMake variables in such scripts, though, so I'd always recommend to use [[
and ]]
unless you actually want to expand a CMake variable.
However, for your concrete example of doing something with all files which match a pattern there is an even simpler alternative: Use the find
command instead of a bash loop:
add_custom_command(
TARGET ${MY_LIBRARY_NAME}
POST_BUILD
COMMAND find
-maxdepth 1
-name *.obj
-printf "%P\\n"
VERBATIM
)
Upvotes: 4
Reputation: 65860
Sometimes, proper scripting of the COMMAND could be performed using debugging. Below the error message there is a point to the line which causes it. Something like
/bin/sh: 1: Syntax error: end of file unexpected
make[2]: *** [CMakeFiles/my_target.dir/build.make:57: CMakeFiles/my_target] Error 2
So, you can look into the line 57 of the file CMakeFiles/my_target.dir/build.make
and find out the command which is actually placed into the Makefile:
CMakeFiles/my_target:
for file in "*.obj" do echo @file done
As you can see, CMake drops all semicolons (;
): this is because this symbol is a separator for CMake.
Quoting semicolons doesn't help in VERBATIM mode:
The commmand
COMMAND for file in *.obj ";" do echo @file ";" done
is transformed
CMakeFiles/my_target:
for file in "*.obj" ";" do echo @file ";" done
But shell doesn't treat quoted semicolons (";"
) as commands separator!
Omitting VERBATIM gives better transformation:
CMakeFiles/my_target:
for file in *.obj ; do echo @file ; done
The next step is to properly refer to the file
variable in the script (@
is definitely a wrong way). A script should see either $file
or ${file}
, but both CMake and Make have a special way for process a dollar ($
). (VERBATIM
could automatically escape things for the Make, but we cannot use it because of semicolons.)
Resulted command could be either
COMMAND for file in *.obj ";" do echo $$file ";" done
or
COMMAND for file in "CMake*" ";" do echo $\${file} ";" done
As you can see, even VERBATIM doesn't handle all situations correctly.
Upvotes: 1