blob: ddeab8ad4717efffd6917f295bb86474ef558a86 [file] [log] [blame]
Chih-Hung Hsiehdf9352e2020-09-10 15:24:06 -07001#!/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 Hsieh8916a9f2020-11-01 17:33:28 -080017# 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 Hsiehdf9352e2020-09-10 15:24:06 -070021# (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 Hsieh8916a9f2020-11-01 17:33:28 -080024
25set -e
26
Joel Galenson810722d2021-05-06 16:13:02 -070027# Wrapper around cargo2android.
28C2A_WRAPPER="/google/bin/releases/android-rust/cargo2android/sandbox.par"
Joel Galensonca72ecc2021-05-19 13:36:58 -070029C2A_WRAPPER_FLAGS="--updater"
Joel Galenson810722d2021-05-06 16:13:02 -070030
Chih-Hung Hsieh8916a9f2020-11-01 17:33:28 -080031function 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 Galensonca72ecc2021-05-19 13:36:58 -070036 echo "Updating Android.bp: $C2A_WRAPPER $C2A_WRAPPER_FLAGS -- $FLAGS"
37 $C2A_WRAPPER $C2A_WRAPPER_FLAGS -- $FLAGS
Chih-Hung Hsieh8916a9f2020-11-01 17:33:28 -080038 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`
Chih-Hung Hsieh8916a9f2020-11-01 17:33:28 -080052 else
53 EXTERNAL_DIR="$2" # e.g. rust/crates/bytes
Chih-Hung Hsiehdf9352e2020-09-10 15:24:06 -070054 fi
Joel Galenson810722d2021-05-06 16:13:02 -070055 [ -f "$C2A_WRAPPER" ] || abort "ERROR: cannot find $C2A_WRAPPER"
Chih-Hung Hsieh8916a9f2020-11-01 17:33:28 -080056 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 Hsiehdf9352e2020-09-10 15:24:06 -070060 fi
Chih-Hung Hsieh8916a9f2020-11-01 17:33:28 -080061 FLAGS=`echo "$LINE1" | sed -e 's:^.*cargo2android.py ::;s:\.$::'`
62 [ -f Cargo.toml ] || abort "ERROR: cannot find ./Cargo.toml."
63}
Chih-Hung Hsiehdf9352e2020-09-10 15:24:06 -070064
Chih-Hung Hsieh8916a9f2020-11-01 17:33:28 -080065function 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 Hsiehdf9352e2020-09-10 15:24:06 -070082
Chih-Hung Hsieh8916a9f2020-11-01 17:33:28 -080083function 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 Hsieh7d553302020-09-22 02:48:34 -070096
Chih-Hung Hsieh8916a9f2020-11-01 17:33:28 -080097function 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 Hsiehdf9352e2020-09-10 15:24:06 -0700103
Chih-Hung Hsieh8916a9f2020-11-01 17:33:28 -0800104function 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 Hsiehdf9352e2020-09-10 15:24:06 -0700118
Chih-Hung Hsieh8916a9f2020-11-01 17:33:28 -0800119function 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 Hsiehdf9352e2020-09-10 15:24:06 -0700129
Chih-Hung Hsieh8916a9f2020-11-01 17:33:28 -0800130function 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 $*