CMake:如何获取目录的所有子目录的名称?

发布于 2024-12-09 21:37:09 字数 425 浏览 0 评论 0原文

我有两个与 CMake 相关的问题

  1. 假设我们有一个变量 ${MY_CURRENT_DIR},其中包含包含多个子目录的目录的路径:mydir1、mydir2 和 mydir3。我想检测这些子目录并将它们的名称放入 ${SUBDIRS} (不是这些目录的完整路径,只有它们的名称)。如何自动执行此操作?

  2. 假设${SUBDIRS}包含“mydir1 mydir2 mydir3”。如何更换

    ADD_SUBDIRECTORY(mydir1)
    ADD_SUBDIRECTORY(mydir2)
    ADD_SUBDIRECTORY(mydir3)
    

${SUBDIRS} 上的循环替换?

I have two questions relative to CMake

  1. Assume that we have a variable ${MY_CURRENT_DIR} that contains the path of a directory that contains several subdirectories : mydir1, mydir2 and mydir3. I want to detect these subdirectories and put their names into ${SUBDIRS} (not the complete path of these directories, only their name). How to do that automatically ?

  2. Assume that ${SUBDIRS} contains "mydir1 mydir2 mydir3". How to replace

    ADD_SUBDIRECTORY(mydir1)
    ADD_SUBDIRECTORY(mydir2)
    ADD_SUBDIRECTORY(mydir3)
    

by a loop over ${SUBDIRS}?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

反差帅 2024-12-16 21:37:09
  1. 使用这个宏:

    MACRO(SUBDIRLIST 结果 curdir)
      FILE(GLOB 子级相对 ${curdir} ${curdir}/*)
      SET(目录列表“”)
      FOREACH(子${children})
        IF(IS_DIRECTORY ${curdir}/${child})
          LIST(APPEND 目录列表 ${child})
        ENDIF()
      ENDFOREACH()
      SET(${结果} ${目录列表})
    END宏()
    

    示例:

    SUBDIRLIST(SUBDIRS ${MY_CURRENT_DIR})
    
  2. 使用 foreach

    FOREACH(子目录 ${SUBDIRS})
      ADD_SUBDIRECTORY(${子目录})
    ENDFOREACH()
    
  1. Use this macro:

    MACRO(SUBDIRLIST result curdir)
      FILE(GLOB children RELATIVE ${curdir} ${curdir}/*)
      SET(dirlist "")
      FOREACH(child ${children})
        IF(IS_DIRECTORY ${curdir}/${child})
          LIST(APPEND dirlist ${child})
        ENDIF()
      ENDFOREACH()
      SET(${result} ${dirlist})
    ENDMACRO()
    

    Example:

    SUBDIRLIST(SUBDIRS ${MY_CURRENT_DIR})
    
  2. Use foreach:

    FOREACH(subdir ${SUBDIRS})
      ADD_SUBDIRECTORY(${subdir})
    ENDFOREACH()
    
唯憾梦倾城 2024-12-16 21:37:09

对于现代更简单的解决方案,请尝试这个
对于 cmake 3.7 及更高版本,GLOB 模块有一个 LIST_DIRECTORIES 选项

此链接解释了 glob 模式

file(GLOB sources_list LIST_DIRECTORIES true YourGLOBPattern)
foreach(dir ${sources_list})
    IF(IS_DIRECTORY ${dir})
        add_subdirectory(${dir})
    ELSE()
        CONTINUE()
    ENDIF()
endforeach()

For a modern simpler solution try this
For cmake 3.7 and above the GLOB module got a LIST_DIRECTORIES option

This link explains glob patterns

file(GLOB sources_list LIST_DIRECTORIES true YourGLOBPattern)
foreach(dir ${sources_list})
    IF(IS_DIRECTORY ${dir})
        add_subdirectory(${dir})
    ELSE()
        CONTINUE()
    ENDIF()
endforeach()
行至春深 2024-12-16 21:37:09

关于上面的回答:
使用这个宏:

MACRO(SUBDIRLIST result curdir)
  FILE(GLOB children RELATIVE ${curdir} ${curdir}/*)
  SET(dirlist "")
  FOREACH(child ${children})
    IF(IS_DIRECTORY ${curdir}/${child})
      LIST(APPEND dirlist ${child})
    ENDIF()
  ENDFOREACH()
  SET(${result} ${dirlist})
ENDMACRO()

示例:

SUBDIRLIST(SUBDIRS ${MY_CURRENT_DIR})

我在使用这个 FILE(GLOB 命令时遇到了问题。(我使用的是 cmake 3.17.3)
否则宏效果很好。
我收到 FILE GLOB 错误,例如“FILE GLOB 在目录后面需要一个 glob 表达式”。 (也许它不喜欢 RELATIVE 和/或只使用 curdir 作为第四个参数。)

我必须使用:(

FILE(GLOB children ${curdir}/*)

取出 RELATIVE 和第一个 ${curdir}
(请注意我上面的 cmake 版本,这可能是我的问题(到目前为止我还不熟悉 glob。)。)


In regard to answer above:
Use this macro:

MACRO(SUBDIRLIST result curdir)
  FILE(GLOB children RELATIVE ${curdir} ${curdir}/*)
  SET(dirlist "")
  FOREACH(child ${children})
    IF(IS_DIRECTORY ${curdir}/${child})
      LIST(APPEND dirlist ${child})
    ENDIF()
  ENDFOREACH()
  SET(${result} ${dirlist})
ENDMACRO()

Example:

SUBDIRLIST(SUBDIRS ${MY_CURRENT_DIR})

I had trouble with this FILE(GLOB command. (I'm on cmake 3.17.3)
Otherwise the macro works great.
I was getting FILE GLOB errors, something like "FILE GLOB requires a glob expression after the directory." (Maybe it didn't like RELATIVE and/or just using the curdir as the fourth paramter.)

I had to use:

FILE(GLOB children ${curdir}/*)

(taking out RELATIVE and the first ${curdir}
(Please note my cmake version above, that could've been my issue (I'm unfamiliar with glob so far.).)

尴尬癌患者 2024-12-16 21:37:09

@refaim 答案对 CMake 3.21.1 不起作用,我必须做一些小更改:

MACRO(SUBDIRLIST result curdir)
    FILE(GLOB children ${curdir}/*) # This was changed
    SET(dirlist "")
    FOREACH(child ${children})
        IF(IS_DIRECTORY ${child}) # This was changed
            LIST(APPEND dirlist ${child})
        ENDIF()
    ENDFOREACH()
    SET(${result} ${dirlist})
ENDMACRO()

@refaim answer didn't work for me with CMake 3.21.1, I had to do small changes:

MACRO(SUBDIRLIST result curdir)
    FILE(GLOB children ${curdir}/*) # This was changed
    SET(dirlist "")
    FOREACH(child ${children})
        IF(IS_DIRECTORY ${child}) # This was changed
            LIST(APPEND dirlist ${child})
        ENDIF()
    ENDFOREACH()
    SET(${result} ${dirlist})
ENDMACRO()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文