blob: c059add95901b7fe3d00ae3e4b68dc863bce8b4c [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) {
Liz Kammerffc17e42022-11-23 09:42:05 -050093 t.Helper()
Paul Duffin4c0765a2022-10-29 17:48:00 +010094 bp2buildSetup := android.GroupFixturePreparers(
95 android.FixtureRegisterWithContext(registerModuleTypes),
96 SetBp2BuildTestRunner,
97 )
Spandan Das5af0bd32022-09-28 20:43:08 +000098 runBp2BuildTestCaseWithSetup(t, bp2buildSetup, tc)
99}
100
101func RunApiBp2BuildTestCase(t *testing.T, registerModuleTypes func(ctx android.RegistrationContext), tc Bp2buildTestCase) {
Liz Kammerffc17e42022-11-23 09:42:05 -0500102 t.Helper()
Paul Duffin4c0765a2022-10-29 17:48:00 +0100103 apiBp2BuildSetup := android.GroupFixturePreparers(
104 android.FixtureRegisterWithContext(registerModuleTypes),
105 SetApiBp2BuildTestRunner,
106 )
Spandan Das5af0bd32022-09-28 20:43:08 +0000107 runBp2BuildTestCaseWithSetup(t, apiBp2BuildSetup, tc)
108}
109
Paul Duffin4c0765a2022-10-29 17:48:00 +0100110func runBp2BuildTestCaseWithSetup(t *testing.T, extraPreparer android.FixturePreparer, tc Bp2buildTestCase) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000111 t.Helper()
112 dir := "."
113 filesystem := make(map[string][]byte)
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000114 for f, content := range tc.Filesystem {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000115 filesystem[f] = []byte(content)
116 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000117
Paul Duffin4c0765a2022-10-29 17:48:00 +0100118 preparers := []android.FixturePreparer{
119 extraPreparer,
120 android.FixtureMergeMockFs(filesystem),
121 android.FixtureWithRootAndroidBp(tc.Blueprint),
122 android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) {
123 ctx.RegisterModuleType(tc.ModuleTypeUnderTest, tc.ModuleTypeUnderTestFactory)
124 }),
125 android.FixtureModifyContext(func(ctx *android.TestContext) {
126 // A default configuration for tests to not have to specify bp2build_available on top level
127 // 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 }
138 ctx.RegisterBp2BuildConfig(bp2buildConfig)
139 }),
140 android.FixtureModifyEnv(func(env map[string]string) {
141 if tc.UnconvertedDepsMode == errorModulesUnconvertedDeps {
142 env["BP2BUILD_ERROR_UNCONVERTED"] = "true"
143 }
144 }),
Jingwen Chen5146ac02021-09-02 11:44:42 +0000145 }
146
Paul Duffin4c0765a2022-10-29 17:48:00 +0100147 preparer := android.GroupFixturePreparers(preparers...)
148 if tc.ExpectedErr != nil {
149 pattern := "\\Q" + tc.ExpectedErr.Error() + "\\E"
150 preparer = preparer.ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(pattern))
151 }
152 result := preparer.RunTestWithCustomResult(t).(*BazelTestResult)
153 if len(result.Errs) > 0 {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000154 return
155 }
156
157 checkDir := dir
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000158 if tc.Dir != "" {
159 checkDir = tc.Dir
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000160 }
Paul Duffin4c0765a2022-10-29 17:48:00 +0100161 expectedTargets := map[string][]string{
162 checkDir: tc.ExpectedBazelTargets,
Liz Kammer6eff3232021-08-26 08:37:59 -0400163 }
Paul Duffin4c0765a2022-10-29 17:48:00 +0100164
165 result.CompareAllBazelTargets(t, tc.Description, expectedTargets, true)
166}
167
168// SetBp2BuildTestRunner customizes the test fixture mechanism to run tests in Bp2Build mode.
169var SetBp2BuildTestRunner = android.FixtureSetTestRunner(&bazelTestRunner{Bp2Build})
170
171// SetApiBp2BuildTestRunner customizes the test fixture mechanism to run tests in ApiBp2build mode.
172var SetApiBp2BuildTestRunner = android.FixtureSetTestRunner(&bazelTestRunner{ApiBp2build})
173
174// bazelTestRunner customizes the test fixture mechanism to run tests of the bp2build and
175// apiBp2build build modes.
176type bazelTestRunner struct {
177 mode CodegenMode
178}
179
180func (b *bazelTestRunner) FinalPreparer(result *android.TestResult) android.CustomTestResult {
181 ctx := result.TestContext
182 switch b.mode {
183 case Bp2Build:
184 ctx.RegisterForBazelConversion()
185 case ApiBp2build:
186 ctx.RegisterForApiBazelConversion()
187 default:
188 panic(fmt.Errorf("unknown build mode: %d", b.mode))
189 }
190
191 return &BazelTestResult{TestResult: result}
192}
193
194func (b *bazelTestRunner) PostParseProcessor(result android.CustomTestResult) {
195 bazelResult := result.(*BazelTestResult)
196 ctx := bazelResult.TestContext
197 config := bazelResult.Config
198 _, errs := ctx.ResolveDependencies(config)
199 if bazelResult.CollateErrs(errs) {
200 return
201 }
202
203 codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build)
204 res, errs := GenerateBazelTargets(codegenCtx, false)
205 if bazelResult.CollateErrs(errs) {
206 return
207 }
208
209 // Store additional data for access by tests.
210 bazelResult.conversionResults = res
211}
212
213// BazelTestResult is a wrapper around android.TestResult to provide type safe access to the bazel
214// specific data stored by the bazelTestRunner.
215type BazelTestResult struct {
216 *android.TestResult
217
218 // The result returned by the GenerateBazelTargets function.
219 conversionResults
220}
221
222// CompareAllBazelTargets compares the BazelTargets produced by the test for all the directories
223// with the supplied set of expected targets.
224//
225// If ignoreUnexpected=false then this enforces an exact match where every BazelTarget produced must
226// have a corresponding expected BazelTarget.
227//
228// If ignoreUnexpected=true then it will ignore directories for which there are no expected targets.
229func (b BazelTestResult) CompareAllBazelTargets(t *testing.T, description string, expectedTargets map[string][]string, ignoreUnexpected bool) {
230 actualTargets := b.buildFileToTargets
231
232 // Generate the sorted set of directories to check.
233 dirsToCheck := android.SortedStringKeys(expectedTargets)
234 if !ignoreUnexpected {
235 // This needs to perform an exact match so add the directories in which targets were
236 // produced to the list of directories to check.
237 dirsToCheck = append(dirsToCheck, android.SortedStringKeys(actualTargets)...)
238 dirsToCheck = android.SortedUniqueStrings(dirsToCheck)
239 }
240
241 for _, dir := range dirsToCheck {
242 expected := expectedTargets[dir]
243 actual := actualTargets[dir]
244
245 if expected == nil {
246 if actual != nil {
247 t.Errorf("did not expect any bazel modules in %q but found %d", dir, len(actual))
248 }
249 } else if actual == nil {
250 expectedCount := len(expected)
251 if expectedCount > 0 {
252 t.Errorf("expected %d bazel modules in %q but did not find any", expectedCount, dir)
253 }
254 } else {
255 b.CompareBazelTargets(t, description, expected, actual)
256 }
257 }
258}
259
260func (b BazelTestResult) CompareBazelTargets(t *testing.T, description string, expectedContents []string, actualTargets BazelTargets) {
261 if actualCount, expectedCount := len(actualTargets), len(expectedContents); actualCount != expectedCount {
Sasha Smundak9d2f1742022-08-04 13:28:38 -0700262 t.Errorf("%s: Expected %d bazel target (%s), got %d (%s)",
Paul Duffin4c0765a2022-10-29 17:48:00 +0100263 description, expectedCount, expectedContents, actualCount, actualTargets)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000264 } else {
Paul Duffin4c0765a2022-10-29 17:48:00 +0100265 for i, actualTarget := range actualTargets {
266 if w, g := expectedContents[i], actualTarget.content; w != g {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000267 t.Errorf(
Paul Duffin4c0765a2022-10-29 17:48:00 +0100268 "%s[%d]: Expected generated Bazel target to be `%s`, got `%s`",
269 description, i, w, g)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000270 }
271 }
272 }
273}
274
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800275type nestedProps struct {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500276 Nested_prop *string
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800277}
278
Liz Kammer32a03392021-09-14 11:17:21 -0400279type EmbeddedProps struct {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500280 Embedded_prop *string
Liz Kammer32a03392021-09-14 11:17:21 -0400281}
282
283type OtherEmbeddedProps struct {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500284 Other_embedded_prop *string
Liz Kammer32a03392021-09-14 11:17:21 -0400285}
286
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800287type customProps struct {
Liz Kammer32a03392021-09-14 11:17:21 -0400288 EmbeddedProps
289 *OtherEmbeddedProps
290
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800291 Bool_prop bool
292 Bool_ptr_prop *bool
293 // 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 +0000294 Int_prop int `blueprint:"mutated"`
295 Int64_ptr_prop *int64
296 String_prop string
297 String_literal_prop *string `android:"arch_variant"`
298 String_ptr_prop *string
299 String_list_prop []string
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800300
301 Nested_props nestedProps
302 Nested_props_ptr *nestedProps
Liz Kammer4562a3b2021-04-21 18:15:34 -0400303
Liz Kammer32b77cf2021-08-04 15:17:02 -0400304 Arch_paths []string `android:"path,arch_variant"`
305 Arch_paths_exclude []string `android:"path,arch_variant"`
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400306
307 // Prop used to indicate this conversion should be 1 module -> multiple targets
308 One_to_many_prop *bool
Spandan Das5af0bd32022-09-28 20:43:08 +0000309
310 Api *string // File describing the APIs of this module
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800311}
312
313type customModule struct {
314 android.ModuleBase
Liz Kammerea6666f2021-02-17 10:17:28 -0500315 android.BazelModuleBase
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800316
317 props customProps
318}
319
320// OutputFiles is needed because some instances of this module use dist with a
321// tag property which requires the module implements OutputFileProducer.
322func (m *customModule) OutputFiles(tag string) (android.Paths, error) {
323 return android.PathsForTesting("path" + tag), nil
324}
325
326func (m *customModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
327 // nothing for now.
328}
329
330func customModuleFactoryBase() android.Module {
331 module := &customModule{}
332 module.AddProperties(&module.props)
Liz Kammerea6666f2021-02-17 10:17:28 -0500333 android.InitBazelModule(module)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800334 return module
335}
336
Liz Kammerdfeb1202022-05-13 17:20:20 -0400337func customModuleFactoryHostAndDevice() android.Module {
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800338 m := customModuleFactoryBase()
Liz Kammer4562a3b2021-04-21 18:15:34 -0400339 android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibBoth)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800340 return m
341}
342
Liz Kammerdfeb1202022-05-13 17:20:20 -0400343func customModuleFactoryDeviceSupported() android.Module {
344 m := customModuleFactoryBase()
345 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibBoth)
346 return m
347}
348
349func customModuleFactoryHostSupported() android.Module {
350 m := customModuleFactoryBase()
351 android.InitAndroidArchModule(m, android.HostSupported, android.MultilibBoth)
352 return m
353}
354
355func customModuleFactoryHostAndDeviceDefault() android.Module {
356 m := customModuleFactoryBase()
357 android.InitAndroidArchModule(m, android.HostAndDeviceDefault, android.MultilibBoth)
358 return m
359}
360
361func customModuleFactoryNeitherHostNorDeviceSupported() android.Module {
362 m := customModuleFactoryBase()
363 android.InitAndroidArchModule(m, android.NeitherHostNorDeviceSupported, android.MultilibBoth)
364 return m
365}
366
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800367type testProps struct {
368 Test_prop struct {
369 Test_string_prop string
370 }
371}
372
373type customTestModule struct {
374 android.ModuleBase
375
376 props customProps
377 test_props testProps
378}
379
380func (m *customTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
381 // nothing for now.
382}
383
384func customTestModuleFactoryBase() android.Module {
385 m := &customTestModule{}
386 m.AddProperties(&m.props)
387 m.AddProperties(&m.test_props)
388 return m
389}
390
391func customTestModuleFactory() android.Module {
392 m := customTestModuleFactoryBase()
393 android.InitAndroidModule(m)
394 return m
395}
396
397type customDefaultsModule struct {
398 android.ModuleBase
399 android.DefaultsModuleBase
400}
401
402func customDefaultsModuleFactoryBase() android.DefaultsModule {
403 module := &customDefaultsModule{}
404 module.AddProperties(&customProps{})
405 return module
406}
407
408func customDefaultsModuleFactoryBasic() android.Module {
409 return customDefaultsModuleFactoryBase()
410}
411
412func customDefaultsModuleFactory() android.Module {
413 m := customDefaultsModuleFactoryBase()
414 android.InitDefaultsModule(m)
415 return m
416}
Jingwen Chen73850672020-12-14 08:25:34 -0500417
Liz Kammer32a03392021-09-14 11:17:21 -0400418type EmbeddedAttr struct {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500419 Embedded_attr *string
Liz Kammer32a03392021-09-14 11:17:21 -0400420}
421
422type OtherEmbeddedAttr struct {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500423 Other_embedded_attr *string
Liz Kammer32a03392021-09-14 11:17:21 -0400424}
425
Jingwen Chen73850672020-12-14 08:25:34 -0500426type customBazelModuleAttributes struct {
Liz Kammer32a03392021-09-14 11:17:21 -0400427 EmbeddedAttr
428 *OtherEmbeddedAttr
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000429 String_literal_prop bazel.StringAttribute
430 String_ptr_prop *string
431 String_list_prop []string
432 Arch_paths bazel.LabelListAttribute
Spandan Das5af0bd32022-09-28 20:43:08 +0000433 Api bazel.LabelAttribute
Jingwen Chen73850672020-12-14 08:25:34 -0500434}
435
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400436func (m *customModule) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400437 if p := m.props.One_to_many_prop; p != nil && *p {
438 customBp2buildOneToMany(ctx, m)
439 return
440 }
Liz Kammer4562a3b2021-04-21 18:15:34 -0400441
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000442 paths := bazel.LabelListAttribute{}
443 strAttr := bazel.StringAttribute{}
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400444 for axis, configToProps := range m.GetArchVariantProperties(ctx, &customProps{}) {
445 for config, props := range configToProps {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000446 if custProps, ok := props.(*customProps); ok {
447 if custProps.Arch_paths != nil {
448 paths.SetSelectValue(axis, config, android.BazelLabelForModuleSrcExcludes(ctx, custProps.Arch_paths, custProps.Arch_paths_exclude))
449 }
450 if custProps.String_literal_prop != nil {
451 strAttr.SetSelectValue(axis, config, custProps.String_literal_prop)
452 }
Liz Kammer4562a3b2021-04-21 18:15:34 -0400453 }
454 }
Jingwen Chen73850672020-12-14 08:25:34 -0500455 }
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400456
457 paths.ResolveExcludes()
458
459 attrs := &customBazelModuleAttributes{
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000460 String_literal_prop: strAttr,
461 String_ptr_prop: m.props.String_ptr_prop,
462 String_list_prop: m.props.String_list_prop,
463 Arch_paths: paths,
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400464 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000465
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400466 attrs.Embedded_attr = m.props.Embedded_prop
467 if m.props.OtherEmbeddedProps != nil {
468 attrs.OtherEmbeddedAttr = &OtherEmbeddedAttr{Other_embedded_attr: m.props.OtherEmbeddedProps.Other_embedded_prop}
469 }
470
471 props := bazel.BazelTargetModuleProperties{
472 Rule_class: "custom",
473 }
474
475 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, attrs)
Jingwen Chen73850672020-12-14 08:25:34 -0500476}
Jingwen Chen40067de2021-01-26 21:58:43 -0500477
Spandan Das5af0bd32022-09-28 20:43:08 +0000478var _ android.ApiProvider = (*customModule)(nil)
479
480func (c *customModule) ConvertWithApiBp2build(ctx android.TopDownMutatorContext) {
481 props := bazel.BazelTargetModuleProperties{
482 Rule_class: "custom_api_contribution",
483 }
484 apiAttribute := bazel.MakeLabelAttribute(
485 android.BazelLabelForModuleSrcSingle(ctx, proptools.String(c.props.Api)).Label,
486 )
487 attrs := &customBazelModuleAttributes{
488 Api: *apiAttribute,
489 }
490 ctx.CreateBazelTargetModule(props,
491 android.CommonAttributes{Name: c.Name()},
492 attrs)
493}
494
Jingwen Chen40067de2021-01-26 21:58:43 -0500495// A bp2build mutator that uses load statements and creates a 1:M mapping from
496// module to target.
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400497func customBp2buildOneToMany(ctx android.TopDownMutatorContext, m *customModule) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500498
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400499 baseName := m.Name()
500 attrs := &customBazelModuleAttributes{}
Jingwen Chen1fd14692021-02-05 03:01:50 -0500501
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400502 myLibraryProps := bazel.BazelTargetModuleProperties{
503 Rule_class: "my_library",
504 Bzl_load_location: "//build/bazel/rules:rules.bzl",
Jingwen Chen40067de2021-01-26 21:58:43 -0500505 }
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400506 ctx.CreateBazelTargetModule(myLibraryProps, android.CommonAttributes{Name: baseName}, attrs)
507
508 protoLibraryProps := bazel.BazelTargetModuleProperties{
509 Rule_class: "proto_library",
510 Bzl_load_location: "//build/bazel/rules:proto.bzl",
511 }
512 ctx.CreateBazelTargetModule(protoLibraryProps, android.CommonAttributes{Name: baseName + "_proto_library_deps"}, attrs)
513
514 myProtoLibraryProps := bazel.BazelTargetModuleProperties{
515 Rule_class: "my_proto_library",
516 Bzl_load_location: "//build/bazel/rules:proto.bzl",
517 }
518 ctx.CreateBazelTargetModule(myProtoLibraryProps, android.CommonAttributes{Name: baseName + "_my_proto_library_deps"}, attrs)
Jingwen Chen40067de2021-01-26 21:58:43 -0500519}
Jingwen Chenba369ad2021-02-22 10:19:34 -0500520
521// Helper method for tests to easily access the targets in a dir.
Liz Kammer6eff3232021-08-26 08:37:59 -0400522func generateBazelTargetsForDir(codegenCtx *CodegenContext, dir string) (BazelTargets, []error) {
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400523 // TODO: Set generateFilegroups to true and/or remove the generateFilegroups argument completely
Liz Kammer6eff3232021-08-26 08:37:59 -0400524 res, err := GenerateBazelTargets(codegenCtx, false)
Alix94e26032022-08-16 20:37:33 +0000525 if err != nil {
526 return BazelTargets{}, err
527 }
Liz Kammer6eff3232021-08-26 08:37:59 -0400528 return res.buildFileToTargets[dir], err
Jingwen Chenba369ad2021-02-22 10:19:34 -0500529}
Liz Kammer32b77cf2021-08-04 15:17:02 -0400530
531func registerCustomModuleForBp2buildConversion(ctx *android.TestContext) {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400532 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
Liz Kammer32b77cf2021-08-04 15:17:02 -0400533 ctx.RegisterForBazelConversion()
534}
Liz Kammer7a210ac2021-09-22 15:52:58 -0400535
536func simpleModuleDoNotConvertBp2build(typ, name string) string {
537 return fmt.Sprintf(`
538%s {
539 name: "%s",
540 bazel_module: { bp2build_available: false },
541}`, typ, name)
542}
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500543
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000544type AttrNameToString map[string]string
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500545
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000546func (a AttrNameToString) clone() AttrNameToString {
547 newAttrs := make(AttrNameToString, len(a))
Liz Kammerdfeb1202022-05-13 17:20:20 -0400548 for k, v := range a {
549 newAttrs[k] = v
550 }
551 return newAttrs
552}
553
554// makeBazelTargetNoRestrictions returns bazel target build file definition that can be host or
555// device specific, or independent of host/device.
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000556func makeBazelTargetHostOrDevice(typ, name string, attrs AttrNameToString, hod android.HostOrDeviceSupported) string {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400557 if _, ok := attrs["target_compatible_with"]; !ok {
558 switch hod {
559 case android.HostSupported:
560 attrs["target_compatible_with"] = `select({
561 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
562 "//conditions:default": [],
563 })`
564 case android.DeviceSupported:
565 attrs["target_compatible_with"] = `["//build/bazel/platforms/os:android"]`
566 }
567 }
568
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500569 attrStrings := make([]string, 0, len(attrs)+1)
Sasha Smundakfb589492022-08-04 11:13:27 -0700570 if name != "" {
571 attrStrings = append(attrStrings, fmt.Sprintf(` name = "%s",`, name))
572 }
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500573 for _, k := range android.SortedStringKeys(attrs) {
574 attrStrings = append(attrStrings, fmt.Sprintf(" %s = %s,", k, attrs[k]))
575 }
576 return fmt.Sprintf(`%s(
577%s
578)`, typ, strings.Join(attrStrings, "\n"))
579}
Liz Kammerdfeb1202022-05-13 17:20:20 -0400580
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000581// MakeBazelTargetNoRestrictions returns bazel target build file definition that does not add a
Liz Kammerdfeb1202022-05-13 17:20:20 -0400582// target_compatible_with. This is useful for module types like filegroup and genrule that arch not
583// arch variant
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000584func MakeBazelTargetNoRestrictions(typ, name string, attrs AttrNameToString) string {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400585 return makeBazelTargetHostOrDevice(typ, name, attrs, android.HostAndDeviceDefault)
586}
587
588// makeBazelTargetNoRestrictions returns bazel target build file definition that is device specific
589// as this is the most common default in Soong.
Alixe06d75b2022-08-31 18:28:19 +0000590func MakeBazelTarget(typ, name string, attrs AttrNameToString) string {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400591 return makeBazelTargetHostOrDevice(typ, name, attrs, android.DeviceSupported)
592}
Sasha Smundak9d2f1742022-08-04 13:28:38 -0700593
594type ExpectedRuleTarget struct {
595 Rule string
596 Name string
597 Attrs AttrNameToString
598 Hod android.HostOrDeviceSupported
599}
600
601func (ebr ExpectedRuleTarget) String() string {
602 return makeBazelTargetHostOrDevice(ebr.Rule, ebr.Name, ebr.Attrs, ebr.Hod)
603}
Trevor Radcliffe087af542022-09-16 15:36:10 +0000604
605func makeCcStubSuiteTargets(name string, attrs AttrNameToString) string {
606 if _, hasStubs := attrs["stubs_symbol_file"]; !hasStubs {
607 return ""
608 }
609 STUB_SUITE_ATTRS := map[string]string{
610 "stubs_symbol_file": "symbol_file",
611 "stubs_versions": "versions",
612 "soname": "soname",
613 "source_library": "source_library",
614 }
615
616 stubSuiteAttrs := AttrNameToString{}
617 for key, _ := range attrs {
618 if _, stubSuiteAttr := STUB_SUITE_ATTRS[key]; stubSuiteAttr {
619 stubSuiteAttrs[STUB_SUITE_ATTRS[key]] = attrs[key]
620 }
621 }
622 return MakeBazelTarget("cc_stub_suite", name+"_stub_libs", stubSuiteAttrs)
623}
Alix341484b2022-10-31 19:08:18 +0000624
625func MakeNeverlinkDuplicateTarget(moduleType string, name string) string {
626 return MakeBazelTarget(moduleType, name+"-neverlink", AttrNameToString{
627 "neverlink": `True`,
628 "exports": `[":` + name + `"]`,
629 })
630}