blob: 4c42048dd17b60f26e3242ada05a050a32886ded [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)
Brian Egizi4aa056e2021-05-17 23:31:47 +00009# SUPERPROJECT_REVISION superproject revision, used for unique id if defined as a sha
10# KZIP_NAME name of the output file (will use SUPERPROJECT_REVISION|SUPERPROJECT_SHA|BUILD_NUMBER|UUID if not set)
Sasha Smundak65143642019-09-26 20:14:28 -070011# DIST_DIR where the resulting all.kzip will be placed
Sasha Smundak6c2d4f92020-01-09 17:34:23 -080012# KYTHE_KZIP_ENCODING proto or json (proto is default)
Sasha Smundakb0addaf2021-02-16 10:39:40 -080013# KYTHE_JAVA_SOURCE_BATCH_SIZE maximum number of the Java source files in a compilation unit
Sasha Smundak65143642019-09-26 20:14:28 -070014# OUT_DIR output directory (out if not specified})
15# TARGET_BUILD_VARIANT variant, e.g., `userdebug`
16# TARGET_PRODUCT target device name, e.g., 'aosp_blueline'
17# XREF_CORPUS source code repository URI, e.g., 'android.googlesource.com/platform/superproject'
Sasha Smundakcb86c3c2019-08-08 12:51:15 -070018
Brian Egizi4aa056e2021-05-17 23:31:47 +000019# If the SUPERPROJECT_REVISION is defined as a sha, use this as the default value if no
20# SUPERPROJECT_SHA is specified.
Brian Egizi2aded7d2021-05-21 00:50:21 +000021if [[ ${SUPERPROJECT_REVISION:-} =~ [0-9a-f]{40} ]]; then
Brian Egizi4aa056e2021-05-17 23:31:47 +000022 : ${KZIP_NAME:=${SUPERPROJECT_REVISION:-}}
23fi
24
25: ${KZIP_NAME:=${SUPERPROJECT_SHA:-}}
26: ${KZIP_NAME:=${BUILD_NUMBER:-}}
27: ${KZIP_NAME:=$(uuidgen)}
28
Sasha Smundakb0addaf2021-02-16 10:39:40 -080029: ${KYTHE_JAVA_SOURCE_BATCH_SIZE:=500}
Sasha Smundak6c2d4f92020-01-09 17:34:23 -080030: ${KYTHE_KZIP_ENCODING:=proto}
Sasha Smundak70492662021-03-08 16:29:31 -080031: ${XREF_CORPUS:?should be set}
Sasha Smundakb0addaf2021-02-16 10:39:40 -080032export KYTHE_JAVA_SOURCE_BATCH_SIZE KYTHE_KZIP_ENCODING
Sasha Smundaka1e178f2019-09-24 11:57:58 -070033
Sasha Smundak65143642019-09-26 20:14:28 -070034# The extraction might fail for some source files, so run with -k and then check that
35# sufficiently many files were generated.
Sasha Smundak65143642019-09-26 20:14:28 -070036declare -r out="${OUT_DIR:-out}"
Sasha Smundakb8f46cd2020-02-25 11:15:25 -080037
Liz Kammer8cac3b32023-06-29 09:55:56 -040038# Build extraction files and `merge_zips` which we use later.
39kzip_targets=(
40 merge_zips
41 xref_cxx
42 xref_java
43 # TODO: b/286390153 - reenable rust
44 # xref_rust
45)
46
Liz Kammer8822ff52023-07-07 18:31:15 -040047build/soong/soong_ui.bash --build-mode --all-modules --dir=$PWD -k --skip-soong-tests --ninja_weight_source=not_used "${kzip_targets[@]}"
Sasha Smundakb8f46cd2020-02-25 11:15:25 -080048
49# Build extraction file for Go the files in build/{blueprint,soong} directories.
Sasha Smundak17c42bd2019-12-27 15:29:05 -080050declare -r abspath_out=$(realpath "${out}")
Sasha Smundakfacb3602020-01-14 17:01:03 -080051declare -r go_extractor=$(realpath prebuilts/build-tools/linux-x86/bin/go_extractor)
52declare -r go_root=$(realpath prebuilts/go/linux-x86)
Sasha Smundakb8f46cd2020-02-25 11:15:25 -080053declare -r source_root=$PWD
Sasha Smundak70492662021-03-08 16:29:31 -080054
Sasha Smundak08253a02022-08-01 16:11:25 -070055# For the Go code, we invoke the extractor directly. The two caveats are that
Sasha Smundak8df2a5c2022-08-08 17:31:06 -070056# the extractor's rewrite rules are generated on the fly as they depend on the
57# value of XREF_CORPUS, and that the name of the kzip file is derived from the
58# directory name by replacing '/' with '_'.
59# Go extractor should succeed.
Sasha Smundak08253a02022-08-01 16:11:25 -070060declare -ar go_modules=(build/blueprint build/soong
61 build/make/tools/canoninja build/make/tools/compliance build/make/tools/rbcrun)
Sasha Smundak8df2a5c2022-08-08 17:31:06 -070062set -e
Sasha Smundak08253a02022-08-01 16:11:25 -070063for dir in "${go_modules[@]}"; do
64 (cd "$dir";
65 outfile=$(echo "$dir" | sed -r 's|/|_|g;s|(.*)|\1.go.kzip|');
Sasha Smundak70492662021-03-08 16:29:31 -080066 KYTHE_ROOT_DIRECTORY="${source_root}" "$go_extractor" --goroot="$go_root" \
67 --rules=<(printf '[{"pattern": "(.*)","vname": {"path": "@1@", "corpus":"%s"}}]' "${XREF_CORPUS}") \
Sasha Smundak08253a02022-08-01 16:11:25 -070068 --canonicalize_package_corpus --output "${abspath_out}/soong/$outfile" ./...
Sasha Smundakfacb3602020-01-14 17:01:03 -080069 )
70done
Sasha Smundak8df2a5c2022-08-08 17:31:06 -070071set +e
Sasha Smundakca540882019-12-20 14:25:50 -080072
Sasha Smundak65143642019-09-26 20:14:28 -070073declare -r kzip_count=$(find "$out" -name '*.kzip' | wc -l)
Jingwen Chence08efd2023-07-04 07:49:03 +000074(($kzip_count>100000)) || { >&2 printf "ERROR: Too few kzip files were generated: %d\n" $kzip_count; exit 1; }
Sasha Smundakcb86c3c2019-08-08 12:51:15 -070075
76# Pack
Brian Egizi4aa056e2021-05-17 23:31:47 +000077declare -r allkzip="$KZIP_NAME.kzip"
Colin Cross6a4d3ba2021-11-05 14:07:48 -070078"$out/host/linux-x86/bin/merge_zips" "$DIST_DIR/$allkzip" @<(find "$out" -name '*.kzip')
Sasha Smundak114d9662019-09-26 14:54:19 -070079