blob: 8a64a56b981194408c2dbd4d9f5cc584808150bf [file] [log] [blame]
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -04001#!/bin/bash -eu
2
3set -o pipefail
4
5# Test that bp2build and Bazel can play nicely together
6
7source "$(dirname "$0")/lib.sh"
8
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -04009readonly GENERATED_BUILD_FILE_NAME="BUILD.bazel"
10
Usta Shrestha572ecec2022-12-08 01:29:21 -050011function test_bp2build_null_build {
Jingwen Chen53dfa402021-08-12 09:37:14 +000012 setup
Lukacs T. Berkia1b93722021-09-02 17:23:06 +020013 run_soong bp2build
Usta Shrestha2c9a5e32022-06-09 12:22:36 -040014 local -r output_mtime1=$(stat -c "%y" out/soong/bp2build_workspace_marker)
Jingwen Chen53dfa402021-08-12 09:37:14 +000015
Lukacs T. Berkia1b93722021-09-02 17:23:06 +020016 run_soong bp2build
Usta Shrestha2c9a5e32022-06-09 12:22:36 -040017 local -r output_mtime2=$(stat -c "%y" out/soong/bp2build_workspace_marker)
Jingwen Chen53dfa402021-08-12 09:37:14 +000018
19 if [[ "$output_mtime1" != "$output_mtime2" ]]; then
20 fail "Output bp2build marker file changed on null build"
21 fi
22}
23
Chris Parsons520e88b2023-02-09 17:54:00 -050024# Tests that, if bp2build reruns due to a blueprint file changing, that
25# BUILD files whose contents are unchanged are not regenerated.
26function test_bp2build_unchanged {
27 setup
28
29 mkdir -p pkg
30 touch pkg/x.txt
31 cat > pkg/Android.bp <<'EOF'
32filegroup {
33 name: "x",
34 srcs: ["x.txt"],
35 bazel_module: {bp2build_available: true},
36 }
37EOF
38
39 run_soong bp2build
40 local -r buildfile_mtime1=$(stat -c "%y" out/soong/bp2build/pkg/BUILD.bazel)
41 local -r marker_mtime1=$(stat -c "%y" out/soong/bp2build_workspace_marker)
42
43 # Force bp2build to rerun by updating the timestamp of a blueprint file.
44 touch pkg/Android.bp
45
46 run_soong bp2build
47 local -r buildfile_mtime2=$(stat -c "%y" out/soong/bp2build/pkg/BUILD.bazel)
48 local -r marker_mtime2=$(stat -c "%y" out/soong/bp2build_workspace_marker)
49
50 if [[ "$marker_mtime1" == "$marker_mtime2" ]]; then
51 fail "Expected bp2build marker file to change"
52 fi
53 if [[ "$buildfile_mtime1" != "$buildfile_mtime2" ]]; then
54 fail "BUILD.bazel was updated even though contents are same"
55 fi
Cole Faustc9508aa2023-02-07 11:38:27 -080056
57 # Force bp2build to rerun by updating the timestamp of the constants_exported_to_soong.bzl file.
58 touch build/bazel/constants_exported_to_soong.bzl
59
60 run_soong bp2build
61 local -r buildfile_mtime3=$(stat -c "%y" out/soong/bp2build/pkg/BUILD.bazel)
62 local -r marker_mtime3=$(stat -c "%y" out/soong/bp2build_workspace_marker)
63
64 if [[ "$marker_mtime2" == "$marker_mtime3" ]]; then
65 fail "Expected bp2build marker file to change"
66 fi
67 if [[ "$buildfile_mtime2" != "$buildfile_mtime3" ]]; then
68 fail "BUILD.bazel was updated even though contents are same"
69 fi
Chris Parsons520e88b2023-02-09 17:54:00 -050070}
71
72# Tests that blueprint files that are deleted are not present when the
73# bp2build tree is regenerated.
74function test_bp2build_deleted_blueprint {
75 setup
76
77 mkdir -p pkg
78 touch pkg/x.txt
79 cat > pkg/Android.bp <<'EOF'
80filegroup {
81 name: "x",
82 srcs: ["x.txt"],
83 bazel_module: {bp2build_available: true},
84 }
85EOF
86
87 run_soong bp2build
88 if [[ ! -e "./out/soong/bp2build/pkg/BUILD.bazel" ]]; then
89 fail "Expected pkg/BUILD.bazel to be generated"
90 fi
91
92 rm pkg/Android.bp
93
94 run_soong bp2build
95 if [[ -e "./out/soong/bp2build/pkg/BUILD.bazel" ]]; then
96 fail "Expected pkg/BUILD.bazel to be deleted"
97 fi
98}
99
Usta Shrestha572ecec2022-12-08 01:29:21 -0500100function test_bp2build_null_build_with_globs {
Jingwen Chen53dfa402021-08-12 09:37:14 +0000101 setup
102
103 mkdir -p foo/bar
104 cat > foo/bar/Android.bp <<'EOF'
105filegroup {
106 name: "globs",
107 srcs: ["*.txt"],
108 }
109EOF
110 touch foo/bar/a.txt foo/bar/b.txt
111
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200112 run_soong bp2build
Usta Shrestha2c9a5e32022-06-09 12:22:36 -0400113 local -r output_mtime1=$(stat -c "%y" out/soong/bp2build_workspace_marker)
Jingwen Chen53dfa402021-08-12 09:37:14 +0000114
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200115 run_soong bp2build
Usta Shrestha2c9a5e32022-06-09 12:22:36 -0400116 local -r output_mtime2=$(stat -c "%y" out/soong/bp2build_workspace_marker)
Jingwen Chen53dfa402021-08-12 09:37:14 +0000117
118 if [[ "$output_mtime1" != "$output_mtime2" ]]; then
119 fail "Output bp2build marker file changed on null build"
120 fi
121}
122
Jingwen Chen7e11eb82022-10-13 09:25:38 +0000123function test_different_relative_outdir {
124 setup
Jingwen Chen7e11eb82022-10-13 09:25:38 +0000125
126 mkdir -p a
127 touch a/g.txt
128 cat > a/Android.bp <<'EOF'
129filegroup {
130 name: "g",
131 srcs: ["g.txt"],
132 bazel_module: {bp2build_available: true},
133 }
134EOF
135
136 # A directory under $MOCK_TOP
137 outdir=out2
138 trap "rm -rf $outdir" EXIT
139 # Modify OUT_DIR in a subshell so it doesn't affect the top level one.
Cole Faustde12be32022-11-19 15:14:48 -0800140 (export OUT_DIR=$outdir; run_soong bp2build && run_bazel build --config=bp2build --config=ci //a:g)
Jingwen Chen7e11eb82022-10-13 09:25:38 +0000141}
142
Jingwen Chen7e11eb82022-10-13 09:25:38 +0000143function test_different_absolute_outdir {
144 setup
Jingwen Chen7e11eb82022-10-13 09:25:38 +0000145
146 mkdir -p a
147 touch a/g.txt
148 cat > a/Android.bp <<'EOF'
149filegroup {
150 name: "g",
151 srcs: ["g.txt"],
152 bazel_module: {bp2build_available: true},
153 }
154EOF
155
156 # A directory under /tmp/...
157 outdir=$(mktemp -t -d st.XXXXX)
158 trap 'rm -rf $outdir' EXIT
159 # Modify OUT_DIR in a subshell so it doesn't affect the top level one.
Cole Faustde12be32022-11-19 15:14:48 -0800160 (export OUT_DIR=$outdir; run_soong bp2build && run_bazel build --config=bp2build --config=ci //a:g)
Jingwen Chen7e11eb82022-10-13 09:25:38 +0000161}
162
Usta Shrestha572ecec2022-12-08 01:29:21 -0500163function _bp2build_generates_all_buildfiles {
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400164 setup
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400165
166 mkdir -p foo/convertible_soong_module
167 cat > foo/convertible_soong_module/Android.bp <<'EOF'
168genrule {
169 name: "the_answer",
170 cmd: "echo '42' > $(out)",
171 out: [
172 "the_answer.txt",
173 ],
174 bazel_module: {
175 bp2build_available: true,
176 },
177 }
178EOF
179
180 mkdir -p foo/unconvertible_soong_module
181 cat > foo/unconvertible_soong_module/Android.bp <<'EOF'
182genrule {
183 name: "not_the_answer",
184 cmd: "echo '43' > $(out)",
185 out: [
186 "not_the_answer.txt",
187 ],
188 bazel_module: {
189 bp2build_available: false,
190 },
191 }
192EOF
193
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200194 run_soong bp2build
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400195
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -0400196 if [[ ! -f "./out/soong/workspace/foo/convertible_soong_module/${GENERATED_BUILD_FILE_NAME}" ]]; then
197 fail "./out/soong/workspace/foo/convertible_soong_module/${GENERATED_BUILD_FILE_NAME} was not generated"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400198 fi
199
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -0400200 if [[ ! -f "./out/soong/workspace/foo/unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME}" ]]; then
201 fail "./out/soong/workspace/foo/unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME} was not generated"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400202 fi
203
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -0400204 if ! grep "the_answer" "./out/soong/workspace/foo/convertible_soong_module/${GENERATED_BUILD_FILE_NAME}"; then
205 fail "missing BUILD target the_answer in convertible_soong_module/${GENERATED_BUILD_FILE_NAME}"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400206 fi
207
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -0400208 if grep "not_the_answer" "./out/soong/workspace/foo/unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME}"; then
209 fail "found unexpected BUILD target not_the_answer in unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME}"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400210 fi
211
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -0400212 if ! grep "filegroup" "./out/soong/workspace/foo/unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME}"; then
213 fail "missing filegroup in unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME}"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400214 fi
215
216 # NOTE: We don't actually use the extra BUILD file for anything here
Cole Faustde12be32022-11-19 15:14:48 -0800217 run_bazel build --config=android --config=bp2build --config=ci //foo/...
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400218
Usta (Tsering) Shresthac4c07b12022-11-08 18:31:14 -0500219 local -r the_answer_file="$(find -L bazel-out -name the_answer.txt)"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400220 if [[ ! -f "${the_answer_file}" ]]; then
Cole Faustb85d1a12022-11-08 18:14:01 -0800221 fail "Expected the_answer.txt to be generated, but was missing"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400222 fi
223 if ! grep 42 "${the_answer_file}"; then
224 fail "Expected to find 42 in '${the_answer_file}'"
225 fi
226}
227
Usta Shrestha572ecec2022-12-08 01:29:21 -0500228function test_bp2build_generates_all_buildfiles {
229 _save_trap=$(trap -p EXIT)
230 trap '[[ $? -ne 0 ]] && echo Are you running this locally? Try changing --sandbox_tmpfs_path to something other than /tmp/ in build/bazel/linux.bazelrc.' EXIT
231 _bp2build_generates_all_buildfiles
232 eval "${_save_trap}"
233}
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200234
Chris Parsons8152a942023-06-06 16:17:50 +0000235function test_build_files_take_precedence {
236 _save_trap=$(trap -p EXIT)
237 trap '[[ $? -ne 0 ]] && echo Are you running this locally? Try changing --sandbox_tmpfs_path to something other than /tmp/ in build/bazel/linux.bazelrc.' EXIT
238 _build_files_take_precedence
239 eval "${_save_trap}"
240}
241
242function _build_files_take_precedence {
243 setup
244
245 # This specific directory is hardcoded in bp2build as being one
246 # where the BUILD file should be intentionally kept.
247 mkdir -p testpkg/keep_build_file
248 cat > testpkg/keep_build_file/Android.bp <<'EOF'
249genrule {
250 name: "print_origin",
251 cmd: "echo 'from_soong' > $(out)",
252 out: [
253 "origin.txt",
254 ],
255 bazel_module: {
256 bp2build_available: true,
257 },
258 }
259EOF
260
261 run_soong bp2build
262 run_bazel build --config=android --config=bp2build --config=ci //testpkg/keep_build_file:print_origin
263
264 local -r output_file="$(find -L bazel-out -name origin.txt)"
265 if [[ ! -f "${output_file}" ]]; then
266 fail "Expected origin.txt to be generated, but was missing"
267 fi
268 if ! grep from_soong "${output_file}"; then
269 fail "Expected to find 'from_soong' in '${output_file}'"
270 fi
271
272 cat > testpkg/keep_build_file/BUILD.bazel <<'EOF'
273genrule(
274 name = "print_origin",
275 outs = ["origin.txt"],
276 cmd = "echo 'from_bazel' > $@",
277)
278EOF
279
280 # Clean the workspace. There is a test infrastructure bug where run_bazel
281 # will symlink Android.bp files in the source directory again and thus
282 # pollute the workspace.
283 # TODO: b/286059878 - Remove this clean after the underlying bug is fixed.
284 run_soong clean
285 run_soong bp2build
286 run_bazel build --config=android --config=bp2build --config=ci //testpkg/keep_build_file:print_origin
287 if ! grep from_bazel "${output_file}"; then
288 fail "Expected to find 'from_bazel' in '${output_file}'"
289 fi
290}
291
Usta (Tsering) Shresthac4c07b12022-11-08 18:31:14 -0500292function test_bp2build_symlinks_files {
293 setup
294 mkdir -p foo
295 touch foo/BLANK1
296 touch foo/BLANK2
297 touch foo/F2D
298 touch foo/BUILD
299
300 run_soong bp2build
301
302 if [[ -e "./out/soong/workspace/foo/BUILD" ]]; then
303 fail "./out/soong/workspace/foo/BUILD should be omitted"
304 fi
305 for file in BLANK1 BLANK2 F2D
306 do
307 if [[ ! -L "./out/soong/workspace/foo/$file" ]]; then
308 fail "./out/soong/workspace/foo/$file should exist"
309 fi
310 done
311 local -r BLANK1_BEFORE=$(stat -c %y "./out/soong/workspace/foo/BLANK1")
312
313 rm foo/BLANK2
314 rm foo/F2D
315 mkdir foo/F2D
316 touch foo/F2D/BUILD
317
318 run_soong bp2build
319
320 if [[ -e "./out/soong/workspace/foo/BUILD" ]]; then
321 fail "./out/soong/workspace/foo/BUILD should be omitted"
322 fi
323 local -r BLANK1_AFTER=$(stat -c %y "./out/soong/workspace/foo/BLANK1")
324 if [[ "$BLANK1_AFTER" != "$BLANK1_BEFORE" ]]; then
325 fail "./out/soong/workspace/foo/BLANK1 should be untouched"
326 fi
327 if [[ -e "./out/soong/workspace/foo/BLANK2" ]]; then
328 fail "./out/soong/workspace/foo/BLANK2 should be removed"
329 fi
330 if [[ -L "./out/soong/workspace/foo/F2D" ]] || [[ ! -d "./out/soong/workspace/foo/F2D" ]]; then
331 fail "./out/soong/workspace/foo/F2D should be a dir"
332 fi
333}
334
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200335function test_cc_correctness {
336 setup
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200337
338 mkdir -p a
339 cat > a/Android.bp <<EOF
340cc_object {
341 name: "qq",
342 srcs: ["qq.cc"],
343 bazel_module: {
344 bp2build_available: true,
345 },
346 stl: "none",
347 system_shared_libs: [],
348}
349EOF
350
351 cat > a/qq.cc <<EOF
352#include "qq.h"
353int qq() {
354 return QQ;
355}
356EOF
357
358 cat > a/qq.h <<EOF
359#define QQ 1
360EOF
361
362 run_soong bp2build
363
Cole Faustde12be32022-11-19 15:14:48 -0800364 run_bazel build --config=android --config=bp2build --config=ci //a:qq
Usta Shrestha2c9a5e32022-06-09 12:22:36 -0400365 local -r output_mtime1=$(stat -c "%y" bazel-bin/a/_objs/qq/qq.o)
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200366
Cole Faustde12be32022-11-19 15:14:48 -0800367 run_bazel build --config=android --config=bp2build --config=ci //a:qq
Usta Shrestha2c9a5e32022-06-09 12:22:36 -0400368 local -r output_mtime2=$(stat -c "%y" bazel-bin/a/_objs/qq/qq.o)
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200369
370 if [[ "$output_mtime1" != "$output_mtime2" ]]; then
371 fail "output changed on null build"
372 fi
373
374 cat > a/qq.h <<EOF
375#define QQ 2
376EOF
377
Cole Faustde12be32022-11-19 15:14:48 -0800378 run_bazel build --config=android --config=bp2build --config=ci //a:qq
Usta Shrestha2c9a5e32022-06-09 12:22:36 -0400379 local -r output_mtime3=$(stat -c "%y" bazel-bin/a/_objs/qq/qq.o)
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200380
381 if [[ "$output_mtime1" == "$output_mtime3" ]]; then
382 fail "output not changed when included header changed"
383 fi
384}
385
Jingwen Chend4b1dc82022-05-12 11:08:03 +0000386# Regression test for the following failure during symlink forest creation:
387#
388# Cannot stat '/tmp/st.rr054/foo/bar/unresolved_symlink': stat /tmp/st.rr054/foo/bar/unresolved_symlink: no such file or directory
389#
390function test_bp2build_null_build_with_unresolved_symlink_in_source() {
391 setup
392
393 mkdir -p foo/bar
394 ln -s /tmp/non-existent foo/bar/unresolved_symlink
395 cat > foo/bar/Android.bp <<'EOF'
396filegroup {
397 name: "fg",
398 srcs: ["unresolved_symlink/non-existent-file.txt"],
399 }
400EOF
401
402 run_soong bp2build
403
404 dest=$(readlink -f out/soong/workspace/foo/bar/unresolved_symlink)
405 if [[ "$dest" != "/tmp/non-existent" ]]; then
406 fail "expected to plant an unresolved symlink out/soong/workspace/foo/bar/unresolved_symlink that resolves to /tmp/non-existent"
407 fi
408}
409
Cole Faust319abae2023-06-06 15:12:49 -0700410function test_bazel_standalone_output_paths_contain_product_name {
411 setup
412 mkdir -p a
413 cat > a/Android.bp <<EOF
414cc_object {
415 name: "qq",
416 srcs: ["qq.cc"],
417 bazel_module: {
418 bp2build_available: true,
419 },
420 stl: "none",
421 system_shared_libs: [],
422}
423EOF
424
425 cat > a/qq.cc <<EOF
426#include "qq.h"
427int qq() {
428 return QQ;
429}
430EOF
431
432 cat > a/qq.h <<EOF
433#define QQ 1
434EOF
435
436 export TARGET_PRODUCT=aosp_arm; run_soong bp2build
437 local -r output=$(run_bazel cquery //a:qq --output=files --config=android --config=bp2build --config=ci)
438 if [[ ! $(echo ${output} | grep "bazel-out/aosp_arm") ]]; then
439 fail "Did not find the product name '${TARGET_PRODUCT}' in the output path. This can cause " \
440 "unnecessary rebuilds when toggling between products as bazel outputs for different products will " \
441 "clobber each other. Output paths are: \n${output}"
442 fi
443}
444
Usta Shrestha572ecec2022-12-08 01:29:21 -0500445scan_and_run_tests