musllvm

musl and LLVM cross-toolchain build system
git clone https://git.daat.foo/musllvm.git
Log | Files | Refs | README | LICENSE

config.sh (5263B)


      1 #!/bin/sh
      2 
      3 die()
      4 {
      5 	printf 'musllvm: %s\n' "$*" >&2
      6 	exit 1
      7 }
      8 
      9 need()
     10 {
     11 	command -v "$1" >/dev/null 2>&1 || die "required program not found: $1"
     12 }
     13 
     14 absolute_path()
     15 {
     16 	case "$1" in
     17 	/*) printf '%s\n' "$1" ;;
     18 	*) printf '%s/%s\n' "$PROOT" "$1" ;;
     19 	esac
     20 }
     21 
     22 validate_directory()
     23 {
     24 	case "$1" in
     25 	*[!a-zA-Z0-9_./-]*) die "build paths must use letters, digits, slashes, dots, underscores or hyphens: $1" ;;
     26 	esac
     27 	case "$1" in
     28 	/|/usr|/usr/local|/bin|/lib|/lib64|/etc|/home|/tmp|"$PROOT")
     29 		die "choose a dedicated directory, not $1" ;;
     30 	*/../*|*/..|*/./*|*/.|*/)
     31 		die "directory must have no dot components or trailing slash: $1" ;;
     32 	esac
     33 }
     34 
     35 source_info()
     36 {
     37 	source_line=$(awk -v component="$1" '$1 == component { print; count++ } END { if (count != 1) exit 1 }' "$SOURCES_FILE") ||
     38 		die "expected one $1 entry in $SOURCES_FILE"
     39 	read -r component archive digest url extra <<EOF
     40 $source_line
     41 EOF
     42 	case "$archive" in
     43 	*[!a-zA-Z0-9._-]*|'') die "invalid archive name: $archive" ;;
     44 	esac
     45 	case "$digest" in
     46 	*[!a-f0-9]*|'') die "invalid SHA-256 for $archive" ;;
     47 	esac
     48 	[ "${#digest}" -eq 64 ] && [ -z "$extra" ] || die "invalid source entry for $component"
     49 	case "$url" in
     50 	https://*) ;;
     51 	*) die "source URL must use HTTPS: $url" ;;
     52 	esac
     53 	source_name=${archive%.tar.*}
     54 }
     55 
     56 archive_ok()
     57 {
     58 	[ -f "$SOURCES_DIR/$archive" ] || return 1
     59 	actual_digest=$(sha256sum "$SOURCES_DIR/$archive") || return 1
     60 	[ "${actual_digest%% *}" = "$digest" ]
     61 }
     62 
     63 extract_source()
     64 {
     65 	source_info "$1"
     66 	archive_ok || die "missing or corrupt $archive; run make pull"
     67 	set -- "$PROOT/patches/$component/"*.patch
     68 	source_digest=$digest
     69 	if [ -f "$1" ]; then
     70 		need patch
     71 		source_digest=$({
     72 			printf '%s\n' "$digest"
     73 			for source_patch do
     74 				printf '%s\n' "${source_patch##*/}"
     75 				cat "$source_patch"
     76 			done
     77 		} | sha256sum)
     78 		source_digest=${source_digest%% *}
     79 	fi
     80 	source_path="$PROOT/build/src/$source_name"
     81 	if [ -f "$source_path/.musllvm-source" ]; then
     82 		[ "$(cat "$source_path/.musllvm-source")" = "$source_digest" ] ||
     83 			die "source or patches changed; remove $source_path and clean the affected toolchain before rebuilding"
     84 		return
     85 	fi
     86 	[ ! -e "$source_path" ] || die "incomplete source directory: $source_path"
     87 	mkdir -p "$PROOT/build/src"
     88 	source_lock="$source_path.lock"
     89 	(
     90 		mkdir "$source_lock" 2>/dev/null || die "source is being extracted: $source_name"
     91 		trap 'rm -rf "$source_lock"' 0
     92 		trap 'exit 1' HUP INT TERM
     93 		source_tmp="$source_lock/tree"
     94 		mkdir "$source_tmp"
     95 		printf 'Extracting %s\n' "$archive"
     96 		tar -xf "$SOURCES_DIR/$archive" --strip-components=1 -C "$source_tmp" || die "could not extract $archive"
     97 		for source_patch do
     98 			[ -f "$source_patch" ] || continue
     99 			printf 'Applying %s\n' "${source_patch##*/}"
    100 			patch --batch --forward --fuzz=0 -p1 -d "$source_tmp" < "$source_patch" ||
    101 				die "could not apply $source_patch"
    102 		done
    103 		printf '%s\n' "$source_digest" > "$source_tmp/.musllvm-source"
    104 		mv "$source_tmp" "$source_path"
    105 	)
    106 }
    107 
    108 PROOT=$(CDPATH= cd "$(dirname "$0")/.." && pwd -P)
    109 ARCH=${ARCH:-x86_64}
    110 TARGET=${TARGET:-$ARCH-linux-musl}
    111 case "$ARCH" in
    112 x86_64) KERNEL_ARCH=x86; ELF_MACHINE='Advanced Micro Devices X86-64' ;;
    113 aarch64) KERNEL_ARCH=arm64; ELF_MACHINE='AArch64' ;;
    114 *) die "unsupported ARCH=$ARCH; choose x86_64 or aarch64" ;;
    115 esac
    116 case "$TARGET" in
    117 "$ARCH-linux-musl"|"$ARCH-unknown-linux-musl") ;;
    118 *) die "TARGET=$TARGET does not match ARCH=$ARCH and the musl ABI" ;;
    119 esac
    120 LLVM_MODE=${LLVM_MODE:-system}
    121 case "$LLVM_MODE" in
    122 system|source|static) ;;
    123 *) die "LLVM_MODE must be system, source or static" ;;
    124 esac
    125 BOOTSTRAP_LLVM_MODE=${BOOTSTRAP_LLVM_MODE:-system}
    126 case "$BOOTSTRAP_LLVM_MODE" in
    127 system|source) ;;
    128 *) die "BOOTSTRAP_LLVM_MODE must be system or source" ;;
    129 esac
    130 HOST_ARCH=$(uname -m)
    131 if [ "$LLVM_MODE" = static ]; then
    132 	[ "$(uname -s)" = Linux ] || die "static LLVM requires a Linux build host"
    133 	case "$HOST_ARCH" in
    134 	x86_64|aarch64) ;;
    135 	*) die "unsupported static LLVM host: $HOST_ARCH" ;;
    136 	esac
    137 fi
    138 JOBS=${JOBS:-2}
    139 LINK_JOBS=${LINK_JOBS:-1}
    140 for jobs in "$JOBS" "$LINK_JOBS"; do
    141 	case "$jobs" in
    142 	*[!0-9]*|'') die "JOBS and LINK_JOBS must be positive integers" ;;
    143 	esac
    144 	[ "$jobs" -gt 0 ] 2>/dev/null || die "JOBS and LINK_JOBS must be positive integers"
    145 done
    146 PREFIX=$(absolute_path "${PREFIX:-build/$TARGET}")
    147 BUILD_DIR=$(absolute_path "${BUILD_DIR:-build/work/$TARGET}")
    148 SOURCES_DIR=$(absolute_path "${SOURCES_DIR:-sources}")
    149 SOURCES_FILE=$(absolute_path "${SOURCES_FILE:-sources.list}")
    150 validate_directory "$PREFIX"
    151 validate_directory "$BUILD_DIR"
    152 validate_directory "$SOURCES_DIR"
    153 case "$PROOT" in
    154 *[!a-zA-Z0-9_./-]*) die "move the checkout to a path without whitespace or shell metacharacters" ;;
    155 esac
    156 case "$PREFIX/" in
    157 "$BUILD_DIR/"*) die "PREFIX must be separate from BUILD_DIR" ;;
    158 esac
    159 case "$BUILD_DIR/" in
    160 "$PREFIX/"*) die "BUILD_DIR must be separate from PREFIX" ;;
    161 esac
    162 SYSROOT="$PREFIX/sysroot"
    163 HOST_CC=${HOST_CC:-clang}
    164 HOST_CXX=${HOST_CXX:-clang++}
    165 TARGET_CFLAGS=${TARGET_CFLAGS:--O2}
    166 HOST_CFLAGS=${HOST_CFLAGS:-}
    167 HOST_CXXFLAGS=${HOST_CXXFLAGS:-}
    168 HOST_LDFLAGS=${HOST_LDFLAGS:-}
    169 RUNNER=${RUNNER:-}
    170 export PROOT ARCH TARGET PREFIX BUILD_DIR SOURCES_DIR SOURCES_FILE LLVM_MODE JOBS LINK_JOBS
    171 export BOOTSTRAP_LLVM_MODE HOST_ARCH
    172 export HOST_CC HOST_CXX TARGET_CFLAGS HOST_CFLAGS HOST_CXXFLAGS HOST_LDFLAGS RUNNER