abinfo "Appending -fPIC ..."
export CFLAGS="${CFLAGS} -fPIC"
export CXXFLAGS="${CXXFLAGS} -fPIC"
export LDFLAGS="${LDFLAGS} -fPIC"
# Note: Append flags only for Clang-enabled architectures.
if bool $USECLANG; then
    abinfo "Appending target flags ..."
    export CFLAGS="${CFLAGS} --target=${HOST}"
    export CXXFLAGS="${CXXFLAGS} --target=${HOST}"
    export LDFLAGS="${LDFLAGS} --target=${HOST}"
fi

# disable system libgit2 and use vendored version
export LIBGIT2_NO_PKG_CONFIG=1

if ab_match_arch riscv64; then
    rust_target="riscv64gc-unknown-linux-gnu"
elif ab_match_arch loongson3; then
    rust_target="mips64el-unknown-linux-gnuabi64"
else
    rust_target="${HOST/-aosc/-unknown}"
fi

if ab_match_arch loongarch64; then
    # The default code model is too small for large applications e.g. Chromium.
    export RUSTFLAGS="${RUSTFLAGS} -C code-model=medium"
fi

if ab_match_arch i486; then
    # FIXME: autobuild passes them but they don't really help
    # so they'll be removed from autobuild later
    abinfo "Removing unhelpful RUSTFLAGS from autobuild default ..."
    export RUSTFLAGS="${RUSTFLAGS/-Ctarget-cpu=i486}"
    export RUSTFLAGS="${RUSTFLAGS/-Clink-args=-latomic}"
    abinfo "RUSTFLAGS now is ${RUSTFLAGS}"
fi

abinfo "Generating bootstrap.toml ..."
cat > "$SRCDIR"/bootstrap.toml <<EOF
[llvm]
link-shared = true
download-ci-llvm = false
ninja = true

[build]
python = "/usr/bin/python3"
extended = true
verbose = 2
sanitizers = true
profiler = true
vendor = true
print-step-timings = true
docs = false
tools = ["cargo","clippy","rust-analyzer","rust-analysis","rustdoc","rustfmt","src"]
EOF

if ! ab_match_arch i486; then
    abinfo "Using system rustc for bootstrapping ..."
    cat >> "$SRCDIR"/bootstrap.toml << EOF
cargo = "/usr/bin/cargo"
rustc = "/usr/bin/rustc"
rustfmt = "/usr/bin/rustfmt"
EOF
else
    # FIXME: Using local bootstrap tarball as we missed a release - rustc needs
    # to bootstrap from an adjacent release.
    SRC_PARENT="$(realpath $SRCDIR/..)"

    abinfo "Extracting bootstrapped Stage 1 Rust toolchain..."
    bsdtar xf "$SRC_PARENT"/rustc-bootstrap-${PKGVER}-${CROSS:-$ARCH}.tar.xz \
        -C "$SRC_PARENT"

    abinfo "Copying bootstrapped Stage 1 Rust toolchain ..."
    cp -rv "$SRC_PARENT"/rustc-bootstrap-${PKGVER}-${CROSS:-$ARCH}/opt/rustc-bootstrap-${PKGVER} \
        /opt/rustc-bootstrap-${PKGVER}-${CROSS:-$ARCH}

    if [ ! -e /opt/rustc-bootstrap-${PKGVER}-${CROSS:-$ARCH}/bin/rustc ] ; then
        abdie "Help! Stage 1 Rust toolchain does not exist in the desired directory."
    fi

    abinfo "Using local bootstrap binaries ..."
    cat >> "$SRCDIR"/bootstrap.toml << EOF
cargo = "/opt/rustc-bootstrap-${PKGVER}-${CROSS:-$ARCH}/bin/cargo"
rustc = "/opt/rustc-bootstrap-${PKGVER}-${CROSS:-$ARCH}/bin/rustc"
rustfmt = "/opt/rustc-bootstrap-${PKGVER}-${CROSS:-$ARCH}/bin/rustfmt"
EOF
fi # i486

if ab_match_arch armv7hf; then
    cat >> "$SRCDIR"/bootstrap.toml << EOF
target = [ "thumbv7neon-unknown-linux-gnueabihf", "armv7-unknown-linux-gnueabihf" ]
EOF
fi

cat >> "$SRCDIR"/bootstrap.toml << EOF
[install]
prefix = "/usr"

[rust]
download-rustc = false
# We cannot build lld as a part of rustc when building with system LLVM.
# See upstream pull request #143175.
lld = false
EOF

if ! ab_match_arch armv4 && \
   ! ab_match_arch armv6hf && \
   ! ab_match_arch armv7hf && \
   ! ab_match_arch i486; then
    cat >> "$SRCDIR"/bootstrap.toml << EOF
# LLVM crashes when passing an object through ThinLTO twice.
# This is triggered when using rust code in cross-language
# LTO if libstd was built using ThinLTO.
# http://blog.llvm.org/2019/09/closing-gap-cross-language-lto-between.html
# https://github.com/rust-lang/rust/issues/54872
codegen-units-std = 1
debuginfo-level = 2
EOF
else
    cat >> "$SRCDIR"/bootstrap.toml << EOF
# Working around limited VM space on arm32, i486
codegen-units = 1
codegen-units-std = 1
debuginfo-level = 0
EOF
fi

# FIXME: try removing this for 1.96 per
# https://github.com/rust-lang/cargo/pull/16760
if ab_match_arch i486; then
    cat >> "$SRCDIR"/bootstrap.toml << EOF
deny-warnings = false
EOF
fi

cat >> "$SRCDIR"/bootstrap.toml << EOF
channel = "stable"
rpath = false

[target.${rust_target}]
llvm-config = "/usr/lib/llvm-20/bin/llvm-config"
EOF

if ab_match_arch armv7hf; then
    cat >> "$SRCDIR"/bootstrap.toml << EOF
[target.thumbv7neon-unknown-linux-gnueabihf]
llvm-config = "/usr/lib/llvm-20/bin/llvm-config"

[target.armv7-unknown-linux-gnueabihf]
llvm-config = "/usr/lib/llvm-20/bin/llvm-config"
EOF
elif ab_match_arch i486; then
    # FIXME: an absolute path is needed in the [patch.crates-io] section
    # because it has to be correct for all projects in the workspace.
    # Thus sed is used instead of patch.
    sed -i 's@arch == "i386"@& || arch == "i486"@' vendor/blake3-1.8.3/build.rs

    cat >> Cargo.toml << EOF
[patch.crates-io]
blake3 = { path = '$PWD/vendor/blake3-1.8.3' }
EOF

    /opt/rustc-bootstrap-${PKGVER}-${CROSS:-$ARCH}/bin/cargo update --offline -p blake3

    cat >> src/tools/cargo/Cargo.toml << EOF
[patch.crates-io]
blake3 = { path = '$PWD/vendor/blake3-1.8.3' }
EOF

    /opt/rustc-bootstrap-${PKGVER}-${CROSS:-$ARCH}/bin/cargo update --offline -p blake3 --manifest-path src/tools/cargo/Cargo.toml
fi # i486
