[CMake] How to create "staging" rules

Paul Smith paul at mad-scientist.net
Mon Jun 17 15:46:27 EDT 2013


I need my build to "stage" the final results (which consist of a few
binaries, shared libraries, static libraries, and header files plus some
scripts etc.) to a location in my workspace where they can be found for
testing and packaging.  #1 requirement: this staging MUST happen as part
of the default target.  It cannot require running a special target
(like, oh, say... install :-)).

I am currently using the "install" facility of CMake to do it using a
hack like this http://marc.info/?l=cmake&m=130365717914933&w=2 to invoke
the install as a side-effect of the default target.  This hack has some
annoying issues so I'm looking for a better way.

What I wanted to try to do instead was create a "stage" target using
add_custom_command() / add_custom_target() that would have an output
file of the staged file and depend on the built target, and where the
rule body would do the copy, something like this:

  macro(stagebin target)
      add_custom_command(OUTPUT "${STAGEDIR}/bin/${target}"
          COMMAND "${CMAKE_COMMAND}" -E make_directory "${STAGEDIR}/bin"
          COMMAND "${CMAKE_COMMAND}" -E copy "$<TARGET_FILE:${target}>" "${STAGEDIR}/bin"
          MAIN_DEPENDENCY ${target}
          COMMENT "Staging ${target} to ${STAGEDIR}/bin"
          VERBATIM)
      add_custom_target("stage_${target}" ALL
          DEPENDS "${STAGEDIR}/bin/${target}")
  endmacro()

This actually seems to work very well for binaries, and I have a version
of this macro for generic files that takes a list of files and works for
header files.

The problem is libraries.  The above works due to a trick, that the
target name is identical to the output file name, but for libraries the
target name is something like "mylibrary" but the output file name is
"libmylibrary.a" or "libmylibrary.so" (or "libmylibrary.dylib" or
"libmylibrary.dll" or whatever).

So I cannot use ${target} in the OUTPUT string; it doesn't work.
Unfortunately I also cannot use a generator expression like
$<TARGET_FILE:${target}> here.  So, I'm kind of stuck.

Any pointers on how to create a "staging" rule like this for libraries?



More information about the CMake mailing list