blob: c23779e24989883b28e9857dcd98292abffe650d [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
Liz Kammerbaced712022-09-16 09:01:29 -040048 filesystem map[string]string
Liz Kammer78cfdaa2021-11-08 12:56:31 -050049 blueprint string
50 targets []testBazelTarget
51}
52
Liz Kammer2b8004b2021-10-04 13:55:44 -040053func registerCcBinaryModuleTypes(ctx android.RegistrationContext) {
54 cc.RegisterCCBuildComponents(ctx)
55 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
56 ctx.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory)
57 ctx.RegisterModuleType("cc_library", cc.LibraryFactory)
Liz Kammere6583482021-10-19 13:56:10 -040058 ctx.RegisterModuleType("genrule", genrule.GenRuleFactory)
Liz Kammer2b8004b2021-10-04 13:55:44 -040059}
60
Liz Kammer78cfdaa2021-11-08 12:56:31 -050061var binaryReplacer = strings.NewReplacer(ccBinaryTypePlaceHolder, "cc_binary")
62var hostBinaryReplacer = strings.NewReplacer(ccBinaryTypePlaceHolder, "cc_binary_host")
Liz Kammer2b8004b2021-10-04 13:55:44 -040063
Liz Kammer78cfdaa2021-11-08 12:56:31 -050064func runCcBinaryTests(t *testing.T, tc ccBinaryBp2buildTestCase) {
Liz Kammer2b8004b2021-10-04 13:55:44 -040065 t.Helper()
66 runCcBinaryTestCase(t, tc)
67 runCcHostBinaryTestCase(t, tc)
68}
69
Liz Kammerdfeb1202022-05-13 17:20:20 -040070func runCcBinaryTestCase(t *testing.T, testCase ccBinaryBp2buildTestCase) {
Liz Kammer2b8004b2021-10-04 13:55:44 -040071 t.Helper()
Liz Kammer78cfdaa2021-11-08 12:56:31 -050072 moduleTypeUnderTest := "cc_binary"
Liz Kammerdfeb1202022-05-13 17:20:20 -040073
74 description := fmt.Sprintf("%s %s", moduleTypeUnderTest, testCase.description)
75 t.Run(description, func(t *testing.T) {
Liz Kammerbe46fcc2021-11-01 15:32:43 -040076 t.Helper()
Sam Delmerico3177a6e2022-06-21 19:28:33 +000077 RunBp2BuildTestCase(t, registerCcBinaryModuleTypes, Bp2buildTestCase{
78 ExpectedBazelTargets: generateBazelTargetsForTest(testCase.targets, android.DeviceSupported),
79 ModuleTypeUnderTest: moduleTypeUnderTest,
80 ModuleTypeUnderTestFactory: cc.BinaryFactory,
81 Description: description,
82 Blueprint: binaryReplacer.Replace(testCase.blueprint),
Liz Kammerbaced712022-09-16 09:01:29 -040083 Filesystem: testCase.filesystem,
Liz Kammerdfeb1202022-05-13 17:20:20 -040084 })
Liz Kammer2b8004b2021-10-04 13:55:44 -040085 })
86}
87
Liz Kammerdfeb1202022-05-13 17:20:20 -040088func runCcHostBinaryTestCase(t *testing.T, testCase ccBinaryBp2buildTestCase) {
Liz Kammer2b8004b2021-10-04 13:55:44 -040089 t.Helper()
Liz Kammer78cfdaa2021-11-08 12:56:31 -050090 moduleTypeUnderTest := "cc_binary_host"
Liz Kammerdfeb1202022-05-13 17:20:20 -040091 description := fmt.Sprintf("%s %s", moduleTypeUnderTest, testCase.description)
92 t.Run(description, func(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +000093 RunBp2BuildTestCase(t, registerCcBinaryModuleTypes, Bp2buildTestCase{
94 ExpectedBazelTargets: generateBazelTargetsForTest(testCase.targets, android.HostSupported),
95 ModuleTypeUnderTest: moduleTypeUnderTest,
96 ModuleTypeUnderTestFactory: cc.BinaryHostFactory,
97 Description: description,
98 Blueprint: hostBinaryReplacer.Replace(testCase.blueprint),
Liz Kammerbaced712022-09-16 09:01:29 -040099 Filesystem: testCase.filesystem,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500100 })
Liz Kammer2b8004b2021-10-04 13:55:44 -0400101 })
102}
103
104func TestBasicCcBinary(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500105 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400106 description: "basic -- properties -> attrs with little/no transformation",
Liz Kammerbaced712022-09-16 09:01:29 -0400107 filesystem: map[string]string{
108 soongCcVersionLibBpPath: soongCcVersionLibBp,
109 },
Liz Kammer2b8004b2021-10-04 13:55:44 -0400110 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 },
Yu Liufc603162022-03-01 15:44:08 -0800129 sdk_version: "current",
130 min_sdk_version: "29",
Yu Liua79c9462022-03-22 16:35:22 -0700131 use_version_lib: true,
Liz Kammer2b8004b2021-10-04 13:55:44 -0400132}
133`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500134 targets: []testBazelTarget{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000135 {"cc_binary", "foo", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500136 "absolute_includes": `["absolute_dir"]`,
137 "asflags": `["-Dasflag"]`,
138 "conlyflags": `["-Dconlyflag"]`,
139 "copts": `["-Dcopt"]`,
140 "cppflags": `["-Dcppflag"]`,
141 "linkopts": `["ld-flag"]`,
142 "local_includes": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400143 "dir",
144 ".",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500145 ]`,
146 "rtti": `True`,
147 "srcs": `["a.cc"]`,
148 "strip": `{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400149 "all": True,
150 "keep_symbols": True,
151 "keep_symbols_and_debug_frame": True,
152 "keep_symbols_list": ["symbol"],
153 "none": True,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500154 }`,
Liz Kammerbaced712022-09-16 09:01:29 -0400155 "sdk_version": `"current"`,
156 "min_sdk_version": `"29"`,
157 "use_version_lib": `True`,
158 "whole_archive_deps": `["//build/soong/cc/libbuildversion:libbuildversion"]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500159 },
160 },
161 },
Liz Kammer2b8004b2021-10-04 13:55:44 -0400162 })
163}
164
165func TestCcBinaryWithSharedLdflagDisableFeature(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500166 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400167 description: `ldflag "-shared" disables static_flag feature`,
168 blueprint: `
169{rule_name} {
170 name: "foo",
171 ldflags: ["-shared"],
172 include_build_directory: false,
173}
174`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500175 targets: []testBazelTarget{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000176 {"cc_binary", "foo", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500177 "features": `["-static_flag"]`,
178 "linkopts": `["-shared"]`,
179 },
180 },
181 },
Liz Kammer2b8004b2021-10-04 13:55:44 -0400182 })
183}
184
185func TestCcBinaryWithLinkStatic(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500186 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400187 description: "link static",
188 blueprint: `
189{rule_name} {
190 name: "foo",
191 static_executable: true,
192 include_build_directory: false,
193}
194`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500195 targets: []testBazelTarget{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000196 {"cc_binary", "foo", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500197 "linkshared": `False`,
198 },
199 },
200 },
Liz Kammer2b8004b2021-10-04 13:55:44 -0400201 })
202}
203
204func TestCcBinaryVersionScript(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500205 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400206 description: `version script`,
207 blueprint: `
208{rule_name} {
209 name: "foo",
210 include_build_directory: false,
211 version_script: "vs",
212}
213`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500214 targets: []testBazelTarget{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000215 {"cc_binary", "foo", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500216 "additional_linker_inputs": `["vs"]`,
217 "linkopts": `["-Wl,--version-script,$(location vs)"]`,
218 },
219 },
220 },
Liz Kammer2b8004b2021-10-04 13:55:44 -0400221 })
222}
223
224func TestCcBinarySplitSrcsByLang(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500225 runCcHostBinaryTestCase(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400226 description: "split srcs by lang",
227 blueprint: `
228{rule_name} {
229 name: "foo",
230 srcs: [
231 "asonly.S",
232 "conly.c",
233 "cpponly.cpp",
234 ":fg_foo",
235 ],
236 include_build_directory: false,
237}
238` + simpleModuleDoNotConvertBp2build("filegroup", "fg_foo"),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500239 targets: []testBazelTarget{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000240 {"cc_binary", "foo", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500241 "srcs": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400242 "cpponly.cpp",
243 ":fg_foo_cpp_srcs",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500244 ]`,
245 "srcs_as": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400246 "asonly.S",
247 ":fg_foo_as_srcs",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500248 ]`,
249 "srcs_c": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400250 "conly.c",
251 ":fg_foo_c_srcs",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500252 ]`,
253 },
254 },
255 },
Liz Kammer2b8004b2021-10-04 13:55:44 -0400256 })
257}
258
259func TestCcBinaryDoNotDistinguishBetweenDepsAndImplementationDeps(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500260 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400261 description: "no implementation deps",
262 blueprint: `
Liz Kammere6583482021-10-19 13:56:10 -0400263genrule {
264 name: "generated_hdr",
265 cmd: "nothing to see here",
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400266 bazel_module: { bp2build_available: false },
Liz Kammere6583482021-10-19 13:56:10 -0400267}
268
269genrule {
270 name: "export_generated_hdr",
271 cmd: "nothing to see here",
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400272 bazel_module: { bp2build_available: false },
Liz Kammere6583482021-10-19 13:56:10 -0400273}
274
Liz Kammer2b8004b2021-10-04 13:55:44 -0400275{rule_name} {
276 name: "foo",
Liz Kammere6583482021-10-19 13:56:10 -0400277 srcs: ["foo.cpp"],
Liz Kammer2b8004b2021-10-04 13:55:44 -0400278 shared_libs: ["implementation_shared_dep", "shared_dep"],
279 export_shared_lib_headers: ["shared_dep"],
280 static_libs: ["implementation_static_dep", "static_dep"],
281 export_static_lib_headers: ["static_dep", "whole_static_dep"],
282 whole_static_libs: ["not_explicitly_exported_whole_static_dep", "whole_static_dep"],
283 include_build_directory: false,
Liz Kammere6583482021-10-19 13:56:10 -0400284 generated_headers: ["generated_hdr", "export_generated_hdr"],
285 export_generated_headers: ["export_generated_hdr"],
Liz Kammer2b8004b2021-10-04 13:55:44 -0400286}
287` +
288 simpleModuleDoNotConvertBp2build("cc_library_static", "static_dep") +
289 simpleModuleDoNotConvertBp2build("cc_library_static", "implementation_static_dep") +
290 simpleModuleDoNotConvertBp2build("cc_library_static", "whole_static_dep") +
291 simpleModuleDoNotConvertBp2build("cc_library_static", "not_explicitly_exported_whole_static_dep") +
292 simpleModuleDoNotConvertBp2build("cc_library", "shared_dep") +
293 simpleModuleDoNotConvertBp2build("cc_library", "implementation_shared_dep"),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500294 targets: []testBazelTarget{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000295 {"cc_binary", "foo", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500296 "deps": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400297 ":implementation_static_dep",
298 ":static_dep",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500299 ]`,
300 "dynamic_deps": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400301 ":implementation_shared_dep",
302 ":shared_dep",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500303 ]`,
304 "srcs": `[
Liz Kammere6583482021-10-19 13:56:10 -0400305 "foo.cpp",
306 ":generated_hdr",
307 ":export_generated_hdr",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500308 ]`,
309 "whole_archive_deps": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400310 ":not_explicitly_exported_whole_static_dep",
311 ":whole_static_dep",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500312 ]`,
Liz Kammer1263d9b2021-12-10 14:28:20 -0500313 "local_includes": `["."]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500314 },
315 },
316 },
Liz Kammer2b8004b2021-10-04 13:55:44 -0400317 })
318}
319
320func TestCcBinaryNocrtTests(t *testing.T) {
321 baseTestCases := []struct {
322 description string
323 soongProperty string
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000324 bazelAttr AttrNameToString
Liz Kammer2b8004b2021-10-04 13:55:44 -0400325 }{
326 {
327 description: "nocrt: true",
328 soongProperty: `nocrt: true,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000329 bazelAttr: AttrNameToString{"link_crt": `False`},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400330 },
331 {
332 description: "nocrt: false",
333 soongProperty: `nocrt: false,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000334 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400335 },
336 {
337 description: "nocrt: not set",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000338 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400339 },
340 }
341
342 baseBlueprint := `{rule_name} {
343 name: "foo",%s
344 include_build_directory: false,
345}
346`
347
Liz Kammer2b8004b2021-10-04 13:55:44 -0400348 for _, btc := range baseTestCases {
349 prop := btc.soongProperty
350 if len(prop) > 0 {
351 prop = "\n" + prop
352 }
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500353 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400354 description: btc.description,
355 blueprint: fmt.Sprintf(baseBlueprint, prop),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500356 targets: []testBazelTarget{
357 {"cc_binary", "foo", btc.bazelAttr},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400358 },
359 })
360 }
361}
362
363func TestCcBinaryNo_libcrtTests(t *testing.T) {
364 baseTestCases := []struct {
365 description string
366 soongProperty string
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000367 bazelAttr AttrNameToString
Liz Kammer2b8004b2021-10-04 13:55:44 -0400368 }{
369 {
370 description: "no_libcrt: true",
371 soongProperty: `no_libcrt: true,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000372 bazelAttr: AttrNameToString{"use_libcrt": `False`},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400373 },
374 {
375 description: "no_libcrt: false",
376 soongProperty: `no_libcrt: false,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000377 bazelAttr: AttrNameToString{"use_libcrt": `True`},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400378 },
379 {
380 description: "no_libcrt: not set",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000381 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400382 },
383 }
384
385 baseBlueprint := `{rule_name} {
386 name: "foo",%s
387 include_build_directory: false,
388}
389`
390
Liz Kammer2b8004b2021-10-04 13:55:44 -0400391 for _, btc := range baseTestCases {
392 prop := btc.soongProperty
393 if len(prop) > 0 {
394 prop = "\n" + prop
395 }
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500396 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400397 description: btc.description,
398 blueprint: fmt.Sprintf(baseBlueprint, prop),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500399 targets: []testBazelTarget{
400 {"cc_binary", "foo", btc.bazelAttr},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400401 },
402 })
403 }
404}
405
406func TestCcBinaryPropertiesToFeatures(t *testing.T) {
407 baseTestCases := []struct {
408 description string
409 soongProperty string
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000410 bazelAttr AttrNameToString
Liz Kammer2b8004b2021-10-04 13:55:44 -0400411 }{
412 {
413 description: "pack_relocation: true",
414 soongProperty: `pack_relocations: true,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000415 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400416 },
417 {
418 description: "pack_relocations: false",
419 soongProperty: `pack_relocations: false,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000420 bazelAttr: AttrNameToString{"features": `["disable_pack_relocations"]`},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400421 },
422 {
423 description: "pack_relocations: not set",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000424 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400425 },
426 {
427 description: "pack_relocation: true",
428 soongProperty: `allow_undefined_symbols: true,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000429 bazelAttr: AttrNameToString{"features": `["-no_undefined_symbols"]`},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400430 },
431 {
432 description: "allow_undefined_symbols: false",
433 soongProperty: `allow_undefined_symbols: false,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000434 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400435 },
436 {
437 description: "allow_undefined_symbols: not set",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000438 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400439 },
440 }
441
442 baseBlueprint := `{rule_name} {
443 name: "foo",%s
444 include_build_directory: false,
445}
446`
Liz Kammer2b8004b2021-10-04 13:55:44 -0400447 for _, btc := range baseTestCases {
448 prop := btc.soongProperty
449 if len(prop) > 0 {
450 prop = "\n" + prop
451 }
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500452 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400453 description: btc.description,
454 blueprint: fmt.Sprintf(baseBlueprint, prop),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500455 targets: []testBazelTarget{
456 {"cc_binary", "foo", btc.bazelAttr},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400457 },
458 })
459 }
460}
Liz Kammer12615db2021-09-28 09:19:17 -0400461
462func TestCcBinarySharedProto(t *testing.T) {
463 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
464 blueprint: soongCcProtoLibraries + `{rule_name} {
465 name: "foo",
466 srcs: ["foo.proto"],
467 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -0400468 },
469 include_build_directory: false,
470}`,
471 targets: []testBazelTarget{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000472 {"proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400473 "srcs": `["foo.proto"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000474 }}, {"cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400475 "deps": `[":foo_proto"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000476 }}, {"cc_binary", "foo", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400477 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
478 "whole_archive_deps": `[":foo_cc_proto_lite"]`,
479 }},
480 },
481 })
482}
483
484func TestCcBinaryStaticProto(t *testing.T) {
485 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
486 blueprint: soongCcProtoLibraries + `{rule_name} {
487 name: "foo",
488 srcs: ["foo.proto"],
489 static_executable: true,
490 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -0400491 },
492 include_build_directory: false,
493}`,
494 targets: []testBazelTarget{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000495 {"proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400496 "srcs": `["foo.proto"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000497 }}, {"cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400498 "deps": `[":foo_proto"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000499 }}, {"cc_binary", "foo", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400500 "deps": `[":libprotobuf-cpp-lite"]`,
501 "whole_archive_deps": `[":foo_cc_proto_lite"]`,
502 "linkshared": `False`,
503 }},
504 },
505 })
506}
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000507
508func TestCcBinaryConvertLex(t *testing.T) {
509 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
510 description: `.l and .ll sources converted to .c and .cc`,
511 blueprint: `
512{rule_name} {
513 name: "foo",
514 srcs: ["foo.c", "bar.cc", "foo1.l", "foo2.l", "bar1.ll", "bar2.ll"],
515 lex: { flags: ["--foo_opt", "--bar_opt"] },
516 include_build_directory: false,
517}
518`,
519 targets: []testBazelTarget{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000520 {"genlex", "foo_genlex_l", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000521 "srcs": `[
522 "foo1.l",
523 "foo2.l",
524 ]`,
525 "lexopts": `[
526 "--foo_opt",
527 "--bar_opt",
528 ]`,
529 }},
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000530 {"genlex", "foo_genlex_ll", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000531 "srcs": `[
532 "bar1.ll",
533 "bar2.ll",
534 ]`,
535 "lexopts": `[
536 "--foo_opt",
537 "--bar_opt",
538 ]`,
539 }},
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000540 {"cc_binary", "foo", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000541 "srcs": `[
542 "bar.cc",
543 ":foo_genlex_ll",
544 ]`,
545 "srcs_c": `[
546 "foo.c",
547 ":foo_genlex_l",
548 ]`,
549 }},
550 },
551 })
552}
Cole Faust6b29f592022-08-09 09:50:56 -0700553
554func TestCcBinaryRuntimeLibs(t *testing.T) {
555 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
556 description: "cc_binary with runtime libs",
557 blueprint: `
558cc_library {
559 name: "bar",
560 srcs: ["b.cc"],
561}
562
563{rule_name} {
564 name: "foo",
565 srcs: ["a.cc"],
566 runtime_libs: ["bar"],
567}
568`,
569 targets: []testBazelTarget{
570 {"cc_library_static", "bar_bp2build_cc_library_static", AttrNameToString{
571 "local_includes": `["."]`,
572 "srcs": `["b.cc"]`,
573 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
574 },
575 },
576 {"cc_library_shared", "bar", AttrNameToString{
577 "local_includes": `["."]`,
578 "srcs": `["b.cc"]`,
579 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
580 },
581 },
582 {"cc_binary", "foo", AttrNameToString{
583 "local_includes": `["."]`,
584 "srcs": `["a.cc"]`,
585 "runtime_deps": `[":bar"]`,
586 },
587 },
588 },
589 })
590}
Cole Faust5fa4e962022-08-22 14:31:04 -0700591
592func TestCcBinaryWithInstructionSet(t *testing.T) {
593 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
594 description: "instruction set",
595 blueprint: `
596{rule_name} {
597 name: "foo",
598 arch: {
599 arm: {
600 instruction_set: "arm",
601 }
602 }
603}
604`,
605 targets: []testBazelTarget{
606 {"cc_binary", "foo", AttrNameToString{
607 "features": `select({
608 "//build/bazel/platforms/arch:arm": [
609 "arm_isa_arm",
610 "-arm_isa_thumb",
611 ],
612 "//conditions:default": [],
613 })`,
614 "local_includes": `["."]`,
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -0500615 }},
616 },
617 })
618}
619
620func TestCcBinaryEmptySuffix(t *testing.T) {
621 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
622 description: "binary with empty suffix",
623 blueprint: `
624{rule_name} {
625 name: "foo",
626 suffix: "",
627}`,
628 targets: []testBazelTarget{
629 {"cc_binary", "foo", AttrNameToString{
630 "local_includes": `["."]`,
631 "suffix": `""`,
632 }},
633 },
634 })
635}
636
637func TestCcBinarySuffix(t *testing.T) {
638 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
639 description: "binary with suffix",
640 blueprint: `
641{rule_name} {
642 name: "foo",
643 suffix: "-suf",
644}
645`,
646 targets: []testBazelTarget{
647 {"cc_binary", "foo", AttrNameToString{
648 "local_includes": `["."]`,
649 "suffix": `"-suf"`,
650 }},
651 },
652 })
653}
654
655func TestCcArchVariantBinarySuffix(t *testing.T) {
656 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
657 description: "binary with suffix",
658 blueprint: `
659{rule_name} {
660 name: "foo",
661 arch: {
662 arm64: { suffix: "-64" },
663 arm: { suffix: "-32" },
664 },
665}
666`,
667 targets: []testBazelTarget{
668 {"cc_binary", "foo", AttrNameToString{
669 "local_includes": `["."]`,
670 "suffix": `select({
671 "//build/bazel/platforms/arch:arm": "-32",
672 "//build/bazel/platforms/arch:arm64": "-64",
673 "//conditions:default": None,
674 })`,
675 }},
Cole Faust5fa4e962022-08-22 14:31:04 -0700676 },
677 })
678}
Trevor Radcliffecee4e052022-09-06 19:31:25 +0000679
680func TestCcBinaryWithSyspropSrcs(t *testing.T) {
681 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
682 description: "cc_binary with sysprop sources",
683 blueprint: `
684{rule_name} {
685 name: "foo",
686 srcs: [
687 "bar.sysprop",
688 "baz.sysprop",
689 "blah.cpp",
690 ],
691 min_sdk_version: "5",
692}`,
693 targets: []testBazelTarget{
694 {"sysprop_library", "foo_sysprop_library", AttrNameToString{
695 "srcs": `[
696 "bar.sysprop",
697 "baz.sysprop",
698 ]`,
699 }},
700 {"cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
701 "dep": `":foo_sysprop_library"`,
702 "min_sdk_version": `"5"`,
703 }},
704 {"cc_binary", "foo", AttrNameToString{
705 "srcs": `["blah.cpp"]`,
706 "local_includes": `["."]`,
707 "min_sdk_version": `"5"`,
708 "whole_archive_deps": `[":foo_cc_sysprop_library_static"]`,
709 }},
710 },
711 })
712}
713
714func TestCcBinaryWithSyspropSrcsSomeConfigs(t *testing.T) {
715 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
716 description: "cc_binary with sysprop sources in some configs but not others",
717 blueprint: `
718{rule_name} {
719 name: "foo",
720 srcs: [
721 "blah.cpp",
722 ],
723 target: {
724 android: {
725 srcs: ["bar.sysprop"],
726 },
727 },
728 min_sdk_version: "5",
729}`,
730 targets: []testBazelTarget{
731 {"sysprop_library", "foo_sysprop_library", AttrNameToString{
732 "srcs": `select({
733 "//build/bazel/platforms/os:android": ["bar.sysprop"],
734 "//conditions:default": [],
735 })`,
736 }},
737 {"cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
738 "dep": `":foo_sysprop_library"`,
739 "min_sdk_version": `"5"`,
740 }},
741 {"cc_binary", "foo", AttrNameToString{
742 "srcs": `["blah.cpp"]`,
743 "local_includes": `["."]`,
744 "min_sdk_version": `"5"`,
745 "whole_archive_deps": `select({
746 "//build/bazel/platforms/os:android": [":foo_cc_sysprop_library_static"],
747 "//conditions:default": [],
748 })`,
749 }},
750 },
751 })
752}