xref: /aosp_15_r20/build/soong/tests/build_action_caching_test.sh (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1*333d2b36SAndroid Build Coastguard Worker#!/bin/bash -u
2*333d2b36SAndroid Build Coastguard Worker
3*333d2b36SAndroid Build Coastguard Workerset -o pipefail
4*333d2b36SAndroid Build Coastguard Worker
5*333d2b36SAndroid Build Coastguard Worker# Test that the mk and ninja files generated by Soong don't change if some
6*333d2b36SAndroid Build Coastguard Worker# incremental modules are restored from cache.
7*333d2b36SAndroid Build Coastguard Worker
8*333d2b36SAndroid Build Coastguard WorkerOUTPUT_DIR="$(mktemp -d tmp.XXXXXX)"
9*333d2b36SAndroid Build Coastguard Worker
10*333d2b36SAndroid Build Coastguard Workerecho ${OUTPUT_DIR}
11*333d2b36SAndroid Build Coastguard Worker
12*333d2b36SAndroid Build Coastguard Workerfunction cleanup {
13*333d2b36SAndroid Build Coastguard Worker  rm -rf "${OUTPUT_DIR}"
14*333d2b36SAndroid Build Coastguard Worker}
15*333d2b36SAndroid Build Coastguard Workertrap cleanup EXIT
16*333d2b36SAndroid Build Coastguard Worker
17*333d2b36SAndroid Build Coastguard Workerfunction run_soong_build {
18*333d2b36SAndroid Build Coastguard Worker  USE_RBE=false TARGET_PRODUCT=aosp_arm TARGET_RELEASE=trunk_staging TARGET_BUILD_VARIANT=userdebug build/soong/soong_ui.bash --make-mode "$@" nothing
19*333d2b36SAndroid Build Coastguard Worker}
20*333d2b36SAndroid Build Coastguard Worker
21*333d2b36SAndroid Build Coastguard Workerfunction run_soong_clean {
22*333d2b36SAndroid Build Coastguard Worker  build/soong/soong_ui.bash --make-mode clean
23*333d2b36SAndroid Build Coastguard Worker}
24*333d2b36SAndroid Build Coastguard Worker
25*333d2b36SAndroid Build Coastguard Workerfunction assert_files_equal {
26*333d2b36SAndroid Build Coastguard Worker  if [ $# -ne 2 ]; then
27*333d2b36SAndroid Build Coastguard Worker    echo "Usage: assert_files_equal file1 file2"
28*333d2b36SAndroid Build Coastguard Worker    exit 1
29*333d2b36SAndroid Build Coastguard Worker  fi
30*333d2b36SAndroid Build Coastguard Worker
31*333d2b36SAndroid Build Coastguard Worker  if ! cmp -s "$1" "$2"; then
32*333d2b36SAndroid Build Coastguard Worker    echo "Files are different: $1 $2"
33*333d2b36SAndroid Build Coastguard Worker    exit 1
34*333d2b36SAndroid Build Coastguard Worker  fi
35*333d2b36SAndroid Build Coastguard Worker}
36*333d2b36SAndroid Build Coastguard Worker
37*333d2b36SAndroid Build Coastguard Workerfunction compare_mtimes() {
38*333d2b36SAndroid Build Coastguard Worker  if [ $# -ne 2 ]; then
39*333d2b36SAndroid Build Coastguard Worker    echo "Usage: compare_mtimes file1 file2"
40*333d2b36SAndroid Build Coastguard Worker    exit 1
41*333d2b36SAndroid Build Coastguard Worker  fi
42*333d2b36SAndroid Build Coastguard Worker
43*333d2b36SAndroid Build Coastguard Worker  file1_mtime=$(stat -c '%Y' $1)
44*333d2b36SAndroid Build Coastguard Worker  file2_mtime=$(stat -c '%Y' $2)
45*333d2b36SAndroid Build Coastguard Worker
46*333d2b36SAndroid Build Coastguard Worker  if [ "$file1_mtime" -eq "$file2_mtime" ]; then
47*333d2b36SAndroid Build Coastguard Worker      return 1
48*333d2b36SAndroid Build Coastguard Worker  else
49*333d2b36SAndroid Build Coastguard Worker      return 0
50*333d2b36SAndroid Build Coastguard Worker  fi
51*333d2b36SAndroid Build Coastguard Worker}
52*333d2b36SAndroid Build Coastguard Worker
53*333d2b36SAndroid Build Coastguard Workerfunction test_build_action_restoring() {
54*333d2b36SAndroid Build Coastguard Worker  local test_dir="${OUTPUT_DIR}/test_build_action_restoring"
55*333d2b36SAndroid Build Coastguard Worker  mkdir -p ${test_dir}
56*333d2b36SAndroid Build Coastguard Worker  run_soong_clean
57*333d2b36SAndroid Build Coastguard Worker  cat > ${test_dir}/Android.bp <<'EOF'
58*333d2b36SAndroid Build Coastguard Workerpython_binary_host {
59*333d2b36SAndroid Build Coastguard Worker  name: "my_little_binary_host",
60*333d2b36SAndroid Build Coastguard Worker  srcs: ["my_little_binary_host.py"],
61*333d2b36SAndroid Build Coastguard Worker}
62*333d2b36SAndroid Build Coastguard WorkerEOF
63*333d2b36SAndroid Build Coastguard Worker  touch ${test_dir}/my_little_binary_host.py
64*333d2b36SAndroid Build Coastguard Worker  run_soong_build --incremental-build-actions
65*333d2b36SAndroid Build Coastguard Worker  local dir_before="${test_dir}/before"
66*333d2b36SAndroid Build Coastguard Worker  mkdir -p ${dir_before}
67*333d2b36SAndroid Build Coastguard Worker  cp -pr out/soong/build_aosp_arm_ninja_incremental out/soong/*.mk out/soong/build.aosp_arm*.ninja ${test_dir}/before
68*333d2b36SAndroid Build Coastguard Worker  # add a comment to the bp file, this should force a new analysis but no module
69*333d2b36SAndroid Build Coastguard Worker  # should be really impacted, so all the incremental modules should be skipped.
70*333d2b36SAndroid Build Coastguard Worker  cat >> ${test_dir}/Android.bp <<'EOF'
71*333d2b36SAndroid Build Coastguard Worker// new comments
72*333d2b36SAndroid Build Coastguard WorkerEOF
73*333d2b36SAndroid Build Coastguard Worker  run_soong_build --incremental-build-actions
74*333d2b36SAndroid Build Coastguard Worker  local dir_after="${test_dir}/after"
75*333d2b36SAndroid Build Coastguard Worker  mkdir -p ${dir_after}
76*333d2b36SAndroid Build Coastguard Worker  cp -pr out/soong/build_aosp_arm_ninja_incremental out/soong/*.mk out/soong/build.aosp_arm*.ninja ${test_dir}/after
77*333d2b36SAndroid Build Coastguard Worker
78*333d2b36SAndroid Build Coastguard Worker  compare_incremental_files $dir_before $dir_after
79*333d2b36SAndroid Build Coastguard Worker  rm -rf "$test_dir"
80*333d2b36SAndroid Build Coastguard Worker  echo "test_build_action_restoring test passed"
81*333d2b36SAndroid Build Coastguard Worker}
82*333d2b36SAndroid Build Coastguard Worker
83*333d2b36SAndroid Build Coastguard Workerfunction test_incremental_build_parity() {
84*333d2b36SAndroid Build Coastguard Worker  local test_dir="${OUTPUT_DIR}/test_incremental_build_parity"
85*333d2b36SAndroid Build Coastguard Worker  run_soong_clean
86*333d2b36SAndroid Build Coastguard Worker  run_soong_build
87*333d2b36SAndroid Build Coastguard Worker  local dir_before="${test_dir}/before"
88*333d2b36SAndroid Build Coastguard Worker  mkdir -p ${dir_before}
89*333d2b36SAndroid Build Coastguard Worker  cp -pr out/soong/*.mk out/soong/build.aosp_arm*.ninja ${test_dir}/before
90*333d2b36SAndroid Build Coastguard Worker
91*333d2b36SAndroid Build Coastguard Worker  # Now run clean build with incremental enabled
92*333d2b36SAndroid Build Coastguard Worker  run_soong_clean
93*333d2b36SAndroid Build Coastguard Worker  run_soong_build --incremental-build-actions
94*333d2b36SAndroid Build Coastguard Worker  local dir_after="${test_dir}/after"
95*333d2b36SAndroid Build Coastguard Worker  mkdir -p ${dir_after}
96*333d2b36SAndroid Build Coastguard Worker  cp -pr out/soong/build_aosp_arm_ninja_incremental out/soong/*.mk out/soong/build.aosp_arm*.ninja ${test_dir}/after
97*333d2b36SAndroid Build Coastguard Worker
98*333d2b36SAndroid Build Coastguard Worker  compare_files_parity $dir_before $dir_after
99*333d2b36SAndroid Build Coastguard Worker  rm -rf "$test_dir"
100*333d2b36SAndroid Build Coastguard Worker  echo "test_incremental_build_parity test passed"
101*333d2b36SAndroid Build Coastguard Worker}
102*333d2b36SAndroid Build Coastguard Worker
103*333d2b36SAndroid Build Coastguard Workerfunction compare_files_parity() {
104*333d2b36SAndroid Build Coastguard Worker  local dir_before=$1; shift
105*333d2b36SAndroid Build Coastguard Worker  local dir_after=$1; shift
106*333d2b36SAndroid Build Coastguard Worker  count=0
107*333d2b36SAndroid Build Coastguard Worker  for file_before in ${dir_before}/*.mk; do
108*333d2b36SAndroid Build Coastguard Worker    file_after="${dir_after}/$(basename "$file_before")"
109*333d2b36SAndroid Build Coastguard Worker    assert_files_equal $file_before $file_after
110*333d2b36SAndroid Build Coastguard Worker    ((count++))
111*333d2b36SAndroid Build Coastguard Worker  done
112*333d2b36SAndroid Build Coastguard Worker  echo "Compared $count mk files"
113*333d2b36SAndroid Build Coastguard Worker
114*333d2b36SAndroid Build Coastguard Worker  combined_before_file="${dir_before}/combined_files.ninja"
115*333d2b36SAndroid Build Coastguard Worker  count=0
116*333d2b36SAndroid Build Coastguard Worker  for file in ${dir_before}/build.aosp_arm.*.ninja; do
117*333d2b36SAndroid Build Coastguard Worker    cat $file >> $combined_before_file
118*333d2b36SAndroid Build Coastguard Worker    ((count++))
119*333d2b36SAndroid Build Coastguard Worker  done
120*333d2b36SAndroid Build Coastguard Worker  echo "Combined $count ninja files from normal build"
121*333d2b36SAndroid Build Coastguard Worker
122*333d2b36SAndroid Build Coastguard Worker  combined_after_file="${dir_after}/combined_files.ninja"
123*333d2b36SAndroid Build Coastguard Worker  count=0
124*333d2b36SAndroid Build Coastguard Worker  for file in ${dir_after}/build.aosp_arm.*.ninja; do
125*333d2b36SAndroid Build Coastguard Worker    cat $file >> $combined_after_file
126*333d2b36SAndroid Build Coastguard Worker    ((count++))
127*333d2b36SAndroid Build Coastguard Worker  done
128*333d2b36SAndroid Build Coastguard Worker  echo "Combined $count ninja files from incremental build"
129*333d2b36SAndroid Build Coastguard Worker
130*333d2b36SAndroid Build Coastguard Worker  combined_incremental_ninjas="${dir_after}/combined_incremental_files.ninja"
131*333d2b36SAndroid Build Coastguard Worker  count=0
132*333d2b36SAndroid Build Coastguard Worker  for file in ${dir_after}/build_aosp_arm_ninja_incremental/*.ninja; do
133*333d2b36SAndroid Build Coastguard Worker    cat $file >> $combined_incremental_ninjas
134*333d2b36SAndroid Build Coastguard Worker    ((count++))
135*333d2b36SAndroid Build Coastguard Worker  done
136*333d2b36SAndroid Build Coastguard Worker  echo "Combined $count incremental ninja files"
137*333d2b36SAndroid Build Coastguard Worker
138*333d2b36SAndroid Build Coastguard Worker  cat $combined_incremental_ninjas >> $combined_after_file
139*333d2b36SAndroid Build Coastguard Worker  sort $combined_after_file -o $combined_after_file
140*333d2b36SAndroid Build Coastguard Worker  sort $combined_before_file -o $combined_before_file
141*333d2b36SAndroid Build Coastguard Worker  assert_files_equal $combined_before_file $combined_after_file
142*333d2b36SAndroid Build Coastguard Worker}
143*333d2b36SAndroid Build Coastguard Worker
144*333d2b36SAndroid Build Coastguard Workerfunction compare_incremental_files() {
145*333d2b36SAndroid Build Coastguard Worker  local dir_before=$1; shift
146*333d2b36SAndroid Build Coastguard Worker  local dir_after=$1; shift
147*333d2b36SAndroid Build Coastguard Worker  count=0
148*333d2b36SAndroid Build Coastguard Worker  for file_before in ${dir_before}/*.ninja; do
149*333d2b36SAndroid Build Coastguard Worker    file_after="${dir_after}/$(basename "$file_before")"
150*333d2b36SAndroid Build Coastguard Worker    assert_files_equal $file_before $file_after
151*333d2b36SAndroid Build Coastguard Worker    compare_mtimes $file_before $file_after
152*333d2b36SAndroid Build Coastguard Worker    if [ $? -ne 0 ]; then
153*333d2b36SAndroid Build Coastguard Worker      echo "Files have identical mtime: $file_before $file_after"
154*333d2b36SAndroid Build Coastguard Worker      exit 1
155*333d2b36SAndroid Build Coastguard Worker    fi
156*333d2b36SAndroid Build Coastguard Worker    ((count++))
157*333d2b36SAndroid Build Coastguard Worker  done
158*333d2b36SAndroid Build Coastguard Worker  echo "Compared $count ninja files"
159*333d2b36SAndroid Build Coastguard Worker
160*333d2b36SAndroid Build Coastguard Worker  count=0
161*333d2b36SAndroid Build Coastguard Worker  for file_before in ${dir_before}/*.mk; do
162*333d2b36SAndroid Build Coastguard Worker    file_after="${dir_after}/$(basename "$file_before")"
163*333d2b36SAndroid Build Coastguard Worker    assert_files_equal $file_before $file_after
164*333d2b36SAndroid Build Coastguard Worker    compare_mtimes $file_before $file_after
165*333d2b36SAndroid Build Coastguard Worker    # mk files shouldn't be regenerated
166*333d2b36SAndroid Build Coastguard Worker    if [ $? -ne 1 ]; then
167*333d2b36SAndroid Build Coastguard Worker      echo "Files have different mtimes: $file_before $file_after"
168*333d2b36SAndroid Build Coastguard Worker      exit 1
169*333d2b36SAndroid Build Coastguard Worker    fi
170*333d2b36SAndroid Build Coastguard Worker    ((count++))
171*333d2b36SAndroid Build Coastguard Worker  done
172*333d2b36SAndroid Build Coastguard Worker  echo "Compared $count mk files"
173*333d2b36SAndroid Build Coastguard Worker
174*333d2b36SAndroid Build Coastguard Worker  count=0
175*333d2b36SAndroid Build Coastguard Worker  for file_before in ${dir_before}/build_aosp_arm_ninja_incremental/*.ninja; do
176*333d2b36SAndroid Build Coastguard Worker    file_after="${dir_after}/build_aosp_arm_ninja_incremental/$(basename "$file_before")"
177*333d2b36SAndroid Build Coastguard Worker    assert_files_equal $file_before $file_after
178*333d2b36SAndroid Build Coastguard Worker    compare_mtimes $file_before $file_after
179*333d2b36SAndroid Build Coastguard Worker    # ninja files of skipped modules shouldn't be regenerated
180*333d2b36SAndroid Build Coastguard Worker    if [ $? -ne 1 ]; then
181*333d2b36SAndroid Build Coastguard Worker      echo "Files have different mtimes: $file_before $file_after"
182*333d2b36SAndroid Build Coastguard Worker      exit 1
183*333d2b36SAndroid Build Coastguard Worker    fi
184*333d2b36SAndroid Build Coastguard Worker    ((count++))
185*333d2b36SAndroid Build Coastguard Worker  done
186*333d2b36SAndroid Build Coastguard Worker  echo "Compared $count incremental ninja files"
187*333d2b36SAndroid Build Coastguard Worker}
188*333d2b36SAndroid Build Coastguard Worker
189*333d2b36SAndroid Build Coastguard Workertest_incremental_build_parity
190*333d2b36SAndroid Build Coastguard Workertest_build_action_restoring
191