Chih-Hung Hsieh | df9352e | 2020-09-10 15:24:06 -0700 | [diff] [blame] | 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 | |
Chih-Hung Hsieh | 8916a9f | 2020-11-01 17:33:28 -0800 | [diff] [blame] | 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 |
Chih-Hung Hsieh | df9352e | 2020-09-10 15:24:06 -0700 | [diff] [blame] | 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. |
Chih-Hung Hsieh | 8916a9f | 2020-11-01 17:33:28 -0800 | [diff] [blame] | 24 | |
| 25 | set -e |
| 26 | |
Joel Galenson | 810722d | 2021-05-06 16:13:02 -0700 | [diff] [blame] | 27 | # Wrapper around cargo2android. |
| 28 | C2A_WRAPPER="/google/bin/releases/android-rust/cargo2android/sandbox.par" |
Joel Galenson | ca72ecc | 2021-05-19 13:36:58 -0700 | [diff] [blame] | 29 | C2A_WRAPPER_FLAGS="--updater" |
Joel Galenson | 810722d | 2021-05-06 16:13:02 -0700 | [diff] [blame] | 30 | |
Chih-Hung Hsieh | 8916a9f | 2020-11-01 17:33:28 -0800 | [diff] [blame] | 31 | function 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 |
Joel Galenson | ca72ecc | 2021-05-19 13:36:58 -0700 | [diff] [blame] | 36 | echo "Updating Android.bp: $C2A_WRAPPER $C2A_WRAPPER_FLAGS -- $FLAGS" |
| 37 | $C2A_WRAPPER $C2A_WRAPPER_FLAGS -- $FLAGS |
Chih-Hung Hsieh | 8916a9f | 2020-11-01 17:33:28 -0800 | [diff] [blame] | 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 | |
| 44 | function abort() { |
| 45 | echo "$1" >&2 |
| 46 | exit 1 |
| 47 | } |
| 48 | |
| 49 | function check_files() { |
| 50 | if [ "$1" == "" ]; then |
| 51 | EXTERNAL_DIR=`pwd` |
Chih-Hung Hsieh | 8916a9f | 2020-11-01 17:33:28 -0800 | [diff] [blame] | 52 | else |
| 53 | EXTERNAL_DIR="$2" # e.g. rust/crates/bytes |
Chih-Hung Hsieh | df9352e | 2020-09-10 15:24:06 -0700 | [diff] [blame] | 54 | fi |
Joel Galenson | 810722d | 2021-05-06 16:13:02 -0700 | [diff] [blame] | 55 | [ -f "$C2A_WRAPPER" ] || abort "ERROR: cannot find $C2A_WRAPPER" |
Chih-Hung Hsieh | 8916a9f | 2020-11-01 17:33:28 -0800 | [diff] [blame] | 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 |
Chih-Hung Hsieh | df9352e | 2020-09-10 15:24:06 -0700 | [diff] [blame] | 60 | fi |
Chih-Hung Hsieh | 8916a9f | 2020-11-01 17:33:28 -0800 | [diff] [blame] | 61 | FLAGS=`echo "$LINE1" | sed -e 's:^.*cargo2android.py ::;s:\.$::'` |
| 62 | [ -f Cargo.toml ] || abort "ERROR: cannot find ./Cargo.toml." |
| 63 | } |
Chih-Hung Hsieh | df9352e | 2020-09-10 15:24:06 -0700 | [diff] [blame] | 64 | |
Chih-Hung Hsieh | 8916a9f | 2020-11-01 17:33:28 -0800 | [diff] [blame] | 65 | function 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 | } |
Chih-Hung Hsieh | df9352e | 2020-09-10 15:24:06 -0700 | [diff] [blame] | 82 | |
Chih-Hung Hsieh | 8916a9f | 2020-11-01 17:33:28 -0800 | [diff] [blame] | 83 | function 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 | } |
Chih-Hung Hsieh | 7d55330 | 2020-09-22 02:48:34 -0700 | [diff] [blame] | 96 | |
Chih-Hung Hsieh | 8916a9f | 2020-11-01 17:33:28 -0800 | [diff] [blame] | 97 | function 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 | } |
Chih-Hung Hsieh | df9352e | 2020-09-10 15:24:06 -0700 | [diff] [blame] | 103 | |
Chih-Hung Hsieh | 8916a9f | 2020-11-01 17:33:28 -0800 | [diff] [blame] | 104 | function 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 | } |
Chih-Hung Hsieh | df9352e | 2020-09-10 15:24:06 -0700 | [diff] [blame] | 118 | |
Chih-Hung Hsieh | 8916a9f | 2020-11-01 17:33:28 -0800 | [diff] [blame] | 119 | function 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 | } |
Chih-Hung Hsieh | df9352e | 2020-09-10 15:24:06 -0700 | [diff] [blame] | 129 | |
Chih-Hung Hsieh | 8916a9f | 2020-11-01 17:33:28 -0800 | [diff] [blame] | 130 | function 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 | |
| 141 | main $* |