Files
build_toochain/linux_shell/build_script/install_llvm.bash
T
2026-07-07 15:23:39 +08:00

112 lines
3.4 KiB
Bash

#!/bin/bash
if ! type "include" >/dev/null 2>&1; then
include() {
. "$(cd "$(dirname "$1")" && pwd)/${2}"
}
fi
include "${BASH_SOURCE[0]}" ./pragma_once.bash
if ! pragma_once ${BASH_SOURCE[0]}; then return 0; fi
install_llvm_mingw() {
local ver="$1"
local install_dir="/ae/opt/llvm-mingw"
local repo="https://github.com/mstorsjo/llvm-mingw.git"
local release_base="https://github.com/mstorsjo/llvm-mingw/releases/download"
local work_dir="${install_dir}/.install"
local download_dir="${work_dir}/download"
local extract_dir="${work_dir}/extract"
sudo mkdir -p "${download_dir}" "${extract_dir}"
if [[ -z "$ver" ]]; then
ver=$(git ls-remote --tags "${repo}" |
awk '{print $2}' |
sed 's#refs/tags/##' |
sed 's/\^{}//g' |
grep -E '^[0-9]{8}$' |
sort -V |
tail -n 1)
fi
if [[ -z "$ver" ]]; then
echo "[llvm-mingw] failed to resolve version" >&2
return 1
fi
echo "[llvm-mingw] selected version=${ver}"
local file=""
local url=""
local candidates=(
"llvm-mingw-${ver}-ucrt-ubuntu-22.04-x86_64.tar.xz"
"llvm-mingw-${ver}-ucrt-ubuntu-20.04-x86_64.tar.xz"
"llvm-mingw-${ver}-ucrt-ubuntu-18.04-x86_64.tar.xz"
"llvm-mingw-${ver}-ucrt-x86_64-linux.tar.xz"
"llvm-mingw-${ver}-msvcrt-ubuntu-22.04-x86_64.tar.xz"
"llvm-mingw-${ver}-msvcrt-ubuntu-20.04-x86_64.tar.xz"
"llvm-mingw-${ver}-msvcrt-ubuntu-18.04-x86_64.tar.xz"
"llvm-mingw-${ver}-msvcrt-x86_64-linux.tar.xz"
)
for file in "${candidates[@]}"; do
url="${release_base}/${ver}/${file}"
echo "[llvm-mingw] test ${url}"
if wget -q --spider "${url}"; then
break
fi
file=""
done
if [[ -z "$file" ]]; then
echo "[llvm-mingw] no downloadable linux x86_64 asset found for ${ver}" >&2
return 1
fi
url="${release_base}/${ver}/${file}"
echo "[llvm-mingw] downloading ${url}"
sudo rm -rf "${work_dir}"
sudo mkdir -p "${download_dir}" "${extract_dir}"
sudo wget -q --show-progress "${url}" -O "${download_dir}/${file}"
if [[ $? -ne 0 ]]; then
echo "[llvm-mingw] download failed ${url}" >&2
return 1
fi
sudo tar -xf "${download_dir}/${file}" -C "${extract_dir}"
local src_dir=""
src_dir=$(find "${extract_dir}" -mindepth 1 -maxdepth 1 -type d -name "llvm-mingw-*" | head -n 1)
if [[ -z "$src_dir" ]]; then
echo "[llvm-mingw] extract failed" >&2
return 1
fi
sudo find "${install_dir}" -mindepth 1 -maxdepth 1 ! -name ".install" -exec rm -rf {} +
sudo cp -a "${src_dir}/." "${install_dir}/"
sudo rm -rf "${work_dir}"
export PATH="${install_dir}/bin:$PATH"
echo "[llvm-mingw installed] version=${ver}"
echo "[llvm-mingw path] ${install_dir}"
}
check_llvm_mingw() {
local clang="/ae/opt/llvm-mingw/bin/x86_64-w64-mingw32-clang"
if [[ -x "$clang" ]]; then
"$clang" --version | head -n1
return 0
fi
if ! command -v x86_64-w64-mingw32-clang >/dev/null 2>&1; then
echo "[llvm-mingw] not installed"
return 1
fi
x86_64-w64-mingw32-clang --version | head -n1
}
get_llvm_mingw_version() {
local clang="/ae/opt/llvm-mingw/bin/x86_64-w64-mingw32-clang"
if [[ -x "$clang" ]]; then
"$clang" --version | head -n1
return
fi
if ! command -v x86_64-w64-mingw32-clang >/dev/null 2>&1; then
echo "none"
return
fi
x86_64-w64-mingw32-clang --version | head -n1
}
llvm_mingw_require() {
local required="$1"
local cur
cur=$(get_llvm_mingw_version)
if [[ "$cur" == "none" ]]; then
return 1
fi
echo "$cur" | grep -q "$required"
}