Debian Codegraph @

Xpjproject/cmake/Pj/Option.cmake

raw
1 function(pj_option variable help_text)
2 # parse options
3 set(options REQUIRED)
4 set(oneValueArgs DEFAULT)
5 set(multiValueArgs ALLOWED_VALUES)
6 cmake_parse_arguments(PARSE_ARGV 2 arg
7 "${options}"
8 "${oneValueArgs}"
9 "${multiValueArgs}"
10 )
11
12 # setup doc string
13 if(arg_ALLOWED_VALUES)
14 string(REPLACE ";" "|" optChoices "${arg_ALLOWED_VALUES}")
15 if(arg_REQUIRED)
16 set(optChoices "<${optChoices}>")
17 else()
18 set(optChoices "[${optChoices}]")
19 endif()
20
21 if(help_text)
22 set(help_text "${help_text}: ${optChoices}")
23 else()
24 set(help_text "${optChoices}")
25 endif()
26 endif()
27
28 # define option cache variable
29 set(${variable} "${arg_DEFAULT}" CACHE STRING "${help_text}")
30 set_property(CACHE "${variable}" PROPERTY STRINGS ${arg_ALLOWED_VALUES})
31
32 # validate option value
33 set(value "${${variable}}")
34 if(value)
35 if(arg_ALLOWED_VALUES AND NOT value IN_LIST arg_ALLOWED_VALUES)
36 message(FATAL_ERROR "Illegal value for ${variable}: \
37 ${value}, allowed: ${arg_ALLOWED_VALUES}")
38 endif()
39 elseif(arg_REQUIRED)
40 message(FATAL_ERROR "Option '${variable}' is required")
41 endif()
42 endfunction()
43
44 function(pj_force_set variable value)
45 # If not a cache variable, set a new internal value
46 if(NOT DEFINED CACHE{${variable}})
47 set(${variable} "${value}" CACHE INTERNAL "(forced)")
48 return()
49 endif()
50
51 # update value
52 set_property(CACHE ${variable} PROPERTY VALUE "${value}")
53
54 # update help string
55 get_property(docstr CACHE ${variable} PROPERTY HELPSTRING)
56 if(NOT docstr MATCHES "\\(forced\\)$")
57 set_property(CACHE ${variable} PROPERTY HELPSTRING "${docstr} (forced)")
58 endif()
59
60 # show warning
61 if(ARGC GREATER 2)
62 message(STATUS "[!] ${ARGV2}. setting ${variable} to ${value}")
63 endif()
64 endfunction()