[CMake] ADD_CUSTOM_COMMAND, crating file with echo redirection, platform independent
Michael Hertling
mhertling at online.de
Mon Nov 1 04:47:12 EDT 2010
On 10/27/2010 08:02 PM, Arne Pagel wrote:
> Hello,
>
> I need a custom command in my build process.
> The tool that I am using has a small problem, I have to insert a line of code in
> the output file manually:
> #include <gtk/gtk.h>
>
> I tried this with echo and output redirection:
> echo "#include <gtk/gtk.h>" > images.c
>
> The tool, gdk-pixbuf-csource, is not able to generate a file, therefor its output has to be
> redirected to the file too:
> gdk-pixbuf-csource --raw pix1 ./images/pix1.png >> images.c
>
> I can get it work one linux and win32 system, but with different syntax:
>
> This works on a win32 System:
>
> ADD_CUSTOM_COMMAND (OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/images.c
> COMMAND ${CMAKE_COMMAND} -E echo "\#include <gtk/gtk.h>" > images.c
> COMMAND gdk-pixbuf-csource --raw pix1 ./images/pix1.png >> images.c
> DEPENDS ./images/pix1.png )
>
>
> This works on a linux System:
>
> ADD_CUSTOM_COMMAND (OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/images.c
> COMMAND echo "\"""#include""<gtk/gtk.h>" "\"" > images.c
> COMMAND gdk-pixbuf-csource --raw pix1 ./images/pix1.png >> images.c
> DEPENDS ./images/pix1.png )
>
> I tried many variations of above examples, but I can't get running on both systems with the same
> syntax, any idea what might be right?
>
> Perhaps there is another way then "echo" to write something in a file, I tried FILE(WRITE) but I
> failed with this.
>
> I could make a file with the include line already inserted and than try to copy it, I saw some
> commands for that, is this the better Way? Actually I don't prefer this, I don't want to have a
> strange single-line source file in the project.
Possibly, you can use a CMake script with EXECUTE_PROCESS() and FILE():
CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(PIXBUF NONE)
FILE(WRITE ${CMAKE_BINARY_DIR}/pixbuf.cmake "
EXECUTE_PROCESS(COMMAND gdk-pixbuf-csource --raw \${SRC}
OUTPUT_VARIABLE PIXBUF)
FILE(WRITE \${DST} \"#include <gtk/gtk.h>\n\")
FILE(APPEND \${DST} \${PIXBUF})
")
ADD_CUSTOM_COMMAND(OUTPUT ${CMAKE_BINARY_DIR}/test.c
COMMAND ${CMAKE_COMMAND}
-DSRC=${CMAKE_SOURCE_DIR}/test.png
-DDST=${CMAKE_BINARY_DIR}/test.c
-P ${CMAKE_BINARY_DIR}/pixbuf.cmake
DEPENDS ${CMAKE_SOURCE_DIR}/test.png)
ADD_CUSTOM_TARGET(pixbuf DEPENDS ${CMAKE_BINARY_DIR}/test.c)
The pixbuf.cmake script gathers the output of gdk-pixbuf-csource and
appends it to the resulting file which already contains the include
line. This approach should work in a platform-independent manner.
Regards,
Michael
More information about the CMake
mailing list