blob: 7524d8220b612e7b83b0cea617bb3b48dbc29eaf [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
Yu Liud37ccb72024-09-19 18:53:41 +000074 echo "Tests passed"
Yu Liu4faab812024-09-12 23:56:31 +000075}
76
77function compare_files() {
Yu Liud37ccb72024-09-19 18:53:41 +000078 count=0
Yu Liu4faab812024-09-12 23:56:31 +000079 for file_before in ${OUTPUT_DIR}/before/*.ninja; do
80 file_after="${OUTPUT_DIR}/after/$(basename "$file_before")"
81 assert_files_equal $file_before $file_after
82 compare_mtimes $file_before $file_after
83 if [ $? -ne 0 ]; then
84 echo "Files have identical mtime: $file_before $file_after"
85 exit 1
86 fi
Yu Liud37ccb72024-09-19 18:53:41 +000087 ((count++))
Yu Liu4faab812024-09-12 23:56:31 +000088 done
Yu Liud37ccb72024-09-19 18:53:41 +000089 echo "Compared $count ninja files"
Yu Liu4faab812024-09-12 23:56:31 +000090
Yu Liud37ccb72024-09-19 18:53:41 +000091 count=0
Yu Liu4faab812024-09-12 23:56:31 +000092 for file_before in ${OUTPUT_DIR}/before/*.mk; do
93 file_after="${OUTPUT_DIR}/after/$(basename "$file_before")"
94 assert_files_equal $file_before $file_after
95 compare_mtimes $file_before $file_after
96 # mk files shouldn't be regenerated
97 if [ $? -ne 1 ]; then
98 echo "Files have different mtimes: $file_before $file_after"
99 exit 1
100 fi
Yu Liud37ccb72024-09-19 18:53:41 +0000101 ((count++))
Yu Liu4faab812024-09-12 23:56:31 +0000102 done
Yu Liud37ccb72024-09-19 18:53:41 +0000103 echo "Compared $count mk files"
Yu Liu4faab812024-09-12 23:56:31 +0000104
Yu Liud37ccb72024-09-19 18:53:41 +0000105 count=0
Yu Liu4faab812024-09-12 23:56:31 +0000106 for file_before in ${OUTPUT_DIR}/before/build_aosp_arm_ninja_incremental/*.ninja; do
107 file_after="${OUTPUT_DIR}/after/build_aosp_arm_ninja_incremental/$(basename "$file_before")"
108 assert_files_equal $file_before $file_after
109 compare_mtimes $file_before $file_after
110 # ninja files of skipped modules shouldn't be regenerated
111 if [ $? -ne 1 ]; then
112 echo "Files have different mtimes: $file_before $file_after"
113 exit 1
114 fi
Yu Liud37ccb72024-09-19 18:53:41 +0000115 ((count++))
Yu Liu4faab812024-09-12 23:56:31 +0000116 done
Yu Liud37ccb72024-09-19 18:53:41 +0000117 echo "Compared $count incremental ninja files"
Yu Liu4faab812024-09-12 23:56:31 +0000118}
119
120test_build_action_restoring