blob: 74e49aabe174202fd773710741d5934c2f46c7ca [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
Lukacs T. Berki90b43342021-11-02 14:42:04 +010014 local 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
Lukacs T. Berki90b43342021-11-02 14:42:04 +010017 local 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
Lukacs T. Berki90b43342021-11-02 14:42:04 +010039 local 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
Lukacs T. Berki90b43342021-11-02 14:42:04 +010042 local 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
106 run_bazel build --package_path=out/soong/workspace //foo/...
107
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
117test_bp2build_generates_all_buildfiles
Lukacs T. Berkie3487c82022-05-02 10:13:19 +0200118
119function test_cc_correctness {
120 setup
121 create_mock_bazel
122
123 mkdir -p a
124 cat > a/Android.bp <<EOF
125cc_object {
126 name: "qq",
127 srcs: ["qq.cc"],
128 bazel_module: {
129 bp2build_available: true,
130 },
131 stl: "none",
132 system_shared_libs: [],
133}
134EOF
135
136 cat > a/qq.cc <<EOF
137#include "qq.h"
138int qq() {
139 return QQ;
140}
141EOF
142
143 cat > a/qq.h <<EOF
144#define QQ 1
145EOF
146
147 run_soong bp2build
148
149 run_bazel build --package_path=out/soong/workspace //a:qq
150 local output_mtime1=$(stat -c "%y" bazel-bin/a/_objs/qq/qq.o)
151
152 run_bazel build --package_path=out/soong/workspace //a:qq
153 local output_mtime2=$(stat -c "%y" bazel-bin/a/_objs/qq/qq.o)
154
155 if [[ "$output_mtime1" != "$output_mtime2" ]]; then
156 fail "output changed on null build"
157 fi
158
159 cat > a/qq.h <<EOF
160#define QQ 2
161EOF
162
163 run_bazel build --package_path=out/soong/workspace //a:qq
164 local output_mtime3=$(stat -c "%y" bazel-bin/a/_objs/qq/qq.o)
165
166 if [[ "$output_mtime1" == "$output_mtime3" ]]; then
167 fail "output not changed when included header changed"
168 fi
169}
170
171test_cc_correctness