[CMake] errors with add_custom_command and add_custom_target

Petr Kmoch petr.kmoch at gmail.com
Tue Jan 26 04:12:01 EST 2016


Normally, you would want to keep your source tree clean from any build-time
modifications (CMake *strongly discourages* in-source builds, with good
reason).
Therefore, you'd want to generate the files in CMAKE_CURRENT_BINARY_DIR.
Assuming the generator always writes them into its current directory, you'd
do this:

set(EXIST_SOURCES A.cc B.cc C.cc)
set(GEN_SOURCES ${CMAKE_CURRENT_BINARY_DIR}/X.cc
${CMAKE_CURRENT_BINARY_DIR}/Y.cc ${CMAKE_CURRENT_BINARY_DIR}/Z.cc)
add_custom_command(OUTPUT ${GEN_SOURCES}
                       COMMAND generator
                       WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
                       MAIN_DEPENDENCY gendir/generator)
add_library(mylib OBJECT ${EXIST_SOURCES} ${GEN_SOURCES} )

Petr


On Tue, Jan 26, 2016 at 10:07 AM, Vania Joloboff <vania.joloboff at inria.fr>
wrote:

> Hi Petr,
>
> Thanks, indeed I had unset the property !
> Cmake works fine with removing the line. But I have now a doubt.
> My generator generates the files in the current dir.
> Should I indicate the working directory in add_custom_command ?
> so that the generated sources are added in CMAKE_CURRENT_SOURCE_DIR
> or it will automatically add them in the source dir and not the build dir ?
> Vania
>
>
>
> On 01/26/2016 09:43 AM, Petr Kmoch wrote:
>
>> Hi, Vania.
>>
>> You should remove this line:
>>
>> set_property(SOURCE ${GEN_SOURCES} PROPERTY GENERATED )
>>
>> CMake will mark the files as generated automatically.
>>
>> What your line is actually doing is setting them as *not* generated,
>> because you didn't pass any argument to set the property to, so it's
>> implicitly unset. What you probably wanted would have been this:
>>
>> set_property(SOURCE ${GEN_SOURCES} PROPERTY GENERATED TRUE)
>>
>> But again, remove the line altogether, it's unnecessary.
>>
>> You should also remove the quotes from your `add_library` call. The way
>> you have it would look for a single file named "A.cc;B.cc;C.cc
>> X.cc;Y.cc;Z.cc".
>>
>> Petr
>>
>> On Tue, Jan 26, 2016 at 9:36 AM, Vania Joloboff <vania.joloboff at inria.fr
>> <mailto:vania.joloboff at inria.fr>> wrote:
>>
>>     Hi
>>
>>     I have a generator that generates some, but not all of the source
>>     files.
>>     My understanding was that I should use add_custom_command for that
>>
>>     When I do
>>     set(EXIST_SOURCES A.cc B.cc C.cc)
>>     set(GEN_SOURCES X.cc Y.cc Z.cc)
>>     add_custom_command(OUTPUT ${GEN_SOURCES}
>>                            COMMAND generator
>>                            MAIN_DEPENDENCY gendir/generator   )
>>     set_property(SOURCE ${GEN_SOURCES} PROPERTY GENERATED )
>>     add_library(mylib OBJECT "${EXIST_SOURCES} ${GEN_SOURCES}" )
>>
>>     I get error "Cannot find source file : X.cc"
>>
>>     So, I tried
>>
>>     set(EXIST_SOURCES A.cc B.cc C.cc)
>>     set(GEN_SOURCES X.cc Y.cc Z.cc)
>>     add_custom_target(generate
>>                   COMMAND generator
>>                   COMMENT "Generate"
>>                   DEPENDS gendir/generator   )
>>     set_property(SOURCE ${GEN_SOURCES} PROPERTY GENERATED )
>>     add_library(mylib OBJECT "${EXIST_SOURCES} ${GEN_SOURCES}" )
>>     add_dependencies(mylib generate)
>>
>>     I get same error "Cannot find source file : X.cc"
>>
>>     I just don't get it...
>>
>>     Vania
>>
>>     --
>>     Powered by www.kitware.com <http://www.kitware.com>
>>
>>     Please keep messages on-topic and check the CMake FAQ at:
>>     http://www.cmake.org/Wiki/CMake_FAQ
>>
>>     Kitware offers various services to support the CMake community.
>>     For more information on each offering, please visit:
>>
>>     CMake Support: http://cmake.org/cmake/help/support.html
>>     CMake Consulting: http://cmake.org/cmake/help/consulting.html
>>     CMake Training Courses: http://cmake.org/cmake/help/training.html
>>
>>     Visit other Kitware open-source projects at
>>     http://www.kitware.com/opensource/opensource.html
>>
>>     Follow this link to subscribe/unsubscribe:
>>     http://public.kitware.com/mailman/listinfo/cmake
>>
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20160126/27f90531/attachment-0001.html>


More information about the CMake mailing list