1#! /bin/bash -uv
2#
3# Build kzip files (source files for the indexing pipeline) for the given configuration,
4# merge them and place the resulting all.kzip into $DIST_DIR.
5# It is assumed that the current directory is the top of the source tree.
6# The following environment variables affect the result:
7#   BUILD_NUMBER          build number, used to generate unique ID (will use UUID if not set)
8#   SUPERPROJECT_SHA      superproject sha, used to generate unique id (will use BUILD_NUMBER if not set)
9#   DIST_DIR              where the resulting all.kzip will be placed
10#   KYTHE_KZIP_ENCODING   proto or json (proto is default)
11#   KYTHE_JAVA_SOURCE_BATCH_SIZE maximum number of the Java source files in a compilation unit
12#   OUT_DIR               output directory (out if not specified})
13#   TARGET_BUILD_VARIANT  variant, e.g., `userdebug`
14#   TARGET_PRODUCT        target device name, e.g., 'aosp_blueline'
15#   XREF_CORPUS           source code repository URI, e.g., 'android.googlesource.com/platform/superproject'
16
17: ${BUILD_NUMBER:=$(uuidgen)}
18: ${SUPERPROJECT_SHA:=$BUILD_NUMBER}
19: ${KYTHE_JAVA_SOURCE_BATCH_SIZE:=500}
20: ${KYTHE_KZIP_ENCODING:=proto}
21: ${XREF_CORPUS:?should be set}
22export KYTHE_JAVA_SOURCE_BATCH_SIZE KYTHE_KZIP_ENCODING
23
24# The extraction might fail for some source files, so run with -k and then check that
25# sufficiently many files were generated.
26declare -r out="${OUT_DIR:-out}"
27
28# Build extraction files for C++ and Java. Build `merge_zips` which we use later.
29build/soong/soong_ui.bash --build-mode --all-modules --dir=$PWD -k merge_zips xref_cxx xref_java
30
31# Build extraction file for Go the files in build/{blueprint,soong} directories.
32declare -r abspath_out=$(realpath "${out}")
33declare -r go_extractor=$(realpath prebuilts/build-tools/linux-x86/bin/go_extractor)
34declare -r go_root=$(realpath prebuilts/go/linux-x86)
35declare -r source_root=$PWD
36
37# TODO(asmundak): Until b/182183061 is fixed, default corpus has to be specified
38# in the rules file. Generate this file on the fly with corpus value set from the
39# environment variable.
40for dir in blueprint soong; do
41  (cd "build/$dir";
42   KYTHE_ROOT_DIRECTORY="${source_root}" "$go_extractor" --goroot="$go_root" \
43   --rules=<(printf '[{"pattern": "(.*)","vname": {"path": "@1@", "corpus":"%s"}}]' "${XREF_CORPUS}") \
44   --canonicalize_package_corpus --output "${abspath_out}/soong/build_${dir}.go.kzip" ./...
45  )
46done
47
48declare -r kzip_count=$(find "$out" -name '*.kzip' | wc -l)
49(($kzip_count>100000)) || { printf "Too few kzip files were generated: %d\n" $kzip_count; exit 1; }
50
51# Pack
52# TODO(asmundak): this should be done by soong.
53declare -r allkzip="$SUPERPROJECT_SHA.kzip"
54"$out/soong/host/linux-x86/bin/merge_zips" "$DIST_DIR/$allkzip" @<(find "$out" -name '*.kzip')
55
56