aboutsummaryrefslogtreecommitdiff
path: root/update_android_bp.sh
blob: c367ecdc9e7c413e031ba6fce4c3c40ea4b5298c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/bin/bash

set -e

if [ "$#" != "1" ]; then
echo "Pass the path to the gRPC checkout as the argument."
exit 1
fi

traperr() {
    STATUS=$?
    echo "Line $BASH_LINENO: Command $BASH_COMMAND failed with status $STATUS" >&2
    exit $STATUS
}
trap traperr ERR

GRPC_PATH="$1"
pushd "$GRPC_PATH"

# Settings for Bazel queries
CONFIGS="--define=grpc_no_ares=true"
TEMP=/tmp/grpc_android_bp
mkdir -p $TEMP

# Query the list of source files with Bazel
# We must use the system version of Bazel, not the Android version
/usr/bin/bazel cquery $CONFIGS 'kind("source file", deps("//:grpc++_unsecure"))' > $TEMP/grpc_unsecure_deps.txt
/usr/bin/bazel cquery $CONFIGS 'kind("source file", deps("//:grpc++"))' > $TEMP/grpc_secure_deps.txt

# Remove irrelevant content from the list and clean it up
sed -i -e '\@\.h @d; \@\.proto @d; \@\.upb\.c@d; \@\.upbdefs\.c@d; \@\.inc @d; \@third_party@d; \@wrap_memcpy\.cc@d; \@ndk_binder\.cc@d; /^@/d; s@ \(.*\)@@; s@:@/@; s@/\+@/@g; s@^/@@; s@^.*$@"\0",@' $TEMP/grpc_unsecure_deps.txt $TEMP/grpc_secure_deps.txt
# Use diff to annotate the file lists with + and -
# This way we detect which files are only in the secure/unsecure target.
# Diff has exit status 1 when the files are different, hence the "|| true".
sort -o $TEMP/grpc_secure_deps.txt $TEMP/grpc_secure_deps.txt
sort -o $TEMP/grpc_unsecure_deps.txt $TEMP/grpc_unsecure_deps.txt
diff -U10000 $TEMP/grpc_unsecure_deps.txt $TEMP/grpc_secure_deps.txt > $TEMP/grpc_deps_diff.txt || true

# Pull out each category to its own file
sed -n '/^ "/s/ /    /p' $TEMP/grpc_deps_diff.txt > $TEMP/grpc_common_srcs.txt
sed -n '/^+"/s/+/    /p' $TEMP/grpc_deps_diff.txt > $TEMP/grpc_secure_srcs.txt
sed -n '/^-"/s/-/    /p' $TEMP/grpc_deps_diff.txt > $TEMP/grpc_unsecure_srcs.txt

# Construct the Android.bp fragment
OUT=$TEMP/grpc_android_bp.txt
echo '// Autogenerated by update_android_bp.sh, do not modify.' > $OUT
echo 'GRPC_COMMON_SRCS = [' >> $OUT
cat $TEMP/grpc_common_srcs.txt >> $OUT
echo ']' >> $OUT
echo >> $OUT
echo '// Autogenerated by update_android_bp.sh, do not modify.' >> $OUT
echo 'GRPC_SECURE_SRCS = [' >> $OUT
cat $TEMP/grpc_secure_srcs.txt >> $OUT
echo ']' >> $OUT
echo >> $OUT
echo '// Autogenerated by update_android_bp.sh, do not modify.' >> $OUT
echo 'GRPC_UNSECURE_SRCS = [' >> $OUT
cat $TEMP/grpc_unsecure_srcs.txt >> $OUT
echo ']' >> $OUT

popd

BPOUT=Android.bp
if [ ! -z "$ANDROID_BUILD_TOP" ]; then
BPOUT="$ANDROID_BUILD_TOP/external/grpc-grpc/Android.bp"
fi

# Paste the computed content into the Android.bp file
LIST_START='^// file_lists_start$'
LIST_END='^// file_lists_end$'
sed -i -e "\@$LIST_START@,\@$LIST_END@{ \@$LIST_START@{p; r $OUT
        }; \@$LIST_END@p; d }" $BPOUT

echo "Android.bp file lists updated."