[CMake] dependency in custom command?

Tyler Roscoe tyler at cryptio.net
Fri Aug 28 15:10:58 EDT 2009


On Fri, Aug 28, 2009 at 11:28:53AM -0700, King, Steven R wrote:
> I'm building a dynamically loadable module and an executable to test
> it.  Each lives in a different directory.  My test program needs to
> know the location of the dll when calling dlopen().   To solve this, I
> created a custom command to copy the dll to the binary directory of
> the test program.   This works, but I have an annoying dependency
> problem.  Specifically, if the dll gets rebuilt, the copy command does
> not execute.  The copy command only executes if the test program gets
> rebuilt.  I do not understand how to make this copy depend on the dll
> being rebuilt.  I want all test programs to "pull" the rebuilt dll as
> needed.  I do not want the CMakeLists.txt for the dll to have any
> knowledge of the test programs.

Wrap your custom_command with a custom_target. See the CMake FAQ for
some recipes.

Then just do:

add_dependencies (test_my_module custom_target_that_copies_my_module)

> In CMakeLists.txt for the test program:
> add_dependencies          (
>                           test_my_module
>                           my_module		# not needed?
>                           )

You do want this stanza.

> # Put the location of the .so library we need in the SO_LOCATION variable.
> # We then use this location in the copy command below.
> get_target_property       (
>                           SO_LOCATION
>                           my_module        # .so library target
>                           LOCATION
>                           )

You don't need to do this. Since "my_module" is a CMake target, you can
just use "my_module" instead of ${SO_LOCATION} in the custom_command
below and CMake will figure out where the .so is on the disk

> # Create a custom build step to copy the dynamically loaded .so file
> # into the this directory so our test executable can find it.
> add_custom_command        (
>                           TARGET test_my_module
>                           POST_BUILD
>                           DEPENDS my_module  # no effect?
>                           COMMAND ${CMAKE_COMMAND} -E copy ${SO_LOCATION} ${CMAKE_CURRENT_BINARY_DIR}
>                           COMMENT "CUSTOM COMMAND: Copy my_module to build directory"
>                           )

You may want to use -E copy_if_different there instead.

tyler


More information about the CMake mailing list