Debian Codegraph @

cmake/tribits/core/utils/TribitsParseArgumentsHelpers.cmake

raw
1 # @HEADER
2 # *****************************************************************************
3 # TriBITS: Tribal Build, Integrate, and Test System
4 #
5 # Copyright 2013-2016 NTESS and the TriBITS contributors.
6 # SPDX-License-Identifier: BSD-3-Clause
7 # *****************************************************************************
8 # @HEADER
9
10
11 ################################################################################
12 #
13 # This module contains function to aid with the usage of
14 # cmake_parse_arguments().
15 #
16 ################################################################################
17
18
19 include("${CMAKE_CURRENT_LIST_DIR}/MessageWrapper.cmake")
20
21
22 # @FUNCTION: tribits_check_for_unparsed_arguments()
23 #
24 # Check to see if there are unparsed arguments after calling
25 # ``cmake_parse_arguments()`` or ``tribits_parse_arguments_from_list()``
26 #
27 # Usage::
28 #
29 # tribits_check_for_unparsed_arguments([<prefix>])
30 #
31 # If `<prefix>` is not given, it is assumed to be `PARSE`.
32 #
33 function(tribits_check_for_unparsed_arguments)
34
35 if ("${ARGC}" GREATER 1)
36 message(FATAL_ERROR
37 "ERROR tribits_check_for_unparsed_arguments() passed arguments '${ARGV}' but only accepts 0 or 1 arguments (for <prefix>)")
38 endif()
39
40 set(prefix "PARSE")
41 foreach(arg ${ARGV})
42 set(prefix "${arg}")
43 endforeach()
44
45 if( NOT "${${prefix}_UNPARSED_ARGUMENTS}" STREQUAL "")
46 message_wrapper(
47 ${${PROJECT_NAME}_CHECK_FOR_UNPARSED_ARGUMENTS}
48 "Arguments passed in unrecognized. ${prefix}_UNPARSED_ARGUMENTS = '${${prefix}_UNPARSED_ARGUMENTS}'"
49 )
50 endif()
51
52 endfunction()
53
54
55 # @MACRO: tribits_assert_parse_arg_one_or_more_values()
56 #
57 # Assert that a set of parse arguments have at least one value
58 #
59 # Usage::
60 #
61 # tribits_assert_parse_arg_one_or_more_values(<prefix> <argname0> <argname1> ...)
62 #
63 macro(tribits_assert_parse_arg_one_or_more_values PREFIX)
64 foreach(ARGNAME ${ARGN})
65 set(PREFIX_ARGNAME "${PREFIX}_${ARGNAME}")
66 list( LENGTH ${PREFIX_ARGNAME} ARG_NUM_VALS )
67 if (ARG_NUM_VALS LESS 1)
68 message_wrapper(FATAL_ERROR
69 "ERROR: ${ARGNAME} must have at least one value!" )
70 return()
71 # NOTE: The return() is needed in unit testing mode
72 endif()
73 endforeach()
74 endmacro()
75
76
77 # @MACRO: tribits_assert_parse_arg_zero_or_one_value()
78 #
79 # Assert a set of parse arguments have zero or one value
80 #
81 # Usage::
82 #
83 # tribits_assert_parse_arg_zero_or_one_value(<prefix> <argname0> <argname1> ...)
84 #
85 macro(tribits_assert_parse_arg_zero_or_one_value PREFIX)
86 foreach(ARGNAME ${ARGN})
87 set(PREFIX_ARGNAME "${PREFIX}_${ARGNAME}")
88 if (NOT "${${PREFIX_ARGNAME}}" STREQUAL "")
89 list( LENGTH ${PREFIX_ARGNAME} ARG_NUM_VALS )
90 if (ARG_NUM_VALS GREATER 1)
91 message_wrapper(FATAL_ERROR
92 "ERROR: ${ARGNAME}='${${PREFIX_ARGNAME}}' can not have more than one value!" )
93 return()
94 # NOTE: The macro return() is needed in unit testing mode
95 endif()
96 endif()
97 endforeach()
98 endmacro()
99
100
101 # @MACRO: tribits_assert_parse_arg_one_value()
102 #
103 # Assert that a set of parse arguments have exactly one value
104 #
105 # Usage::
106 #
107 # tribits_assert_parse_arg_one_value(<prefix> <argname0> <argname1> ...)
108 #
109 macro(tribits_assert_parse_arg_one_value PREFIX)
110 foreach(ARGNAME ${ARGN})
111 set(PREFIX_ARGNAME "${PREFIX}_${ARGNAME}")
112 if (NOT "${${PREFIX_ARGNAME}}" STREQUAL "")
113 list( LENGTH ${PREFIX_ARGNAME} ARG_NUM_VALS )
114 if (NOT ARG_NUM_VALS EQUAL 1)
115 message_wrapper(FATAL_ERROR
116 "ERROR: ${ARGNAME}='${${PREFIX_ARGNAME}}' Must have exactly one value!" )
117 return()
118 # NOTE: The macro return() is needed in unit testing mode
119 endif()
120 endif()
121 endforeach()
122 endmacro()
123
124
125 # NOTE: Above, we use macros for the assert functions with returns in unit
126 # test mode so that it will abort the calling function these are called from!