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

Eric Noulard eric.noulard at gmail.com
Wed Jul 28 13:27:23 EDT 2010


2010/7/28 Óscar Fuentes <ofv at wanadoo.es>:
> 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.
>
> This looks as if `echo' was using the system shell for doing the job,
> because the escapes works differently on each system.
>
> What's the right way to write the above command so it works on all
> platforms?

If you want to write to a file, you'd rather use

file(WRITE ...)
file(APPEND ...)

or

configure_file(...)

since those commands are CMake time commands, if you need build
time creation of the file then you may write a CMake script
"generateMyFile.cmake" which contains such commands and use

add_custom_command( ...
  COMMAND ${CMAKE_COMMAND} -P generateMyFile.cmake
       ...)


If you are trying to write some kind of "config.h" things you may
find this link useful:
http://www.cmake.org/Wiki/CMake:How_To_Write_Platform_Checks

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


More information about the CMake mailing list