blob: ad21d7d89edcf4431288cdff66d10c5d0d406274 [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
53 create_mock_bazel
54
55 mkdir -p a
56 touch a/g.txt
57 cat > a/Android.bp <<'EOF'
58filegroup {
59 name: "g",
60 srcs: ["g.txt"],
61 bazel_module: {bp2build_available: true},
62 }
63EOF
64
65 # A directory under $MOCK_TOP
66 outdir=out2
67 trap "rm -rf $outdir" EXIT
68 # Modify OUT_DIR in a subshell so it doesn't affect the top level one.
69 (export OUT_DIR=$outdir; run_soong bp2build && run_bazel build --config=bp2build //a:g)
70}
71
72test_different_relative_outdir
73
74function test_different_absolute_outdir {
75 setup
76 create_mock_bazel
77
78 mkdir -p a
79 touch a/g.txt
80 cat > a/Android.bp <<'EOF'
81filegroup {
82 name: "g",
83 srcs: ["g.txt"],
84 bazel_module: {bp2build_available: true},
85 }
86EOF
87
88 # A directory under /tmp/...
89 outdir=$(mktemp -t -d st.XXXXX)
90 trap 'rm -rf $outdir' EXIT
91 # Modify OUT_DIR in a subshell so it doesn't affect the top level one.
92 (export OUT_DIR=$outdir; run_soong bp2build && run_bazel build --config=bp2build //a:g)
93}
94
95test_different_absolute_outdir
96
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -040097function test_bp2build_generates_all_buildfiles {
98 setup
99 create_mock_bazel
100
101 mkdir -p foo/convertible_soong_module
102 cat > foo/convertible_soong_module/Android.bp <<'EOF'
103genrule {
104 name: "the_answer",
105 cmd: "echo '42' > $(out)",
106 out: [
107 "the_answer.txt",
108 ],
109 bazel_module: {
110 bp2build_available: true,
111 },
112 }
113EOF
114
115 mkdir -p foo/unconvertible_soong_module
116 cat > foo/unconvertible_soong_module/Android.bp <<'EOF'
117genrule {
118 name: "not_the_answer",
119 cmd: "echo '43' > $(out)",
120 out: [
121 "not_the_answer.txt",
122 ],
123 bazel_module: {
124 bp2build_available: false,
125 },
126 }
127EOF
128
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200129 run_soong bp2build
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400130
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -0400131 if [[ ! -f "./out/soong/workspace/foo/convertible_soong_module/${GENERATED_BUILD_FILE_NAME}" ]]; then
132 fail "./out/soong/workspace/foo/convertible_soong_module/${GENERATED_BUILD_FILE_NAME} was not generated"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400133 fi
134
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -0400135 if [[ ! -f "./out/soong/workspace/foo/unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME}" ]]; then
136 fail "./out/soong/workspace/foo/unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME} was not generated"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400137 fi
138
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -0400139 if ! grep "the_answer" "./out/soong/workspace/foo/convertible_soong_module/${GENERATED_BUILD_FILE_NAME}"; then
140 fail "missing BUILD target the_answer in convertible_soong_module/${GENERATED_BUILD_FILE_NAME}"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400141 fi
142
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -0400143 if grep "not_the_answer" "./out/soong/workspace/foo/unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME}"; then
144 fail "found unexpected BUILD target not_the_answer in unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME}"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400145 fi
146
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -0400147 if ! grep "filegroup" "./out/soong/workspace/foo/unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME}"; then
148 fail "missing filegroup in unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME}"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400149 fi
150
151 # NOTE: We don't actually use the extra BUILD file for anything here
Jingwen Chen60d88402022-09-07 11:03:33 +0000152 run_bazel build --config=android --package_path=out/soong/workspace //foo/...
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400153
Jingwen Chenba6d4ac2021-11-09 11:55:36 +0000154 local the_answer_file="bazel-out/android_target-fastbuild/bin/foo/convertible_soong_module/the_answer.txt"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400155 if [[ ! -f "${the_answer_file}" ]]; then
156 fail "Expected '${the_answer_file}' to be generated, but was missing"
157 fi
158 if ! grep 42 "${the_answer_file}"; then
159 fail "Expected to find 42 in '${the_answer_file}'"
160 fi
161}
162
Sam Delmerico0d08b9b2022-09-22 11:43:21 -0400163_save_trap=$(trap -p EXIT)
164trap '[[ $? -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 -0400165test_bp2build_generates_all_buildfiles
Sam Delmerico0d08b9b2022-09-22 11:43:21 -0400166eval ${_save_trap}
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200167
168function test_cc_correctness {
169 setup
170 create_mock_bazel
171
172 mkdir -p a
173 cat > a/Android.bp <<EOF
174cc_object {
175 name: "qq",
176 srcs: ["qq.cc"],
177 bazel_module: {
178 bp2build_available: true,
179 },
180 stl: "none",
181 system_shared_libs: [],
182}
183EOF
184
185 cat > a/qq.cc <<EOF
186#include "qq.h"
187int qq() {
188 return QQ;
189}
190EOF
191
192 cat > a/qq.h <<EOF
193#define QQ 1
194EOF
195
196 run_soong bp2build
197
Jingwen Chen60d88402022-09-07 11:03:33 +0000198 run_bazel build --config=android --package_path=out/soong/workspace //a:qq
Usta Shrestha2c9a5e32022-06-09 12:22:36 -0400199 local -r output_mtime1=$(stat -c "%y" bazel-bin/a/_objs/qq/qq.o)
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200200
Jingwen Chen60d88402022-09-07 11:03:33 +0000201 run_bazel build --config=android --package_path=out/soong/workspace //a:qq
Usta Shrestha2c9a5e32022-06-09 12:22:36 -0400202 local -r output_mtime2=$(stat -c "%y" bazel-bin/a/_objs/qq/qq.o)
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200203
204 if [[ "$output_mtime1" != "$output_mtime2" ]]; then
205 fail "output changed on null build"
206 fi
207
208 cat > a/qq.h <<EOF
209#define QQ 2
210EOF
211
Jingwen Chen60d88402022-09-07 11:03:33 +0000212 run_bazel build --config=android --package_path=out/soong/workspace //a:qq
Usta Shrestha2c9a5e32022-06-09 12:22:36 -0400213 local -r output_mtime3=$(stat -c "%y" bazel-bin/a/_objs/qq/qq.o)
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200214
215 if [[ "$output_mtime1" == "$output_mtime3" ]]; then
216 fail "output not changed when included header changed"
217 fi
218}
219
220test_cc_correctness
Jingwen Chend4b1dc82022-05-12 11:08:03 +0000221
222# Regression test for the following failure during symlink forest creation:
223#
224# Cannot stat '/tmp/st.rr054/foo/bar/unresolved_symlink': stat /tmp/st.rr054/foo/bar/unresolved_symlink: no such file or directory
225#
226function test_bp2build_null_build_with_unresolved_symlink_in_source() {
227 setup
228
229 mkdir -p foo/bar
230 ln -s /tmp/non-existent foo/bar/unresolved_symlink
231 cat > foo/bar/Android.bp <<'EOF'
232filegroup {
233 name: "fg",
234 srcs: ["unresolved_symlink/non-existent-file.txt"],
235 }
236EOF
237
238 run_soong bp2build
239
240 dest=$(readlink -f out/soong/workspace/foo/bar/unresolved_symlink)
241 if [[ "$dest" != "/tmp/non-existent" ]]; then
242 fail "expected to plant an unresolved symlink out/soong/workspace/foo/bar/unresolved_symlink that resolves to /tmp/non-existent"
243 fi
244}
245
246test_bp2build_null_build_with_unresolved_symlink_in_source