xref: /libbtbb/cmake/modules/CMakeParseArguments.cmake (revision 0c4345b4af6f79de3a7ff3dc01692f6d597247eb)
1*0c4345b4SDominic Spill# CMAKE_PARSE_ARGUMENTS(<prefix> <options> <one_value_keywords> <multi_value_keywords> args...)
2*0c4345b4SDominic Spill#
3*0c4345b4SDominic Spill# CMAKE_PARSE_ARGUMENTS() is intended to be used in macros or functions for
4*0c4345b4SDominic Spill# parsing the arguments given to that macro or function.
5*0c4345b4SDominic Spill# It processes the arguments and defines a set of variables which hold the
6*0c4345b4SDominic Spill# values of the respective options.
7*0c4345b4SDominic Spill#
8*0c4345b4SDominic Spill# The <options> argument contains all options for the respective macro,
9*0c4345b4SDominic Spill# i.e. keywords which can be used when calling the macro without any value
10*0c4345b4SDominic Spill# following, like e.g. the OPTIONAL keyword of the install() command.
11*0c4345b4SDominic Spill#
12*0c4345b4SDominic Spill# The <one_value_keywords> argument contains all keywords for this macro
13*0c4345b4SDominic Spill# which are followed by one value, like e.g. DESTINATION keyword of the
14*0c4345b4SDominic Spill# install() command.
15*0c4345b4SDominic Spill#
16*0c4345b4SDominic Spill# The <multi_value_keywords> argument contains all keywords for this macro
17*0c4345b4SDominic Spill# which can be followed by more than one value, like e.g. the TARGETS or
18*0c4345b4SDominic Spill# FILES keywords of the install() command.
19*0c4345b4SDominic Spill#
20*0c4345b4SDominic Spill# When done, CMAKE_PARSE_ARGUMENTS() will have defined for each of the
21*0c4345b4SDominic Spill# keywords listed in <options>, <one_value_keywords> and
22*0c4345b4SDominic Spill# <multi_value_keywords> a variable composed of the given <prefix>
23*0c4345b4SDominic Spill# followed by "_" and the name of the respective keyword.
24*0c4345b4SDominic Spill# These variables will then hold the respective value from the argument list.
25*0c4345b4SDominic Spill# For the <options> keywords this will be TRUE or FALSE.
26*0c4345b4SDominic Spill#
27*0c4345b4SDominic Spill# All remaining arguments are collected in a variable
28*0c4345b4SDominic Spill# <prefix>_UNPARSED_ARGUMENTS, this can be checked afterwards to see whether
29*0c4345b4SDominic Spill# your macro was called with unrecognized parameters.
30*0c4345b4SDominic Spill#
31*0c4345b4SDominic Spill# As an example here a my_install() macro, which takes similar arguments as the
32*0c4345b4SDominic Spill# real install() command:
33*0c4345b4SDominic Spill#
34*0c4345b4SDominic Spill#   function(MY_INSTALL)
35*0c4345b4SDominic Spill#     set(options OPTIONAL FAST)
36*0c4345b4SDominic Spill#     set(oneValueArgs DESTINATION RENAME)
37*0c4345b4SDominic Spill#     set(multiValueArgs TARGETS CONFIGURATIONS)
38*0c4345b4SDominic Spill#     cmake_parse_arguments(MY_INSTALL "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )
39*0c4345b4SDominic Spill#     ...
40*0c4345b4SDominic Spill#
41*0c4345b4SDominic Spill# Assume my_install() has been called like this:
42*0c4345b4SDominic Spill#   my_install(TARGETS foo bar DESTINATION bin OPTIONAL blub)
43*0c4345b4SDominic Spill#
44*0c4345b4SDominic Spill# After the cmake_parse_arguments() call the macro will have set the following
45*0c4345b4SDominic Spill# variables:
46*0c4345b4SDominic Spill#   MY_INSTALL_OPTIONAL = TRUE
47*0c4345b4SDominic Spill#   MY_INSTALL_FAST = FALSE (this option was not used when calling my_install()
48*0c4345b4SDominic Spill#   MY_INSTALL_DESTINATION = "bin"
49*0c4345b4SDominic Spill#   MY_INSTALL_RENAME = "" (was not used)
50*0c4345b4SDominic Spill#   MY_INSTALL_TARGETS = "foo;bar"
51*0c4345b4SDominic Spill#   MY_INSTALL_CONFIGURATIONS = "" (was not used)
52*0c4345b4SDominic Spill#   MY_INSTALL_UNPARSED_ARGUMENTS = "blub" (no value expected after "OPTIONAL"
53*0c4345b4SDominic Spill#
54*0c4345b4SDominic Spill# You can the continue and process these variables.
55*0c4345b4SDominic Spill#
56*0c4345b4SDominic Spill# Keywords terminate lists of values, e.g. if directly after a one_value_keyword
57*0c4345b4SDominic Spill# another recognized keyword follows, this is interpreted as the beginning of
58*0c4345b4SDominic Spill# the new option.
59*0c4345b4SDominic Spill# E.g. my_install(TARGETS foo DESTINATION OPTIONAL) would result in
60*0c4345b4SDominic Spill# MY_INSTALL_DESTINATION set to "OPTIONAL", but MY_INSTALL_DESTINATION would
61*0c4345b4SDominic Spill# be empty and MY_INSTALL_OPTIONAL would be set to TRUE therefor.
62*0c4345b4SDominic Spill
63*0c4345b4SDominic Spill#=============================================================================
64*0c4345b4SDominic Spill# Copyright 2010 Alexander Neundorf <[email protected]>
65*0c4345b4SDominic Spill#
66*0c4345b4SDominic Spill# Distributed under the OSI-approved BSD License (the "License");
67*0c4345b4SDominic Spill# see accompanying file Copyright.txt for details.
68*0c4345b4SDominic Spill#
69*0c4345b4SDominic Spill# This software is distributed WITHOUT ANY WARRANTY; without even the
70*0c4345b4SDominic Spill# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
71*0c4345b4SDominic Spill# See the License for more information.
72*0c4345b4SDominic Spill#=============================================================================
73*0c4345b4SDominic Spill# (To distribute this file outside of CMake, substitute the full
74*0c4345b4SDominic Spill#  License text for the above reference.)
75*0c4345b4SDominic Spill
76*0c4345b4SDominic Spill
77*0c4345b4SDominic Spillif(__CMAKE_PARSE_ARGUMENTS_INCLUDED)
78*0c4345b4SDominic Spill  return()
79*0c4345b4SDominic Spillendif()
80*0c4345b4SDominic Spillset(__CMAKE_PARSE_ARGUMENTS_INCLUDED TRUE)
81*0c4345b4SDominic Spill
82*0c4345b4SDominic Spill
83*0c4345b4SDominic Spillfunction(CMAKE_PARSE_ARGUMENTS prefix _optionNames _singleArgNames _multiArgNames)
84*0c4345b4SDominic Spill  # first set all result variables to empty/FALSE
85*0c4345b4SDominic Spill  foreach(arg_name ${_singleArgNames} ${_multiArgNames})
86*0c4345b4SDominic Spill    set(${prefix}_${arg_name})
87*0c4345b4SDominic Spill  endforeach()
88*0c4345b4SDominic Spill
89*0c4345b4SDominic Spill  foreach(option ${_optionNames})
90*0c4345b4SDominic Spill    set(${prefix}_${option} FALSE)
91*0c4345b4SDominic Spill  endforeach()
92*0c4345b4SDominic Spill
93*0c4345b4SDominic Spill  set(${prefix}_UNPARSED_ARGUMENTS)
94*0c4345b4SDominic Spill
95*0c4345b4SDominic Spill  set(insideValues FALSE)
96*0c4345b4SDominic Spill  set(currentArgName)
97*0c4345b4SDominic Spill
98*0c4345b4SDominic Spill  # now iterate over all arguments and fill the result variables
99*0c4345b4SDominic Spill  foreach(currentArg ${ARGN})
100*0c4345b4SDominic Spill    list(FIND _optionNames "${currentArg}" optionIndex)  # ... then this marks the end of the arguments belonging to this keyword
101*0c4345b4SDominic Spill    list(FIND _singleArgNames "${currentArg}" singleArgIndex)  # ... then this marks the end of the arguments belonging to this keyword
102*0c4345b4SDominic Spill    list(FIND _multiArgNames "${currentArg}" multiArgIndex)  # ... then this marks the end of the arguments belonging to this keyword
103*0c4345b4SDominic Spill
104*0c4345b4SDominic Spill    if(${optionIndex} EQUAL -1  AND  ${singleArgIndex} EQUAL -1  AND  ${multiArgIndex} EQUAL -1)
105*0c4345b4SDominic Spill      if(insideValues)
106*0c4345b4SDominic Spill        if("${insideValues}" STREQUAL "SINGLE")
107*0c4345b4SDominic Spill          set(${prefix}_${currentArgName} ${currentArg})
108*0c4345b4SDominic Spill          set(insideValues FALSE)
109*0c4345b4SDominic Spill        elseif("${insideValues}" STREQUAL "MULTI")
110*0c4345b4SDominic Spill          list(APPEND ${prefix}_${currentArgName} ${currentArg})
111*0c4345b4SDominic Spill        endif()
112*0c4345b4SDominic Spill      else()
113*0c4345b4SDominic Spill        list(APPEND ${prefix}_UNPARSED_ARGUMENTS ${currentArg})
114*0c4345b4SDominic Spill      endif()
115*0c4345b4SDominic Spill    else()
116*0c4345b4SDominic Spill      if(NOT ${optionIndex} EQUAL -1)
117*0c4345b4SDominic Spill        set(${prefix}_${currentArg} TRUE)
118*0c4345b4SDominic Spill        set(insideValues FALSE)
119*0c4345b4SDominic Spill      elseif(NOT ${singleArgIndex} EQUAL -1)
120*0c4345b4SDominic Spill        set(currentArgName ${currentArg})
121*0c4345b4SDominic Spill        set(${prefix}_${currentArgName})
122*0c4345b4SDominic Spill        set(insideValues "SINGLE")
123*0c4345b4SDominic Spill      elseif(NOT ${multiArgIndex} EQUAL -1)
124*0c4345b4SDominic Spill        set(currentArgName ${currentArg})
125*0c4345b4SDominic Spill        set(${prefix}_${currentArgName})
126*0c4345b4SDominic Spill        set(insideValues "MULTI")
127*0c4345b4SDominic Spill      endif()
128*0c4345b4SDominic Spill    endif()
129*0c4345b4SDominic Spill
130*0c4345b4SDominic Spill  endforeach()
131*0c4345b4SDominic Spill
132*0c4345b4SDominic Spill  # propagate the result variables to the caller:
133*0c4345b4SDominic Spill  foreach(arg_name ${_singleArgNames} ${_multiArgNames} ${_optionNames})
134*0c4345b4SDominic Spill    set(${prefix}_${arg_name}  ${${prefix}_${arg_name}} PARENT_SCOPE)
135*0c4345b4SDominic Spill  endforeach()
136*0c4345b4SDominic Spill  set(${prefix}_UNPARSED_ARGUMENTS ${${prefix}_UNPARSED_ARGUMENTS} PARENT_SCOPE)
137*0c4345b4SDominic Spill
138*0c4345b4SDominic Spillendfunction()
139