jdk

Форк
0
/
util.m4 
829 строк · 31.7 Кб
1
#
2
# Copyright (c) 2011, 2024, Oracle and/or its affiliates. All rights reserved.
3
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
#
5
# This code is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License version 2 only, as
7
# published by the Free Software Foundation.  Oracle designates this
8
# particular file as subject to the "Classpath" exception as provided
9
# by Oracle in the LICENSE file that accompanied this code.
10
#
11
# This code is distributed in the hope that it will be useful, but WITHOUT
12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14
# version 2 for more details (a copy is included in the LICENSE file that
15
# accompanied this code).
16
#
17
# You should have received a copy of the GNU General Public License version
18
# 2 along with this work; if not, write to the Free Software Foundation,
19
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
#
21
# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
# or visit www.oracle.com if you need additional information or have any
23
# questions.
24
#
25

26
m4_include([util_paths.m4])
27

28
###############################################################################
29
# Create a function/macro that takes a series of named arguments. The call is
30
# similar to AC_DEFUN, but the setup of the function looks like this:
31
# UTIL_DEFUN_NAMED([MYFUNC], [FOO *BAR], [$@], [
32
# ... do something
33
#   AC_MSG_NOTICE([Value of BAR is ARG_BAR])
34
# ])
35
# A star (*) in front of a named argument means that it is required and it's
36
# presence will be verified. To pass e.g. the first value as a normal indexed
37
# argument, use [m4_shift($@)] as the third argument instead of [$@]. These
38
# arguments are referenced in the function by their name prefixed by ARG_, e.g.
39
# "ARG_FOO".
40
#
41
# The generated function can be called like this:
42
# MYFUNC(FOO: [foo-val],
43
#     BAR: [
44
#         $ECHO hello world
45
#     ])
46
# Note that the argument value must start on the same line as the argument name.
47
#
48
# Argument 1: Name of the function to define
49
# Argument 2: List of legal named arguments, with a * prefix for required arguments
50
# Argument 3: Argument array to treat as named, typically $@
51
# Argument 4: The main function body
52
AC_DEFUN([UTIL_DEFUN_NAMED],
53
[
54
  AC_DEFUN($1, [
55
    m4_foreach([arg], m4_split(m4_normalize($2)), [
56
      m4_if(m4_bregexp(arg, [^\*]), -1,
57
        [
58
          m4_set_add(legal_named_args, arg)
59
        ],
60
        [
61
          m4_set_add(legal_named_args, m4_substr(arg, 1))
62
          m4_set_add(required_named_args, m4_substr(arg, 1))
63
        ]
64
      )
65
    ])
66

67
    # Delicate quoting and unquoting sequence to ensure the actual value is passed along unchanged
68
    # For details on how this works, see https://git.openjdk.org/jdk/pull/11458#discussion_r1038173051
69
    # WARNING: Proceed at the risk of your own sanity, getting this to work has made me completely
70
    # incapable of feeling love or any other positive emotion
71
    # ~Julian
72
    m4_foreach([arg], m4_dquote(m4_dquote_elt($3)), [
73
      m4_if(m4_index(arg, [: ]), -1, [m4_define([arg], m4_dquote(m4_bpatsubst(m4_dquote(arg), [:], [: ])))])
74
      m4_define(arg_name, m4_substr(arg, 0, m4_index(arg, [: ])))
75
      m4_set_contains(legal_named_args, arg_name, [],[AC_MSG_ERROR([Internal error: m4_if(arg_name, , arg, arg_name) is not a valid named argument to [$1]. Valid arguments are 'm4_set_contents(defined_args, [ ]) m4_set_contents(legal_named_args, [ ])'.])])
76
      m4_set_remove(required_named_args, arg_name)
77
      m4_set_remove(legal_named_args, arg_name)
78
      m4_pushdef([ARG_][]arg_name, m4_bpatsubst(m4_bpatsubst(m4_dquote(m4_dquote(arg)), arg_name[: ]), [^\s*]))
79
      m4_set_add(defined_args, arg_name)
80
      m4_undefine([arg_name])
81
    ])
82
    m4_set_empty(required_named_args, [], [
83
      AC_MSG_ERROR([Internal error: Required named arguments are missing for [$1]. Missing arguments: 'm4_set_contents(required_named_args, [ ])'])
84
    ])
85
    m4_foreach([arg], m4_indir([m4_dquote]m4_set_listc([legal_named_args])), [
86
      m4_pushdef([ARG_][]arg, [])
87
      m4_set_add(defined_args, arg)
88
    ])
89
    m4_set_delete(legal_named_args)
90
    m4_set_delete(required_named_args)
91

92
    # Execute function body
93
    $4
94

95
    m4_foreach([arg], m4_indir([m4_dquote]m4_set_listc([defined_args])), [
96
      m4_popdef([ARG_][]arg)
97
    ])
98

99
    m4_set_delete(defined_args)
100
  ])
101
])
102

