[CMake] Problems with auto-generated sources

Jakub Schmidtke sjakub at gmail.com
Mon Sep 30 12:58:40 EDT 2013


Hi,

I am trying to get dependencies right in a project that uses auto-generated
sources.
I have a library, that uses auto-generated files, and other executables
that use that library.
I created a set of CMakeLists.txt files that show the problem.

To run it you need test.c and org.c in the source directory, and one of
them should contain int main(void) { return 0; } in it,
the other can be empty. First try - the simplest approach:

project(Test)

add_custom_command(OUTPUT file.c
    COMMAND echo "GENERATING FILE"
    COMMAND rm -f file.c
    COMMAND cp -i ${CMAKE_CURRENT_SOURCE_DIR}/org.c file.c
    DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/org.c
  )
add_library(LibFile file.c)

add_executable(test1 test.c)
target_link_libraries(test1 LibFile)

add_executable(test2 test.c)
target_link_libraries(test2 LibFile)

When I build it with 'make' it builds just fine.
However, if (from the build/) directory I run: touch ../org.c; make -j
test1 test2 it sometimes gives me this:

[ 25%] [ 25%] Generating file.c
Generating file.c
GENERATING FILE
GENERATING FILE
cp: overwrite ‘file.c’? [ 25%] [ 25%] Built target gen_file
Built target gen_file
Scanning dependencies of target LibFile
Scanning dependencies of target LibFile
[ 50%] [ 50%] Building C object CMakeFiles/LibFile.dir/file.o
Building C object CMakeFiles/LibFile.dir/file.o


Linking C static library libLibFile.a
Linking C static library libLibFile.a
[ 75%] [ 75%] Built target LibFile
Built target LibFile
Linking C executable test1
Linking C executable test2
[125%] [125%] Built target test2
Built target test1

It builds (because it isn't very sophisticated example), but it clearly
tries to generate 'file.c' twice.

I tried several other options and they all have the same problem:
(the add_custom_command is exactly the same in all cases)

2.
add_library(LibFile file.c)

add_executable(test1 test.c)
target_link_libraries(test1 LibFile)
add_dependencies(test1 LibFile)

add_executable(test2 test.c)
target_link_libraries(test2 LibFile)
add_dependencies(test2 LibFile)

3.
add_custom_target(gen_file DEPENDS file.c)
add_library(LibFile file.c)
add_dependencies(LibFile gen_file)

add_executable(test1 test.c)
target_link_libraries(test1 LibFile)
add_dependencies(test1 LibFile)

add_executable(test2 test.c)
target_link_libraries(test2 LibFile)
add_dependencies(test2 LibFile)

4.
add_custom_target(gen_file DEPENDS file.c)
add_library(LibFile file.c)
add_dependencies(LibFile gen_file)

add_executable(test1 test.c)
target_link_libraries(test1 LibFile)
add_dependencies(test1 LibFile gen_file)

add_executable(test2 test.c)
target_link_libraries(test2 LibFile)
add_dependencies(test2 LibFile gen_file)

What am I doing wrong? Is it a problem with CMake?
How to solve this?

Thanks!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20130930/fdbc9a11/attachment.htm>


More information about the CMake mailing list