[CMake] Does the echo command use the system shell?

Eric Noulard eric.noulard at gmail.com
Thu Jul 29 05:10:50 EDT 2010


2010/7/29 Óscar Fuentes <ofv at wanadoo.es>:
> Eric Noulard <eric.noulard at gmail.com>
> writes:
>
>> I think you are wrong.
>>
>> May be you can try the attach script.
>>
>> try:
>> cmake -DYOURSTRING="I like # \ it" -DTHE_FILE=toto.txt -P writeany.cmake
>>
>> You shouldn't have "quite" problem with the previous approach.
>
> I tried this on Linux:
>
> add_custom_command(OUTPUT ${EMACS_BUILD_DIR}/src/buildobj.h
>  COMMAND cmake -DYOURSTRING="#define BUILDOBJ" -DTHE_FILE=buildobj.h -P writeany.cmake
>  COMMENT "Creating buildobj.h"
>  )
>
> It didn't create the file.

If this not created the file then there must be an error during the execution
of the custom command.

When this happen you should try: make VERBOSE=1

> So tried this:
>
> add_custom_command(OUTPUT ${EMACS_BUILD_DIR}/src/buildobj.h
>  COMMAND cmake -DYOURSTRING="\#define BUILDOBJ" -DTHE_FILE=buildobj.h -P writeany.cmake
>  COMMENT "Creating buildobj.h"
>  )
>
> It put this text on the resulting file:
>
> #define\ BUILDOBJ
>
> That's not quite what I wanted.

You are right and after thinking about it, I remember some discussion
occurs here on the usage of VERBATIM with custom command.

This should theoretically send the command line "as-is" as you expected but
I end up having  an unexpected behavior with:

add_custom_command(OUTPUT buildobj.h
  COMMAND ${CMAKE_COMMAND} -DYOURSTRING="#define BUILDOBJ"
-DTHE_FILE=${CMAKE_BINARY_DIR}/buildobj.h -P
${PROJECT_SOURCE_DIR}/writeany.cmake
  COMMENT "Creating buildobj.h"
  VERBATIM
  )

because the generated command line (seen with make VERBOSE=1) is

cmake -DYOURSTRING= "#define BUILDOBJ"
-DTHE_FILE=/full/path/to/buildobj.h -P /full/pathto/writeany.cmake

which DOESN'T work because of "stupid" whitespace inserted between

-DYOURSTRING=
and
"#define BUILDOBJ"

Looks like a bug.

Now you know VERBATIM you would be tempted to write:
add_custom_command( OUTPUT buildobj.h
                   COMMAND cmake -E echo "#define FOO 1" > build.h
		   VERBATIM)


but this doesn't work either because the ">" is not interpreted as a redirection
anymore :-(

>>
>> I would say a feature :-)
>>
>> You do not want to have to "escape" some sequences
>
> I'm happy escaping whatever as much sequences as necessary. What is not
> okay is to change escape rules depending on the plataform (or worse,
> depending on the shell cmake uses for the generator. "Mingw Makefiles"
> -> cmd.exe, "MSYS Makefiles" -> sh.exe)

Agreed, but last message from Andreas
seems to show there exist an appropriate cross-platform solution.
Thanks to him for that.

I must admit the solution is "not so" obvious though.

>> More seriously I think it's complicated to avoid system specific
>> issues with a command like "echo".
>
> It should be possible to pass an arbitrary string (containing
> backslashes if necessary) to "echo" and send it to the console or
> redirected to a file on a platform independent way. That means avoiding
> any middleman that may reinterpret certain characters as per its own
> rules.

it should be the goal of VERBATIM option but you lose redirection too.
Excerpt from VERBATIM option doc (sub paragraph of add_custom_command):

"Use of VERBATIM is recommended as it enables
       correct behavior.  When VERBATIM is not given the behavior is platform
       specific because there is no protection of tool-specific special
       characters."

>> I think that if you tell me what you would expect I may probably gives
>> you an example of mine were YOUR bug is a feature for ME.
>
> I doubt that, because on the end we want the same: saying "take this
> string and put it on that file." We can then open the file and test
> that, effectively, the string is there.

May be I was too pushy on that, sorry.

-- 
Erk
Membre de l'April - « promouvoir et défendre le logiciel libre » -
http://www.april.org


More information about the CMake mailing list