blob: c67970375dc5324f70384f61b90d554e838370c5 [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 {
Chris Parsonscd209032023-09-19 01:12:48 +000047 description string
48 filesystem map[string]string
49 blueprint string
50 targets []testBazelTarget
51 stubbedBuildDefinitions []string
Liz Kammer78cfdaa2021-11-08 12:56:31 -050052}
53
Liz Kammer2b8004b2021-10-04 13:55:44 -040054func registerCcBinaryModuleTypes(ctx android.RegistrationContext) {
55 cc.RegisterCCBuildComponents(ctx)
56 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
57 ctx.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory)
58 ctx.RegisterModuleType("cc_library", cc.LibraryFactory)
Liz Kammere6583482021-10-19 13:56:10 -040059 ctx.RegisterModuleType("genrule", genrule.GenRuleFactory)
Liz Kammer2b8004b2021-10-04 13:55:44 -040060}
61
Liz Kammer78cfdaa2021-11-08 12:56:31 -050062var binaryReplacer = strings.NewReplacer(ccBinaryTypePlaceHolder, "cc_binary")
63var hostBinaryReplacer = strings.NewReplacer(ccBinaryTypePlaceHolder, "cc_binary_host")
Liz Kammer2b8004b2021-10-04 13:55:44 -040064
Liz Kammer78cfdaa2021-11-08 12:56:31 -050065func runCcBinaryTests(t *testing.T, tc ccBinaryBp2buildTestCase) {
Liz Kammer2b8004b2021-10-04 13:55:44 -040066 t.Helper()
67 runCcBinaryTestCase(t, tc)
68 runCcHostBinaryTestCase(t, tc)
69}
70
Liz Kammerdfeb1202022-05-13 17:20:20 -040071func runCcBinaryTestCase(t *testing.T, testCase ccBinaryBp2buildTestCase) {
Liz Kammer2b8004b2021-10-04 13:55:44 -040072 t.Helper()
Liz Kammer78cfdaa2021-11-08 12:56:31 -050073 moduleTypeUnderTest := "cc_binary"
Liz Kammerdfeb1202022-05-13 17:20:20 -040074
75 description := fmt.Sprintf("%s %s", moduleTypeUnderTest, testCase.description)
76 t.Run(description, func(t *testing.T) {
Liz Kammerbe46fcc2021-11-01 15:32:43 -040077 t.Helper()
Sam Delmerico3177a6e2022-06-21 19:28:33 +000078 RunBp2BuildTestCase(t, registerCcBinaryModuleTypes, Bp2buildTestCase{
79 ExpectedBazelTargets: generateBazelTargetsForTest(testCase.targets, android.DeviceSupported),
80 ModuleTypeUnderTest: moduleTypeUnderTest,
81 ModuleTypeUnderTestFactory: cc.BinaryFactory,
82 Description: description,
83 Blueprint: binaryReplacer.Replace(testCase.blueprint),
Liz Kammerbaced712022-09-16 09:01:29 -040084 Filesystem: testCase.filesystem,
Chris Parsonscd209032023-09-19 01:12:48 +000085 StubbedBuildDefinitions: testCase.stubbedBuildDefinitions,
Liz Kammerdfeb1202022-05-13 17:20:20 -040086 })
Liz Kammer2b8004b2021-10-04 13:55:44 -040087 })
88}
89
Liz Kammerdfeb1202022-05-13 17:20:20 -040090func runCcHostBinaryTestCase(t *testing.T, testCase ccBinaryBp2buildTestCase) {
Liz Kammer2b8004b2021-10-04 13:55:44 -040091 t.Helper()
Liz Kammer78cfdaa2021-11-08 12:56:31 -050092 moduleTypeUnderTest := "cc_binary_host"
Liz Kammerdfeb1202022-05-13 17:20:20 -040093 description := fmt.Sprintf("%s %s", moduleTypeUnderTest, testCase.description)
94 t.Run(description, func(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +000095 RunBp2BuildTestCase(t, registerCcBinaryModuleTypes, Bp2buildTestCase{
96 ExpectedBazelTargets: generateBazelTargetsForTest(testCase.targets, android.HostSupported),
97 ModuleTypeUnderTest: moduleTypeUnderTest,
98 ModuleTypeUnderTestFactory: cc.BinaryHostFactory,
99 Description: description,
100 Blueprint: hostBinaryReplacer.Replace(testCase.blueprint),
Liz Kammerbaced712022-09-16 09:01:29 -0400101 Filesystem: testCase.filesystem,
Chris Parsonscd209032023-09-19 01:12:48 +0000102 StubbedBuildDefinitions: testCase.stubbedBuildDefinitions,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500103 })
Liz Kammer2b8004b2021-10-04 13:55:44 -0400104 })
105}
106
107func TestBasicCcBinary(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500108 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400109 description: "basic -- properties -> attrs with little/no transformation",
Liz Kammerbaced712022-09-16 09:01:29 -0400110 filesystem: map[string]string{
111 soongCcVersionLibBpPath: soongCcVersionLibBp,
112 },
Chris Parsonscd209032023-09-19 01:12:48 +0000113 stubbedBuildDefinitions: []string{"//build/soong/cc/libbuildversion:libbuildversion"},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400114 blueprint: `
115{rule_name} {
116 name: "foo",
117 srcs: ["a.cc"],
118 local_include_dirs: ["dir"],
119 include_dirs: ["absolute_dir"],
120 cflags: ["-Dcopt"],
121 cppflags: ["-Dcppflag"],
122 conlyflags: ["-Dconlyflag"],
123 asflags: ["-Dasflag"],
124 ldflags: ["ld-flag"],
125 rtti: true,
126 strip: {
127 all: true,
128 keep_symbols: true,
129 keep_symbols_and_debug_frame: true,
130 keep_symbols_list: ["symbol"],
131 none: true,
132 },
Yu Liufc603162022-03-01 15:44:08 -0800133 sdk_version: "current",
134 min_sdk_version: "29",
Yu Liua79c9462022-03-22 16:35:22 -0700135 use_version_lib: true,
Liz Kammer2b8004b2021-10-04 13:55:44 -0400136}
137`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500138 targets: []testBazelTarget{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000139 {"cc_binary", "foo", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500140 "absolute_includes": `["absolute_dir"]`,
141 "asflags": `["-Dasflag"]`,
142 "conlyflags": `["-Dconlyflag"]`,
143 "copts": `["-Dcopt"]`,
144 "cppflags": `["-Dcppflag"]`,
145 "linkopts": `["ld-flag"]`,
146 "local_includes": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400147 "dir",
148 ".",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500149 ]`,
150 "rtti": `True`,
151 "srcs": `["a.cc"]`,
152 "strip": `{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400153 "all": True,
154 "keep_symbols": True,
155 "keep_symbols_and_debug_frame": True,
156 "keep_symbols_list": ["symbol"],
157 "none": True,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500158 }`,
Liz Kammerbaced712022-09-16 09:01:29 -0400159 "sdk_version": `"current"`,
160 "min_sdk_version": `"29"`,
161 "use_version_lib": `True`,
162 "whole_archive_deps": `["//build/soong/cc/libbuildversion:libbuildversion"]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500163 },
164 },
165 },
Liz Kammer2b8004b2021-10-04 13:55:44 -0400166 })
167}
168
169func TestCcBinaryWithSharedLdflagDisableFeature(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500170 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400171 description: `ldflag "-shared" disables static_flag feature`,
172 blueprint: `
173{rule_name} {
174 name: "foo",
175 ldflags: ["-shared"],
176 include_build_directory: false,
177}
178`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500179 targets: []testBazelTarget{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000180 {"cc_binary", "foo", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500181 "features": `["-static_flag"]`,
182 "linkopts": `["-shared"]`,
183 },
184 },
185 },
Liz Kammer2b8004b2021-10-04 13:55:44 -0400186 })
187}
188
189func TestCcBinaryWithLinkStatic(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500190 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400191 description: "link static",
192 blueprint: `
193{rule_name} {
194 name: "foo",
195 static_executable: true,
196 include_build_directory: false,
197}
198`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500199 targets: []testBazelTarget{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000200 {"cc_binary", "foo", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500201 "linkshared": `False`,
202 },
203 },
204 },
Liz Kammer2b8004b2021-10-04 13:55:44 -0400205 })
206}
207
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000208func TestCcBinaryVersionScriptAndDynamicList(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500209 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000210 description: `version script and dynamic list`,
Liz Kammer2b8004b2021-10-04 13:55:44 -0400211 blueprint: `
212{rule_name} {
213 name: "foo",
214 include_build_directory: false,
215 version_script: "vs",
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000216 dynamic_list: "dynamic.list",
Liz Kammer2b8004b2021-10-04 13:55:44 -0400217}
218`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500219 targets: []testBazelTarget{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000220 {"cc_binary", "foo", AttrNameToString{
Trevor Radcliffe82dd8552022-10-03 20:27:27 +0000221 "additional_linker_inputs": `[
222 "vs",
223 "dynamic.list",
224 ]`,
225 "linkopts": `[
226 "-Wl,--version-script,$(location vs)",
227 "-Wl,--dynamic-list,$(location dynamic.list)",
228 ]`,
Trevor Radcliffef06dd912023-05-19 14:51:41 +0000229 "features": `["android_cfi_exports_map"]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500230 },
231 },
232 },
Liz Kammer2b8004b2021-10-04 13:55:44 -0400233 })
234}
235
Trevor Radcliffeea6a45d2022-09-20 18:58:01 +0000236func TestCcBinaryLdflagsSplitBySpaceExceptSoongAdded(t *testing.T) {
237 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
238 description: "ldflags are split by spaces except for the ones added by soong (version script and dynamic list)",
239 blueprint: `
240{rule_name} {
241 name: "foo",
242 ldflags: [
243 "--nospace_flag",
244 "-z spaceflag",
245 ],
246 version_script: "version_script",
247 dynamic_list: "dynamic.list",
248 include_build_directory: false,
249}
250`,
251 targets: []testBazelTarget{
252 {"cc_binary", "foo", AttrNameToString{
253 "additional_linker_inputs": `[
254 "version_script",
255 "dynamic.list",
256 ]`,
Trevor Radcliffef06dd912023-05-19 14:51:41 +0000257 "features": `["android_cfi_exports_map"]`,
Trevor Radcliffeea6a45d2022-09-20 18:58:01 +0000258 "linkopts": `[
259 "--nospace_flag",
260 "-z",
261 "spaceflag",
262 "-Wl,--version-script,$(location version_script)",
263 "-Wl,--dynamic-list,$(location dynamic.list)",
264 ]`,
265 }}},
266 })
267}
268
Liz Kammer2b8004b2021-10-04 13:55:44 -0400269func TestCcBinarySplitSrcsByLang(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500270 runCcHostBinaryTestCase(t, ccBinaryBp2buildTestCase{
Chris Parsonscd209032023-09-19 01:12:48 +0000271 description: "split srcs by lang",
272 stubbedBuildDefinitions: []string{"fg_foo"},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400273 blueprint: `
274{rule_name} {
275 name: "foo",
276 srcs: [
277 "asonly.S",
278 "conly.c",
279 "cpponly.cpp",
280 ":fg_foo",
281 ],
282 include_build_directory: false,
283}
Chris Parsonscd209032023-09-19 01:12:48 +0000284` + simpleModule("filegroup", "fg_foo"),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500285 targets: []testBazelTarget{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000286 {"cc_binary", "foo", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500287 "srcs": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400288 "cpponly.cpp",
289 ":fg_foo_cpp_srcs",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500290 ]`,
291 "srcs_as": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400292 "asonly.S",
293 ":fg_foo_as_srcs",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500294 ]`,
295 "srcs_c": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400296 "conly.c",
297 ":fg_foo_c_srcs",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500298 ]`,
299 },
300 },
301 },
Liz Kammer2b8004b2021-10-04 13:55:44 -0400302 })
303}
304
305func TestCcBinaryDoNotDistinguishBetweenDepsAndImplementationDeps(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500306 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400307 description: "no implementation deps",
Chris Parsonscd209032023-09-19 01:12:48 +0000308 stubbedBuildDefinitions: []string{"generated_hdr", "export_generated_hdr", "static_dep", "implementation_static_dep",
309 "whole_static_dep", "not_explicitly_exported_whole_static_dep", "shared_dep", "implementation_shared_dep"},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400310 blueprint: `
Liz Kammere6583482021-10-19 13:56:10 -0400311genrule {
312 name: "generated_hdr",
313 cmd: "nothing to see here",
314}
315
316genrule {
317 name: "export_generated_hdr",
318 cmd: "nothing to see here",
319}
320
Liz Kammer2b8004b2021-10-04 13:55:44 -0400321{rule_name} {
322 name: "foo",
Liz Kammere6583482021-10-19 13:56:10 -0400323 srcs: ["foo.cpp"],
Liz Kammer2b8004b2021-10-04 13:55:44 -0400324 shared_libs: ["implementation_shared_dep", "shared_dep"],
325 export_shared_lib_headers: ["shared_dep"],
326 static_libs: ["implementation_static_dep", "static_dep"],
327 export_static_lib_headers: ["static_dep", "whole_static_dep"],
328 whole_static_libs: ["not_explicitly_exported_whole_static_dep", "whole_static_dep"],
329 include_build_directory: false,
Liz Kammere6583482021-10-19 13:56:10 -0400330 generated_headers: ["generated_hdr", "export_generated_hdr"],
331 export_generated_headers: ["export_generated_hdr"],
Liz Kammer2b8004b2021-10-04 13:55:44 -0400332}
333` +
Chris Parsonscd209032023-09-19 01:12:48 +0000334 simpleModule("cc_library_static", "static_dep") +
335 simpleModule("cc_library_static", "implementation_static_dep") +
336 simpleModule("cc_library_static", "whole_static_dep") +
337 simpleModule("cc_library_static", "not_explicitly_exported_whole_static_dep") +
338 simpleModule("cc_library", "shared_dep") +
339 simpleModule("cc_library", "implementation_shared_dep"),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500340 targets: []testBazelTarget{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000341 {"cc_binary", "foo", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500342 "deps": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400343 ":implementation_static_dep",
344 ":static_dep",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500345 ]`,
346 "dynamic_deps": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400347 ":implementation_shared_dep",
348 ":shared_dep",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500349 ]`,
350 "srcs": `[
Liz Kammere6583482021-10-19 13:56:10 -0400351 "foo.cpp",
352 ":generated_hdr",
353 ":export_generated_hdr",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500354 ]`,
355 "whole_archive_deps": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400356 ":not_explicitly_exported_whole_static_dep",
357 ":whole_static_dep",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500358 ]`,
Liz Kammer1263d9b2021-12-10 14:28:20 -0500359 "local_includes": `["."]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500360 },
361 },
362 },
Liz Kammer2b8004b2021-10-04 13:55:44 -0400363 })
364}
365
366func TestCcBinaryNocrtTests(t *testing.T) {
367 baseTestCases := []struct {
368 description string
369 soongProperty string
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000370 bazelAttr AttrNameToString
Liz Kammer2b8004b2021-10-04 13:55:44 -0400371 }{
372 {
373 description: "nocrt: true",
374 soongProperty: `nocrt: true,`,
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +0000375 bazelAttr: AttrNameToString{"features": `["-link_crt"]`},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400376 },
377 {
378 description: "nocrt: false",
379 soongProperty: `nocrt: false,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000380 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400381 },
382 {
383 description: "nocrt: not set",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000384 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400385 },
386 }
387
388 baseBlueprint := `{rule_name} {
389 name: "foo",%s
390 include_build_directory: false,
391}
392`
393
Liz Kammer2b8004b2021-10-04 13:55:44 -0400394 for _, btc := range baseTestCases {
395 prop := btc.soongProperty
396 if len(prop) > 0 {
397 prop = "\n" + prop
398 }
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500399 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400400 description: btc.description,
401 blueprint: fmt.Sprintf(baseBlueprint, prop),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500402 targets: []testBazelTarget{
403 {"cc_binary", "foo", btc.bazelAttr},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400404 },
405 })
406 }
407}
408
409func TestCcBinaryNo_libcrtTests(t *testing.T) {
410 baseTestCases := []struct {
411 description string
412 soongProperty string
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000413 bazelAttr AttrNameToString
Liz Kammer2b8004b2021-10-04 13:55:44 -0400414 }{
415 {
416 description: "no_libcrt: true",
417 soongProperty: `no_libcrt: true,`,
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +0000418 bazelAttr: AttrNameToString{"features": `["-use_libcrt"]`},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400419 },
420 {
421 description: "no_libcrt: false",
422 soongProperty: `no_libcrt: false,`,
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +0000423 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400424 },
425 {
426 description: "no_libcrt: not set",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000427 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400428 },
429 }
430
431 baseBlueprint := `{rule_name} {
432 name: "foo",%s
433 include_build_directory: false,
434}
435`
436
Liz Kammer2b8004b2021-10-04 13:55:44 -0400437 for _, btc := range baseTestCases {
438 prop := btc.soongProperty
439 if len(prop) > 0 {
440 prop = "\n" + prop
441 }
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500442 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400443 description: btc.description,
444 blueprint: fmt.Sprintf(baseBlueprint, prop),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500445 targets: []testBazelTarget{
446 {"cc_binary", "foo", btc.bazelAttr},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400447 },
448 })
449 }
450}
451
452func TestCcBinaryPropertiesToFeatures(t *testing.T) {
453 baseTestCases := []struct {
454 description string
455 soongProperty string
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000456 bazelAttr AttrNameToString
Liz Kammer2b8004b2021-10-04 13:55:44 -0400457 }{
458 {
459 description: "pack_relocation: true",
460 soongProperty: `pack_relocations: true,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000461 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400462 },
463 {
464 description: "pack_relocations: false",
465 soongProperty: `pack_relocations: false,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000466 bazelAttr: AttrNameToString{"features": `["disable_pack_relocations"]`},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400467 },
468 {
469 description: "pack_relocations: not set",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000470 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400471 },
472 {
473 description: "pack_relocation: true",
474 soongProperty: `allow_undefined_symbols: true,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000475 bazelAttr: AttrNameToString{"features": `["-no_undefined_symbols"]`},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400476 },
477 {
478 description: "allow_undefined_symbols: false",
479 soongProperty: `allow_undefined_symbols: false,`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000480 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400481 },
482 {
483 description: "allow_undefined_symbols: not set",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000484 bazelAttr: AttrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400485 },
486 }
487
488 baseBlueprint := `{rule_name} {
489 name: "foo",%s
490 include_build_directory: false,
491}
492`
Liz Kammer2b8004b2021-10-04 13:55:44 -0400493 for _, btc := range baseTestCases {
494 prop := btc.soongProperty
495 if len(prop) > 0 {
496 prop = "\n" + prop
497 }
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500498 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400499 description: btc.description,
500 blueprint: fmt.Sprintf(baseBlueprint, prop),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500501 targets: []testBazelTarget{
502 {"cc_binary", "foo", btc.bazelAttr},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400503 },
504 })
505 }
506}
Liz Kammer12615db2021-09-28 09:19:17 -0400507
508func TestCcBinarySharedProto(t *testing.T) {
509 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Chris Parsonscd209032023-09-19 01:12:48 +0000510 stubbedBuildDefinitions: []string{"libprotobuf-cpp-full", "libprotobuf-cpp-lite"},
Liz Kammer12615db2021-09-28 09:19:17 -0400511 blueprint: soongCcProtoLibraries + `{rule_name} {
512 name: "foo",
513 srcs: ["foo.proto"],
514 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -0400515 },
516 include_build_directory: false,
517}`,
518 targets: []testBazelTarget{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000519 {"proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400520 "srcs": `["foo.proto"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000521 }}, {"cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400522 "deps": `[":foo_proto"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000523 }}, {"cc_binary", "foo", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400524 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
525 "whole_archive_deps": `[":foo_cc_proto_lite"]`,
526 }},
527 },
528 })
529}
530
531func TestCcBinaryStaticProto(t *testing.T) {
532 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Chris Parsonscd209032023-09-19 01:12:48 +0000533 stubbedBuildDefinitions: []string{"libprotobuf-cpp-full", "libprotobuf-cpp-lite"},
Liz Kammer12615db2021-09-28 09:19:17 -0400534 blueprint: soongCcProtoLibraries + `{rule_name} {
535 name: "foo",
536 srcs: ["foo.proto"],
537 static_executable: true,
538 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -0400539 },
540 include_build_directory: false,
541}`,
542 targets: []testBazelTarget{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000543 {"proto_library", "foo_proto", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400544 "srcs": `["foo.proto"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000545 }}, {"cc_lite_proto_library", "foo_cc_proto_lite", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400546 "deps": `[":foo_proto"]`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000547 }}, {"cc_binary", "foo", AttrNameToString{
Liz Kammer12615db2021-09-28 09:19:17 -0400548 "deps": `[":libprotobuf-cpp-lite"]`,
549 "whole_archive_deps": `[":foo_cc_proto_lite"]`,
550 "linkshared": `False`,
551 }},
552 },
553 })
554}
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000555
556func TestCcBinaryConvertLex(t *testing.T) {
557 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
558 description: `.l and .ll sources converted to .c and .cc`,
559 blueprint: `
560{rule_name} {
561 name: "foo",
562 srcs: ["foo.c", "bar.cc", "foo1.l", "foo2.l", "bar1.ll", "bar2.ll"],
563 lex: { flags: ["--foo_opt", "--bar_opt"] },
564 include_build_directory: false,
565}
566`,
567 targets: []testBazelTarget{
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000568 {"genlex", "foo_genlex_l", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000569 "srcs": `[
570 "foo1.l",
571 "foo2.l",
572 ]`,
573 "lexopts": `[
574 "--foo_opt",
575 "--bar_opt",
576 ]`,
577 }},
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000578 {"genlex", "foo_genlex_ll", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000579 "srcs": `[
580 "bar1.ll",
581 "bar2.ll",
582 ]`,
583 "lexopts": `[
584 "--foo_opt",
585 "--bar_opt",
586 ]`,
587 }},
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000588 {"cc_binary", "foo", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000589 "srcs": `[
590 "bar.cc",
591 ":foo_genlex_ll",
592 ]`,
593 "srcs_c": `[
594 "foo.c",
595 ":foo_genlex_l",
596 ]`,
597 }},
598 },
599 })
600}
Cole Faust6b29f592022-08-09 09:50:56 -0700601
602func TestCcBinaryRuntimeLibs(t *testing.T) {
603 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
604 description: "cc_binary with runtime libs",
605 blueprint: `
606cc_library {
607 name: "bar",
608 srcs: ["b.cc"],
609}
610
611{rule_name} {
612 name: "foo",
613 srcs: ["a.cc"],
614 runtime_libs: ["bar"],
615}
616`,
617 targets: []testBazelTarget{
618 {"cc_library_static", "bar_bp2build_cc_library_static", AttrNameToString{
619 "local_includes": `["."]`,
620 "srcs": `["b.cc"]`,
621 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
622 },
623 },
624 {"cc_library_shared", "bar", AttrNameToString{
625 "local_includes": `["."]`,
626 "srcs": `["b.cc"]`,
627 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
628 },
629 },
630 {"cc_binary", "foo", AttrNameToString{
631 "local_includes": `["."]`,
632 "srcs": `["a.cc"]`,
633 "runtime_deps": `[":bar"]`,
634 },
635 },
636 },
637 })
638}
Cole Faust5fa4e962022-08-22 14:31:04 -0700639
640func TestCcBinaryWithInstructionSet(t *testing.T) {
641 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
642 description: "instruction set",
643 blueprint: `
644{rule_name} {
645 name: "foo",
646 arch: {
647 arm: {
648 instruction_set: "arm",
649 }
650 }
651}
652`,
653 targets: []testBazelTarget{
654 {"cc_binary", "foo", AttrNameToString{
655 "features": `select({
Trevor Radcliffe5f0c2ac2023-05-15 18:00:59 +0000656 "//build/bazel/platforms/arch:arm": ["arm_isa_arm"],
Cole Faust5fa4e962022-08-22 14:31:04 -0700657 "//conditions:default": [],
658 })`,
659 "local_includes": `["."]`,
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -0500660 }},
661 },
662 })
663}
664
665func TestCcBinaryEmptySuffix(t *testing.T) {
666 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
667 description: "binary with empty suffix",
668 blueprint: `
669{rule_name} {
670 name: "foo",
671 suffix: "",
672}`,
673 targets: []testBazelTarget{
674 {"cc_binary", "foo", AttrNameToString{
675 "local_includes": `["."]`,
676 "suffix": `""`,
677 }},
678 },
679 })
680}
681
682func TestCcBinarySuffix(t *testing.T) {
683 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
684 description: "binary with suffix",
685 blueprint: `
686{rule_name} {
687 name: "foo",
688 suffix: "-suf",
689}
690`,
691 targets: []testBazelTarget{
692 {"cc_binary", "foo", AttrNameToString{
693 "local_includes": `["."]`,
694 "suffix": `"-suf"`,
695 }},
696 },
697 })
698}
699
700func TestCcArchVariantBinarySuffix(t *testing.T) {
701 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
702 description: "binary with suffix",
703 blueprint: `
704{rule_name} {
705 name: "foo",
706 arch: {
707 arm64: { suffix: "-64" },
708 arm: { suffix: "-32" },
709 },
710}
711`,
712 targets: []testBazelTarget{
713 {"cc_binary", "foo", AttrNameToString{
714 "local_includes": `["."]`,
715 "suffix": `select({
716 "//build/bazel/platforms/arch:arm": "-32",
717 "//build/bazel/platforms/arch:arm64": "-64",
718 "//conditions:default": None,
719 })`,
720 }},
Cole Faust5fa4e962022-08-22 14:31:04 -0700721 },
722 })
723}
Trevor Radcliffecee4e052022-09-06 19:31:25 +0000724
725func TestCcBinaryWithSyspropSrcs(t *testing.T) {
726 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
727 description: "cc_binary with sysprop sources",
728 blueprint: `
729{rule_name} {
730 name: "foo",
731 srcs: [
732 "bar.sysprop",
733 "baz.sysprop",
734 "blah.cpp",
735 ],
736 min_sdk_version: "5",
737}`,
738 targets: []testBazelTarget{
739 {"sysprop_library", "foo_sysprop_library", AttrNameToString{
740 "srcs": `[
741 "bar.sysprop",
742 "baz.sysprop",
743 ]`,
744 }},
745 {"cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
746 "dep": `":foo_sysprop_library"`,
747 "min_sdk_version": `"5"`,
748 }},
749 {"cc_binary", "foo", AttrNameToString{
750 "srcs": `["blah.cpp"]`,
751 "local_includes": `["."]`,
752 "min_sdk_version": `"5"`,
753 "whole_archive_deps": `[":foo_cc_sysprop_library_static"]`,
754 }},
755 },
756 })
757}
758
759func TestCcBinaryWithSyspropSrcsSomeConfigs(t *testing.T) {
760 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
761 description: "cc_binary with sysprop sources in some configs but not others",
762 blueprint: `
763{rule_name} {
764 name: "foo",
765 srcs: [
766 "blah.cpp",
767 ],
768 target: {
769 android: {
770 srcs: ["bar.sysprop"],
771 },
772 },
773 min_sdk_version: "5",
774}`,
775 targets: []testBazelTarget{
776 {"sysprop_library", "foo_sysprop_library", AttrNameToString{
777 "srcs": `select({
778 "//build/bazel/platforms/os:android": ["bar.sysprop"],
779 "//conditions:default": [],
780 })`,
781 }},
782 {"cc_sysprop_library_static", "foo_cc_sysprop_library_static", AttrNameToString{
783 "dep": `":foo_sysprop_library"`,
784 "min_sdk_version": `"5"`,
785 }},
786 {"cc_binary", "foo", AttrNameToString{
787 "srcs": `["blah.cpp"]`,
788 "local_includes": `["."]`,
789 "min_sdk_version": `"5"`,
790 "whole_archive_deps": `select({
791 "//build/bazel/platforms/os:android": [":foo_cc_sysprop_library_static"],
792 "//conditions:default": [],
793 })`,
794 }},
795 },
796 })
797}
Trevor Radcliffedb7e0262022-10-28 16:48:18 +0000798
799func TestCcBinaryWithIntegerOverflowProperty(t *testing.T) {
800 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
801 description: "cc_binary with integer overflow property specified",
802 blueprint: `
803{rule_name} {
804 name: "foo",
805 sanitize: {
806 integer_overflow: true,
807 },
808}`,
809 targets: []testBazelTarget{
810 {"cc_binary", "foo", AttrNameToString{
811 "local_includes": `["."]`,
812 "features": `["ubsan_integer_overflow"]`,
813 }},
814 },
815 })
816}
817
818func TestCcBinaryWithMiscUndefinedProperty(t *testing.T) {
819 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
820 description: "cc_binary with miscellaneous properties specified",
821 blueprint: `
822{rule_name} {
823 name: "foo",
824 sanitize: {
825 misc_undefined: ["undefined", "nullability"],
826 },
827}`,
828 targets: []testBazelTarget{
829 {"cc_binary", "foo", AttrNameToString{
830 "local_includes": `["."]`,
831 "features": `[
832 "ubsan_undefined",
833 "ubsan_nullability",
834 ]`,
835 }},
836 },
837 })
838}
839
840func TestCcBinaryWithUBSanPropertiesArchSpecific(t *testing.T) {
841 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
842 description: "cc_binary has correct feature select when UBSan props are specified in arch specific blocks",
843 blueprint: `
844{rule_name} {
845 name: "foo",
846 sanitize: {
847 misc_undefined: ["undefined", "nullability"],
848 },
849 target: {
850 android: {
851 sanitize: {
852 misc_undefined: ["alignment"],
853 },
854 },
855 linux_glibc: {
856 sanitize: {
857 integer_overflow: true,
858 },
859 },
860 },
861}`,
862 targets: []testBazelTarget{
863 {"cc_binary", "foo", AttrNameToString{
864 "local_includes": `["."]`,
865 "features": `[
866 "ubsan_undefined",
867 "ubsan_nullability",
868 ] + select({
869 "//build/bazel/platforms/os:android": ["ubsan_alignment"],
870 "//build/bazel/platforms/os:linux_glibc": ["ubsan_integer_overflow"],
871 "//conditions:default": [],
872 })`,
873 }},
874 },
875 })
876}
Trevor Radcliffe56b1a2b2023-02-06 21:58:30 +0000877
Trevor Radcliffeded095c2023-06-12 19:18:28 +0000878func TestCcBinaryWithSanitizerBlocklist(t *testing.T) {
879 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
880 description: "cc_binary has the correct feature when sanitize.blocklist is provided",
881 blueprint: `
882{rule_name} {
883 name: "foo",
884 sanitize: {
885 blocklist: "foo_blocklist.txt",
886 },
887}`,
888 targets: []testBazelTarget{
889 {"cc_binary", "foo", AttrNameToString{
Trevor Radcliffed9b7f172023-08-09 22:21:38 +0000890 "copts": `select({
891 "//build/bazel/rules/cc:sanitizers_enabled": ["-fsanitize-ignorelist=$(location foo_blocklist.txt)"],
892 "//conditions:default": [],
893 })`,
894 "additional_compiler_inputs": `select({
895 "//build/bazel/rules/cc:sanitizers_enabled": [":foo_blocklist.txt"],
896 "//conditions:default": [],
897 })`,
Trevor Radcliffeded095c2023-06-12 19:18:28 +0000898 "local_includes": `["."]`,
Trevor Radcliffeded095c2023-06-12 19:18:28 +0000899 }},
900 },
901 })
902}
903
Trevor Radcliffe56b1a2b2023-02-06 21:58:30 +0000904func TestCcBinaryWithThinLto(t *testing.T) {
905 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
906 description: "cc_binary has correct features when thin LTO is enabled",
907 blueprint: `
908{rule_name} {
909 name: "foo",
910 lto: {
911 thin: true,
912 },
913}`,
914 targets: []testBazelTarget{
915 {"cc_binary", "foo", AttrNameToString{
916 "local_includes": `["."]`,
917 "features": `["android_thin_lto"]`,
918 }},
919 },
920 })
921}
922
923func TestCcBinaryWithLtoNever(t *testing.T) {
924 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
925 description: "cc_binary has correct features when LTO is explicitly disabled",
926 blueprint: `
927{rule_name} {
928 name: "foo",
929 lto: {
930 never: true,
931 },
932}`,
933 targets: []testBazelTarget{
934 {"cc_binary", "foo", AttrNameToString{
935 "local_includes": `["."]`,
936 "features": `["-android_thin_lto"]`,
937 }},
938 },
939 })
940}
941
942func TestCcBinaryWithThinLtoArchSpecific(t *testing.T) {
943 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
944 description: "cc_binary has correct features when LTO differs across arch and os variants",
945 blueprint: `
946{rule_name} {
947 name: "foo",
948 target: {
949 android: {
950 lto: {
951 thin: true,
952 },
953 },
954 },
955 arch: {
956 riscv64: {
957 lto: {
958 thin: false,
959 },
960 },
961 },
962}`,
963 targets: []testBazelTarget{
964 {"cc_binary", "foo", AttrNameToString{
965 "local_includes": `["."]`,
966 "features": `select({
967 "//build/bazel/platforms/os_arch:android_arm": ["android_thin_lto"],
968 "//build/bazel/platforms/os_arch:android_arm64": ["android_thin_lto"],
969 "//build/bazel/platforms/os_arch:android_riscv64": ["-android_thin_lto"],
970 "//build/bazel/platforms/os_arch:android_x86": ["android_thin_lto"],
971 "//build/bazel/platforms/os_arch:android_x86_64": ["android_thin_lto"],
972 "//conditions:default": [],
973 })`,
974 }},
975 },
976 })
977}
978
979func TestCcBinaryWithThinLtoDisabledDefaultEnabledVariant(t *testing.T) {
980 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
981 description: "cc_binary has correct features when LTO disabled by default but enabled on a particular variant",
982 blueprint: `
983{rule_name} {
984 name: "foo",
985 lto: {
986 never: true,
987 },
988 target: {
989 android: {
990 lto: {
991 thin: true,
992 never: false,
993 },
994 },
995 },
996}`,
997 targets: []testBazelTarget{
998 {"cc_binary", "foo", AttrNameToString{
999 "local_includes": `["."]`,
1000 "features": `select({
1001 "//build/bazel/platforms/os:android": ["android_thin_lto"],
1002 "//conditions:default": ["-android_thin_lto"],
1003 })`,
1004 }},
1005 },
1006 })
1007}
1008
1009func TestCcBinaryWithThinLtoAndWholeProgramVtables(t *testing.T) {
1010 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
1011 description: "cc_binary has correct features when thin LTO is enabled with whole_program_vtables",
1012 blueprint: `
1013{rule_name} {
1014 name: "foo",
1015 lto: {
1016 thin: true,
1017 },
1018 whole_program_vtables: true,
1019}`,
1020 targets: []testBazelTarget{
1021 {"cc_binary", "foo", AttrNameToString{
1022 "local_includes": `["."]`,
1023 "features": `[
1024 "android_thin_lto",
1025 "android_thin_lto_whole_program_vtables",
1026 ]`,
1027 }},
1028 },
1029 })
1030}
Trevor Radcliffea8b44162023-04-14 18:25:24 +00001031
1032func TestCcBinaryHiddenVisibilityConvertedToFeature(t *testing.T) {
1033 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
1034 description: "cc_binary changes hidden visibility to feature",
1035 blueprint: `
1036{rule_name} {
1037 name: "foo",
1038 cflags: ["-fvisibility=hidden"],
1039}`,
1040 targets: []testBazelTarget{
1041 {"cc_binary", "foo", AttrNameToString{
1042 "local_includes": `["."]`,
1043 "features": `["visibility_hidden"]`,
1044 }},
1045 },
1046 })
1047}
1048
1049func TestCcBinaryHiddenVisibilityConvertedToFeatureOsSpecific(t *testing.T) {
1050 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
1051 description: "cc_binary changes hidden visibility to feature for specific os",
1052 blueprint: `
1053{rule_name} {
1054 name: "foo",
1055 target: {
1056 android: {
1057 cflags: ["-fvisibility=hidden"],
1058 },
1059 },
1060}`,
1061 targets: []testBazelTarget{
1062 {"cc_binary", "foo", AttrNameToString{
1063 "local_includes": `["."]`,
1064 "features": `select({
1065 "//build/bazel/platforms/os:android": ["visibility_hidden"],
1066 "//conditions:default": [],
1067 })`,
1068 }},
1069 },
1070 })
1071}
Trevor Radcliffe27669c02023-03-28 20:47:10 +00001072
1073func TestCcBinaryWithCfi(t *testing.T) {
1074 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
1075 description: "cc_binary has correct features when cfi is enabled",
1076 blueprint: `
1077{rule_name} {
1078 name: "foo",
1079 sanitize: {
1080 cfi: true,
1081 },
1082}`,
1083 targets: []testBazelTarget{
1084 {"cc_binary", "foo", AttrNameToString{
1085 "features": `["android_cfi"]`,
1086 "local_includes": `["."]`,
1087 }},
1088 },
1089 })
1090}
1091
1092func TestCcBinaryWithCfiOsSpecific(t *testing.T) {
1093 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
1094 description: "cc_binary has correct features when cfi is enabled for specific variants",
1095 blueprint: `
1096{rule_name} {
1097 name: "foo",
1098 target: {
1099 android: {
1100 sanitize: {
1101 cfi: true,
1102 },
1103 },
1104 },
1105}`,
1106 targets: []testBazelTarget{
1107 {"cc_binary", "foo", AttrNameToString{
1108 "features": `select({
1109 "//build/bazel/platforms/os:android": ["android_cfi"],
1110 "//conditions:default": [],
1111 })`,
1112 "local_includes": `["."]`,
1113 }},
1114 },
1115 })
1116}
1117
1118func TestCcBinaryWithCfiAndCfiAssemblySupport(t *testing.T) {
1119 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
1120 description: "cc_binary has correct features when cfi is enabled with cfi assembly support",
1121 blueprint: `
1122{rule_name} {
1123 name: "foo",
1124 sanitize: {
1125 cfi: true,
1126 config: {
1127 cfi_assembly_support: true,
1128 },
1129 },
1130}`,
1131 targets: []testBazelTarget{
1132 {"cc_binary", "foo", AttrNameToString{
1133 "features": `[
1134 "android_cfi",
1135 "android_cfi_assembly_support",
1136 ]`,
1137 "local_includes": `["."]`,
1138 }},
1139 },
1140 })
1141}
Spandan Das39ccf932023-05-26 18:03:39 +00001142
Trevor Radcliffe523c5c62023-06-16 20:15:45 +00001143func TestCcBinaryExplicitlyDisablesCfiWhenFalse(t *testing.T) {
1144 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
1145 description: "cc_binary disables cfi when explciitly set to false in the bp",
1146 blueprint: `
1147{rule_name} {
1148 name: "foo",
1149 sanitize: {
1150 cfi: false,
1151 },
1152}
1153`,
1154 targets: []testBazelTarget{
1155 {"cc_binary", "foo", AttrNameToString{
1156 "features": `["-android_cfi"]`,
1157 "local_includes": `["."]`,
1158 }},
1159 },
1160 })
1161}
1162
Spandan Das39ccf932023-05-26 18:03:39 +00001163func TestCcBinaryStem(t *testing.T) {
1164 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
1165 description: "cc_binary with stem property",
1166 blueprint: `
1167cc_binary {
1168 name: "foo_with_stem_simple",
1169 stem: "foo",
1170}
1171cc_binary {
1172 name: "foo_with_arch_variant_stem",
1173 arch: {
1174 arm: {
1175 stem: "foo-arm",
1176 },
1177 arm64: {
1178 stem: "foo-arm64",
1179 },
1180 },
1181}
1182`,
1183 targets: []testBazelTarget{
1184 {"cc_binary", "foo_with_stem_simple", AttrNameToString{
1185 "stem": `"foo"`,
1186 "local_includes": `["."]`,
1187 }},
1188 {"cc_binary", "foo_with_arch_variant_stem", AttrNameToString{
1189 "stem": `select({
1190 "//build/bazel/platforms/arch:arm": "foo-arm",
1191 "//build/bazel/platforms/arch:arm64": "foo-arm64",
1192 "//conditions:default": None,
1193 })`,
1194 "local_includes": `["."]`,
1195 }},
1196 },
1197 })
1198}
Alixe2667872023-04-24 14:57:32 +00001199
1200func TestCCBinaryRscriptSrc(t *testing.T) {
1201 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
1202 description: `cc_binary with rscript files in sources`,
1203 blueprint: `
1204{rule_name} {
1205 name : "foo",
1206 srcs : [
1207 "ccSrc.cc",
1208 "rsSrc.rscript",
1209 ],
1210 include_build_directory: false,
1211}
1212`,
1213 targets: []testBazelTarget{
1214 {"rscript_to_cpp", "foo_renderscript", AttrNameToString{
1215 "srcs": `["rsSrc.rscript"]`,
1216 }},
1217 {"cc_binary", "foo", AttrNameToString{
1218 "absolute_includes": `[
1219 "frameworks/rs",
1220 "frameworks/rs/cpp",
1221 ]`,
1222 "local_includes": `["."]`,
1223 "srcs": `[
1224 "ccSrc.cc",
1225 "foo_renderscript",
1226 ]`,
1227 }},
1228 },
1229 })
1230}
Liz Kammerb4928432023-06-02 18:43:36 -04001231
1232func TestCcBinaryStatic_SystemSharedLibUsedAsDep(t *testing.T) {
1233 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
Chris Parsonscd209032023-09-19 01:12:48 +00001234 stubbedBuildDefinitions: []string{"libm", "libc"},
1235 description: "cc_library_static system_shared_lib empty for linux_bionic variant",
Liz Kammerb4928432023-06-02 18:43:36 -04001236 blueprint: soongCcLibraryStaticPreamble +
Chris Parsonscd209032023-09-19 01:12:48 +00001237 simpleModule("cc_library", "libc") + `
Liz Kammerb4928432023-06-02 18:43:36 -04001238
1239cc_library {
1240 name: "libm",
Liz Kammerb4928432023-06-02 18:43:36 -04001241}
1242
1243cc_binary {
1244 name: "used_in_bionic_oses",
1245 target: {
1246 android: {
1247 static_libs: ["libc"],
1248 },
1249 linux_bionic: {
1250 static_libs: ["libc"],
1251 },
1252 },
1253 include_build_directory: false,
1254 static_executable: true,
1255}
1256
1257cc_binary {
1258 name: "all",
1259 static_libs: ["libc"],
1260 include_build_directory: false,
1261 static_executable: true,
1262}
1263
1264cc_binary {
1265 name: "keep_for_empty_system_shared_libs",
1266 static_libs: ["libc"],
1267 system_shared_libs: [],
1268 include_build_directory: false,
1269 static_executable: true,
1270}
1271
1272cc_binary {
1273 name: "used_with_stubs",
1274 static_libs: ["libm"],
1275 include_build_directory: false,
1276 static_executable: true,
1277}
1278
1279cc_binary {
1280 name: "keep_with_stubs",
1281 static_libs: ["libm"],
1282 system_shared_libs: [],
1283 include_build_directory: false,
1284 static_executable: true,
1285}
1286`,
1287 targets: []testBazelTarget{
1288 {"cc_binary", "all", AttrNameToString{
1289 "linkshared": "False",
1290 }},
1291 {"cc_binary", "keep_for_empty_system_shared_libs", AttrNameToString{
1292 "deps": `[":libc_bp2build_cc_library_static"]`,
1293 "system_deps": `[]`,
1294 "linkshared": "False",
1295 }},
1296 {"cc_binary", "keep_with_stubs", AttrNameToString{
1297 "linkshared": "False",
1298 "deps": `[":libm_bp2build_cc_library_static"]`,
1299 "system_deps": `[]`,
1300 }},
1301 {"cc_binary", "used_in_bionic_oses", AttrNameToString{
1302 "linkshared": "False",
1303 }},
1304 {"cc_binary", "used_with_stubs", AttrNameToString{
1305 "linkshared": "False",
1306 }},
1307 },
1308 })
1309}