blob: 467b0b286043286fce0e82ad4823f56630282ab5 [file] [log] [blame]
Rupert Shuttleworth095081c2021-03-25 09:06:03 +00001// Copyright 2021 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package bp2build
16
17import (
18 "android/soong/android"
19 "android/soong/cc"
20 "strings"
21 "testing"
22)
23
24const (
25 // See cc/testing.go for more context
26 soongCcLibraryStaticPreamble = `
27cc_defaults {
28 name: "linux_bionic_supported",
29}
30
31toolchain_library {
32 name: "libclang_rt.builtins-x86_64-android",
33 defaults: ["linux_bionic_supported"],
34 vendor_available: true,
35 vendor_ramdisk_available: true,
36 product_available: true,
37 recovery_available: true,
38 native_bridge_supported: true,
39 src: "",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +000040}`
41)
42
43func TestCcLibraryStaticLoadStatement(t *testing.T) {
44 testCases := []struct {
45 bazelTargets BazelTargets
46 expectedLoadStatements string
47 }{
48 {
49 bazelTargets: BazelTargets{
50 BazelTarget{
51 name: "cc_library_static_target",
52 ruleClass: "cc_library_static",
53 // NOTE: No bzlLoadLocation for native rules
54 },
55 },
56 expectedLoadStatements: ``,
57 },
58 }
59
60 for _, testCase := range testCases {
61 actual := testCase.bazelTargets.LoadStatements()
62 expected := testCase.expectedLoadStatements
63 if actual != expected {
64 t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
65 }
66 }
67
68}
69
70func TestCcLibraryStaticBp2Build(t *testing.T) {
71 testCases := []struct {
72 description string
73 moduleTypeUnderTest string
74 moduleTypeUnderTestFactory android.ModuleFactory
75 moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
76 preArchMutators []android.RegisterMutatorFunc
77 depsMutators []android.RegisterMutatorFunc
78 bp string
79 expectedBazelTargets []string
80 filesystem map[string]string
81 dir string
82 }{
83 {
84 description: "cc_library_static test",
85 moduleTypeUnderTest: "cc_library_static",
86 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
87 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
88 filesystem: map[string]string{
89 // NOTE: include_dir headers *should not* appear in Bazel hdrs later (?)
90 "include_dir_1/include_dir_1_a.h": "",
91 "include_dir_1/include_dir_1_b.h": "",
92 "include_dir_2/include_dir_2_a.h": "",
93 "include_dir_2/include_dir_2_b.h": "",
94 // NOTE: local_include_dir headers *should not* appear in Bazel hdrs later (?)
95 "local_include_dir_1/local_include_dir_1_a.h": "",
96 "local_include_dir_1/local_include_dir_1_b.h": "",
97 "local_include_dir_2/local_include_dir_2_a.h": "",
98 "local_include_dir_2/local_include_dir_2_b.h": "",
99 // NOTE: export_include_dir headers *should* appear in Bazel hdrs later
100 "export_include_dir_1/export_include_dir_1_a.h": "",
101 "export_include_dir_1/export_include_dir_1_b.h": "",
102 "export_include_dir_2/export_include_dir_2_a.h": "",
103 "export_include_dir_2/export_include_dir_2_b.h": "",
104 },
105 bp: soongCcLibraryStaticPreamble + `
106cc_library_headers {
107 name: "header_lib_1",
108 export_include_dirs: ["header_lib_1"],
109}
110
111cc_library_headers {
112 name: "header_lib_2",
113 export_include_dirs: ["header_lib_2"],
114}
115
116cc_library_static {
117 name: "static_lib_1",
118 srcs: ["static_lib_1.cc"],
119 bazel_module: { bp2build_available: true },
120}
121
122cc_library_static {
123 name: "static_lib_2",
124 srcs: ["static_lib_2.cc"],
125 bazel_module: { bp2build_available: true },
126}
127
128cc_library_static {
129 name: "whole_static_lib_1",
130 srcs: ["whole_static_lib_1.cc"],
131 bazel_module: { bp2build_available: true },
132}
133
134cc_library_static {
135 name: "whole_static_lib_2",
136 srcs: ["whole_static_lib_2.cc"],
137 bazel_module: { bp2build_available: true },
138}
139
140cc_library_static {
141 name: "foo_static",
142 srcs: [
143 "foo_static1.cc",
144 "foo_static2.cc",
145 ],
146 cflags: [
147 "-Dflag1",
148 "-Dflag2"
149 ],
150 static_libs: [
151 "static_lib_1",
152 "static_lib_2"
153 ],
154 whole_static_libs: [
155 "whole_static_lib_1",
156 "whole_static_lib_2"
157 ],
158 include_dirs: [
159 "include_dir_1",
160 "include_dir_2",
161 ],
162 local_include_dirs: [
163 "local_include_dir_1",
164 "local_include_dir_2",
165 ],
166 export_include_dirs: [
167 "export_include_dir_1",
168 "export_include_dir_2"
169 ],
170 header_libs: [
171 "header_lib_1",
172 "header_lib_2"
173 ],
174
175 // TODO: Also support export_header_lib_headers
176
177 bazel_module: { bp2build_available: true },
178}`,
179 expectedBazelTargets: []string{`cc_library_static(
180 name = "foo_static",
181 copts = [
182 "-Dflag1",
183 "-Dflag2",
184 ],
185 deps = [
186 ":header_lib_1",
187 ":header_lib_2",
188 ":static_lib_1",
189 ":static_lib_2",
190 ":whole_static_lib_1",
191 ":whole_static_lib_2",
192 ],
193 hdrs = [
194 "export_include_dir_1/export_include_dir_1_a.h",
195 "export_include_dir_1/export_include_dir_1_b.h",
196 "export_include_dir_2/export_include_dir_2_a.h",
197 "export_include_dir_2/export_include_dir_2_b.h",
198 ],
199 includes = [
200 "export_include_dir_1",
201 "export_include_dir_2",
202 "include_dir_1",
203 "include_dir_2",
204 "local_include_dir_1",
205 "local_include_dir_2",
206 ],
207 linkstatic = True,
208 srcs = [
209 "foo_static1.cc",
210 "foo_static2.cc",
211 ],
212)`, `cc_library_static(
213 name = "static_lib_1",
214 linkstatic = True,
215 srcs = [
216 "static_lib_1.cc",
217 ],
218)`, `cc_library_static(
219 name = "static_lib_2",
220 linkstatic = True,
221 srcs = [
222 "static_lib_2.cc",
223 ],
224)`, `cc_library_static(
225 name = "whole_static_lib_1",
226 linkstatic = True,
227 srcs = [
228 "whole_static_lib_1.cc",
229 ],
230)`, `cc_library_static(
231 name = "whole_static_lib_2",
232 linkstatic = True,
233 srcs = [
234 "whole_static_lib_2.cc",
235 ],
236)`},
237 },
238 }
239
240 dir := "."
241 for _, testCase := range testCases {
242 filesystem := make(map[string][]byte)
243 toParse := []string{
244 "Android.bp",
245 }
246 for f, content := range testCase.filesystem {
247 if strings.HasSuffix(f, "Android.bp") {
248 toParse = append(toParse, f)
249 }
250 filesystem[f] = []byte(content)
251 }
252 config := android.TestConfig(buildDir, nil, testCase.bp, filesystem)
253 ctx := android.NewTestContext(config)
254
255 cc.RegisterCCBuildComponents(ctx)
256 ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
257 ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
258
259 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
260 for _, m := range testCase.depsMutators {
261 ctx.DepsBp2BuildMutators(m)
262 }
263 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
264 ctx.RegisterForBazelConversion()
265
266 _, errs := ctx.ParseFileList(dir, toParse)
267 if Errored(t, testCase.description, errs) {
268 continue
269 }
270 _, errs = ctx.ResolveDependencies(config)
271 if Errored(t, testCase.description, errs) {
272 continue
273 }
274
275 checkDir := dir
276 if testCase.dir != "" {
277 checkDir = testCase.dir
278 }
279 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
280 bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir)
281 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
282 t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount)
283 } else {
284 for i, target := range bazelTargets {
285 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
286 t.Errorf(
287 "%s: Expected generated Bazel target to be '%s', got '%s'",
288 testCase.description,
289 w,
290 g,
291 )
292 }
293 }
294 }
295 }
296}