[source]
git = "https://gitlab.redox-os.org/redox-os/rust.git"
branch = "redox-2026-05-24"
shallow_clone = true

[build]
template = "custom"
dependencies = [
    "llvm21",
    "zlib",
    "curl",
    "openssl3",
]
dev-dependencies = [ 
    "llvm21.dev",
    "llvm21.runtime",
    "lld21",
]

script = """
if [ "${COOKBOOK_HOST_SYSROOT}" = "/usr" ]; then

OS=$(echo "${TARGET}" | cut -d - -f3)
cat ${COOKBOOK_RECIPE}/config-cross-$OS.toml > config.toml
sed -i "s|COOKBOOK_TOOLCHAIN|${COOKBOOK_TOOLCHAIN}|g" config.toml
sed -i "s|COOKBOOK_TARGET|${COOKBOOK_CROSS_TARGET}|g" config.toml
sed -i "s|COOKBOOK_GNU_TARGET|${COOKBOOK_CROSS_GNU_TARGET}|g" config.toml
sed -i "s|TARGET|${TARGET}|g" config.toml

else

DYNAMIC_INIT
# Linker flags for stage2 compiler (host -> target)
ARCH="${TARGET%%-*}"
export CARGO_TARGET_${ARCH^^}_UNKNOWN_REDOX_RUSTFLAGS="${RUSTFLAGS}" # Rust
# Hack for Rust errorneusly running `llvm-config --version` on cross compiled llvm-config
cat "${COOKBOOK_ROOT}/bin/${TARGET}-llvm-config" > "${COOKBOOK_SYSROOT}/bin/llvm-config"
# Hack for Rust stage1 being dynamically linked with LLVM in prefix toolchain
export LD_LIBRARY_PATH="${COOKBOOK_HOST_SYSROOT}/lib:${LD_LIBRARY_PATH}"
# Linker flags for stage1 compiler (host -> host)
export RUSTFLAGS_BOOTSTRAP="\
-Clink-args=-L${COOKBOOK_HOST_SYSROOT}/lib \
-Clink-args=-Wl,-rpath-link,${COOKBOOK_HOST_SYSROOT}/lib"
export CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUSTFLAGS="${RUSTFLAGS_BOOTSTRAP}"
export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_RUSTFLAGS="${RUSTFLAGS_BOOTSTRAP}"

cat ${COOKBOOK_RECIPE}/config.toml > config.toml
sed -i "s|COOKBOOK_SYSROOT|${COOKBOOK_SYSROOT}|g" config.toml
sed -i "s|COOKBOOK_TOOLCHAIN|${COOKBOOK_HOST_SYSROOT}|g" config.toml
sed -i "s|COOKBOOK_TARGET|${TARGET}|g" config.toml
sed -i "s|COOKBOOK_GNU_TARGET|${GNU_TARGET}|g" config.toml

fi

# Don't poison the stage1 compiler (host -> host)
unset AR AS CC CXX LD LDFLAGS NM OBJCOPY OBJDUMP RANLIB READELF RUSTFLAGS CARGO_ENCODED_RUSTFLAGS STRIP

python3 "${COOKBOOK_SOURCE}/x.py" install \
    --config config.toml \
    --jobs ${COOKBOOK_MAKE_JOBS}

mkdir -p "${COOKBOOK_STAGE}"/usr
rsync -av --delete "${COOKBOOK_BUILD}"/install/* "${COOKBOOK_STAGE}"/usr/
rm -rf "${COOKBOOK_STAGE}"/usr/lib/rustlib/*.log

# workaround for rust.lld, needed by e.g. compiling bootloader
# https://github.com/rust-lang/rust/issues/143076#issuecomment-3011710678
LLD_DIR="${COOKBOOK_STAGE}/usr/lib/rustlib/$TARGET/bin"
mkdir -p $LLD_DIR/gcc-ld
cp ${COOKBOOK_SYSROOT}/usr/bin/lld $LLD_DIR/rust-lld
ln -s rust-lld $LLD_DIR/wasm-component-ld
ln -s ../rust-lld $LLD_DIR/gcc-ld/ld.lld
ln -s ../rust-lld $LLD_DIR/gcc-ld/ld64.lld
ln -s ../rust-lld $LLD_DIR/gcc-ld/lld-link
ln -s ../rust-lld $LLD_DIR/gcc-ld/wasm-ld
ln -s ../../../libLLVM.so.21.1 $LLD_DIR/../lib/libLLVM.so.21.1
"""

[[optional-packages]]
name = "doc"
files = [
    "usr/share/doc/**",
]
