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

Chuck Atkins chuck.atkins at kitware.com
Tue Apr 19 21:40:57 EDT 2016


Hi Chao,
As Konstantin alluded to, trying to pass "srtingified" shell scripting
through the layers of cmake and makefile escaping is bound to be fraught
with problems.  The necessary escaping will very quickly become
unmaintainable.  You're really best suited to place these in a separate
shell script (or cmake script to make it os-portable) and just call the
script from add_custom_command.
Hi Mr. Atkins,

Thanks a lot for your reply, and it worked well and help me a lot, there
still are some confusions about "for cyclic sentence".
I have tested some other case as below:
add_custom_target(temp_target ALL)
add_custom_command(TARGET temp_target
                                   POST_BUILD
                                   COMMAND
                                   if [ "$(dir_name)" = "test_dir" ]\;
                                   then
echo $(dir_name)\;
                                         for x in $$\(cat
$(file_name).txt\)\;
do echo $$x \;
echo $(file_name)\;
                                   done\;
                                   fi)
 Confusions:
1. I should use $$x in "for cyclic sentence", but i can not use $$(x) or
$$\(x\). And i can use "$$\(cat $(file_name).txt\)" and it worked well.
Even it worked when i use "$$x"(withdouble quote ).
2. When i make makefile, i use "make dir_name=test_dir
file_name=test_file", and i can get the value of dir_name and file_name by
use $(dir_name), $(file_name).

Thanks again for your help,
Chao Zhang

2016-04-19 22:20 GMT+08:00 Chuck Atkins <chuck.atkins at kitware.com>:

> 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/f28107a4/attachment.html>


More information about the CMake mailing list