aboutsummaryrefslogtreecommitdiff
path: root/build_common.sh
diff options
context:
space:
mode:
Diffstat (limited to 'build_common.sh')
-rw-r--r--build_common.sh176
1 files changed, 151 insertions, 25 deletions
diff --git a/build_common.sh b/build_common.sh
index 50fcc388..21d62b83 100644
--- a/build_common.sh
+++ b/build_common.sh
@@ -12,7 +12,7 @@ mktempdir_diy() {
# /bin/sh implements $RANDOM in AIX 7, but not in Solaris before 11,
# thus use dd and od instead.
# shellcheck disable=SC2006
- mktempdir_diy_suffix=`dd if=/dev/urandom bs=1 count=4 2>/dev/null | od -t x -A n | head -1 | tr -d '\t '`
+ mktempdir_diy_suffix=`dd if=/dev/urandom bs=4 count=1 2>/dev/null | od -t x -A n | head -1 | tr -d '\t '`
[ -z "$mktempdir_diy_suffix" ] && return 1
mktempdir_diy_path="${TMPDIR:-/tmp}/${1:?}.${mktempdir_diy_suffix}"
# "test -e" would be more appropriate, but it is not available in
@@ -23,33 +23,26 @@ mktempdir_diy() {
echo "$mktempdir_diy_path"
break
fi
- # Try again (AIX /dev/urandom returns zeroes quite often).
+ # Try again (very unlikely, just in case).
done
}
mktempdir() {
mktempdir_prefix=${1:-tmp}
# shellcheck disable=SC2006
- case `uname -s` in
- Darwin|FreeBSD|NetBSD)
+ case `os_id` in
+ Darwin-*|FreeBSD-*|NetBSD-*)
# In these operating systems mktemp(1) always appends an implicit
# ".XXXXXXXX" suffix to the requested template when creating a
# temporary directory.
mktemp -d -t "$mktempdir_prefix"
;;
- AIX)
- mktempdir_diy "$mktempdir_prefix"
+ SunOS-5.10|SunOS-5.11)
+ # Although the suffix is optional, specify it for consistent results.
+ mktemp -d -t "${mktempdir_prefix}.XXXXXXXX"
;;
- SunOS)
- # shellcheck disable=SC2006
- case `uname -r` in
- 5.10|5.11)
- mktemp -d -t "${mktempdir_prefix}.XXXXXXXX"
- ;;
- *)
- mktempdir_diy "$mktempdir_prefix"
- ;;
- esac
+ SunOS-*|AIX-*)
+ mktempdir_diy "$mktempdir_prefix"
;;
*)
# At least Linux and OpenBSD implementations require explicit trailing
@@ -61,31 +54,164 @@ mktempdir() {
print_sysinfo() {
uname -a
+ printf 'OS identification: '
+ os_id
date
}
-print_cc_version() {
- # shellcheck disable=SC2006
+# Try to make the current C compiler print its version information (usually
+# multi-line) to stdout.
+# shellcheck disable=SC2006
+cc_version_nocache() {
+ : "${CC:?}"
case `basename "$CC"` in
- gcc*|clang*)
+ gcc*|egcc*|clang*)
# GCC and Clang recognize --version, print to stdout and exit with 0.
"$CC" --version
;;
xl*)
- # XL C for AIX recognizes -qversion, prints to stdout and exits with 0,
- # but on an unknown command-line flag displays its man page and waits.
- "$CC" -qversion
+ # XL C 12.1 and 13.1 recognize "-qversion", print to stdout and exit
+ # with 0. XL C 12.1 on an unknown command-line flag displays its man
+ # page and waits.
+ # XL C 16.1 recognizes "-qversion" and "--version", prints to stdout
+ # and exits with 0. Community Edition also prints a banner to stderr.
+ "$CC" -qversion 2>/dev/null
;;
sun*)
# Sun compilers recognize -V, print to stderr and exit with an error.
"$CC" -V 2>&1 || :
;;
+ cc)
+ case `os_id` in
+ SunOS-*)
+ # Most likely Sun C.
+ "$CC" -V 2>&1 || :
+ ;;
+ Darwin-*)
+ # Most likely Clang.
+ "$CC" --version
+ ;;
+ Linux-*|FreeBSD-*|NetBSD-*|OpenBSD-*)
+ # Most likely Clang or GCC.
+ "$CC" --version
+ ;;
+ esac
+ ;;
*)
"$CC" --version || "$CC" -V || :
;;
esac
}
+# shellcheck disable=SC2006
+cc_version() {
+ echo "${cc_version_cached:=`cc_version_nocache`}"
+}
+
+print_cc_version() {
+ cc_version
+ printf 'Compiler identification: '
+ cc_id
+}
+
+# For the current C compiler try to print a short and uniform identification
+# string (such as "gcc-9.3.0") that is convenient to use in a case statement.
+# shellcheck disable=SC2006
+cc_id_nocache() {
+ cc_id_firstline=`cc_version | head -1`
+ : "${cc_id_firstline:?}"
+
+ cc_id_guessed=`echo "$cc_id_firstline" | sed 's/^.*clang version \([0-9\.]*\).*$/clang-\1/'`
+ if [ "$cc_id_firstline" != "$cc_id_guessed" ]; then
+ echo "$cc_id_guessed"
+ return
+ fi
+
+ cc_id_guessed=`echo "$cc_id_firstline" | sed 's/^IBM XL C.*, V\([0-9\.]*\).*$/xlc-\1/'`
+ if [ "$cc_id_firstline" != "$cc_id_guessed" ]; then
+ echo "$cc_id_guessed"
+ return
+ fi
+
+ cc_id_guessed=`echo "$cc_id_firstline" | sed 's/^.* Sun C \([0-9\.]*\) .*$/suncc-\1/'`
+ if [ "$cc_id_firstline" != "$cc_id_guessed" ]; then
+ echo "$cc_id_guessed"
+ return
+ fi
+
+ cc_id_guessed=`echo "$cc_id_firstline" | sed 's/^.* (.*) \([0-9\.]*\)$/gcc-\1/'`
+ if [ "$cc_id_firstline" != "$cc_id_guessed" ]; then
+ echo "$cc_id_guessed"
+ return
+ fi
+}
+
+# shellcheck disable=SC2006
+cc_id() {
+ echo "${cc_id_cached:=`cc_id_nocache`}"
+}
+
+# Call this function each time CC has changed.
+discard_cc_cache() {
+ cc_version_cached=
+ cc_id_cached=
+}
+
+# For the current C compiler try to print CFLAGS value that tells to treat
+# warnings as errors.
+# shellcheck disable=SC2006
+cc_werr_cflags() {
+ case `cc_id` in
+ gcc-*|clang-*)
+ echo '-Werror'
+ ;;
+ xlc-*)
+ # XL C 12.1 and 13.1 recognize "-qhalt=w". XL C 16.1 recognizes that
+ # and "-Werror".
+ echo '-qhalt=w'
+ ;;
+ suncc-*)
+ echo '-errwarn=%all'
+ ;;
+ esac
+}
+
+# Tell whether "gcc" is a symlink to Clang (this is the case on macOS).
+# shellcheck disable=SC2006
+gcc_is_clang_in_disguise() {
+ case `cc_id`/`basename "${CC:?}"` in
+ clang-*/gcc)
+ return 0
+ ;;
+ esac
+ return 1
+}
+
+# shellcheck disable=SC2006
+os_id() {
+ # OS does not change between builds or in the middle of a build, so it is
+ # fine to cache uname output.
+ : "${os_id_sysname:=`uname -s`}"
+ printf '%s-' "$os_id_sysname"
+ : "${os_id_release:=`uname -r`}"
+ case "$os_id_sysname" in
+ AIX)
+ : "${os_id_version:=`uname -v`}"
+ echo "${os_id_version}.${os_id_release}"
+ ;;
+ Darwin|NetBSD|OpenBSD|SunOS)
+ echo "$os_id_release"
+ ;;
+ FreeBSD|Linux)
+ # Meaningful version is usually the substring before the first dash.
+ echo "$os_id_release" | sed 's/^\([0-9\.]*\).*$/\1/'
+ ;;
+ *)
+ echo 'UNKNOWN'
+ ;;
+ esac
+}
+
increment() {
# No arithmetic expansion in Solaris /bin/sh before 11.
echo "${1:?} + 1" | bc
@@ -106,8 +232,8 @@ run_after_echo() {
print_so_deps() {
# shellcheck disable=SC2006
- case `uname -s` in
- Darwin)
+ case `os_id` in
+ Darwin-*)
run_after_echo otool -L "${1:?}"
;;
*)
@@ -131,7 +257,7 @@ handle_matrix_debug() {
purge_directory() {
# shellcheck disable=SC2006
- if [ "`uname -s`" = SunOS ] && [ "`uname -r`" = 5.11 ]; then
+ if [ "`os_id`" = SunOS-5.11 ]; then
# In Solaris 11 /bin/sh the pathname expansion of "*" always includes
# "." and "..", so the straightforward rm would always fail.
(