简体   繁体   English

如何使用主要CMakeLists.txt中的cmake复制目标文件?

[英]How to copy target files using cmake from major CMakeLists.txt?

As an example suppose four folders(app1, app2, app3 and main) such as below 作为示例,假设四个文件夹(app1,app2,app3和main)如下所示

main
|__ CMakeLists.txt
\__ module1
|______ CMakeLists.txt
|______ sub1.cpp
|______ sub1.h
\__ library5
|______ CMakeLists.txt
|______ sub5.cpp
|______ sub5.h
\__app1
\__app2
\__app3

Which output of module1 is module1.dll and output of library5 is lib5.dll. 其中module1的输出是module1.dll,而library5的输出是lib5.dll。 Folder of app1 must contain module1.dll and lib5.dll, app2 needs lib5.dll and finally app3 needs module1.dll(number of apps, modules and libs are more than this example and as I explain below we don't want to change modules/libraries's CMakeLists.txt , just main's CMakeLists.txt is ours). app1的文件夹必须包含module1.dll和lib5.dll,app2需要lib5.dll,最后app3需要module1.dll(应用程序,模块和lib的数量超过此示例,正如我在下面解释的那样,我们不想更改模块/库的CMakeLists.txt ,只有main的CMakeLists.txt是我们的)。

PS: PS:

I have a cmake project which has several libraries and modules. 我有一个cmake项目,其中包含几个库和模块。 They included in my project using add_subdirectory command (note that my project just made up from multiple modules and it has not any add_library or add_target ). 它们使用add_subdirectory命令包含在我的项目中(请注意,我的项目仅由多个模块组成,并且没有任何add_libraryadd_target )。

I need to copy outputs of libraries/modules without changing their CMakeLists.txt ( add_custom_command with POST_BUILD option actually is not a good choice because at this point I need to change CMakeLists.txt of libraries/modules which they are not just belong to my project ). 我需要复制的库/输出模块, 无需改变自己CMakeLists.txtadd_custom_commandPOST_BUILD选项其实并不是一个很好的选择,因为在这一点上我需要改变CMakeLists.txt库/模块, 他们不只是属于我的项目 )。 On the other hand it must done in outer(major) CMakeLists.txt which has others(libraries/modules). 另一方面,它必须在具有其他(库/模块)的外部(主要) CMakeLists.txt 完成

I tried some other commands such as file (COPY ) and configure_file() but I think they operate in generating cmake-cache phase and just can copy resource files which are exist in pre-build phase. 我尝试了其他一些命令,例如file (COPY )configure_file()但是我认为它们在生成cmake-cache阶段中起作用,并且可以复制预构建阶段中存在的资源文件。

Moreover, In another approach I write a bash script file to copy the files and call it in major CMakeLists.txt via bellow command. 此外,在另一种方法中,我编写了一个bash脚本文件来复制文件,然后通过bellow命令在主要的CMakeLists.txt调用它。

add_custom_target (copy_all
        COMMAND ${CMAKE_SOURCE_DIR}/copy.sh ${files}
        WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)

The files has the list of files. 这些files具有文件列表。 But the copy not performed! 但是复制没有执行! I manually test the script which works as desired. 我手动测试了可以正常工作的脚本。 But I don't have any idea why it can not operate at call in CMakeLists.txt. 但是我不知道为什么它不能在CMakeLists.txt中调用时运行。

What can I do to copy sub-projects outputs to some locations from major CMakeLists.txt ? 如何将子项目输出从主要CMakeLists.txt复制到某些位置?

The Setup 设置

To simplify it a little, let's say you have: 为了简化一点,假设您有:

CMakeLists.txt 的CMakeLists.txt

cmake_minimum_required(VERSION 3.0)

project(PostBuildCopyFromRoot)

add_subdirectory(module)

module/CMakeLists.txt 模块/的CMakeLists.txt

file(WRITE "module.h" "int ModuleFunc();")
file(WRITE "module.cpp" "int ModuleFunc() { return 1; }")

add_library(module SHARED "module.cpp" "module.h")
target_include_directories(module PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
set_target_properties(module PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS 1)

app/app.mexw64 应用程序/ app.mexw64

The Problem 问题

If you now just add to following to the root CMakeLists.txt : 如果现在将以下内容添加到根CMakeLists.txt

add_custom_command(
    TARGET module
    POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy
        "$<TARGET_FILE:module>"
        "app/$<TARGET_FILE_NAME:module>"
)

You will get from CMake: 您将从CMake获得:

 CMake Warning (dev) at CMakeLists.txt:8 (add_custom_command): Policy CMP0040 is not set: The target in the TARGET signature of add_custom_command() must exist. Run "cmake --help-policy CMP0040" for policy details. Use the cmake_policy command to set the policy and suppress this warning. TARGET 'module' was not created in this directory. 

Solutions 解决方案

You can always overwrite command behaviors: 您始终可以覆盖命令行为:

    function(add_library _target)
        _add_library(${_target} ${ARGN})

        add_custom_command(
            TARGET ${_target}
            POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy
                "$<TARGET_FILE:${_target}>"
                "${CMAKE_SOURCE_DIR}/app/$<TARGET_FILE_NAME:${_target}>"
        )
    endfunction()

NOTE: Put the code snippets before the add_subdirectory() call 注: 之前把代码片段add_subdirectory()调用

References 参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM