libpng/ci/lib/ci.lib.sh
Cosmin Truta c0616f1017 ci: Add ci.lib.ch; update ci_verify_*.sh accordingly
Move the common declarations and initializations from ci_verify_*.sh
to ci.lib.sh, and update them as follows:
 * Simplify the ci_ function names.
 * Refactor the CI_ variable names:
    - Add the new variable CI_TOPLEVEL_DIR.
    - Rename the variables CI_SCRIPTNAME, CI_SCRIPTDIR, etc.,
      to CI_SCRIPT_NAME, CI_SCRIPT_DIR, etc.
    - Rename the variables CI_SRCDIR_FROM_BUILDDIR, etc., to
      CI_BUILD_TO_SRC_RELDIR, etc.
 * Add new functions inside ci.lib.sh:
    - Replace ci_err with ci_err_usage, ci_err_fatal, ci_err_internal.
    - Add the new functions ci_warn and ci_assert.
 * Simplify the ci_ function names inside ci_verify_*.sh.
2023-07-03 22:59:24 +03:00

70 lines
2.1 KiB
Bash

# Copyright (c) 2019-2023 Cosmin Truta.
#
# Use, modification and distribution are subject
# to the Boost Software License, Version 1.0.
# See the accompanying file LICENSE_BSL_1_0.txt
# or visit http://www.boost.org/LICENSE_1_0.txt
#
# SPDX-License-Identifier: BSL-1.0
test -f "$BASH_SOURCE" ||
echo >&2 "warning: this module requires Bash version 3 or newer"
test "${#BASH_SOURCE[@]}" -gt 1 ||
echo >&2 "warning: this module should be sourced from a Bash script"
# Reset the locale to avoid surprises from locale-dependent commands.
export LC_ALL=C
export LANG=C
export LANGUAGE=C
# Reset CDPATH to avoid surprises from the "cd" command.
export CDPATH=""
# Initialize the global constants CI_SCRIPT_{NAME,DIR} and CI_TOPLEVEL_DIR.
CI_SCRIPT_NAME="$(basename -- "$0")"
CI_SCRIPT_DIR="$(cd "$(dirname -- "$0")" && pwd)"
CI_TOPLEVEL_DIR="$(cd "$(dirname -- "${BASH_SOURCE[0]}")/../.." && pwd)"
# Initialize the global constants CI_{HOST,TARGET}_{SYSTEM,MACHINE}.
CI_SYSTEM_NAME="${CI_SYSTEM_NAME:-"$(uname -s)"}"
CI_MACHINE_NAME="${CI_MACHINE_NAME:-"$(uname -m)"}"
function ci_info {
printf >&2 "%s: %s\\n" "$CI_SCRIPT_NAME" "$*"
}
function ci_warn {
printf >&2 "%s: warning: %s\\n" "$CI_SCRIPT_NAME" "$*"
}
function ci_err {
printf >&2 "%s: error: %s\\n" "$CI_SCRIPT_NAME" "$*"
exit 2
}
function ci_err_internal {
printf >&2 "%s: internal error: %s\\n" "$CI_SCRIPT_NAME" "$*"
printf >&2 "ABORTED\\n"
# Exit with the conventional SIGABRT code.
exit 134
}
function ci_assert {
# Use the "test" built-in command instead of the "[[ ]]" syntax,
# to ensure the a-priori expansion of all assertion arguments.
# (Consistently, both "ci_assert" and "test" have a command-like behavior.)
test "$@" || ci_err_internal "failed:" test "$@"
}
function ci_spawn {
printf >&2 "%s: executing:" "$CI_SCRIPT_NAME"
printf >&2 " %q" "$@"
printf >&2 "\\n"
"$@"
}
# Ensure that the initialization is correct.
ci_assert "$CI_TOPLEVEL_DIR/ci/lib/ci.lib.sh" -ef "${BASH_SOURCE[0]}"
ci_assert "$CI_SCRIPT_DIR/$CI_SCRIPT_NAME" -ef "$0"
ci_assert -n "$CI_SYSTEM_NAME" -a -n "$CI_MACHINE_NAME"