File: ManageVariable.cmake

package info (click to toggle)
ibus-table-chinese 1.8.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 29,812 kB
  • sloc: sh: 106; makefile: 11
file content (326 lines) | stat: -rw-r--r-- 12,547 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# - Get or set variables from various sources.
#
# Includes:
#   ManageString
#
# Included by:
#   ManageVersion
#   PackRPM
#
# Defines following functions:
#   SETTING_STRING_GET_VARIABLE(var value str [NOUNQUOTE] [NOREPLACE] [setting_sign])
#     - Get a variable and a value from a setting in string format.
#       i.e.  VAR=Value
#       pattern. '#' is used for comment.
#       * Parameters:
#         + var: Variable name extracted from str.
#         + value: Value extracted from str
#         + str: String to be extracted variable and value from.
#         + NOUNQUOTE: (Optional) do not remove the double quote mark around the string.
#         + NOREPLACE (Optional) Without this parameter, this macro replaces
#           previous defined variables, use NOREPLACE to prevent this.
#         + NOESCAPE_SEMICOLON: (Optional) do not escape semicolons.
#         + setting_sign: (Optional) The symbol that separate attribute name and its value.
#           Default value: "="
#
# Defines following macros:
#   COMMAND_OUTPUT_TO_VARIABLE(var cmd)
#     - Store command output to a variable, without new line characters (\n and \r).
#       This macro is suitable for command that output one line result.
#       Note that the var will be set to ${var_name}-NOVALUE if cmd does not have
#       any output.
#       * Parameters:
#         var: A variable that stores the result.
#         cmd: A command.
#
#   SETTING_FILE_GET_VARIABLES_PATTERN(var attr_pattern setting_file [NOUNQUOTE] [NOREPLACE]
#     [NOESCAPE_SEMICOLON] [setting_sign])
#     - Get variable values from a setting file if their names matches given
#       pattern. '#' is used for comment.
#       * Parameters:
#         + var: Variable to store the attribute value.
#           Set to "" to set attribute under matched variable name.
#         + attr_pattern: Regex pattern of variable name.
#         + setting_file: Setting filename.
#         + NOUNQUOTE: (Optional) do not remove the double quote mark around the string.
#         + NOREPLACE (Optional) Without this parameter, this macro replaces
#           previous defined variables, use NOREPLACE to prevent this.
#         + NOESCAPE_SEMICOLON: (Optional) do not escape semicolons.
#         + setting_sign: (Optional) The symbol that separate attribute name and its value.
#           Default value: "="
#
#   SETTING_FILE_GET_ALL_VARIABLES(setting_file [NOUNQUOTE] [NOREPLACE]
#     [NOESCAPE_SEMICOLON] [setting_sign])
#     - Get all variable values from a setting file.
#       It is equivalent to:
#       SETTING_FILE_GET_VARIABLES_PATTERN("" "[A-Za-z_][A-Za-z0-9_]*"
#        "${setting_file}" ${ARGN})
#      '#' is used to comment out setting.
#       * Parameters:
#         + setting_file: Setting filename.
#         + NOUNQUOTE: (Optional) do not remove the double quote mark around the string.
#         + NOREPLACE (Optional) Without this parameter, this macro replaces
#           previous defined variables, use NOREPLACE to prevent this.
#         + NOESCAPE_SEMICOLON: (Optional) Do not escape semicolons.
#         + setting_sign: (Optional) The symbol that separate attribute name and its value.
#           Default value: "="
#
#   SETTING_FILE_GET_VARIABLE(var attr_name setting_file [NOUNQUOTE] [NOREPLACE]
#     [NOESCAPE_SEMICOLON] [setting_sign])
#     - Get a variable value from a setting file.
#       It is equivalent to:
#	SETTING_FILE_GET_VARIABLES_PATTERN(${var} "${attr_name}"
#	    "${setting_file}" ${ARGN})
#      '#' is used to comment out setting.
#       * Parameters:
#         + var: Variable to store the attribute value.
#         + attr_name: Name of the variable.
#         + setting_file: Setting filename.
#         + NOUNQUOTE: (Optional) do not remove the double quote mark around the string.
#         + NOREPLACE (Optional) Without this parameter, this macro replaces
#           previous defined variables, use NOREPLACE to prevent this.
#         + NOESCAPE_SEMICOLON: (Optional) do not escape semicolons.
#         + setting_sign: (Optional) The symbol that separate attribute name and its value.
#           Default value: "="
#
#   SETTING_FILE_GET_ALL_VARIABLES(setting_file [NOUNQUOTE] [NOREPLACE]
#     [NOESCAPE_SEMICOLON] [setting_sign])
#     - Get all attribute values from a setting file.
#       '#' is used to comment out setting.
#       * Parameters:
#         + setting_file: Setting filename.
#         + NOUNQUOTE: (Optional) do not remove the double quote mark around the string.
#         + NOREPLACE (Optional) Without this parameter, this macro replaces
#           previous defined variables, use NOREPLACE to prevent this.
#         + NOESCAPE_SEMICOLON: (Optional) Do not escape semicolons.
#         + setting_sign: (Optional) The symbol that separate attribute name and its value.
#           Default value: "="
#
#   GET_ENV(var default_value [env])
#     - Get the value of a environment variable, or use default
#       if the environment variable does not exist or is empty.
#       * Parameters:
#         var: Variable to be set
#         default_value: Default value of the var
#         env: (Optional) The name of environment variable. Only need if different from var.
#
#   SET_VAR(var untrimmed_value)
#     - Trim an set the value to a variable.
#       * Parameters:
#         var: Variable to be set
#         untrimmed_value: Untrimmed values that may have space, \t, \n, \r in the front or back of the string.
#

