blob: b5c6863112159f455c44dcaaba6bb7b77a9adafa [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
Jingwen Chen53dfa402021-08-12 09:37:14 +000011function test_bp2build_null_build() {
12 setup
13 run_bp2build
14 local output_mtime1=$(stat -c "%y" out/soong/.bootstrap/bp2build_workspace_marker)
15
16 run_bp2build
17 local output_mtime2=$(stat -c "%y" out/soong/.bootstrap/bp2build_workspace_marker)
18
19 if [[ "$output_mtime1" != "$output_mtime2" ]]; then
20 fail "Output bp2build marker file changed on null build"
21 fi
22}
23
24test_bp2build_null_build
25
26function test_bp2build_null_build_with_globs() {
27 setup
28
29 mkdir -p foo/bar
30 cat > foo/bar/Android.bp <<'EOF'
31filegroup {
32 name: "globs",
33 srcs: ["*.txt"],
34 }
35EOF
36 touch foo/bar/a.txt foo/bar/b.txt
37
38 run_bp2build
39 local output_mtime1=$(stat -c "%y" out/soong/.bootstrap/bp2build_workspace_marker)
40
41 run_bp2build
42 local output_mtime2=$(stat -c "%y" out/soong/.bootstrap/bp2build_workspace_marker)
43
44 if [[ "$output_mtime1" != "$output_mtime2" ]]; then
45 fail "Output bp2build marker file changed on null build"
46 fi
47}
48
49test_bp2build_null_build_with_globs
50
51function test_soong_after_bp2build_regenerates_build_globs_ninja() {
52 setup
53
54 mkdir -p foo/bar
55 cat > foo/bar/Android.bp <<'EOF'
56filegroup {
57 name: "bp2build-files",
58 srcs: ["bp2build.*"],
59 bazel_module: { bp2build_available: true },
60}
61
62filegroup {
63 name: "soong-files",
64 srcs: ["soong.*"],
65}
66EOF
67 touch foo/bar/bp2build.txt foo/bar/soong.txt
68
69 build_globs_file="out/soong/.bootstrap/build-globs.ninja"
70
71 # Test: the build-globs file for bp2build should only contain the bp2build-files
72 # glob, whereas the build-globs file for soong should contain both bp2build-files
73 # and soong-files globs.
74
75 run_bp2build
76 local output_mtime1=$(stat -c "%y" "${build_globs_file}")
77
78 if ! grep "\"foo/bar/bp2build.*\"" "${build_globs_file}"; then
79 fail "bp2build filegroup globs not found in bp2build's globs file"
80 fi
81
82 if grep "\"foo/bar/soong.*\"" "${build_globs_file}"; then
83 fail "soong filegroup globs unexpectedly found in bp2build's globs file"
84 fi
85
86 run_soong
87 local output_mtime2=$(stat -c "%y" "${build_globs_file}")
88
89 if [[ "$output_mtime1" == "$output_mtime2" ]]; then
90 fail "Output build-globs.ninja file did not change"
91 fi
92
93 if ! grep "\"foo/bar/bp2build.*\"" "${build_globs_file}"; then
94 fail "bp2build filegroup globs not found in bp2build's globs file"
95 fi
96
97 if ! grep "\"foo/bar/soong.*\"" "${build_globs_file}"; then
98 fail "soong filegroup globs not found in bp2build's globs file"
99 fi
100
101}
102
103test_soong_after_bp2build_regenerates_build_globs_ninja
104
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400105function test_bp2build_generates_all_buildfiles {
106 setup
107 create_mock_bazel
108
109 mkdir -p foo/convertible_soong_module
110 cat > foo/convertible_soong_module/Android.bp <<'EOF'
111genrule {
112 name: "the_answer",
113 cmd: "echo '42' > $(out)",
114 out: [
115 "the_answer.txt",
116 ],
117 bazel_module: {
118 bp2build_available: true,
119 },
120 }
121EOF
122
123 mkdir -p foo/unconvertible_soong_module
124 cat > foo/unconvertible_soong_module/Android.bp <<'EOF'
125genrule {
126 name: "not_the_answer",
127 cmd: "echo '43' > $(out)",
128 out: [
129 "not_the_answer.txt",
130 ],
131 bazel_module: {
132 bp2build_available: false,
133 },
134 }
135EOF
136
137 run_bp2build
138
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -0400139 if [[ ! -f "./out/soong/workspace/foo/convertible_soong_module/${GENERATED_BUILD_FILE_NAME}" ]]; then
140 fail "./out/soong/workspace/foo/convertible_soong_module/${GENERATED_BUILD_FILE_NAME} was not generated"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400141 fi
142
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -0400143 if [[ ! -f "./out/soong/workspace/foo/unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME}" ]]; then
144 fail "./out/soong/workspace/foo/unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME} was not generated"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400145 fi
146
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -0400147 if ! grep "the_answer" "./out/soong/workspace/foo/convertible_soong_module/${GENERATED_BUILD_FILE_NAME}"; then
148 fail "missing BUILD target the_answer in convertible_soong_module/${GENERATED_BUILD_FILE_NAME}"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400149 fi
150
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -0400151 if grep "not_the_answer" "./out/soong/workspace/foo/unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME}"; then
152 fail "found unexpected BUILD target not_the_answer in unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME}"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400153 fi
154
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -0400155 if ! grep "filegroup" "./out/soong/workspace/foo/unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME}"; then
156 fail "missing filegroup in unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME}"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400157 fi
158
159 # NOTE: We don't actually use the extra BUILD file for anything here
160 run_bazel build --package_path=out/soong/workspace //foo/...
161
162 local the_answer_file="bazel-out/k8-fastbuild/bin/foo/convertible_soong_module/the_answer.txt"
163 if [[ ! -f "${the_answer_file}" ]]; then
164 fail "Expected '${the_answer_file}' to be generated, but was missing"
165 fi
166 if ! grep 42 "${the_answer_file}"; then
167 fail "Expected to find 42 in '${the_answer_file}'"
168 fi
169}
170
171test_bp2build_generates_all_buildfiles