开发者

Tell CMake to copy a generated file plus some source files to a directory, then invoke an external tool

I'm very new to CMake but have experience with general build environments like Make.

Our project structure is like the following:

   $ROOT\cpp\bindings\ruby
   $ROOT\cpp\bindings\ruby\ext
   $ROOT\cpp\src
   $ROOT\开发者_如何学JAVAcpp\include

We use swig to generate Ruby bindings for our C++ library. I want to now copy the source code generated by swig ($ROOT\cpp\bindings\ruby\rubyRUBY_wrap.cxx) along with the headers in $ROOT\cpp\include into the directory $ROOT\cpp\bindings\ruby\ext. Then I can have our build environment package all of that together as a GEM for distribution.

I can't seem to find an example online of how to do this in CMake and would appreciate an assist. How do I copy a generated file plus a known directory to a location?


In your case, I'd rather add a special target for distribution, and include a special CMakeLists.txt file (or another build system, like Autotools) in the resulting archive. Something like this, supposing you have CMakeLists.txt in your $ROOT/cpp:

set (dist_gem_name package-gem-1.2.3)
add_custom_target (dist_gem
    COMMAND ${CMAKE_COMMAND} -E  # Create a working directory.
        make_directory ${dist_gem_name}
    COMMAND ${CMAKE_COMMAND} -E  # Copy a build system in there.
        copy ${CMAKE_SOURCE_DIR}/CMakeLists-gem.txt ${dist_gem_name}
    COMMAND ${CMAKE_COMMAND} -E  # Copy the source file.
        copy ${CMAKE_SOURCE_DIR}/bindings/ruby/rubyRUBY_wrap.cxx ${dist_gem_name}
    COMMAND ${CMAKE_COMMAND} -E  # Copy the directory with header files.
        copy_directory ${CMAKE_SOURCE_DIR}/include ${dist_gem_name}
    COMMAND ${CMAKE_COMMAND} -E  # Create a distribution archive.
        tar czf ${dist_gem_name}.tgz ${dist_gem_name}
    COMMAND ${CMAKE_COMMAND} -E  # Dispose of our directory.
        remove_directory ${dist_gem_name}
    DEPENDS ${CMAKE_SOURCE_DIR}/bindings/ruby/rubyRUBY_wrap.cxx
    WORKING_DIRECTORY ${CMAKE_BINARY_DIR})

As you can see, you can do this type of things with cmake -E. If you want to run such things during the configure part, use execute_process. If you want to see the list of available commands, just run cmake -E with no further parameters.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