IF(NOT DEFINED _MANAGE_VARIABLE_CMAKE_)
    SET(_MANAGE_VARIABLE_CMAKE_ "DEFINED")
    INCLUDE(ManageString)

    MACRO(COMMAND_OUTPUT_TO_VARIABLE var cmd)
	EXECUTE_PROCESS(
	    COMMAND ${cmd} ${ARGN}
	    OUTPUT_VARIABLE _cmd_output
	    OUTPUT_STRIP_TRAILING_WHITESPACE
	    )
	IF(_cmd_output)
	    SET(${var} ${_cmd_output})
	ELSE(_cmd_output)
	    SET(var "${var}-NOVALUE")
	ENDIF(_cmd_output)
	#SET(value ${${var}})
	#MESSAGE("var=${var} _cmd_output=${_cmd_output} value=|${value}|" )
    ENDMACRO(COMMAND_OUTPUT_TO_VARIABLE var cmd)

    # This macro is meant to be internal.
    MACRO(_MANAGE_VARIABLE_SET var value)
	SET(${var} "${value}")
    ENDMACRO(_MANAGE_VARIABLE_SET var value)

    # it deals the "encoded" line.
    FUNCTION(SETTING_STRING_GET_VARIABLE var value str )
	SET(setting_sign "=")
	SET(_NOUNQUOTE "")
	SET(_NOREPLACE "")
	FOREACH(_arg ${ARGN})
	    IF (${_arg} STREQUAL "NOUNQUOTE")
		SET(_NOUNQUOTE "NOUNQUOTE")
	    ELSEIF (${_arg} STREQUAL "NOREPLACE")
		SET(_NOREPLACE "NOREPLACE")
	    ELSE(${_arg} STREQUAL "NOUNQUOTE")
		SET(setting_sign ${_arg})
	    ENDIF(${_arg} STREQUAL "NOUNQUOTE")
	ENDFOREACH(_arg ${ARGN})

	STRING_SPLIT(_tokens "${setting_sign}" "${str}" 2)
	#MESSAGE("_tokens=${_tokens}")
	SET(_varName "")
	SET(_val "")
	FOREACH(_token ${_tokens})
	    #MESSAGE("_varName=${_varName} _token=${_token}")
	    IF(_varName STREQUAL "")
		SET(_varName "${_token}")
	    ELSE(_varName STREQUAL "")
		SET(_val "${_token}")
	    ENDIF(_varName STREQUAL "")
	ENDFOREACH(_token ${_tokens})
	#MESSAGE("_varName=${_varName} _val=${_val}")

	SET(${var} "${_varName}" PARENT_SCOPE)
	# Set var when
	# 1. NOREPLACE is not set, or
	# 2. var has value already.
	SET(_setVar 0)
	IF(_NOREPLACE STREQUAL "")
	    STRING_TRIM(_value "${_val}" ${_NOUNQUOTE})
	ELSEIF(${var} STREQUAL "")
	    STRING_TRIM(_value "${_val}" ${_NOUNQUOTE})
	ELSE(_NOREPLACE STREQUAL "")
	    SET(_value "${${var}}")
	ENDIF(_NOREPLACE STREQUAL "")
	SET(${value} "${_value}" PARENT_SCOPE)
	#MESSAGE("_varName=${_varName} _value=${_value}")

    ENDFUNCTION(SETTING_STRING_GET_VARIABLE var str)

    # Internal macro
    # Similar to STRING_ESCAPE, but read directly from file,
    # This avoid the variable substitution
    # Variable escape is enforced.
    MACRO(FILE_READ_ESCAPE var filename)
	# '$' is very tricky.
	# '$' => '#D'
	GET_FILENAME_COMPONENT(_filename_abs "${filename}" ABSOLUTE)
	EXECUTE_PROCESS(COMMAND cat ${filename}
	    COMMAND sed -e "s/#/#H/g"
	    COMMAND sed -e "s/[$]/#D/g"
	    COMMAND sed -e "s/;/#S/g"
	    COMMAND sed -e "s/[\\]/#B/g"
	    OUTPUT_VARIABLE _ret
	    OUTPUT_STRIP_TRAILING_WHITESPACE)

	STRING(REGEX REPLACE "\n" ";" _ret "${_ret}")
	#MESSAGE("_ret=|${_ret}|")
	SET(${var} "${_ret}")
    ENDMACRO(FILE_READ_ESCAPE var filename)

    MACRO(SETTING_FILE_GET_VARIABLES_PATTERN var attr_pattern setting_file)
	IF("${setting_file}" STREQUAL "")
	    M_MSG(${M_FATAL} "SETTING_FILE_GET_VARIABLES_PATTERN: setting_file ${setting_file} is empty")
	ENDIF("${setting_file}" STREQUAL "")
	SET(setting_sign "=")
	SET(_noUnQuoted "")
	SET(_noEscapeSemicolon "")
	SET(_noReplace "")
	SET(_escapeVariable "")
	FOREACH(_arg ${ARGN})
	    IF (${_arg} STREQUAL "NOUNQUOTE")
		SET(_noUnQuoted "NOUNQUOTE")
	    ELSEIF (${_arg} STREQUAL "NOREPLACE")
		SET(_noReplace "NOREPLACE")
	    ELSEIF (${_arg} STREQUAL "NOESCAPE_SEMICOLON")
		SET(_noEscapeSemicolon "NOESCAPE_SEMICOLON")
	    ELSEIF (${_arg} STREQUAL "ESCAPE_VARIABLE")
		SET(_escapeVariable "ESCAPE_VARIABLE")
	    ELSE(${_arg} STREQUAL "NOUNQUOTE")
		SET(setting_sign ${_arg})
	    ENDIF(${_arg} STREQUAL "NOUNQUOTE")
	ENDFOREACH(_arg)

	# Escape everything to be safe.
	FILE_READ_ESCAPE(_lines "${setting_file}")

	#STRING_SPLIT(_lines "\n" "${_txt_content}")
	#MESSAGE("_lines=|${_lines}|")
	SET(_actual_line "")
	SET(_join_next 0)
	FOREACH(_line ${_lines})
	    #MESSAGE("_line=|${_line}|")
	    IF(NOT _line MATCHES "^[ \\t]*#H")
		# Not a comment line.
		IF(_join_next EQUAL 1)
		    SET(_actual_line "${_actual_line}${_line}" )
		ELSE(_join_next EQUAL 1)
		    SET(_actual_line "${_line}")
		ENDIF(_join_next EQUAL 1)
		#MESSAGE("_actual_line=|${_actual_line}|")

		IF(_actual_line MATCHES "#B$")
		    #Join the lines that end with \\
		    SET(_join_next 1)
		    STRING(REGEX REPLACE "#B$" "" _actual_line "${_actual_line}")
		ELSE(_actual_line MATCHES "#B$")
		    SET(_join_next 0)
		    IF(_actual_line MATCHES "[ \\t]*${attr_pattern}[ \\t]*${setting_sign}")
			#MESSAGE("*** matched_line=|${_actual_line}|")
			SETTING_STRING_GET_VARIABLE(_attr _value
			    "${_actual_line}" ${setting_sign} ${_noUnQuoted} )
			#MESSAGE("*** _attr=${_attr} _value=${_value}")
			IF(_noReplace STREQUAL "" OR NOT DEFINED ${_attr})
			    # Unencoding
			    _STRING_UNESCAPE(_value "${_value}" ${_noEscapeSemicolon} ESCAPE_VARIABLE)
			    IF(_escapeVariable STREQUAL "")
				# Variable should not be escaped
				# i.e. need substitution
				_MANAGE_VARIABLE_SET(_value "${_value}")
			    ENDIF(_escapeVariable STREQUAL "")
			    IF("${var}" STREQUAL "")
				SET(${_attr} "${_value}")
			    ELSE("${var}" STREQUAL "")
				SET(${var} "${_value}")
			    ENDIF("${var}" STREQUAL "")
			ENDIF(_noReplace STREQUAL "" OR NOT DEFINED ${_attr})
		    ENDIF(_actual_line MATCHES "[ \\t]*${attr_pattern}[ \\t]*${setting_sign}")

		ENDIF(_actual_line MATCHES "#B$")

	    ENDIF(NOT _line MATCHES "^[ \\t]*#H")
	ENDFOREACH(_line ${_lines})
	#SET(${var} "${_value}")

    ENDMACRO(SETTING_FILE_GET_VARIABLES_PATTERN var attr_pattern setting_file)

    MACRO(SETTING_FILE_GET_VARIABLE var attr_name setting_file)
	SETTING_FILE_GET_VARIABLES_PATTERN(${var} "${attr_name}"
	    "${setting_file}" ${ARGN})
    ENDMACRO(SETTING_FILE_GET_VARIABLE var attr_name setting_file)

    MACRO(SETTING_FILE_GET_ALL_VARIABLES setting_file)
	SETTING_FILE_GET_VARIABLES_PATTERN("" "[A-Za-z_][A-Za-z0-9_.]*"
	    "${setting_file}" ${ARGN})
    ENDMACRO(SETTING_FILE_GET_ALL_VARIABLES setting_file)

    MACRO(GET_ENV var default_value)
	IF(${ARGC} GREATER 2)
	    SET(_env "${ARGV2}")
	ELSE(${ARGC} GREATER 2)
	    SET(_env "${var}")
	ENDIF(${ARGC} GREATER 2)

	IF ("$ENV{${_env}}" STREQUAL "")
	    SET(${var} "${default_value}")
	ELSE("$ENV{${_env}}" STREQUAL "")
	    SET(${var} "$ENV{${_env}}")
	ENDIF("$ENV{${_env}}" STREQUAL "")
	# MESSAGE("Variable ${var}=${${var}}")
    ENDMACRO(GET_ENV var default_value)

    MACRO(SET_VAR var untrimmedValue)
	SET(_noUnQuoted "")
	FOREACH(_arg ${ARGN})
	    IF (${_arg} STREQUAL "NOUNQUOTE")
		SET(_noUnQuoted "NOUNQUOTE")
	    ENDIF(${_arg} STREQUAL "NOUNQUOTE")
	ENDFOREACH(_arg ${ARGN})
	#MESSAGE("untrimmedValue=${untrimmedValue}")
	IF ("${untrimmedValue}" STREQUAL "")
	    SET(${var} "")
	ELSE("${untrimmedValue}" STREQUAL "")
	    STRING_TRIM(trimmedValue "${untrimmedValue}" ${_noUnQuoted})
	    #MESSAGE("***SET_VAR: trimmedValue=${trimmedValue}")
	    SET(${var} "${trimmedValue}")
	ENDIF("${untrimmedValue}" STREQUAL "")
	#SET(value "${${var}}")
	#MESSAGE("***SET_VAR: ${var}=|${value}|")
    ENDMACRO(SET_VAR var untrimmedValue)

ENDIF(NOT DEFINED _MANAGE_VARIABLE_CMAKE_)