blob: 878b4a16fc67f3bb525afae0643677b0ec7c0e8e [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
Usta Shrestha572ecec2022-12-08 01:29:21 -050024function test_bp2build_null_build_with_globs {
Jingwen Chen53dfa402021-08-12 09:37:14 +000025 setup
26
27 mkdir -p foo/bar
28 cat > foo/bar/Android.bp <<'EOF'
29filegroup {
30 name: "globs",
31 srcs: ["*.txt"],
32 }
33EOF
34 touch foo/bar/a.txt foo/bar/b.txt
35
Lukacs T. Berkia1b93722021-09-02 17:23:06 +020036 run_soong bp2build
Usta Shrestha2c9a5e32022-06-09 12:22:36 -040037 local -r output_mtime1=$(stat -c "%y" out/soong/bp2build_workspace_marker)
Jingwen Chen53dfa402021-08-12 09:37:14 +000038
Lukacs T. Berkia1b93722021-09-02 17:23:06 +020039 run_soong bp2build
Usta Shrestha2c9a5e32022-06-09 12:22:36 -040040 local -r output_mtime2=$(stat -c "%y" out/soong/bp2build_workspace_marker)
Jingwen Chen53dfa402021-08-12 09:37:14 +000041
42 if [[ "$output_mtime1" != "$output_mtime2" ]]; then
43 fail "Output bp2build marker file changed on null build"
44 fi
45}
46
Jingwen Chen7e11eb82022-10-13 09:25:38 +000047function test_different_relative_outdir {
48 setup
Jingwen Chen7e11eb82022-10-13 09:25:38 +000049
50 mkdir -p a
51 touch a/g.txt
52 cat > a/Android.bp <<'EOF'
53filegroup {
54 name: "g",
55 srcs: ["g.txt"],
56 bazel_module: {bp2build_available: true},
57 }
58EOF
59
60 # A directory under $MOCK_TOP
61 outdir=out2
62 trap "rm -rf $outdir" EXIT
63 # Modify OUT_DIR in a subshell so it doesn't affect the top level one.
Cole Faustde12be32022-11-19 15:14:48 -080064 (export OUT_DIR=$outdir; run_soong bp2build && run_bazel build --config=bp2build --config=ci //a:g)
Jingwen Chen7e11eb82022-10-13 09:25:38 +000065}
66
Jingwen Chen7e11eb82022-10-13 09:25:38 +000067function test_different_absolute_outdir {
68 setup
Jingwen Chen7e11eb82022-10-13 09:25:38 +000069
70 mkdir -p a
71 touch a/g.txt
72 cat > a/Android.bp <<'EOF'
73filegroup {
74 name: "g",
75 srcs: ["g.txt"],
76 bazel_module: {bp2build_available: true},
77 }
78EOF
79
80 # A directory under /tmp/...
81 outdir=$(mktemp -t -d st.XXXXX)
82 trap 'rm -rf $outdir' EXIT
83 # Modify OUT_DIR in a subshell so it doesn't affect the top level one.
Cole Faustde12be32022-11-19 15:14:48 -080084 (export OUT_DIR=$outdir; run_soong bp2build && run_bazel build --config=bp2build --config=ci //a:g)
Jingwen Chen7e11eb82022-10-13 09:25:38 +000085}
86
Usta Shrestha572ecec2022-12-08 01:29:21 -050087function _bp2build_generates_all_buildfiles {
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -040088 setup
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -040089
90 mkdir -p foo/convertible_soong_module
91 cat > foo/convertible_soong_module/Android.bp <<'EOF'
92genrule {
93 name: "the_answer",
94 cmd: "echo '42' > $(out)",
95 out: [
96 "the_answer.txt",
97 ],
98 bazel_module: {
99 bp2build_available: true,
100 },
101 }
102EOF
103
104 mkdir -p foo/unconvertible_soong_module
105 cat > foo/unconvertible_soong_module/Android.bp <<'EOF'
106genrule {
107 name: "not_the_answer",
108 cmd: "echo '43' > $(out)",
109 out: [
110 "not_the_answer.txt",
111 ],
112 bazel_module: {
113 bp2build_available: false,
114 },
115 }
116EOF
117
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200118 run_soong bp2build
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400119
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -0400120 if [[ ! -f "./out/soong/workspace/foo/convertible_soong_module/${GENERATED_BUILD_FILE_NAME}" ]]; then
121 fail "./out/soong/workspace/foo/convertible_soong_module/${GENERATED_BUILD_FILE_NAME} was not generated"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400122 fi
123
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -0400124 if [[ ! -f "./out/soong/workspace/foo/unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME}" ]]; then
125 fail "./out/soong/workspace/foo/unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME} was not generated"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400126 fi
127
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -0400128 if ! grep "the_answer" "./out/soong/workspace/foo/convertible_soong_module/${GENERATED_BUILD_FILE_NAME}"; then
129 fail "missing BUILD target the_answer in convertible_soong_module/${GENERATED_BUILD_FILE_NAME}"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400130 fi
131
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -0400132 if grep "not_the_answer" "./out/soong/workspace/foo/unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME}"; then
133 fail "found unexpected BUILD target not_the_answer in unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME}"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400134 fi
135
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -0400136 if ! grep "filegroup" "./out/soong/workspace/foo/unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME}"; then
137 fail "missing filegroup in unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME}"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400138 fi
139
140 # NOTE: We don't actually use the extra BUILD file for anything here
Cole Faustde12be32022-11-19 15:14:48 -0800141 run_bazel build --config=android --config=bp2build --config=ci //foo/...
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400142
Usta (Tsering) Shresthac4c07b12022-11-08 18:31:14 -0500143 local -r the_answer_file="$(find -L bazel-out -name the_answer.txt)"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400144 if [[ ! -f "${the_answer_file}" ]]; then
Cole Faustb85d1a12022-11-08 18:14:01 -0800145 fail "Expected the_answer.txt to be generated, but was missing"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400146 fi
147 if ! grep 42 "${the_answer_file}"; then
148 fail "Expected to find 42 in '${the_answer_file}'"
149 fi
150}
151
Usta Shrestha572ecec2022-12-08 01:29:21 -0500152function test_bp2build_generates_all_buildfiles {
153 _save_trap=$(trap -p EXIT)
154 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
155 _bp2build_generates_all_buildfiles
156 eval "${_save_trap}"
157}
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200158
Usta (Tsering) Shresthac4c07b12022-11-08 18:31:14 -0500159function test_bp2build_symlinks_files {
160 setup
161 mkdir -p foo
162 touch foo/BLANK1
163 touch foo/BLANK2
164 touch foo/F2D
165 touch foo/BUILD
166
167 run_soong bp2build
168
169 if [[ -e "./out/soong/workspace/foo/BUILD" ]]; then
170 fail "./out/soong/workspace/foo/BUILD should be omitted"
171 fi
172 for file in BLANK1 BLANK2 F2D
173 do
174 if [[ ! -L "./out/soong/workspace/foo/$file" ]]; then
175 fail "./out/soong/workspace/foo/$file should exist"
176 fi
177 done
178 local -r BLANK1_BEFORE=$(stat -c %y "./out/soong/workspace/foo/BLANK1")
179
180 rm foo/BLANK2
181 rm foo/F2D
182 mkdir foo/F2D
183 touch foo/F2D/BUILD
184
185 run_soong bp2build
186
187 if [[ -e "./out/soong/workspace/foo/BUILD" ]]; then
188 fail "./out/soong/workspace/foo/BUILD should be omitted"
189 fi
190 local -r BLANK1_AFTER=$(stat -c %y "./out/soong/workspace/foo/BLANK1")
191 if [[ "$BLANK1_AFTER" != "$BLANK1_BEFORE" ]]; then
192 fail "./out/soong/workspace/foo/BLANK1 should be untouched"
193 fi
194 if [[ -e "./out/soong/workspace/foo/BLANK2" ]]; then
195 fail "./out/soong/workspace/foo/BLANK2 should be removed"
196 fi
197 if [[ -L "./out/soong/workspace/foo/F2D" ]] || [[ ! -d "./out/soong/workspace/foo/F2D" ]]; then
198 fail "./out/soong/workspace/foo/F2D should be a dir"
199 fi
200}
201
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200202function test_cc_correctness {
203 setup
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200204
205 mkdir -p a
206 cat > a/Android.bp <<EOF
207cc_object {
208 name: "qq",
209 srcs: ["qq.cc"],
210 bazel_module: {
211 bp2build_available: true,
212 },
213 stl: "none",
214 system_shared_libs: [],
215}
216EOF
217
218 cat > a/qq.cc <<EOF
219#include "qq.h"
220int qq() {
221 return QQ;
222}
223EOF
224
225 cat > a/qq.h <<EOF
226#define QQ 1
227EOF
228
229 run_soong bp2build
230
Cole Faustde12be32022-11-19 15:14:48 -0800231 run_bazel build --config=android --config=bp2build --config=ci //a:qq
Usta Shrestha2c9a5e32022-06-09 12:22:36 -0400232 local -r output_mtime1=$(stat -c "%y" bazel-bin/a/_objs/qq/qq.o)
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200233
Cole Faustde12be32022-11-19 15:14:48 -0800234 run_bazel build --config=android --config=bp2build --config=ci //a:qq
Usta Shrestha2c9a5e32022-06-09 12:22:36 -0400235 local -r output_mtime2=$(stat -c "%y" bazel-bin/a/_objs/qq/qq.o)
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200236
237 if [[ "$output_mtime1" != "$output_mtime2" ]]; then
238 fail "output changed on null build"
239 fi
240
241 cat > a/qq.h <<EOF
242#define QQ 2
243EOF
244
Cole Faustde12be32022-11-19 15:14:48 -0800245 run_bazel build --config=android --config=bp2build --config=ci //a:qq
Usta Shrestha2c9a5e32022-06-09 12:22:36 -0400246 local -r output_mtime3=$(stat -c "%y" bazel-bin/a/_objs/qq/qq.o)
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200247
248 if [[ "$output_mtime1" == "$output_mtime3" ]]; then
249 fail "output not changed when included header changed"
250 fi
251}
252
Jingwen Chend4b1dc82022-05-12 11:08:03 +0000253# Regression test for the following failure during symlink forest creation:
254#
255# Cannot stat '/tmp/st.rr054/foo/bar/unresolved_symlink': stat /tmp/st.rr054/foo/bar/unresolved_symlink: no such file or directory
256#
257function test_bp2build_null_build_with_unresolved_symlink_in_source() {
258 setup
259
260 mkdir -p foo/bar
261 ln -s /tmp/non-existent foo/bar/unresolved_symlink
262 cat > foo/bar/Android.bp <<'EOF'
263filegroup {
264 name: "fg",
265 srcs: ["unresolved_symlink/non-existent-file.txt"],
266 }
267EOF
268
269 run_soong bp2build
270
271 dest=$(readlink -f out/soong/workspace/foo/bar/unresolved_symlink)
272 if [[ "$dest" != "/tmp/non-existent" ]]; then
273 fail "expected to plant an unresolved symlink out/soong/workspace/foo/bar/unresolved_symlink that resolves to /tmp/non-existent"
274 fi
275}
276
Spandan Das255648c2023-01-11 03:05:24 +0000277# Smoke test to verify api_bp2build worksapce does not contain any errors
278function test_api_bp2build_empty_build() {
279 setup
280 run_soong api_bp2build
281 run_bazel build --config=android --config=api_bp2build //:empty
282}
283
Usta Shrestha572ecec2022-12-08 01:29:21 -0500284scan_and_run_tests