[CMake] Retrieving a named argument from a list

George Neill georgen at neillnet.com
Thu May 1 00:53:24 EDT 2008


Hi All,

Below is what I have come up with to fetch named arg from a list.  Is
there a preferred or built-in way to do this I may have missed?

MACRO(GET_NAMED_ARG _key)



  LIST(APPEND STOP_KEYWORDS DEPENDS SOURCE)



  SET(${_key} FALSE)

  SET(_stop   -1)



  FOREACH(arg ${ARGN})


    IF(NOT ${${_key}})

      STRING(COMPARE EQUAL "${arg}" "${_key}" ${_key})

    ELSE(NOT ${${_key}})


      IF(${_stop} LESS 0)

        LIST(FIND STOP_KEYWORDS ${arg} _stop)


        IF(${_stop} LESS 0)

          LIST(APPEND ${_key}_ARGS ${arg})

        ELSE(${_stop} LESS 0)

          LIST(FIND STOP_KEYWORDS ${arg} _stop)

        ENDIF(${_stop} LESS 0)


      ENDIF(${_stop} LESS 0)


    ENDIF(NOT ${${_key}})

  ENDFOREACH(arg ${ARGN})




ENDMACRO(GET_NAMED_ARG _key)


An example,


SET(MY_DEPENDS
 0 1 2 3 4
)

SET(MY_SOURCES
 5 6 7 8 9
)

LIST(APPEND MY_ARGS
  DEPENDS ${MY_DEPENDS}
  SOURCE  ${MY_SOURCES}
)

GET_NAMED_ARG(DEPENDS ${MY_ARGS})

LIST(LENGTH DEPENDS_ARGS DEPENDS_ARG_COUNT)

MESSAGE(STATUS "DEPENDS_ARGS: ${DEPENDS_ARGS}  COUNT: ${DEPENDS_ARG_COUNT}")

produces

DEPENDS_ARGS: 0;1;2;3;4  COUNT: 5


TIA,
George.


More information about the CMake mailing list