Meta: Move compiler detection functions to their own file

This way we can use them in other scripts that want to build Lagom
and pick a suitable host compiler.
This commit is contained in:
Andrew Kaster 2023-09-07 08:44:33 -06:00 committed by Andrew Kaster
parent b258450fa6
commit bd7d01e975
Notes: sideshowbarker 2024-07-17 20:19:08 +09:00
2 changed files with 81 additions and 73 deletions

78
Meta/find_compiler.sh Normal file
View file

@ -0,0 +1,78 @@
# shellcheck shell=bash
HOST_COMPILER=""
is_supported_compiler() {
local COMPILER="$1"
if [ -z "$COMPILER" ]; then
return 1
fi
local VERSION=""
VERSION="$($COMPILER -dumpversion)" || return 1
local MAJOR_VERSION=""
MAJOR_VERSION="${VERSION%%.*}"
if $COMPILER --version 2>&1 | grep "Apple clang" >/dev/null; then
# Apple Clang version check
BUILD_VERSION=$(echo | $COMPILER -dM -E - | grep __apple_build_version__ | cut -d ' ' -f3)
# Xcode 14.3, based on upstream LLVM 15
[ "$BUILD_VERSION" -ge 14030022 ] && return 0
elif $COMPILER --version 2>&1 | grep "clang" >/dev/null; then
# Clang version check
[ "$MAJOR_VERSION" -ge 15 ] && return 0
else
# GCC version check
[ "$MAJOR_VERSION" -ge 12 ] && return 0
fi
return 1
}
find_newest_compiler() {
local BEST_VERSION=0
local BEST_CANDIDATE=""
for CANDIDATE in "$@"; do
if ! command -v "$CANDIDATE" >/dev/null 2>&1; then
continue
fi
if ! $CANDIDATE -dumpversion >/dev/null 2>&1; then
continue
fi
local VERSION=""
VERSION="$($CANDIDATE -dumpversion)"
local MAJOR_VERSION="${VERSION%%.*}"
if [ "$MAJOR_VERSION" -gt "$BEST_VERSION" ]; then
BEST_VERSION=$MAJOR_VERSION
BEST_CANDIDATE="$CANDIDATE"
fi
done
HOST_COMPILER=$BEST_CANDIDATE
}
pick_host_compiler() {
CC=${CC:-"cc"}
CXX=${CXX:-"cxx"}
if is_supported_compiler "$CC" && is_supported_compiler "$CXX"; then
return
fi
find_newest_compiler clang clang-15 clang-16 /opt/homebrew/opt/llvm/bin/clang
if is_supported_compiler "$HOST_COMPILER"; then
export CC="${HOST_COMPILER}"
export CXX="${HOST_COMPILER/clang/clang++}"
return
fi
find_newest_compiler egcc gcc gcc-12 gcc-13 /usr/local/bin/gcc-{12,13} /opt/homebrew/bin/gcc-{12,13}
if is_supported_compiler "$HOST_COMPILER"; then
export CC="${HOST_COMPILER}"
export CXX="${HOST_COMPILER/gcc/g++}"
return
fi
if [ "$(uname -s)" = "Darwin" ]; then
die "Please make sure that Xcode 14.3, Homebrew Clang 15, or higher is installed."
else
die "Please make sure that GCC version 12, Clang version 15, or higher is installed."
fi
}

View file

@ -86,6 +86,9 @@ DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
exit_if_running_as_root "Do not run serenity.sh as root, your Build directory will become root-owned"
# shellcheck source=/dev/null
. "${DIR}/find_compiler.sh"
if [ -n "$1" ]; then
TARGET="$1"; shift
else
@ -93,7 +96,6 @@ else
fi
CMAKE_ARGS=()
HOST_COMPILER=""
# Toolchain selection only applies to non-lagom targets.
if [ "$TARGET" != "lagom" ] && [ -n "$1" ]; then
@ -153,78 +155,6 @@ create_build_dir() {
fi
}
is_supported_compiler() {
local COMPILER="$1"
if [ -z "$COMPILER" ]; then
return 1
fi
local VERSION=""
VERSION="$($COMPILER -dumpversion)" || return 1
local MAJOR_VERSION=""
MAJOR_VERSION="${VERSION%%.*}"
if $COMPILER --version 2>&1 | grep "Apple clang" >/dev/null; then
# Apple Clang version check
BUILD_VERSION=$(echo | $COMPILER -dM -E - | grep __apple_build_version__ | cut -d ' ' -f3)
# Xcode 14.3, based on upstream LLVM 15
[ "$BUILD_VERSION" -ge 14030022 ] && return 0
elif $COMPILER --version 2>&1 | grep "clang" >/dev/null; then
# Clang version check
[ "$MAJOR_VERSION" -ge 15 ] && return 0
else
# GCC version check
[ "$MAJOR_VERSION" -ge 12 ] && return 0
fi
return 1
}
find_newest_compiler() {
local BEST_VERSION=0
local BEST_CANDIDATE=""
for CANDIDATE in "$@"; do
if ! command -v "$CANDIDATE" >/dev/null 2>&1; then
continue
fi
if ! $CANDIDATE -dumpversion >/dev/null 2>&1; then
continue
fi
local VERSION=""
VERSION="$($CANDIDATE -dumpversion)"
local MAJOR_VERSION="${VERSION%%.*}"
if [ "$MAJOR_VERSION" -gt "$BEST_VERSION" ]; then
BEST_VERSION=$MAJOR_VERSION
BEST_CANDIDATE="$CANDIDATE"
fi
done
HOST_COMPILER=$BEST_CANDIDATE
}
pick_host_compiler() {
if is_supported_compiler "$CC" && is_supported_compiler "$CXX"; then
return
fi
find_newest_compiler clang clang-15 clang-16 /opt/homebrew/opt/llvm/bin/clang
if is_supported_compiler "$HOST_COMPILER"; then
export CC="${HOST_COMPILER}"
export CXX="${HOST_COMPILER/clang/clang++}"
return
fi
find_newest_compiler egcc gcc gcc-12 gcc-13 /usr/local/bin/gcc-{12,13} /opt/homebrew/bin/gcc-{12,13}
if is_supported_compiler "$HOST_COMPILER"; then
export CC="${HOST_COMPILER}"
export CXX="${HOST_COMPILER/gcc/g++}"
return
fi
if [ "$(uname -s)" = "Darwin" ]; then
die "Please make sure that Xcode 14.3, Homebrew Clang 15, or higher is installed."
else
die "Please make sure that GCC version 12, Clang version 15, or higher is installed."
fi
}
cmd_with_target() {
is_valid_target || ( >&2 echo "Unknown target: $TARGET"; usage )