【问题标题】:How to deal with target name conflicts between Mercurial repositories and subrepositories?如何处理 Mercurial 存储库和子存储库之间的目标名称冲突?
【发布时间】:2017-03-09 10:26:07
【问题描述】:

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

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

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 中做同样的事情这一事实。那么我可以在这里做点什么更聪明的事情吗?

【问题讨论】:

  • I kind of like the fact that 'make foo' does the same thing, conceptually, both in repo1 and in repo2. - 你期望在 repo1 中make foo 的行为是什么?在 repo1 和 repo2 中构建 foo?还是只在 repo1 中构建 foo?如果您想要最后一种情况,请使用 ExternalProject_Add 而不是 add_subdirectory
  • @Tsyvarev:实际上,我不太介意它是哪个选项(此外,在某些情况下,这可能是同一件事,例如,如果 foo 类似于运行 clocctags)。但是阅读这个问题的其他人可能有两种偏好。你能把你的建议变成答案吗?

标签: cmake mercurial subrepos


【解决方案1】:

方法取决于您的期望

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

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

  2. 两个项目制定目标。

    通常此类目标是“项目范围的操作”,例如 make uninstallmake 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})
    

【讨论】:

  • 我非常喜欢你的第二个选项。第一个选项可以使用链接或更多解释,但这足以让我接受。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
  • 1970-01-01
  • 2012-05-04
  • 2011-03-05
相关资源
最近更新 更多