3

I am trying to add a custom_command with CMake and call COMMAND echo "$" > file.txt

as long as I put $ in it, the config file will generate but failed to build.

I have also tried echo "\$" and doesn't seems to work.

add_custom_command( TARGET ${TARGET_NAME}
                    POST_BUILD
                    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/out
                    COMMAND echo "-keep class com.android.**\$* { ; }" >> ./proguard.txt
                  )

The cmake command works but as long as I call ninja, I got the following error:

error: 'src', needed by 'all', missing and no known rule to make it 

Seems like cmake is unable to generate the build step. My intention is to print that **$ into a file.

DevSolar
  • 67,862
  • 21
  • 134
  • 209
apikachu
  • 51
  • 1
  • 2
  • It would help if you posted segments of your efforts. – Rohit Gupta Jul 22 '15 at 00:24
  • add_custom_command(TARGET ${TARGET_NAME} POST_BUILD WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/out COMMAND echo "-keep class com.android.**\$* { *; }" >> ./proguard.txt the cmake command works but as long as I call ninja, I got the following error: error: 'src', needed by 'all', missing and no known rule to make it Seems like cmake is unable to generate the build step. My intention is to print that **$* into a file. – apikachu Jul 23 '15 at 05:24
  • @apikachu: Your comment should be part of your question, as it changes its meaning significally. Just "edit" your question and copy-past your comment into it. – Tsyvarev Jul 23 '15 at 08:47
  • I deleted my comment with stated you can't use redirection with with `add_custom_command`. I also updated this answer: http://stackoverflow.com/questions/31582344/how-to-redirect-the-output-of-a-cmake-custom-command-to-a-file which still contains some usable info about the gotchas with `add_custom_command` – tamas.kenez Jul 23 '15 at 13:13

1 Answers1

0

Both

COMMAND echo "$$" > file.txt

and

COMMAND echo "$" > file.txt VERBATIM

output $ sign into given file.

EDIT: This works on makefile generators, and only when make is run from the terminal. Generally redirection sign ">" is not worked as expected in COMMAND expression.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
  • Actually they don't since `add_custom_command` does not support redirection. – DevSolar Jul 23 '15 at 08:51
  • @DevSolar: It works on my machine with cmake 2.8.9 and makefile generator. At the time when this answer has been written, question didn't contain refinements such as ninja generator. – Tsyvarev Jul 23 '15 at 09:38
  • Ah.... fair enough. I use CMake on several different platforms, and have run into the very problem tamas.kenez is linking to long ago. So I just had "does not work" filed away in my brain, not aware of it working for the specific case of Makefiles. If you'd do a minor edit to your answer, I'll retract the downvote. – DevSolar Jul 23 '15 at 09:45
  • @DevSolar: I have added limitations to my answer. – Tsyvarev Jul 23 '15 at 10:05
  • @apikachu I just tested @Tsyvarev solution with CMake 3.3 and Ninja 1.4 on Windows and it works fine. So I have the feeling that we are talking about the usage of Ninja on Linux or Mac OS. If I'm right please try using single quotes instead of double quotes (see [How to echo a variable containing an unescaped dollar sign in bash](http://stackoverflow.com/questions/3923623/how-to-echo-a-variable-containing-an-unescaped-dollar-sign-in-bash)). And generally be aware the `$` is the escape character in Ninja (see Ninja: [Lexical syntax](http://martine.github.io/ninja/manual.html#_lexical_syntax)). – Florian Jul 24 '15 at 07:50
  • @Tsyvarev I generally would recommend the usage of `COMMAND ${CMAKE_COMMAND} -E echo ...` over calling `COMMAND echo ...` directly. It's more platform independent and - as @tamas.kenez has mentioned in his linked post - sometimes its preferable to call an executable over using a build-in shell command. – Florian Jul 24 '15 at 07:55
  • @Florian, thanks for testing. Actually, double quotes are parsed with CMake, generator never sees them even with VERBATIM. Moreover, escaping is needed not for the shell, but for make/Ninja. Luckly, both make and Ninja interpret `$$` as single `$` character. So, my second solution(with VERBATIM) is more portable. – Tsyvarev Jul 24 '15 at 08:44
  • @Tsyvarev I just checked the generated `build.ninja` files again and the weird part is that for your example the double quotes are removed `echo $$ > file.txt`, but if I take @apikachu example they are still there `echo "-keep class com.android.**$$* { ; }"` (in both cases I used your `$$` without `VERBATIM`). Just extended my tests using MinGW/MSYS and Ninja and everything still works fine. What I've observed with MinGW and @apikachu's example: single/double quotes are kept in the `proguard.txt` output file using direct `echo` calls, if I use `${CMAKE_COMMAND} -E echo` the quotes are removed. – Florian Jul 24 '15 at 09:36