[CMake] CMake: how to use 'for cyclic sentence' in command add_custom_command(...)

Chuck Atkins chuck.atkins at kitware.com
Tue Apr 19 10:20:08 EDT 2016


Hi Chao,


> I was using 'for cyclic sentence' in command add_custom_command(...), the
> content of the CMakeLists.txt as below:
> add_custom_command(TARGET temp_target
>                                 POST_BUILD
>                                 COMMAND for x in a b c\;
>                                 do echo $x \;
>                                 done\;)
>

The $x is getting parsed by the makefile so you need to escape it with the
$ makefile escape sequence.  Try:

add_custom_command(TARGET temp_target
                                POST_BUILD
                                COMMAND for x in a b c\;
                                do echo $$x \;
                                done\;)

You can see the difference in a make VERBOSE=1 :

[100%] Linking C static library libtemp_target.a
/home/khq.kitware.com/chuck.atkins/Code/CMake/build/master-release/bin/cmake
-P CMakeFiles/temp_target.dir/cmake_clean_target.cmake
/home/khq.kitware.com/chuck.atkins/Code/CMake/build/master-release/bin/cmake
-E cmake_link_script CMakeFiles/temp_target.dir/link.txt --verbose=1
/usr/bin/ar qc libtemp_target.a  CMakeFiles/temp_target.dir/foo.c.o
/usr/bin/ranlib libtemp_target.a
*for x in a b c; do echo  ; done;*



make[2]: Leaving directory '/home/khq.kitware.com/chuck.atkins/tmp/bld'

Now becomes:

[100%] Linking C static library libtemp_target.a
/home/khq.kitware.com/chuck.atkins/Code/CMake/build/master-release/bin/cmake
-P CMakeFiles/temp_target.dir/cmake_clean_target.cmake
/home/khq.kitware.com/chuck.atkins/Code/CMake/build/master-release/bin/cmake
-E cmake_link_script CMakeFiles/temp_target.dir/link.txt --verbose=1
/usr/bin/ar qc libtemp_target.a  CMakeFiles/temp_target.dir/foo.c.o
/usr/bin/ranlib libtemp_target.a



*for x in a b c; do echo $x ; done;abc*
make[2]: Leaving directory '/home/khq.kitware.com/chuck.atkins/tmp/bld'

- Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20160419/2029732c/attachment.html>


More information about the CMake mailing list