blob: 30be9979eefd67730b617f1b5a2c119da3d179d8 [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
34 attrs attrNameToString
35}
36
37func generateBazelTargetsForTest(targets []testBazelTarget) []string {
38 ret := make([]string, 0, len(targets))
39 for _, t := range targets {
40 ret = append(ret, makeBazelTarget(t.typ, t.name, t.attrs))
41 }
42 return ret
43}
44
45type ccBinaryBp2buildTestCase struct {
46 description string
47 blueprint string
48 targets []testBazelTarget
49}
50
Liz Kammer2b8004b2021-10-04 13:55:44 -040051func registerCcBinaryModuleTypes(ctx android.RegistrationContext) {
52 cc.RegisterCCBuildComponents(ctx)
53 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
54 ctx.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory)
55 ctx.RegisterModuleType("cc_library", cc.LibraryFactory)
Liz Kammere6583482021-10-19 13:56:10 -040056 ctx.RegisterModuleType("genrule", genrule.GenRuleFactory)
Liz Kammer2b8004b2021-10-04 13:55:44 -040057}
58
Liz Kammer78cfdaa2021-11-08 12:56:31 -050059var binaryReplacer = strings.NewReplacer(ccBinaryTypePlaceHolder, "cc_binary")
60var hostBinaryReplacer = strings.NewReplacer(ccBinaryTypePlaceHolder, "cc_binary_host")
Liz Kammer2b8004b2021-10-04 13:55:44 -040061
Liz Kammer78cfdaa2021-11-08 12:56:31 -050062func runCcBinaryTests(t *testing.T, tc ccBinaryBp2buildTestCase) {
Liz Kammer2b8004b2021-10-04 13:55:44 -040063 t.Helper()
64 runCcBinaryTestCase(t, tc)
65 runCcHostBinaryTestCase(t, tc)
66}
67
Liz Kammer78cfdaa2021-11-08 12:56:31 -050068func runCcBinaryTestCase(t *testing.T, tc ccBinaryBp2buildTestCase) {
Liz Kammer2b8004b2021-10-04 13:55:44 -040069 t.Helper()
Liz Kammer78cfdaa2021-11-08 12:56:31 -050070 moduleTypeUnderTest := "cc_binary"
71 testCase := bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -040072 expectedBazelTargets: generateBazelTargetsForTest(tc.targets),
73 moduleTypeUnderTest: moduleTypeUnderTest,
74 moduleTypeUnderTestFactory: cc.BinaryFactory,
75 description: fmt.Sprintf("%s %s", moduleTypeUnderTest, tc.description),
76 blueprint: binaryReplacer.Replace(tc.blueprint),
Liz Kammer2b8004b2021-10-04 13:55:44 -040077 }
78 t.Run(testCase.description, func(t *testing.T) {
Liz Kammerbe46fcc2021-11-01 15:32:43 -040079 t.Helper()
Liz Kammer2b8004b2021-10-04 13:55:44 -040080 runBp2BuildTestCase(t, registerCcBinaryModuleTypes, testCase)
81 })
82}
83
Liz Kammer78cfdaa2021-11-08 12:56:31 -050084func runCcHostBinaryTestCase(t *testing.T, tc ccBinaryBp2buildTestCase) {
Liz Kammer2b8004b2021-10-04 13:55:44 -040085 t.Helper()
86 testCase := tc
Liz Kammer12615db2021-09-28 09:19:17 -040087 for i, tar := range testCase.targets {
Sam Delmerico75539d62022-01-31 14:37:29 +000088 switch tar.typ {
Trevor Radcliffeef9c9002022-05-13 20:55:35 +000089 case "cc_binary", "proto_library", "cc_lite_proto_library", "genlex":
Sam Delmerico75539d62022-01-31 14:37:29 +000090 tar.attrs["target_compatible_with"] = `select({
Liz Kammer78cfdaa2021-11-08 12:56:31 -050091 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
92 "//conditions:default": [],
93 })`
Sam Delmerico75539d62022-01-31 14:37:29 +000094 }
Liz Kammer12615db2021-09-28 09:19:17 -040095 testCase.targets[i] = tar
Liz Kammer2b8004b2021-10-04 13:55:44 -040096 }
Liz Kammer78cfdaa2021-11-08 12:56:31 -050097 moduleTypeUnderTest := "cc_binary_host"
Liz Kammer2b8004b2021-10-04 13:55:44 -040098 t.Run(testCase.description, func(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -050099 runBp2BuildTestCase(t, registerCcBinaryModuleTypes, bp2buildTestCase{
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400100 expectedBazelTargets: generateBazelTargetsForTest(testCase.targets),
101 moduleTypeUnderTest: moduleTypeUnderTest,
102 moduleTypeUnderTestFactory: cc.BinaryHostFactory,
103 description: fmt.Sprintf("%s %s", moduleTypeUnderTest, tc.description),
104 blueprint: hostBinaryReplacer.Replace(testCase.blueprint),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500105 })
Liz Kammer2b8004b2021-10-04 13:55:44 -0400106 })
107}
108
109func TestBasicCcBinary(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500110 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400111 description: "basic -- properties -> attrs with little/no transformation",
112 blueprint: `
113{rule_name} {
114 name: "foo",
115 srcs: ["a.cc"],
116 local_include_dirs: ["dir"],
117 include_dirs: ["absolute_dir"],
118 cflags: ["-Dcopt"],
119 cppflags: ["-Dcppflag"],
120 conlyflags: ["-Dconlyflag"],
121 asflags: ["-Dasflag"],
122 ldflags: ["ld-flag"],
123 rtti: true,
124 strip: {
125 all: true,
126 keep_symbols: true,
127 keep_symbols_and_debug_frame: true,
128 keep_symbols_list: ["symbol"],
129 none: true,
130 },
Yu Liufc603162022-03-01 15:44:08 -0800131 sdk_version: "current",
132 min_sdk_version: "29",
Yu Liua79c9462022-03-22 16:35:22 -0700133 use_version_lib: true,
Liz Kammer2b8004b2021-10-04 13:55:44 -0400134}
135`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500136 targets: []testBazelTarget{
137 {"cc_binary", "foo", attrNameToString{
138 "absolute_includes": `["absolute_dir"]`,
139 "asflags": `["-Dasflag"]`,
140 "conlyflags": `["-Dconlyflag"]`,
141 "copts": `["-Dcopt"]`,
142 "cppflags": `["-Dcppflag"]`,
143 "linkopts": `["ld-flag"]`,
144 "local_includes": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400145 "dir",
146 ".",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500147 ]`,
148 "rtti": `True`,
149 "srcs": `["a.cc"]`,
150 "strip": `{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400151 "all": True,
152 "keep_symbols": True,
153 "keep_symbols_and_debug_frame": True,
154 "keep_symbols_list": ["symbol"],
155 "none": True,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500156 }`,
Yu Liua79c9462022-03-22 16:35:22 -0700157 "sdk_version": `"current"`,
158 "min_sdk_version": `"29"`,
159 "use_version_lib": `True`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500160 },
161 },
162 },
Liz Kammer2b8004b2021-10-04 13:55:44 -0400163 })
164}
165
166func TestCcBinaryWithSharedLdflagDisableFeature(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500167 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400168 description: `ldflag "-shared" disables static_flag feature`,
169 blueprint: `
170{rule_name} {
171 name: "foo",
172 ldflags: ["-shared"],
173 include_build_directory: false,
174}
175`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500176 targets: []testBazelTarget{
177 {"cc_binary", "foo", attrNameToString{
178 "features": `["-static_flag"]`,
179 "linkopts": `["-shared"]`,
180 },
181 },
182 },
Liz Kammer2b8004b2021-10-04 13:55:44 -0400183 })
184}
185
186func TestCcBinaryWithLinkStatic(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500187 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400188 description: "link static",
189 blueprint: `
190{rule_name} {
191 name: "foo",
192 static_executable: true,
193 include_build_directory: false,
194}
195`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500196 targets: []testBazelTarget{
197 {"cc_binary", "foo", attrNameToString{
198 "linkshared": `False`,
199 },
200 },
201 },
Liz Kammer2b8004b2021-10-04 13:55:44 -0400202 })
203}
204
205func TestCcBinaryVersionScript(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500206 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400207 description: `version script`,
208 blueprint: `
209{rule_name} {
210 name: "foo",
211 include_build_directory: false,
212 version_script: "vs",
213}
214`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500215 targets: []testBazelTarget{
216 {"cc_binary", "foo", attrNameToString{
217 "additional_linker_inputs": `["vs"]`,
218 "linkopts": `["-Wl,--version-script,$(location vs)"]`,
219 },
220 },
221 },
Liz Kammer2b8004b2021-10-04 13:55:44 -0400222 })
223}
224
225func TestCcBinarySplitSrcsByLang(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500226 runCcHostBinaryTestCase(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400227 description: "split srcs by lang",
228 blueprint: `
229{rule_name} {
230 name: "foo",
231 srcs: [
232 "asonly.S",
233 "conly.c",
234 "cpponly.cpp",
235 ":fg_foo",
236 ],
237 include_build_directory: false,
238}
239` + simpleModuleDoNotConvertBp2build("filegroup", "fg_foo"),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500240 targets: []testBazelTarget{
241 {"cc_binary", "foo", attrNameToString{
242 "srcs": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400243 "cpponly.cpp",
244 ":fg_foo_cpp_srcs",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500245 ]`,
246 "srcs_as": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400247 "asonly.S",
248 ":fg_foo_as_srcs",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500249 ]`,
250 "srcs_c": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400251 "conly.c",
252 ":fg_foo_c_srcs",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500253 ]`,
254 },
255 },
256 },
Liz Kammer2b8004b2021-10-04 13:55:44 -0400257 })
258}
259
260func TestCcBinaryDoNotDistinguishBetweenDepsAndImplementationDeps(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500261 runCcBinaryTestCase(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400262 description: "no implementation deps",
263 blueprint: `
Liz Kammere6583482021-10-19 13:56:10 -0400264genrule {
265 name: "generated_hdr",
266 cmd: "nothing to see here",
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400267 bazel_module: { bp2build_available: false },
Liz Kammere6583482021-10-19 13:56:10 -0400268}
269
270genrule {
271 name: "export_generated_hdr",
272 cmd: "nothing to see here",
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400273 bazel_module: { bp2build_available: false },
Liz Kammere6583482021-10-19 13:56:10 -0400274}
275
Liz Kammer2b8004b2021-10-04 13:55:44 -0400276{rule_name} {
277 name: "foo",
Liz Kammere6583482021-10-19 13:56:10 -0400278 srcs: ["foo.cpp"],
Liz Kammer2b8004b2021-10-04 13:55:44 -0400279 shared_libs: ["implementation_shared_dep", "shared_dep"],
280 export_shared_lib_headers: ["shared_dep"],
281 static_libs: ["implementation_static_dep", "static_dep"],
282 export_static_lib_headers: ["static_dep", "whole_static_dep"],
283 whole_static_libs: ["not_explicitly_exported_whole_static_dep", "whole_static_dep"],
284 include_build_directory: false,
Liz Kammere6583482021-10-19 13:56:10 -0400285 generated_headers: ["generated_hdr", "export_generated_hdr"],
286 export_generated_headers: ["export_generated_hdr"],
Liz Kammer2b8004b2021-10-04 13:55:44 -0400287}
288` +
289 simpleModuleDoNotConvertBp2build("cc_library_static", "static_dep") +
290 simpleModuleDoNotConvertBp2build("cc_library_static", "implementation_static_dep") +
291 simpleModuleDoNotConvertBp2build("cc_library_static", "whole_static_dep") +
292 simpleModuleDoNotConvertBp2build("cc_library_static", "not_explicitly_exported_whole_static_dep") +
293 simpleModuleDoNotConvertBp2build("cc_library", "shared_dep") +
294 simpleModuleDoNotConvertBp2build("cc_library", "implementation_shared_dep"),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500295 targets: []testBazelTarget{
296 {"cc_binary", "foo", attrNameToString{
297 "deps": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400298 ":implementation_static_dep",
299 ":static_dep",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500300 ]`,
301 "dynamic_deps": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400302 ":implementation_shared_dep",
303 ":shared_dep",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500304 ]`,
305 "srcs": `[
Liz Kammere6583482021-10-19 13:56:10 -0400306 "foo.cpp",
307 ":generated_hdr",
308 ":export_generated_hdr",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500309 ]`,
310 "whole_archive_deps": `[
Liz Kammer2b8004b2021-10-04 13:55:44 -0400311 ":not_explicitly_exported_whole_static_dep",
312 ":whole_static_dep",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500313 ]`,
Liz Kammer1263d9b2021-12-10 14:28:20 -0500314 "local_includes": `["."]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500315 },
316 },
317 },
Liz Kammer2b8004b2021-10-04 13:55:44 -0400318 })
319}
320
321func TestCcBinaryNocrtTests(t *testing.T) {
322 baseTestCases := []struct {
323 description string
324 soongProperty string
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500325 bazelAttr attrNameToString
Liz Kammer2b8004b2021-10-04 13:55:44 -0400326 }{
327 {
328 description: "nocrt: true",
329 soongProperty: `nocrt: true,`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500330 bazelAttr: attrNameToString{"link_crt": `False`},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400331 },
332 {
333 description: "nocrt: false",
334 soongProperty: `nocrt: false,`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500335 bazelAttr: attrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400336 },
337 {
338 description: "nocrt: not set",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500339 bazelAttr: attrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400340 },
341 }
342
343 baseBlueprint := `{rule_name} {
344 name: "foo",%s
345 include_build_directory: false,
346}
347`
348
Liz Kammer2b8004b2021-10-04 13:55:44 -0400349 for _, btc := range baseTestCases {
350 prop := btc.soongProperty
351 if len(prop) > 0 {
352 prop = "\n" + prop
353 }
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500354 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400355 description: btc.description,
356 blueprint: fmt.Sprintf(baseBlueprint, prop),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500357 targets: []testBazelTarget{
358 {"cc_binary", "foo", btc.bazelAttr},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400359 },
360 })
361 }
362}
363
364func TestCcBinaryNo_libcrtTests(t *testing.T) {
365 baseTestCases := []struct {
366 description string
367 soongProperty string
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500368 bazelAttr attrNameToString
Liz Kammer2b8004b2021-10-04 13:55:44 -0400369 }{
370 {
371 description: "no_libcrt: true",
372 soongProperty: `no_libcrt: true,`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500373 bazelAttr: attrNameToString{"use_libcrt": `False`},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400374 },
375 {
376 description: "no_libcrt: false",
377 soongProperty: `no_libcrt: false,`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500378 bazelAttr: attrNameToString{"use_libcrt": `True`},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400379 },
380 {
381 description: "no_libcrt: not set",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500382 bazelAttr: attrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400383 },
384 }
385
386 baseBlueprint := `{rule_name} {
387 name: "foo",%s
388 include_build_directory: false,
389}
390`
391
Liz Kammer2b8004b2021-10-04 13:55:44 -0400392 for _, btc := range baseTestCases {
393 prop := btc.soongProperty
394 if len(prop) > 0 {
395 prop = "\n" + prop
396 }
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500397 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400398 description: btc.description,
399 blueprint: fmt.Sprintf(baseBlueprint, prop),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500400 targets: []testBazelTarget{
401 {"cc_binary", "foo", btc.bazelAttr},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400402 },
403 })
404 }
405}
406
407func TestCcBinaryPropertiesToFeatures(t *testing.T) {
408 baseTestCases := []struct {
409 description string
410 soongProperty string
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500411 bazelAttr attrNameToString
Liz Kammer2b8004b2021-10-04 13:55:44 -0400412 }{
413 {
414 description: "pack_relocation: true",
415 soongProperty: `pack_relocations: true,`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500416 bazelAttr: attrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400417 },
418 {
419 description: "pack_relocations: false",
420 soongProperty: `pack_relocations: false,`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500421 bazelAttr: attrNameToString{"features": `["disable_pack_relocations"]`},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400422 },
423 {
424 description: "pack_relocations: not set",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500425 bazelAttr: attrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400426 },
427 {
428 description: "pack_relocation: true",
429 soongProperty: `allow_undefined_symbols: true,`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500430 bazelAttr: attrNameToString{"features": `["-no_undefined_symbols"]`},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400431 },
432 {
433 description: "allow_undefined_symbols: false",
434 soongProperty: `allow_undefined_symbols: false,`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500435 bazelAttr: attrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400436 },
437 {
438 description: "allow_undefined_symbols: not set",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500439 bazelAttr: attrNameToString{},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400440 },
441 }
442
443 baseBlueprint := `{rule_name} {
444 name: "foo",%s
445 include_build_directory: false,
446}
447`
Liz Kammer2b8004b2021-10-04 13:55:44 -0400448 for _, btc := range baseTestCases {
449 prop := btc.soongProperty
450 if len(prop) > 0 {
451 prop = "\n" + prop
452 }
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500453 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
Liz Kammer2b8004b2021-10-04 13:55:44 -0400454 description: btc.description,
455 blueprint: fmt.Sprintf(baseBlueprint, prop),
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500456 targets: []testBazelTarget{
457 {"cc_binary", "foo", btc.bazelAttr},
Liz Kammer2b8004b2021-10-04 13:55:44 -0400458 },
459 })
460 }
461}
Liz Kammer12615db2021-09-28 09:19:17 -0400462
463func TestCcBinarySharedProto(t *testing.T) {
464 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
465 blueprint: soongCcProtoLibraries + `{rule_name} {
466 name: "foo",
467 srcs: ["foo.proto"],
468 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -0400469 },
470 include_build_directory: false,
471}`,
472 targets: []testBazelTarget{
473 {"proto_library", "foo_proto", attrNameToString{
474 "srcs": `["foo.proto"]`,
475 }}, {"cc_lite_proto_library", "foo_cc_proto_lite", attrNameToString{
476 "deps": `[":foo_proto"]`,
477 }}, {"cc_binary", "foo", attrNameToString{
478 "dynamic_deps": `[":libprotobuf-cpp-lite"]`,
479 "whole_archive_deps": `[":foo_cc_proto_lite"]`,
480 }},
481 },
482 })
483}
484
485func TestCcBinaryStaticProto(t *testing.T) {
486 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
487 blueprint: soongCcProtoLibraries + `{rule_name} {
488 name: "foo",
489 srcs: ["foo.proto"],
490 static_executable: true,
491 proto: {
Liz Kammer12615db2021-09-28 09:19:17 -0400492 },
493 include_build_directory: false,
494}`,
495 targets: []testBazelTarget{
496 {"proto_library", "foo_proto", attrNameToString{
497 "srcs": `["foo.proto"]`,
498 }}, {"cc_lite_proto_library", "foo_cc_proto_lite", attrNameToString{
499 "deps": `[":foo_proto"]`,
500 }}, {"cc_binary", "foo", attrNameToString{
501 "deps": `[":libprotobuf-cpp-lite"]`,
502 "whole_archive_deps": `[":foo_cc_proto_lite"]`,
503 "linkshared": `False`,
504 }},
505 },
506 })
507}
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000508
509func TestCcBinaryConvertLex(t *testing.T) {
510 runCcBinaryTests(t, ccBinaryBp2buildTestCase{
511 description: `.l and .ll sources converted to .c and .cc`,
512 blueprint: `
513{rule_name} {
514 name: "foo",
515 srcs: ["foo.c", "bar.cc", "foo1.l", "foo2.l", "bar1.ll", "bar2.ll"],
516 lex: { flags: ["--foo_opt", "--bar_opt"] },
517 include_build_directory: false,
518}
519`,
520 targets: []testBazelTarget{
521 {"genlex", "foo_genlex_l", attrNameToString{
522 "srcs": `[
523 "foo1.l",
524 "foo2.l",
525 ]`,
526 "lexopts": `[
527 "--foo_opt",
528 "--bar_opt",
529 ]`,
530 }},
531 {"genlex", "foo_genlex_ll", attrNameToString{
532 "srcs": `[
533 "bar1.ll",
534 "bar2.ll",
535 ]`,
536 "lexopts": `[
537 "--foo_opt",
538 "--bar_opt",
539 ]`,
540 }},
541 {"cc_binary", "foo", attrNameToString{
542 "srcs": `[
543 "bar.cc",
544 ":foo_genlex_ll",
545 ]`,
546 "srcs_c": `[
547 "foo.c",
548 ":foo_genlex_l",
549 ]`,
550 }},
551 },
552 })
553}