blob: 31aa8304a5890d07c8beb1e0ef5a7ac6d4b0cbb7 [file] [log] [blame]
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxf5a3eac2021-08-23 17:05:17 +00001// 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
Liz Kammer2dd9ca42020-11-25 16:06:39 -080015package bp2build
16
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0da7ce62021-08-23 17:04:20 +000017/*
18For shareable/common bp2build testing functionality and dumping ground for
19specific-but-shared functionality among tests in package
20*/
21
Liz Kammer2dd9ca42020-11-25 16:06:39 -080022import (
Liz Kammer7a210ac2021-09-22 15:52:58 -040023 "fmt"
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +000024 "strings"
Rupert Shuttleworth06559d02021-05-19 09:14:26 -040025 "testing"
26
Spandan Das5af0bd32022-09-28 20:43:08 +000027 "github.com/google/blueprint/proptools"
28
Liz Kammer2dd9ca42020-11-25 16:06:39 -080029 "android/soong/android"
Sam Delmerico24c56032022-03-28 19:53:03 +000030 "android/soong/android/allowlists"
Jingwen Chen73850672020-12-14 08:25:34 -050031 "android/soong/bazel"
Liz Kammer2dd9ca42020-11-25 16:06:39 -080032)
33
Jingwen Chen91220d72021-03-24 02:18:33 -040034var (
Rupert Shuttleworth06559d02021-05-19 09:14:26 -040035 buildDir string
Jingwen Chen91220d72021-03-24 02:18:33 -040036)
37
Jingwen Chen5146ac02021-09-02 11:44:42 +000038func checkError(t *testing.T, errs []error, expectedErr error) bool {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +000039 t.Helper()
Jingwen Chen5146ac02021-09-02 11:44:42 +000040
Jingwen Chen5146ac02021-09-02 11:44:42 +000041 if len(errs) != 1 {
Liz Kammer6eff3232021-08-26 08:37:59 -040042 return false
Jingwen Chen5146ac02021-09-02 11:44:42 +000043 }
Liz Kammer54309532021-12-14 12:21:22 -050044 if strings.Contains(errs[0].Error(), expectedErr.Error()) {
Jingwen Chen5146ac02021-09-02 11:44:42 +000045 return true
46 }
47
48 return false
49}
50
Sam Delmerico3177a6e2022-06-21 19:28:33 +000051func errored(t *testing.T, tc Bp2buildTestCase, errs []error) bool {
Jingwen Chen5146ac02021-09-02 11:44:42 +000052 t.Helper()
Sam Delmerico3177a6e2022-06-21 19:28:33 +000053 if tc.ExpectedErr != nil {
Jingwen Chen5146ac02021-09-02 11:44:42 +000054 // Rely on checkErrors, as this test case is expected to have an error.
55 return false
56 }
57
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +000058 if len(errs) > 0 {
59 for _, err := range errs {
Sam Delmerico3177a6e2022-06-21 19:28:33 +000060 t.Errorf("%s: %s", tc.Description, err)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +000061 }
62 return true
63 }
Jingwen Chen5146ac02021-09-02 11:44:42 +000064
65 // All good, continue execution.
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +000066 return false
67}
68
Trevor Radcliffe1b4b2d92022-09-01 18:57:01 +000069func RunBp2BuildTestCaseSimple(t *testing.T, tc Bp2buildTestCase) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +000070 t.Helper()
Sam Delmerico3177a6e2022-06-21 19:28:33 +000071 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, tc)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +000072}
73
Sam Delmerico3177a6e2022-06-21 19:28:33 +000074type Bp2buildTestCase struct {
75 Description string
76 ModuleTypeUnderTest string
77 ModuleTypeUnderTestFactory android.ModuleFactory
78 Blueprint string
79 ExpectedBazelTargets []string
80 Filesystem map[string]string
81 Dir string
Trevor Radcliffe58ea4512022-04-07 20:36:39 +000082 // An error with a string contained within the string of the expected error
Sam Delmerico3177a6e2022-06-21 19:28:33 +000083 ExpectedErr error
84 UnconvertedDepsMode unconvertedDepsMode
Jingwen Chen0eeaeb82022-09-21 10:27:42 +000085
86 // For every directory listed here, the BUILD file for that directory will
87 // be merged with the generated BUILD file. This allows custom BUILD targets
88 // to be used in tests, or use BUILD files to draw package boundaries.
89 KeepBuildFileForDirs []string
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +000090}
91
Sam Delmerico3177a6e2022-06-21 19:28:33 +000092func RunBp2BuildTestCase(t *testing.T, registerModuleTypes func(ctx android.RegistrationContext), tc Bp2buildTestCase) {
Spandan Das5af0bd32022-09-28 20:43:08 +000093 bp2buildSetup := func(ctx *android.TestContext) {
94 registerModuleTypes(ctx)
95 ctx.RegisterForBazelConversion()
96 }
97 runBp2BuildTestCaseWithSetup(t, bp2buildSetup, tc)
98}
99
100func RunApiBp2BuildTestCase(t *testing.T, registerModuleTypes func(ctx android.RegistrationContext), tc Bp2buildTestCase) {
101 apiBp2BuildSetup := func(ctx *android.TestContext) {
102 registerModuleTypes(ctx)
103 ctx.RegisterForApiBazelConversion()
104 }
105 runBp2BuildTestCaseWithSetup(t, apiBp2BuildSetup, tc)
106}
107
108func runBp2BuildTestCaseWithSetup(t *testing.T, setup func(ctx *android.TestContext), tc Bp2buildTestCase) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000109 t.Helper()
110 dir := "."
111 filesystem := make(map[string][]byte)
112 toParse := []string{
113 "Android.bp",
114 }
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000115 for f, content := range tc.Filesystem {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000116 if strings.HasSuffix(f, "Android.bp") {
117 toParse = append(toParse, f)
118 }
119 filesystem[f] = []byte(content)
120 }
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000121 config := android.TestConfig(buildDir, nil, tc.Blueprint, filesystem)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000122 ctx := android.NewTestContext(config)
123
Spandan Das5af0bd32022-09-28 20:43:08 +0000124 setup(ctx)
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000125 ctx.RegisterModuleType(tc.ModuleTypeUnderTest, tc.ModuleTypeUnderTestFactory)
Jingwen Chen0eeaeb82022-09-21 10:27:42 +0000126
127 // A default configuration for tests to not have to specify bp2build_available on top level targets.
128 bp2buildConfig := android.NewBp2BuildAllowlist().SetDefaultConfig(
129 allowlists.Bp2BuildConfig{
130 android.Bp2BuildTopLevel: allowlists.Bp2BuildDefaultTrueRecursively,
131 },
132 )
133 for _, f := range tc.KeepBuildFileForDirs {
134 bp2buildConfig.SetKeepExistingBuildFile(map[string]bool{
135 f: /*recursive=*/ false,
136 })
137 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000138 ctx.RegisterBp2BuildConfig(bp2buildConfig)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000139
Jingwen Chen5146ac02021-09-02 11:44:42 +0000140 _, parseErrs := ctx.ParseFileList(dir, toParse)
141 if errored(t, tc, parseErrs) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000142 return
143 }
Jingwen Chen5146ac02021-09-02 11:44:42 +0000144 _, resolveDepsErrs := ctx.ResolveDependencies(config)
145 if errored(t, tc, resolveDepsErrs) {
146 return
147 }
148
Cole Faustb09da7e2022-05-18 10:57:33 -0700149 parseAndResolveErrs := append(parseErrs, resolveDepsErrs...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000150 if tc.ExpectedErr != nil && checkError(t, parseAndResolveErrs, tc.ExpectedErr) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000151 return
152 }
153
154 checkDir := dir
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000155 if tc.Dir != "" {
156 checkDir = tc.Dir
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000157 }
158 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000159 codegenCtx.unconvertedDepMode = tc.UnconvertedDepsMode
Liz Kammer6eff3232021-08-26 08:37:59 -0400160 bazelTargets, errs := generateBazelTargetsForDir(codegenCtx, checkDir)
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000161 if tc.ExpectedErr != nil {
162 if checkError(t, errs, tc.ExpectedErr) {
Liz Kammer54309532021-12-14 12:21:22 -0500163 return
164 } else {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000165 t.Errorf("Expected error: %q, got: %q and %q", tc.ExpectedErr, errs, parseAndResolveErrs)
Liz Kammer54309532021-12-14 12:21:22 -0500166 }
Liz Kammer6eff3232021-08-26 08:37:59 -0400167 } else {
168 android.FailIfErrored(t, errs)
169 }
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000170 if actualCount, expectedCount := len(bazelTargets), len(tc.ExpectedBazelTargets); actualCount != expectedCount {
Sasha Smundak9d2f1742022-08-04 13:28:38 -0700171 t.Errorf("%s: Expected %d bazel target (%s), got %d (%s)",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000172 tc.Description, expectedCount, tc.ExpectedBazelTargets, actualCount, bazelTargets)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000173 } else {
174 for i, target := range bazelTargets {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000175 if w, g := tc.ExpectedBazelTargets[i], target.content; w != g {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000176 t.Errorf(
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000177 "%s: Expected generated Bazel target to be `%s`, got `%s`",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000178 tc.Description, w, g)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000179 }
180 }
181 }
182}
183
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800184type nestedProps struct {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500185 Nested_prop *string
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800186}
187
Liz Kammer32a03392021-09-14 11:17:21 -0400188type EmbeddedProps struct {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500189 Embedded_prop *string
Liz Kammer32a03392021-09-14 11:17:21 -0400190}
191
192type OtherEmbeddedProps struct {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500193 Other_embedded_prop *string
Liz Kammer32a03392021-09-14 11:17:21 -0400194}
195
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800196type customProps struct {
Liz Kammer32a03392021-09-14 11:17:21 -0400197 EmbeddedProps
198 *OtherEmbeddedProps
199
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800200 Bool_prop bool
201 Bool_ptr_prop *bool
202 // Ensure that properties tagged `blueprint:mutated` are omitted
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000203 Int_prop int `blueprint:"mutated"`
204 Int64_ptr_prop *int64
205 String_prop string
206 String_literal_prop *string `android:"arch_variant"`
207 String_ptr_prop *string
208 String_list_prop []string
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800209
210 Nested_props nestedProps
211 Nested_props_ptr *nestedProps
Liz Kammer4562a3b2021-04-21 18:15:34 -0400212
Liz Kammer32b77cf2021-08-04 15:17:02 -0400213 Arch_paths []string `android:"path,arch_variant"`
214 Arch_paths_exclude []string `android:"path,arch_variant"`
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400215
216 // Prop used to indicate this conversion should be 1 module -> multiple targets
217 One_to_many_prop *bool
Spandan Das5af0bd32022-09-28 20:43:08 +0000218
219 Api *string // File describing the APIs of this module
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800220}
221
222type customModule struct {
223 android.ModuleBase
Liz Kammerea6666f2021-02-17 10:17:28 -0500224 android.BazelModuleBase
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800225
226 props customProps
227}
228
229// OutputFiles is needed because some instances of this module use dist with a
230// tag property which requires the module implements OutputFileProducer.
231func (m *customModule) OutputFiles(tag string) (android.Paths, error) {
232 return android.PathsForTesting("path" + tag), nil
233}
234
235func (m *customModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
236 // nothing for now.
237}
238
239func customModuleFactoryBase() android.Module {
240 module := &customModule{}
241 module.AddProperties(&module.props)
Liz Kammerea6666f2021-02-17 10:17:28 -0500242 android.InitBazelModule(module)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800243 return module
244}
245
Liz Kammerdfeb1202022-05-13 17:20:20 -0400246func customModuleFactoryHostAndDevice() android.Module {
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800247 m := customModuleFactoryBase()
Liz Kammer4562a3b2021-04-21 18:15:34 -0400248 android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibBoth)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800249 return m
250}
251
Liz Kammerdfeb1202022-05-13 17:20:20 -0400252func customModuleFactoryDeviceSupported() android.Module {
253 m := customModuleFactoryBase()
254 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibBoth)
255 return m
256}
257
258func customModuleFactoryHostSupported() android.Module {
259 m := customModuleFactoryBase()
260 android.InitAndroidArchModule(m, android.HostSupported, android.MultilibBoth)
261 return m
262}
263
264func customModuleFactoryHostAndDeviceDefault() android.Module {
265 m := customModuleFactoryBase()
266 android.InitAndroidArchModule(m, android.HostAndDeviceDefault, android.MultilibBoth)
267 return m
268}
269
270func customModuleFactoryNeitherHostNorDeviceSupported() android.Module {
271 m := customModuleFactoryBase()
272 android.InitAndroidArchModule(m, android.NeitherHostNorDeviceSupported, android.MultilibBoth)
273 return m
274}
275
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800276type testProps struct {
277 Test_prop struct {
278 Test_string_prop string
279 }
280}
281
282type customTestModule struct {
283 android.ModuleBase
284
285 props customProps
286 test_props testProps
287}
288
289func (m *customTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
290 // nothing for now.
291}
292
293func customTestModuleFactoryBase() android.Module {
294 m := &customTestModule{}
295 m.AddProperties(&m.props)
296 m.AddProperties(&m.test_props)
297 return m
298}
299
300func customTestModuleFactory() android.Module {
301 m := customTestModuleFactoryBase()
302 android.InitAndroidModule(m)
303 return m
304}
305
306type customDefaultsModule struct {
307 android.ModuleBase
308 android.DefaultsModuleBase
309}
310
311func customDefaultsModuleFactoryBase() android.DefaultsModule {
312 module := &customDefaultsModule{}
313 module.AddProperties(&customProps{})
314 return module
315}
316
317func customDefaultsModuleFactoryBasic() android.Module {
318 return customDefaultsModuleFactoryBase()
319}
320
321func customDefaultsModuleFactory() android.Module {
322 m := customDefaultsModuleFactoryBase()
323 android.InitDefaultsModule(m)
324 return m
325}
Jingwen Chen73850672020-12-14 08:25:34 -0500326
Liz Kammer32a03392021-09-14 11:17:21 -0400327type EmbeddedAttr struct {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500328 Embedded_attr *string
Liz Kammer32a03392021-09-14 11:17:21 -0400329}
330
331type OtherEmbeddedAttr struct {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500332 Other_embedded_attr *string
Liz Kammer32a03392021-09-14 11:17:21 -0400333}
334
Jingwen Chen73850672020-12-14 08:25:34 -0500335type customBazelModuleAttributes struct {
Liz Kammer32a03392021-09-14 11:17:21 -0400336 EmbeddedAttr
337 *OtherEmbeddedAttr
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000338 String_literal_prop bazel.StringAttribute
339 String_ptr_prop *string
340 String_list_prop []string
341 Arch_paths bazel.LabelListAttribute
Spandan Das5af0bd32022-09-28 20:43:08 +0000342 Api bazel.LabelAttribute
Jingwen Chen73850672020-12-14 08:25:34 -0500343}
344
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400345func (m *customModule) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400346 if p := m.props.One_to_many_prop; p != nil && *p {
347 customBp2buildOneToMany(ctx, m)
348 return
349 }
Liz Kammer4562a3b2021-04-21 18:15:34 -0400350
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000351 paths := bazel.LabelListAttribute{}
352 strAttr := bazel.StringAttribute{}
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400353 for axis, configToProps := range m.GetArchVariantProperties(ctx, &customProps{}) {
354 for config, props := range configToProps {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000355 if custProps, ok := props.(*customProps); ok {
356 if custProps.Arch_paths != nil {
357 paths.SetSelectValue(axis, config, android.BazelLabelForModuleSrcExcludes(ctx, custProps.Arch_paths, custProps.Arch_paths_exclude))
358 }
359 if custProps.String_literal_prop != nil {
360 strAttr.SetSelectValue(axis, config, custProps.String_literal_prop)
361 }
Liz Kammer4562a3b2021-04-21 18:15:34 -0400362 }
363 }
Jingwen Chen73850672020-12-14 08:25:34 -0500364 }
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400365
366 paths.ResolveExcludes()
367
368 attrs := &customBazelModuleAttributes{
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000369 String_literal_prop: strAttr,
370 String_ptr_prop: m.props.String_ptr_prop,
371 String_list_prop: m.props.String_list_prop,
372 Arch_paths: paths,
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400373 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000374
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400375 attrs.Embedded_attr = m.props.Embedded_prop
376 if m.props.OtherEmbeddedProps != nil {
377 attrs.OtherEmbeddedAttr = &OtherEmbeddedAttr{Other_embedded_attr: m.props.OtherEmbeddedProps.Other_embedded_prop}
378 }
379
380 props := bazel.BazelTargetModuleProperties{
381 Rule_class: "custom",
382 }
383
384 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, attrs)
Jingwen Chen73850672020-12-14 08:25:34 -0500385}
Jingwen Chen40067de2021-01-26 21:58:43 -0500386
Spandan Das5af0bd32022-09-28 20:43:08 +0000387var _ android.ApiProvider = (*customModule)(nil)
388
389func (c *customModule) ConvertWithApiBp2build(ctx android.TopDownMutatorContext) {
390 props := bazel.BazelTargetModuleProperties{
391 Rule_class: "custom_api_contribution",
392 }
393 apiAttribute := bazel.MakeLabelAttribute(
394 android.BazelLabelForModuleSrcSingle(ctx, proptools.String(c.props.Api)).Label,
395 )
396 attrs := &customBazelModuleAttributes{
397 Api: *apiAttribute,
398 }
399 ctx.CreateBazelTargetModule(props,
400 android.CommonAttributes{Name: c.Name()},
401 attrs)
402}
403
Jingwen Chen40067de2021-01-26 21:58:43 -0500404// A bp2build mutator that uses load statements and creates a 1:M mapping from
405// module to target.
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400406func customBp2buildOneToMany(ctx android.TopDownMutatorContext, m *customModule) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500407
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400408 baseName := m.Name()
409 attrs := &customBazelModuleAttributes{}
Jingwen Chen1fd14692021-02-05 03:01:50 -0500410
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400411 myLibraryProps := bazel.BazelTargetModuleProperties{
412 Rule_class: "my_library",
413 Bzl_load_location: "//build/bazel/rules:rules.bzl",
Jingwen Chen40067de2021-01-26 21:58:43 -0500414 }
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400415 ctx.CreateBazelTargetModule(myLibraryProps, android.CommonAttributes{Name: baseName}, attrs)
416
417 protoLibraryProps := bazel.BazelTargetModuleProperties{
418 Rule_class: "proto_library",
419 Bzl_load_location: "//build/bazel/rules:proto.bzl",
420 }
421 ctx.CreateBazelTargetModule(protoLibraryProps, android.CommonAttributes{Name: baseName + "_proto_library_deps"}, attrs)
422
423 myProtoLibraryProps := bazel.BazelTargetModuleProperties{
424 Rule_class: "my_proto_library",
425 Bzl_load_location: "//build/bazel/rules:proto.bzl",
426 }
427 ctx.CreateBazelTargetModule(myProtoLibraryProps, android.CommonAttributes{Name: baseName + "_my_proto_library_deps"}, attrs)
Jingwen Chen40067de2021-01-26 21:58:43 -0500428}
Jingwen Chenba369ad2021-02-22 10:19:34 -0500429
430// Helper method for tests to easily access the targets in a dir.
Liz Kammer6eff3232021-08-26 08:37:59 -0400431func generateBazelTargetsForDir(codegenCtx *CodegenContext, dir string) (BazelTargets, []error) {
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400432 // TODO: Set generateFilegroups to true and/or remove the generateFilegroups argument completely
Liz Kammer6eff3232021-08-26 08:37:59 -0400433 res, err := GenerateBazelTargets(codegenCtx, false)
Alix94e26032022-08-16 20:37:33 +0000434 if err != nil {
435 return BazelTargets{}, err
436 }
Liz Kammer6eff3232021-08-26 08:37:59 -0400437 return res.buildFileToTargets[dir], err
Jingwen Chenba369ad2021-02-22 10:19:34 -0500438}
Liz Kammer32b77cf2021-08-04 15:17:02 -0400439
440func registerCustomModuleForBp2buildConversion(ctx *android.TestContext) {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400441 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
Liz Kammer32b77cf2021-08-04 15:17:02 -0400442 ctx.RegisterForBazelConversion()
443}
Liz Kammer7a210ac2021-09-22 15:52:58 -0400444
445func simpleModuleDoNotConvertBp2build(typ, name string) string {
446 return fmt.Sprintf(`
447%s {
448 name: "%s",
449 bazel_module: { bp2build_available: false },
450}`, typ, name)
451}
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500452
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000453type AttrNameToString map[string]string
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500454
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000455func (a AttrNameToString) clone() AttrNameToString {
456 newAttrs := make(AttrNameToString, len(a))
Liz Kammerdfeb1202022-05-13 17:20:20 -0400457 for k, v := range a {
458 newAttrs[k] = v
459 }
460 return newAttrs
461}
462
463// makeBazelTargetNoRestrictions returns bazel target build file definition that can be host or
464// device specific, or independent of host/device.
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000465func makeBazelTargetHostOrDevice(typ, name string, attrs AttrNameToString, hod android.HostOrDeviceSupported) string {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400466 if _, ok := attrs["target_compatible_with"]; !ok {
467 switch hod {
468 case android.HostSupported:
469 attrs["target_compatible_with"] = `select({
470 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
471 "//conditions:default": [],
472 })`
473 case android.DeviceSupported:
474 attrs["target_compatible_with"] = `["//build/bazel/platforms/os:android"]`
475 }
476 }
477
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500478 attrStrings := make([]string, 0, len(attrs)+1)
Sasha Smundakfb589492022-08-04 11:13:27 -0700479 if name != "" {
480 attrStrings = append(attrStrings, fmt.Sprintf(` name = "%s",`, name))
481 }
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500482 for _, k := range android.SortedStringKeys(attrs) {
483 attrStrings = append(attrStrings, fmt.Sprintf(" %s = %s,", k, attrs[k]))
484 }
485 return fmt.Sprintf(`%s(
486%s
487)`, typ, strings.Join(attrStrings, "\n"))
488}
Liz Kammerdfeb1202022-05-13 17:20:20 -0400489
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000490// MakeBazelTargetNoRestrictions returns bazel target build file definition that does not add a
Liz Kammerdfeb1202022-05-13 17:20:20 -0400491// target_compatible_with. This is useful for module types like filegroup and genrule that arch not
492// arch variant
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000493func MakeBazelTargetNoRestrictions(typ, name string, attrs AttrNameToString) string {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400494 return makeBazelTargetHostOrDevice(typ, name, attrs, android.HostAndDeviceDefault)
495}
496
497// makeBazelTargetNoRestrictions returns bazel target build file definition that is device specific
498// as this is the most common default in Soong.
Alixe06d75b2022-08-31 18:28:19 +0000499func MakeBazelTarget(typ, name string, attrs AttrNameToString) string {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400500 return makeBazelTargetHostOrDevice(typ, name, attrs, android.DeviceSupported)
501}
Sasha Smundak9d2f1742022-08-04 13:28:38 -0700502
503type ExpectedRuleTarget struct {
504 Rule string
505 Name string
506 Attrs AttrNameToString
507 Hod android.HostOrDeviceSupported
508}
509
510func (ebr ExpectedRuleTarget) String() string {
511 return makeBazelTargetHostOrDevice(ebr.Rule, ebr.Name, ebr.Attrs, ebr.Hod)
512}
Trevor Radcliffe087af542022-09-16 15:36:10 +0000513
514func makeCcStubSuiteTargets(name string, attrs AttrNameToString) string {
515 if _, hasStubs := attrs["stubs_symbol_file"]; !hasStubs {
516 return ""
517 }
518 STUB_SUITE_ATTRS := map[string]string{
519 "stubs_symbol_file": "symbol_file",
520 "stubs_versions": "versions",
521 "soname": "soname",
522 "source_library": "source_library",
523 }
524
525 stubSuiteAttrs := AttrNameToString{}
526 for key, _ := range attrs {
527 if _, stubSuiteAttr := STUB_SUITE_ATTRS[key]; stubSuiteAttr {
528 stubSuiteAttrs[STUB_SUITE_ATTRS[key]] = attrs[key]
529 }
530 }
531 return MakeBazelTarget("cc_stub_suite", name+"_stub_libs", stubSuiteAttrs)
532}