Debian Codegraph @

cmake/tribits/core/utils/CombinedOption.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 include(CMakeParseArguments)
11 include(MultilineSet)
12 include(ConcatStrings)
13
14
15 # @FUNCTION: combined_option()
16 #
17 # Set up a ``BOOL`` cache variable (i.e. an option) based on a set of
18 # dependent options.
19 #
20 # Usage::
21 #
22 # combined_option( <combinedOptionName>
23 # DEP_OPTIONS_NAMES <depOpName0> <depOptName1> ...
24 # DOCSTR "<docstr0>" "<docstr1>" ...
25 # )
26 #
27 # This sets up a ``BOOL`` cache variable ``<combinedOptionName>`` which is
28 # defaulted to ``ON`` if all of the listed dependent option variables
29 # ``<depOpName0>``, ``<depOptName1>``, ... are all ``ON``. However, if
30 # ``<combinedOptionName>`` is set to ``ON`` by the user and not all of the
31 # dependent option variables are also ``ON``, then this results in a fatal
32 # error and all processing stops.
33 #
34 # This is used by a CMake project to automatically turn on a feature that
35 # requires a set of other features (when they are all enabled) but allows a
36 # user to disable the feature if desired.
37 #
38 function(combined_option COMBINED_OPTION_NAME)
39
40 cmake_parse_arguments(
41 #prefix
42 PARSE
43 #options
44 ""
45 #one_value_keywords
46 ""
47 #multi_value_keywords
48 "DEP_OPTIONS_NAMES;DOCSTR"
49 ${ARGN}
50 )
51
52 tribits_check_for_unparsed_arguments()
53
54 # ToDo: Assert that the right input was passed in!
55
56 set(DEFAULT_VAL ON)
57 foreach( DEP_OPTION_NAME ${PARSE_DEP_OPTIONS_NAMES} )
58 if (NOT ${DEP_OPTION_NAME})
59 set(DEFAULT_VAL OFF)
60 endif()
61 endforeach()
62
63 concat_strings(DOCSTR ${PARSE_DOCSTR})
64
65 option(${COMBINED_OPTION_NAME} "${DOCSTR}"
66 ${DEFAULT_VAL} )
67
68 #print_var(${COMBINED_OPTION_NAME})
69
70 # Determine if the combined option was turned on by the individual options
71 # are not turned on as well.
72 set(ALL_ENABLED TRUE)
73 if (${COMBINED_OPTION_NAME})
74 foreach( DEP_OPTION_NAME ${PARSE_DEP_OPTIONS_NAMES})
75 #print_var(${DEP_OPTION_NAME})
76 if (NOT ${DEP_OPTION_NAME})
77 set(ALL_ENABLED FALSE)
78 break()
79 endif()
80 endforeach()
81 endif()
82
83 # Print out detailed error message if the combined option was enabled but
84 # the dependent options were not.
85 if (NOT ALL_ENABLED)
86
87 set(OPTION_NAMES "")
88 set(OPTION_NAMES_AND_VALUES "")
89 foreach( DEP_OPTION_NAME ${PARSE_DEP_OPTIONS_NAMES})
90 if (NOT OPTION_NAMES)
91 set(OPTION_NAMES "${DEP_OPTION_NAME}")
92 else()
93 set(OPTION_NAMES "${OPTION_NAMES}, ${DEP_OPTION_NAME}")
94 endif()
95 set(OPTION_NAMES_AND_VALUES
96 "${OPTION_NAMES_AND_VALUES} ${DEP_OPTION_NAME}='${${DEP_OPTION_NAME}}'\n")
97 endforeach()
98
99 message(FATAL_ERROR
100 "Error: you can not enable the option ${COMBINED_OPTION_NAME} unless"
101 " you also enable the options ${OPTION_NAMES}. The current option"
102 " values are:\n${OPTION_NAMES_AND_VALUES}" )
103
104 endif()
105
106 endfunction()