emliy
emliy

Reputation: 119

CMake macro across CMakeLists

I have a c++ project which directory structure like below:

server/
   code/
      BASE/
         Thread/
         Log/
         Memory/
      Net/
   cmake/
      CMakeList.txt
      BASE/
         CMakeList.txt
      Net/
         CMakeList.txt

here is part of /cmake/CMakeList.txt:

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

add_subdirectory(Base)

then use macro in /cmake/Base/CMakeList.txt:

SET(SUBDIR, "")
SUBDIRLIST(SUBDIRS, ${BASE_SRC_DIR})
message("SUBDIRS : " ${SUBDIRS})

output: SUBDIRS :

I check ${dirlist} by output it's value in macro, I get directory list expected,but when message("result " ${result}) after SET(${result} ${dirlist}),I can not get output expected , what's wrong with my CMakeLists.txt?

Upvotes: 4

Views: 2948

Answers (1)

Fraser
Fraser

Reputation: 78300

There are a couple of minor issues here:

  1. In your macro, SET(dirlist "") could be just SET(dirlist). Likewise, SET(SUBDIR, "") could be just SET(SUBDIRS) (I guess "SUBDIR" is a typo and should be "SUBDIRS". Also you don't want the comma in the set command - probably another typo?)
  2. To output the contents of ${result} in the macro, use message("result: ${${result}}"), since you're not appending ${child} to result each time, but to ${result}. In your example ${result} is SUBDIRS, so ${${result}} is ${SUBDIRS}.
  3. When you call SUBDIRLIST, don't use a comma between the arguments.
  4. When you output the value of SUBDIRS, include ${SUBDIRS} in the quotes, i.e. message("SUBDIRS: ${SUBDIRS}") or you'll lose the semi-colon separators.

Other than those, your macro seems fine.

Upvotes: 2

Related Questions