blob: 72d3940c4a241afaebc6e8dde0d4b3a4eb532e11 [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
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000204func TestCcBinaryVersionScriptAndDynamicList(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500205 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000206 description: `version script and dynamic list`,
Liz Kammer2b8004b2021-10-04 13:55:44 -0400207 blueprint: `
208{rule_name} {
209 name: "foo",
210 include_build_directory: false,
211 version_script: "vs",
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000212 dynamic_list: "dynamic.list",
Liz Kammer2b8004b2021-10-04 13:55:44 -0400213}
214`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500215 targets: []testBazelTarget{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000216 {"cc_binary", "foo", AttrNameToString{
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000217 "additional_linker_inputs": `[
218 "vs",
219 "dynamic.list",
220 ]`,
221 "linkopts": `[
222 "-Wl,--version-script,$(location vs)",
223 "-Wl,--dynamic-list,$(location dynamic.list)",
224 ]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500225 },
226 },
227 },
Liz Kammer2b8004b2021-10-04 13:55:44 -0400228 })
229}
230
231func TestCcBinarySplitSrcsByLang(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500232 runCcHostBinaryTestCase(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400233 description: "split srcs by lang",
234 blueprint: `
235{rule_name} {
236 name: "foo",
237 srcs: [
238 "asonly.S",
239 "conly.c",
240 "cpponly.cpp",
241 ":fg_foo",
242 ],
243 include_build_directory: false,
244}
245` + simpleModuleDoNotConvertBp2build("filegroup", "fg_foo"),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500246 targets: []testBazelTarget{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000247 {"cc_binary", "foo", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500248 "srcs": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400249 "cpponly.cpp",
250 ":fg_foo_cpp_srcs",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500251 ]`,
252 "srcs_as": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400253 "asonly.S",
254 ":fg_foo_as_srcs",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500255 ]`,
256 "srcs_c": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400257 "conly.c",
258 ":fg_foo_c_srcs",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500259 ]`,
260 },
261 },
262 },
Liz Kammer2b8004b2021-10-04 13:55:44 -0400263 })
264}
265
266func TestCcBinaryDoNotDistinguishBetweenDepsAndImplementationDeps(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500267 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400268 description: "no implementation deps",
269 blueprint: `
Liz Kammere6583482021-10-19 13:56:10 -0400270genrule {
271 name: "generated_hdr",
272 cmd: "nothing to see here",
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400273 bazel_module: { bp2build_available: false },
Liz Kammere6583482021-10-19 13:56:10 -0400274}
275
276genrule {
277 name: "export_generated_hdr",
278 cmd: "nothing to see here",
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400279 bazel_module: { bp2build_available: false },
Liz Kammere6583482021-10-19 13:56:10 -0400280}
281
Liz Kammer2b8004b2021-10-04 13:55:44 -0400282{rule_name} {
283 name: "foo",
Liz Kammere6583482021-10-19 13:56:10 -0400284 srcs: ["foo.cpp"],
Liz Kammer2b8004b2021-10-04 13:55:44 -0400285 shared_libs: ["implementation_shared_dep", "shared_dep"],
286 export_shared_lib_headers: ["shared_dep"],
287 static_libs: ["implementation_static_dep", "static_dep"],
288 export_static_lib_headers: ["static_dep", "whole_static_dep"],
289 whole_static_libs: ["not_explicitly_exported_whole_static_dep", "whole_static_dep"],
290 include_build_directory: false,
Liz Kammere6583482021-10-19 13:56:10 -0400291 generated_headers: ["generated_hdr", "export_generated_hdr"],
292 export_generated_headers: ["export_generated_hdr"],
Liz Kammer2b8004b2021-10-04 13:55:44 -0400293}
294` +
295 simpleModuleDoNotConvertBp2build("cc_library_static", "static_dep") +
296 simpleModuleDoNotConvertBp2build("cc_library_static", "implementation_static_dep") +
297 simpleModuleDoNotConvertBp2build("cc_library_static", "whole_static_dep") +
298 simpleModuleDoNotConvertBp2build("cc_library_static", "not_explicitly_exported_whole_static_dep") +
299 simpleModuleDoNotConvertBp2build("cc_library", "shared_dep") +
300 simpleModuleDoNotConvertBp2build("cc_library", "implementation_shared_dep"),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500301 targets: []testBazelTarget{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000302 {"cc_binary", "foo", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500303 "deps": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400304 ":implementation_static_dep",
305 ":static_dep",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500306 ]`,
307 "dynamic_deps": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400308 ":implementation_shared_dep",
309 ":shared_dep",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500310 ]`,
311 "srcs": `[
Liz Kammere6583482021-10-19 13:56:10 -0400312 "foo.cpp",
313 ":generated_hdr",
314 ":export_generated_hdr",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500315 ]`,
316 "whole_archive_deps": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400317 ":not_explicitly_exported_whole_static_dep",
318 ":whole_static_dep",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500319 ]`,
Liz Kammer1263d9b2021-12-10 14:28:20 -0500320 "local_includes": `["."]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500321 },
322 },
323 },
Liz Kammer2b8004b2021-10-04 13:55:44 -0400324 })
325}
326
327func TestCcBinaryNocrtTests(t *testing.T) {
328 baseTestCases := []struct {
329 description string
330 soongProperty string
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000331 bazelAttr AttrNameToString
Liz Kammer2b8004b2021-10-04 13:55:44 -0400332 }{
333 {
334 description: "nocrt: true",
335 soongProperty: `nocrt: true,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000336 bazelAttr: AttrNameToString{"link_crt": `False`},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400337 },
338 {
339 description: "nocrt: false",
340 soongProperty: `nocrt: false,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000341 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400342 },
343 {
344 description: "nocrt: not set",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000345 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400346 },
347 }
348
349 baseBlueprint := `{rule_name} {
350 name: "foo",%s
351 include_build_directory: false,
352}
353`
354
Liz Kammer2b8004b2021-10-04 13:55:44 -0400355 for _, btc := range baseTestCases {
356 prop := btc.soongProperty
357 if len(prop) > 0 {
358 prop = "\n" + prop
359 }
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500360 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400361 description: btc.description,
362 blueprint: fmt.Sprintf(baseBlueprint, prop),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500363 targets: []testBazelTarget{
364 {"cc_binary", "foo", btc.bazelAttr},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400365 },
366 })
367 }
368}
369
370func TestCcBinaryNo_libcrtTests(t *testing.T) {
371 baseTestCases := []struct {
372 description string
373 soongProperty string
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000374 bazelAttr AttrNameToString
Liz Kammer2b8004b2021-10-04 13:55:44 -0400375 }{
376 {
377 description: "no_libcrt: true",
378 soongProperty: `no_libcrt: true,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000379 bazelAttr: AttrNameToString{"use_libcrt": `False`},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400380 },
381 {
382 description: "no_libcrt: false",
383 soongProperty: `no_libcrt: false,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000384 bazelAttr: AttrNameToString{"use_libcrt": `True`},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400385 },
386 {
387 description: "no_libcrt: not set",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000388 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400389 },
390 }
391
392 baseBlueprint := `{rule_name} {
393 name: "foo",%s
394 include_build_directory: false,
395}
396`
397
Liz Kammer2b8004b2021-10-04 13:55:44 -0400398 for _, btc := range baseTestCases {
399 prop := btc.soongProperty
400 if len(prop) > 0 {
401 prop = "\n" + prop
402 }
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500403 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400404 description: btc.description,
405 blueprint: fmt.Sprintf(baseBlueprint, prop),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500406 targets: []testBazelTarget{
407 {"cc_binary", "foo", btc.bazelAttr},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400408 },
409 })
410 }
411}
412
413func TestCcBinaryPropertiesToFeatures(t *testing.T) {
414 baseTestCases := []struct {
415 description string
416 soongProperty string
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: `pack_relocations: true,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000422 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400423 },
424 {
425 description: "pack_relocations: false",
426 soongProperty: `pack_relocations: false,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000427 bazelAttr: AttrNameToString{"features": `["disable_pack_relocations"]`},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400428 },
429 {
430 description: "pack_relocations: not set",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000431 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400432 },
433 {
434 description: "pack_relocation: true",
435 soongProperty: `allow_undefined_symbols: true,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000436 bazelAttr: AttrNameToString{"features": `["-no_undefined_symbols"]`},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400437 },
438 {
439 description: "allow_undefined_symbols: false",
440 soongProperty: `allow_undefined_symbols: false,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000441 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400442 },
443 {
444 description: "allow_undefined_symbols: not set",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000445 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400446 },
447 }
448
449 baseBlueprint := `{rule_name} {
450 name: "foo",%s
451 include_build_directory: false,
452}
453`
Liz Kammer2b8004b2021-10-04 13:55:44 -0400454 for _, btc := range baseTestCases {
455 prop := btc.soongProperty
456 if len(prop) > 0 {
457 prop = "\n" + prop
458 }
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500459 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400460 description: btc.description,
461 blueprint: fmt.Sprintf(baseBlueprint, prop),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500462 targets: []testBazelTarget{
463 {"cc_binary", "foo", btc.bazelAttr},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400464 },
465 })
466 }
467}
Liz Kammer12615db2021-09-28 09:19:17 -0400468
469func TestCcBinarySharedProto(t *testing.T) {
470 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
471 blueprint: soongCcProtoLibraries + `{rule_name} {
472 name: "foo",
473 srcs: ["foo.proto"],
474 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -0400475 },
476 include_build_directory: false,
477}`,
478 targets: []testBazelTarget{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000479 {"proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400480 "srcs": `["foo.proto"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000481 }}, {"cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400482 "deps": `[":foo_proto"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000483 }}, {"cc_binary", "foo", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400484 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
485 "whole_archive_deps": `[":foo_cc_proto_lite"]`,
486 }},
487 },
488 })
489}
490
491func TestCcBinaryStaticProto(t *testing.T) {
492 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
493 blueprint: soongCcProtoLibraries + `{rule_name} {
494 name: "foo",
495 srcs: ["foo.proto"],
496 static_executable: true,
497 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -0400498 },
499 include_build_directory: false,
500}`,
501 targets: []testBazelTarget{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000502 {"proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400503 "srcs": `["foo.proto"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000504 }}, {"cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400505 "deps": `[":foo_proto"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000506 }}, {"cc_binary", "foo", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400507 "deps": `[":libprotobuf-cpp-lite"]`,
508 "whole_archive_deps": `[":foo_cc_proto_lite"]`,
509 "linkshared": `False`,
510 }},
511 },
512 })
513}
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000514
515func TestCcBinaryConvertLex(t *testing.T) {
516 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
517 description: `.l and .ll sources converted to .c and .cc`,
518 blueprint: `
519{rule_name} {
520 name: "foo",
521 srcs: ["foo.c", "bar.cc", "foo1.l", "foo2.l", "bar1.ll", "bar2.ll"],
522 lex: { flags: ["--foo_opt", "--bar_opt"] },
523 include_build_directory: false,
524}
525`,
526 targets: []testBazelTarget{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000527 {"genlex", "foo_genlex_l", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000528 "srcs": `[
529 "foo1.l",
530 "foo2.l",
531 ]`,
532 "lexopts": `[
533 "--foo_opt",
534 "--bar_opt",
535 ]`,
536 }},
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000537 {"genlex", "foo_genlex_ll", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000538 "srcs": `[
539 "bar1.ll",
540 "bar2.ll",
541 ]`,
542 "lexopts": `[
543 "--foo_opt",
544 "--bar_opt",
545 ]`,
546 }},
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000547 {"cc_binary", "foo", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000548 "srcs": `[
549 "bar.cc",
550 ":foo_genlex_ll",
551 ]`,
552 "srcs_c": `[
553 "foo.c",
554 ":foo_genlex_l",
555 ]`,
556 }},
557 },
558 })
559}
Cole Faust6b29f592022-08-09 09:50:56 -0700560
561func TestCcBinaryRuntimeLibs(t *testing.T) {
562 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
563 description: "cc_binary with runtime libs",
564 blueprint: `
565cc_library {
566 name: "bar",
567 srcs: ["b.cc"],
568}
569
570{rule_name} {
571 name: "foo",
572 srcs: ["a.cc"],
573 runtime_libs: ["bar"],
574}
575`,
576 targets: []testBazelTarget{
577 {"cc_library_static", "bar_bp2build_cc_library_static", AttrNameToString{
578 "local_includes": `["."]`,
579 "srcs": `["b.cc"]`,
580 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
581 },
582 },
583 {"cc_library_shared", "bar", AttrNameToString{
584 "local_includes": `["."]`,
585 "srcs": `["b.cc"]`,
586 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
587 },
588 },
589 {"cc_binary", "foo", AttrNameToString{
590 "local_includes": `["."]`,
591 "srcs": `["a.cc"]`,
592 "runtime_deps": `[":bar"]`,
593 },
594 },
595 },
596 })
597}
Cole Faust5fa4e962022-08-22 14:31:04 -0700598
599func TestCcBinaryWithInstructionSet(t *testing.T) {
600 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
601 description: "instruction set",
602 blueprint: `
603{rule_name} {
604 name: "foo",
605 arch: {
606 arm: {
607 instruction_set: "arm",
608 }
609 }
610}
611`,
612 targets: []testBazelTarget{
613 {"cc_binary", "foo", AttrNameToString{
614 "features": `select({
615 "//build/bazel/platforms/arch:arm": [
616 "arm_isa_arm",
617 "-arm_isa_thumb",
618 ],
619 "//conditions:default": [],
620 })`,
621 "local_includes": `["."]`,
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -0500622 }},
623 },
624 })
625}
626
627func TestCcBinaryEmptySuffix(t *testing.T) {
628 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
629 description: "binary with empty suffix",
630 blueprint: `
631{rule_name} {
632 name: "foo",
633 suffix: "",
634}`,
635 targets: []testBazelTarget{
636 {"cc_binary", "foo", AttrNameToString{
637 "local_includes": `["."]`,
638 "suffix": `""`,
639 }},
640 },
641 })
642}
643
644func TestCcBinarySuffix(t *testing.T) {
645 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
646 description: "binary with suffix",
647 blueprint: `
648{rule_name} {
649 name: "foo",
650 suffix: "-suf",
651}
652`,
653 targets: []testBazelTarget{
654 {"cc_binary", "foo", AttrNameToString{
655 "local_includes": `["."]`,
656 "suffix": `"-suf"`,
657 }},
658 },
659 })
660}
661
662func TestCcArchVariantBinarySuffix(t *testing.T) {
663 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
664 description: "binary with suffix",
665 blueprint: `
666{rule_name} {
667 name: "foo",
668 arch: {
669 arm64: { suffix: "-64" },
670 arm: { suffix: "-32" },
671 },
672}
673`,
674 targets: []testBazelTarget{
675 {"cc_binary", "foo", AttrNameToString{
676 "local_includes": `["."]`,
677 "suffix": `select({
678 "//build/bazel/platforms/arch:arm": "-32",
679 "//build/bazel/platforms/arch:arm64": "-64",
680 "//conditions:default": None,
681 })`,
682 }},
Cole Faust5fa4e962022-08-22 14:31:04 -0700683 },
684 })
685}
Trevor Radcliffecee4e052022-09-06 19:31:25 +0000686
687func TestCcBinaryWithSyspropSrcs(t *testing.T) {
688 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
689 description: "cc_binary with sysprop sources",
690 blueprint: `
691{rule_name} {
692 name: "foo",
693 srcs: [
694 "bar.sysprop",
695 "baz.sysprop",
696 "blah.cpp",
697 ],
698 min_sdk_version: "5",
699}`,
700 targets: []testBazelTarget{
701 {"sysprop_library", "foo_sysprop_library", AttrNameToString{
702 "srcs": `[
703 "bar.sysprop",
704 "baz.sysprop",
705 ]`,
706 }},
707 {"cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
708 "dep": `":foo_sysprop_library"`,
709 "min_sdk_version": `"5"`,
710 }},
711 {"cc_binary", "foo", AttrNameToString{
712 "srcs": `["blah.cpp"]`,
713 "local_includes": `["."]`,
714 "min_sdk_version": `"5"`,
715 "whole_archive_deps": `[":foo_cc_sysprop_library_static"]`,
716 }},
717 },
718 })
719}
720
721func TestCcBinaryWithSyspropSrcsSomeConfigs(t *testing.T) {
722 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
723 description: "cc_binary with sysprop sources in some configs but not others",
724 blueprint: `
725{rule_name} {
726 name: "foo",
727 srcs: [
728 "blah.cpp",
729 ],
730 target: {
731 android: {
732 srcs: ["bar.sysprop"],
733 },
734 },
735 min_sdk_version: "5",
736}`,
737 targets: []testBazelTarget{
738 {"sysprop_library", "foo_sysprop_library", AttrNameToString{
739 "srcs": `select({
740 "//build/bazel/platforms/os:android": ["bar.sysprop"],
741 "//conditions:default": [],
742 })`,
743 }},
744 {"cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
745 "dep": `":foo_sysprop_library"`,
746 "min_sdk_version": `"5"`,
747 }},
748 {"cc_binary", "foo", AttrNameToString{
749 "srcs": `["blah.cpp"]`,
750 "local_includes": `["."]`,
751 "min_sdk_version": `"5"`,
752 "whole_archive_deps": `select({
753 "//build/bazel/platforms/os:android": [":foo_cc_sysprop_library_static"],
754 "//conditions:default": [],
755 })`,
756 }},
757 },
758 })
759}