dan123123
dan123123

Reputation: 97

CMake only copy non-cmake files

I am currently building a shared library along with an exectuable with cmake. The exectuable and shared library must both have a specific folder structure that they are built into, and share the same folder.

So, after building both targets, I copy the required files to this folder (along with a couple extra dependencies that the exectuable has).

I can copy the exectuable's binary output directory fine, but when I do, I want to be able to leave out the extra files and folders that cmake generates, ie cmake_install.cmake and CMakeFiles/.

Is there a way to copy the exectuable along with its dependencies, while leaving out these files? There's a few extra shared libraries that are external to the executable target that also need to be distributed with it, which is why I also want to copy these.

These are my current CMakeLists.txt:

CMakeLists.txt

add_library(my_library SHARED src/main.cpp)

add_subdirectory(executable)

add_custom_command(
        TARGET my_library
        POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy
        $<TARGET_FILE:my_library>
        $<TARGET_FILE_DIR:my_library>/bin/${PLATFORM_NAME}${PROCESSOR_ARCH}/$<TARGET_FILE_NAME:my_library>
)

executable/CMakeLists.txt

add_executable(executable main.cpp)

target_link_libraries(executable PRIVATE some_external_libraries...)

add_custom_command(
        TARGET executable 
        POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_directory
        $<TARGET_FILE_DIR:executable>
        $<TARGET_FILE_DIR:my_library>/bin/${PLATFORM_NAME}${PROCESSOR_ARCH}
)

Upvotes: 0

Views: 87

Answers (1)

fabian
fabian

Reputation: 82491

Provide logic for installing the project via the install command:

CMakeLists.txt

...

set(CMAKE_INSTALL_BINDIR bin/${PLATFORM_NAME}${PROCESSOR_ARCH}) # set exe/dll output dir
set(CMAKE_INSTALL_LIBDIR ${CMAKE_INSTALL_BINDIR}) # not recommended; so files only; use INSTALL_RPATH for exe instead

add_library(my_library SHARED src/main.cpp)
install(TARGETS my_library RUNTIME LIBRARY)

add_subdirectory(executable)

executable/CMakeLists.txt

add_executable(executable main.cpp)

target_link_libraries(executable PRIVATE some_external_libraries...)

install(TARGETS executable RUNTIME)

this allows you to use

cmake --install build_dir --prefix install_dir

optionally passing --config <Configuration> for multi config generators to install copy the files built for the cmake project using build_dir as build directory to the directory install_dir. The files will be located in install_dir/bin/${PLATFORM_NAME}${PROCESSOR_ARCH} (variables replaced with the content of the cmake variables during configuration).


Note: If this is just an attempt to make the executable runnable from the build tree, e.g. using the Visual Studio Debugger, you could instead simply set the CMAKE_RUNTIME_OUTPUT_DIRECTORY to an absolute path in the toplevel CMakeLists.txt which results in all the dlls and executables where you do not set the RUNTIME_OUTPUT_DIRECTORY or OUTPUT_DIRECTORY properties yourself going into the same directory. Alternatively you could set the VS_DEBUGGER_ENVIRONMENT target property to something like "PATH=$<TARGET_FILE_DIR:my_library>;$ENV{PATH}" to run the debugger with the PATH environment variable set in a way allowing the executable to locate the dll.

Upvotes: 1

Related Questions