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# cargo2android.py 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. 28C2A_WRAPPER="/google/bin/releases/android-rust/cargo2android/sandbox.par" 29C2A_WRAPPER_FLAGS="--updater" 30 31function main() { 32 check_files $* 33 update_files_with_cargo_pkg_vars 34 # Save Cargo.lock if it existed before this update. 35 [ ! -f Cargo.lock ] || mv Cargo.lock Cargo.lock.saved 36 echo "Updating Android.bp: $C2A_WRAPPER $C2A_WRAPPER_FLAGS -- $FLAGS" 37 $C2A_WRAPPER $C2A_WRAPPER_FLAGS -- $FLAGS 38 copy_cargo_out_files $* 39 rm -rf target.tmp cargo.out Cargo.lock 40 # Restore Cargo.lock if it existed before this update. 41 [ ! -f Cargo.lock.saved ] || mv Cargo.lock.saved Cargo.lock 42} 43 44function abort() { 45 echo "$1" >&2 46 exit 1 47} 48 49function check_files() { 50 if [ "$1" == "" ]; then 51 EXTERNAL_DIR=`pwd` 52 else 53 EXTERNAL_DIR="$2" # e.g. rust/crates/bytes 54 fi 55 [ -f "$C2A_WRAPPER" ] || abort "ERROR: cannot find $C2A_WRAPPER" 56 LINE1=`head -1 Android.bp || abort "ERROR: cannot find Android.bp"` 57 if [[ ! "$LINE1" =~ ^.*cargo2android.py.*$ ]]; then 58 echo 'Android.bp header does not contain "cargo2android.py"; skip regen_bp' 59 exit 0 60 fi 61 FLAGS=`echo "$LINE1" | sed -e 's:^.*cargo2android.py ::;s:\.$::'` 62 [ -f Cargo.toml ] || abort "ERROR: cannot find ./Cargo.toml." 63} 64 65function copy_cargo_out_files() { 66 if [ -d $2/out ]; then 67 # copy files generated by cargo build to out directory 68 PKGNAME=`basename $2` 69 for f in $2/out/* 70 do 71 OUTF=`basename $f` 72 SRC=`ls ./target.tmp/*/debug/build/$PKGNAME-*/out/$OUTF || 73 ls ./target.tmp/debug/build/$PKGNAME-*/out/$OUTF || true` 74 if [ "$SRC" != "" ]; then 75 echo "Copying $SRC to out/$OUTF" 76 mkdir -p out 77 cp $SRC out/$OUTF 78 fi 79 done 80 fi 81} 82 83function update_files_with_cargo_pkg_vars() { 84 FILES=`grep -r -l --include \*.rs \ 85 --exclude-dir .git --exclude build.rs \ 86 --exclude-dir target.tmp --exclude-dir target \ 87 -E 'env!\("CARGO_PKG_(NAME|VERSION|AUTHORS|DESCRIPTION)"\)' * || true` 88 if [ "$FILES" != "" ]; then 89 printf "INFO: to update FILES: %s\n" "`echo ${FILES} | paste -s -d' '`" 90 # Find in ./Cargo.toml the 'name', 'version', 'authors', 'description' 91 # strings and use them to replace env!("CARGO_PKG_*") in $FILES. 92 grep_cargo_key_values 93 update_files 94 fi 95} 96 97function grep_one_key_value() 98{ 99 # Grep the first key $1 in Cargo.toml and return its value. 100 grep "^$1 = " Cargo.toml | head -1 | sed -e "s:^$1 = ::" \ 101 || abort "ERROR: Cannot find '$1' in ./Cargo.toml" 102} 103 104function grep_cargo_key_values() 105{ 106 NAME=`grep_one_key_value name` 107 VERSION=`grep_one_key_value version` 108 AUTHORS=`grep_one_key_value authors` 109 DESCRIPTION=`grep_one_key_value description` 110 if [ "$DESCRIPTION" == "\"\"\"" ]; then 111 # Old Cargo.toml description format, found only in the 'shlex' crate. 112 DESCRIPTION=`printf '"%s-%s"' "$NAME" "$VERSION"` 113 printf "WARNING: use %s for its CARGO_PKG_DESCRIPTION." "$DESCRIPTION" 114 fi 115 # CARGO_PKG_AUTHORS uses ':' as the separator. 116 AUTHORS="$AUTHORS.join(\":\")" 117} 118 119function build_sed_cmd() 120{ 121 # Replace '\' with '\\' to keep escape sequence in the sed command. 122 # NAME and VERSION are simple stings without escape sequence. 123 s1=`printf "$1" "NAME" "$NAME"` 124 s2=`printf "$1" "VERSION" "$VERSION"` 125 s3=`printf "$1" "AUTHORS" "${AUTHORS//\\\\/\\\\\\\\}"` 126 s4=`printf "$1" "DESCRIPTION" "${DESCRIPTION//\\\\/\\\\\\\\}"` 127 echo "$s1;$s2;$s3;$s4" 128} 129 130function update_files() 131{ 132 # Replace option_env!("...") with Some("...") 133 # Replace env!("...") with string literal "..." 134 # Do not replace run-time std::env::var("....") with 135 # (Ok("...".to_string()) as std::result::Result<...>) 136 local cmd=`build_sed_cmd 's%%option_env!("CARGO_PKG_%s")%%Some(%s)%%g'` 137 cmd="$cmd;"`build_sed_cmd 's%%env!("CARGO_PKG_%s")%%%s%%g'` 138 sed -i -e "$cmd" $FILES 139} 140 141main $* 142