103
###############################################################################
104
# Assert that a programmatic condition holds. If not, exit with an error message.
105
# Check that a shell expression gives return code 0
106
#
107
# $1: The shell expression to evaluate
108
# $2: A message to describe the expression in case of failure
109
# $2: An message to print in case of failure [optional]
110
#
111
AC_DEFUN([UTIL_ASSERT_SHELL_TEST],
112
[
113
  ASSERTION_MSG="m4_normalize([$3])"
114
  if $1; then
115
    $ECHO Assertion failed: $2
116
    if test "x$3" != x; then
117
      $ECHO Assertion message: "$3"
118
    fi
119
    exit 1
120
  fi
121
])
122

123

124
###############################################################################
125
# Assert that a programmatic condition holds. If not, exit with an error message.
126
# Check that two strings are equal.
127
#
128
# $1: The actual string found
129
# $2: The expected string
130
# $3: An message to print in case of failure [optional]
131
#
132
AC_DEFUN([UTIL_ASSERT_STRING_EQUALS],
133
[
134
  UTIL_ASSERT_SHELL_TEST(
135
      [test "x[$1]" != "x[$2]"],
136
      [Actual value '[$1]' \("[$1]"\) did not match expected value '[$2]' \("[$2]"\)],
137
      $3)
138
])
139

140
###############################################################################
141
# Assert that a programmatic condition holds. If not, exit with an error message.
142
# Check that two strings not are equal.
143
#
144
# $1: The actual string found
145
# $2: The expected string
146
# $3: An message to print in case of failure [optional]
147
#
148
AC_DEFUN([UTIL_ASSERT_STRING_NOT_EQUALS],
149
[
150
  UTIL_ASSERT_SHELL_TEST(
151
      [test "x[$1]" = "x[$2]"],
152
      [Actual value '[$1]' \("[$1]"\) unexpectedly matched '[$2]' \("[$2]"\)],
153
      $3)
154
])
155

156
###############################################################################
157
# Assert that a programmatic condition holds. If not, exit with an error message.
158
# Check that the given expression evaluates to the string 'true'
159
#
160
# $1: The expression to evaluate
161
# $2: An message to print in case of failure [optional]
162
#
163
AC_DEFUN([UTIL_ASSERT_TRUE],
164
[
165
  UTIL_ASSERT_STRING_EQUALS($1, true, $3)
166
])
167

168
###############################################################################
169
# Assert that a programmatic condition holds. If not, exit with an error message.
170
# Check that the given expression does not evaluate to the string 'true'
171
#
172
# $1: The expression to evaluate
173
# $2: An message to print in case of failure [optional]
174
#
175
AC_DEFUN([UTIL_ASSERT_NOT_TRUE],
176
[
177
  UTIL_ASSERT_STRING_NOT_EQUALS($1, true, $3)
178
])
179

