blob: b5f7b1c8e50b2afa9d336a472385be49af24d188 [file] [log] [blame]
#!/bin/bash
# Copyright 2018 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#### CATEGORY=Source tree
### updates rustc_library and rustc_binary third_party dependencies
## usage: fx update-rustc-third-party
## Updates third_party/rust_crates based on the contents of
## third_party/rust_crates/Cargo.toml
##
## See https://fuchsia.dev/fuchsia-src/development/languages/rust/third_party.md
## for more details.
set -e
function preserve_owner_files {
local owners_tmp=$1
find . -name "OWNERS" -print0 | while read -d $'\0' owner_file
do
local owner_dir=$(dirname "$owner_file")
local version=$(sed -n "s/^version = \"\(.*\)\"$/\\1/p" "$owner_dir/Cargo.toml" | head -n 1)
if [[ $owner_dir != *-"$version" ]]; then
owner_dir="$owner_dir-$version"
fi
local temp_owner_dir="$owners_tmp/$owner_dir"
mkdir -p "$temp_owner_dir"
cp "$owner_file" "$temp_owner_dir"
done
}
# This function looks for any vendored crates that are missing OWNERS files and
# attempts to source one from the previous latest version of that crate.
function propagate_owner_files {
local owners_tmp=$1
local vendor_dir=$2
cd "$vendor_dir"
for crate in *
do
if [[ ! -f "$vendor_dir/$crate/OWNERS" ]]; then
cd "$owners_tmp"
local crate_name=$(echo "$crate" | sed -rn "s/^([a-zA-Z][a-zA-Z0-9_\\-]*)-[0-9]+\\.[0-9]+\\.[0-9]+.*$/\\1/p")
local semver_regex="s/^$crate_name-([0-9]+)\\.([0-9]+)\\.([0-9]+)(.*$)/\\1 \\2 \\3 \\4/p"
local latest_version=$(echo "$crate" "$crate_name"-* | tr ' ' '\n' | sed -rn "$semver_regex" | sort -rn -k 1,1 -k 2,2 -k 3,3 -k 4,4 | sed -rn "s/^(.+) (.+) (.+) (.*$)/$crate_name-\\1.\\2.\\3\\4/p" | grep -A1 -F "$crate" | tail -n1)
if [[ "$latest_version" != "$crate" ]]; then
cp "$owners_tmp/$latest_version/OWNERS" "$vendor_dir/$crate"
fi
fi
done
}
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/../lib/vars.sh || exit $?
fx-config-read
case "$(uname -s)" in
Linux*) ;;
Darwin*)
if ! [[ -x "$(command -v brew)" ]]; then
fx-error "'brew' binary not found"
fx-error "A homebrew <https://brew.sh> installation of opensslis required in order to update"
fx-error "Rust third party crates on the Mac."
exit 1
fi
declare -x LDFLAGS="-L$(brew --prefix)/opt/openssl/lib"
declare -x CPPFLAGS="-I$(brew --prefix)/opt/openssl/include";;
*) fx-error "unrecognized OS"; exit 1;;
esac
declare -x PATH=$PREBUILT_CMAKE_DIR/cmake/bin:$PATH
export RUSTC=$PREBUILT_RUST_DIR/bin/rustc
export CARGO=$PREBUILT_RUST_DIR/bin/cargo
GNAW_TARGET="host-tools/gnaw"
GNAW_BIN="${FUCHSIA_BUILD_DIR}/${GNAW_TARGET}"
if [[ "$1" == "--no-build" ]]; then
if [ ! -f "$GNAW_BIN" ]; then
fx-error "--no-build was specified, but $GNAW_BIN does not exist."
fx-error "Rerun without --no-build to build cargo-gnaw."
exit 1
fi
else
fx-command-run build ${GNAW_TARGET} || ( \
fx-error "Failed to build cargo-gnaw."; \
fx-error "This can happen after cargo-gnaw exits early."; \
fx-error "To retry an old build of cargo-gnaw, specify --no-build."; \
exit 1
)
fi
export VENDOR_DIR=$FUCHSIA_DIR/third_party/rust_crates/vendor
export OWNERS_TMP=$(mktemp -d)
# preserve OWNERS files
(cd $VENDOR_DIR; $(preserve_owner_files $OWNERS_TMP))
# this deletes OWNERS files
(cd $FUCHSIA_DIR; $PREBUILT_RUST_DIR/bin/cargo vendor \
--manifest-path $FUCHSIA_DIR/third_party/rust_crates/Cargo.toml \
--versioned-dirs \
$FUCHSIA_DIR/third_party/rust_crates/vendor \
-q)
# restore OWNERS files for unchanged directories
shopt -s nullglob
for f in $OWNERS_TMP/*
do
crate_dir=$(basename "$f")
# if the exact same version of the crate still exists
if [ -d "${VENDOR_DIR}/${crate_dir}" ]; then
# put the OWNERS file back
cp -r "$f" "${VENDOR_DIR}/"
fi
done
# copy the OWNERS from the previous latest version for the new latest version
# for newly-vendored versions
$(propagate_owner_files $OWNERS_TMP $VENDOR_DIR)
# ensure LICENSE* files exist
$FUCHSIA_DIR/scripts/rust/check_rust_licenses.py \
--directory $FUCHSIA_DIR/third_party/rust_crates/vendor
# regenerate BUILD.gn
(cd $FUCHSIA_DIR; $GNAW_BIN \
--manifest-path $FUCHSIA_DIR/third_party/rust_crates/Cargo.toml \
--project-root $FUCHSIA_DIR \
--cargo $CARGO \
--output $FUCHSIA_DIR/third_party/rust_crates/BUILD.gn \
--emit-metadata $FUCHSIA_BUILD_DIR/rustlang/3p-crates-metadata.json \
--gn-bin $PREBUILT_GN \
--skip-root)
# Add to gnaw invocation to enable third-party SDK metadata generation:
# --output-fuchsia-sdk-metadata $FUCHSIA_DIR/third_party/rust_crates/sdk_metas \