blob: fe156dffdb235e68a8f575a50d927d72f0d5b750 [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
Trevor Radcliffeea6a45d2022-09-20 18:58:01 +0000231func TestCcBinaryLdflagsSplitBySpaceExceptSoongAdded(t *testing.T) {
232 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
233 description: "ldflags are split by spaces except for the ones added by soong (version script and dynamic list)",
234 blueprint: `
235{rule_name} {
236 name: "foo",
237 ldflags: [
238 "--nospace_flag",
239 "-z spaceflag",
240 ],
241 version_script: "version_script",
242 dynamic_list: "dynamic.list",
243 include_build_directory: false,
244}
245`,
246 targets: []testBazelTarget{
247 {"cc_binary", "foo", AttrNameToString{
248 "additional_linker_inputs": `[
249 "version_script",
250 "dynamic.list",
251 ]`,
252 "linkopts": `[
253 "--nospace_flag",
254 "-z",
255 "spaceflag",
256 "-Wl,--version-script,$(location version_script)",
257 "-Wl,--dynamic-list,$(location dynamic.list)",
258 ]`,
259 }}},
260 })
261}
262
Liz Kammer2b8004b2021-10-04 13:55:44 -0400263func TestCcBinarySplitSrcsByLang(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500264 runCcHostBinaryTestCase(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400265 description: "split srcs by lang",
266 blueprint: `
267{rule_name} {
268 name: "foo",
269 srcs: [
270 "asonly.S",
271 "conly.c",
272 "cpponly.cpp",
273 ":fg_foo",
274 ],
275 include_build_directory: false,
276}
277` + simpleModuleDoNotConvertBp2build("filegroup", "fg_foo"),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500278 targets: []testBazelTarget{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000279 {"cc_binary", "foo", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500280 "srcs": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400281 "cpponly.cpp",
282 ":fg_foo_cpp_srcs",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500283 ]`,
284 "srcs_as": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400285 "asonly.S",
286 ":fg_foo_as_srcs",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500287 ]`,
288 "srcs_c": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400289 "conly.c",
290 ":fg_foo_c_srcs",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500291 ]`,
292 },
293 },
294 },
Liz Kammer2b8004b2021-10-04 13:55:44 -0400295 })
296}
297
298func TestCcBinaryDoNotDistinguishBetweenDepsAndImplementationDeps(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500299 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400300 description: "no implementation deps",
301 blueprint: `
Liz Kammere6583482021-10-19 13:56:10 -0400302genrule {
303 name: "generated_hdr",
304 cmd: "nothing to see here",
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400305 bazel_module: { bp2build_available: false },
Liz Kammere6583482021-10-19 13:56:10 -0400306}
307
308genrule {
309 name: "export_generated_hdr",
310 cmd: "nothing to see here",
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400311 bazel_module: { bp2build_available: false },
Liz Kammere6583482021-10-19 13:56:10 -0400312}
313
Liz Kammer2b8004b2021-10-04 13:55:44 -0400314{rule_name} {
315 name: "foo",
Liz Kammere6583482021-10-19 13:56:10 -0400316 srcs: ["foo.cpp"],
Liz Kammer2b8004b2021-10-04 13:55:44 -0400317 shared_libs: ["implementation_shared_dep", "shared_dep"],
318 export_shared_lib_headers: ["shared_dep"],
319 static_libs: ["implementation_static_dep", "static_dep"],
320 export_static_lib_headers: ["static_dep", "whole_static_dep"],
321 whole_static_libs: ["not_explicitly_exported_whole_static_dep", "whole_static_dep"],
322 include_build_directory: false,
Liz Kammere6583482021-10-19 13:56:10 -0400323 generated_headers: ["generated_hdr", "export_generated_hdr"],
324 export_generated_headers: ["export_generated_hdr"],
Liz Kammer2b8004b2021-10-04 13:55:44 -0400325}
326` +
327 simpleModuleDoNotConvertBp2build("cc_library_static", "static_dep") +
328 simpleModuleDoNotConvertBp2build("cc_library_static", "implementation_static_dep") +
329 simpleModuleDoNotConvertBp2build("cc_library_static", "whole_static_dep") +
330 simpleModuleDoNotConvertBp2build("cc_library_static", "not_explicitly_exported_whole_static_dep") +
331 simpleModuleDoNotConvertBp2build("cc_library", "shared_dep") +
332 simpleModuleDoNotConvertBp2build("cc_library", "implementation_shared_dep"),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500333 targets: []testBazelTarget{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000334 {"cc_binary", "foo", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500335 "deps": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400336 ":implementation_static_dep",
337 ":static_dep",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500338 ]`,
339 "dynamic_deps": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400340 ":implementation_shared_dep",
341 ":shared_dep",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500342 ]`,
343 "srcs": `[
Liz Kammere6583482021-10-19 13:56:10 -0400344 "foo.cpp",
345 ":generated_hdr",
346 ":export_generated_hdr",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500347 ]`,
348 "whole_archive_deps": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400349 ":not_explicitly_exported_whole_static_dep",
350 ":whole_static_dep",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500351 ]`,
Liz Kammer1263d9b2021-12-10 14:28:20 -0500352 "local_includes": `["."]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500353 },
354 },
355 },
Liz Kammer2b8004b2021-10-04 13:55:44 -0400356 })
357}
358
359func TestCcBinaryNocrtTests(t *testing.T) {
360 baseTestCases := []struct {
361 description string
362 soongProperty string
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000363 bazelAttr AttrNameToString
Liz Kammer2b8004b2021-10-04 13:55:44 -0400364 }{
365 {
366 description: "nocrt: true",
367 soongProperty: `nocrt: true,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000368 bazelAttr: AttrNameToString{"link_crt": `False`},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400369 },
370 {
371 description: "nocrt: false",
372 soongProperty: `nocrt: false,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000373 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400374 },
375 {
376 description: "nocrt: not set",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000377 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400378 },
379 }
380
381 baseBlueprint := `{rule_name} {
382 name: "foo",%s
383 include_build_directory: false,
384}
385`
386
Liz Kammer2b8004b2021-10-04 13:55:44 -0400387 for _, btc := range baseTestCases {
388 prop := btc.soongProperty
389 if len(prop) > 0 {
390 prop = "\n" + prop
391 }
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500392 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400393 description: btc.description,
394 blueprint: fmt.Sprintf(baseBlueprint, prop),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500395 targets: []testBazelTarget{
396 {"cc_binary", "foo", btc.bazelAttr},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400397 },
398 })
399 }
400}
401
402func TestCcBinaryNo_libcrtTests(t *testing.T) {
403 baseTestCases := []struct {
404 description string
405 soongProperty string
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000406 bazelAttr AttrNameToString
Liz Kammer2b8004b2021-10-04 13:55:44 -0400407 }{
408 {
409 description: "no_libcrt: true",
410 soongProperty: `no_libcrt: true,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000411 bazelAttr: AttrNameToString{"use_libcrt": `False`},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400412 },
413 {
414 description: "no_libcrt: false",
415 soongProperty: `no_libcrt: false,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000416 bazelAttr: AttrNameToString{"use_libcrt": `True`},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400417 },
418 {
419 description: "no_libcrt: not set",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000420 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400421 },
422 }
423
424 baseBlueprint := `{rule_name} {
425 name: "foo",%s
426 include_build_directory: false,
427}
428`
429
Liz Kammer2b8004b2021-10-04 13:55:44 -0400430 for _, btc := range baseTestCases {
431 prop := btc.soongProperty
432 if len(prop) > 0 {
433 prop = "\n" + prop
434 }
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500435 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400436 description: btc.description,
437 blueprint: fmt.Sprintf(baseBlueprint, prop),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500438 targets: []testBazelTarget{
439 {"cc_binary", "foo", btc.bazelAttr},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400440 },
441 })
442 }
443}
444
445func TestCcBinaryPropertiesToFeatures(t *testing.T) {
446 baseTestCases := []struct {
447 description string
448 soongProperty string
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000449 bazelAttr AttrNameToString
Liz Kammer2b8004b2021-10-04 13:55:44 -0400450 }{
451 {
452 description: "pack_relocation: true",
453 soongProperty: `pack_relocations: true,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000454 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400455 },
456 {
457 description: "pack_relocations: false",
458 soongProperty: `pack_relocations: false,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000459 bazelAttr: AttrNameToString{"features": `["disable_pack_relocations"]`},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400460 },
461 {
462 description: "pack_relocations: not set",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000463 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400464 },
465 {
466 description: "pack_relocation: true",
467 soongProperty: `allow_undefined_symbols: true,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000468 bazelAttr: AttrNameToString{"features": `["-no_undefined_symbols"]`},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400469 },
470 {
471 description: "allow_undefined_symbols: false",
472 soongProperty: `allow_undefined_symbols: false,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000473 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400474 },
475 {
476 description: "allow_undefined_symbols: not set",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000477 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400478 },
479 }
480
481 baseBlueprint := `{rule_name} {
482 name: "foo",%s
483 include_build_directory: false,
484}
485`
Liz Kammer2b8004b2021-10-04 13:55:44 -0400486 for _, btc := range baseTestCases {
487 prop := btc.soongProperty
488 if len(prop) > 0 {
489 prop = "\n" + prop
490 }
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500491 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400492 description: btc.description,
493 blueprint: fmt.Sprintf(baseBlueprint, prop),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500494 targets: []testBazelTarget{
495 {"cc_binary", "foo", btc.bazelAttr},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400496 },
497 })
498 }
499}
Liz Kammer12615db2021-09-28 09:19:17 -0400500
501func TestCcBinarySharedProto(t *testing.T) {
502 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
503 blueprint: soongCcProtoLibraries + `{rule_name} {
504 name: "foo",
505 srcs: ["foo.proto"],
506 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -0400507 },
508 include_build_directory: false,
509}`,
510 targets: []testBazelTarget{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000511 {"proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400512 "srcs": `["foo.proto"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000513 }}, {"cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400514 "deps": `[":foo_proto"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000515 }}, {"cc_binary", "foo", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400516 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
517 "whole_archive_deps": `[":foo_cc_proto_lite"]`,
518 }},
519 },
520 })
521}
522
523func TestCcBinaryStaticProto(t *testing.T) {
524 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
525 blueprint: soongCcProtoLibraries + `{rule_name} {
526 name: "foo",
527 srcs: ["foo.proto"],
528 static_executable: true,
529 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -0400530 },
531 include_build_directory: false,
532}`,
533 targets: []testBazelTarget{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000534 {"proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400535 "srcs": `["foo.proto"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000536 }}, {"cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400537 "deps": `[":foo_proto"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000538 }}, {"cc_binary", "foo", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400539 "deps": `[":libprotobuf-cpp-lite"]`,
540 "whole_archive_deps": `[":foo_cc_proto_lite"]`,
541 "linkshared": `False`,
542 }},
543 },
544 })
545}
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000546
547func TestCcBinaryConvertLex(t *testing.T) {
548 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
549 description: `.l and .ll sources converted to .c and .cc`,
550 blueprint: `
551{rule_name} {
552 name: "foo",
553 srcs: ["foo.c", "bar.cc", "foo1.l", "foo2.l", "bar1.ll", "bar2.ll"],
554 lex: { flags: ["--foo_opt", "--bar_opt"] },
555 include_build_directory: false,
556}
557`,
558 targets: []testBazelTarget{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000559 {"genlex", "foo_genlex_l", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000560 "srcs": `[
561 "foo1.l",
562 "foo2.l",
563 ]`,
564 "lexopts": `[
565 "--foo_opt",
566 "--bar_opt",
567 ]`,
568 }},
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000569 {"genlex", "foo_genlex_ll", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000570 "srcs": `[
571 "bar1.ll",
572 "bar2.ll",
573 ]`,
574 "lexopts": `[
575 "--foo_opt",
576 "--bar_opt",
577 ]`,
578 }},
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000579 {"cc_binary", "foo", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000580 "srcs": `[
581 "bar.cc",
582 ":foo_genlex_ll",
583 ]`,
584 "srcs_c": `[
585 "foo.c",
586 ":foo_genlex_l",
587 ]`,
588 }},
589 },
590 })
591}
Cole Faust6b29f592022-08-09 09:50:56 -0700592
593func TestCcBinaryRuntimeLibs(t *testing.T) {
594 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
595 description: "cc_binary with runtime libs",
596 blueprint: `
597cc_library {
598 name: "bar",
599 srcs: ["b.cc"],
600}
601
602{rule_name} {
603 name: "foo",
604 srcs: ["a.cc"],
605 runtime_libs: ["bar"],
606}
607`,
608 targets: []testBazelTarget{
609 {"cc_library_static", "bar_bp2build_cc_library_static", AttrNameToString{
610 "local_includes": `["."]`,
611 "srcs": `["b.cc"]`,
612 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
613 },
614 },
615 {"cc_library_shared", "bar", AttrNameToString{
616 "local_includes": `["."]`,
617 "srcs": `["b.cc"]`,
618 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
619 },
620 },
621 {"cc_binary", "foo", AttrNameToString{
622 "local_includes": `["."]`,
623 "srcs": `["a.cc"]`,
624 "runtime_deps": `[":bar"]`,
625 },
626 },
627 },
628 })
629}
Cole Faust5fa4e962022-08-22 14:31:04 -0700630
631func TestCcBinaryWithInstructionSet(t *testing.T) {
632 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
633 description: "instruction set",
634 blueprint: `
635{rule_name} {
636 name: "foo",
637 arch: {
638 arm: {
639 instruction_set: "arm",
640 }
641 }
642}
643`,
644 targets: []testBazelTarget{
645 {"cc_binary", "foo", AttrNameToString{
646 "features": `select({
647 "//build/bazel/platforms/arch:arm": [
648 "arm_isa_arm",
649 "-arm_isa_thumb",
650 ],
651 "//conditions:default": [],
652 })`,
653 "local_includes": `["."]`,
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -0500654 }},
655 },
656 })
657}
658
659func TestCcBinaryEmptySuffix(t *testing.T) {
660 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
661 description: "binary with empty suffix",
662 blueprint: `
663{rule_name} {
664 name: "foo",
665 suffix: "",
666}`,
667 targets: []testBazelTarget{
668 {"cc_binary", "foo", AttrNameToString{
669 "local_includes": `["."]`,
670 "suffix": `""`,
671 }},
672 },
673 })
674}
675
676func TestCcBinarySuffix(t *testing.T) {
677 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
678 description: "binary with suffix",
679 blueprint: `
680{rule_name} {
681 name: "foo",
682 suffix: "-suf",
683}
684`,
685 targets: []testBazelTarget{
686 {"cc_binary", "foo", AttrNameToString{
687 "local_includes": `["."]`,
688 "suffix": `"-suf"`,
689 }},
690 },
691 })
692}
693
694func TestCcArchVariantBinarySuffix(t *testing.T) {
695 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
696 description: "binary with suffix",
697 blueprint: `
698{rule_name} {
699 name: "foo",
700 arch: {
701 arm64: { suffix: "-64" },
702 arm: { suffix: "-32" },
703 },
704}
705`,
706 targets: []testBazelTarget{
707 {"cc_binary", "foo", AttrNameToString{
708 "local_includes": `["."]`,
709 "suffix": `select({
710 "//build/bazel/platforms/arch:arm": "-32",
711 "//build/bazel/platforms/arch:arm64": "-64",
712 "//conditions:default": None,
713 })`,
714 }},
Cole Faust5fa4e962022-08-22 14:31:04 -0700715 },
716 })
717}
Trevor Radcliffecee4e052022-09-06 19:31:25 +0000718
719func TestCcBinaryWithSyspropSrcs(t *testing.T) {
720 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
721 description: "cc_binary with sysprop sources",
722 blueprint: `
723{rule_name} {
724 name: "foo",
725 srcs: [
726 "bar.sysprop",
727 "baz.sysprop",
728 "blah.cpp",
729 ],
730 min_sdk_version: "5",
731}`,
732 targets: []testBazelTarget{
733 {"sysprop_library", "foo_sysprop_library", AttrNameToString{
734 "srcs": `[
735 "bar.sysprop",
736 "baz.sysprop",
737 ]`,
738 }},
739 {"cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
740 "dep": `":foo_sysprop_library"`,
741 "min_sdk_version": `"5"`,
742 }},
743 {"cc_binary", "foo", AttrNameToString{
744 "srcs": `["blah.cpp"]`,
745 "local_includes": `["."]`,
746 "min_sdk_version": `"5"`,
747 "whole_archive_deps": `[":foo_cc_sysprop_library_static"]`,
748 }},
749 },
750 })
751}
752
753func TestCcBinaryWithSyspropSrcsSomeConfigs(t *testing.T) {
754 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
755 description: "cc_binary with sysprop sources in some configs but not others",
756 blueprint: `
757{rule_name} {
758 name: "foo",
759 srcs: [
760 "blah.cpp",
761 ],
762 target: {
763 android: {
764 srcs: ["bar.sysprop"],
765 },
766 },
767 min_sdk_version: "5",
768}`,
769 targets: []testBazelTarget{
770 {"sysprop_library", "foo_sysprop_library", AttrNameToString{
771 "srcs": `select({
772 "//build/bazel/platforms/os:android": ["bar.sysprop"],
773 "//conditions:default": [],
774 })`,
775 }},
776 {"cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
777 "dep": `":foo_sysprop_library"`,
778 "min_sdk_version": `"5"`,
779 }},
780 {"cc_binary", "foo", AttrNameToString{
781 "srcs": `["blah.cpp"]`,
782 "local_includes": `["."]`,
783 "min_sdk_version": `"5"`,
784 "whole_archive_deps": `select({
785 "//build/bazel/platforms/os:android": [":foo_cc_sysprop_library_static"],
786 "//conditions:default": [],
787 })`,
788 }},
789 },
790 })
791}
Trevor Radcliffedb7e0262022-10-28 16:48:18 +0000792
793func TestCcBinaryWithIntegerOverflowProperty(t *testing.T) {
794 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
795 description: "cc_binary with integer overflow property specified",
796 blueprint: `
797{rule_name} {
798 name: "foo",
799 sanitize: {
800 integer_overflow: true,
801 },
802}`,
803 targets: []testBazelTarget{
804 {"cc_binary", "foo", AttrNameToString{
805 "local_includes": `["."]`,
806 "features": `["ubsan_integer_overflow"]`,
807 }},
808 },
809 })
810}
811
812func TestCcBinaryWithMiscUndefinedProperty(t *testing.T) {
813 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
814 description: "cc_binary with miscellaneous properties specified",
815 blueprint: `
816{rule_name} {
817 name: "foo",
818 sanitize: {
819 misc_undefined: ["undefined", "nullability"],
820 },
821}`,
822 targets: []testBazelTarget{
823 {"cc_binary", "foo", AttrNameToString{
824 "local_includes": `["."]`,
825 "features": `[
826 "ubsan_undefined",
827 "ubsan_nullability",
828 ]`,
829 }},
830 },
831 })
832}
833
834func TestCcBinaryWithUBSanPropertiesArchSpecific(t *testing.T) {
835 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
836 description: "cc_binary has correct feature select when UBSan props are specified in arch specific blocks",
837 blueprint: `
838{rule_name} {
839 name: "foo",
840 sanitize: {
841 misc_undefined: ["undefined", "nullability"],
842 },
843 target: {
844 android: {
845 sanitize: {
846 misc_undefined: ["alignment"],
847 },
848 },
849 linux_glibc: {
850 sanitize: {
851 integer_overflow: true,
852 },
853 },
854 },
855}`,
856 targets: []testBazelTarget{
857 {"cc_binary", "foo", AttrNameToString{
858 "local_includes": `["."]`,
859 "features": `[
860 "ubsan_undefined",
861 "ubsan_nullability",
862 ] + select({
863 "//build/bazel/platforms/os:android": ["ubsan_alignment"],
864 "//build/bazel/platforms/os:linux_glibc": ["ubsan_integer_overflow"],
865 "//conditions:default": [],
866 })`,
867 }},
868 },
869 })
870}