180
###############################################################################
181
# Check if a list of space-separated words are selected only from a list of
182
# space-separated legal words. Typical use is to see if a user-specified
183
# set of words is selected from a set of legal words.
184
#
185
# Sets the specified variable to list of non-matching (offending) words, or to
186
# the empty string if all words are matching the legal set.
187
#
188
# $1: result variable name
189
# $2: list of values to check
190
# $3: list of legal values
191
AC_DEFUN([UTIL_GET_NON_MATCHING_VALUES],
192
[
193
  # grep filter function inspired by a comment to http://stackoverflow.com/a/1617326
194
  # Notice that the original variant fails on SLES 10 and 11
195
  # Some grep versions (at least bsd) behaves strangely on the base case with
196
  # no legal_values, so make it explicit.
197
  values_to_check=`$ECHO $2 | $TR ' ' '\n'`
198
  legal_values=`$ECHO $3 | $TR ' ' '\n'`
199
  if test -z "$legal_values"; then
200
    $1="$2"
201
  else
202
    result=`$GREP -Fvx -- "$legal_values" <<< "$values_to_check" | $GREP -v '^$'`
203
    $1=${result//$'\n'/ }
204
  fi
205
])
206

207
###############################################################################
208
# Check if a list of space-separated words contains any word(s) from a list of
209
# space-separated illegal words. Typical use is to see if a user-specified
210
# set of words contains any from a set of illegal words.
211
#
212
# Sets the specified variable to list of matching illegal words, or to
213
# the empty string if no words are matching the illegal set.
214
#
215
# $1: result variable name
216
# $2: list of values to check
217
# $3: list of illegal values
218
AC_DEFUN([UTIL_GET_MATCHING_VALUES],
219
[
220
  # grep filter function inspired by a comment to http://stackoverflow.com/a/1617326
221
  # Notice that the original variant fails on SLES 10 and 11
222
  # Some grep versions (at least bsd) behaves strangely on the base case with
223
  # no legal_values, so make it explicit.
224
  values_to_check=`$ECHO $2 | $TR ' ' '\n'`
225
  illegal_values=`$ECHO $3 | $TR ' ' '\n'`
226
  if test -z "$illegal_values"; then
227
    $1=""
228
  else
229
    result=`$GREP -Fx -- "$illegal_values" <<< "$values_to_check" | $GREP -v '^$'`
230
    $1=${result//$'\n'/ }
231
  fi
232
])
233

234
###############################################################################
235
# Converts an ISO-8601 date/time string to a unix epoch timestamp. If no
236
# suitable conversion method was found, an empty string is returned.
237
#
238
# $1: result variable name
239
# $2: input date/time string
240
AC_DEFUN([UTIL_GET_EPOCH_TIMESTAMP],
241
[
242
  if test "x$IS_GNU_DATE" = xyes; then
243
    # GNU date
244
    timestamp=$($DATE --utc --date=$2 +"%s" 2> /dev/null)
245
  else
246
    # BSD date
247
    # ISO-8601 date&time in Zulu 'date'T'time'Z
248
    timestamp=$($DATE -u -j -f "%FT%TZ" "$2" "+%s" 2> /dev/null)
249
    if test "x$timestamp" = x; then
250
      # BSD date cannot handle trailing milliseconds.
251
      # Try again ignoring characters at end
252
      timestamp=$($DATE -u -j -f "%Y-%m-%dT%H:%M:%S" "$2" "+%s" 2> /dev/null)
253
    fi
254
    if test "x$timestamp" = x; then
255
      # Perhaps the time was missing.
256
      timestamp=$($DATE -u -j -f "%FT%TZ" "$2""T00:00:00Z" "+%s" 2> /dev/null)
257
    fi
258
  fi
259
  $1=$timestamp
260
])
261

262
###############################################################################
263
# Sort a space-separated list, and remove duplicates.
264
#
265
# Sets the specified variable to the resulting list.
266
#
267
# $1: result variable name
268
# $2: list of values to sort
269
AC_DEFUN([UTIL_SORT_LIST],
270
[
271
  values_to_sort=`$ECHO $2 | $TR ' ' '\n'`
272
  result=`$SORT -u <<< "$values_to_sort" | $GREP -v '^$'`
273
  $1=${result//$'\n'/ }
274
])
275

276
###############################################################################
277
# Test if $1 is a valid argument to $3 (often is $JAVA passed as $3)
278
# If so, then append $1 to $2 \
279
# Also set JVM_ARG_OK to true/false depending on outcome.
280
AC_DEFUN([UTIL_ADD_JVM_ARG_IF_OK],
281
[
282
  $ECHO "Check if jvm arg is ok: $1" >&AS_MESSAGE_LOG_FD
283
  $ECHO "Command: $3 $1 -version" >&AS_MESSAGE_LOG_FD
284
  OUTPUT=`$3 $1 $USER_BOOT_JDK_OPTIONS -version 2>&1`
285
  FOUND_WARN=`$ECHO "$OUTPUT" | $GREP -i warn`
286
  FOUND_VERSION=`$ECHO $OUTPUT | $GREP " version \""`
287
  if test "x$FOUND_VERSION" != x && test "x$FOUND_WARN" = x; then
288
    $2="[$]$2 $1"
289
    JVM_ARG_OK=true
290
  else
291
    $ECHO "Arg failed:" >&AS_MESSAGE_LOG_FD
292
    $ECHO "$OUTPUT" >&AS_MESSAGE_LOG_FD
293
    JVM_ARG_OK=false
294
  fi
295
])
296

297
###############################################################################
298
# Register a --with argument but mark it as deprecated
299
# $1: The name of the with argument to deprecate, not including --with-
300
AC_DEFUN([UTIL_DEPRECATED_ARG_WITH],
301
[
302
  AC_ARG_WITH($1, [AS_HELP_STRING([--with-$1],
303
      [Deprecated. Option is kept for backwards compatibility and is ignored])],
304
      [AC_MSG_WARN([Option --with-$1 is deprecated and will be ignored.])])
305
])
306

307
###############################################################################
308
# Register a --enable argument but mark it as deprecated
309
# $1: The name of the with argument to deprecate, not including --enable-
310
AC_DEFUN([UTIL_DEPRECATED_ARG_ENABLE],
311
[
312
  AC_ARG_ENABLE($1, [AS_HELP_STRING([--enable-$1],
313
      [Deprecated. Option is kept for backwards compatibility and is ignored])],
314
      [AC_MSG_WARN([Option --enable-$1 is deprecated and will be ignored.])])
315
])
316

317
###############################################################################
318
# Register an --enable-* argument as an alias for another argument.
319
# $1: The name of the enable argument for the new alias, not including --enable-
320
# $2: The full name of the argument of which to make this an alias, including
321
#     --enable- or --with-.
322
AC_DEFUN([UTIL_ALIASED_ARG_ENABLE],
323
[
324
  AC_ARG_ENABLE($1, [AS_HELP_STRING([--enable-$1], [alias for $2])], [
325
    # Use m4 to strip initial -- from target ($2), convert - to _, prefix enable_
326
    # to new alias name, and create a shell variable assignment,
327
    # e.g.: enable_old_style="$enable_new_alias"
328
    m4_translit(m4_bpatsubst($2, --), -, _)="$[enable_]m4_translit($1, -, _)"
329
  ])
330
])
331

332
###############################################################################
333
# Creates a command-line option using the --enable-* pattern. Will return a
334
# value of 'true' or 'false' in the RESULT variable, depending on whether the
335
# option was enabled or not by the user. The option can not be turned on if it
336
# is not available, as specified by AVAILABLE and/or CHECK_AVAILABLE.
337
#
338
# Arguments:
339
#   NAME: The base name of this option (i.e. what follows --enable-). Required.
340
#   RESULT: The name of the variable to set to the result. Defaults to
341
#     <NAME in uppercase>_ENABLED.
342
#   DEFAULT: The default value for this option. Can be true, false or auto.
343
#     Defaults to true.
344
#   AVAILABLE: If true, this option is allowed to be selected. Defaults to true.
345
#   DESC: A description of this option. Defaults to a generic and unhelpful
346
#     string.
347
#   DEFAULT_DESC: A message describing the default value, for the help. Defaults
348
#     to the literal value of DEFAULT.
349
#   CHECKING_MSG: The message to present to user when checking this option.
350
#     Defaults to a generic message.
351
#   CHECK_AVAILABLE: An optional code block to execute to determine if the
352
#     option should be available. Must set AVAILABLE to 'false' if not.
353
#   IF_GIVEN:  An optional code block to execute if the option was given on the
354
#     command line (regardless of the value).
355
#   IF_NOT_GIVEN:  An optional code block to execute if the option was not given
356
#     on the command line (regardless of the value).
357
#   IF_ENABLED:  An optional code block to execute if the option is turned on.
358
#   IF_DISABLED:  An optional code block to execute if the option is turned off.
359
#
360
UTIL_DEFUN_NAMED([UTIL_ARG_ENABLE],
361
    [*NAME RESULT DEFAULT AVAILABLE DESC DEFAULT_DESC CHECKING_MSG
362
    CHECK_AVAILABLE IF_GIVEN IF_NOT_GIVEN IF_ENABLED IF_DISABLED], [$@],
363
[
364
  ##########################
365
  # Part 1: Set up m4 macros
366
  ##########################
367

368
  # If DEFAULT is not specified, set it to 'true'.
369
  m4_define([ARG_DEFAULT], m4_if(ARG_DEFAULT, , true, ARG_DEFAULT))
370

371
  # If AVAILABLE is not specified, set it to 'true'.
372
  m4_define([ARG_AVAILABLE], m4_if(ARG_AVAILABLE, , true, ARG_AVAILABLE))
373

374
  # If DEFAULT_DESC is not specified, calculate it from DEFAULT.
375
  m4_define([ARG_DEFAULT_DESC], m4_if(ARG_DEFAULT_DESC, , m4_if(ARG_DEFAULT, true, enabled, m4_if(ARG_DEFAULT, false, disabled, ARG_DEFAULT)), ARG_DEFAULT_DESC))
376

377
  # If RESULT is not specified, set it to 'ARG_NAME[_ENABLED]'.
378
  m4_define([ARG_RESULT], m4_if(ARG_RESULT, , m4_translit(ARG_NAME, [a-z-], [A-Z_])[_ENABLED], ARG_RESULT))
379
  # Construct shell variable names for the option
380
  m4_define(ARG_OPTION, [enable_]m4_translit(ARG_NAME, [-], [_]))
381
  m4_define(ARG_GIVEN, m4_translit(ARG_NAME, [a-z-], [A-Z_])[_GIVEN])
382

383
  # If DESC is not specified, set it to a generic description.
384
  m4_define([ARG_DESC], m4_if(m4_quote(ARG_DESC), , [[Enable the ARG_NAME feature]], [m4_normalize(ARG_DESC)]))
385

386
  # If CHECKING_MSG is not specified, set it to a generic description.
387
  m4_define([ARG_CHECKING_MSG], m4_if(m4_quote(ARG_CHECKING_MSG), , [[for --enable-ARG_NAME]], [m4_normalize(ARG_CHECKING_MSG)]))
388

389
  # If the code blocks are not given, set them to the empty statements to avoid
390
  # tripping up bash.
391
  m4_if(ARG_CHECK_AVAILABLE, , [m4_define([ARG_CHECK_AVAILABLE], [:])])
392
  m4_if(ARG_IF_GIVEN, , [m4_define([ARG_IF_GIVEN], [:])])
393
  m4_if(ARG_IF_NOT_GIVEN, , [m4_define([ARG_IF_NOT_GIVEN], [:])])
394
  m4_if(ARG_IF_ENABLED, , [m4_define([ARG_IF_ENABLED], [:])])
395
  m4_if(ARG_IF_DISABLED, , [m4_define([ARG_IF_DISABLED], [:])])
396

397
  ##########################
398
  # Part 2: Set up autoconf shell code
399
  ##########################
400

401
  # Check that DEFAULT has a valid value
402
  if test "[x]ARG_DEFAULT" != xtrue && test "[x]ARG_DEFAULT" != xfalse && \
403
      test "[x]ARG_DEFAULT" != xauto ; then
404
    AC_MSG_ERROR([Internal error: Argument DEFAULT to [UTIL_ARG_ENABLE] can only be true, false or auto, was: 'ARG_DEFAULT'])
405
  fi
406

407
  # Check that AVAILABLE has a valid value
408
  if test "[x]ARG_AVAILABLE" != xtrue && test "[x]ARG_AVAILABLE" != xfalse; then
409
    AC_MSG_ERROR([Internal error: Argument AVAILABLE to [UTIL_ARG_ENABLE] can only be true or false, was: 'ARG_AVAILABLE'])
410
  fi
411

412
  AC_ARG_ENABLE(ARG_NAME, AS_HELP_STRING([--enable-]ARG_NAME,
413
      [ARG_DESC [ARG_DEFAULT_DESC]]), [ARG_GIVEN=true], [ARG_GIVEN=false])
414

415
  # Check if the option is available
416
  AVAILABLE=ARG_AVAILABLE
417
  # Run the available check block (if any), which can overwrite AVAILABLE.
418
  ARG_CHECK_AVAILABLE
419

420
  # Check if the option should be turned on
421
  AC_MSG_CHECKING(ARG_CHECKING_MSG)
422
  if test x$ARG_GIVEN = xfalse; then
423
    if test ARG_DEFAULT = auto; then
424
      # If not given, and default is auto, set it to true iff it's available.
425
      ARG_RESULT=$AVAILABLE
426
      REASON="from default 'auto'"
427
    else
428
      ARG_RESULT=ARG_DEFAULT
429
      REASON="default"
430
    fi
431
  else
432
    if test x$ARG_OPTION = xyes; then
433
      ARG_RESULT=true
434
      REASON="from command line"
435
    elif test x$ARG_OPTION = xno; then
436
      ARG_RESULT=false
437
      REASON="from command line"
438
    elif test x$ARG_OPTION = xauto; then
439
      if test ARG_DEFAULT = auto; then
440
        # If both given and default is auto, set it to true iff it's available.
441
        ARG_RESULT=$AVAILABLE
442
      else
443
        ARG_RESULT=ARG_DEFAULT
444
      fi
445
      REASON="from command line 'auto'"
446
    else
447
      AC_MSG_ERROR([Option [--enable-]ARG_NAME can only be 'yes', 'no' or 'auto'])
448
    fi
449
  fi
450

451
  if test x$ARG_RESULT = xtrue; then
452
    AC_MSG_RESULT([enabled, $REASON])
453
    if test x$AVAILABLE = xfalse; then
454
      AC_MSG_ERROR([Option [--enable-]ARG_NAME is not available])
455
    fi
456
  else
457
    AC_MSG_RESULT([disabled, $REASON])
458
  fi
459

460
  # Execute result payloads, if present
461
  if test x$ARG_GIVEN = xtrue; then
462
    ARG_IF_GIVEN
463
  else
464
    ARG_IF_NOT_GIVEN
465
  fi
466

467
  if test x$ARG_RESULT = xtrue; then
468
    ARG_IF_ENABLED
469
  else
470
    ARG_IF_DISABLED
471
  fi
472
])
473

474
###############################################################################
475
# Helper functions for ARG_WITH, to validate different types of argument
476

477
# Dispatcher to call the correct UTIL_CHECK_TYPE_* function depending on the ARG_TYPE
478
AC_DEFUN([UTIL_CHECK_TYPE],
479
[
480
  UTIL_CHECK_TYPE_$1($2)
481
])
482

483
AC_DEFUN([UTIL_CHECK_TYPE_string],
484
[
485
  # All strings always passes
486
])
487

488
AC_DEFUN([UTIL_CHECK_TYPE_integer],
489
[
490
  # Check that the argument is an integer
491
  # Additional [] needed to keep m4 from mangling shell constructs.
492
  [ if [[ ! "$1" =~ ^[0-9]+$ ]] ; then ]
493
    FAILURE="Not an integer: $1"
494
  fi
495
])
496

497
AC_DEFUN([UTIL_CHECK_TYPE_file],
498
[
499
  # Check that the argument is an existing file
500
  if test ! -f "$1" ; then
501
    FAILURE="File $1 does not exist or is not readable"
502
  fi
503
])
504

505
AC_DEFUN([UTIL_CHECK_TYPE_directory],
506
[
507
  # Check that the argument is an existing directory
508
  if test ! -d "$1" ; then
509
    FAILURE="Directory $1 does not exist or is not readable"
510
  fi
511

512
  if test "[x]ARG_CHECK_FOR_FILES" != "x:"; then
513
    for file in ARG_CHECK_FOR_FILES; do
514
      found_files=$($ECHO $(ls $1/$file 2> /dev/null))
515
      if test "x$found_files" = x; then
516
        FAILURE="Directory $1 does not contain $file"
517
        break
518
      elif ! test -e "$found_files"; then
519
        FAILURE="Directory $1 contains multiple $file: $found_files"
520
        break
521
      fi
522
    done
523
  fi
524
])
525

526
AC_DEFUN([UTIL_CHECK_TYPE_literal],
527
[
528
  # Check if it contains a space between non-space characters
529
  # Additional [] needed to keep m4 from mangling shell constructs.
530
  [ if [[ "$1" =~ [^' ']' '+[^' '] ]] ; then ]
531
    FAILURE="Multiple words: $1"
532
  fi
533

534
  # Check that the selected variants are valid
535
  UTIL_GET_NON_MATCHING_VALUES(invalid_value, $1, \
536
      ARG_VALID_VALUES)
537
  if test "x$invalid_value" != x; then
538
    FAILURE="Invalid value: $invalid_value. Valid values are: ARG_VALID_VALUES"
539
  fi
540
])
541

542
AC_DEFUN([UTIL_CHECK_TYPE_multivalue],
543
[
544
  # We accept either space or comma as separator, but use space internally
545
  values=`$ECHO $1 | $SED -e 's/,/ /g'`
546

547
  # Check that the selected variants are valid
548
  UTIL_GET_NON_MATCHING_VALUES(invalid_value, $values, \
549
      ARG_VALID_VALUES)
550
  if test "x$invalid_value" != x; then
551
    FAILURE="Invalid value(s): $invalid_value. Valid values are: ARG_VALID_VALUES"
552
  fi
553

554
  # Update to version without comma
555
  ARG_RESULT=$($ECHO $values)
556
])
557

558
AC_DEFUN([UTIL_CHECK_TYPE_features],
559
[
560
  # We accept either space or comma as separator, but use space internally
561
  feature_list=`$ECHO $1 | $SED -e 's/,/ /g'`
562
  features_enabled=`$ECHO $feature_list | \
563
    $AWK '{ for (i=1; i<=NF; i++) if (!match($i, /^-.*/)) printf("%s ", $i) }'`
564
  features_disabled=`$ECHO $feature_list | \
565
    $AWK '{ for (i=1; i<=NF; i++) if (match($i, /^-.*/)) printf("%s ", substr($i, 2))}'`
566

567
  # Check that the selected features are valid
568
  UTIL_GET_NON_MATCHING_VALUES(invalid_features, $features_enabled \
569
        $features_disabled, ARG_VALID_VALUES)
570
  if test "x$invalid_features" != x; then
571
    FAILURE="Invalid feature(s): $invalid_features. Valid values are: ARG_VALID_VALUES"
572
  fi
573

574
  # Update to version without comma
575
  ARG_RESULT=$($ECHO $feature_list)
576
])
577

578
###############################################################################
579
# Creates a command-line option using the --with-* pattern. Will return a
580
# string in the RESULT variable with the option provided by the user, or the
581
# empty string if the --with-* option was not given. The option can not be given
582
# if it is not available, as specified by AVAILABLE and/or CHECK_AVAILABLE.
583
#
584
# Arguments:
585
#   NAME: The base name of this option (i.e. what follows --with-). Required.
586
#   TYPE: The type of the value. Can be one of "string", "integer", "file",
587
#     "directory", "literal", "multivalue" or "features". Required.
588
#   DEFAULT: The default value for this option. Can be any valid string.
589
#     Required.
590
#   OPTIONAL: If this feature can be disabled. Defaults to false. If true,
591
#     the feature can be disabled using --without-FOO, --with-FOO=no, or
592
#     --with-FOO=. Check the ENABLED_RESULT variable for the enabled/disabled
593
#     state.
594
#   RESULT: The name of the variable to set to the result. Defaults to
595
#     <NAME in uppercase>. Set to empty if ENABLED_RESULT is false.
596
#   ENABLED_DEFAULT: If the value is enabled by default. Defaults to false. Only
597
#     relevant if OPTIONAL is true.
598
#   ENABLED_RESULT: The name of the variable to set to the enabled/disabled
599
#     result state. Defaults to <NAME in uppercase>_ENABLED.
600
#   AVAILABLE: If true, this option is allowed to be selected. Defaults to true.
601
#   DESC: A description of this option. Defaults to a generic and unhelpful
602
#     string.
603
#   DEFAULT_DESC: A message describing the default value, for the help. Defaults
604
#     to the literal value of DEFAULT, or "<none>" if DEFAULT is empty.
605
#   CHECKING_MSG: The message to present to user when checking this option.
606
#     Defaults to a generic message.
607
#   CHECK_AVAILABLE: An optional code block to execute to determine if the
608
#     option should be available. Must set AVAILABLE to 'false' if not.
609
#   VALID_VALUES: A list of literals that are the allowed values. Only valid if
610
#     TYPE is "literal", "multivalue" or "features".
611
#   CHECK_VALUE: An optional code block to execute to determine if the value
612
#     is correct. Must set FAILURE to a non-empty string if not. This string
613
#     will be displayed. The value is given in $RESULT.
614
#   CHECK_FOR_FILES: A list of files to verify the presence for. Only valid if
615
#     TYPE is "directory". Paths are relative the directory given as value.
616
#     Wildcards are accepted. Exactly one matching file must be found, for each
617
#     listed file, or FAILURE is set.
618
#   IF_AUTO: An optional code block to execute if the value is "auto", either by
619
#     default or given by the command line. Must set RESULT to the calculated
620
#     value.
621
#   IF_GIVEN:  An optional code block to execute if the option was given on the
622
#     command line (regardless of the value).
623
#   IF_NOT_GIVEN:  An optional code block to execute if the option was not given
624
#     on the command line (regardless of the value).
625
#
626
UTIL_DEFUN_NAMED([UTIL_ARG_WITH],
627
    [*NAME *TYPE *DEFAULT OPTIONAL RESULT ENABLED_DEFAULT ENABLED_RESULT
628
    AVAILABLE DESC DEFAULT_DESC CHECKING_MSG CHECK_AVAILABLE VALID_VALUES
629
    CHECK_VALUE CHECK_FOR_FILES IF_AUTO IF_GIVEN IF_NOT_GIVEN], [$@],
630
[
631
  ##########################
632
  # Part 1: Set up m4 macros
633
  ##########################
634

635
  # If ENABLED_DEFAULT is not specified, set it to 'false'.
636
  m4_define([ARG_ENABLED_DEFAULT], m4_if(ARG_ENABLED_DEFAULT, , false, ARG_ENABLED_DEFAULT))
637

638
  # If AVAILABLE is not specified, set it to 'true'.
639
  m4_define([ARG_AVAILABLE], m4_if(ARG_AVAILABLE, , true, ARG_AVAILABLE))
640

641
  # If OPTIONAL is not specified, set it to 'false'.
642
  m4_define([ARG_OPTIONAL], m4_if(ARG_OPTIONAL, , false, ARG_OPTIONAL))
643

644
  # If DEFAULT_DESC is not specified, calculate it from DEFAULT.
645
  m4_define([ARG_DEFAULT_DESC], m4_if(ARG_DEFAULT_DESC, , m4_if(ARG_DEFAULT, , <none>, ARG_DEFAULT), ARG_DEFAULT_DESC))
646

647
  # If RESULT is not specified, set it to 'ARG_NAME'.
648
  m4_define([ARG_RESULT], m4_if(ARG_RESULT, , m4_translit(ARG_NAME, [a-z-], [A-Z_]), ARG_RESULT))
649

650
  # If ENABLED_RESULT is not specified, set it to 'ARG_NAME[_ENABLED]'.
651
  m4_define([ARG_ENABLED_RESULT], m4_if(ARG_ENABLED_RESULT, , m4_translit(ARG_NAME, [a-z-], [A-Z_])[_ENABLED], ARG_ENABLED_RESULT))
652

653
  # Construct shell variable names for the option
654
  m4_define(ARG_OPTION, [with_]m4_translit(ARG_NAME, [-], [_]))
655
  m4_define(ARG_GIVEN, m4_translit(ARG_NAME, [a-z-], [A-Z_])[_GIVEN])
656

657
  # If DESC is not specified, set it to a generic description.
658
  m4_define([ARG_DESC], m4_if(m4_quote(ARG_DESC), , [[Give a value for the ARG_NAME feature]], [m4_normalize(ARG_DESC)]))
659

660
  # If CHECKING_MSG is not specified, set it to a generic description.
661
  m4_define([ARG_CHECKING_MSG], m4_if(m4_quote(ARG_CHECKING_MSG), , [[for --with-ARG_NAME]], [m4_normalize(ARG_CHECKING_MSG)]))
662

663
  m4_define([ARG_HAS_AUTO_BLOCK], m4_if(ARG_IF_AUTO, , false, true))
664

665
  # If the code blocks are not given, set them to the empty statements to avoid
666
  # tripping up bash.
667
  m4_if(ARG_CHECK_AVAILABLE, , [m4_define([ARG_CHECK_AVAILABLE], [:])])
668
  m4_if(ARG_CHECK_VALUE, , [m4_define([ARG_CHECK_VALUE], [:])])
669
  m4_if(ARG_CHECK_FOR_FILES, , [m4_define([ARG_CHECK_FOR_FILES], [:])])
670
  m4_if(ARG_IF_AUTO, , [m4_define([ARG_IF_AUTO], [:])])
671
  m4_if(ARG_IF_GIVEN, , [m4_define([ARG_IF_GIVEN], [:])])
672
  m4_if(ARG_IF_NOT_GIVEN, , [m4_define([ARG_IF_NOT_GIVEN], [:])])
673

674
  ##########################
675
  # Part 2: Set up autoconf shell code
676
  ##########################
677

678
  # Check that OPTIONAL has a valid value
679
  if test "[x]ARG_OPTIONAL" != xtrue && test "[x]ARG_OPTIONAL" != xfalse ; then
680
    AC_MSG_ERROR([Internal error: Argument OPTIONAL to [UTIL_ARG_WITH] can only be true or false, was: 'ARG_OPTIONAL'])
681
  fi
682

683
  # Check that ENABLED_DEFAULT has a valid value
684
  if test "[x]ARG_ENABLED_DEFAULT" != xtrue && test "[x]ARG_ENABLED_DEFAULT" != xfalse ; then
685
    AC_MSG_ERROR([Internal error: Argument ENABLED_DEFAULT to [UTIL_ARG_WITH] can only be true or false, was: 'ARG_ENABLED_DEFAULT'])
686
  fi
687

688
  # Check that AVAILABLE has a valid value
689
  if test "[x]ARG_AVAILABLE" != xtrue && test "[x]ARG_AVAILABLE" != xfalse; then
690
    AC_MSG_ERROR([Internal error: Argument AVAILABLE to [UTIL_ARG_WITH] can only be true or false, was: 'ARG_AVAILABLE'])
691
  fi
692

693
  # Check that TYPE has a valid value
694
  # Need to assign since we can't expand ARG TYPE inside the m4 quoted if statement
695
  TEST_TYPE="ARG_TYPE"
696
  # Additional [] needed to keep m4 from mangling shell constructs.
697
  [ if [[ ! "$TEST_TYPE" =~ ^(string|integer|file|directory|literal|multivalue|features)$ ]] ; then ]
698
    AC_MSG_ERROR([Internal error: Argument TYPE to [UTIL_ARG_WITH] must be a valid type, was: 'ARG_TYPE'])
699
  fi
700

701
  AC_ARG_WITH(ARG_NAME, AS_HELP_STRING([--with-]ARG_NAME,
702
      [ARG_DESC [ARG_DEFAULT_DESC]]), [ARG_GIVEN=true], [ARG_GIVEN=false])
703

704
  # Check if the option is available
705
  AVAILABLE=ARG_AVAILABLE
706
  # Run the available check block (if any), which can overwrite AVAILABLE.
707
  ARG_CHECK_AVAILABLE
708

709
  # Check if the option should be turned on
710
  AC_MSG_CHECKING(ARG_CHECKING_MSG)
711

712
  if test x$AVAILABLE = xfalse; then
713
    ARG_RESULT="$ARG_OPTION"
714
    ARG_ENABLED_RESULT=false
715
    REASON="not available"
716
  else
717
    if test x$ARG_GIVEN = xfalse; then
718
      ARG_RESULT="ARG_DEFAULT"
719
      if test "[x]ARG_OPTIONAL" = xtrue; then
720
        ARG_ENABLED_RESULT=ARG_ENABLED_DEFAULT
721
      else
722
        ARG_ENABLED_RESULT=true
723
      fi
724
      REASON="default"
725

726
    else # ARG_GIVEN is true
727
      # Special treatment of "yes" and "no" for "--with-ARG" and "--without-ARG"
728
      if test "x$ARG_OPTION" = xyes || test "x$ARG_OPTION" = xno || test "x$ARG_OPTION" = x ; then
729
        if test "[x]ARG_OPTIONAL" = xfalse; then
730
          if test "x$ARG_OPTION" = x; then
731
            # If not optional, the empty string is a valid value
732
            ARG_RESULT=""
733
            ARG_ENABLED_RESULT=true
734
            REASON="from command line"
735
          else
736
            AC_MSG_RESULT([invalid])
737
            AC_MSG_ERROR([Option [--with-]ARG_NAME must have a specified value])
738
          fi
739
        else
740
          if test "x$ARG_OPTION" = xyes; then
741
            ARG_RESULT="ARG_DEFAULT"
742
            ARG_ENABLED_RESULT=true
743
            REASON="default as enabled from command line"
744
          else
745
            # For optional values, both --without-FOO and --with-FOO= disables
746
            ARG_RESULT=""
747
            ARG_ENABLED_RESULT=false
748
            REASON="from command line"
749
          fi
750
        fi
751
      else
752
        # The most common case -- the user gives a value for the option.
753
        ARG_RESULT="$ARG_OPTION"
754
        ARG_ENABLED_RESULT=true
755
        REASON="from command line"
756
      fi
757
    fi
758
  fi
759

760
  if test "x$ARG_ENABLED_RESULT" = xfalse; then
761
    if test "x$REASON" = "xnot available"; then
762
      AC_MSG_RESULT([<invalid>, $REASON])
763
      if test "x$ARG_RESULT" != "x" && test "x$ARG_RESULT" != "xno" ; then
764
        AC_MSG_WARN([Option [--with-]ARG_NAME is not available for this configuration])
765
      fi
766
    else
767
      AC_MSG_RESULT([<disabled>, $REASON])
768
    fi
769
    ARG_RESULT=""
770
  else
771
    if test [x]ARG_HAS_AUTO_BLOCK = xtrue && test "x$ARG_RESULT" = xauto; then
772
      # Execute "auto" payload
773
      ARG_IF_AUTO
774

775
      ARG_RESULT="$RESULT"
776
      REASON="$REASON (calculated from 'auto')"
777
    fi
778

779
    if test "x$ARG_RESULT" = x; then
780
      AC_MSG_RESULT([<none>, $REASON])
781
    else
782
      AC_MSG_RESULT([$ARG_RESULT, $REASON])
783
    fi
784

785
    # Verify value
786
    # First use our dispatcher to verify that type requirements are satisfied
787
    UTIL_CHECK_TYPE(ARG_TYPE, $ARG_RESULT)
788

789
    if test "x$FAILURE" = x; then
790
      # Execute custom verification payload, if present
791
      RESULT="$ARG_RESULT"
792

793
      ARG_CHECK_VALUE
794

795
      ARG_RESULT="$RESULT"
796
    fi
797

798
    if test "x$FAILURE" != x; then
799
      AC_MSG_NOTICE([Invalid value for [--with-]ARG_NAME: "$ARG_RESULT"])
800
      AC_MSG_NOTICE([$FAILURE])
801
      AC_MSG_ERROR([Cannot continue])
802
    fi
803
  fi
804

805
  # Execute result payloads, if present
806
  if test x$ARG_GIVEN = xtrue; then
807
    ARG_IF_GIVEN
808
  else
809
    ARG_IF_NOT_GIVEN
810
  fi
811
])
812

813
###############################################################################
814
# Helper functions for CHECK_VALUE in ARG_WITH.
815
AC_DEFUN([UTIL_CHECK_STRING_NON_EMPTY],
816
[
817
  if test "x$RESULT" = "x"; then
818
    FAILURE="Value cannot be empty"
819
  fi
820
])
821

822
AC_DEFUN([UTIL_CHECK_STRING_NON_EMPTY_PRINTABLE],
823
[
824
  if test "x$RESULT" = x; then
825
    FAILURE="Value cannot be empty"
826
  elif [ ! [[ $RESULT =~ ^[[:print:]]*$ ]] ]; then
827
    FAILURE="Value contains non-printing characters: $RESULT"
828
  fi
829
])
830

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.