blob: 3cb6c8ccad6a6dc1c920f646ddd7e770d2c4ca60 [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
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -040051function test_bp2build_generates_all_buildfiles {
52 setup
53 create_mock_bazel
54
55 mkdir -p foo/convertible_soong_module
56 cat > foo/convertible_soong_module/Android.bp <<'EOF'
57genrule {
58 name: "the_answer",
59 cmd: "echo '42' > $(out)",
60 out: [
61 "the_answer.txt",
62 ],
63 bazel_module: {
64 bp2build_available: true,
65 },
66 }
67EOF
68
69 mkdir -p foo/unconvertible_soong_module
70 cat > foo/unconvertible_soong_module/Android.bp <<'EOF'
71genrule {
72 name: "not_the_answer",
73 cmd: "echo '43' > $(out)",
74 out: [
75 "not_the_answer.txt",
76 ],
77 bazel_module: {
78 bp2build_available: false,
79 },
80 }
81EOF
82
Lukacs T. Berkia1b93722021-09-02 17:23:06 +020083 run_soong bp2build
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -040084
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -040085 if [[ ! -f "./out/soong/workspace/foo/convertible_soong_module/${GENERATED_BUILD_FILE_NAME}" ]]; then
86 fail "./out/soong/workspace/foo/convertible_soong_module/${GENERATED_BUILD_FILE_NAME} was not generated"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -040087 fi
88
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -040089 if [[ ! -f "./out/soong/workspace/foo/unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME}" ]]; then
90 fail "./out/soong/workspace/foo/unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME} was not generated"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -040091 fi
92
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -040093 if ! grep "the_answer" "./out/soong/workspace/foo/convertible_soong_module/${GENERATED_BUILD_FILE_NAME}"; then
94 fail "missing BUILD target the_answer in convertible_soong_module/${GENERATED_BUILD_FILE_NAME}"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -040095 fi
96
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -040097 if grep "not_the_answer" "./out/soong/workspace/foo/unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME}"; then
98 fail "found unexpected BUILD target not_the_answer in unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME}"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -040099 fi
100
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -0400101 if ! grep "filegroup" "./out/soong/workspace/foo/unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME}"; then
102 fail "missing filegroup in unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME}"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400103 fi
104
105 # NOTE: We don't actually use the extra BUILD file for anything here
Jingwen Chen60d88402022-09-07 11:03:33 +0000106 run_bazel build --config=android --package_path=out/soong/workspace //foo/...
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400107
Jingwen Chenba6d4ac2021-11-09 11:55:36 +0000108 local the_answer_file="bazel-out/android_target-fastbuild/bin/foo/convertible_soong_module/the_answer.txt"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400109 if [[ ! -f "${the_answer_file}" ]]; then
110 fail "Expected '${the_answer_file}' to be generated, but was missing"
111 fi
112 if ! grep 42 "${the_answer_file}"; then
113 fail "Expected to find 42 in '${the_answer_file}'"
114 fi
115}
116
Sam Delmerico0d08b9b2022-09-22 11:43:21 -0400117_save_trap=$(trap -p EXIT)
118trap '[[ $? -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 -0400119test_bp2build_generates_all_buildfiles
Sam Delmerico0d08b9b2022-09-22 11:43:21 -0400120eval ${_save_trap}
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200121
122function test_cc_correctness {
123 setup
124 create_mock_bazel
125
126 mkdir -p a
127 cat > a/Android.bp <<EOF
128cc_object {
129 name: "qq",
130 srcs: ["qq.cc"],
131 bazel_module: {
132 bp2build_available: true,
133 },
134 stl: "none",
135 system_shared_libs: [],
136}
137EOF
138
139 cat > a/qq.cc <<EOF
140#include "qq.h"
141int qq() {
142 return QQ;
143}
144EOF
145
146 cat > a/qq.h <<EOF
147#define QQ 1
148EOF
149
150 run_soong bp2build
151
Jingwen Chen60d88402022-09-07 11:03:33 +0000152 run_bazel build --config=android --package_path=out/soong/workspace //a:qq
Usta Shrestha2c9a5e32022-06-09 12:22:36 -0400153 local -r output_mtime1=$(stat -c "%y" bazel-bin/a/_objs/qq/qq.o)
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200154
Jingwen Chen60d88402022-09-07 11:03:33 +0000155 run_bazel build --config=android --package_path=out/soong/workspace //a:qq
Usta Shrestha2c9a5e32022-06-09 12:22:36 -0400156 local -r output_mtime2=$(stat -c "%y" bazel-bin/a/_objs/qq/qq.o)
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200157
158 if [[ "$output_mtime1" != "$output_mtime2" ]]; then
159 fail "output changed on null build"
160 fi
161
162 cat > a/qq.h <<EOF
163#define QQ 2
164EOF
165
Jingwen Chen60d88402022-09-07 11:03:33 +0000166 run_bazel build --config=android --package_path=out/soong/workspace //a:qq
Usta Shrestha2c9a5e32022-06-09 12:22:36 -0400167 local -r output_mtime3=$(stat -c "%y" bazel-bin/a/_objs/qq/qq.o)
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200168
169 if [[ "$output_mtime1" == "$output_mtime3" ]]; then
170 fail "output not changed when included header changed"
171 fi
172}
173
174test_cc_correctness
Jingwen Chend4b1dc82022-05-12 11:08:03 +0000175
176# Regression test for the following failure during symlink forest creation:
177#
178# Cannot stat '/tmp/st.rr054/foo/bar/unresolved_symlink': stat /tmp/st.rr054/foo/bar/unresolved_symlink: no such file or directory
179#
180function test_bp2build_null_build_with_unresolved_symlink_in_source() {
181 setup
182
183 mkdir -p foo/bar
184 ln -s /tmp/non-existent foo/bar/unresolved_symlink
185 cat > foo/bar/Android.bp <<'EOF'
186filegroup {
187 name: "fg",
188 srcs: ["unresolved_symlink/non-existent-file.txt"],
189 }
190EOF
191
192 run_soong bp2build
193
194 dest=$(readlink -f out/soong/workspace/foo/bar/unresolved_symlink)
195 if [[ "$dest" != "/tmp/non-existent" ]]; then
196 fail "expected to plant an unresolved symlink out/soong/workspace/foo/bar/unresolved_symlink that resolves to /tmp/non-existent"
197 fi
198}
199
200test_bp2build_null_build_with_unresolved_symlink_in_source