blob: 1b60f2baf233a9cf9400b479029d1a6b7e57e668 [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
Trevor Radcliffeea6a45d2022-09-20 18:58:01 +0000224func TestCcBinaryLdflagsSplitBySpaceExceptSoongAdded(t *testing.T) {
225 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
226 description: "ldflags are split by spaces except for the ones added by soong (version script and dynamic list)",
227 blueprint: `
228{rule_name} {
229 name: "foo",
230 ldflags: [
231 "--nospace_flag",
232 "-z spaceflag",
233 ],
234 version_script: "version_script",
235 dynamic_list: "dynamic.list",
236 include_build_directory: false,
237}
238`,
239 targets: []testBazelTarget{
240 {"cc_binary", "foo", AttrNameToString{
241 "additional_linker_inputs": `[
242 "version_script",
243 "dynamic.list",
244 ]`,
245 "linkopts": `[
246 "--nospace_flag",
247 "-z",
248 "spaceflag",
249 "-Wl,--version-script,$(location version_script)",
250 "-Wl,--dynamic-list,$(location dynamic.list)",
251 ]`,
252 }}},
253 })
254}
255
Liz Kammer2b8004b2021-10-04 13:55:44 -0400256func TestCcBinarySplitSrcsByLang(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500257 runCcHostBinaryTestCase(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400258 description: "split srcs by lang",
259 blueprint: `
260{rule_name} {
261 name: "foo",
262 srcs: [
263 "asonly.S",
264 "conly.c",
265 "cpponly.cpp",
266 ":fg_foo",
267 ],
268 include_build_directory: false,
269}
270` + simpleModuleDoNotConvertBp2build("filegroup", "fg_foo"),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500271 targets: []testBazelTarget{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000272 {"cc_binary", "foo", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500273 "srcs": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400274 "cpponly.cpp",
275 ":fg_foo_cpp_srcs",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500276 ]`,
277 "srcs_as": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400278 "asonly.S",
279 ":fg_foo_as_srcs",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500280 ]`,
281 "srcs_c": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400282 "conly.c",
283 ":fg_foo_c_srcs",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500284 ]`,
285 },
286 },
287 },
Liz Kammer2b8004b2021-10-04 13:55:44 -0400288 })
289}
290
291func TestCcBinaryDoNotDistinguishBetweenDepsAndImplementationDeps(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500292 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400293 description: "no implementation deps",
294 blueprint: `
Liz Kammere6583482021-10-19 13:56:10 -0400295genrule {
296 name: "generated_hdr",
297 cmd: "nothing to see here",
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400298 bazel_module: { bp2build_available: false },
Liz Kammere6583482021-10-19 13:56:10 -0400299}
300
301genrule {
302 name: "export_generated_hdr",
303 cmd: "nothing to see here",
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400304 bazel_module: { bp2build_available: false },
Liz Kammere6583482021-10-19 13:56:10 -0400305}
306
Liz Kammer2b8004b2021-10-04 13:55:44 -0400307{rule_name} {
308 name: "foo",
Liz Kammere6583482021-10-19 13:56:10 -0400309 srcs: ["foo.cpp"],
Liz Kammer2b8004b2021-10-04 13:55:44 -0400310 shared_libs: ["implementation_shared_dep", "shared_dep"],
311 export_shared_lib_headers: ["shared_dep"],
312 static_libs: ["implementation_static_dep", "static_dep"],
313 export_static_lib_headers: ["static_dep", "whole_static_dep"],
314 whole_static_libs: ["not_explicitly_exported_whole_static_dep", "whole_static_dep"],
315 include_build_directory: false,
Liz Kammere6583482021-10-19 13:56:10 -0400316 generated_headers: ["generated_hdr", "export_generated_hdr"],
317 export_generated_headers: ["export_generated_hdr"],
Liz Kammer2b8004b2021-10-04 13:55:44 -0400318}
319` +
320 simpleModuleDoNotConvertBp2build("cc_library_static", "static_dep") +
321 simpleModuleDoNotConvertBp2build("cc_library_static", "implementation_static_dep") +
322 simpleModuleDoNotConvertBp2build("cc_library_static", "whole_static_dep") +
323 simpleModuleDoNotConvertBp2build("cc_library_static", "not_explicitly_exported_whole_static_dep") +
324 simpleModuleDoNotConvertBp2build("cc_library", "shared_dep") +
325 simpleModuleDoNotConvertBp2build("cc_library", "implementation_shared_dep"),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500326 targets: []testBazelTarget{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000327 {"cc_binary", "foo", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500328 "deps": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400329 ":implementation_static_dep",
330 ":static_dep",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500331 ]`,
332 "dynamic_deps": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400333 ":implementation_shared_dep",
334 ":shared_dep",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500335 ]`,
336 "srcs": `[
Liz Kammere6583482021-10-19 13:56:10 -0400337 "foo.cpp",
338 ":generated_hdr",
339 ":export_generated_hdr",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500340 ]`,
341 "whole_archive_deps": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400342 ":not_explicitly_exported_whole_static_dep",
343 ":whole_static_dep",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500344 ]`,
Liz Kammer1263d9b2021-12-10 14:28:20 -0500345 "local_includes": `["."]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500346 },
347 },
348 },
Liz Kammer2b8004b2021-10-04 13:55:44 -0400349 })
350}
351
352func TestCcBinaryNocrtTests(t *testing.T) {
353 baseTestCases := []struct {
354 description string
355 soongProperty string
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000356 bazelAttr AttrNameToString
Liz Kammer2b8004b2021-10-04 13:55:44 -0400357 }{
358 {
359 description: "nocrt: true",
360 soongProperty: `nocrt: true,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000361 bazelAttr: AttrNameToString{"link_crt": `False`},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400362 },
363 {
364 description: "nocrt: false",
365 soongProperty: `nocrt: false,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000366 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400367 },
368 {
369 description: "nocrt: not set",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000370 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400371 },
372 }
373
374 baseBlueprint := `{rule_name} {
375 name: "foo",%s
376 include_build_directory: false,
377}
378`
379
Liz Kammer2b8004b2021-10-04 13:55:44 -0400380 for _, btc := range baseTestCases {
381 prop := btc.soongProperty
382 if len(prop) > 0 {
383 prop = "\n" + prop
384 }
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500385 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400386 description: btc.description,
387 blueprint: fmt.Sprintf(baseBlueprint, prop),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500388 targets: []testBazelTarget{
389 {"cc_binary", "foo", btc.bazelAttr},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400390 },
391 })
392 }
393}
394
395func TestCcBinaryNo_libcrtTests(t *testing.T) {
396 baseTestCases := []struct {
397 description string
398 soongProperty string
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000399 bazelAttr AttrNameToString
Liz Kammer2b8004b2021-10-04 13:55:44 -0400400 }{
401 {
402 description: "no_libcrt: true",
403 soongProperty: `no_libcrt: true,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000404 bazelAttr: AttrNameToString{"use_libcrt": `False`},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400405 },
406 {
407 description: "no_libcrt: false",
408 soongProperty: `no_libcrt: false,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000409 bazelAttr: AttrNameToString{"use_libcrt": `True`},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400410 },
411 {
412 description: "no_libcrt: not set",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000413 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400414 },
415 }
416
417 baseBlueprint := `{rule_name} {
418 name: "foo",%s
419 include_build_directory: false,
420}
421`
422
Liz Kammer2b8004b2021-10-04 13:55:44 -0400423 for _, btc := range baseTestCases {
424 prop := btc.soongProperty
425 if len(prop) > 0 {
426 prop = "\n" + prop
427 }
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500428 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400429 description: btc.description,
430 blueprint: fmt.Sprintf(baseBlueprint, prop),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500431 targets: []testBazelTarget{
432 {"cc_binary", "foo", btc.bazelAttr},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400433 },
434 })
435 }
436}
437
438func TestCcBinaryPropertiesToFeatures(t *testing.T) {
439 baseTestCases := []struct {
440 description string
441 soongProperty string
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000442 bazelAttr AttrNameToString
Liz Kammer2b8004b2021-10-04 13:55:44 -0400443 }{
444 {
445 description: "pack_relocation: true",
446 soongProperty: `pack_relocations: true,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000447 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400448 },
449 {
450 description: "pack_relocations: false",
451 soongProperty: `pack_relocations: false,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000452 bazelAttr: AttrNameToString{"features": `["disable_pack_relocations"]`},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400453 },
454 {
455 description: "pack_relocations: not set",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000456 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400457 },
458 {
459 description: "pack_relocation: true",
460 soongProperty: `allow_undefined_symbols: true,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000461 bazelAttr: AttrNameToString{"features": `["-no_undefined_symbols"]`},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400462 },
463 {
464 description: "allow_undefined_symbols: false",
465 soongProperty: `allow_undefined_symbols: false,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000466 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400467 },
468 {
469 description: "allow_undefined_symbols: not set",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000470 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400471 },
472 }
473
474 baseBlueprint := `{rule_name} {
475 name: "foo",%s
476 include_build_directory: false,
477}
478`
Liz Kammer2b8004b2021-10-04 13:55:44 -0400479 for _, btc := range baseTestCases {
480 prop := btc.soongProperty
481 if len(prop) > 0 {
482 prop = "\n" + prop
483 }
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500484 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400485 description: btc.description,
486 blueprint: fmt.Sprintf(baseBlueprint, prop),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500487 targets: []testBazelTarget{
488 {"cc_binary", "foo", btc.bazelAttr},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400489 },
490 })
491 }
492}
Liz Kammer12615db2021-09-28 09:19:17 -0400493
494func TestCcBinarySharedProto(t *testing.T) {
495 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
496 blueprint: soongCcProtoLibraries + `{rule_name} {
497 name: "foo",
498 srcs: ["foo.proto"],
499 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -0400500 },
501 include_build_directory: false,
502}`,
503 targets: []testBazelTarget{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000504 {"proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400505 "srcs": `["foo.proto"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000506 }}, {"cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400507 "deps": `[":foo_proto"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000508 }}, {"cc_binary", "foo", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400509 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
510 "whole_archive_deps": `[":foo_cc_proto_lite"]`,
511 }},
512 },
513 })
514}
515
516func TestCcBinaryStaticProto(t *testing.T) {
517 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
518 blueprint: soongCcProtoLibraries + `{rule_name} {
519 name: "foo",
520 srcs: ["foo.proto"],
521 static_executable: true,
522 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -0400523 },
524 include_build_directory: false,
525}`,
526 targets: []testBazelTarget{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000527 {"proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400528 "srcs": `["foo.proto"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000529 }}, {"cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400530 "deps": `[":foo_proto"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000531 }}, {"cc_binary", "foo", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400532 "deps": `[":libprotobuf-cpp-lite"]`,
533 "whole_archive_deps": `[":foo_cc_proto_lite"]`,
534 "linkshared": `False`,
535 }},
536 },
537 })
538}
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000539
540func TestCcBinaryConvertLex(t *testing.T) {
541 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
542 description: `.l and .ll sources converted to .c and .cc`,
543 blueprint: `
544{rule_name} {
545 name: "foo",
546 srcs: ["foo.c", "bar.cc", "foo1.l", "foo2.l", "bar1.ll", "bar2.ll"],
547 lex: { flags: ["--foo_opt", "--bar_opt"] },
548 include_build_directory: false,
549}
550`,
551 targets: []testBazelTarget{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000552 {"genlex", "foo_genlex_l", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000553 "srcs": `[
554 "foo1.l",
555 "foo2.l",
556 ]`,
557 "lexopts": `[
558 "--foo_opt",
559 "--bar_opt",
560 ]`,
561 }},
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000562 {"genlex", "foo_genlex_ll", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000563 "srcs": `[
564 "bar1.ll",
565 "bar2.ll",
566 ]`,
567 "lexopts": `[
568 "--foo_opt",
569 "--bar_opt",
570 ]`,
571 }},
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000572 {"cc_binary", "foo", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000573 "srcs": `[
574 "bar.cc",
575 ":foo_genlex_ll",
576 ]`,
577 "srcs_c": `[
578 "foo.c",
579 ":foo_genlex_l",
580 ]`,
581 }},
582 },
583 })
584}
Cole Faust6b29f592022-08-09 09:50:56 -0700585
586func TestCcBinaryRuntimeLibs(t *testing.T) {
587 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
588 description: "cc_binary with runtime libs",
589 blueprint: `
590cc_library {
591 name: "bar",
592 srcs: ["b.cc"],
593}
594
595{rule_name} {
596 name: "foo",
597 srcs: ["a.cc"],
598 runtime_libs: ["bar"],
599}
600`,
601 targets: []testBazelTarget{
602 {"cc_library_static", "bar_bp2build_cc_library_static", AttrNameToString{
603 "local_includes": `["."]`,
604 "srcs": `["b.cc"]`,
605 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
606 },
607 },
608 {"cc_library_shared", "bar", AttrNameToString{
609 "local_includes": `["."]`,
610 "srcs": `["b.cc"]`,
611 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
612 },
613 },
614 {"cc_binary", "foo", AttrNameToString{
615 "local_includes": `["."]`,
616 "srcs": `["a.cc"]`,
617 "runtime_deps": `[":bar"]`,
618 },
619 },
620 },
621 })
622}
Cole Faust5fa4e962022-08-22 14:31:04 -0700623
624func TestCcBinaryWithInstructionSet(t *testing.T) {
625 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
626 description: "instruction set",
627 blueprint: `
628{rule_name} {
629 name: "foo",
630 arch: {
631 arm: {
632 instruction_set: "arm",
633 }
634 }
635}
636`,
637 targets: []testBazelTarget{
638 {"cc_binary", "foo", AttrNameToString{
639 "features": `select({
640 "//build/bazel/platforms/arch:arm": [
641 "arm_isa_arm",
642 "-arm_isa_thumb",
643 ],
644 "//conditions:default": [],
645 })`,
646 "local_includes": `["."]`,
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -0500647 }},
648 },
649 })
650}
651
652func TestCcBinaryEmptySuffix(t *testing.T) {
653 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
654 description: "binary with empty suffix",
655 blueprint: `
656{rule_name} {
657 name: "foo",
658 suffix: "",
659}`,
660 targets: []testBazelTarget{
661 {"cc_binary", "foo", AttrNameToString{
662 "local_includes": `["."]`,
663 "suffix": `""`,
664 }},
665 },
666 })
667}
668
669func TestCcBinarySuffix(t *testing.T) {
670 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
671 description: "binary with suffix",
672 blueprint: `
673{rule_name} {
674 name: "foo",
675 suffix: "-suf",
676}
677`,
678 targets: []testBazelTarget{
679 {"cc_binary", "foo", AttrNameToString{
680 "local_includes": `["."]`,
681 "suffix": `"-suf"`,
682 }},
683 },
684 })
685}
686
687func TestCcArchVariantBinarySuffix(t *testing.T) {
688 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
689 description: "binary with suffix",
690 blueprint: `
691{rule_name} {
692 name: "foo",
693 arch: {
694 arm64: { suffix: "-64" },
695 arm: { suffix: "-32" },
696 },
697}
698`,
699 targets: []testBazelTarget{
700 {"cc_binary", "foo", AttrNameToString{
701 "local_includes": `["."]`,
702 "suffix": `select({
703 "//build/bazel/platforms/arch:arm": "-32",
704 "//build/bazel/platforms/arch:arm64": "-64",
705 "//conditions:default": None,
706 })`,
707 }},
Cole Faust5fa4e962022-08-22 14:31:04 -0700708 },
709 })
710}
Trevor Radcliffecee4e052022-09-06 19:31:25 +0000711
712func TestCcBinaryWithSyspropSrcs(t *testing.T) {
713 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
714 description: "cc_binary with sysprop sources",
715 blueprint: `
716{rule_name} {
717 name: "foo",
718 srcs: [
719 "bar.sysprop",
720 "baz.sysprop",
721 "blah.cpp",
722 ],
723 min_sdk_version: "5",
724}`,
725 targets: []testBazelTarget{
726 {"sysprop_library", "foo_sysprop_library", AttrNameToString{
727 "srcs": `[
728 "bar.sysprop",
729 "baz.sysprop",
730 ]`,
731 }},
732 {"cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
733 "dep": `":foo_sysprop_library"`,
734 "min_sdk_version": `"5"`,
735 }},
736 {"cc_binary", "foo", AttrNameToString{
737 "srcs": `["blah.cpp"]`,
738 "local_includes": `["."]`,
739 "min_sdk_version": `"5"`,
740 "whole_archive_deps": `[":foo_cc_sysprop_library_static"]`,
741 }},
742 },
743 })
744}
745
746func TestCcBinaryWithSyspropSrcsSomeConfigs(t *testing.T) {
747 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
748 description: "cc_binary with sysprop sources in some configs but not others",
749 blueprint: `
750{rule_name} {
751 name: "foo",
752 srcs: [
753 "blah.cpp",
754 ],
755 target: {
756 android: {
757 srcs: ["bar.sysprop"],
758 },
759 },
760 min_sdk_version: "5",
761}`,
762 targets: []testBazelTarget{
763 {"sysprop_library", "foo_sysprop_library", AttrNameToString{
764 "srcs": `select({
765 "//build/bazel/platforms/os:android": ["bar.sysprop"],
766 "//conditions:default": [],
767 })`,
768 }},
769 {"cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
770 "dep": `":foo_sysprop_library"`,
771 "min_sdk_version": `"5"`,
772 }},
773 {"cc_binary", "foo", AttrNameToString{
774 "srcs": `["blah.cpp"]`,
775 "local_includes": `["."]`,
776 "min_sdk_version": `"5"`,
777 "whole_archive_deps": `select({
778 "//build/bazel/platforms/os:android": [":foo_cc_sysprop_library_static"],
779 "//conditions:default": [],
780 })`,
781 }},
782 },
783 })
784}