blob: 2e7b9a892c65baea7d97d486c4816711fed89777 [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 Faust762c2de2022-11-30 05:16:30 +000064 (export OUT_DIR=$outdir; run_soong bp2build && run_bazel build --config=bp2build //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 Faust762c2de2022-11-30 05:16:30 +000084 (export OUT_DIR=$outdir; run_soong bp2build && run_bazel build --config=bp2build //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 Faust762c2de2022-11-30 05:16:30 +0000141 run_bazel build --config=android --package_path=out/soong/workspace //foo/...
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400142
Jingwen Chenba6d4ac2021-11-09 11:55:36 +0000143 local the_answer_file="bazel-out/android_target-fastbuild/bin/foo/convertible_soong_module/the_answer.txt"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400144 if [[ ! -f "${the_answer_file}" ]]; then
145 fail "Expected '${the_answer_file}' to be generated, but was missing"
146 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
159function test_cc_correctness {
160 setup
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200161
162 mkdir -p a
163 cat > a/Android.bp <<EOF
164cc_object {
165 name: "qq",
166 srcs: ["qq.cc"],
167 bazel_module: {
168 bp2build_available: true,
169 },
170 stl: "none",
171 system_shared_libs: [],
172}
173EOF
174
175 cat > a/qq.cc <<EOF
176#include "qq.h"
177int qq() {
178 return QQ;
179}
180EOF
181
182 cat > a/qq.h <<EOF
183#define QQ 1
184EOF
185
186 run_soong bp2build
187
Cole Faust762c2de2022-11-30 05:16:30 +0000188 run_bazel build --config=android --package_path=out/soong/workspace //a:qq
Usta Shrestha2c9a5e32022-06-09 12:22:36 -0400189 local -r output_mtime1=$(stat -c "%y" bazel-bin/a/_objs/qq/qq.o)
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200190
Cole Faust762c2de2022-11-30 05:16:30 +0000191 run_bazel build --config=android --package_path=out/soong/workspace //a:qq
Usta Shrestha2c9a5e32022-06-09 12:22:36 -0400192 local -r output_mtime2=$(stat -c "%y" bazel-bin/a/_objs/qq/qq.o)
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200193
194 if [[ "$output_mtime1" != "$output_mtime2" ]]; then
195 fail "output changed on null build"
196 fi
197
198 cat > a/qq.h <<EOF
199#define QQ 2
200EOF
201
Cole Faust762c2de2022-11-30 05:16:30 +0000202 run_bazel build --config=android --package_path=out/soong/workspace //a:qq
Usta Shrestha2c9a5e32022-06-09 12:22:36 -0400203 local -r output_mtime3=$(stat -c "%y" bazel-bin/a/_objs/qq/qq.o)
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200204
205 if [[ "$output_mtime1" == "$output_mtime3" ]]; then
206 fail "output not changed when included header changed"
207 fi
208}
209
Jingwen Chend4b1dc82022-05-12 11:08:03 +0000210# Regression test for the following failure during symlink forest creation:
211#
212# Cannot stat '/tmp/st.rr054/foo/bar/unresolved_symlink': stat /tmp/st.rr054/foo/bar/unresolved_symlink: no such file or directory
213#
214function test_bp2build_null_build_with_unresolved_symlink_in_source() {
215 setup
216
217 mkdir -p foo/bar
218 ln -s /tmp/non-existent foo/bar/unresolved_symlink
219 cat > foo/bar/Android.bp <<'EOF'
220filegroup {
221 name: "fg",
222 srcs: ["unresolved_symlink/non-existent-file.txt"],
223 }
224EOF
225
226 run_soong bp2build
227
228 dest=$(readlink -f out/soong/workspace/foo/bar/unresolved_symlink)
229 if [[ "$dest" != "/tmp/non-existent" ]]; then
230 fail "expected to plant an unresolved symlink out/soong/workspace/foo/bar/unresolved_symlink that resolves to /tmp/non-existent"
231 fi
232}
233
Usta Shrestha572ecec2022-12-08 01:29:21 -0500234scan_and_run_tests