blob: 71e6af009c0a41ff561cc50edb3a84218e9f8ab7 [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
Usta (Tsering) Shresthac4c07b12022-11-08 18:31:14 -0500235function test_bp2build_symlinks_files {
236 setup
237 mkdir -p foo
238 touch foo/BLANK1
239 touch foo/BLANK2
240 touch foo/F2D
241 touch foo/BUILD
242
243 run_soong bp2build
244
245 if [[ -e "./out/soong/workspace/foo/BUILD" ]]; then
246 fail "./out/soong/workspace/foo/BUILD should be omitted"
247 fi
248 for file in BLANK1 BLANK2 F2D
249 do
250 if [[ ! -L "./out/soong/workspace/foo/$file" ]]; then
251 fail "./out/soong/workspace/foo/$file should exist"
252 fi
253 done
254 local -r BLANK1_BEFORE=$(stat -c %y "./out/soong/workspace/foo/BLANK1")
255
256 rm foo/BLANK2
257 rm foo/F2D
258 mkdir foo/F2D
259 touch foo/F2D/BUILD
260
261 run_soong bp2build
262
263 if [[ -e "./out/soong/workspace/foo/BUILD" ]]; then
264 fail "./out/soong/workspace/foo/BUILD should be omitted"
265 fi
266 local -r BLANK1_AFTER=$(stat -c %y "./out/soong/workspace/foo/BLANK1")
267 if [[ "$BLANK1_AFTER" != "$BLANK1_BEFORE" ]]; then
268 fail "./out/soong/workspace/foo/BLANK1 should be untouched"
269 fi
270 if [[ -e "./out/soong/workspace/foo/BLANK2" ]]; then
271 fail "./out/soong/workspace/foo/BLANK2 should be removed"
272 fi
273 if [[ -L "./out/soong/workspace/foo/F2D" ]] || [[ ! -d "./out/soong/workspace/foo/F2D" ]]; then
274 fail "./out/soong/workspace/foo/F2D should be a dir"
275 fi
276}
277
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200278function test_cc_correctness {
279 setup
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200280
281 mkdir -p a
282 cat > a/Android.bp <<EOF
283cc_object {
284 name: "qq",
285 srcs: ["qq.cc"],
286 bazel_module: {
287 bp2build_available: true,
288 },
289 stl: "none",
290 system_shared_libs: [],
291}
292EOF
293
294 cat > a/qq.cc <<EOF
295#include "qq.h"
296int qq() {
297 return QQ;
298}
299EOF
300
301 cat > a/qq.h <<EOF
302#define QQ 1
303EOF
304
305 run_soong bp2build
306
Cole Faustde12be32022-11-19 15:14:48 -0800307 run_bazel build --config=android --config=bp2build --config=ci //a:qq
Usta Shrestha2c9a5e32022-06-09 12:22:36 -0400308 local -r output_mtime1=$(stat -c "%y" bazel-bin/a/_objs/qq/qq.o)
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200309
Cole Faustde12be32022-11-19 15:14:48 -0800310 run_bazel build --config=android --config=bp2build --config=ci //a:qq
Usta Shrestha2c9a5e32022-06-09 12:22:36 -0400311 local -r output_mtime2=$(stat -c "%y" bazel-bin/a/_objs/qq/qq.o)
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200312
313 if [[ "$output_mtime1" != "$output_mtime2" ]]; then
314 fail "output changed on null build"
315 fi
316
317 cat > a/qq.h <<EOF
318#define QQ 2
319EOF
320
Cole Faustde12be32022-11-19 15:14:48 -0800321 run_bazel build --config=android --config=bp2build --config=ci //a:qq
Usta Shrestha2c9a5e32022-06-09 12:22:36 -0400322 local -r output_mtime3=$(stat -c "%y" bazel-bin/a/_objs/qq/qq.o)
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200323
324 if [[ "$output_mtime1" == "$output_mtime3" ]]; then
325 fail "output not changed when included header changed"
326 fi
327}
328
Jingwen Chend4b1dc82022-05-12 11:08:03 +0000329# Regression test for the following failure during symlink forest creation:
330#
331# Cannot stat '/tmp/st.rr054/foo/bar/unresolved_symlink': stat /tmp/st.rr054/foo/bar/unresolved_symlink: no such file or directory
332#
333function test_bp2build_null_build_with_unresolved_symlink_in_source() {
334 setup
335
336 mkdir -p foo/bar
337 ln -s /tmp/non-existent foo/bar/unresolved_symlink
338 cat > foo/bar/Android.bp <<'EOF'
339filegroup {
340 name: "fg",
341 srcs: ["unresolved_symlink/non-existent-file.txt"],
342 }
343EOF
344
345 run_soong bp2build
346
347 dest=$(readlink -f out/soong/workspace/foo/bar/unresolved_symlink)
348 if [[ "$dest" != "/tmp/non-existent" ]]; then
349 fail "expected to plant an unresolved symlink out/soong/workspace/foo/bar/unresolved_symlink that resolves to /tmp/non-existent"
350 fi
351}
352
Spandan Das255648c2023-01-11 03:05:24 +0000353# Smoke test to verify api_bp2build worksapce does not contain any errors
354function test_api_bp2build_empty_build() {
355 setup
356 run_soong api_bp2build
357 run_bazel build --config=android --config=api_bp2build //:empty
358}
359
Spandan Das68bcbb52023-03-10 02:32:18 +0000360# Verify that an *_api_contribution target can refer to an api file from
361# another Bazel package.
362function test_api_export_from_another_bazel_package() {
363 setup
364 # Parent dir Android.bp
365 mkdir -p foo
366 cat > foo/Android.bp << 'EOF'
367cc_library {
368 name: "libfoo",
369 stubs: {
370 symbol_file: "api/libfoo.map.txt",
371 },
372}
373EOF
374 # Child dir Android.bp
375 mkdir -p foo/api
376 cat > foo/api/Android.bp << 'EOF'
377package{}
378EOF
379 touch foo/api/libfoo.map.txt
380 # Run test
381 run_soong api_bp2build
382 run_bazel build --config=android --config=api_bp2build //foo:libfoo.contribution
383}
384
Usta Shrestha572ecec2022-12-08 01:29:21 -0500385scan_and_run_tests