blob: a09335ee0d5e2f4877c21c7b729009a10d8358c1 [file] [log] [blame]
Sasha Smundak65143642019-09-26 20:14:28 -07001#! /bin/bash -uv
Sasha Smundakcb86c3c2019-08-08 12:51:15 -07002#
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.
Sasha Smundak69ee7b02019-09-23 19:13:37 -07006# The following environment variables affect the result:
Sasha Smundak69ee7b02019-09-23 19:13:37 -07007# BUILD_NUMBER build number, used to generate unique ID (will use UUID if not set)
Brian Egizi29161df2021-05-05 23:04:34 +00008# SUPERPROJECT_SHA superproject sha, used to generate unique id (will use BUILD_NUMBER if not set)
Sasha Smundak65143642019-09-26 20:14:28 -07009# DIST_DIR where the resulting all.kzip will be placed
Sasha Smundak6c2d4f92020-01-09 17:34:23 -080010# KYTHE_KZIP_ENCODING proto or json (proto is default)
Sasha Smundakb0addaf2021-02-16 10:39:40 -080011# KYTHE_JAVA_SOURCE_BATCH_SIZE maximum number of the Java source files in a compilation unit
Sasha Smundak65143642019-09-26 20:14:28 -070012# 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'
Sasha Smundakcb86c3c2019-08-08 12:51:15 -070016
Sasha Smundak114d9662019-09-26 14:54:19 -070017: ${BUILD_NUMBER:=$(uuidgen)}
Brian Egizi29161df2021-05-05 23:04:34 +000018: ${SUPERPROJECT_SHA:=$BUILD_NUMBER}
Sasha Smundakb0addaf2021-02-16 10:39:40 -080019: ${KYTHE_JAVA_SOURCE_BATCH_SIZE:=500}
Sasha Smundak6c2d4f92020-01-09 17:34:23 -080020: ${KYTHE_KZIP_ENCODING:=proto}
Sasha Smundak70492662021-03-08 16:29:31 -080021: ${XREF_CORPUS:?should be set}
Sasha Smundakb0addaf2021-02-16 10:39:40 -080022export KYTHE_JAVA_SOURCE_BATCH_SIZE KYTHE_KZIP_ENCODING
Sasha Smundaka1e178f2019-09-24 11:57:58 -070023
Sasha Smundak65143642019-09-26 20:14:28 -070024# The extraction might fail for some source files, so run with -k and then check that
25# sufficiently many files were generated.
Sasha Smundak65143642019-09-26 20:14:28 -070026declare -r out="${OUT_DIR:-out}"
Sasha Smundakb8f46cd2020-02-25 11:15:25 -080027
Sasha Smundakca540882019-12-20 14:25:50 -080028# 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
Sasha Smundakb8f46cd2020-02-25 11:15:25 -080030
31# Build extraction file for Go the files in build/{blueprint,soong} directories.
Sasha Smundak17c42bd2019-12-27 15:29:05 -080032declare -r abspath_out=$(realpath "${out}")
Sasha Smundakfacb3602020-01-14 17:01:03 -080033declare -r go_extractor=$(realpath prebuilts/build-tools/linux-x86/bin/go_extractor)
34declare -r go_root=$(realpath prebuilts/go/linux-x86)
Sasha Smundakb8f46cd2020-02-25 11:15:25 -080035declare -r source_root=$PWD
Sasha Smundak70492662021-03-08 16:29:31 -080036
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.
Sasha Smundakfacb3602020-01-14 17:01:03 -080040for dir in blueprint soong; do
41 (cd "build/$dir";
Sasha Smundak70492662021-03-08 16:29:31 -080042 KYTHE_ROOT_DIRECTORY="${source_root}" "$go_extractor" --goroot="$go_root" \
43 --rules=<(printf '[{"pattern": "(.*)","vname": {"path": "@1@", "corpus":"%s"}}]' "${XREF_CORPUS}") \
Sasha Smundakb8f46cd2020-02-25 11:15:25 -080044 --canonicalize_package_corpus --output "${abspath_out}/soong/build_${dir}.go.kzip" ./...
Sasha Smundakfacb3602020-01-14 17:01:03 -080045 )
46done
Sasha Smundakca540882019-12-20 14:25:50 -080047
Sasha Smundak65143642019-09-26 20:14:28 -070048declare -r kzip_count=$(find "$out" -name '*.kzip' | wc -l)
Sasha Smundakcb86c3c2019-08-08 12:51:15 -070049(($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.
Brian Egizi29161df2021-05-05 23:04:34 +000053declare -r allkzip="$SUPERPROJECT_SHA.kzip"
Sasha Smundak65143642019-09-26 20:14:28 -070054"$out/soong/host/linux-x86/bin/merge_zips" "$DIST_DIR/$allkzip" @<(find "$out" -name '*.kzip')
Sasha Smundak114d9662019-09-26 14:54:19 -070055