|
| 1 | +#!/bin/bash |
| 2 | +# 为 x86_64 → arm64 交叉编译准备工具链与 sysroot |
| 3 | +set -ex |
| 4 | + |
| 5 | +root_dir=$(cd "$(dirname "$0")/../.." && pwd -P) |
| 6 | + |
| 7 | +# ──────────────────────────────────────────── |
| 8 | +# 1. 检查系统交叉编译工具链 |
| 9 | +# ──────────────────────────────────────────── |
| 10 | +if ! command -v aarch64-linux-gnu-gcc &>/dev/null; then |
| 11 | + echo "错误:未找到 aarch64-linux-gnu-gcc。" >&2 |
| 12 | + echo "请先安装:sudo apt install gcc-aarch64-linux-gnu g++-aarch64-linux-gnu" >&2 |
| 13 | + exit 1 |
| 14 | +fi |
| 15 | + |
| 16 | +# ──────────────────────────────────────────── |
| 17 | +# 2. 下载 sysroot(Debian bookworm arm64 包) |
| 18 | +# ──────────────────────────────────────────── |
| 19 | +SYSROOT="$root_dir/cache/cross-tools-arm64/target" |
| 20 | +mkdir -p "$SYSROOT" |
| 21 | +mkdir -p "$root_dir/cache/pkg" |
| 22 | +cd "$root_dir/cache/pkg" |
| 23 | + |
| 24 | +BASE_DEBIAN="http://ftp.kr.debian.org/debian" |
| 25 | +SUITE="bookworm" |
| 26 | +COMPONENT="main" |
| 27 | +DEB_ARCH="arm64" |
| 28 | + |
| 29 | +function _index_path { |
| 30 | + local suite=$1 |
| 31 | + local arch=$2 |
| 32 | + echo "$root_dir/cache/pkg/Packages-debian-${suite}-${arch}.xz" |
| 33 | +} |
| 34 | + |
| 35 | +function _download_index { |
| 36 | + local suite=$1 |
| 37 | + local arch=$2 |
| 38 | + local index_path=$(_index_path "$suite" "$arch") |
| 39 | + local index_url="$BASE_DEBIAN/dists/$suite/$COMPONENT/binary-$arch/Packages.xz" |
| 40 | + if [ ! -f "$index_path" ]; then |
| 41 | + wget -c "$index_url" -O "$index_path" |
| 42 | + fi |
| 43 | +} |
| 44 | + |
| 45 | +function _find_filename_in_index { |
| 46 | + local pkg_name=$1 |
| 47 | + local index_path=$2 |
| 48 | + if [ ! -f "$index_path" ]; then |
| 49 | + return 1 |
| 50 | + fi |
| 51 | + xzcat "$index_path" | awk -v pkg="$pkg_name" ' |
| 52 | + $1=="Package:" {p=$2} |
| 53 | + $1=="Filename:" && p==pkg {print $2; exit} |
| 54 | + ' |
| 55 | +} |
| 56 | + |
| 57 | +function _resolve_pkg_url { |
| 58 | + local pkg_name=$1 |
| 59 | + local filename="" |
| 60 | + |
| 61 | + # 先查 arm64 |
| 62 | + _download_index "$SUITE" "$DEB_ARCH" |
| 63 | + filename=$(_find_filename_in_index "$pkg_name" "$(_index_path "$SUITE" "$DEB_ARCH")") |
| 64 | + if [ -n "$filename" ]; then |
| 65 | + echo "$BASE_DEBIAN/$filename" |
| 66 | + return 0 |
| 67 | + fi |
| 68 | + |
| 69 | + # 再查 arch-independent(all) |
| 70 | + _download_index "$SUITE" "all" |
| 71 | + filename=$(_find_filename_in_index "$pkg_name" "$(_index_path "$SUITE" "all")") |
| 72 | + if [ -n "$filename" ]; then |
| 73 | + echo "$BASE_DEBIAN/$filename" |
| 74 | + return 0 |
| 75 | + fi |
| 76 | + |
| 77 | + return 1 |
| 78 | +} |
| 79 | + |
| 80 | +function install_pkg { |
| 81 | + local pkg_name=$1 |
| 82 | + local pkg_url |
| 83 | + pkg_url=$(_resolve_pkg_url "$pkg_name") |
| 84 | + if [ -z "$pkg_url" ]; then |
| 85 | + echo "Failed to resolve package URL for $pkg_name" >&2 |
| 86 | + exit 1 |
| 87 | + fi |
| 88 | + local pkg_file |
| 89 | + pkg_file=$(basename "$pkg_url") |
| 90 | + if [ ! -f "$pkg_file" ]; then |
| 91 | + wget -c "$pkg_url" -O "$pkg_file" |
| 92 | + fi |
| 93 | + dpkg -x "$pkg_file" "$SYSROOT" |
| 94 | +} |
| 95 | + |
| 96 | +install_pkg libx11-dev |
| 97 | +install_pkg libxkbfile-dev |
| 98 | +install_pkg x11proto-dev |
| 99 | +install_pkg libx11-6 |
| 100 | +install_pkg libxkbfile1 |
| 101 | +install_pkg libkrb5-dev |
| 102 | +install_pkg krb5-multidev |
| 103 | +install_pkg openssl |
| 104 | +install_pkg libssl-dev |
| 105 | +install_pkg libssh2-1-dev |
| 106 | +install_pkg libssh2-1 |
| 107 | +install_pkg zlib1g |
| 108 | +install_pkg comerr-dev |
| 109 | +install_pkg libkrb5-3 |
| 110 | +install_pkg libk5crypto3 |
| 111 | +install_pkg libgssapi-krb5-2 |
| 112 | +install_pkg libcom-err2 |
| 113 | + |
| 114 | +# ──────────────────────────────────────────── |
| 115 | +# 3. 安装 node-gyp 头文件 |
| 116 | +# ──────────────────────────────────────────── |
| 117 | +node-gyp install |
| 118 | + |
| 119 | +# ──────────────────────────────────────────── |
| 120 | +# 4. 修复 krb5-config(输出正确的 arm64 链接参数) |
| 121 | +# ──────────────────────────────────────────── |
| 122 | +mkdir -p "$SYSROOT/usr/bin" |
| 123 | +echo 'echo "-L/usr/lib/aarch64-linux-gnu/mit-krb5 -Wl,-Bsymbolic-functions -Wl,-z,relro -lgssapi_krb5 -lkrb5 -lk5crypto -lcom_err"' \ |
| 124 | + > "$SYSROOT/usr/bin/krb5-config" |
| 125 | +chmod +x "$SYSROOT/usr/bin/krb5-config" |
0 commit comments