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

Andreas Pakulat apaku at gmx.de
Thu Jul 29 04:49:40 EDT 2010


On 28.07.10 19:19:08, Óscar Fuentes wrote:
> For creating a file at build time with a content like this:
> 
> #define foo "bar"
> 
> I use this on Linux:
> 
> add_custom_command(OUTPUT buildobj.h
>   COMMAND ${CMAKE_COMMAND} -E echo "\\#define foo \\\"bar\\\""
>   > buildobj.h
>   )
> 
> but that doesn't work on Windows, because it outpus:
> 
> \#define foo "bar"
> 
> Removing the backslashes before `#' fixes the problem on Windows, but
> then breaks the Linux build.

Just tried your example here on Linux+Windows and your problem has nothing
to do with the echo command not being cross-platform, its a simple matter
of not escaping/quoting the command call as necessary. The following
custom-command works on all platforms equally:

add_custom_command( OUTPUT build.h
                    COMMAND cmake -E echo \"\#define FOO 1\" >build.h )

You need to escape the # (with a single \) as its the comment-character in
CMake files. Then you also need to quote the " once to make sure they are
part of the actual command invocation. Now if you want to put a string
literal into the mix it would be

add_custom_command( OUTPUT build.h
                    COMMAND cmake -E echo \"\#define FOO \\"bar\\"\" >build.h )

You can easily see your mistake by running (n)make VERBOSE=1 to see the
actual cmake -E execution.

Andreas

-- 
Your analyst has you mixed up with another patient.  Don't believe a
thing he tells you.


More information about the CMake mailing list