[CMake] Re: Custom build step

Brandon Van Every bvanevery at gmail.com
Mon Jan 21 08:39:24 EST 2008


On Jan 21, 2008 7:40 AM, Brandon Van Every <bvanevery at gmail.com> wrote:
> On Jan 21, 2008 6:16 AM, pepone. onrez <pepone.onrez at gmail.com> wrote:
> >
> > SET ( COPY_FILE cp -v )
>
> Better: ${CMAKE_COMMAND} -E copy
> Type cmake -E for details of OS portable commands.
>
> Your example build fails on Cygwin with the same problem.

Ok, here's the essence of what your code does.  In the future when
providing a "trivial reproducer" for a problem, it would be best to
strip it down to this level.  One very simple CMakeLists.txt, no
subdirectories, no long variable names, no extraneous stuff.  To the
degree that you can, of course.

project(copygen)
SET(f0 sample.g)
STRING( REGEX REPLACE "\\.g" .cpp f1 "${f0}" )

ADD_CUSTOM_COMMAND(
    OUTPUT ${CMAKE_BINARY_DIR}/${f1}
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_SOURCE_DIR}/${f0} ${CMAKE_BINARY_DIR}/${f0}
    DEPENDS ${CMAKE_SOURCE_DIR}/${f0}
    COMMENT "step 1")

ADD_CUSTOM_COMMAND(
    OUTPUT ${CMAKE_BINARY_DIR}/generated/${f1}
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_BINARY_DIR}/${f1} ${CMAKE_BINARY_DIR}/generated/${f1}
    DEPENDS ${CMAKE_BINARY_DIR}/${f1}
    COMMENT "step 2")

add_library (mylib SHARED ${CMAKE_BINARY_DIR}/generated/${f1} )


This fails because add_library only understands target level
dependencies.  add_custom_command only generates file level
dependencies.  To make a target level dependency, you'd have to wrap
the last add_custom_command with an add_custom_target(blah DEPENDS
${CMAKE_BINARY_DIR}/generated/${f1}), then ADD_DEPENDENCIES(mylib
DEPENDS blah).

add_library would have understood the dependency just fine, if it were
not generated.  Question: why couldn't add_library look for generated
files as well as non-generated files?


Cheers,
Brandon Van Every


More information about the CMake mailing list