Sasha Smundak | 6514364 | 2019-09-26 20:14:28 -0700 | [diff] [blame] | 1 | #! /bin/bash -uv |
Sasha Smundak | cb86c3c | 2019-08-08 12:51:15 -0700 | [diff] [blame] | 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. |
Sasha Smundak | 69ee7b0 | 2019-09-23 19:13:37 -0700 | [diff] [blame] | 6 | # The following environment variables affect the result: |
Sasha Smundak | 69ee7b0 | 2019-09-23 19:13:37 -0700 | [diff] [blame] | 7 | # BUILD_NUMBER build number, used to generate unique ID (will use UUID if not set) |
Brian Egizi | 29161df | 2021-05-05 23:04:34 +0000 | [diff] [blame] | 8 | # SUPERPROJECT_SHA superproject sha, used to generate unique id (will use BUILD_NUMBER if not set) |
Brian Egizi | 4aa056e | 2021-05-17 23:31:47 +0000 | [diff] [blame] | 9 | # 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 Smundak | 6514364 | 2019-09-26 20:14:28 -0700 | [diff] [blame] | 11 | # DIST_DIR where the resulting all.kzip will be placed |
Sasha Smundak | 6c2d4f9 | 2020-01-09 17:34:23 -0800 | [diff] [blame] | 12 | # KYTHE_KZIP_ENCODING proto or json (proto is default) |
Sasha Smundak | b0addaf | 2021-02-16 10:39:40 -0800 | [diff] [blame] | 13 | # KYTHE_JAVA_SOURCE_BATCH_SIZE maximum number of the Java source files in a compilation unit |
Sasha Smundak | 6514364 | 2019-09-26 20:14:28 -0700 | [diff] [blame] | 14 | # 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 Smundak | cb86c3c | 2019-08-08 12:51:15 -0700 | [diff] [blame] | 18 | |
Brian Egizi | 4aa056e | 2021-05-17 23:31:47 +0000 | [diff] [blame] | 19 | # If the SUPERPROJECT_REVISION is defined as a sha, use this as the default value if no |
| 20 | # SUPERPROJECT_SHA is specified. |
Brian Egizi | 2aded7d | 2021-05-21 00:50:21 +0000 | [diff] [blame] | 21 | if [[ ${SUPERPROJECT_REVISION:-} =~ [0-9a-f]{40} ]]; then |
Brian Egizi | 4aa056e | 2021-05-17 23:31:47 +0000 | [diff] [blame] | 22 | : ${KZIP_NAME:=${SUPERPROJECT_REVISION:-}} |
| 23 | fi |
| 24 | |
| 25 | : ${KZIP_NAME:=${SUPERPROJECT_SHA:-}} |
| 26 | : ${KZIP_NAME:=${BUILD_NUMBER:-}} |
| 27 | : ${KZIP_NAME:=$(uuidgen)} |
| 28 | |
Sasha Smundak | b0addaf | 2021-02-16 10:39:40 -0800 | [diff] [blame] | 29 | : ${KYTHE_JAVA_SOURCE_BATCH_SIZE:=500} |
Sasha Smundak | 6c2d4f9 | 2020-01-09 17:34:23 -0800 | [diff] [blame] | 30 | : ${KYTHE_KZIP_ENCODING:=proto} |
Sasha Smundak | 7049266 | 2021-03-08 16:29:31 -0800 | [diff] [blame] | 31 | : ${XREF_CORPUS:?should be set} |
Sasha Smundak | b0addaf | 2021-02-16 10:39:40 -0800 | [diff] [blame] | 32 | export KYTHE_JAVA_SOURCE_BATCH_SIZE KYTHE_KZIP_ENCODING |
Sasha Smundak | a1e178f | 2019-09-24 11:57:58 -0700 | [diff] [blame] | 33 | |
Sasha Smundak | 6514364 | 2019-09-26 20:14:28 -0700 | [diff] [blame] | 34 | # The extraction might fail for some source files, so run with -k and then check that |
| 35 | # sufficiently many files were generated. |
Sasha Smundak | 6514364 | 2019-09-26 20:14:28 -0700 | [diff] [blame] | 36 | declare -r out="${OUT_DIR:-out}" |
Sasha Smundak | b8f46cd | 2020-02-25 11:15:25 -0800 | [diff] [blame] | 37 | |
Liz Kammer | 8cac3b3 | 2023-06-29 09:55:56 -0400 | [diff] [blame] | 38 | # Build extraction files and `merge_zips` which we use later. |
| 39 | kzip_targets=( |
| 40 | merge_zips |
| 41 | xref_cxx |
| 42 | xref_java |
| 43 | # TODO: b/286390153 - reenable rust |
| 44 | # xref_rust |
| 45 | ) |
| 46 | |
| 47 | build/soong/soong_ui.bash --build-mode --all-modules --skip-soong-tests --dir=$PWD -k "${kzip_targets[@]}" |
Sasha Smundak | b8f46cd | 2020-02-25 11:15:25 -0800 | [diff] [blame] | 48 | |
| 49 | # Build extraction file for Go the files in build/{blueprint,soong} directories. |
Sasha Smundak | 17c42bd | 2019-12-27 15:29:05 -0800 | [diff] [blame] | 50 | declare -r abspath_out=$(realpath "${out}") |
Sasha Smundak | facb360 | 2020-01-14 17:01:03 -0800 | [diff] [blame] | 51 | declare -r go_extractor=$(realpath prebuilts/build-tools/linux-x86/bin/go_extractor) |
| 52 | declare -r go_root=$(realpath prebuilts/go/linux-x86) |
Sasha Smundak | b8f46cd | 2020-02-25 11:15:25 -0800 | [diff] [blame] | 53 | declare -r source_root=$PWD |
Sasha Smundak | 7049266 | 2021-03-08 16:29:31 -0800 | [diff] [blame] | 54 | |
Sasha Smundak | 08253a0 | 2022-08-01 16:11:25 -0700 | [diff] [blame] | 55 | # For the Go code, we invoke the extractor directly. The two caveats are that |
Sasha Smundak | 8df2a5c | 2022-08-08 17:31:06 -0700 | [diff] [blame] | 56 | # 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 Smundak | 08253a0 | 2022-08-01 16:11:25 -0700 | [diff] [blame] | 60 | declare -ar go_modules=(build/blueprint build/soong |
| 61 | build/make/tools/canoninja build/make/tools/compliance build/make/tools/rbcrun) |
Sasha Smundak | 8df2a5c | 2022-08-08 17:31:06 -0700 | [diff] [blame] | 62 | set -e |
Sasha Smundak | 08253a0 | 2022-08-01 16:11:25 -0700 | [diff] [blame] | 63 | for dir in "${go_modules[@]}"; do |
| 64 | (cd "$dir"; |
| 65 | outfile=$(echo "$dir" | sed -r 's|/|_|g;s|(.*)|\1.go.kzip|'); |
Sasha Smundak | 7049266 | 2021-03-08 16:29:31 -0800 | [diff] [blame] | 66 | KYTHE_ROOT_DIRECTORY="${source_root}" "$go_extractor" --goroot="$go_root" \ |
| 67 | --rules=<(printf '[{"pattern": "(.*)","vname": {"path": "@1@", "corpus":"%s"}}]' "${XREF_CORPUS}") \ |
Sasha Smundak | 08253a0 | 2022-08-01 16:11:25 -0700 | [diff] [blame] | 68 | --canonicalize_package_corpus --output "${abspath_out}/soong/$outfile" ./... |
Sasha Smundak | facb360 | 2020-01-14 17:01:03 -0800 | [diff] [blame] | 69 | ) |
| 70 | done |
Sasha Smundak | 8df2a5c | 2022-08-08 17:31:06 -0700 | [diff] [blame] | 71 | set +e |
Sasha Smundak | ca54088 | 2019-12-20 14:25:50 -0800 | [diff] [blame] | 72 | |
Sasha Smundak | 6514364 | 2019-09-26 20:14:28 -0700 | [diff] [blame] | 73 | declare -r kzip_count=$(find "$out" -name '*.kzip' | wc -l) |
Jingwen Chen | ce08efd | 2023-07-04 07:49:03 +0000 | [diff] [blame^] | 74 | (($kzip_count>100000)) || { >&2 printf "ERROR: Too few kzip files were generated: %d\n" $kzip_count; exit 1; } |
Sasha Smundak | cb86c3c | 2019-08-08 12:51:15 -0700 | [diff] [blame] | 75 | |
| 76 | # Pack |
Brian Egizi | 4aa056e | 2021-05-17 23:31:47 +0000 | [diff] [blame] | 77 | declare -r allkzip="$KZIP_NAME.kzip" |
Colin Cross | 6a4d3ba | 2021-11-05 14:07:48 -0700 | [diff] [blame] | 78 | "$out/host/linux-x86/bin/merge_zips" "$DIST_DIR/$allkzip" @<(find "$out" -name '*.kzip') |
Sasha Smundak | 114d966 | 2019-09-26 14:54:19 -0700 | [diff] [blame] | 79 | |