[CMake] Arbitrary order named arguments

Michael Jackson mike.jackson at bluequartz.net
Fri Jul 20 00:28:50 EDT 2012


include (CMakeParseArguments)

function(BuildQtAppBundle)
    set(options )
    set(oneValueArgs TARGET DEBUG_EXTENSION ICON_FILE VERSION_MAJOR VERSION_MINOR VERSION_PATCH 
                     BINARY_DIR COMPONENT INSTALL_DEST PLUGIN_LIST_FILE)
    set(multiValueArgs SOURCES LINK_LIBRARIES LIB_SEARCH_DIRS QT_PLUGINS OTHER_PLUGINS)
    cmake_parse_arguments(QAB "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )

endfunction()


Then you have access to variables such as QAB_TARGET or QAB_INSTALL_DEST. This is the preferred way of doing this. 

___________________________________________________________
Mike Jackson                    Principal Software Engineer
BlueQuartz Software                            Dayton, Ohio
mike.jackson at bluequartz.net              www.bluequartz.net

On Jul 19, 2012, at 11:42 PM, Doug wrote:

> There are some cute native functions that let you specify arguments in
> random order with named keys preceeding them, eg.
> 
> execute_process(COMMAND blah WORKING_DIRECTORY blah2 OUTPUT_FILE x TIMEOUT 2)
> 
> ===
> 
> execute_process(COMMAND blah WORKING_DIRECTORY blah2 TIMEOUT 2 OUTPUT_FILE x)
> 
> 
> I'm doing this like this at the moment, but it's clumsy:
> 
> function(args NAMED ARGS)
>  set(STATE 0)
>  foreach(ARG ${ARGS})
>    list(FIND NAMED ${ARG} HAS_NAMED_ITEM)
>    if(STATE)
>      set(${STATE} ${ARG} PARENT_SCOPE)
>      set(STATE 0)
>    else()
>      if(NOT HAS_NAMED_ITEM EQUAL -1)
>        set(STATE ${ARG})
>      endif()
>    endif()
>  endforeach()
> endfunction()
> 
> function(blah)
>  set(NAMED_ARGS LD_CONF;C_FLAGS)
>  args("${NAMED_ARGS}" "${ARGV}")
>  message("C_FLAGS --> ${C_FLAGS}")
>  message("LD_CONF --> ${LD_CONF}")
> endfunction()
> 
> It's particularly annoying when I need a required value (or multiple
> ones), like:
> 
> function(blah2 REQUIRED)
>  list(REMOVE_AT ARGV 0)
>  set(NAMED_ARGS LD_CONF;C_FLAGS)
>  args("${NAMED_ARGS}" "${ARGV}")
>  message("REQUIRED --> ${REQUIRED}")
>  message("C_FLAGS --> ${C_FLAGS}")
>  message("LD_CONF --> ${LD_CONF}")
> endfunction()
> 
> blah2("item_required" C_FLAGS "G" LD_CONF "blah")
> 
> Anyone suggest a better way?
> 
> ~
> Doug.
> --
> 
> Powered by www.kitware.com
> 
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
> 
> Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ
> 
> Follow this link to subscribe/unsubscribe:
> http://www.cmake.org/mailman/listinfo/cmake



More information about the CMake mailing list