xref: /aosp_15_r20/build/soong/build_kzip.bash (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1*333d2b36SAndroid Build Coastguard Worker#! /bin/bash -uv
2*333d2b36SAndroid Build Coastguard Worker#
3*333d2b36SAndroid Build Coastguard Worker# Build kzip files (source files for the indexing pipeline) for the given configuration,
4*333d2b36SAndroid Build Coastguard Worker# merge them and place the resulting all.kzip into $DIST_DIR.
5*333d2b36SAndroid Build Coastguard Worker# It is assumed that the current directory is the top of the source tree.
6*333d2b36SAndroid Build Coastguard Worker# The following environment variables affect the result:
7*333d2b36SAndroid Build Coastguard Worker#   BUILD_NUMBER          build number, used to generate unique ID (will use UUID if not set)
8*333d2b36SAndroid Build Coastguard Worker#   SUPERPROJECT_SHA      superproject sha, used to generate unique id (will use BUILD_NUMBER if not set)
9*333d2b36SAndroid Build Coastguard Worker#   SUPERPROJECT_REVISION superproject revision, used for unique id if defined as a sha
10*333d2b36SAndroid Build Coastguard Worker#   KZIP_NAME             name of the output file (will use SUPERPROJECT_REVISION|SUPERPROJECT_SHA|BUILD_NUMBER|UUID if not set)
11*333d2b36SAndroid Build Coastguard Worker#   DIST_DIR              where the resulting all.kzip will be placed
12*333d2b36SAndroid Build Coastguard Worker#   KYTHE_KZIP_ENCODING   proto or json (proto is default)
13*333d2b36SAndroid Build Coastguard Worker#   KYTHE_JAVA_SOURCE_BATCH_SIZE maximum number of the Java source files in a compilation unit
14*333d2b36SAndroid Build Coastguard Worker#   OUT_DIR               output directory (out if not specified})
15*333d2b36SAndroid Build Coastguard Worker#   TARGET_BUILD_VARIANT  variant, e.g., `userdebug`
16*333d2b36SAndroid Build Coastguard Worker#   TARGET_PRODUCT        target device name, e.g., 'aosp_blueline'
17*333d2b36SAndroid Build Coastguard Worker#   XREF_CORPUS           source code repository URI, e.g., 'android.googlesource.com/platform/superproject'
18*333d2b36SAndroid Build Coastguard Worker
19*333d2b36SAndroid Build Coastguard Worker# If the SUPERPROJECT_REVISION is defined as a sha, use this as the default value if no
20*333d2b36SAndroid Build Coastguard Worker# SUPERPROJECT_SHA is specified.
21*333d2b36SAndroid Build Coastguard Workerif [[ ${SUPERPROJECT_REVISION:-} =~ [0-9a-f]{40} ]]; then
22*333d2b36SAndroid Build Coastguard Worker  : ${KZIP_NAME:=${SUPERPROJECT_REVISION:-}}
23*333d2b36SAndroid Build Coastguard Workerfi
24*333d2b36SAndroid Build Coastguard Worker
25*333d2b36SAndroid Build Coastguard Worker: ${KZIP_NAME:=${SUPERPROJECT_SHA:-}}
26*333d2b36SAndroid Build Coastguard Worker: ${KZIP_NAME:=${BUILD_NUMBER:-}}
27*333d2b36SAndroid Build Coastguard Worker: ${KZIP_NAME:=$(uuidgen)}
28*333d2b36SAndroid Build Coastguard Worker
29*333d2b36SAndroid Build Coastguard Worker: ${KYTHE_JAVA_SOURCE_BATCH_SIZE:=500}
30*333d2b36SAndroid Build Coastguard Worker: ${KYTHE_KZIP_ENCODING:=proto}
31*333d2b36SAndroid Build Coastguard Worker: ${XREF_CORPUS:?should be set}
32*333d2b36SAndroid Build Coastguard Workerexport KYTHE_JAVA_SOURCE_BATCH_SIZE KYTHE_KZIP_ENCODING
33*333d2b36SAndroid Build Coastguard Worker
34*333d2b36SAndroid Build Coastguard Worker# The extraction might fail for some source files, so run with -k and then check that
35*333d2b36SAndroid Build Coastguard Worker# sufficiently many files were generated.
36*333d2b36SAndroid Build Coastguard Workerdeclare -r out="${OUT_DIR:-out}"
37*333d2b36SAndroid Build Coastguard Worker
38*333d2b36SAndroid Build Coastguard Worker# Build extraction files and `merge_zips` which we use later.
39*333d2b36SAndroid Build Coastguard Workerkzip_targets=(
40*333d2b36SAndroid Build Coastguard Worker  merge_zips
41*333d2b36SAndroid Build Coastguard Worker  xref_cxx
42*333d2b36SAndroid Build Coastguard Worker  xref_java
43*333d2b36SAndroid Build Coastguard Worker  xref_kotlin
44*333d2b36SAndroid Build Coastguard Worker  # TODO: b/286390153 - reenable rust
45*333d2b36SAndroid Build Coastguard Worker  # xref_rust
46*333d2b36SAndroid Build Coastguard Worker)
47*333d2b36SAndroid Build Coastguard Worker
48*333d2b36SAndroid Build Coastguard Workerbuild/soong/soong_ui.bash --build-mode --all-modules --dir=$PWD -k --skip-soong-tests --ninja_weight_source=not_used "${kzip_targets[@]}"
49*333d2b36SAndroid Build Coastguard Worker
50*333d2b36SAndroid Build Coastguard Worker# Build extraction file for Go the files in build/{blueprint,soong} directories.
51*333d2b36SAndroid Build Coastguard Workerdeclare -r abspath_out=$(realpath "${out}")
52*333d2b36SAndroid Build Coastguard Workerdeclare -r go_extractor=$(realpath prebuilts/build-tools/linux-x86/bin/go_extractor)
53*333d2b36SAndroid Build Coastguard Workerdeclare -r go_root=$(realpath prebuilts/go/linux-x86)
54*333d2b36SAndroid Build Coastguard Workerdeclare -r source_root=$PWD
55*333d2b36SAndroid Build Coastguard Worker
56*333d2b36SAndroid Build Coastguard Worker# For the Go code, we invoke the extractor directly. The two caveats are that
57*333d2b36SAndroid Build Coastguard Worker# the extractor's rewrite rules are generated on the fly as they depend on the
58*333d2b36SAndroid Build Coastguard Worker# value of XREF_CORPUS, and that the name of the kzip file is derived from the
59*333d2b36SAndroid Build Coastguard Worker# directory name by replacing '/' with '_'.
60*333d2b36SAndroid Build Coastguard Worker# Go extractor should succeed.
61*333d2b36SAndroid Build Coastguard Workerdeclare -ar go_modules=(build/blueprint build/soong
62*333d2b36SAndroid Build Coastguard Worker  build/make/tools/canoninja build/make/tools/compliance build/make/tools/rbcrun)
63*333d2b36SAndroid Build Coastguard Workerset -e
64*333d2b36SAndroid Build Coastguard Workerfor dir in "${go_modules[@]}"; do
65*333d2b36SAndroid Build Coastguard Worker  (cd "$dir";
66*333d2b36SAndroid Build Coastguard Worker   outfile=$(echo "$dir" | sed -r 's|/|_|g;s|(.*)|\1.go.kzip|');
67*333d2b36SAndroid Build Coastguard Worker   KYTHE_ROOT_DIRECTORY="${source_root}" "$go_extractor" --goroot="$go_root" \
68*333d2b36SAndroid Build Coastguard Worker   --rules=<(printf '[{"pattern": "(.*)","vname": {"path": "@1@", "corpus":"%s"}}]' "${XREF_CORPUS}") \
69*333d2b36SAndroid Build Coastguard Worker   --canonicalize_package_corpus --output "${abspath_out}/soong/$outfile" ./...
70*333d2b36SAndroid Build Coastguard Worker  )
71*333d2b36SAndroid Build Coastguard Workerdone
72*333d2b36SAndroid Build Coastguard Workerset +e
73*333d2b36SAndroid Build Coastguard Worker
74*333d2b36SAndroid Build Coastguard Workerdeclare -r kzip_count=$(find "$out" -name '*.kzip' | wc -l)
75*333d2b36SAndroid Build Coastguard Worker(($kzip_count>100000)) || { >&2 printf "ERROR: Too few kzip files were generated: %d\n" $kzip_count; exit 1; }
76*333d2b36SAndroid Build Coastguard Worker
77*333d2b36SAndroid Build Coastguard Worker# Pack
78*333d2b36SAndroid Build Coastguard Workerdeclare -r allkzip="$KZIP_NAME.kzip"
79*333d2b36SAndroid Build Coastguard Worker"$out/host/linux-x86/bin/merge_zips" "$DIST_DIR/$allkzip" @<(find "$out" -name '*.kzip')
80*333d2b36SAndroid Build Coastguard Worker
81