blob: 3d3b860fabadd6a23fcc59fedd84148136facbe8 [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 ]`,
Trevor Radcliffef06dd912023-05-19 14:51:41 +0000225 "features": `["android_cfi_exports_map"]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500226 },
227 },
228 },
Liz Kammer2b8004b2021-10-04 13:55:44 -0400229 })
230}
231
Trevor Radcliffeea6a45d2022-09-20 18:58:01 +0000232func TestCcBinaryLdflagsSplitBySpaceExceptSoongAdded(t *testing.T) {
233 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
234 description: "ldflags are split by spaces except for the ones added by soong (version script and dynamic list)",
235 blueprint: `
236{rule_name} {
237 name: "foo",
238 ldflags: [
239 "--nospace_flag",
240 "-z spaceflag",
241 ],
242 version_script: "version_script",
243 dynamic_list: "dynamic.list",
244 include_build_directory: false,
245}
246`,
247 targets: []testBazelTarget{
248 {"cc_binary", "foo", AttrNameToString{
249 "additional_linker_inputs": `[
250 "version_script",
251 "dynamic.list",
252 ]`,
Trevor Radcliffef06dd912023-05-19 14:51:41 +0000253 "features": `["android_cfi_exports_map"]`,
Trevor Radcliffeea6a45d2022-09-20 18:58:01 +0000254 "linkopts": `[
255 "--nospace_flag",
256 "-z",
257 "spaceflag",
258 "-Wl,--version-script,$(location version_script)",
259 "-Wl,--dynamic-list,$(location dynamic.list)",
260 ]`,
261 }}},
262 })
263}
264
Liz Kammer2b8004b2021-10-04 13:55:44 -0400265func TestCcBinarySplitSrcsByLang(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500266 runCcHostBinaryTestCase(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400267 description: "split srcs by lang",
268 blueprint: `
269{rule_name} {
270 name: "foo",
271 srcs: [
272 "asonly.S",
273 "conly.c",
274 "cpponly.cpp",
275 ":fg_foo",
276 ],
277 include_build_directory: false,
278}
Sam Delmerico130d75b2023-08-31 00:51:44 +0000279` + SimpleModuleDoNotConvertBp2build("filegroup", "fg_foo"),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500280 targets: []testBazelTarget{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000281 {"cc_binary", "foo", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500282 "srcs": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400283 "cpponly.cpp",
284 ":fg_foo_cpp_srcs",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500285 ]`,
286 "srcs_as": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400287 "asonly.S",
288 ":fg_foo_as_srcs",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500289 ]`,
290 "srcs_c": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400291 "conly.c",
292 ":fg_foo_c_srcs",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500293 ]`,
294 },
295 },
296 },
Liz Kammer2b8004b2021-10-04 13:55:44 -0400297 })
298}
299
300func TestCcBinaryDoNotDistinguishBetweenDepsAndImplementationDeps(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500301 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400302 description: "no implementation deps",
303 blueprint: `
Liz Kammere6583482021-10-19 13:56:10 -0400304genrule {
305 name: "generated_hdr",
306 cmd: "nothing to see here",
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400307 bazel_module: { bp2build_available: false },
Liz Kammere6583482021-10-19 13:56:10 -0400308}
309
310genrule {
311 name: "export_generated_hdr",
312 cmd: "nothing to see here",
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400313 bazel_module: { bp2build_available: false },
Liz Kammere6583482021-10-19 13:56:10 -0400314}
315
Liz Kammer2b8004b2021-10-04 13:55:44 -0400316{rule_name} {
317 name: "foo",
Liz Kammere6583482021-10-19 13:56:10 -0400318 srcs: ["foo.cpp"],
Liz Kammer2b8004b2021-10-04 13:55:44 -0400319 shared_libs: ["implementation_shared_dep", "shared_dep"],
320 export_shared_lib_headers: ["shared_dep"],
321 static_libs: ["implementation_static_dep", "static_dep"],
322 export_static_lib_headers: ["static_dep", "whole_static_dep"],
323 whole_static_libs: ["not_explicitly_exported_whole_static_dep", "whole_static_dep"],
324 include_build_directory: false,
Liz Kammere6583482021-10-19 13:56:10 -0400325 generated_headers: ["generated_hdr", "export_generated_hdr"],
326 export_generated_headers: ["export_generated_hdr"],
Liz Kammer2b8004b2021-10-04 13:55:44 -0400327}
328` +
Sam Delmerico130d75b2023-08-31 00:51:44 +0000329 SimpleModuleDoNotConvertBp2build("cc_library_static", "static_dep") +
330 SimpleModuleDoNotConvertBp2build("cc_library_static", "implementation_static_dep") +
331 SimpleModuleDoNotConvertBp2build("cc_library_static", "whole_static_dep") +
332 SimpleModuleDoNotConvertBp2build("cc_library_static", "not_explicitly_exported_whole_static_dep") +
333 SimpleModuleDoNotConvertBp2build("cc_library", "shared_dep") +
334 SimpleModuleDoNotConvertBp2build("cc_library", "implementation_shared_dep"),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500335 targets: []testBazelTarget{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000336 {"cc_binary", "foo", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500337 "deps": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400338 ":implementation_static_dep",
339 ":static_dep",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500340 ]`,
341 "dynamic_deps": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400342 ":implementation_shared_dep",
343 ":shared_dep",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500344 ]`,
345 "srcs": `[
Liz Kammere6583482021-10-19 13:56:10 -0400346 "foo.cpp",
347 ":generated_hdr",
348 ":export_generated_hdr",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500349 ]`,
350 "whole_archive_deps": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400351 ":not_explicitly_exported_whole_static_dep",
352 ":whole_static_dep",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500353 ]`,
Liz Kammer1263d9b2021-12-10 14:28:20 -0500354 "local_includes": `["."]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500355 },
356 },
357 },
Liz Kammer2b8004b2021-10-04 13:55:44 -0400358 })
359}
360
361func TestCcBinaryNocrtTests(t *testing.T) {
362 baseTestCases := []struct {
363 description string
364 soongProperty string
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000365 bazelAttr AttrNameToString
Liz Kammer2b8004b2021-10-04 13:55:44 -0400366 }{
367 {
368 description: "nocrt: true",
369 soongProperty: `nocrt: true,`,
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +0000370 bazelAttr: AttrNameToString{"features": `["-link_crt"]`},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400371 },
372 {
373 description: "nocrt: false",
374 soongProperty: `nocrt: false,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000375 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400376 },
377 {
378 description: "nocrt: not set",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000379 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400380 },
381 }
382
383 baseBlueprint := `{rule_name} {
384 name: "foo",%s
385 include_build_directory: false,
386}
387`
388
Liz Kammer2b8004b2021-10-04 13:55:44 -0400389 for _, btc := range baseTestCases {
390 prop := btc.soongProperty
391 if len(prop) > 0 {
392 prop = "\n" + prop
393 }
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500394 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400395 description: btc.description,
396 blueprint: fmt.Sprintf(baseBlueprint, prop),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500397 targets: []testBazelTarget{
398 {"cc_binary", "foo", btc.bazelAttr},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400399 },
400 })
401 }
402}
403
404func TestCcBinaryNo_libcrtTests(t *testing.T) {
405 baseTestCases := []struct {
406 description string
407 soongProperty string
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000408 bazelAttr AttrNameToString
Liz Kammer2b8004b2021-10-04 13:55:44 -0400409 }{
410 {
411 description: "no_libcrt: true",
412 soongProperty: `no_libcrt: true,`,
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +0000413 bazelAttr: AttrNameToString{"features": `["-use_libcrt"]`},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400414 },
415 {
416 description: "no_libcrt: false",
417 soongProperty: `no_libcrt: false,`,
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +0000418 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400419 },
420 {
421 description: "no_libcrt: not set",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000422 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400423 },
424 }
425
426 baseBlueprint := `{rule_name} {
427 name: "foo",%s
428 include_build_directory: false,
429}
430`
431
Liz Kammer2b8004b2021-10-04 13:55:44 -0400432 for _, btc := range baseTestCases {
433 prop := btc.soongProperty
434 if len(prop) > 0 {
435 prop = "\n" + prop
436 }
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500437 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400438 description: btc.description,
439 blueprint: fmt.Sprintf(baseBlueprint, prop),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500440 targets: []testBazelTarget{
441 {"cc_binary", "foo", btc.bazelAttr},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400442 },
443 })
444 }
445}
446
447func TestCcBinaryPropertiesToFeatures(t *testing.T) {
448 baseTestCases := []struct {
449 description string
450 soongProperty string
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000451 bazelAttr AttrNameToString
Liz Kammer2b8004b2021-10-04 13:55:44 -0400452 }{
453 {
454 description: "pack_relocation: true",
455 soongProperty: `pack_relocations: true,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000456 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400457 },
458 {
459 description: "pack_relocations: false",
460 soongProperty: `pack_relocations: false,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000461 bazelAttr: AttrNameToString{"features": `["disable_pack_relocations"]`},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400462 },
463 {
464 description: "pack_relocations: not set",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000465 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400466 },
467 {
468 description: "pack_relocation: true",
469 soongProperty: `allow_undefined_symbols: true,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000470 bazelAttr: AttrNameToString{"features": `["-no_undefined_symbols"]`},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400471 },
472 {
473 description: "allow_undefined_symbols: false",
474 soongProperty: `allow_undefined_symbols: false,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000475 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400476 },
477 {
478 description: "allow_undefined_symbols: not set",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000479 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400480 },
481 }
482
483 baseBlueprint := `{rule_name} {
484 name: "foo",%s
485 include_build_directory: false,
486}
487`
Liz Kammer2b8004b2021-10-04 13:55:44 -0400488 for _, btc := range baseTestCases {
489 prop := btc.soongProperty
490 if len(prop) > 0 {
491 prop = "\n" + prop
492 }
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500493 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400494 description: btc.description,
495 blueprint: fmt.Sprintf(baseBlueprint, prop),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500496 targets: []testBazelTarget{
497 {"cc_binary", "foo", btc.bazelAttr},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400498 },
499 })
500 }
501}
Liz Kammer12615db2021-09-28 09:19:17 -0400502
503func TestCcBinarySharedProto(t *testing.T) {
504 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
505 blueprint: soongCcProtoLibraries + `{rule_name} {
506 name: "foo",
507 srcs: ["foo.proto"],
508 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -0400509 },
510 include_build_directory: false,
511}`,
512 targets: []testBazelTarget{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000513 {"proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400514 "srcs": `["foo.proto"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000515 }}, {"cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400516 "deps": `[":foo_proto"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000517 }}, {"cc_binary", "foo", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400518 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
519 "whole_archive_deps": `[":foo_cc_proto_lite"]`,
520 }},
521 },
522 })
523}
524
525func TestCcBinaryStaticProto(t *testing.T) {
526 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
527 blueprint: soongCcProtoLibraries + `{rule_name} {
528 name: "foo",
529 srcs: ["foo.proto"],
530 static_executable: true,
531 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -0400532 },
533 include_build_directory: false,
534}`,
535 targets: []testBazelTarget{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000536 {"proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400537 "srcs": `["foo.proto"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000538 }}, {"cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400539 "deps": `[":foo_proto"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000540 }}, {"cc_binary", "foo", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400541 "deps": `[":libprotobuf-cpp-lite"]`,
542 "whole_archive_deps": `[":foo_cc_proto_lite"]`,
543 "linkshared": `False`,
544 }},
545 },
546 })
547}
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000548
549func TestCcBinaryConvertLex(t *testing.T) {
550 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
551 description: `.l and .ll sources converted to .c and .cc`,
552 blueprint: `
553{rule_name} {
554 name: "foo",
555 srcs: ["foo.c", "bar.cc", "foo1.l", "foo2.l", "bar1.ll", "bar2.ll"],
556 lex: { flags: ["--foo_opt", "--bar_opt"] },
557 include_build_directory: false,
558}
559`,
560 targets: []testBazelTarget{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000561 {"genlex", "foo_genlex_l", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000562 "srcs": `[
563 "foo1.l",
564 "foo2.l",
565 ]`,
566 "lexopts": `[
567 "--foo_opt",
568 "--bar_opt",
569 ]`,
570 }},
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000571 {"genlex", "foo_genlex_ll", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000572 "srcs": `[
573 "bar1.ll",
574 "bar2.ll",
575 ]`,
576 "lexopts": `[
577 "--foo_opt",
578 "--bar_opt",
579 ]`,
580 }},
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000581 {"cc_binary", "foo", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000582 "srcs": `[
583 "bar.cc",
584 ":foo_genlex_ll",
585 ]`,
586 "srcs_c": `[
587 "foo.c",
588 ":foo_genlex_l",
589 ]`,
590 }},
591 },
592 })
593}
Cole Faust6b29f592022-08-09 09:50:56 -0700594
595func TestCcBinaryRuntimeLibs(t *testing.T) {
596 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
597 description: "cc_binary with runtime libs",
598 blueprint: `
599cc_library {
600 name: "bar",
601 srcs: ["b.cc"],
602}
603
604{rule_name} {
605 name: "foo",
606 srcs: ["a.cc"],
607 runtime_libs: ["bar"],
608}
609`,
610 targets: []testBazelTarget{
611 {"cc_library_static", "bar_bp2build_cc_library_static", AttrNameToString{
612 "local_includes": `["."]`,
613 "srcs": `["b.cc"]`,
614 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
615 },
616 },
617 {"cc_library_shared", "bar", AttrNameToString{
618 "local_includes": `["."]`,
619 "srcs": `["b.cc"]`,
620 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
621 },
622 },
623 {"cc_binary", "foo", AttrNameToString{
624 "local_includes": `["."]`,
625 "srcs": `["a.cc"]`,
626 "runtime_deps": `[":bar"]`,
627 },
628 },
629 },
630 })
631}
Cole Faust5fa4e962022-08-22 14:31:04 -0700632
633func TestCcBinaryWithInstructionSet(t *testing.T) {
634 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
635 description: "instruction set",
636 blueprint: `
637{rule_name} {
638 name: "foo",
639 arch: {
640 arm: {
641 instruction_set: "arm",
642 }
643 }
644}
645`,
646 targets: []testBazelTarget{
647 {"cc_binary", "foo", AttrNameToString{
648 "features": `select({
Trevor Radcliffe5f0c2ac2023-05-15 18:00:59 +0000649 "//build/bazel/platforms/arch:arm": ["arm_isa_arm"],
Cole Faust5fa4e962022-08-22 14:31:04 -0700650 "//conditions:default": [],
651 })`,
652 "local_includes": `["."]`,
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -0500653 }},
654 },
655 })
656}
657
658func TestCcBinaryEmptySuffix(t *testing.T) {
659 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
660 description: "binary with empty suffix",
661 blueprint: `
662{rule_name} {
663 name: "foo",
664 suffix: "",
665}`,
666 targets: []testBazelTarget{
667 {"cc_binary", "foo", AttrNameToString{
668 "local_includes": `["."]`,
669 "suffix": `""`,
670 }},
671 },
672 })
673}
674
675func TestCcBinarySuffix(t *testing.T) {
676 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
677 description: "binary with suffix",
678 blueprint: `
679{rule_name} {
680 name: "foo",
681 suffix: "-suf",
682}
683`,
684 targets: []testBazelTarget{
685 {"cc_binary", "foo", AttrNameToString{
686 "local_includes": `["."]`,
687 "suffix": `"-suf"`,
688 }},
689 },
690 })
691}
692
693func TestCcArchVariantBinarySuffix(t *testing.T) {
694 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
695 description: "binary with suffix",
696 blueprint: `
697{rule_name} {
698 name: "foo",
699 arch: {
700 arm64: { suffix: "-64" },
701 arm: { suffix: "-32" },
702 },
703}
704`,
705 targets: []testBazelTarget{
706 {"cc_binary", "foo", AttrNameToString{
707 "local_includes": `["."]`,
708 "suffix": `select({
709 "//build/bazel/platforms/arch:arm": "-32",
710 "//build/bazel/platforms/arch:arm64": "-64",
711 "//conditions:default": None,
712 })`,
713 }},
Cole Faust5fa4e962022-08-22 14:31:04 -0700714 },
715 })
716}
Trevor Radcliffecee4e052022-09-06 19:31:25 +0000717
718func TestCcBinaryWithSyspropSrcs(t *testing.T) {
719 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
720 description: "cc_binary with sysprop sources",
721 blueprint: `
722{rule_name} {
723 name: "foo",
724 srcs: [
725 "bar.sysprop",
726 "baz.sysprop",
727 "blah.cpp",
728 ],
729 min_sdk_version: "5",
730}`,
731 targets: []testBazelTarget{
732 {"sysprop_library", "foo_sysprop_library", AttrNameToString{
733 "srcs": `[
734 "bar.sysprop",
735 "baz.sysprop",
736 ]`,
737 }},
738 {"cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
739 "dep": `":foo_sysprop_library"`,
740 "min_sdk_version": `"5"`,
741 }},
742 {"cc_binary", "foo", AttrNameToString{
743 "srcs": `["blah.cpp"]`,
744 "local_includes": `["."]`,
745 "min_sdk_version": `"5"`,
746 "whole_archive_deps": `[":foo_cc_sysprop_library_static"]`,
747 }},
748 },
749 })
750}
751
752func TestCcBinaryWithSyspropSrcsSomeConfigs(t *testing.T) {
753 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
754 description: "cc_binary with sysprop sources in some configs but not others",
755 blueprint: `
756{rule_name} {
757 name: "foo",
758 srcs: [
759 "blah.cpp",
760 ],
761 target: {
762 android: {
763 srcs: ["bar.sysprop"],
764 },
765 },
766 min_sdk_version: "5",
767}`,
768 targets: []testBazelTarget{
769 {"sysprop_library", "foo_sysprop_library", AttrNameToString{
770 "srcs": `select({
771 "//build/bazel/platforms/os:android": ["bar.sysprop"],
772 "//conditions:default": [],
773 })`,
774 }},
775 {"cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
776 "dep": `":foo_sysprop_library"`,
777 "min_sdk_version": `"5"`,
778 }},
779 {"cc_binary", "foo", AttrNameToString{
780 "srcs": `["blah.cpp"]`,
781 "local_includes": `["."]`,
782 "min_sdk_version": `"5"`,
783 "whole_archive_deps": `select({
784 "//build/bazel/platforms/os:android": [":foo_cc_sysprop_library_static"],
785 "//conditions:default": [],
786 })`,
787 }},
788 },
789 })
790}
Trevor Radcliffedb7e0262022-10-28 16:48:18 +0000791
792func TestCcBinaryWithIntegerOverflowProperty(t *testing.T) {
793 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
794 description: "cc_binary with integer overflow property specified",
795 blueprint: `
796{rule_name} {
797 name: "foo",
798 sanitize: {
799 integer_overflow: true,
800 },
801}`,
802 targets: []testBazelTarget{
803 {"cc_binary", "foo", AttrNameToString{
804 "local_includes": `["."]`,
805 "features": `["ubsan_integer_overflow"]`,
806 }},
807 },
808 })
809}
810
811func TestCcBinaryWithMiscUndefinedProperty(t *testing.T) {
812 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
813 description: "cc_binary with miscellaneous properties specified",
814 blueprint: `
815{rule_name} {
816 name: "foo",
817 sanitize: {
818 misc_undefined: ["undefined", "nullability"],
819 },
820}`,
821 targets: []testBazelTarget{
822 {"cc_binary", "foo", AttrNameToString{
823 "local_includes": `["."]`,
824 "features": `[
825 "ubsan_undefined",
826 "ubsan_nullability",
827 ]`,
828 }},
829 },
830 })
831}
832
833func TestCcBinaryWithUBSanPropertiesArchSpecific(t *testing.T) {
834 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
835 description: "cc_binary has correct feature select when UBSan props are specified in arch specific blocks",
836 blueprint: `
837{rule_name} {
838 name: "foo",
839 sanitize: {
840 misc_undefined: ["undefined", "nullability"],
841 },
842 target: {
843 android: {
844 sanitize: {
845 misc_undefined: ["alignment"],
846 },
847 },
848 linux_glibc: {
849 sanitize: {
850 integer_overflow: true,
851 },
852 },
853 },
854}`,
855 targets: []testBazelTarget{
856 {"cc_binary", "foo", AttrNameToString{
857 "local_includes": `["."]`,
858 "features": `[
859 "ubsan_undefined",
860 "ubsan_nullability",
861 ] + select({
862 "//build/bazel/platforms/os:android": ["ubsan_alignment"],
863 "//build/bazel/platforms/os:linux_glibc": ["ubsan_integer_overflow"],
864 "//conditions:default": [],
865 })`,
866 }},
867 },
868 })
869}
Trevor Radcliffe56b1a2b2023-02-06 21:58:30 +0000870
Trevor Radcliffeded095c2023-06-12 19:18:28 +0000871func TestCcBinaryWithSanitizerBlocklist(t *testing.T) {
872 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
873 description: "cc_binary has the correct feature when sanitize.blocklist is provided",
874 blueprint: `
875{rule_name} {
876 name: "foo",
877 sanitize: {
878 blocklist: "foo_blocklist.txt",
879 },
880}`,
881 targets: []testBazelTarget{
882 {"cc_binary", "foo", AttrNameToString{
Trevor Radcliffed9b7f172023-08-09 22:21:38 +0000883 "copts": `select({
884 "//build/bazel/rules/cc:sanitizers_enabled": ["-fsanitize-ignorelist=$(location foo_blocklist.txt)"],
885 "//conditions:default": [],
886 })`,
887 "additional_compiler_inputs": `select({
888 "//build/bazel/rules/cc:sanitizers_enabled": [":foo_blocklist.txt"],
889 "//conditions:default": [],
890 })`,
Trevor Radcliffeded095c2023-06-12 19:18:28 +0000891 "local_includes": `["."]`,
Trevor Radcliffeded095c2023-06-12 19:18:28 +0000892 }},
893 },
894 })
895}
896
Trevor Radcliffe56b1a2b2023-02-06 21:58:30 +0000897func TestCcBinaryWithThinLto(t *testing.T) {
898 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
899 description: "cc_binary has correct features when thin LTO is enabled",
900 blueprint: `
901{rule_name} {
902 name: "foo",
903 lto: {
904 thin: true,
905 },
906}`,
907 targets: []testBazelTarget{
908 {"cc_binary", "foo", AttrNameToString{
909 "local_includes": `["."]`,
910 "features": `["android_thin_lto"]`,
911 }},
912 },
913 })
914}
915
916func TestCcBinaryWithLtoNever(t *testing.T) {
917 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
918 description: "cc_binary has correct features when LTO is explicitly disabled",
919 blueprint: `
920{rule_name} {
921 name: "foo",
922 lto: {
923 never: true,
924 },
925}`,
926 targets: []testBazelTarget{
927 {"cc_binary", "foo", AttrNameToString{
928 "local_includes": `["."]`,
929 "features": `["-android_thin_lto"]`,
930 }},
931 },
932 })
933}
934
935func TestCcBinaryWithThinLtoArchSpecific(t *testing.T) {
936 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
937 description: "cc_binary has correct features when LTO differs across arch and os variants",
938 blueprint: `
939{rule_name} {
940 name: "foo",
941 target: {
942 android: {
943 lto: {
944 thin: true,
945 },
946 },
947 },
948 arch: {
949 riscv64: {
950 lto: {
951 thin: false,
952 },
953 },
954 },
955}`,
956 targets: []testBazelTarget{
957 {"cc_binary", "foo", AttrNameToString{
958 "local_includes": `["."]`,
959 "features": `select({
960 "//build/bazel/platforms/os_arch:android_arm": ["android_thin_lto"],
961 "//build/bazel/platforms/os_arch:android_arm64": ["android_thin_lto"],
962 "//build/bazel/platforms/os_arch:android_riscv64": ["-android_thin_lto"],
963 "//build/bazel/platforms/os_arch:android_x86": ["android_thin_lto"],
964 "//build/bazel/platforms/os_arch:android_x86_64": ["android_thin_lto"],
965 "//conditions:default": [],
966 })`,
967 }},
968 },
969 })
970}
971
972func TestCcBinaryWithThinLtoDisabledDefaultEnabledVariant(t *testing.T) {
973 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
974 description: "cc_binary has correct features when LTO disabled by default but enabled on a particular variant",
975 blueprint: `
976{rule_name} {
977 name: "foo",
978 lto: {
979 never: true,
980 },
981 target: {
982 android: {
983 lto: {
984 thin: true,
985 never: false,
986 },
987 },
988 },
989}`,
990 targets: []testBazelTarget{
991 {"cc_binary", "foo", AttrNameToString{
992 "local_includes": `["."]`,
993 "features": `select({
994 "//build/bazel/platforms/os:android": ["android_thin_lto"],
995 "//conditions:default": ["-android_thin_lto"],
996 })`,
997 }},
998 },
999 })
1000}
1001
1002func TestCcBinaryWithThinLtoAndWholeProgramVtables(t *testing.T) {
1003 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
1004 description: "cc_binary has correct features when thin LTO is enabled with whole_program_vtables",
1005 blueprint: `
1006{rule_name} {
1007 name: "foo",
1008 lto: {
1009 thin: true,
1010 },
1011 whole_program_vtables: true,
1012}`,
1013 targets: []testBazelTarget{
1014 {"cc_binary", "foo", AttrNameToString{
1015 "local_includes": `["."]`,
1016 "features": `[
1017 "android_thin_lto",
1018 "android_thin_lto_whole_program_vtables",
1019 ]`,
1020 }},
1021 },
1022 })
1023}
Trevor Radcliffea8b44162023-04-14 18:25:24 +00001024
1025func TestCcBinaryHiddenVisibilityConvertedToFeature(t *testing.T) {
1026 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
1027 description: "cc_binary changes hidden visibility to feature",
1028 blueprint: `
1029{rule_name} {
1030 name: "foo",
1031 cflags: ["-fvisibility=hidden"],
1032}`,
1033 targets: []testBazelTarget{
1034 {"cc_binary", "foo", AttrNameToString{
1035 "local_includes": `["."]`,
1036 "features": `["visibility_hidden"]`,
1037 }},
1038 },
1039 })
1040}
1041
1042func TestCcBinaryHiddenVisibilityConvertedToFeatureOsSpecific(t *testing.T) {
1043 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
1044 description: "cc_binary changes hidden visibility to feature for specific os",
1045 blueprint: `
1046{rule_name} {
1047 name: "foo",
1048 target: {
1049 android: {
1050 cflags: ["-fvisibility=hidden"],
1051 },
1052 },
1053}`,
1054 targets: []testBazelTarget{
1055 {"cc_binary", "foo", AttrNameToString{
1056 "local_includes": `["."]`,
1057 "features": `select({
1058 "//build/bazel/platforms/os:android": ["visibility_hidden"],
1059 "//conditions:default": [],
1060 })`,
1061 }},
1062 },
1063 })
1064}
Trevor Radcliffe27669c02023-03-28 20:47:10 +00001065
1066func TestCcBinaryWithCfi(t *testing.T) {
1067 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
1068 description: "cc_binary has correct features when cfi is enabled",
1069 blueprint: `
1070{rule_name} {
1071 name: "foo",
1072 sanitize: {
1073 cfi: true,
1074 },
1075}`,
1076 targets: []testBazelTarget{
1077 {"cc_binary", "foo", AttrNameToString{
1078 "features": `["android_cfi"]`,
1079 "local_includes": `["."]`,
1080 }},
1081 },
1082 })
1083}
1084
1085func TestCcBinaryWithCfiOsSpecific(t *testing.T) {
1086 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
1087 description: "cc_binary has correct features when cfi is enabled for specific variants",
1088 blueprint: `
1089{rule_name} {
1090 name: "foo",
1091 target: {
1092 android: {
1093 sanitize: {
1094 cfi: true,
1095 },
1096 },
1097 },
1098}`,
1099 targets: []testBazelTarget{
1100 {"cc_binary", "foo", AttrNameToString{
1101 "features": `select({
1102 "//build/bazel/platforms/os:android": ["android_cfi"],
1103 "//conditions:default": [],
1104 })`,
1105 "local_includes": `["."]`,
1106 }},
1107 },
1108 })
1109}
1110
1111func TestCcBinaryWithCfiAndCfiAssemblySupport(t *testing.T) {
1112 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
1113 description: "cc_binary has correct features when cfi is enabled with cfi assembly support",
1114 blueprint: `
1115{rule_name} {
1116 name: "foo",
1117 sanitize: {
1118 cfi: true,
1119 config: {
1120 cfi_assembly_support: true,
1121 },
1122 },
1123}`,
1124 targets: []testBazelTarget{
1125 {"cc_binary", "foo", AttrNameToString{
1126 "features": `[
1127 "android_cfi",
1128 "android_cfi_assembly_support",
1129 ]`,
1130 "local_includes": `["."]`,
1131 }},
1132 },
1133 })
1134}
Spandan Das39ccf932023-05-26 18:03:39 +00001135
Trevor Radcliffe523c5c62023-06-16 20:15:45 +00001136func TestCcBinaryExplicitlyDisablesCfiWhenFalse(t *testing.T) {
1137 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
1138 description: "cc_binary disables cfi when explciitly set to false in the bp",
1139 blueprint: `
1140{rule_name} {
1141 name: "foo",
1142 sanitize: {
1143 cfi: false,
1144 },
1145}
1146`,
1147 targets: []testBazelTarget{
1148 {"cc_binary", "foo", AttrNameToString{
1149 "features": `["-android_cfi"]`,
1150 "local_includes": `["."]`,
1151 }},
1152 },
1153 })
1154}
1155
Spandan Das39ccf932023-05-26 18:03:39 +00001156func TestCcBinaryStem(t *testing.T) {
1157 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
1158 description: "cc_binary with stem property",
1159 blueprint: `
1160cc_binary {
1161 name: "foo_with_stem_simple",
1162 stem: "foo",
1163}
1164cc_binary {
1165 name: "foo_with_arch_variant_stem",
1166 arch: {
1167 arm: {
1168 stem: "foo-arm",
1169 },
1170 arm64: {
1171 stem: "foo-arm64",
1172 },
1173 },
1174}
1175`,
1176 targets: []testBazelTarget{
1177 {"cc_binary", "foo_with_stem_simple", AttrNameToString{
1178 "stem": `"foo"`,
1179 "local_includes": `["."]`,
1180 }},
1181 {"cc_binary", "foo_with_arch_variant_stem", AttrNameToString{
1182 "stem": `select({
1183 "//build/bazel/platforms/arch:arm": "foo-arm",
1184 "//build/bazel/platforms/arch:arm64": "foo-arm64",
1185 "//conditions:default": None,
1186 })`,
1187 "local_includes": `["."]`,
1188 }},
1189 },
1190 })
1191}
Alixe2667872023-04-24 14:57:32 +00001192
1193func TestCCBinaryRscriptSrc(t *testing.T) {
1194 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
1195 description: `cc_binary with rscript files in sources`,
1196 blueprint: `
1197{rule_name} {
1198 name : "foo",
1199 srcs : [
1200 "ccSrc.cc",
1201 "rsSrc.rscript",
1202 ],
1203 include_build_directory: false,
1204}
1205`,
1206 targets: []testBazelTarget{
1207 {"rscript_to_cpp", "foo_renderscript", AttrNameToString{
1208 "srcs": `["rsSrc.rscript"]`,
1209 }},
1210 {"cc_binary", "foo", AttrNameToString{
1211 "absolute_includes": `[
1212 "frameworks/rs",
1213 "frameworks/rs/cpp",
1214 ]`,
1215 "local_includes": `["."]`,
1216 "srcs": `[
1217 "ccSrc.cc",
1218 "foo_renderscript",
1219 ]`,
1220 }},
1221 },
1222 })
1223}
Liz Kammerb4928432023-06-02 18:43:36 -04001224
1225func TestCcBinaryStatic_SystemSharedLibUsedAsDep(t *testing.T) {
1226 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
1227 description: "cc_library_static system_shared_lib empty for linux_bionic variant",
1228 blueprint: soongCcLibraryStaticPreamble +
Sam Delmerico130d75b2023-08-31 00:51:44 +00001229 SimpleModuleDoNotConvertBp2build("cc_library", "libc") + `
Liz Kammerb4928432023-06-02 18:43:36 -04001230
1231cc_library {
1232 name: "libm",
1233 bazel_module: { bp2build_available: false },
1234}
1235
1236cc_binary {
1237 name: "used_in_bionic_oses",
1238 target: {
1239 android: {
1240 static_libs: ["libc"],
1241 },
1242 linux_bionic: {
1243 static_libs: ["libc"],
1244 },
1245 },
1246 include_build_directory: false,
1247 static_executable: true,
1248}
1249
1250cc_binary {
1251 name: "all",
1252 static_libs: ["libc"],
1253 include_build_directory: false,
1254 static_executable: true,
1255}
1256
1257cc_binary {
1258 name: "keep_for_empty_system_shared_libs",
1259 static_libs: ["libc"],
1260 system_shared_libs: [],
1261 include_build_directory: false,
1262 static_executable: true,
1263}
1264
1265cc_binary {
1266 name: "used_with_stubs",
1267 static_libs: ["libm"],
1268 include_build_directory: false,
1269 static_executable: true,
1270}
1271
1272cc_binary {
1273 name: "keep_with_stubs",
1274 static_libs: ["libm"],
1275 system_shared_libs: [],
1276 include_build_directory: false,
1277 static_executable: true,
1278}
1279`,
1280 targets: []testBazelTarget{
1281 {"cc_binary", "all", AttrNameToString{
1282 "linkshared": "False",
1283 }},
1284 {"cc_binary", "keep_for_empty_system_shared_libs", AttrNameToString{
1285 "deps": `[":libc_bp2build_cc_library_static"]`,
1286 "system_deps": `[]`,
1287 "linkshared": "False",
1288 }},
1289 {"cc_binary", "keep_with_stubs", AttrNameToString{
1290 "linkshared": "False",
1291 "deps": `[":libm_bp2build_cc_library_static"]`,
1292 "system_deps": `[]`,
1293 }},
1294 {"cc_binary", "used_in_bionic_oses", AttrNameToString{
1295 "linkshared": "False",
1296 }},
1297 {"cc_binary", "used_with_stubs", AttrNameToString{
1298 "linkshared": "False",
1299 }},
1300 },
1301 })
1302}