blob: 981827d6fd2a6ca41b32c1746c5f54f2fa01a3e4 [file] [log] [blame]
Yu Liu4faab812024-09-12 23:56:31 +00001#!/bin/bash -u
2
3set -o pipefail
4
5# Test that the mk and ninja files generated by Soong don't change if some
6# incremental modules are restored from cache.
7
8OUTPUT_DIR="$(mktemp -d tmp.XXXXXX)"
9echo ${OUTPUT_DIR}
10
11function cleanup {
12 rm -rf "${OUTPUT_DIR}"
13}
14trap cleanup EXIT
15
16function run_soong_build {
17 USE_RBE=false TARGET_PRODUCT=aosp_arm TARGET_RELEASE=trunk_staging TARGET_BUILD_VARIANT=userdebug build/soong/soong_ui.bash --make-mode --incremental-build-actions nothing
18}
19
20function run_soong_clean {
21 build/soong/soong_ui.bash --make-mode clean
22}
23
24function assert_files_equal {
25 if [ $# -ne 2 ]; then
26 echo "Usage: assert_files_equal file1 file2"
27 exit 1
28 fi
29
30 if ! cmp -s "$1" "$2"; then
31 echo "Files are different: $1 $2"
32 exit 1
33 fi
34}
35
36function compare_mtimes() {
37 if [ $# -ne 2 ]; then
38 echo "Usage: compare_mtimes file1 file2"
39 exit 1
40 fi
41
42 file1_mtime=$(stat -c '%Y' $1)
43 file2_mtime=$(stat -c '%Y' $2)
44
45 if [ "$file1_mtime" -eq "$file2_mtime" ]; then
46 return 1
47 else
48 return 0
49 fi
50}
51
52function test_build_action_restoring() {
53 run_soong_clean
54 cat > ${OUTPUT_DIR}/Android.bp <<'EOF'
55python_binary_host {
56 name: "my_little_binary_host",
57 srcs: ["my_little_binary_host.py"],
58}
59EOF
60 touch ${OUTPUT_DIR}/my_little_binary_host.py
61 run_soong_build
62 mkdir -p "${OUTPUT_DIR}/before"
63 cp -pr out/soong/build_aosp_arm_ninja_incremental out/soong/*.mk out/soong/build.aosp_arm.*.ninja ${OUTPUT_DIR}/before
64 # add a comment to the bp file, this should force a new analysis but no module
65 # should be really impacted, so all the incremental modules should be skipped.
66 cat >> ${OUTPUT_DIR}/Android.bp <<'EOF'
67// new comments
68EOF
69 run_soong_build
70 mkdir -p "${OUTPUT_DIR}/after"
71 cp -pr out/soong/build_aosp_arm_ninja_incremental out/soong/*.mk out/soong/build.aosp_arm.*.ninja ${OUTPUT_DIR}/after
72
73 compare_files
74}
75
76function compare_files() {
77 for file_before in ${OUTPUT_DIR}/before/*.ninja; do
78 file_after="${OUTPUT_DIR}/after/$(basename "$file_before")"
79 assert_files_equal $file_before $file_after
80 compare_mtimes $file_before $file_after
81 if [ $? -ne 0 ]; then
82 echo "Files have identical mtime: $file_before $file_after"
83 exit 1
84 fi
85 done
86
87 for file_before in ${OUTPUT_DIR}/before/*.mk; do
88 file_after="${OUTPUT_DIR}/after/$(basename "$file_before")"
89 assert_files_equal $file_before $file_after
90 compare_mtimes $file_before $file_after
91 # mk files shouldn't be regenerated
92 if [ $? -ne 1 ]; then
93 echo "Files have different mtimes: $file_before $file_after"
94 exit 1
95 fi
96 done
97
98 for file_before in ${OUTPUT_DIR}/before/build_aosp_arm_ninja_incremental/*.ninja; do
99 file_after="${OUTPUT_DIR}/after/build_aosp_arm_ninja_incremental/$(basename "$file_before")"
100 assert_files_equal $file_before $file_after
101 compare_mtimes $file_before $file_after
102 # ninja files of skipped modules shouldn't be regenerated
103 if [ $? -ne 1 ]; then
104 echo "Files have different mtimes: $file_before $file_after"
105 exit 1
106 fi
107 done
108}
109
110test_build_action_restoring