blob: c367ecdc9e7c413e031ba6fce4c3c40ea4b5298c [file] [log] [blame]
#!/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."