Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
320 views
in Technique[技术] by (71.8m points)

CMake : How to get the name of all subdirectories of a directory?

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}?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  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()
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...