[CMake] Copy two files into one file

Michael Hertling mhertling at online.de
Fri Jul 9 10:48:11 EDT 2010


On 07/09/2010 03:48 PM, Bo Thorsen wrote:
> Hi all,
> 
> Is there an elegant way of copying two files into one?
> 
> Right now, I have this:
> 
> ADD_CUSTOM_COMMAND(OUTPUT privilege_tables.sql
>                     COMMAND copy /b
>                       ${CMAKE_CURRENT_SOURCE_DIR}/system_tables.sql
>                       + ${CMAKE_CURRENT_SOURCE_DIR}/tables_fix.sql
>                       fix_privilege_tables.sql
>                     DEPENDS
>                     ${CMAKE_CURRENT_SOURCE_DIR}/system_tables.sql
>                     ${CMAKE_CURRENT_SOURCE_DIR}/tables_fix.sql)
> 
> That's hardly elegant and it doesn't work :(
> 
> The problem with this is that the Windows copy command wants \ instead of /.
> 
> So, one of two fixes are possible for me: Either there is a much cleaner 
> way to do this, or I have to replace / with \ in the path.
> 
> If I have to do the replacement, is there a better way to do this than 
> to use the STRING command with a char replace?

Look at the following CMakeLists.txt:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(CONCAT C)
FILE(WRITE ${CMAKE_BINARY_DIR}/f1.c.in "void f1(void){}\n")
FILE(WRITE ${CMAKE_BINARY_DIR}/f2.c.in "void f2(void){}\n")
FILE(WRITE ${CMAKE_BINARY_DIR}/main.c
"int main(void){f1(); f2(); return 0;}\n"
)
FILE(WRITE ${CMAKE_BINARY_DIR}/concat.cmake "
FILE(READ \${SRC1} S1)
FILE(READ \${SRC2} S2)
FILE(WRITE \${DST} \${S1}\${S2})
")
ADD_CUSTOM_COMMAND(
    OUTPUT f.c
    COMMAND ${CMAKE_COMMAND} -D SRC1=${CMAKE_BINARY_DIR}/f1.c.in
                             -D SRC2=${CMAKE_BINARY_DIR}/f2.c.in
                             -D DST=${CMAKE_BINARY_DIR}/f.c
                             -P ${CMAKE_BINARY_DIR}/concat.cmake
    DEPENDS ${CMAKE_BINARY_DIR}/f1.c.in ${CMAKE_BINARY_DIR}/f2.c.in
)
ADD_EXECUTABLE(main main.c f.c)

Usually, one has f{1,2}.c.in, main.c and concat.cmake in the source
directory, of course. Possibly, concat.cmake may even be enhanced
with little effort to concat an arbitrary number of files.

Regards,

Michael


More information about the CMake mailing list