1#!/bin/bash 2# 3# Copyright (C) 2020 The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17# This script is used by external_updater to replace a package. 18# It can also be invoked directly. It is used in two ways: 19# (1) in a .../external/* rust directory with .bp and Cargo.toml; 20# cargo_embargo must be in PATH 21# (2) in a tmp new directory with .bp and Cargo.toml, 22# and $1 equals to the rust Android source tree root, 23# and $2 equals to the rust sub-directory path name under external. 24 25set -e 26 27# Wrapper around cargo2android. 28SANDBOX="/google/bin/releases/android-rust/cargo2android/sandbox.par" 29SANDBOX_FLAGS="--updater" 30SANDBOX_RULESMK_FLAGS="--rulesmk" 31 32function main() { 33 check_files $* 34 update_files_with_cargo_pkg_vars 35 # Save Cargo.lock if it existed before this update. 36 [ ! -f Cargo.lock ] || mv Cargo.lock Cargo.lock.saved 37 if [[ "$CARGO_EMBARGO" = 'true' ]]; then 38 echo "Updating Android.bp: cargo_embargo generate cargo_embargo.json" 39 cargo_embargo generate cargo_embargo.json 40 fi 41 if [ -f rules.mk ]; then 42 echo "Updating rules.mk: $SANDBOX $SANDBOX_FLAGS $SANDBOX_RULESMK_FLAGS -- $C2R_SCRIPT_FLAGS" 43 $SANDBOX $SANDBOX_FLAGS $SANDBOX_RULESMK_FLAGS -- $C2R_SCRIPT_FLAGS 44 fi 45 copy_cargo_out_files $* 46 rm -rf target.tmp cargo.metadata cargo.out Cargo.lock 47 # Restore Cargo.lock if it existed before this update. 48 [ ! -f Cargo.lock.saved ] || mv Cargo.lock.saved Cargo.lock 49} 50 51function abort() { 52 echo "$1" >&2 53 exit 1 54} 55 56function check_files() { 57 if [ "$1" == "" ]; then 58 EXTERNAL_DIR=`pwd` 59 else 60 EXTERNAL_DIR="$2" # e.g. rust/crates/bytes 61 fi 62 [ -f "$SANDBOX" ] || abort "ERROR: cannot find $SANDBOX" 63 LINE1=`head -1 Android.bp || abort "ERROR: cannot find Android.bp"` 64 if [[ "$LINE1" =~ ^.*cargo_embargo.*$ ]]; then 65 CARGO_EMBARGO='true' 66 else 67 echo 'Android.bp header does not contain "cargo_embargo"; skip regen_bp' 68 exit 0 69 fi 70 [ -f Cargo.toml ] || abort "ERROR: cannot find ./Cargo.toml." 71 72 if [ -f rules.mk ]; then 73 LINE1=`head -1 rules.mk` 74 if [[ ! "$LINE1" =~ ^.*cargo2rulesmk.py.*$ ]]; then 75 echo 'rules.mk header does not contain "cargo2rulesmk.py"; skip regen_bp' 76 exit 0 77 fi 78 C2R_SCRIPT_FLAGS=`echo "$LINE1" | sed -e 's:^.*cargo2rulesmk.py ::;s:\.$::'` 79 fi 80} 81 82function copy_cargo_out_files() { 83 if [ -d $2/out ]; then 84 # copy files generated by cargo build to out directory 85 PKGNAME=`basename $2` 86 for f in $2/out/* 87 do 88 OUTF=`basename $f` 89 SRC=`ls ./target.tmp/*/debug/build/$PKGNAME-*/out/$OUTF || 90 ls ./target.tmp/debug/build/$PKGNAME-*/out/$OUTF || true` 91 if [ "$SRC" != "" ]; then 92 echo "Copying $SRC to out/$OUTF" 93 mkdir -p out 94 cp $SRC out/$OUTF 95 fi 96 done 97 fi 98} 99 100function update_files_with_cargo_pkg_vars() { 101 FILES=`grep -r -l --include \*.rs \ 102 --exclude-dir .git --exclude build.rs \ 103 --exclude-dir target.tmp --exclude-dir target \ 104 -E 'env!\("CARGO_PKG_(NAME|VERSION|AUTHORS|DESCRIPTION)"\)' * || true` 105 if [ "$FILES" != "" ]; then 106 printf "INFO: to update FILES: %s\n" "`echo ${FILES} | paste -s -d' '`" 107 # Find in ./Cargo.toml the 'name', 'version', 'authors', 'description' 108 # strings and use them to replace env!("CARGO_PKG_*") in $FILES. 109 grep_cargo_key_values 110 update_files 111 fi 112} 113 114function grep_one_key_value() 115{ 116 # Grep the first key $1 in Cargo.toml and return its value. 117 grep "^$1 = " Cargo.toml | head -1 | sed -e "s:^$1 = ::" \ 118 || abort "ERROR: Cannot find '$1' in ./Cargo.toml" 119} 120 121function grep_cargo_key_values() 122{ 123 NAME=`grep_one_key_value name` 124 VERSION=`grep_one_key_value version` 125 AUTHORS=`grep_one_key_value authors` 126 DESCRIPTION=`grep_one_key_value description` 127 if [ "$DESCRIPTION" == "\"\"\"" ]; then 128 # Old Cargo.toml description format, found only in the 'shlex' crate. 129 DESCRIPTION=`printf '"%s-%s"' "$NAME" "$VERSION"` 130 printf "WARNING: use %s for its CARGO_PKG_DESCRIPTION." "$DESCRIPTION" 131 fi 132 # CARGO_PKG_AUTHORS uses ':' as the separator. 133 AUTHORS="$AUTHORS.join(\":\")" 134} 135 136function build_sed_cmd() 137{ 138 # Replace '\' with '\\' to keep escape sequence in the sed command. 139 # NAME and VERSION are simple stings without escape sequence. 140 s1=`printf "$1" "NAME" "$NAME"` 141 s2=`printf "$1" "VERSION" "$VERSION"` 142 s3=`printf "$1" "AUTHORS" "${AUTHORS//\\\\/\\\\\\\\}"` 143 s4=`printf "$1" "DESCRIPTION" "${DESCRIPTION//\\\\/\\\\\\\\}"` 144 echo "$s1;$s2;$s3;$s4" 145} 146 147function update_files() 148{ 149 # Replace option_env!("...") with Some("...") 150 # Replace env!("...") with string literal "..." 151 # Do not replace run-time std::env::var("....") with 152 # (Ok("...".to_string()) as std::result::Result<...>) 153 local cmd=`build_sed_cmd 's%%option_env!("CARGO_PKG_%s")%%Some(%s)%%g'` 154 cmd="$cmd;"`build_sed_cmd 's%%env!("CARGO_PKG_%s")%%%s%%g'` 155 sed -i -e "$cmd" $FILES 156} 157 158main $* 159