blob: 6907487566633c61df7c59c17aaa811625991bf6 [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"
Chris Parsons5011e612023-09-13 23:33:20 +000024 "path/filepath"
Chris Parsonscd209032023-09-19 01:12:48 +000025 "regexp"
Zi Wangfba0a212023-03-07 16:48:19 -080026 "sort"
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +000027 "strings"
Rupert Shuttleworth06559d02021-05-19 09:14:26 -040028 "testing"
29
Chris Parsons2173b5f2023-09-25 19:01:40 +000030 "android/soong/ui/metrics/bp2build_metrics_proto"
Spandan Das5af0bd32022-09-28 20:43:08 +000031 "github.com/google/blueprint/proptools"
32
Liz Kammer2dd9ca42020-11-25 16:06:39 -080033 "android/soong/android"
Sam Delmerico24c56032022-03-28 19:53:03 +000034 "android/soong/android/allowlists"
Jingwen Chen73850672020-12-14 08:25:34 -050035 "android/soong/bazel"
Liz Kammer2dd9ca42020-11-25 16:06:39 -080036)
37
Jingwen Chen91220d72021-03-24 02:18:33 -040038var (
Rupert Shuttleworth06559d02021-05-19 09:14:26 -040039 buildDir string
Jingwen Chen91220d72021-03-24 02:18:33 -040040)
41
Chris Parsonscd209032023-09-19 01:12:48 +000042var labelRegex = regexp.MustCompile(`^//([^: ]+):([^ ]+)$`)
43var simpleModuleNameRegex = regexp.MustCompile(`^[^: /]+$`)
44
Jingwen Chen5146ac02021-09-02 11:44:42 +000045func 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 +000046 t.Helper()
Jingwen Chen5146ac02021-09-02 11:44:42 +000047
Jingwen Chen5146ac02021-09-02 11:44:42 +000048 if len(errs) != 1 {
Liz Kammer6eff3232021-08-26 08:37:59 -040049 return false
Jingwen Chen5146ac02021-09-02 11:44:42 +000050 }
Liz Kammer54309532021-12-14 12:21:22 -050051 if strings.Contains(errs[0].Error(), expectedErr.Error()) {
Jingwen Chen5146ac02021-09-02 11:44:42 +000052 return true
53 }
54
55 return false
56}
57
Sam Delmerico3177a6e2022-06-21 19:28:33 +000058func errored(t *testing.T, tc Bp2buildTestCase, errs []error) bool {
Jingwen Chen5146ac02021-09-02 11:44:42 +000059 t.Helper()
Sam Delmerico3177a6e2022-06-21 19:28:33 +000060 if tc.ExpectedErr != nil {
Jingwen Chen5146ac02021-09-02 11:44:42 +000061 // Rely on checkErrors, as this test case is expected to have an error.
62 return false
63 }
64
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +000065 if len(errs) > 0 {
66 for _, err := range errs {
Sam Delmerico3177a6e2022-06-21 19:28:33 +000067 t.Errorf("%s: %s", tc.Description, err)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +000068 }
69 return true
70 }
Jingwen Chen5146ac02021-09-02 11:44:42 +000071
72 // All good, continue execution.
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +000073 return false
74}
75
Trevor Radcliffe1b4b2d92022-09-01 18:57:01 +000076func RunBp2BuildTestCaseSimple(t *testing.T, tc Bp2buildTestCase) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +000077 t.Helper()
Sam Delmerico3177a6e2022-06-21 19:28:33 +000078 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, tc)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +000079}
80
Sam Delmerico3177a6e2022-06-21 19:28:33 +000081type Bp2buildTestCase struct {
82 Description string
83 ModuleTypeUnderTest string
84 ModuleTypeUnderTestFactory android.ModuleFactory
Sam Delmerico5840afc2023-06-12 15:44:03 -040085 // Text to add to the toplevel, root Android.bp file. If Dir is not set, all
86 // ExpectedBazelTargets are assumed to be generated by this file.
Chris Parsons39a16972023-06-08 14:28:51 +000087 Blueprint string
Sam Delmerico5840afc2023-06-12 15:44:03 -040088 // ExpectedBazelTargets compares the BazelTargets generated in `Dir` (if not empty).
89 // Otherwise, it checks the BazelTargets generated by `Blueprint` in the root directory.
90 ExpectedBazelTargets []string
Chris Parsons2173b5f2023-09-25 19:01:40 +000091 // ExpectedConvertedModules asserts that modules in this list are labeled as "converted
92 // by bp2build" in the metrics reported by bp2build.
93 ExpectedConvertedModules []string
94 // ExpectedHandcraftedModules asserts that modules in this list are labeled as "handcrafted
95 // in build files" in the metrics reported by bp2build. Such modules are either explicitly
96 // defined in a BUILD file (by name), or registered as "otherwise implicitly handled"
97 // by bp2build (for example, by macros owned by other modules).
98 ExpectedHandcraftedModules []string
99
Chris Parsons5011e612023-09-13 23:33:20 +0000100 // AlreadyExistingBuildContents, if non-empty, simulates an already-present source BUILD file
101 // in the directory under test. The BUILD file has the given contents. This BUILD file
102 // will also be treated as "BUILD file to keep" by the simulated bp2build environment.
103 AlreadyExistingBuildContents string
Chris Parsonscd209032023-09-19 01:12:48 +0000104
105 // StubbedBuildDefinitions, if non-empty, adds stub definitions to already-present source
106 // BUILD files for each bazel label given. The BUILD files with these stub definitions
107 // are added to the BUILD file given in AlreadyExistingBuildContents.
108 // Labels may be of the form //pkg/to:target_name (which would be defined in pkg/to/BUILD.bazel)
109 // or `target_name` (which would be defined in ./BUILD.bazel).
Chris Parsons5011e612023-09-13 23:33:20 +0000110 StubbedBuildDefinitions []string
111
112 Filesystem map[string]string
Sam Delmerico5840afc2023-06-12 15:44:03 -0400113 // Dir sets the directory which will be compared against the targets in ExpectedBazelTargets.
114 // This should used in conjunction with the Filesystem property to check for targets
115 // generated from a directory that is not the root.
116 // If not set, all ExpectedBazelTargets are assumed to be generated by the text in the
117 // Blueprint property.
118 Dir string
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000119 // An error with a string contained within the string of the expected error
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000120 ExpectedErr error
121 UnconvertedDepsMode unconvertedDepsMode
Jingwen Chen0eeaeb82022-09-21 10:27:42 +0000122
123 // For every directory listed here, the BUILD file for that directory will
124 // be merged with the generated BUILD file. This allows custom BUILD targets
125 // to be used in tests, or use BUILD files to draw package boundaries.
126 KeepBuildFileForDirs []string
Chris Parsons5f1b3c72023-09-28 20:41:03 +0000127
128 // If true, the bp2build_deps mutator is used for this test. This is an
129 // experimental mutator that will disable modules which have transitive
130 // dependencies with no bazel definition.
131 // TODO: b/285631638 - Enable this feature by default.
132 DepsMutator bool
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000133}
134
Chris Parsons2173b5f2023-09-25 19:01:40 +0000135func RunBp2BuildTestCaseExtraContext(t *testing.T, registerModuleTypes func(ctx android.RegistrationContext), modifyContext func(ctx *android.TestContext), tc Bp2buildTestCase) {
136 t.Helper()
Chris Parsons5f1b3c72023-09-28 20:41:03 +0000137 preparers := []android.FixturePreparer{
Chris Parsons2173b5f2023-09-25 19:01:40 +0000138 android.FixtureRegisterWithContext(registerModuleTypes),
Chris Parsons5f1b3c72023-09-28 20:41:03 +0000139 }
140 if modifyContext != nil {
141 preparers = append(preparers, android.FixtureModifyContext(modifyContext))
142 }
143 if tc.DepsMutator {
144 preparers = append(preparers, android.FixtureModifyConfig(func(cfg android.Config) {
145 cfg.Bp2buildDepsMutator = true
146 }))
147 }
148 preparers = append(preparers, SetBp2BuildTestRunner)
149 bp2buildSetup := android.GroupFixturePreparers(
150 preparers...,
Chris Parsons2173b5f2023-09-25 19:01:40 +0000151 )
152 runBp2BuildTestCaseWithSetup(t, bp2buildSetup, tc)
153}
154
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000155func RunBp2BuildTestCase(t *testing.T, registerModuleTypes func(ctx android.RegistrationContext), tc Bp2buildTestCase) {
Liz Kammerffc17e42022-11-23 09:42:05 -0500156 t.Helper()
Chris Parsons5f1b3c72023-09-28 20:41:03 +0000157 RunBp2BuildTestCaseExtraContext(t, registerModuleTypes, nil, tc)
Spandan Das5af0bd32022-09-28 20:43:08 +0000158}
159
Paul Duffin4c0765a2022-10-29 17:48:00 +0100160func 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 +0000161 t.Helper()
Chris Parsonscd209032023-09-19 01:12:48 +0000162 if tc.Filesystem == nil {
163 tc.Filesystem = map[string]string{}
164 }
Chris Parsons5011e612023-09-13 23:33:20 +0000165 checkDir := "."
166 if tc.Dir != "" {
167 checkDir = tc.Dir
168 }
169 keepExistingBuildDirs := tc.KeepBuildFileForDirs
170 buildFilesToParse := []string{}
Chris Parsonscd209032023-09-19 01:12:48 +0000171
172 if len(tc.StubbedBuildDefinitions) > 0 {
173 for _, buildDef := range tc.StubbedBuildDefinitions {
174 globalLabelMatch := labelRegex.FindStringSubmatch(buildDef)
175 var dir, targetName string
176 if len(globalLabelMatch) > 0 {
177 dir = globalLabelMatch[1]
178 targetName = globalLabelMatch[2]
179 } else {
180 if !simpleModuleNameRegex.MatchString(buildDef) {
181 t.Errorf("Stubbed build definition '%s' must be either a simple module name or of global target syntax (//foo/bar:baz).", buildDef)
182 return
183 }
184 dir = "."
185 targetName = buildDef
186 }
187 buildFilePath := filepath.Join(dir, "BUILD")
188 tc.Filesystem[buildFilePath] +=
189 MakeBazelTarget(
190 "bp2build_test_stub",
191 targetName,
192 AttrNameToString{})
193 keepExistingBuildDirs = append(keepExistingBuildDirs, dir)
194 buildFilesToParse = append(buildFilesToParse, buildFilePath)
195 }
196 }
197 if len(tc.AlreadyExistingBuildContents) > 0 {
198 buildFilePath := filepath.Join(checkDir, "BUILD")
199 tc.Filesystem[buildFilePath] += tc.AlreadyExistingBuildContents
200 keepExistingBuildDirs = append(keepExistingBuildDirs, checkDir)
201 buildFilesToParse = append(buildFilesToParse, buildFilePath)
202 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000203 filesystem := make(map[string][]byte)
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000204 for f, content := range tc.Filesystem {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000205 filesystem[f] = []byte(content)
206 }
Paul Duffin4c0765a2022-10-29 17:48:00 +0100207 preparers := []android.FixturePreparer{
208 extraPreparer,
209 android.FixtureMergeMockFs(filesystem),
210 android.FixtureWithRootAndroidBp(tc.Blueprint),
211 android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) {
212 ctx.RegisterModuleType(tc.ModuleTypeUnderTest, tc.ModuleTypeUnderTestFactory)
213 }),
Chris Parsons5011e612023-09-13 23:33:20 +0000214 android.FixtureModifyContextWithMockFs(func(ctx *android.TestContext) {
Paul Duffin4c0765a2022-10-29 17:48:00 +0100215 // A default configuration for tests to not have to specify bp2build_available on top level
216 // targets.
217 bp2buildConfig := android.NewBp2BuildAllowlist().SetDefaultConfig(
218 allowlists.Bp2BuildConfig{
219 android.Bp2BuildTopLevel: allowlists.Bp2BuildDefaultTrueRecursively,
220 },
221 )
Chris Parsons5011e612023-09-13 23:33:20 +0000222 for _, f := range keepExistingBuildDirs {
Paul Duffin4c0765a2022-10-29 17:48:00 +0100223 bp2buildConfig.SetKeepExistingBuildFile(map[string]bool{
224 f: /*recursive=*/ false,
225 })
226 }
227 ctx.RegisterBp2BuildConfig(bp2buildConfig)
Chris Parsons39a16972023-06-08 14:28:51 +0000228 // This setting is added to bp2build invocations. It prevents bp2build
229 // from cloning modules to their original state after mutators run. This
230 // would lose some data intentionally set by these mutators.
231 ctx.SkipCloneModulesAfterMutators = true
Chris Parsons5011e612023-09-13 23:33:20 +0000232 err := ctx.RegisterExistingBazelTargets(".", buildFilesToParse)
233 if err != nil {
234 t.Errorf("error parsing build files in test setup: %s", err)
235 }
Paul Duffin4c0765a2022-10-29 17:48:00 +0100236 }),
237 android.FixtureModifyEnv(func(env map[string]string) {
238 if tc.UnconvertedDepsMode == errorModulesUnconvertedDeps {
239 env["BP2BUILD_ERROR_UNCONVERTED"] = "true"
240 }
241 }),
Jingwen Chen5146ac02021-09-02 11:44:42 +0000242 }
243
Paul Duffin4c0765a2022-10-29 17:48:00 +0100244 preparer := android.GroupFixturePreparers(preparers...)
245 if tc.ExpectedErr != nil {
246 pattern := "\\Q" + tc.ExpectedErr.Error() + "\\E"
247 preparer = preparer.ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(pattern))
248 }
249 result := preparer.RunTestWithCustomResult(t).(*BazelTestResult)
250 if len(result.Errs) > 0 {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000251 return
252 }
253
Paul Duffin4c0765a2022-10-29 17:48:00 +0100254 expectedTargets := map[string][]string{
255 checkDir: tc.ExpectedBazelTargets,
Liz Kammer6eff3232021-08-26 08:37:59 -0400256 }
Paul Duffin4c0765a2022-10-29 17:48:00 +0100257
Chris Parsons2173b5f2023-09-25 19:01:40 +0000258 result.CompareAllBazelTargets(t, tc, expectedTargets, true)
Paul Duffin4c0765a2022-10-29 17:48:00 +0100259}
260
261// SetBp2BuildTestRunner customizes the test fixture mechanism to run tests in Bp2Build mode.
Chris Parsons73f411b2023-06-20 21:46:57 +0000262var SetBp2BuildTestRunner = android.FixtureSetTestRunner(&bazelTestRunner{})
Paul Duffin4c0765a2022-10-29 17:48:00 +0100263
Chris Parsons73f411b2023-06-20 21:46:57 +0000264// bazelTestRunner customizes the test fixture mechanism to run tests of the bp2build build mode.
265type bazelTestRunner struct{}
Paul Duffin4c0765a2022-10-29 17:48:00 +0100266
267func (b *bazelTestRunner) FinalPreparer(result *android.TestResult) android.CustomTestResult {
268 ctx := result.TestContext
Chris Parsons73f411b2023-06-20 21:46:57 +0000269 ctx.RegisterForBazelConversion()
Paul Duffin4c0765a2022-10-29 17:48:00 +0100270
271 return &BazelTestResult{TestResult: result}
272}
273
274func (b *bazelTestRunner) PostParseProcessor(result android.CustomTestResult) {
275 bazelResult := result.(*BazelTestResult)
276 ctx := bazelResult.TestContext
277 config := bazelResult.Config
278 _, errs := ctx.ResolveDependencies(config)
279 if bazelResult.CollateErrs(errs) {
280 return
281 }
282
Chris Parsons73f411b2023-06-20 21:46:57 +0000283 codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "")
Paul Duffin4c0765a2022-10-29 17:48:00 +0100284 res, errs := GenerateBazelTargets(codegenCtx, false)
285 if bazelResult.CollateErrs(errs) {
286 return
287 }
288
289 // Store additional data for access by tests.
290 bazelResult.conversionResults = res
291}
292
293// BazelTestResult is a wrapper around android.TestResult to provide type safe access to the bazel
294// specific data stored by the bazelTestRunner.
295type BazelTestResult struct {
296 *android.TestResult
297
298 // The result returned by the GenerateBazelTargets function.
299 conversionResults
300}
301
302// CompareAllBazelTargets compares the BazelTargets produced by the test for all the directories
303// with the supplied set of expected targets.
304//
305// If ignoreUnexpected=false then this enforces an exact match where every BazelTarget produced must
306// have a corresponding expected BazelTarget.
307//
308// If ignoreUnexpected=true then it will ignore directories for which there are no expected targets.
Chris Parsons2173b5f2023-09-25 19:01:40 +0000309func (b BazelTestResult) CompareAllBazelTargets(t *testing.T, tc Bp2buildTestCase, expectedTargets map[string][]string, ignoreUnexpected bool) {
Liz Kammer2b3f56e2023-03-23 11:51:49 -0400310 t.Helper()
Paul Duffin4c0765a2022-10-29 17:48:00 +0100311 actualTargets := b.buildFileToTargets
312
313 // Generate the sorted set of directories to check.
Cole Faust18994c72023-02-28 16:02:16 -0800314 dirsToCheck := android.SortedKeys(expectedTargets)
Paul Duffin4c0765a2022-10-29 17:48:00 +0100315 if !ignoreUnexpected {
316 // This needs to perform an exact match so add the directories in which targets were
317 // produced to the list of directories to check.
Cole Faust18994c72023-02-28 16:02:16 -0800318 dirsToCheck = append(dirsToCheck, android.SortedKeys(actualTargets)...)
Paul Duffin4c0765a2022-10-29 17:48:00 +0100319 dirsToCheck = android.SortedUniqueStrings(dirsToCheck)
320 }
321
322 for _, dir := range dirsToCheck {
323 expected := expectedTargets[dir]
324 actual := actualTargets[dir]
325
326 if expected == nil {
327 if actual != nil {
328 t.Errorf("did not expect any bazel modules in %q but found %d", dir, len(actual))
329 }
330 } else if actual == nil {
331 expectedCount := len(expected)
332 if expectedCount > 0 {
333 t.Errorf("expected %d bazel modules in %q but did not find any", expectedCount, dir)
334 }
335 } else {
Chris Parsons2173b5f2023-09-25 19:01:40 +0000336 b.CompareBazelTargets(t, tc.Description, expected, actual)
337 }
338 }
339
340 for _, module := range tc.ExpectedConvertedModules {
341 if _, found := b.metrics.convertedModulePathMap[module]; !found {
342 t.Errorf("expected %s to be generated by bp2build, but was not. Map of converted modules: %s", module, b.metrics.convertedModulePathMap)
343 }
344 }
345
346 for _, module := range tc.ExpectedHandcraftedModules {
347 if reason, found := b.metrics.serialized.UnconvertedModules[module]; !found {
348 t.Errorf("expected %s to be marked 'unconverted' by bp2build, but was not found. Full list: %s",
349 module, b.metrics.serialized.UnconvertedModules)
350 } else {
351 if reason.Type != bp2build_metrics_proto.UnconvertedReasonType_DEFINED_IN_BUILD_FILE {
352 t.Errorf("expected %s to be marked 'handcrafted' by bp2build, but was disabled for another reason: %s", module, reason)
353 }
Paul Duffin4c0765a2022-10-29 17:48:00 +0100354 }
355 }
356}
357
358func (b BazelTestResult) CompareBazelTargets(t *testing.T, description string, expectedContents []string, actualTargets BazelTargets) {
Liz Kammer748d7072023-01-25 12:07:43 -0500359 t.Helper()
Paul Duffin4c0765a2022-10-29 17:48:00 +0100360 if actualCount, expectedCount := len(actualTargets), len(expectedContents); actualCount != expectedCount {
Sasha Smundak9d2f1742022-08-04 13:28:38 -0700361 t.Errorf("%s: Expected %d bazel target (%s), got %d (%s)",
Paul Duffin4c0765a2022-10-29 17:48:00 +0100362 description, expectedCount, expectedContents, actualCount, actualTargets)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000363 } else {
Zi Wangfba0a212023-03-07 16:48:19 -0800364 sort.SliceStable(actualTargets, func(i, j int) bool {
365 return actualTargets[i].name < actualTargets[j].name
366 })
367 sort.SliceStable(expectedContents, func(i, j int) bool {
368 return getTargetName(expectedContents[i]) < getTargetName(expectedContents[j])
369 })
Paul Duffin4c0765a2022-10-29 17:48:00 +0100370 for i, actualTarget := range actualTargets {
371 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 +0000372 t.Errorf(
Paul Duffin4c0765a2022-10-29 17:48:00 +0100373 "%s[%d]: Expected generated Bazel target to be `%s`, got `%s`",
374 description, i, w, g)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000375 }
376 }
377 }
378}
379
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800380type nestedProps struct {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500381 Nested_prop *string
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800382}
383
Liz Kammer32a03392021-09-14 11:17:21 -0400384type EmbeddedProps struct {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500385 Embedded_prop *string
Liz Kammer32a03392021-09-14 11:17:21 -0400386}
387
388type OtherEmbeddedProps struct {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500389 Other_embedded_prop *string
Liz Kammer32a03392021-09-14 11:17:21 -0400390}
391
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800392type customProps struct {
Liz Kammer32a03392021-09-14 11:17:21 -0400393 EmbeddedProps
394 *OtherEmbeddedProps
395
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800396 Bool_prop bool
397 Bool_ptr_prop *bool
398 // 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 +0000399 Int_prop int `blueprint:"mutated"`
400 Int64_ptr_prop *int64
401 String_prop string
402 String_literal_prop *string `android:"arch_variant"`
403 String_ptr_prop *string
404 String_list_prop []string
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800405
406 Nested_props nestedProps
407 Nested_props_ptr *nestedProps
Liz Kammer4562a3b2021-04-21 18:15:34 -0400408
Liz Kammer32b77cf2021-08-04 15:17:02 -0400409 Arch_paths []string `android:"path,arch_variant"`
410 Arch_paths_exclude []string `android:"path,arch_variant"`
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400411
412 // Prop used to indicate this conversion should be 1 module -> multiple targets
413 One_to_many_prop *bool
Spandan Das5af0bd32022-09-28 20:43:08 +0000414
Chris Parsons5f1b3c72023-09-28 20:41:03 +0000415 // Prop used to simulate an unsupported property in bp2build conversion. If this
416 // is true, this module should be treated as "unconvertible" via bp2build.
417 Does_not_convert_to_bazel *bool
418
Spandan Das5af0bd32022-09-28 20:43:08 +0000419 Api *string // File describing the APIs of this module
Spandan Das6a448ec2023-04-19 17:36:12 +0000420
421 Test_config_setting *bool // Used to test generation of config_setting targets
Spandan Das3131d672023-08-03 22:33:47 +0000422
423 Dir *string // Dir in which the Bazel Target will be created
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800424}
425
426type customModule struct {
427 android.ModuleBase
Liz Kammerea6666f2021-02-17 10:17:28 -0500428 android.BazelModuleBase
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800429
430 props customProps
431}
432
433// OutputFiles is needed because some instances of this module use dist with a
434// tag property which requires the module implements OutputFileProducer.
435func (m *customModule) OutputFiles(tag string) (android.Paths, error) {
436 return android.PathsForTesting("path" + tag), nil
437}
438
439func (m *customModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
440 // nothing for now.
441}
442
443func customModuleFactoryBase() android.Module {
444 module := &customModule{}
445 module.AddProperties(&module.props)
Liz Kammerea6666f2021-02-17 10:17:28 -0500446 android.InitBazelModule(module)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800447 return module
448}
449
Liz Kammerdfeb1202022-05-13 17:20:20 -0400450func customModuleFactoryHostAndDevice() android.Module {
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800451 m := customModuleFactoryBase()
Liz Kammer4562a3b2021-04-21 18:15:34 -0400452 android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibBoth)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800453 return m
454}
455
Liz Kammerdfeb1202022-05-13 17:20:20 -0400456func customModuleFactoryDeviceSupported() android.Module {
457 m := customModuleFactoryBase()
458 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibBoth)
459 return m
460}
461
462func customModuleFactoryHostSupported() android.Module {
463 m := customModuleFactoryBase()
464 android.InitAndroidArchModule(m, android.HostSupported, android.MultilibBoth)
465 return m
466}
467
468func customModuleFactoryHostAndDeviceDefault() android.Module {
469 m := customModuleFactoryBase()
470 android.InitAndroidArchModule(m, android.HostAndDeviceDefault, android.MultilibBoth)
471 return m
472}
473
474func customModuleFactoryNeitherHostNorDeviceSupported() android.Module {
475 m := customModuleFactoryBase()
476 android.InitAndroidArchModule(m, android.NeitherHostNorDeviceSupported, android.MultilibBoth)
477 return m
478}
479
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800480type testProps struct {
481 Test_prop struct {
482 Test_string_prop string
483 }
484}
485
486type customTestModule struct {
487 android.ModuleBase
488
489 props customProps
490 test_props testProps
491}
492
493func (m *customTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
494 // nothing for now.
495}
496
497func customTestModuleFactoryBase() android.Module {
498 m := &customTestModule{}
499 m.AddProperties(&m.props)
500 m.AddProperties(&m.test_props)
501 return m
502}
503
504func customTestModuleFactory() android.Module {
505 m := customTestModuleFactoryBase()
506 android.InitAndroidModule(m)
507 return m
508}
509
510type customDefaultsModule struct {
511 android.ModuleBase
512 android.DefaultsModuleBase
513}
514
515func customDefaultsModuleFactoryBase() android.DefaultsModule {
516 module := &customDefaultsModule{}
517 module.AddProperties(&customProps{})
518 return module
519}
520
521func customDefaultsModuleFactoryBasic() android.Module {
522 return customDefaultsModuleFactoryBase()
523}
524
525func customDefaultsModuleFactory() android.Module {
526 m := customDefaultsModuleFactoryBase()
527 android.InitDefaultsModule(m)
528 return m
529}
Jingwen Chen73850672020-12-14 08:25:34 -0500530
Liz Kammer32a03392021-09-14 11:17:21 -0400531type EmbeddedAttr struct {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500532 Embedded_attr *string
Liz Kammer32a03392021-09-14 11:17:21 -0400533}
534
535type OtherEmbeddedAttr struct {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500536 Other_embedded_attr *string
Liz Kammer32a03392021-09-14 11:17:21 -0400537}
538
Jingwen Chen73850672020-12-14 08:25:34 -0500539type customBazelModuleAttributes struct {
Liz Kammer32a03392021-09-14 11:17:21 -0400540 EmbeddedAttr
541 *OtherEmbeddedAttr
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000542 String_literal_prop bazel.StringAttribute
543 String_ptr_prop *string
544 String_list_prop []string
545 Arch_paths bazel.LabelListAttribute
Spandan Das5af0bd32022-09-28 20:43:08 +0000546 Api bazel.LabelAttribute
Jingwen Chen73850672020-12-14 08:25:34 -0500547}
548
Spandan Das3131d672023-08-03 22:33:47 +0000549func (m *customModule) dir() *string {
550 return m.props.Dir
551}
552
Chris Parsons637458d2023-09-19 20:09:00 +0000553func (m *customModule) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) {
Chris Parsons5f1b3c72023-09-28 20:41:03 +0000554 if p := m.props.Does_not_convert_to_bazel; p != nil && *p {
555 ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_PROPERTY_UNSUPPORTED, "")
556 return
557 }
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400558 if p := m.props.One_to_many_prop; p != nil && *p {
559 customBp2buildOneToMany(ctx, m)
560 return
561 }
Liz Kammer4562a3b2021-04-21 18:15:34 -0400562
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000563 paths := bazel.LabelListAttribute{}
564 strAttr := bazel.StringAttribute{}
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400565 for axis, configToProps := range m.GetArchVariantProperties(ctx, &customProps{}) {
566 for config, props := range configToProps {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000567 if custProps, ok := props.(*customProps); ok {
568 if custProps.Arch_paths != nil {
569 paths.SetSelectValue(axis, config, android.BazelLabelForModuleSrcExcludes(ctx, custProps.Arch_paths, custProps.Arch_paths_exclude))
570 }
571 if custProps.String_literal_prop != nil {
572 strAttr.SetSelectValue(axis, config, custProps.String_literal_prop)
573 }
Liz Kammer4562a3b2021-04-21 18:15:34 -0400574 }
575 }
Jingwen Chen73850672020-12-14 08:25:34 -0500576 }
Liz Kammer9e2a5a72023-09-19 08:41:14 -0400577 productVariableProps, errs := android.ProductVariableProperties(ctx, ctx.Module())
578 for _, err := range errs {
579 ctx.ModuleErrorf("ProductVariableProperties error: %s", err)
580 }
Liz Kammer9d2d4102022-12-21 14:51:37 -0500581 if props, ok := productVariableProps["String_literal_prop"]; ok {
582 for c, p := range props {
583 if val, ok := p.(*string); ok {
584 strAttr.SetSelectValue(c.ConfigurationAxis(), c.SelectKey(), val)
585 }
586 }
587 }
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400588
589 paths.ResolveExcludes()
590
591 attrs := &customBazelModuleAttributes{
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000592 String_literal_prop: strAttr,
593 String_ptr_prop: m.props.String_ptr_prop,
594 String_list_prop: m.props.String_list_prop,
595 Arch_paths: paths,
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400596 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000597
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400598 attrs.Embedded_attr = m.props.Embedded_prop
599 if m.props.OtherEmbeddedProps != nil {
600 attrs.OtherEmbeddedAttr = &OtherEmbeddedAttr{Other_embedded_attr: m.props.OtherEmbeddedProps.Other_embedded_prop}
601 }
602
603 props := bazel.BazelTargetModuleProperties{
604 Rule_class: "custom",
605 }
606
Spandan Das3131d672023-08-03 22:33:47 +0000607 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name(), Dir: m.dir()}, attrs)
Spandan Das6a448ec2023-04-19 17:36:12 +0000608
609 if proptools.Bool(m.props.Test_config_setting) {
610 m.createConfigSetting(ctx)
611 }
612
613}
614
Chris Parsons637458d2023-09-19 20:09:00 +0000615func (m *customModule) createConfigSetting(ctx android.Bp2buildMutatorContext) {
Spandan Das6a448ec2023-04-19 17:36:12 +0000616 csa := bazel.ConfigSettingAttributes{
617 Flag_values: bazel.StringMapAttribute{
618 "//build/bazel/rules/my_string_setting": m.Name(),
619 },
620 }
621 ca := android.CommonAttributes{
622 Name: m.Name() + "_config_setting",
623 }
624 ctx.CreateBazelConfigSetting(
625 csa,
626 ca,
627 ctx.ModuleDir(),
628 )
Jingwen Chen73850672020-12-14 08:25:34 -0500629}
Jingwen Chen40067de2021-01-26 21:58:43 -0500630
631// A bp2build mutator that uses load statements and creates a 1:M mapping from
632// module to target.
Chris Parsons637458d2023-09-19 20:09:00 +0000633func customBp2buildOneToMany(ctx android.Bp2buildMutatorContext, m *customModule) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500634
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400635 baseName := m.Name()
636 attrs := &customBazelModuleAttributes{}
Jingwen Chen1fd14692021-02-05 03:01:50 -0500637
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400638 myLibraryProps := bazel.BazelTargetModuleProperties{
639 Rule_class: "my_library",
640 Bzl_load_location: "//build/bazel/rules:rules.bzl",
Jingwen Chen40067de2021-01-26 21:58:43 -0500641 }
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400642 ctx.CreateBazelTargetModule(myLibraryProps, android.CommonAttributes{Name: baseName}, attrs)
643
644 protoLibraryProps := bazel.BazelTargetModuleProperties{
645 Rule_class: "proto_library",
646 Bzl_load_location: "//build/bazel/rules:proto.bzl",
647 }
648 ctx.CreateBazelTargetModule(protoLibraryProps, android.CommonAttributes{Name: baseName + "_proto_library_deps"}, attrs)
649
650 myProtoLibraryProps := bazel.BazelTargetModuleProperties{
651 Rule_class: "my_proto_library",
652 Bzl_load_location: "//build/bazel/rules:proto.bzl",
653 }
654 ctx.CreateBazelTargetModule(myProtoLibraryProps, android.CommonAttributes{Name: baseName + "_my_proto_library_deps"}, attrs)
Jingwen Chen40067de2021-01-26 21:58:43 -0500655}
Jingwen Chenba369ad2021-02-22 10:19:34 -0500656
657// Helper method for tests to easily access the targets in a dir.
Liz Kammer6eff3232021-08-26 08:37:59 -0400658func generateBazelTargetsForDir(codegenCtx *CodegenContext, dir string) (BazelTargets, []error) {
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400659 // TODO: Set generateFilegroups to true and/or remove the generateFilegroups argument completely
Liz Kammer6eff3232021-08-26 08:37:59 -0400660 res, err := GenerateBazelTargets(codegenCtx, false)
Alix94e26032022-08-16 20:37:33 +0000661 if err != nil {
662 return BazelTargets{}, err
663 }
Liz Kammer6eff3232021-08-26 08:37:59 -0400664 return res.buildFileToTargets[dir], err
Jingwen Chenba369ad2021-02-22 10:19:34 -0500665}
Liz Kammer32b77cf2021-08-04 15:17:02 -0400666
667func registerCustomModuleForBp2buildConversion(ctx *android.TestContext) {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400668 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
Liz Kammer32b77cf2021-08-04 15:17:02 -0400669 ctx.RegisterForBazelConversion()
670}
Liz Kammer7a210ac2021-09-22 15:52:58 -0400671
Chris Parsonscd209032023-09-19 01:12:48 +0000672func simpleModule(typ, name string) string {
Liz Kammer7a210ac2021-09-22 15:52:58 -0400673 return fmt.Sprintf(`
674%s {
675 name: "%s",
Liz Kammer7a210ac2021-09-22 15:52:58 -0400676}`, typ, name)
677}
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500678
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000679type AttrNameToString map[string]string
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500680
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000681func (a AttrNameToString) clone() AttrNameToString {
682 newAttrs := make(AttrNameToString, len(a))
Liz Kammerdfeb1202022-05-13 17:20:20 -0400683 for k, v := range a {
684 newAttrs[k] = v
685 }
686 return newAttrs
687}
688
689// makeBazelTargetNoRestrictions returns bazel target build file definition that can be host or
690// device specific, or independent of host/device.
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000691func makeBazelTargetHostOrDevice(typ, name string, attrs AttrNameToString, hod android.HostOrDeviceSupported) string {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400692 if _, ok := attrs["target_compatible_with"]; !ok {
693 switch hod {
694 case android.HostSupported:
695 attrs["target_compatible_with"] = `select({
696 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
697 "//conditions:default": [],
698 })`
699 case android.DeviceSupported:
700 attrs["target_compatible_with"] = `["//build/bazel/platforms/os:android"]`
701 }
702 }
703
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500704 attrStrings := make([]string, 0, len(attrs)+1)
Sasha Smundakfb589492022-08-04 11:13:27 -0700705 if name != "" {
706 attrStrings = append(attrStrings, fmt.Sprintf(` name = "%s",`, name))
707 }
Cole Faust18994c72023-02-28 16:02:16 -0800708 for _, k := range android.SortedKeys(attrs) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500709 attrStrings = append(attrStrings, fmt.Sprintf(" %s = %s,", k, attrs[k]))
710 }
711 return fmt.Sprintf(`%s(
712%s
713)`, typ, strings.Join(attrStrings, "\n"))
714}
Liz Kammerdfeb1202022-05-13 17:20:20 -0400715
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000716// MakeBazelTargetNoRestrictions returns bazel target build file definition that does not add a
Liz Kammerdfeb1202022-05-13 17:20:20 -0400717// target_compatible_with. This is useful for module types like filegroup and genrule that arch not
718// arch variant
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000719func MakeBazelTargetNoRestrictions(typ, name string, attrs AttrNameToString) string {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400720 return makeBazelTargetHostOrDevice(typ, name, attrs, android.HostAndDeviceDefault)
721}
722
723// makeBazelTargetNoRestrictions returns bazel target build file definition that is device specific
724// as this is the most common default in Soong.
Alixe06d75b2022-08-31 18:28:19 +0000725func MakeBazelTarget(typ, name string, attrs AttrNameToString) string {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400726 return makeBazelTargetHostOrDevice(typ, name, attrs, android.DeviceSupported)
727}
Sasha Smundak9d2f1742022-08-04 13:28:38 -0700728
729type ExpectedRuleTarget struct {
730 Rule string
731 Name string
732 Attrs AttrNameToString
733 Hod android.HostOrDeviceSupported
734}
735
736func (ebr ExpectedRuleTarget) String() string {
737 return makeBazelTargetHostOrDevice(ebr.Rule, ebr.Name, ebr.Attrs, ebr.Hod)
738}
Trevor Radcliffe087af542022-09-16 15:36:10 +0000739
740func makeCcStubSuiteTargets(name string, attrs AttrNameToString) string {
741 if _, hasStubs := attrs["stubs_symbol_file"]; !hasStubs {
742 return ""
743 }
744 STUB_SUITE_ATTRS := map[string]string{
Spandan Das04f9f4c2023-09-13 23:59:05 +0000745 "api_surface": "api_surface",
Sam Delmerico5f906492023-03-15 18:06:18 -0400746 "stubs_symbol_file": "symbol_file",
747 "stubs_versions": "versions",
748 "soname": "soname",
749 "source_library_label": "source_library_label",
Trevor Radcliffe087af542022-09-16 15:36:10 +0000750 }
751
752 stubSuiteAttrs := AttrNameToString{}
753 for key, _ := range attrs {
754 if _, stubSuiteAttr := STUB_SUITE_ATTRS[key]; stubSuiteAttr {
755 stubSuiteAttrs[STUB_SUITE_ATTRS[key]] = attrs[key]
Sam Delmerico5f906492023-03-15 18:06:18 -0400756 } else {
757 panic(fmt.Sprintf("unused cc_stub_suite attr %q\n", key))
Trevor Radcliffe087af542022-09-16 15:36:10 +0000758 }
759 }
760 return MakeBazelTarget("cc_stub_suite", name+"_stub_libs", stubSuiteAttrs)
761}
Alix341484b2022-10-31 19:08:18 +0000762
763func MakeNeverlinkDuplicateTarget(moduleType string, name string) string {
Liz Kammer02914402023-08-07 13:38:18 -0400764 return MakeNeverlinkDuplicateTargetWithAttrs(moduleType, name, AttrNameToString{
765 "sdk_version": `"current"`, // use as default
766 })
Romain Jobredeaux2eef2e12023-02-24 12:07:08 -0500767}
768
769func MakeNeverlinkDuplicateTargetWithAttrs(moduleType string, name string, extraAttrs AttrNameToString) string {
770 attrs := extraAttrs
771 attrs["neverlink"] = `True`
772 attrs["exports"] = `[":` + name + `"]`
773 return MakeBazelTarget(moduleType, name+"-neverlink", attrs)
Alix341484b2022-10-31 19:08:18 +0000774}
Zi Wangfba0a212023-03-07 16:48:19 -0800775
776func getTargetName(targetContent string) string {
777 data := strings.Split(targetContent, "name = \"")
778 if len(data) < 2 {
779 return ""
780 } else {
781 endIndex := strings.Index(data[1], "\"")
782 return data[1][:endIndex]
783 }
784}