#!/bin/bash set -e # 基础定义 BENCHVER=20240318 # 基准测试版本号 LLVMVER=18.1.1 # LLVM版本号 LLVMURL="https://git.whlug.cn/api/packages/github-mirror/generic/llvm/$LLVMVER/llvm-project-$LLVMVER.src.tar.xz" LLVMDIR="$(basename $LLVMURL | rev | cut -f3- -d'.' | rev)" DEPENDENCIES="devel-base" # 所需依赖包 BUILDLOGFILE="$(pwd)/benchmark.log" # 构建日志文件 # LLVM配置选项 CONFIG_OPTS=( '-DCMAKE_BUILD_TYPE:STRING=Release' '-DBENCHMARK_BUILD_32_BITS:BOOL=OFF' '-DBENCHMARK_DOWNLOAD_DEPENDENCIES:BOOL=OFF' '-DBENCHMARK_ENABLE_ASSEMBLY_TESTS:BOOL=OFF' '-DBENCHMARK_ENABLE_DOXYGEN:BOOL=OFF' '-DBENCHMARK_ENABLE_EXCEPTIONS:BOOL=OFF' '-DBENCHMARK_ENABLE_GTEST_TESTS:BOOL=OFF' '-DBENCHMARK_ENABLE_INSTALL:BOOL=OFF' '-DBENCHMARK_ENABLE_LIBPFM:BOOL=OFF' '-DBENCHMARK_ENABLE_LTO:BOOL=OFF' '-DBENCHMARK_ENABLE_TESTING:BOOL=OFF' '-DBENCHMARK_ENABLE_WERROR:BOOL=OFF' '-DBENCHMARK_FORCE_WERROR:BOOL=OFF' '-DBENCHMARK_INSTALL_DOCS:BOOL=OFF' '-DBENCHMARK_USE_BUNDLED_GTEST:BOOL=OFF' '-DBENCHMARK_USE_LIBCXX:BOOL=OFF' '-DBUILD_SHARED_LIBS:BOOL=OFF' '-DHAVE_STD_REGEX:BOOL=ON' '-DLLVM_ADDITIONAL_BUILD_TYPES:BOOL=OFF' '-DLLVM_ALLOW_PROBLEMATIC_CONFIGURATIONS:BOOL=OFF' '-DLLVM_APPEND_VC_REV:BOOL=OFF' '-DLLVM_BUILD_32_BITS:BOOL=OFF' '-DLLVM_BUILD_BENCHMARKS:BOOL=OFF' '-DLLVM_BUILD_DOCS:BOOL=OFF' '-DLLVM_BUILD_EXAMPLES:BOOL=OFF' '-DLLVM_BUILD_EXTERNAL_COMPILER_RT:BOOL=OFF' '-DLLVM_BUILD_LLVM_C_DYLIB:BOOL=OFF' '-DLLVM_BUILD_LLVM_DYLIB:BOOL=OFF' '-DLLVM_BUILD_RUNTIME:BOOL=ON' '-DLLVM_BUILD_RUNTIMES:BOOL=ON' '-DLLVM_BUILD_TESTS:BOOL=OFF' '-DLLVM_BUILD_TOOLS:BOOL=OFF' '-DLLVM_BUILD_UTILS:BOOL=OFF' '-DLLVM_BYE_LINK_INTO_TOOLS:BOOL=OFF' '-DLLVM_CCACHE_BUILD:BOOL=OFF' '-DLLVM_DEPENDENCY_DEBUGGING:BOOL=OFF' '-DLLVM_ENABLE_ASSERTIONS:BOOL=OFF' '-DLLVM_ENABLE_BACKTRACES:BOOL=OFF' '-DLLVM_ENABLE_BINDINGS:BOOL=OFF' '-DLLVM_ENABLE_CRASH_DUMPS:BOOL=OFF' '-DLLVM_ENABLE_CRASH_OVERRIDES:BOOL=OFF' '-DLLVM_ENABLE_DAGISEL_COV:BOOL=OFF' '-DLLVM_ENABLE_DOXYGEN:BOOL=OFF' '-DLLVM_ENABLE_DUMP:BOOL=OFF' '-DLLVM_ENABLE_EH:BOOL=OFF' '-DLLVM_ENABLE_EXPENSIVE_CHECKS:BOOL=OFF' '-DLLVM_ENABLE_FFI:BOOL=OFF' '-DLLVM_ENABLE_GISEL_COV:BOOL=OFF' '-DLLVM_ENABLE_IDE:BOOL=OFF' '-DLLVM_ENABLE_LIBCXX:BOOL=OFF' '-DLLVM_ENABLE_LIBEDIT:BOOL=OFF' '-DLLVM_ENABLE_LIBPFM:BOOL=OFF' '-DLLVM_ENABLE_LLD:BOOL=OFF' '-DLLVM_ENABLE_LLVM_LIBC:BOOL=OFF' '-DLLVM_ENABLE_LOCAL_SUBMODULE_VISIBILITY:BOOL=ON' '-DLLVM_ENABLE_MODULES:BOOL=OFF' '-DLLVM_ENABLE_MODULE_DEBUGGING:BOOL=OFF' '-DLLVM_ENABLE_NEW_PASS_MANAGER:BOOL=TRUE' '-DLLVM_ENABLE_OCAMLDOC:BOOL=OFF' '-DLLVM_ENABLE_PEDANTIC:BOOL=OFF' '-DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR:BOOL=ON' '-DLLVM_ENABLE_PIC:BOOL=ON' '-DLLVM_ENABLE_PLUGINS:BOOL=OFF' '-DLLVM_ENABLE_RTTI:BOOL=OFF' '-DLLVM_ENABLE_SPHINX:BOOL=OFF' '-DLLVM_ENABLE_STRICT_FIXED_SIZE_VECTORS:BOOL=OFF' '-DLLVM_ENABLE_TERMINFO:BOOL=OFF' '-DLLVM_ENABLE_THREADS:BOOL=ON' '-DLLVM_ENABLE_UNWIND_TABLES:BOOL=OFF' '-DLLVM_ENABLE_WARNINGS:BOOL=ON' '-DLLVM_ENABLE_WERROR:BOOL=OFF' '-DLLVM_ENABLE_Z3_SOLVER:BOOL=OFF' '-DLLVM_EXPERIMENTAL_DEBUGINFO_ITERATORS:BOOL=OFF' '-DLLVM_EXPORT_SYMBOLS_FOR_PLUGINS:BOOL=OFF' '-DLLVM_EXTERNALIZE_DEBUGINFO:BOOL=OFF' '-DLLVM_FORCE_ENABLE_STATS:BOOL=OFF' '-DLLVM_FORCE_USE_OLD_TOOLCHAIN:BOOL=OFF' '-DLLVM_HAVE_TFLITE:BOOL=' '-DLLVM_INCLUDE_BENCHMARKS:BOOL=OFF' '-DLLVM_INCLUDE_DOCS:BOOL=OFF' '-DLLVM_INCLUDE_EXAMPLES:BOOL=OFF' '-DLLVM_INCLUDE_GO_TESTS:BOOL=OFF' '-DLLVM_INCLUDE_RUNTIMES:BOOL=ON' '-DLLVM_INCLUDE_TESTS:BOOL=OFF' '-DLLVM_INCLUDE_TOOLS:BOOL=OFF' '-DLLVM_INCLUDE_UTILS:BOOL=OFF' '-DLLVM_INDIVIDUAL_TEST_COVERAGE:BOOL=OFF' '-DLLVM_INSTALL_BINUTILS_SYMLINKS:BOOL=OFF' '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' '-DLLVM_USE_INTEL_JITEVENTS:BOOL=OFF' '-DLLVM_USE_OPROFILE:BOOL=OFF' '-DLLVM_USE_PERF:BOOL=OFF' '-DLLVM_USE_RELATIVE_PATHS_IN_DEBUG_INFO:BOOL=OFF' '-DLLVM_USE_RELATIVE_PATHS_IN_FILES:BOOL=OFF' '-DLLVM_USE_SPLIT_DWARF:BOOL=OFF' '-DLLVM_USE_STATIC_ZSTD:BOOL=FALSE' '-DLLVM_USE_SYMLINKS:BOOL=ON' '-DLLVM_VERSION_PRINTER_SHOW_HOST_TARGET_INFO:BOOL=ON' '-DLLVM_WINDOWS_PREFER_FORWARD_SLASH:BOOL=OFF' '-DPY_PYGMENTS_FOUND:BOOL=OFF' '-DPY_PYGMENTS_LEXERS_C_CPP_FOUND:BOOL=OFF' '-DPY_YAML_FOUND:BOOL=OFF' '-DLLVM_ENABLE_LTO:BOOL=ON' ) # 自动构建输出格式化函数 abwarn() { echo -e "[\e[33m警告\e[0m]: \e[1m$*\e[0m"; } # 黄色警告信息 aberr() { echo -e "[\e[31m错误\e[0m]: \e[1m$*\e[0m"; exit 1; } # 红色错误信息 abinfo() { echo -e "[\e[96m信息\e[0m]: \e[1m$*\e[0m"; } # 青色信息提示 # 软件包管理函数 pm_exists(){ for p in "$@"; do dpkg $PM_ROOTPARAM -l "$p" | grep ^ii >/dev/null 2>&1 || return 1 done } pm_repoupdate(){ apt-get update } pm_repoinstall(){ apt-get install "$@" --yes } # 系统检测逻辑 sys_detect() { . /etc/os-release export $ID } echo -e " ****************************************************************************** ---------------- 构建机器性能测试套件 (版本 $BENCHVER) ----------------- ****************************************************************************** 测试项目: 构建LLVM运行时 (版本 $LLVMVER), 使用Ninja构建工具, 启用LTO优化 系统架构: $(uname -m) 处理器核心数: $(nproc) 系统内存: $(( $(cat /proc/meminfo | grep MemTotal | awk '{ print $2 }') / 1024 / 1024 )) GiB " abinfo "(1/6) 准备基准测试: 获取依赖项..." sys_detect if [[ "$ID" = "aosc" ]]; then if ! pm_exists $DEPENDENCIES; then abinfo "构建或运行时依赖未满足,正在获取所需软件包" pm_repoupdate || \ aberr "仓库更新失败: $?" pm_repoinstall $DEPENDENCIES || \ aberr "依赖安装失败: $?" fi else abwarn "检测到非AOSC操作系统, 请自行处理依赖项! 通常您需要安装基础开发工具元包, 例如Debian/Ubuntu的ninja和build-essential, 或Arch Linux的base-devel。\n" fi abinfo "(2/6) 准备基准测试: 下载LLVM (版本 $LLVMVER)..." wget -c $LLVMURL 2> $BUILDLOGFILE || \ aberr "LLVM下载失败: $?." abinfo "(3/6) 准备基准测试: 解压LLVM (版本 $LLVMVER)..." rm -rf "$LLVMDIR" tar xf $(basename $LLVMURL) || \ aberr "LLVM解压失败: $?." abinfo "(4/6) 准备基准测试: 设置构建环境..." mkdir -p "$LLVMDIR"/llvm/build || \ aberr "创建构建目录失败: $?." cd "$LLVMDIR"/llvm/build || \ aberr "切换至构建目录失败: $?." # 设置LANG为C以提升gcc编译速度(轻微提升) export LC_ALL=C export LANG=C abinfo "(5/6) 准备基准测试: 配置LLVM (版本 $LLVMVER)..." cmake .. \ "${CONFIG_OPTS[@]}" \ -GNinja >> $BUILDLOGFILE 2>&1 || \ aberr "LLVM配置失败: $?." abinfo "(6/6) 执行基准测试: 构建LLVM..." time ninja 2>> $BUILDLOGFILE || \ aberr "LLVM构建失败: $?." unset LC_ALL LANG