blob: 6ec37031b829736726eb9fe176e2302b82c410a3 [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,`,
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +0000368 bazelAttr: AttrNameToString{"features": `["-link_crt"]`},
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,`,
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +0000411 bazelAttr: AttrNameToString{"features": `["-use_libcrt"]`},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400412 },
413 {
414 description: "no_libcrt: false",
415 soongProperty: `no_libcrt: false,`,
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +0000416 bazelAttr: AttrNameToString{},
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({
Trevor Radcliffe5f0c2ac2023-05-15 18:00:59 +0000647 "//build/bazel/platforms/arch:arm": ["arm_isa_arm"],
Cole Faust5fa4e962022-08-22 14:31:04 -0700648 "//conditions:default": [],
649 })`,
650 "local_includes": `["."]`,
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -0500651 }},
652 },
653 })
654}
655
656func TestCcBinaryEmptySuffix(t *testing.T) {
657 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
658 description: "binary with empty suffix",
659 blueprint: `
660{rule_name} {
661 name: "foo",
662 suffix: "",
663}`,
664 targets: []testBazelTarget{
665 {"cc_binary", "foo", AttrNameToString{
666 "local_includes": `["."]`,
667 "suffix": `""`,
668 }},
669 },
670 })
671}
672
673func TestCcBinarySuffix(t *testing.T) {
674 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
675 description: "binary with suffix",
676 blueprint: `
677{rule_name} {
678 name: "foo",
679 suffix: "-suf",
680}
681`,
682 targets: []testBazelTarget{
683 {"cc_binary", "foo", AttrNameToString{
684 "local_includes": `["."]`,
685 "suffix": `"-suf"`,
686 }},
687 },
688 })
689}
690
691func TestCcArchVariantBinarySuffix(t *testing.T) {
692 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
693 description: "binary with suffix",
694 blueprint: `
695{rule_name} {
696 name: "foo",
697 arch: {
698 arm64: { suffix: "-64" },
699 arm: { suffix: "-32" },
700 },
701}
702`,
703 targets: []testBazelTarget{
704 {"cc_binary", "foo", AttrNameToString{
705 "local_includes": `["."]`,
706 "suffix": `select({
707 "//build/bazel/platforms/arch:arm": "-32",
708 "//build/bazel/platforms/arch:arm64": "-64",
709 "//conditions:default": None,
710 })`,
711 }},
Cole Faust5fa4e962022-08-22 14:31:04 -0700712 },
713 })
714}
Trevor Radcliffecee4e052022-09-06 19:31:25 +0000715
716func TestCcBinaryWithSyspropSrcs(t *testing.T) {
717 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
718 description: "cc_binary with sysprop sources",
719 blueprint: `
720{rule_name} {
721 name: "foo",
722 srcs: [
723 "bar.sysprop",
724 "baz.sysprop",
725 "blah.cpp",
726 ],
727 min_sdk_version: "5",
728}`,
729 targets: []testBazelTarget{
730 {"sysprop_library", "foo_sysprop_library", AttrNameToString{
731 "srcs": `[
732 "bar.sysprop",
733 "baz.sysprop",
734 ]`,
735 }},
736 {"cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
737 "dep": `":foo_sysprop_library"`,
738 "min_sdk_version": `"5"`,
739 }},
740 {"cc_binary", "foo", AttrNameToString{
741 "srcs": `["blah.cpp"]`,
742 "local_includes": `["."]`,
743 "min_sdk_version": `"5"`,
744 "whole_archive_deps": `[":foo_cc_sysprop_library_static"]`,
745 }},
746 },
747 })
748}
749
750func TestCcBinaryWithSyspropSrcsSomeConfigs(t *testing.T) {
751 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
752 description: "cc_binary with sysprop sources in some configs but not others",
753 blueprint: `
754{rule_name} {
755 name: "foo",
756 srcs: [
757 "blah.cpp",
758 ],
759 target: {
760 android: {
761 srcs: ["bar.sysprop"],
762 },
763 },
764 min_sdk_version: "5",
765}`,
766 targets: []testBazelTarget{
767 {"sysprop_library", "foo_sysprop_library", AttrNameToString{
768 "srcs": `select({
769 "//build/bazel/platforms/os:android": ["bar.sysprop"],
770 "//conditions:default": [],
771 })`,
772 }},
773 {"cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
774 "dep": `":foo_sysprop_library"`,
775 "min_sdk_version": `"5"`,
776 }},
777 {"cc_binary", "foo", AttrNameToString{
778 "srcs": `["blah.cpp"]`,
779 "local_includes": `["."]`,
780 "min_sdk_version": `"5"`,
781 "whole_archive_deps": `select({
782 "//build/bazel/platforms/os:android": [":foo_cc_sysprop_library_static"],
783 "//conditions:default": [],
784 })`,
785 }},
786 },
787 })
788}
Trevor Radcliffedb7e0262022-10-28 16:48:18 +0000789
790func TestCcBinaryWithIntegerOverflowProperty(t *testing.T) {
791 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
792 description: "cc_binary with integer overflow property specified",
793 blueprint: `
794{rule_name} {
795 name: "foo",
796 sanitize: {
797 integer_overflow: true,
798 },
799}`,
800 targets: []testBazelTarget{
801 {"cc_binary", "foo", AttrNameToString{
802 "local_includes": `["."]`,
803 "features": `["ubsan_integer_overflow"]`,
804 }},
805 },
806 })
807}
808
809func TestCcBinaryWithMiscUndefinedProperty(t *testing.T) {
810 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
811 description: "cc_binary with miscellaneous properties specified",
812 blueprint: `
813{rule_name} {
814 name: "foo",
815 sanitize: {
816 misc_undefined: ["undefined", "nullability"],
817 },
818}`,
819 targets: []testBazelTarget{
820 {"cc_binary", "foo", AttrNameToString{
821 "local_includes": `["."]`,
822 "features": `[
823 "ubsan_undefined",
824 "ubsan_nullability",
825 ]`,
826 }},
827 },
828 })
829}
830
831func TestCcBinaryWithUBSanPropertiesArchSpecific(t *testing.T) {
832 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
833 description: "cc_binary has correct feature select when UBSan props are specified in arch specific blocks",
834 blueprint: `
835{rule_name} {
836 name: "foo",
837 sanitize: {
838 misc_undefined: ["undefined", "nullability"],
839 },
840 target: {
841 android: {
842 sanitize: {
843 misc_undefined: ["alignment"],
844 },
845 },
846 linux_glibc: {
847 sanitize: {
848 integer_overflow: true,
849 },
850 },
851 },
852}`,
853 targets: []testBazelTarget{
854 {"cc_binary", "foo", AttrNameToString{
855 "local_includes": `["."]`,
856 "features": `[
857 "ubsan_undefined",
858 "ubsan_nullability",
859 ] + select({
860 "//build/bazel/platforms/os:android": ["ubsan_alignment"],
861 "//build/bazel/platforms/os:linux_glibc": ["ubsan_integer_overflow"],
862 "//conditions:default": [],
863 })`,
864 }},
865 },
866 })
867}
Trevor Radcliffe56b1a2b2023-02-06 21:58:30 +0000868
869func TestCcBinaryWithThinLto(t *testing.T) {
870 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
871 description: "cc_binary has correct features when thin LTO is enabled",
872 blueprint: `
873{rule_name} {
874 name: "foo",
875 lto: {
876 thin: true,
877 },
878}`,
879 targets: []testBazelTarget{
880 {"cc_binary", "foo", AttrNameToString{
881 "local_includes": `["."]`,
882 "features": `["android_thin_lto"]`,
883 }},
884 },
885 })
886}
887
888func TestCcBinaryWithLtoNever(t *testing.T) {
889 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
890 description: "cc_binary has correct features when LTO is explicitly disabled",
891 blueprint: `
892{rule_name} {
893 name: "foo",
894 lto: {
895 never: true,
896 },
897}`,
898 targets: []testBazelTarget{
899 {"cc_binary", "foo", AttrNameToString{
900 "local_includes": `["."]`,
901 "features": `["-android_thin_lto"]`,
902 }},
903 },
904 })
905}
906
907func TestCcBinaryWithThinLtoArchSpecific(t *testing.T) {
908 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
909 description: "cc_binary has correct features when LTO differs across arch and os variants",
910 blueprint: `
911{rule_name} {
912 name: "foo",
913 target: {
914 android: {
915 lto: {
916 thin: true,
917 },
918 },
919 },
920 arch: {
921 riscv64: {
922 lto: {
923 thin: false,
924 },
925 },
926 },
927}`,
928 targets: []testBazelTarget{
929 {"cc_binary", "foo", AttrNameToString{
930 "local_includes": `["."]`,
931 "features": `select({
932 "//build/bazel/platforms/os_arch:android_arm": ["android_thin_lto"],
933 "//build/bazel/platforms/os_arch:android_arm64": ["android_thin_lto"],
934 "//build/bazel/platforms/os_arch:android_riscv64": ["-android_thin_lto"],
935 "//build/bazel/platforms/os_arch:android_x86": ["android_thin_lto"],
936 "//build/bazel/platforms/os_arch:android_x86_64": ["android_thin_lto"],
937 "//conditions:default": [],
938 })`,
939 }},
940 },
941 })
942}
943
944func TestCcBinaryWithThinLtoDisabledDefaultEnabledVariant(t *testing.T) {
945 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
946 description: "cc_binary has correct features when LTO disabled by default but enabled on a particular variant",
947 blueprint: `
948{rule_name} {
949 name: "foo",
950 lto: {
951 never: true,
952 },
953 target: {
954 android: {
955 lto: {
956 thin: true,
957 never: false,
958 },
959 },
960 },
961}`,
962 targets: []testBazelTarget{
963 {"cc_binary", "foo", AttrNameToString{
964 "local_includes": `["."]`,
965 "features": `select({
966 "//build/bazel/platforms/os:android": ["android_thin_lto"],
967 "//conditions:default": ["-android_thin_lto"],
968 })`,
969 }},
970 },
971 })
972}
973
974func TestCcBinaryWithThinLtoAndWholeProgramVtables(t *testing.T) {
975 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
976 description: "cc_binary has correct features when thin LTO is enabled with whole_program_vtables",
977 blueprint: `
978{rule_name} {
979 name: "foo",
980 lto: {
981 thin: true,
982 },
983 whole_program_vtables: true,
984}`,
985 targets: []testBazelTarget{
986 {"cc_binary", "foo", AttrNameToString{
987 "local_includes": `["."]`,
988 "features": `[
989 "android_thin_lto",
990 "android_thin_lto_whole_program_vtables",
991 ]`,
992 }},
993 },
994 })
995}
Trevor Radcliffea8b44162023-04-14 18:25:24 +0000996
997func TestCcBinaryHiddenVisibilityConvertedToFeature(t *testing.T) {
998 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
999 description: "cc_binary changes hidden visibility to feature",
1000 blueprint: `
1001{rule_name} {
1002 name: "foo",
1003 cflags: ["-fvisibility=hidden"],
1004}`,
1005 targets: []testBazelTarget{
1006 {"cc_binary", "foo", AttrNameToString{
1007 "local_includes": `["."]`,
1008 "features": `["visibility_hidden"]`,
1009 }},
1010 },
1011 })
1012}
1013
1014func TestCcBinaryHiddenVisibilityConvertedToFeatureOsSpecific(t *testing.T) {
1015 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
1016 description: "cc_binary changes hidden visibility to feature for specific os",
1017 blueprint: `
1018{rule_name} {
1019 name: "foo",
1020 target: {
1021 android: {
1022 cflags: ["-fvisibility=hidden"],
1023 },
1024 },
1025}`,
1026 targets: []testBazelTarget{
1027 {"cc_binary", "foo", AttrNameToString{
1028 "local_includes": `["."]`,
1029 "features": `select({
1030 "//build/bazel/platforms/os:android": ["visibility_hidden"],
1031 "//conditions:default": [],
1032 })`,
1033 }},
1034 },
1035 })
1036}