#!/bin/bash
# Vendor the standard library dependencies. Currently this is needed
# in order to support use of the unstable "-Z build-std" rustc feature in offline
# environments. Once the feature is stabilized it should become no longer necessary 
# (pending resolution of https://github.com/rust-lang/wg-cargo-std-aware/issues/23
# and related issues).

set -e

std_vendored_deps="$1"
if [ -d "$std_vendored_deps" ]; then
    echo "directory $std_vendored_deps already exists" >&2
    exit 1
fi

# List of supported platforms; edit as needed
# This list should be a superset of the list in debian/vendor-deps --
# it should contain both the standard platforms in that list *plus*
# every non-host target we build std for (such as wasm targets).
supported_platforms=(
    "x86_64-unknown-linux-gnu"
    "aarch64-unknown-linux-gnu"
    "i686-unknown-linux-gnu"
    "armv7-unknown-linux-gnueabihf"
    "powerpc64le-unknown-linux-gnu"
    "powerpc-unknown-linux-gnu"
    "s390x-unknown-linux-gnu"
    "riscv64gc-unknown-linux-gnu"
    "riscv64a23-unknown-linux-gnu"
    "wasm32-unknown-unknown"
    "wasm32-wasip1"
    "wasm32-wasip2"
)

vendor_filterer_args=()
for platform in "${supported_platforms[@]}"; do
    vendor_filterer_args+=("--platform=$platform")
done

quilt push -a

echo "Vendoring standard library dependencies to ${std_vendored_deps}..."

RUSTC_BOOTSTRAP=1 \
    PATH="${RUST_BOOTSTRAP_DIR}/bin:$PATH" \
    CARGO="${RUST_BOOTSTRAP_DIR}/bin/cargo" \
    RUSTC="${RUST_BOOTSTRAP_DIR}/bin/rustc" \
  ${RUST_BOOTSTRAP_DIR}/bin/cargo vendor-filterer \
  --manifest-path library/Cargo.toml \
  --versioned-dirs \
  "${vendor_filterer_args[@]}" \
  ${std_vendored_deps}

quilt pop -a
