blob: 679ac557c8bea864aa8bd9c6c92d56e67cd02f06 [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
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
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
Lukacs T. Berkia1b93722021-09-02 17:23:06 +020038 run_soong bp2build
Usta Shrestha2c9a5e32022-06-09 12:22:36 -040039 local -r output_mtime1=$(stat -c "%y" out/soong/bp2build_workspace_marker)
Jingwen Chen53dfa402021-08-12 09:37:14 +000040
Lukacs T. Berkia1b93722021-09-02 17:23:06 +020041 run_soong bp2build
Usta Shrestha2c9a5e32022-06-09 12:22:36 -040042 local -r output_mtime2=$(stat -c "%y" out/soong/bp2build_workspace_marker)
Jingwen Chen53dfa402021-08-12 09:37:14 +000043
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
Jingwen Chen7e11eb82022-10-13 09:25:38 +000051function test_different_relative_outdir {
52 setup
Jingwen Chen7e11eb82022-10-13 09:25:38 +000053
54 mkdir -p a
55 touch a/g.txt
56 cat > a/Android.bp <<'EOF'
57filegroup {
58 name: "g",
59 srcs: ["g.txt"],
60 bazel_module: {bp2build_available: true},
61 }
62EOF
63
64 # A directory under $MOCK_TOP
65 outdir=out2
66 trap "rm -rf $outdir" EXIT
67 # Modify OUT_DIR in a subshell so it doesn't affect the top level one.
Cole Faust762c2de2022-11-30 05:16:30 +000068 (export OUT_DIR=$outdir; run_soong bp2build && run_bazel build --config=bp2build //a:g)
Jingwen Chen7e11eb82022-10-13 09:25:38 +000069}
70
71test_different_relative_outdir
72
73function test_different_absolute_outdir {
74 setup
Jingwen Chen7e11eb82022-10-13 09:25:38 +000075
76 mkdir -p a
77 touch a/g.txt
78 cat > a/Android.bp <<'EOF'
79filegroup {
80 name: "g",
81 srcs: ["g.txt"],
82 bazel_module: {bp2build_available: true},
83 }
84EOF
85
86 # A directory under /tmp/...
87 outdir=$(mktemp -t -d st.XXXXX)
88 trap 'rm -rf $outdir' EXIT
89 # Modify OUT_DIR in a subshell so it doesn't affect the top level one.
Cole Faust762c2de2022-11-30 05:16:30 +000090 (export OUT_DIR=$outdir; run_soong bp2build && run_bazel build --config=bp2build //a:g)
Jingwen Chen7e11eb82022-10-13 09:25:38 +000091}
92
93test_different_absolute_outdir
94
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -040095function test_bp2build_generates_all_buildfiles {
96 setup
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -040097
98 mkdir -p foo/convertible_soong_module
99 cat > foo/convertible_soong_module/Android.bp <<'EOF'
100genrule {
101 name: "the_answer",
102 cmd: "echo '42' > $(out)",
103 out: [
104 "the_answer.txt",
105 ],
106 bazel_module: {
107 bp2build_available: true,
108 },
109 }
110EOF
111
112 mkdir -p foo/unconvertible_soong_module
113 cat > foo/unconvertible_soong_module/Android.bp <<'EOF'
114genrule {
115 name: "not_the_answer",
116 cmd: "echo '43' > $(out)",
117 out: [
118 "not_the_answer.txt",
119 ],
120 bazel_module: {
121 bp2build_available: false,
122 },
123 }
124EOF
125
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200126 run_soong bp2build
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400127
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -0400128 if [[ ! -f "./out/soong/workspace/foo/convertible_soong_module/${GENERATED_BUILD_FILE_NAME}" ]]; then
129 fail "./out/soong/workspace/foo/convertible_soong_module/${GENERATED_BUILD_FILE_NAME} was not generated"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400130 fi
131
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -0400132 if [[ ! -f "./out/soong/workspace/foo/unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME}" ]]; then
133 fail "./out/soong/workspace/foo/unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME} was not generated"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400134 fi
135
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -0400136 if ! grep "the_answer" "./out/soong/workspace/foo/convertible_soong_module/${GENERATED_BUILD_FILE_NAME}"; then
137 fail "missing BUILD target the_answer in convertible_soong_module/${GENERATED_BUILD_FILE_NAME}"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400138 fi
139
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -0400140 if grep "not_the_answer" "./out/soong/workspace/foo/unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME}"; then
141 fail "found unexpected BUILD target not_the_answer in unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME}"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400142 fi
143
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -0400144 if ! grep "filegroup" "./out/soong/workspace/foo/unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME}"; then
145 fail "missing filegroup in unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME}"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400146 fi
147
148 # NOTE: We don't actually use the extra BUILD file for anything here
Cole Faust762c2de2022-11-30 05:16:30 +0000149 run_bazel build --config=android --package_path=out/soong/workspace //foo/...
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400150
Jingwen Chenba6d4ac2021-11-09 11:55:36 +0000151 local the_answer_file="bazel-out/android_target-fastbuild/bin/foo/convertible_soong_module/the_answer.txt"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400152 if [[ ! -f "${the_answer_file}" ]]; then
153 fail "Expected '${the_answer_file}' to be generated, but was missing"
154 fi
155 if ! grep 42 "${the_answer_file}"; then
156 fail "Expected to find 42 in '${the_answer_file}'"
157 fi
158}
159
Sam Delmerico0d08b9b2022-09-22 11:43:21 -0400160_save_trap=$(trap -p EXIT)
161trap '[[ $? -ne 0 ]] && echo Are you running this locally? Try changing --sandbox_tmpfs_path to something other than /tmp/ in build/bazel/linux.bazelrc.' EXIT
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400162test_bp2build_generates_all_buildfiles
Sam Delmerico0d08b9b2022-09-22 11:43:21 -0400163eval ${_save_trap}
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200164
165function test_cc_correctness {
166 setup
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200167
168 mkdir -p a
169 cat > a/Android.bp <<EOF
170cc_object {
171 name: "qq",
172 srcs: ["qq.cc"],
173 bazel_module: {
174 bp2build_available: true,
175 },
176 stl: "none",
177 system_shared_libs: [],
178}
179EOF
180
181 cat > a/qq.cc <<EOF
182#include "qq.h"
183int qq() {
184 return QQ;
185}
186EOF
187
188 cat > a/qq.h <<EOF
189#define QQ 1
190EOF
191
192 run_soong bp2build
193
Cole Faust762c2de2022-11-30 05:16:30 +0000194 run_bazel build --config=android --package_path=out/soong/workspace //a:qq
Usta Shrestha2c9a5e32022-06-09 12:22:36 -0400195 local -r output_mtime1=$(stat -c "%y" bazel-bin/a/_objs/qq/qq.o)
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200196
Cole Faust762c2de2022-11-30 05:16:30 +0000197 run_bazel build --config=android --package_path=out/soong/workspace //a:qq
Usta Shrestha2c9a5e32022-06-09 12:22:36 -0400198 local -r output_mtime2=$(stat -c "%y" bazel-bin/a/_objs/qq/qq.o)
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200199
200 if [[ "$output_mtime1" != "$output_mtime2" ]]; then
201 fail "output changed on null build"
202 fi
203
204 cat > a/qq.h <<EOF
205#define QQ 2
206EOF
207
Cole Faust762c2de2022-11-30 05:16:30 +0000208 run_bazel build --config=android --package_path=out/soong/workspace //a:qq
Usta Shrestha2c9a5e32022-06-09 12:22:36 -0400209 local -r output_mtime3=$(stat -c "%y" bazel-bin/a/_objs/qq/qq.o)
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200210
211 if [[ "$output_mtime1" == "$output_mtime3" ]]; then
212 fail "output not changed when included header changed"
213 fi
214}
215
216test_cc_correctness
Jingwen Chend4b1dc82022-05-12 11:08:03 +0000217
218# Regression test for the following failure during symlink forest creation:
219#
220# Cannot stat '/tmp/st.rr054/foo/bar/unresolved_symlink': stat /tmp/st.rr054/foo/bar/unresolved_symlink: no such file or directory
221#
222function test_bp2build_null_build_with_unresolved_symlink_in_source() {
223 setup
224
225 mkdir -p foo/bar
226 ln -s /tmp/non-existent foo/bar/unresolved_symlink
227 cat > foo/bar/Android.bp <<'EOF'
228filegroup {
229 name: "fg",
230 srcs: ["unresolved_symlink/non-existent-file.txt"],
231 }
232EOF
233
234 run_soong bp2build
235
236 dest=$(readlink -f out/soong/workspace/foo/bar/unresolved_symlink)
237 if [[ "$dest" != "/tmp/non-existent" ]]; then
238 fail "expected to plant an unresolved symlink out/soong/workspace/foo/bar/unresolved_symlink that resolves to /tmp/non-existent"
239 fi
240}
241
242test_bp2build_null_build_with_unresolved_symlink_in_source