3

我的 Mercurial 存储库 repo1 有一个名为的自定义目标foo;别管它做了什么。我还有另一个存储库 repo2,我想将其用作 repo1 的子存储库。repo2 以与 repo1 类似的方式开发,并且还有一个名为 的自定义目标foo,做同样的事情(当然只是针对 repo2 目录)。

add_subdirectory(relative/path/to/repo2)如果我尝试在中为 repo1 运行 CMake CMakeLists.txt,我会得到:

CMake Error at CMakeLists.txt:123 (add_custom_target):
  add_custom_target cannot create target "foo" because another target with
  the same name already exists.  The existing target is a custom target
  created in source directory

我想我可以在自定义目标名称前加上存储库名称,但这似乎是解决这个问题的粗略方法;我有点喜欢make foo在 repo1 和 repo2 中在概念上做同样事情的事实。那么我可以在这里做些什么更聪明的事情吗?

4

1 回答 1

1

方法取决于您的期望

make foo
  1. 仅为当前项目构建目标。也就是说,从 project1 的目录运行,make foo应该为这个项目构建目标。项目2相同。

    在这种情况下,请使用ExternalProject_Add而不是add_subdirectory将项目绑定在一起。

  2. 为这两个项目建立目标。

    通常这样的目标是“项目范围的行动”,比如make uninstallor make test

    在这种情况下,在将目标添加到项目之前,您需要检查目标是否存在并采取适当的措施:

    if(NOT TARGET foo)
        <create target foo>
    endif()
    <append-new-actions-to-foo>
    

    步骤“创建”和“附加”取决于目标类型。

    例如,经典目标通过读取文件uninstall自动处理所有子项目:install_manifest.txt

    if(NOT TARGET uninstall)
        add_custom_target(uninstall ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
    endif()
    

    对于一般情况,您可以创建每个项目的目标并将它们附加add_dependencies到“共享”目标:

    if(NOT TARGET foo)
        add_custom_target(foo)
    endif()
    add_custom_target(foo_${CMAKE_PROJECT_NAME} <do-something>)
    add_dependencies(foo foo_${CMAKE_PROJECT_NAME})
    
于 2017-03-09T11:51:44.177 回答