<p dir="ltr">Hi Chao,<br>
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.</p>
<div class="gmail_quot<blockquote class=" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi Mr. Atkins,<div><br></div><div>Thanks a lot for your reply, and it worked well and help me a lot, there still are some confusions about "<span style="font-size:14px">for cyclic sentence</span>".</div><div>I have tested some other case as below:</div><div><div>add_custom_target(temp_target ALL)</div><div>add_custom_command(TARGET temp_target</div><div>                                   POST_BUILD</div><div>                                   COMMAND </div><div>                                   if [ "$(dir_name)" = "test_dir" ]\;</div><div>                                   then</div><div><span style="white-space:pre-wrap">                                        </span>echo $(dir_name)\;</div><div>                                         for x in $$\(cat $(file_name).txt\)\;</div><div><span style="white-space:pre-wrap">                                     </span>do echo $$x \;</div><div><span style="white-space:pre-wrap">                                       </span>echo $(file_name)\;</div><div>                                   done\;</div><div>                                   fi)</div></div><div> Confusions:<span style="font-size:14px"><br></span></div><div>1. I should use $$x in "<span style="font-size:14px">for cyclic sentence", but i can not use $$(x) or $$\(x\). And i can use "</span>$$\(cat $(file_name).txt\)" and it worked well. Even it worked when i use "$$x"(withdouble quote ).</div><div>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).</div><div><br></div><div>Thanks again for your help,</div><div>Chao Zhang</div></div><div class="gmail_extra"><br><div class="gmail_quote">2016-04-19 22:20 GMT+08:00 Chuck Atkins <span dir="ltr"><<a href="mailto:chuck.atkins@kitware.com" target="_blank">chuck.atkins@kitware.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div>Hi Chao,<br> <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
I was using 'for cyclic sentence' in command add_custom_command(...), the<br>
content of the CMakeLists.txt as below:<br>
add_custom_command(TARGET temp_target<br>
                                POST_BUILD<br>
                                COMMAND for x in a b c\;<br>
                                do echo $x \;<br>
                                done\;)<br></blockquote></div><br></div><div class="gmail_extra">The $x is getting parsed by the makefile so you need to escape it with the $ makefile escape sequence.  Try:<br><br>add_custom_command(TARGET temp_target<br>
                                POST_BUILD<br>
                                COMMAND for x in a b c\;<br>
                                do echo $$x \;<br>
                                done\;)<br><br></div><div class="gmail_extra">You can see the difference in a make VERBOSE=1 :<br><br><span style="font-family:monospace,monospace">[100%] Linking C static library libtemp_target.a<br>/home/<a href="http://khq.kitware.com/chuck.atkins/Code/CMake/build/master-release/bin/cmake" target="_blank">khq.kitware.com/chuck.atkins/Code/CMake/build/master-release/bin/cmake</a> -P CMakeFiles/temp_target.dir/cmake_clean_target.cmake<br>/home/<a href="http://khq.kitware.com/chuck.atkins/Code/CMake/build/master-release/bin/cmake" target="_blank">khq.kitware.com/chuck.atkins/Code/CMake/build/master-release/bin/cmake</a> -E cmake_link_script CMakeFiles/temp_target.dir/link.txt --verbose=1<br>/usr/bin/ar qc libtemp_target.a  CMakeFiles/temp_target.dir/foo.c.o<br>/usr/bin/ranlib libtemp_target.a<br><b>for x in a b c; do echo  ; done;</b><br><br><br><br>make[2]: Leaving directory '/home/<a href="http://khq.kitware.com/chuck.atkins/tmp/bld" target="_blank">khq.kitware.com/chuck.atkins/tmp/bld</a>'<br><br></span></div><div class="gmail_extra"><span style="font-family:arial,helvetica,sans-serif">Now becomes:</span><span style="font-family:monospace,monospace"><br><br>[100%] Linking C static library libtemp_target.a<br>/home/<a href="http://khq.kitware.com/chuck.atkins/Code/CMake/build/master-release/bin/cmake" target="_blank">khq.kitware.com/chuck.atkins/Code/CMake/build/master-release/bin/cmake</a> -P CMakeFiles/temp_target.dir/cmake_clean_target.cmake<br>/home/<a href="http://khq.kitware.com/chuck.atkins/Code/CMake/build/master-release/bin/cmake" target="_blank">khq.kitware.com/chuck.atkins/Code/CMake/build/master-release/bin/cmake</a> -E cmake_link_script CMakeFiles/temp_target.dir/link.txt --verbose=1<br>/usr/bin/ar qc libtemp_target.a  CMakeFiles/temp_target.dir/foo.c.o<br>/usr/bin/ranlib libtemp_target.a<br><b>for x in a b c; do echo $x ; done;<br>a<br>b<br>c</b><br>make[2]: Leaving directory '/home/<a href="http://khq.kitware.com/chuck.atkins/tmp/bld" target="_blank">khq.kitware.com/chuck.atkins/tmp/bld</a>'</span><span><font color="#888888"><br><br></font></span></div><span><font color="#888888"><div class="gmail_extra">- Chuck<br></div><div class="gmail_extra"><span style="font-family:monospace,monospace"><br></span></div><div class="gmail_extra"><br></div></font></span></div>
</blockquote></div><br></div>
</div>