blob: f9abcba6fc36a2be8c81a44d76eee7be09f3d7d6 [file] [log] [blame]
Liz Kammer2b8004b2021-10-04 13:55:44 -04001// 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"
Liz Kammere6583482021-10-19 13:56:10 -040020 "android/soong/genrule"
Liz Kammer2b8004b2021-10-04 13:55:44 -040021 "fmt"
22 "strings"
23 "testing"
24)
25
26const (
27 ccBinaryTypePlaceHolder = "{rule_name}"
28 compatibleWithPlaceHolder = "{target_compatible_with}"
29)
30
Liz Kammer78cfdaa2021-11-08 12:56:31 -050031type testBazelTarget struct {
32 typ string
33 name string
34 attrs attrNameToString
35}
36
37func generateBazelTargetsForTest(targets []testBazelTarget) []string {
38 ret := make([]string, 0, len(targets))
39 for _, t := range targets {
40 ret = append(ret, makeBazelTarget(t.typ, t.name, t.attrs))
41 }
42 return ret
43}
44
45type ccBinaryBp2buildTestCase struct {
46 description string
47 blueprint string
48 targets []testBazelTarget
49}
50
Liz Kammer2b8004b2021-10-04 13:55:44 -040051func registerCcBinaryModuleTypes(ctx android.RegistrationContext) {
52 cc.RegisterCCBuildComponents(ctx)
53 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
54 ctx.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory)
55 ctx.RegisterModuleType("cc_library", cc.LibraryFactory)
Liz Kammere6583482021-10-19 13:56:10 -040056 ctx.RegisterModuleType("genrule", genrule.GenRuleFactory)
Liz Kammer2b8004b2021-10-04 13:55:44 -040057}
58
Liz Kammer78cfdaa2021-11-08 12:56:31 -050059var binaryReplacer = strings.NewReplacer(ccBinaryTypePlaceHolder, "cc_binary")
60var hostBinaryReplacer = strings.NewReplacer(ccBinaryTypePlaceHolder, "cc_binary_host")
Liz Kammer2b8004b2021-10-04 13:55:44 -040061
Liz Kammer78cfdaa2021-11-08 12:56:31 -050062func runCcBinaryTests(t *testing.T, tc ccBinaryBp2buildTestCase) {
Liz Kammer2b8004b2021-10-04 13:55:44 -040063 t.Helper()
64 runCcBinaryTestCase(t, tc)
65 runCcHostBinaryTestCase(t, tc)
66}
67
Liz Kammer78cfdaa2021-11-08 12:56:31 -050068func runCcBinaryTestCase(t *testing.T, tc ccBinaryBp2buildTestCase) {
Liz Kammer2b8004b2021-10-04 13:55:44 -040069 t.Helper()
Liz Kammer78cfdaa2021-11-08 12:56:31 -050070 moduleTypeUnderTest := "cc_binary"
71 testCase := bp2buildTestCase{
72 expectedBazelTargets: generateBazelTargetsForTest(tc.targets),
73 moduleTypeUnderTest: moduleTypeUnderTest,
74 moduleTypeUnderTestFactory: cc.BinaryFactory,
75 moduleTypeUnderTestBp2BuildMutator: cc.BinaryBp2build,
76 description: fmt.Sprintf("%s %s", moduleTypeUnderTest, tc.description),
77 blueprint: binaryReplacer.Replace(tc.blueprint),
Liz Kammer2b8004b2021-10-04 13:55:44 -040078 }
79 t.Run(testCase.description, func(t *testing.T) {
80 runBp2BuildTestCase(t, registerCcBinaryModuleTypes, testCase)
81 })
82}
83
Liz Kammer78cfdaa2021-11-08 12:56:31 -050084func runCcHostBinaryTestCase(t *testing.T, tc ccBinaryBp2buildTestCase) {
Liz Kammer2b8004b2021-10-04 13:55:44 -040085 t.Helper()
86 testCase := tc
Liz Kammer78cfdaa2021-11-08 12:56:31 -050087 for i, t := range testCase.targets {
88 t.attrs["target_compatible_with"] = `select({
89 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
90 "//conditions:default": [],
91 })`
92 testCase.targets[i] = t
Liz Kammer2b8004b2021-10-04 13:55:44 -040093 }
Liz Kammer78cfdaa2021-11-08 12:56:31 -050094 moduleTypeUnderTest := "cc_binary_host"
Liz Kammer2b8004b2021-10-04 13:55:44 -040095 t.Run(testCase.description, func(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -050096 runBp2BuildTestCase(t, registerCcBinaryModuleTypes, bp2buildTestCase{
97 expectedBazelTargets: generateBazelTargetsForTest(testCase.targets),
98 moduleTypeUnderTest: moduleTypeUnderTest,
99 moduleTypeUnderTestFactory: cc.BinaryHostFactory,
100 moduleTypeUnderTestBp2BuildMutator: cc.BinaryHostBp2build,
101 description: fmt.Sprintf("%s %s", moduleTypeUnderTest, tc.description),
102 blueprint: hostBinaryReplacer.Replace(testCase.blueprint),
103 })
Liz Kammer2b8004b2021-10-04 13:55:44 -0400104 })
105}
106
107func TestBasicCcBinary(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500108 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400109 description: "basic -- properties -> attrs with little/no transformation",
110 blueprint: `
111{rule_name} {
112 name: "foo",
113 srcs: ["a.cc"],
114 local_include_dirs: ["dir"],
115 include_dirs: ["absolute_dir"],
116 cflags: ["-Dcopt"],
117 cppflags: ["-Dcppflag"],
118 conlyflags: ["-Dconlyflag"],
119 asflags: ["-Dasflag"],
120 ldflags: ["ld-flag"],
121 rtti: true,
122 strip: {
123 all: true,
124 keep_symbols: true,
125 keep_symbols_and_debug_frame: true,
126 keep_symbols_list: ["symbol"],
127 none: true,
128 },
129}
130`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500131 targets: []testBazelTarget{
132 {"cc_binary", "foo", attrNameToString{
133 "absolute_includes": `["absolute_dir"]`,
134 "asflags": `["-Dasflag"]`,
135 "conlyflags": `["-Dconlyflag"]`,
136 "copts": `["-Dcopt"]`,
137 "cppflags": `["-Dcppflag"]`,
138 "linkopts": `["ld-flag"]`,
139 "local_includes": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400140 "dir",
141 ".",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500142 ]`,
143 "rtti": `True`,
144 "srcs": `["a.cc"]`,
145 "strip": `{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400146 "all": True,
147 "keep_symbols": True,
148 "keep_symbols_and_debug_frame": True,
149 "keep_symbols_list": ["symbol"],
150 "none": True,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500151 }`,
152 },
153 },
154 },
Liz Kammer2b8004b2021-10-04 13:55:44 -0400155 })
156}
157
158func TestCcBinaryWithSharedLdflagDisableFeature(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500159 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400160 description: `ldflag "-shared" disables static_flag feature`,
161 blueprint: `
162{rule_name} {
163 name: "foo",
164 ldflags: ["-shared"],
165 include_build_directory: false,
166}
167`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500168 targets: []testBazelTarget{
169 {"cc_binary", "foo", attrNameToString{
170 "features": `["-static_flag"]`,
171 "linkopts": `["-shared"]`,
172 },
173 },
174 },
Liz Kammer2b8004b2021-10-04 13:55:44 -0400175 })
176}
177
178func TestCcBinaryWithLinkStatic(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500179 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400180 description: "link static",
181 blueprint: `
182{rule_name} {
183 name: "foo",
184 static_executable: true,
185 include_build_directory: false,
186}
187`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500188 targets: []testBazelTarget{
189 {"cc_binary", "foo", attrNameToString{
190 "linkshared": `False`,
191 },
192 },
193 },
Liz Kammer2b8004b2021-10-04 13:55:44 -0400194 })
195}
196
197func TestCcBinaryVersionScript(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500198 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400199 description: `version script`,
200 blueprint: `
201{rule_name} {
202 name: "foo",
203 include_build_directory: false,
204 version_script: "vs",
205}
206`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500207 targets: []testBazelTarget{
208 {"cc_binary", "foo", attrNameToString{
209 "additional_linker_inputs": `["vs"]`,
210 "linkopts": `["-Wl,--version-script,$(location vs)"]`,
211 },
212 },
213 },
Liz Kammer2b8004b2021-10-04 13:55:44 -0400214 })
215}
216
217func TestCcBinarySplitSrcsByLang(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500218 runCcHostBinaryTestCase(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400219 description: "split srcs by lang",
220 blueprint: `
221{rule_name} {
222 name: "foo",
223 srcs: [
224 "asonly.S",
225 "conly.c",
226 "cpponly.cpp",
227 ":fg_foo",
228 ],
229 include_build_directory: false,
230}
231` + simpleModuleDoNotConvertBp2build("filegroup", "fg_foo"),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500232 targets: []testBazelTarget{
233 {"cc_binary", "foo", attrNameToString{
234 "srcs": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400235 "cpponly.cpp",
236 ":fg_foo_cpp_srcs",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500237 ]`,
238 "srcs_as": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400239 "asonly.S",
240 ":fg_foo_as_srcs",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500241 ]`,
242 "srcs_c": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400243 "conly.c",
244 ":fg_foo_c_srcs",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500245 ]`,
246 },
247 },
248 },
Liz Kammer2b8004b2021-10-04 13:55:44 -0400249 })
250}
251
252func TestCcBinaryDoNotDistinguishBetweenDepsAndImplementationDeps(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500253 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400254 description: "no implementation deps",
255 blueprint: `
Liz Kammere6583482021-10-19 13:56:10 -0400256genrule {
257 name: "generated_hdr",
258 cmd: "nothing to see here",
259}
260
261genrule {
262 name: "export_generated_hdr",
263 cmd: "nothing to see here",
264}
265
Liz Kammer2b8004b2021-10-04 13:55:44 -0400266{rule_name} {
267 name: "foo",
Liz Kammere6583482021-10-19 13:56:10 -0400268 srcs: ["foo.cpp"],
Liz Kammer2b8004b2021-10-04 13:55:44 -0400269 shared_libs: ["implementation_shared_dep", "shared_dep"],
270 export_shared_lib_headers: ["shared_dep"],
271 static_libs: ["implementation_static_dep", "static_dep"],
272 export_static_lib_headers: ["static_dep", "whole_static_dep"],
273 whole_static_libs: ["not_explicitly_exported_whole_static_dep", "whole_static_dep"],
274 include_build_directory: false,
Liz Kammere6583482021-10-19 13:56:10 -0400275 generated_headers: ["generated_hdr", "export_generated_hdr"],
276 export_generated_headers: ["export_generated_hdr"],
Liz Kammer2b8004b2021-10-04 13:55:44 -0400277}
278` +
279 simpleModuleDoNotConvertBp2build("cc_library_static", "static_dep") +
280 simpleModuleDoNotConvertBp2build("cc_library_static", "implementation_static_dep") +
281 simpleModuleDoNotConvertBp2build("cc_library_static", "whole_static_dep") +
282 simpleModuleDoNotConvertBp2build("cc_library_static", "not_explicitly_exported_whole_static_dep") +
283 simpleModuleDoNotConvertBp2build("cc_library", "shared_dep") +
284 simpleModuleDoNotConvertBp2build("cc_library", "implementation_shared_dep"),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500285 targets: []testBazelTarget{
286 {"cc_binary", "foo", attrNameToString{
287 "deps": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400288 ":implementation_static_dep",
289 ":static_dep",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500290 ]`,
291 "dynamic_deps": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400292 ":implementation_shared_dep",
293 ":shared_dep",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500294 ]`,
295 "srcs": `[
Liz Kammere6583482021-10-19 13:56:10 -0400296 "foo.cpp",
297 ":generated_hdr",
298 ":export_generated_hdr",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500299 ]`,
300 "whole_archive_deps": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400301 ":not_explicitly_exported_whole_static_dep",
302 ":whole_static_dep",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500303 ]`,
304 },
305 },
306 },
Liz Kammer2b8004b2021-10-04 13:55:44 -0400307 })
308}
309
310func TestCcBinaryNocrtTests(t *testing.T) {
311 baseTestCases := []struct {
312 description string
313 soongProperty string
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500314 bazelAttr attrNameToString
Liz Kammer2b8004b2021-10-04 13:55:44 -0400315 }{
316 {
317 description: "nocrt: true",
318 soongProperty: `nocrt: true,`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500319 bazelAttr: attrNameToString{"link_crt": `False`},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400320 },
321 {
322 description: "nocrt: false",
323 soongProperty: `nocrt: false,`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500324 bazelAttr: attrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400325 },
326 {
327 description: "nocrt: not set",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500328 bazelAttr: attrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400329 },
330 }
331
332 baseBlueprint := `{rule_name} {
333 name: "foo",%s
334 include_build_directory: false,
335}
336`
337
Liz Kammer2b8004b2021-10-04 13:55:44 -0400338 for _, btc := range baseTestCases {
339 prop := btc.soongProperty
340 if len(prop) > 0 {
341 prop = "\n" + prop
342 }
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500343 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400344 description: btc.description,
345 blueprint: fmt.Sprintf(baseBlueprint, prop),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500346 targets: []testBazelTarget{
347 {"cc_binary", "foo", btc.bazelAttr},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400348 },
349 })
350 }
351}
352
353func TestCcBinaryNo_libcrtTests(t *testing.T) {
354 baseTestCases := []struct {
355 description string
356 soongProperty string
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500357 bazelAttr attrNameToString
Liz Kammer2b8004b2021-10-04 13:55:44 -0400358 }{
359 {
360 description: "no_libcrt: true",
361 soongProperty: `no_libcrt: true,`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500362 bazelAttr: attrNameToString{"use_libcrt": `False`},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400363 },
364 {
365 description: "no_libcrt: false",
366 soongProperty: `no_libcrt: false,`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500367 bazelAttr: attrNameToString{"use_libcrt": `True`},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400368 },
369 {
370 description: "no_libcrt: not set",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500371 bazelAttr: attrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400372 },
373 }
374
375 baseBlueprint := `{rule_name} {
376 name: "foo",%s
377 include_build_directory: false,
378}
379`
380
Liz Kammer2b8004b2021-10-04 13:55:44 -0400381 for _, btc := range baseTestCases {
382 prop := btc.soongProperty
383 if len(prop) > 0 {
384 prop = "\n" + prop
385 }
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500386 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400387 description: btc.description,
388 blueprint: fmt.Sprintf(baseBlueprint, prop),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500389 targets: []testBazelTarget{
390 {"cc_binary", "foo", btc.bazelAttr},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400391 },
392 })
393 }
394}
395
396func TestCcBinaryPropertiesToFeatures(t *testing.T) {
397 baseTestCases := []struct {
398 description string
399 soongProperty string
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500400 bazelAttr attrNameToString
Liz Kammer2b8004b2021-10-04 13:55:44 -0400401 }{
402 {
403 description: "pack_relocation: true",
404 soongProperty: `pack_relocations: true,`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500405 bazelAttr: attrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400406 },
407 {
408 description: "pack_relocations: false",
409 soongProperty: `pack_relocations: false,`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500410 bazelAttr: attrNameToString{"features": `["disable_pack_relocations"]`},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400411 },
412 {
413 description: "pack_relocations: not set",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500414 bazelAttr: attrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400415 },
416 {
417 description: "pack_relocation: true",
418 soongProperty: `allow_undefined_symbols: true,`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500419 bazelAttr: attrNameToString{"features": `["-no_undefined_symbols"]`},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400420 },
421 {
422 description: "allow_undefined_symbols: false",
423 soongProperty: `allow_undefined_symbols: false,`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500424 bazelAttr: attrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400425 },
426 {
427 description: "allow_undefined_symbols: not set",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500428 bazelAttr: attrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400429 },
430 }
431
432 baseBlueprint := `{rule_name} {
433 name: "foo",%s
434 include_build_directory: false,
435}
436`
Liz Kammer2b8004b2021-10-04 13:55:44 -0400437 for _, btc := range baseTestCases {
438 prop := btc.soongProperty
439 if len(prop) > 0 {
440 prop = "\n" + prop
441 }
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500442 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400443 description: btc.description,
444 blueprint: fmt.Sprintf(baseBlueprint, prop),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500445 targets: []testBazelTarget{
446 {"cc_binary", "foo", btc.bazelAttr},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400447 },
448 })
449 }
450}