blob: 5fc05f8b10a82325667e78652bb9b6a698512824 [file] [log] [blame]
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +01001#!/bin/bash -eu
2
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -04003set -o pipefail
4
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +01005# This test exercises the bootstrapping process of the build system
6# in a source tree that only contains enough files for Bazel and Soong to work.
7
Lukacs T. Berki3b730c42021-04-08 13:21:13 +02008source "$(dirname "$0")/lib.sh"
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +01009
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -040010readonly GENERATED_BUILD_FILE_NAME="BUILD.bazel"
11
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +010012function test_smoke {
13 setup
14 run_soong
15}
16
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +010017function test_null_build() {
18 setup
19 run_soong
Usta Shrestha4e17a2e2022-12-08 01:18:44 -050020 local -r bootstrap_mtime1=$(stat -c "%y" out/soong/bootstrap.ninja)
21 local -r output_mtime1=$(stat -c "%y" out/soong/build.ninja)
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +010022 run_soong
Usta Shrestha4e17a2e2022-12-08 01:18:44 -050023 local -r bootstrap_mtime2=$(stat -c "%y" out/soong/bootstrap.ninja)
24 local -r output_mtime2=$(stat -c "%y" out/soong/build.ninja)
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +010025
26 if [[ "$bootstrap_mtime1" == "$bootstrap_mtime2" ]]; then
27 # Bootstrapping is always done. It doesn't take a measurable amount of time.
28 fail "Bootstrap Ninja file did not change on null build"
29 fi
30
31 if [[ "$output_mtime1" != "$output_mtime2" ]]; then
32 fail "Output Ninja file changed on null build"
33 fi
34}
35
36function test_soong_build_rebuilt_if_blueprint_changes() {
37 setup
38 run_soong
Usta Shrestha4e17a2e2022-12-08 01:18:44 -050039 local -r mtime1=$(stat -c "%y" out/soong/bootstrap.ninja)
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +010040
41 sed -i 's/pluginGenSrcCmd/pluginGenSrcCmd2/g' build/blueprint/bootstrap/bootstrap.go
42
43 run_soong
Usta Shrestha4e17a2e2022-12-08 01:18:44 -050044 local -r mtime2=$(stat -c "%y" out/soong/bootstrap.ninja)
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +010045
46 if [[ "$mtime1" == "$mtime2" ]]; then
47 fail "Bootstrap Ninja file did not change"
48 fi
49}
50
51function test_change_android_bp() {
52 setup
53 mkdir -p a
54 cat > a/Android.bp <<'EOF'
55python_binary_host {
56 name: "my_little_binary_host",
57 srcs: ["my_little_binary_host.py"]
58}
59EOF
60 touch a/my_little_binary_host.py
61 run_soong
62
63 grep -q "^# Module:.*my_little_binary_host" out/soong/build.ninja || fail "module not found"
64
65 cat > a/Android.bp <<'EOF'
66python_binary_host {
67 name: "my_great_binary_host",
68 srcs: ["my_great_binary_host.py"]
69}
70EOF
71 touch a/my_great_binary_host.py
72 run_soong
73
74 grep -q "^# Module:.*my_little_binary_host" out/soong/build.ninja && fail "old module found"
75 grep -q "^# Module:.*my_great_binary_host" out/soong/build.ninja || fail "new module not found"
76}
77
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +010078function test_add_android_bp() {
79 setup
80 run_soong
Usta Shrestha4e17a2e2022-12-08 01:18:44 -050081 local -r mtime1=$(stat -c "%y" out/soong/build.ninja)
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +010082
83 mkdir -p a
84 cat > a/Android.bp <<'EOF'
85python_binary_host {
86 name: "my_little_binary_host",
87 srcs: ["my_little_binary_host.py"]
88}
89EOF
90 touch a/my_little_binary_host.py
91 run_soong
92
Usta Shrestha4e17a2e2022-12-08 01:18:44 -050093 local -r mtime2=$(stat -c "%y" out/soong/build.ninja)
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +010094 if [[ "$mtime1" == "$mtime2" ]]; then
95 fail "Output Ninja file did not change"
96 fi
97
98 grep -q "^# Module:.*my_little_binary_host$" out/soong/build.ninja || fail "New module not in output"
99
100 run_soong
101}
102
103function test_delete_android_bp() {
104 setup
105 mkdir -p a
106 cat > a/Android.bp <<'EOF'
107python_binary_host {
108 name: "my_little_binary_host",
109 srcs: ["my_little_binary_host.py"]
110}
111EOF
112 touch a/my_little_binary_host.py
113 run_soong
114
115 grep -q "^# Module:.*my_little_binary_host$" out/soong/build.ninja || fail "Module not in output"
116
117 rm a/Android.bp
118 run_soong
119
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200120 if grep -q "^# Module:.*my_little_binary_host$" out/soong/build.ninja; then
121 fail "Old module in output"
122 fi
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100123}
124
Colin Cross10425952021-04-12 18:59:18 -0700125# Test that an incremental build with a glob doesn't rerun soong_build, and
126# only regenerates the globs on the first but not the second incremental build.
127function test_glob_noop_incremental() {
128 setup
129
Colin Crossb72877f2021-04-16 10:09:54 -0700130 # This test needs to start from a clean build, but setup creates an
131 # initialized tree that has already been built once. Clear the out
Lukacs T. Berki731bb912021-04-16 09:16:19 +0200132 # directory to start from scratch (see b/185591972)
Colin Crossb72877f2021-04-16 10:09:54 -0700133 rm -rf out
134
Colin Cross10425952021-04-12 18:59:18 -0700135 mkdir -p a
136 cat > a/Android.bp <<'EOF'
137python_binary_host {
138 name: "my_little_binary_host",
139 srcs: ["*.py"],
140}
141EOF
142 touch a/my_little_binary_host.py
143 run_soong
Usta Shrestha4e17a2e2022-12-08 01:18:44 -0500144 local -r ninja_mtime1=$(stat -c "%y" out/soong/build.ninja)
Colin Cross10425952021-04-12 18:59:18 -0700145
Lukacs T. Berkia806e412021-09-01 08:57:48 +0200146 local glob_deps_file=out/soong/globs/build/0.d
Colin Cross10425952021-04-12 18:59:18 -0700147
148 if [ -e "$glob_deps_file" ]; then
149 fail "Glob deps file unexpectedly written on first build"
150 fi
151
152 run_soong
Usta Shrestha4e17a2e2022-12-08 01:18:44 -0500153 local -r ninja_mtime2=$(stat -c "%y" out/soong/build.ninja)
Colin Cross10425952021-04-12 18:59:18 -0700154
155 # There is an ineffiencency in glob that requires bpglob to rerun once for each glob to update
156 # the entry in the .ninja_log. It doesn't update the output file, but we can detect the rerun
157 # by checking if the deps file was created.
158 if [ ! -e "$glob_deps_file" ]; then
159 fail "Glob deps file missing after second build"
160 fi
161
Usta Shrestha4e17a2e2022-12-08 01:18:44 -0500162 local -r glob_deps_mtime2=$(stat -c "%y" "$glob_deps_file")
Colin Cross10425952021-04-12 18:59:18 -0700163
164 if [[ "$ninja_mtime1" != "$ninja_mtime2" ]]; then
165 fail "Ninja file rewritten on null incremental build"
166 fi
167
168 run_soong
Usta Shrestha4e17a2e2022-12-08 01:18:44 -0500169 local -r ninja_mtime3=$(stat -c "%y" out/soong/build.ninja)
170 local -r glob_deps_mtime3=$(stat -c "%y" "$glob_deps_file")
Colin Cross10425952021-04-12 18:59:18 -0700171
172 if [[ "$ninja_mtime2" != "$ninja_mtime3" ]]; then
173 fail "Ninja file rewritten on null incremental build"
174 fi
175
176 # The bpglob commands should not rerun after the first incremental build.
177 if [[ "$glob_deps_mtime2" != "$glob_deps_mtime3" ]]; then
178 fail "Glob deps file rewritten on second null incremental build"
179 fi
180}
181
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100182function test_add_file_to_glob() {
183 setup
184
185 mkdir -p a
186 cat > a/Android.bp <<'EOF'
187python_binary_host {
188 name: "my_little_binary_host",
189 srcs: ["*.py"],
190}
191EOF
192 touch a/my_little_binary_host.py
193 run_soong
Usta Shrestha4e17a2e2022-12-08 01:18:44 -0500194 local -r mtime1=$(stat -c "%y" out/soong/build.ninja)
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100195
196 touch a/my_little_library.py
197 run_soong
198
Usta Shrestha4e17a2e2022-12-08 01:18:44 -0500199 local -r mtime2=$(stat -c "%y" out/soong/build.ninja)
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100200 if [[ "$mtime1" == "$mtime2" ]]; then
201 fail "Output Ninja file did not change"
202 fi
203
204 grep -q my_little_library.py out/soong/build.ninja || fail "new file is not in output"
205}
206
Lukacs T. Berkif0b3b942021-03-23 11:46:47 +0100207function test_soong_build_rerun_iff_environment_changes() {
208 setup
209
Liz Kammer33cc80e2023-05-18 18:20:28 +0000210 mkdir -p build/soong/cherry
211 cat > build/soong/cherry/Android.bp <<'EOF'
Lukacs T. Berkif0b3b942021-03-23 11:46:47 +0100212bootstrap_go_package {
213 name: "cherry",
214 pkgPath: "android/soong/cherry",
215 deps: [
216 "blueprint",
217 "soong",
218 "soong-android",
219 ],
220 srcs: [
221 "cherry.go",
222 ],
223 pluginFor: ["soong_build"],
224}
225EOF
226
Liz Kammer33cc80e2023-05-18 18:20:28 +0000227 cat > build/soong/cherry/cherry.go <<'EOF'
Lukacs T. Berkif0b3b942021-03-23 11:46:47 +0100228package cherry
229
230import (
231 "android/soong/android"
232 "github.com/google/blueprint"
233)
234
235var (
236 pctx = android.NewPackageContext("cherry")
237)
238
239func init() {
240 android.RegisterSingletonType("cherry", CherrySingleton)
241}
242
243func CherrySingleton() android.Singleton {
244 return &cherrySingleton{}
245}
246
247type cherrySingleton struct{}
248
249func (p *cherrySingleton) GenerateBuildActions(ctx android.SingletonContext) {
250 cherryRule := ctx.Rule(pctx, "cherry",
251 blueprint.RuleParams{
252 Command: "echo CHERRY IS " + ctx.Config().Getenv("CHERRY") + " > ${out}",
253 CommandDeps: []string{},
254 Description: "Cherry",
255 })
256
257 outputFile := android.PathForOutput(ctx, "cherry", "cherry.txt")
258 var deps android.Paths
259
260 ctx.Build(pctx, android.BuildParams{
261 Rule: cherryRule,
262 Output: outputFile,
263 Inputs: deps,
264 })
265}
266EOF
267
268 export CHERRY=TASTY
269 run_soong
270 grep -q "CHERRY IS TASTY" out/soong/build.ninja \
271 || fail "first value of environment variable is not used"
272
273 export CHERRY=RED
274 run_soong
275 grep -q "CHERRY IS RED" out/soong/build.ninja \
276 || fail "second value of environment variable not used"
Usta Shrestha4e17a2e2022-12-08 01:18:44 -0500277 local -r mtime1=$(stat -c "%y" out/soong/build.ninja)
Lukacs T. Berkif0b3b942021-03-23 11:46:47 +0100278
279 run_soong
Usta Shrestha4e17a2e2022-12-08 01:18:44 -0500280 local -r mtime2=$(stat -c "%y" out/soong/build.ninja)
Lukacs T. Berkif0b3b942021-03-23 11:46:47 +0100281 if [[ "$mtime1" != "$mtime2" ]]; then
282 fail "Output Ninja file changed when environment variable did not"
283 fi
284
285}
286
Colin Cross662d6142022-11-03 20:38:01 -0700287function test_create_global_include_directory() {
288 setup
289 run_soong
Usta Shrestha4e17a2e2022-12-08 01:18:44 -0500290 local -r mtime1=$(stat -c "%y" out/soong/build.ninja)
Colin Cross662d6142022-11-03 20:38:01 -0700291
292 # Soong needs to know if top level directories like hardware/ exist for use
293 # as global include directories. Make sure that doesn't cause regens for
294 # unrelated changes to the top level directory.
295 mkdir -p system/core
296
297 run_soong
Usta Shrestha4e17a2e2022-12-08 01:18:44 -0500298 local -r mtime2=$(stat -c "%y" out/soong/build.ninja)
Colin Cross662d6142022-11-03 20:38:01 -0700299 if [[ "$mtime1" != "$mtime2" ]]; then
300 fail "Output Ninja file changed when top level directory changed"
301 fi
302
303 # Make sure it does regen if a missing directory in the path of a global
304 # include directory is added.
305 mkdir -p system/core/include
306
307 run_soong
Usta Shrestha4e17a2e2022-12-08 01:18:44 -0500308 local -r mtime3=$(stat -c "%y" out/soong/build.ninja)
Colin Cross662d6142022-11-03 20:38:01 -0700309 if [[ "$mtime2" = "$mtime3" ]]; then
310 fail "Output Ninja file did not change when global include directory created"
311 fi
312
313}
314
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100315function test_add_file_to_soong_build() {
316 setup
317 run_soong
Usta Shrestha4e17a2e2022-12-08 01:18:44 -0500318 local -r mtime1=$(stat -c "%y" out/soong/build.ninja)
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100319
Liz Kammer33cc80e2023-05-18 18:20:28 +0000320 mkdir -p vendor/foo/picard
321 cat > vendor/foo/picard/Android.bp <<'EOF'
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100322bootstrap_go_package {
323 name: "picard-soong-rules",
324 pkgPath: "android/soong/picard",
325 deps: [
326 "blueprint",
327 "soong",
328 "soong-android",
329 ],
330 srcs: [
331 "picard.go",
332 ],
333 pluginFor: ["soong_build"],
334}
335EOF
336
Liz Kammer33cc80e2023-05-18 18:20:28 +0000337 cat > vendor/foo/picard/picard.go <<'EOF'
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100338package picard
339
340import (
341 "android/soong/android"
342 "github.com/google/blueprint"
343)
344
345var (
346 pctx = android.NewPackageContext("picard")
347)
348
349func init() {
350 android.RegisterSingletonType("picard", PicardSingleton)
351}
352
353func PicardSingleton() android.Singleton {
354 return &picardSingleton{}
355}
356
357type picardSingleton struct{}
358
359func (p *picardSingleton) GenerateBuildActions(ctx android.SingletonContext) {
360 picardRule := ctx.Rule(pctx, "picard",
361 blueprint.RuleParams{
362 Command: "echo Make it so. > ${out}",
363 CommandDeps: []string{},
364 Description: "Something quotable",
365 })
366
367 outputFile := android.PathForOutput(ctx, "picard", "picard.txt")
368 var deps android.Paths
369
370 ctx.Build(pctx, android.BuildParams{
371 Rule: picardRule,
372 Output: outputFile,
373 Inputs: deps,
374 })
375}
376
377EOF
378
379 run_soong
Usta Shrestha4e17a2e2022-12-08 01:18:44 -0500380 local -r mtime2=$(stat -c "%y" out/soong/build.ninja)
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100381 if [[ "$mtime1" == "$mtime2" ]]; then
382 fail "Output Ninja file did not change"
383 fi
384
385 grep -q "Make it so" out/soong/build.ninja || fail "New action not present"
386}
387
Colin Crossc02504e2021-04-08 10:34:16 -0700388# Tests a glob in a build= statement in an Android.bp file, which is interpreted
389# during bootstrapping.
390function test_glob_during_bootstrapping() {
391 setup
392
Liz Kammer33cc80e2023-05-18 18:20:28 +0000393 mkdir -p build/soong/picard
394 cat > build/soong/picard/Android.bp <<'EOF'
Colin Crossc02504e2021-04-08 10:34:16 -0700395build=["foo*.bp"]
396EOF
Liz Kammer33cc80e2023-05-18 18:20:28 +0000397 cat > build/soong/picard/fooa.bp <<'EOF'
Colin Crossc02504e2021-04-08 10:34:16 -0700398bootstrap_go_package {
399 name: "picard-soong-rules",
400 pkgPath: "android/soong/picard",
401 deps: [
402 "blueprint",
403 "soong",
404 "soong-android",
405 ],
406 srcs: [
407 "picard.go",
408 ],
409 pluginFor: ["soong_build"],
410}
411EOF
412
Liz Kammer33cc80e2023-05-18 18:20:28 +0000413 cat > build/soong/picard/picard.go <<'EOF'
Colin Crossc02504e2021-04-08 10:34:16 -0700414package picard
415
416import (
417 "android/soong/android"
418 "github.com/google/blueprint"
419)
420
421var (
422 pctx = android.NewPackageContext("picard")
423)
424
425func init() {
426 android.RegisterSingletonType("picard", PicardSingleton)
427}
428
429func PicardSingleton() android.Singleton {
430 return &picardSingleton{}
431}
432
433type picardSingleton struct{}
434
435var Message = "Make it so."
436
437func (p *picardSingleton) GenerateBuildActions(ctx android.SingletonContext) {
438 picardRule := ctx.Rule(pctx, "picard",
439 blueprint.RuleParams{
440 Command: "echo " + Message + " > ${out}",
441 CommandDeps: []string{},
442 Description: "Something quotable",
443 })
444
445 outputFile := android.PathForOutput(ctx, "picard", "picard.txt")
446 var deps android.Paths
447
448 ctx.Build(pctx, android.BuildParams{
449 Rule: picardRule,
450 Output: outputFile,
451 Inputs: deps,
452 })
453}
454
455EOF
456
457 run_soong
Usta Shrestha4e17a2e2022-12-08 01:18:44 -0500458 local -r mtime1=$(stat -c "%y" out/soong/build.ninja)
Colin Crossc02504e2021-04-08 10:34:16 -0700459
460 grep -q "Make it so" out/soong/build.ninja || fail "Original action not present"
461
Liz Kammer33cc80e2023-05-18 18:20:28 +0000462 cat > build/soong/picard/foob.bp <<'EOF'
Colin Crossc02504e2021-04-08 10:34:16 -0700463bootstrap_go_package {
464 name: "worf-soong-rules",
465 pkgPath: "android/soong/worf",
466 deps: [
467 "blueprint",
468 "soong",
469 "soong-android",
470 "picard-soong-rules",
471 ],
472 srcs: [
473 "worf.go",
474 ],
475 pluginFor: ["soong_build"],
476}
477EOF
478
Liz Kammer33cc80e2023-05-18 18:20:28 +0000479 cat > build/soong/picard/worf.go <<'EOF'
Colin Crossc02504e2021-04-08 10:34:16 -0700480package worf
481
482import "android/soong/picard"
483
484func init() {
485 picard.Message = "Engage."
486}
487EOF
488
489 run_soong
Usta Shrestha4e17a2e2022-12-08 01:18:44 -0500490 local -r mtime2=$(stat -c "%y" out/soong/build.ninja)
Colin Crossc02504e2021-04-08 10:34:16 -0700491 if [[ "$mtime1" == "$mtime2" ]]; then
492 fail "Output Ninja file did not change"
493 fi
494
495 grep -q "Engage" out/soong/build.ninja || fail "New action not present"
496
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200497 if grep -q "Make it so" out/soong/build.ninja; then
498 fail "Original action still present"
499 fi
Colin Crossc02504e2021-04-08 10:34:16 -0700500}
501
Lukacs T. Berkic6012f32021-09-06 18:31:46 +0200502function test_soong_docs_smoke() {
Lukacs T. Berkif0b3b942021-03-23 11:46:47 +0100503 setup
Lukacs T. Berkif0b3b942021-03-23 11:46:47 +0100504
Lukacs T. Berkic6012f32021-09-06 18:31:46 +0200505 run_soong soong_docs
506
507 [[ -e "out/soong/docs/soong_build.html" ]] || fail "Documentation for main page not created"
508 [[ -e "out/soong/docs/cc.html" ]] || fail "Documentation for C++ modules not created"
509}
510
511function test_null_build_after_soong_docs() {
512 setup
Lukacs T. Berki1a86bd22021-08-19 16:24:30 +0200513
Lukacs T. Berkif0b3b942021-03-23 11:46:47 +0100514 run_soong
Usta Shrestha4e17a2e2022-12-08 01:18:44 -0500515 local -r ninja_mtime1=$(stat -c "%y" out/soong/build.ninja)
Lukacs T. Berkif0b3b942021-03-23 11:46:47 +0100516
Lukacs T. Berkic6012f32021-09-06 18:31:46 +0200517 run_soong soong_docs
Usta Shrestha4e17a2e2022-12-08 01:18:44 -0500518 local -r docs_mtime1=$(stat -c "%y" out/soong/docs/soong_build.html)
Lukacs T. Berkic6012f32021-09-06 18:31:46 +0200519
520 run_soong soong_docs
Usta Shrestha4e17a2e2022-12-08 01:18:44 -0500521 local -r docs_mtime2=$(stat -c "%y" out/soong/docs/soong_build.html)
Lukacs T. Berkic6012f32021-09-06 18:31:46 +0200522
523 if [[ "$docs_mtime1" != "$docs_mtime2" ]]; then
524 fail "Output Ninja file changed on null build"
525 fi
526
527 run_soong
Usta Shrestha4e17a2e2022-12-08 01:18:44 -0500528 local -r ninja_mtime2=$(stat -c "%y" out/soong/build.ninja)
Lukacs T. Berkic6012f32021-09-06 18:31:46 +0200529
530 if [[ "$ninja_mtime1" != "$ninja_mtime2" ]]; then
Lukacs T. Berkif0b3b942021-03-23 11:46:47 +0100531 fail "Output Ninja file changed on null build"
532 fi
533}
534
Spandan Das05063612021-06-25 01:39:04 +0000535function test_write_to_source_tree {
536 setup
537 mkdir -p a
538 cat > a/Android.bp <<EOF
539genrule {
540 name: "write_to_source_tree",
541 out: ["write_to_source_tree"],
542 cmd: "touch file_in_source_tree && touch \$(out)",
543}
544EOF
545 readonly EXPECTED_OUT=out/soong/.intermediates/a/write_to_source_tree/gen/write_to_source_tree
546 readonly ERROR_LOG=${MOCK_TOP}/out/error.log
547 readonly ERROR_MSG="Read-only file system"
548 readonly ERROR_HINT_PATTERN="BUILD_BROKEN_SRC_DIR"
549 # Test in ReadOnly source tree
550 run_ninja BUILD_BROKEN_SRC_DIR_IS_WRITABLE=false ${EXPECTED_OUT} &> /dev/null && \
551 fail "Write to source tree should not work in a ReadOnly source tree"
552
Usta Shrestha4e17a2e2022-12-08 01:18:44 -0500553 if grep -q "${ERROR_MSG}" "${ERROR_LOG}" && grep -q "${ERROR_HINT_PATTERN}" "${ERROR_LOG}" ; then
Spandan Das05063612021-06-25 01:39:04 +0000554 echo Error message and error hint found in logs >/dev/null
555 else
556 fail "Did not find Read-only error AND error hint in error.log"
557 fi
558
559 # Test in ReadWrite source tree
560 run_ninja BUILD_BROKEN_SRC_DIR_IS_WRITABLE=true ${EXPECTED_OUT} &> /dev/null || \
561 fail "Write to source tree did not succeed in a ReadWrite source tree"
562
Usta Shrestha4e17a2e2022-12-08 01:18:44 -0500563 if grep -q "${ERROR_MSG}\|${ERROR_HINT_PATTERN}" "${ERROR_LOG}" ; then
Spandan Das05063612021-06-25 01:39:04 +0000564 fail "Found read-only error OR error hint in error.log"
565 fi
566}
567
Chris Parsonsec1a3dc2021-04-20 15:32:07 -0400568function test_bp2build_smoke {
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200569 setup
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200570 run_soong bp2build
Lukacs T. Berki90b43342021-11-02 14:42:04 +0100571 [[ -e out/soong/bp2build_workspace_marker ]] || fail "bp2build marker file not created"
Lukacs T. Berkib353cca2021-04-16 13:47:36 +0200572 [[ -e out/soong/workspace ]] || fail "Bazel workspace not created"
Lukacs T. Berkid518e1a2021-04-14 13:49:50 +0200573}
574
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200575function test_bp2build_generates_marker_file {
Jingwen Chen4fabaf52021-05-25 02:40:29 +0000576 setup
Jingwen Chen4fabaf52021-05-25 02:40:29 +0000577
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200578 run_soong bp2build
Jingwen Chen4fabaf52021-05-25 02:40:29 +0000579
Lukacs T. Berkic541cd22022-10-26 07:26:50 +0000580 if [[ ! -f "./out/soong/bp2build_files_marker" ]]; then
581 fail "bp2build marker file was not generated"
582 fi
583
Lukacs T. Berki90b43342021-11-02 14:42:04 +0100584 if [[ ! -f "./out/soong/bp2build_workspace_marker" ]]; then
Lukacs T. Berkic541cd22022-10-26 07:26:50 +0000585 fail "symlink forest marker file was not generated"
586 fi
587}
588
589function test_bp2build_add_irrelevant_file {
590 setup
591
592 mkdir -p a/b
593 touch a/b/c.txt
594 cat > a/b/Android.bp <<'EOF'
595filegroup {
596 name: "c",
597 srcs: ["c.txt"],
598 bazel_module: { bp2build_available: true },
599}
600EOF
601
602 run_soong bp2build
603 if [[ ! -e out/soong/bp2build/a/b/BUILD.bazel ]]; then
604 fail "BUILD file in symlink forest was not created";
605 fi
606
Usta Shrestha4e17a2e2022-12-08 01:18:44 -0500607 local -r mtime1=$(stat -c "%y" out/soong/bp2build/a/b/BUILD.bazel)
Lukacs T. Berkic541cd22022-10-26 07:26:50 +0000608
609 touch a/irrelevant.txt
610 run_soong bp2build
Usta Shrestha4e17a2e2022-12-08 01:18:44 -0500611 local -r mtime2=$(stat -c "%y" out/soong/bp2build/a/b/BUILD.bazel)
Lukacs T. Berkic541cd22022-10-26 07:26:50 +0000612
613 if [[ "$mtime1" != "$mtime2" ]]; then
614 fail "BUILD.bazel file was regenerated"
615 fi
616
617 if [[ ! -e "out/soong/workspace/a/irrelevant.txt" ]]; then
618 fail "New file was not symlinked into symlink forest"
Jingwen Chen4fabaf52021-05-25 02:40:29 +0000619 fi
620}
621
Chris Parsonsec1a3dc2021-04-20 15:32:07 -0400622function test_bp2build_add_android_bp {
Lukacs T. Berkid518e1a2021-04-14 13:49:50 +0200623 setup
624
625 mkdir -p a
626 touch a/a.txt
627 cat > a/Android.bp <<'EOF'
628filegroup {
629 name: "a",
630 srcs: ["a.txt"],
631 bazel_module: { bp2build_available: true },
632}
633EOF
634
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200635 run_soong bp2build
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -0400636 [[ -e out/soong/bp2build/a/${GENERATED_BUILD_FILE_NAME} ]] || fail "a/${GENERATED_BUILD_FILE_NAME} not created"
637 [[ -L out/soong/workspace/a/${GENERATED_BUILD_FILE_NAME} ]] || fail "a/${GENERATED_BUILD_FILE_NAME} not symlinked"
Lukacs T. Berkid518e1a2021-04-14 13:49:50 +0200638
639 mkdir -p b
640 touch b/b.txt
641 cat > b/Android.bp <<'EOF'
642filegroup {
643 name: "b",
644 srcs: ["b.txt"],
645 bazel_module: { bp2build_available: true },
646}
647EOF
648
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200649 run_soong bp2build
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -0400650 [[ -e out/soong/bp2build/b/${GENERATED_BUILD_FILE_NAME} ]] || fail "a/${GENERATED_BUILD_FILE_NAME} not created"
651 [[ -L out/soong/workspace/b/${GENERATED_BUILD_FILE_NAME} ]] || fail "a/${GENERATED_BUILD_FILE_NAME} not symlinked"
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200652}
653
Chris Parsonsec1a3dc2021-04-20 15:32:07 -0400654function test_bp2build_null_build {
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200655 setup
Lukacs T. Berkid518e1a2021-04-14 13:49:50 +0200656
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200657 run_soong bp2build
Usta Shrestha4e17a2e2022-12-08 01:18:44 -0500658 local -r mtime1=$(stat -c "%y" out/soong/bp2build_workspace_marker)
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200659
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200660 run_soong bp2build
Usta Shrestha4e17a2e2022-12-08 01:18:44 -0500661 local -r mtime2=$(stat -c "%y" out/soong/bp2build_workspace_marker)
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200662
663 if [[ "$mtime1" != "$mtime2" ]]; then
664 fail "Output Ninja file changed on null build"
665 fi
666}
667
Chris Parsonsec1a3dc2021-04-20 15:32:07 -0400668function test_bp2build_add_to_glob {
Lukacs T. Berkid518e1a2021-04-14 13:49:50 +0200669 setup
670
671 mkdir -p a
672 touch a/a1.txt
673 cat > a/Android.bp <<'EOF'
674filegroup {
675 name: "a",
676 srcs: ["*.txt"],
677 bazel_module: { bp2build_available: true },
678}
679EOF
680
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200681 run_soong bp2build
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -0400682 grep -q a1.txt "out/soong/bp2build/a/${GENERATED_BUILD_FILE_NAME}" || fail "a1.txt not in ${GENERATED_BUILD_FILE_NAME} file"
Lukacs T. Berkid518e1a2021-04-14 13:49:50 +0200683
684 touch a/a2.txt
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200685 run_soong bp2build
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -0400686 grep -q a2.txt "out/soong/bp2build/a/${GENERATED_BUILD_FILE_NAME}" || fail "a2.txt not in ${GENERATED_BUILD_FILE_NAME} file"
Lukacs T. Berkid518e1a2021-04-14 13:49:50 +0200687}
688
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200689function test_multiple_soong_build_modes() {
690 setup
691 run_soong json-module-graph bp2build nothing
Lukacs T. Berki90b43342021-11-02 14:42:04 +0100692 if [[ ! -f "out/soong/bp2build_workspace_marker" ]]; then
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200693 fail "bp2build marker file was not generated"
694 fi
695
696
697 if [[ ! -f "out/soong/module-graph.json" ]]; then
698 fail "JSON file was not created"
699 fi
700
701 if [[ ! -f "out/soong/build.ninja" ]]; then
702 fail "Main build.ninja file was not created"
703 fi
704}
705
Lukacs T. Berki97bb9f12021-04-01 18:28:45 +0200706function test_dump_json_module_graph() {
707 setup
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200708 run_soong json-module-graph
709 if [[ ! -r "out/soong/module-graph.json" ]]; then
Lukacs T. Berki97bb9f12021-04-01 18:28:45 +0200710 fail "JSON file was not created"
711 fi
712}
713
Lukacs T. Berkie571dc32021-08-25 14:14:13 +0200714function test_json_module_graph_back_and_forth_null_build() {
715 setup
716
717 run_soong
Usta Shrestha4e17a2e2022-12-08 01:18:44 -0500718 local -r ninja_mtime1=$(stat -c "%y" out/soong/build.ninja)
Lukacs T. Berkie571dc32021-08-25 14:14:13 +0200719
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200720 run_soong json-module-graph
Usta Shrestha4e17a2e2022-12-08 01:18:44 -0500721 local -r json_mtime1=$(stat -c "%y" out/soong/module-graph.json)
Lukacs T. Berkie571dc32021-08-25 14:14:13 +0200722
723 run_soong
Usta Shrestha4e17a2e2022-12-08 01:18:44 -0500724 local -r ninja_mtime2=$(stat -c "%y" out/soong/build.ninja)
Lukacs T. Berkie571dc32021-08-25 14:14:13 +0200725 if [[ "$ninja_mtime1" != "$ninja_mtime2" ]]; then
726 fail "Output Ninja file changed after writing JSON module graph"
727 fi
728
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200729 run_soong json-module-graph
Usta Shrestha4e17a2e2022-12-08 01:18:44 -0500730 local -r json_mtime2=$(stat -c "%y" out/soong/module-graph.json)
Lukacs T. Berkie571dc32021-08-25 14:14:13 +0200731 if [[ "$json_mtime1" != "$json_mtime2" ]]; then
732 fail "JSON module graph file changed after writing Ninja file"
733 fi
734
735}
736
Chris Parsonsec1a3dc2021-04-20 15:32:07 -0400737function test_bp2build_bazel_workspace_structure {
Lukacs T. Berkib353cca2021-04-16 13:47:36 +0200738 setup
739
740 mkdir -p a/b
741 touch a/a.txt
742 touch a/b/b.txt
743 cat > a/b/Android.bp <<'EOF'
744filegroup {
745 name: "b",
746 srcs: ["b.txt"],
747 bazel_module: { bp2build_available: true },
748}
749EOF
750
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200751 run_soong bp2build
Lukacs T. Berkib353cca2021-04-16 13:47:36 +0200752 [[ -e out/soong/workspace ]] || fail "Bazel workspace not created"
753 [[ -d out/soong/workspace/a/b ]] || fail "module directory not a directory"
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -0400754 [[ -L "out/soong/workspace/a/b/${GENERATED_BUILD_FILE_NAME}" ]] || fail "${GENERATED_BUILD_FILE_NAME} file not symlinked"
755 [[ "$(readlink -f out/soong/workspace/a/b/${GENERATED_BUILD_FILE_NAME})" =~ "bp2build/a/b/${GENERATED_BUILD_FILE_NAME}"$ ]] \
Lukacs T. Berkib353cca2021-04-16 13:47:36 +0200756 || fail "BUILD files symlinked at the wrong place"
757 [[ -L out/soong/workspace/a/b/b.txt ]] || fail "a/b/b.txt not symlinked"
758 [[ -L out/soong/workspace/a/a.txt ]] || fail "a/b/a.txt not symlinked"
759 [[ ! -e out/soong/workspace/out ]] || fail "out directory symlinked"
760}
761
Chris Parsonsec1a3dc2021-04-20 15:32:07 -0400762function test_bp2build_bazel_workspace_add_file {
Lukacs T. Berkib353cca2021-04-16 13:47:36 +0200763 setup
764
765 mkdir -p a
766 touch a/a.txt
767 cat > a/Android.bp <<EOF
768filegroup {
769 name: "a",
770 srcs: ["a.txt"],
771 bazel_module: { bp2build_available: true },
772}
773EOF
774
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200775 run_soong bp2build
Lukacs T. Berkib353cca2021-04-16 13:47:36 +0200776
777 touch a/a2.txt # No reference in the .bp file needed
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200778 run_soong bp2build
Lukacs T. Berkib353cca2021-04-16 13:47:36 +0200779 [[ -L out/soong/workspace/a/a2.txt ]] || fail "a/a2.txt not symlinked"
780}
781
Lukacs T. Berkib21166e2021-04-21 11:58:36 +0200782function test_bp2build_build_file_precedence {
783 setup
784
785 mkdir -p a
786 touch a/a.txt
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -0400787 touch a/${GENERATED_BUILD_FILE_NAME}
Lukacs T. Berkib21166e2021-04-21 11:58:36 +0200788 cat > a/Android.bp <<EOF
789filegroup {
790 name: "a",
791 srcs: ["a.txt"],
792 bazel_module: { bp2build_available: true },
793}
794EOF
795
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200796 run_soong bp2build
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -0400797 [[ -L "out/soong/workspace/a/${GENERATED_BUILD_FILE_NAME}" ]] || fail "${GENERATED_BUILD_FILE_NAME} file not symlinked"
798 [[ "$(readlink -f out/soong/workspace/a/${GENERATED_BUILD_FILE_NAME})" =~ "bp2build/a/${GENERATED_BUILD_FILE_NAME}"$ ]] \
799 || fail "${GENERATED_BUILD_FILE_NAME} files symlinked to the wrong place"
Lukacs T. Berkib21166e2021-04-21 11:58:36 +0200800}
801
Usta Shrestha071f6c22022-10-25 12:34:06 -0400802function test_bp2build_fails_fast {
Lukacs T. Berkib21166e2021-04-21 11:58:36 +0200803 setup
804
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -0400805 mkdir -p "a/${GENERATED_BUILD_FILE_NAME}"
Lukacs T. Berkib21166e2021-04-21 11:58:36 +0200806 cat > a/Android.bp <<EOF
807filegroup {
808 name: "a",
809 srcs: ["a.txt"],
810 bazel_module: { bp2build_available: true },
811}
812EOF
813
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -0400814 mkdir -p "b/${GENERATED_BUILD_FILE_NAME}"
Lukacs T. Berkib21166e2021-04-21 11:58:36 +0200815 cat > b/Android.bp <<EOF
816filegroup {
817 name: "b",
818 srcs: ["b.txt"],
819 bazel_module: { bp2build_available: true },
820}
821EOF
822
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200823 if run_soong bp2build >& "$MOCK_TOP/errors"; then
Lukacs T. Berkib21166e2021-04-21 11:58:36 +0200824 fail "Build should have failed"
825 fi
826
Usta (Tsering) Shrestha93932642023-02-13 16:20:51 +0000827 # we should expect at least one error
828 grep -q -E "(a|b)/${GENERATED_BUILD_FILE_NAME}' exist" "$MOCK_TOP/errors" || fail "Error for ${GENERATED_BUILD_FILE_NAME} not found"
Lukacs T. Berkib21166e2021-04-21 11:58:36 +0200829}
830
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200831function test_bp2build_back_and_forth_null_build {
832 setup
833
834 run_soong
Usta Shrestha4e17a2e2022-12-08 01:18:44 -0500835 local -r output_mtime1=$(stat -c "%y" out/soong/build.ninja)
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200836
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200837 run_soong bp2build
Usta Shrestha4e17a2e2022-12-08 01:18:44 -0500838 local -r output_mtime2=$(stat -c "%y" out/soong/build.ninja)
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200839 if [[ "$output_mtime1" != "$output_mtime2" ]]; then
840 fail "Output Ninja file changed when switching to bp2build"
841 fi
842
Usta Shrestha4e17a2e2022-12-08 01:18:44 -0500843 local -r marker_mtime1=$(stat -c "%y" out/soong/bp2build_workspace_marker)
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200844
845 run_soong
Usta Shrestha4e17a2e2022-12-08 01:18:44 -0500846 local -r output_mtime3=$(stat -c "%y" out/soong/build.ninja)
847 local -r marker_mtime2=$(stat -c "%y" out/soong/bp2build_workspace_marker)
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200848 if [[ "$output_mtime1" != "$output_mtime3" ]]; then
849 fail "Output Ninja file changed when switching to regular build from bp2build"
850 fi
851 if [[ "$marker_mtime1" != "$marker_mtime2" ]]; then
852 fail "bp2build marker file changed when switching to regular build from bp2build"
853 fi
854
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200855 run_soong bp2build
Usta Shrestha4e17a2e2022-12-08 01:18:44 -0500856 local -r output_mtime4=$(stat -c "%y" out/soong/build.ninja)
857 local -r marker_mtime3=$(stat -c "%y" out/soong/bp2build_workspace_marker)
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200858 if [[ "$output_mtime1" != "$output_mtime4" ]]; then
859 fail "Output Ninja file changed when switching back to bp2build"
860 fi
861 if [[ "$marker_mtime1" != "$marker_mtime3" ]]; then
862 fail "bp2build marker file changed when switching back to bp2build"
863 fi
864}
865
Lukacs T. Berki3a821692021-09-06 17:08:02 +0200866function test_queryview_smoke() {
867 setup
868
869 run_soong queryview
870 [[ -e out/soong/queryview/WORKSPACE ]] || fail "queryview WORKSPACE file not created"
871
872}
873
874function test_queryview_null_build() {
875 setup
876
877 run_soong queryview
Usta Shrestha4e17a2e2022-12-08 01:18:44 -0500878 local -r output_mtime1=$(stat -c "%y" out/soong/queryview.marker)
Lukacs T. Berki3a821692021-09-06 17:08:02 +0200879
880 run_soong queryview
Usta Shrestha4e17a2e2022-12-08 01:18:44 -0500881 local -r output_mtime2=$(stat -c "%y" out/soong/queryview.marker)
Lukacs T. Berki3a821692021-09-06 17:08:02 +0200882
883 if [[ "$output_mtime1" != "$output_mtime2" ]]; then
884 fail "Queryview marker file changed on null build"
885 fi
886}
887
Chris Parsonsa3ae0072023-05-10 21:10:08 +0000888# This test verifies that adding a new glob to a blueprint file only
889# causes build.ninja to be regenerated on the *next* build, and *not*
890# the build after. (This is a regression test for a bug where globs
891# resulted in two successive regenerations.)
892function test_new_glob_incrementality {
893 setup
894
895 run_soong nothing
896 local -r mtime1=$(stat -c "%y" out/soong/build.ninja)
897
898 mkdir -p globdefpkg/
899 cat > globdefpkg/Android.bp <<'EOF'
900filegroup {
901 name: "fg_with_glob",
902 srcs: ["*.txt"],
903}
904EOF
905
906 run_soong nothing
907 local -r mtime2=$(stat -c "%y" out/soong/build.ninja)
908
909 if [[ "$mtime1" == "$mtime2" ]]; then
910 fail "Ninja file was not regenerated, despite a new bp file"
911 fi
912
913 run_soong nothing
914 local -r mtime3=$(stat -c "%y" out/soong/build.ninja)
915
916 if [[ "$mtime2" != "$mtime3" ]]; then
917 fail "Ninja file was regenerated despite no previous bp changes"
918 fi
919}
920
Usta Shrestha572ecec2022-12-08 01:29:21 -0500921scan_and_run_tests