blob: 1f69b5add260540df7d7a1691090f5c5e7e194e1 [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 (
Liz Kammer2b8004b2021-10-04 13:55:44 -040018 "fmt"
19 "strings"
20 "testing"
Yu Liufc603162022-03-01 15:44:08 -080021
22 "android/soong/android"
23 "android/soong/cc"
24 "android/soong/genrule"
Liz Kammer2b8004b2021-10-04 13:55:44 -040025)
26
27const (
Liz Kammer12615db2021-09-28 09:19:17 -040028 ccBinaryTypePlaceHolder = "{rule_name}"
Liz Kammer2b8004b2021-10-04 13:55:44 -040029)
30
Liz Kammer78cfdaa2021-11-08 12:56:31 -050031type testBazelTarget struct {
32 typ string
33 name string
Sam Delmerico3177a6e2022-06-21 19:28:33 +000034 attrs AttrNameToString
Liz Kammer78cfdaa2021-11-08 12:56:31 -050035}
36
Liz Kammerdfeb1202022-05-13 17:20:20 -040037func generateBazelTargetsForTest(targets []testBazelTarget, hod android.HostOrDeviceSupported) []string {
Liz Kammer78cfdaa2021-11-08 12:56:31 -050038 ret := make([]string, 0, len(targets))
39 for _, t := range targets {
Liz Kammerdfeb1202022-05-13 17:20:20 -040040 attrs := t.attrs.clone()
41 ret = append(ret, makeBazelTargetHostOrDevice(t.typ, t.name, attrs, hod))
Liz Kammer78cfdaa2021-11-08 12:56:31 -050042 }
43 return ret
44}
45
46type ccBinaryBp2buildTestCase struct {
47 description string
48 blueprint string
49 targets []testBazelTarget
50}
51
Liz Kammer2b8004b2021-10-04 13:55:44 -040052func registerCcBinaryModuleTypes(ctx android.RegistrationContext) {
53 cc.RegisterCCBuildComponents(ctx)
54 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
55 ctx.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory)
56 ctx.RegisterModuleType("cc_library", cc.LibraryFactory)
Liz Kammere6583482021-10-19 13:56:10 -040057 ctx.RegisterModuleType("genrule", genrule.GenRuleFactory)
Liz Kammer2b8004b2021-10-04 13:55:44 -040058}
59
Liz Kammer78cfdaa2021-11-08 12:56:31 -050060var binaryReplacer = strings.NewReplacer(ccBinaryTypePlaceHolder, "cc_binary")
61var hostBinaryReplacer = strings.NewReplacer(ccBinaryTypePlaceHolder, "cc_binary_host")
Liz Kammer2b8004b2021-10-04 13:55:44 -040062
Liz Kammer78cfdaa2021-11-08 12:56:31 -050063func runCcBinaryTests(t *testing.T, tc ccBinaryBp2buildTestCase) {
Liz Kammer2b8004b2021-10-04 13:55:44 -040064 t.Helper()
65 runCcBinaryTestCase(t, tc)
66 runCcHostBinaryTestCase(t, tc)
67}
68
Liz Kammerdfeb1202022-05-13 17:20:20 -040069func runCcBinaryTestCase(t *testing.T, testCase ccBinaryBp2buildTestCase) {
Liz Kammer2b8004b2021-10-04 13:55:44 -040070 t.Helper()
Liz Kammer78cfdaa2021-11-08 12:56:31 -050071 moduleTypeUnderTest := "cc_binary"
Liz Kammerdfeb1202022-05-13 17:20:20 -040072
73 description := fmt.Sprintf("%s %s", moduleTypeUnderTest, testCase.description)
74 t.Run(description, func(t *testing.T) {
Liz Kammerbe46fcc2021-11-01 15:32:43 -040075 t.Helper()
Sam Delmerico3177a6e2022-06-21 19:28:33 +000076 RunBp2BuildTestCase(t, registerCcBinaryModuleTypes, Bp2buildTestCase{
77 ExpectedBazelTargets: generateBazelTargetsForTest(testCase.targets, android.DeviceSupported),
78 ModuleTypeUnderTest: moduleTypeUnderTest,
79 ModuleTypeUnderTestFactory: cc.BinaryFactory,
80 Description: description,
81 Blueprint: binaryReplacer.Replace(testCase.blueprint),
Liz Kammerdfeb1202022-05-13 17:20:20 -040082 })
Liz Kammer2b8004b2021-10-04 13:55:44 -040083 })
84}
85
Liz Kammerdfeb1202022-05-13 17:20:20 -040086func runCcHostBinaryTestCase(t *testing.T, testCase ccBinaryBp2buildTestCase) {
Liz Kammer2b8004b2021-10-04 13:55:44 -040087 t.Helper()
Liz Kammer78cfdaa2021-11-08 12:56:31 -050088 moduleTypeUnderTest := "cc_binary_host"
Liz Kammerdfeb1202022-05-13 17:20:20 -040089 description := fmt.Sprintf("%s %s", moduleTypeUnderTest, testCase.description)
90 t.Run(description, func(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +000091 RunBp2BuildTestCase(t, registerCcBinaryModuleTypes, Bp2buildTestCase{
92 ExpectedBazelTargets: generateBazelTargetsForTest(testCase.targets, android.HostSupported),
93 ModuleTypeUnderTest: moduleTypeUnderTest,
94 ModuleTypeUnderTestFactory: cc.BinaryHostFactory,
95 Description: description,
96 Blueprint: hostBinaryReplacer.Replace(testCase.blueprint),
Liz Kammer78cfdaa2021-11-08 12:56:31 -050097 })
Liz Kammer2b8004b2021-10-04 13:55:44 -040098 })
99}
100
101func TestBasicCcBinary(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500102 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400103 description: "basic -- properties -> attrs with little/no transformation",
104 blueprint: `
105{rule_name} {
106 name: "foo",
107 srcs: ["a.cc"],
108 local_include_dirs: ["dir"],
109 include_dirs: ["absolute_dir"],
110 cflags: ["-Dcopt"],
111 cppflags: ["-Dcppflag"],
112 conlyflags: ["-Dconlyflag"],
113 asflags: ["-Dasflag"],
114 ldflags: ["ld-flag"],
115 rtti: true,
116 strip: {
117 all: true,
118 keep_symbols: true,
119 keep_symbols_and_debug_frame: true,
120 keep_symbols_list: ["symbol"],
121 none: true,
122 },
Yu Liufc603162022-03-01 15:44:08 -0800123 sdk_version: "current",
124 min_sdk_version: "29",
Yu Liua79c9462022-03-22 16:35:22 -0700125 use_version_lib: true,
Liz Kammer2b8004b2021-10-04 13:55:44 -0400126}
127`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500128 targets: []testBazelTarget{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000129 {"cc_binary", "foo", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500130 "absolute_includes": `["absolute_dir"]`,
131 "asflags": `["-Dasflag"]`,
132 "conlyflags": `["-Dconlyflag"]`,
133 "copts": `["-Dcopt"]`,
134 "cppflags": `["-Dcppflag"]`,
135 "linkopts": `["ld-flag"]`,
136 "local_includes": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400137 "dir",
138 ".",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500139 ]`,
140 "rtti": `True`,
141 "srcs": `["a.cc"]`,
142 "strip": `{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400143 "all": True,
144 "keep_symbols": True,
145 "keep_symbols_and_debug_frame": True,
146 "keep_symbols_list": ["symbol"],
147 "none": True,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500148 }`,
Yu Liua79c9462022-03-22 16:35:22 -0700149 "sdk_version": `"current"`,
150 "min_sdk_version": `"29"`,
151 "use_version_lib": `True`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500152 },
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{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000169 {"cc_binary", "foo", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500170 "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{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000189 {"cc_binary", "foo", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500190 "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{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000208 {"cc_binary", "foo", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500209 "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{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000233 {"cc_binary", "foo", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500234 "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",
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400259 bazel_module: { bp2build_available: false },
Liz Kammere6583482021-10-19 13:56:10 -0400260}
261
262genrule {
263 name: "export_generated_hdr",
264 cmd: "nothing to see here",
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400265 bazel_module: { bp2build_available: false },
Liz Kammere6583482021-10-19 13:56:10 -0400266}
267
Liz Kammer2b8004b2021-10-04 13:55:44 -0400268{rule_name} {
269 name: "foo",
Liz Kammere6583482021-10-19 13:56:10 -0400270 srcs: ["foo.cpp"],
Liz Kammer2b8004b2021-10-04 13:55:44 -0400271 shared_libs: ["implementation_shared_dep", "shared_dep"],
272 export_shared_lib_headers: ["shared_dep"],
273 static_libs: ["implementation_static_dep", "static_dep"],
274 export_static_lib_headers: ["static_dep", "whole_static_dep"],
275 whole_static_libs: ["not_explicitly_exported_whole_static_dep", "whole_static_dep"],
276 include_build_directory: false,
Liz Kammere6583482021-10-19 13:56:10 -0400277 generated_headers: ["generated_hdr", "export_generated_hdr"],
278 export_generated_headers: ["export_generated_hdr"],
Liz Kammer2b8004b2021-10-04 13:55:44 -0400279}
280` +
281 simpleModuleDoNotConvertBp2build("cc_library_static", "static_dep") +
282 simpleModuleDoNotConvertBp2build("cc_library_static", "implementation_static_dep") +
283 simpleModuleDoNotConvertBp2build("cc_library_static", "whole_static_dep") +
284 simpleModuleDoNotConvertBp2build("cc_library_static", "not_explicitly_exported_whole_static_dep") +
285 simpleModuleDoNotConvertBp2build("cc_library", "shared_dep") +
286 simpleModuleDoNotConvertBp2build("cc_library", "implementation_shared_dep"),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500287 targets: []testBazelTarget{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000288 {"cc_binary", "foo", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500289 "deps": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400290 ":implementation_static_dep",
291 ":static_dep",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500292 ]`,
293 "dynamic_deps": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400294 ":implementation_shared_dep",
295 ":shared_dep",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500296 ]`,
297 "srcs": `[
Liz Kammere6583482021-10-19 13:56:10 -0400298 "foo.cpp",
299 ":generated_hdr",
300 ":export_generated_hdr",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500301 ]`,
302 "whole_archive_deps": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400303 ":not_explicitly_exported_whole_static_dep",
304 ":whole_static_dep",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500305 ]`,
Liz Kammer1263d9b2021-12-10 14:28:20 -0500306 "local_includes": `["."]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500307 },
308 },
309 },
Liz Kammer2b8004b2021-10-04 13:55:44 -0400310 })
311}
312
313func TestCcBinaryNocrtTests(t *testing.T) {
314 baseTestCases := []struct {
315 description string
316 soongProperty string
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000317 bazelAttr AttrNameToString
Liz Kammer2b8004b2021-10-04 13:55:44 -0400318 }{
319 {
320 description: "nocrt: true",
321 soongProperty: `nocrt: true,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000322 bazelAttr: AttrNameToString{"link_crt": `False`},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400323 },
324 {
325 description: "nocrt: false",
326 soongProperty: `nocrt: false,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000327 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400328 },
329 {
330 description: "nocrt: not set",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000331 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400332 },
333 }
334
335 baseBlueprint := `{rule_name} {
336 name: "foo",%s
337 include_build_directory: false,
338}
339`
340
Liz Kammer2b8004b2021-10-04 13:55:44 -0400341 for _, btc := range baseTestCases {
342 prop := btc.soongProperty
343 if len(prop) > 0 {
344 prop = "\n" + prop
345 }
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500346 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400347 description: btc.description,
348 blueprint: fmt.Sprintf(baseBlueprint, prop),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500349 targets: []testBazelTarget{
350 {"cc_binary", "foo", btc.bazelAttr},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400351 },
352 })
353 }
354}
355
356func TestCcBinaryNo_libcrtTests(t *testing.T) {
357 baseTestCases := []struct {
358 description string
359 soongProperty string
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000360 bazelAttr AttrNameToString
Liz Kammer2b8004b2021-10-04 13:55:44 -0400361 }{
362 {
363 description: "no_libcrt: true",
364 soongProperty: `no_libcrt: true,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000365 bazelAttr: AttrNameToString{"use_libcrt": `False`},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400366 },
367 {
368 description: "no_libcrt: false",
369 soongProperty: `no_libcrt: false,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000370 bazelAttr: AttrNameToString{"use_libcrt": `True`},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400371 },
372 {
373 description: "no_libcrt: not set",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000374 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400375 },
376 }
377
378 baseBlueprint := `{rule_name} {
379 name: "foo",%s
380 include_build_directory: false,
381}
382`
383
Liz Kammer2b8004b2021-10-04 13:55:44 -0400384 for _, btc := range baseTestCases {
385 prop := btc.soongProperty
386 if len(prop) > 0 {
387 prop = "\n" + prop
388 }
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500389 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400390 description: btc.description,
391 blueprint: fmt.Sprintf(baseBlueprint, prop),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500392 targets: []testBazelTarget{
393 {"cc_binary", "foo", btc.bazelAttr},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400394 },
395 })
396 }
397}
398
399func TestCcBinaryPropertiesToFeatures(t *testing.T) {
400 baseTestCases := []struct {
401 description string
402 soongProperty string
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000403 bazelAttr AttrNameToString
Liz Kammer2b8004b2021-10-04 13:55:44 -0400404 }{
405 {
406 description: "pack_relocation: true",
407 soongProperty: `pack_relocations: true,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000408 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400409 },
410 {
411 description: "pack_relocations: false",
412 soongProperty: `pack_relocations: false,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000413 bazelAttr: AttrNameToString{"features": `["disable_pack_relocations"]`},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400414 },
415 {
416 description: "pack_relocations: not set",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000417 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400418 },
419 {
420 description: "pack_relocation: true",
421 soongProperty: `allow_undefined_symbols: true,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000422 bazelAttr: AttrNameToString{"features": `["-no_undefined_symbols"]`},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400423 },
424 {
425 description: "allow_undefined_symbols: false",
426 soongProperty: `allow_undefined_symbols: false,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000427 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400428 },
429 {
430 description: "allow_undefined_symbols: not set",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000431 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400432 },
433 }
434
435 baseBlueprint := `{rule_name} {
436 name: "foo",%s
437 include_build_directory: false,
438}
439`
Liz Kammer2b8004b2021-10-04 13:55:44 -0400440 for _, btc := range baseTestCases {
441 prop := btc.soongProperty
442 if len(prop) > 0 {
443 prop = "\n" + prop
444 }
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500445 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400446 description: btc.description,
447 blueprint: fmt.Sprintf(baseBlueprint, prop),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500448 targets: []testBazelTarget{
449 {"cc_binary", "foo", btc.bazelAttr},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400450 },
451 })
452 }
453}
Liz Kammer12615db2021-09-28 09:19:17 -0400454
455func TestCcBinarySharedProto(t *testing.T) {
456 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
457 blueprint: soongCcProtoLibraries + `{rule_name} {
458 name: "foo",
459 srcs: ["foo.proto"],
460 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -0400461 },
462 include_build_directory: false,
463}`,
464 targets: []testBazelTarget{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000465 {"proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400466 "srcs": `["foo.proto"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000467 }}, {"cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400468 "deps": `[":foo_proto"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000469 }}, {"cc_binary", "foo", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400470 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
471 "whole_archive_deps": `[":foo_cc_proto_lite"]`,
472 }},
473 },
474 })
475}
476
477func TestCcBinaryStaticProto(t *testing.T) {
478 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
479 blueprint: soongCcProtoLibraries + `{rule_name} {
480 name: "foo",
481 srcs: ["foo.proto"],
482 static_executable: true,
483 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -0400484 },
485 include_build_directory: false,
486}`,
487 targets: []testBazelTarget{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000488 {"proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400489 "srcs": `["foo.proto"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000490 }}, {"cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400491 "deps": `[":foo_proto"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000492 }}, {"cc_binary", "foo", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400493 "deps": `[":libprotobuf-cpp-lite"]`,
494 "whole_archive_deps": `[":foo_cc_proto_lite"]`,
495 "linkshared": `False`,
496 }},
497 },
498 })
499}
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000500
501func TestCcBinaryConvertLex(t *testing.T) {
502 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
503 description: `.l and .ll sources converted to .c and .cc`,
504 blueprint: `
505{rule_name} {
506 name: "foo",
507 srcs: ["foo.c", "bar.cc", "foo1.l", "foo2.l", "bar1.ll", "bar2.ll"],
508 lex: { flags: ["--foo_opt", "--bar_opt"] },
509 include_build_directory: false,
510}
511`,
512 targets: []testBazelTarget{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000513 {"genlex", "foo_genlex_l", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000514 "srcs": `[
515 "foo1.l",
516 "foo2.l",
517 ]`,
518 "lexopts": `[
519 "--foo_opt",
520 "--bar_opt",
521 ]`,
522 }},
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000523 {"genlex", "foo_genlex_ll", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000524 "srcs": `[
525 "bar1.ll",
526 "bar2.ll",
527 ]`,
528 "lexopts": `[
529 "--foo_opt",
530 "--bar_opt",
531 ]`,
532 }},
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000533 {"cc_binary", "foo", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000534 "srcs": `[
535 "bar.cc",
536 ":foo_genlex_ll",
537 ]`,
538 "srcs_c": `[
539 "foo.c",
540 ":foo_genlex_l",
541 ]`,
542 }},
543 },
544 })
545}
Cole Faust6b29f592022-08-09 09:50:56 -0700546
547func TestCcBinaryRuntimeLibs(t *testing.T) {
548 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
549 description: "cc_binary with runtime libs",
550 blueprint: `
551cc_library {
552 name: "bar",
553 srcs: ["b.cc"],
554}
555
556{rule_name} {
557 name: "foo",
558 srcs: ["a.cc"],
559 runtime_libs: ["bar"],
560}
561`,
562 targets: []testBazelTarget{
563 {"cc_library_static", "bar_bp2build_cc_library_static", AttrNameToString{
564 "local_includes": `["."]`,
565 "srcs": `["b.cc"]`,
566 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
567 },
568 },
569 {"cc_library_shared", "bar", AttrNameToString{
570 "local_includes": `["."]`,
571 "srcs": `["b.cc"]`,
572 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
573 },
574 },
575 {"cc_binary", "foo", AttrNameToString{
576 "local_includes": `["."]`,
577 "srcs": `["a.cc"]`,
578 "runtime_deps": `[":bar"]`,
579 },
580 },
581 },
582 })
583}