blob: d26b346ddeb74e3cd77ef47c77a079f4134d2695 [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
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000127}
128
Chris Parsons2173b5f2023-09-25 19:01:40 +0000129func RunBp2BuildTestCaseExtraContext(t *testing.T, registerModuleTypes func(ctx android.RegistrationContext), modifyContext func(ctx *android.TestContext), tc Bp2buildTestCase) {
130 t.Helper()
131 bp2buildSetup := android.GroupFixturePreparers(
132 android.FixtureRegisterWithContext(registerModuleTypes),
133 android.FixtureModifyContext(modifyContext),
134 SetBp2BuildTestRunner,
135 )
136 runBp2BuildTestCaseWithSetup(t, bp2buildSetup, tc)
137}
138
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000139func RunBp2BuildTestCase(t *testing.T, registerModuleTypes func(ctx android.RegistrationContext), tc Bp2buildTestCase) {
Liz Kammerffc17e42022-11-23 09:42:05 -0500140 t.Helper()
Paul Duffin4c0765a2022-10-29 17:48:00 +0100141 bp2buildSetup := android.GroupFixturePreparers(
142 android.FixtureRegisterWithContext(registerModuleTypes),
143 SetBp2BuildTestRunner,
144 )
Spandan Das5af0bd32022-09-28 20:43:08 +0000145 runBp2BuildTestCaseWithSetup(t, bp2buildSetup, tc)
146}
147
Paul Duffin4c0765a2022-10-29 17:48:00 +0100148func 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 +0000149 t.Helper()
Chris Parsonscd209032023-09-19 01:12:48 +0000150 if tc.Filesystem == nil {
151 tc.Filesystem = map[string]string{}
152 }
Chris Parsons5011e612023-09-13 23:33:20 +0000153 checkDir := "."
154 if tc.Dir != "" {
155 checkDir = tc.Dir
156 }
157 keepExistingBuildDirs := tc.KeepBuildFileForDirs
158 buildFilesToParse := []string{}
Chris Parsonscd209032023-09-19 01:12:48 +0000159
160 if len(tc.StubbedBuildDefinitions) > 0 {
161 for _, buildDef := range tc.StubbedBuildDefinitions {
162 globalLabelMatch := labelRegex.FindStringSubmatch(buildDef)
163 var dir, targetName string
164 if len(globalLabelMatch) > 0 {
165 dir = globalLabelMatch[1]
166 targetName = globalLabelMatch[2]
167 } else {
168 if !simpleModuleNameRegex.MatchString(buildDef) {
169 t.Errorf("Stubbed build definition '%s' must be either a simple module name or of global target syntax (//foo/bar:baz).", buildDef)
170 return
171 }
172 dir = "."
173 targetName = buildDef
174 }
175 buildFilePath := filepath.Join(dir, "BUILD")
176 tc.Filesystem[buildFilePath] +=
177 MakeBazelTarget(
178 "bp2build_test_stub",
179 targetName,
180 AttrNameToString{})
181 keepExistingBuildDirs = append(keepExistingBuildDirs, dir)
182 buildFilesToParse = append(buildFilesToParse, buildFilePath)
183 }
184 }
185 if len(tc.AlreadyExistingBuildContents) > 0 {
186 buildFilePath := filepath.Join(checkDir, "BUILD")
187 tc.Filesystem[buildFilePath] += tc.AlreadyExistingBuildContents
188 keepExistingBuildDirs = append(keepExistingBuildDirs, checkDir)
189 buildFilesToParse = append(buildFilesToParse, buildFilePath)
190 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000191 filesystem := make(map[string][]byte)
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000192 for f, content := range tc.Filesystem {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000193 filesystem[f] = []byte(content)
194 }
Paul Duffin4c0765a2022-10-29 17:48:00 +0100195 preparers := []android.FixturePreparer{
196 extraPreparer,
197 android.FixtureMergeMockFs(filesystem),
198 android.FixtureWithRootAndroidBp(tc.Blueprint),
199 android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) {
200 ctx.RegisterModuleType(tc.ModuleTypeUnderTest, tc.ModuleTypeUnderTestFactory)
201 }),
Chris Parsons5011e612023-09-13 23:33:20 +0000202 android.FixtureModifyContextWithMockFs(func(ctx *android.TestContext) {
Paul Duffin4c0765a2022-10-29 17:48:00 +0100203 // A default configuration for tests to not have to specify bp2build_available on top level
204 // targets.
205 bp2buildConfig := android.NewBp2BuildAllowlist().SetDefaultConfig(
206 allowlists.Bp2BuildConfig{
207 android.Bp2BuildTopLevel: allowlists.Bp2BuildDefaultTrueRecursively,
208 },
209 )
Chris Parsons5011e612023-09-13 23:33:20 +0000210 for _, f := range keepExistingBuildDirs {
Paul Duffin4c0765a2022-10-29 17:48:00 +0100211 bp2buildConfig.SetKeepExistingBuildFile(map[string]bool{
212 f: /*recursive=*/ false,
213 })
214 }
215 ctx.RegisterBp2BuildConfig(bp2buildConfig)
Chris Parsons39a16972023-06-08 14:28:51 +0000216 // This setting is added to bp2build invocations. It prevents bp2build
217 // from cloning modules to their original state after mutators run. This
218 // would lose some data intentionally set by these mutators.
219 ctx.SkipCloneModulesAfterMutators = true
Chris Parsons5011e612023-09-13 23:33:20 +0000220 err := ctx.RegisterExistingBazelTargets(".", buildFilesToParse)
221 if err != nil {
222 t.Errorf("error parsing build files in test setup: %s", err)
223 }
Paul Duffin4c0765a2022-10-29 17:48:00 +0100224 }),
225 android.FixtureModifyEnv(func(env map[string]string) {
226 if tc.UnconvertedDepsMode == errorModulesUnconvertedDeps {
227 env["BP2BUILD_ERROR_UNCONVERTED"] = "true"
228 }
229 }),
Jingwen Chen5146ac02021-09-02 11:44:42 +0000230 }
231
Paul Duffin4c0765a2022-10-29 17:48:00 +0100232 preparer := android.GroupFixturePreparers(preparers...)
233 if tc.ExpectedErr != nil {
234 pattern := "\\Q" + tc.ExpectedErr.Error() + "\\E"
235 preparer = preparer.ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(pattern))
236 }
237 result := preparer.RunTestWithCustomResult(t).(*BazelTestResult)
238 if len(result.Errs) > 0 {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000239 return
240 }
241
Paul Duffin4c0765a2022-10-29 17:48:00 +0100242 expectedTargets := map[string][]string{
243 checkDir: tc.ExpectedBazelTargets,
Liz Kammer6eff3232021-08-26 08:37:59 -0400244 }
Paul Duffin4c0765a2022-10-29 17:48:00 +0100245
Chris Parsons2173b5f2023-09-25 19:01:40 +0000246 result.CompareAllBazelTargets(t, tc, expectedTargets, true)
Paul Duffin4c0765a2022-10-29 17:48:00 +0100247}
248
249// SetBp2BuildTestRunner customizes the test fixture mechanism to run tests in Bp2Build mode.
Chris Parsons73f411b2023-06-20 21:46:57 +0000250var SetBp2BuildTestRunner = android.FixtureSetTestRunner(&bazelTestRunner{})
Paul Duffin4c0765a2022-10-29 17:48:00 +0100251
Chris Parsons73f411b2023-06-20 21:46:57 +0000252// bazelTestRunner customizes the test fixture mechanism to run tests of the bp2build build mode.
253type bazelTestRunner struct{}
Paul Duffin4c0765a2022-10-29 17:48:00 +0100254
255func (b *bazelTestRunner) FinalPreparer(result *android.TestResult) android.CustomTestResult {
256 ctx := result.TestContext
Chris Parsons73f411b2023-06-20 21:46:57 +0000257 ctx.RegisterForBazelConversion()
Paul Duffin4c0765a2022-10-29 17:48:00 +0100258
259 return &BazelTestResult{TestResult: result}
260}
261
262func (b *bazelTestRunner) PostParseProcessor(result android.CustomTestResult) {
263 bazelResult := result.(*BazelTestResult)
264 ctx := bazelResult.TestContext
265 config := bazelResult.Config
266 _, errs := ctx.ResolveDependencies(config)
267 if bazelResult.CollateErrs(errs) {
268 return
269 }
270
Chris Parsons73f411b2023-06-20 21:46:57 +0000271 codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "")
Paul Duffin4c0765a2022-10-29 17:48:00 +0100272 res, errs := GenerateBazelTargets(codegenCtx, false)
273 if bazelResult.CollateErrs(errs) {
274 return
275 }
276
277 // Store additional data for access by tests.
278 bazelResult.conversionResults = res
279}
280
281// BazelTestResult is a wrapper around android.TestResult to provide type safe access to the bazel
282// specific data stored by the bazelTestRunner.
283type BazelTestResult struct {
284 *android.TestResult
285
286 // The result returned by the GenerateBazelTargets function.
287 conversionResults
288}
289
290// CompareAllBazelTargets compares the BazelTargets produced by the test for all the directories
291// with the supplied set of expected targets.
292//
293// If ignoreUnexpected=false then this enforces an exact match where every BazelTarget produced must
294// have a corresponding expected BazelTarget.
295//
296// If ignoreUnexpected=true then it will ignore directories for which there are no expected targets.
Chris Parsons2173b5f2023-09-25 19:01:40 +0000297func (b BazelTestResult) CompareAllBazelTargets(t *testing.T, tc Bp2buildTestCase, expectedTargets map[string][]string, ignoreUnexpected bool) {
Liz Kammer2b3f56e2023-03-23 11:51:49 -0400298 t.Helper()
Paul Duffin4c0765a2022-10-29 17:48:00 +0100299 actualTargets := b.buildFileToTargets
300
301 // Generate the sorted set of directories to check.
Cole Faust18994c72023-02-28 16:02:16 -0800302 dirsToCheck := android.SortedKeys(expectedTargets)
Paul Duffin4c0765a2022-10-29 17:48:00 +0100303 if !ignoreUnexpected {
304 // This needs to perform an exact match so add the directories in which targets were
305 // produced to the list of directories to check.
Cole Faust18994c72023-02-28 16:02:16 -0800306 dirsToCheck = append(dirsToCheck, android.SortedKeys(actualTargets)...)
Paul Duffin4c0765a2022-10-29 17:48:00 +0100307 dirsToCheck = android.SortedUniqueStrings(dirsToCheck)
308 }
309
310 for _, dir := range dirsToCheck {
311 expected := expectedTargets[dir]
312 actual := actualTargets[dir]
313
314 if expected == nil {
315 if actual != nil {
316 t.Errorf("did not expect any bazel modules in %q but found %d", dir, len(actual))
317 }
318 } else if actual == nil {
319 expectedCount := len(expected)
320 if expectedCount > 0 {
321 t.Errorf("expected %d bazel modules in %q but did not find any", expectedCount, dir)
322 }
323 } else {
Chris Parsons2173b5f2023-09-25 19:01:40 +0000324 b.CompareBazelTargets(t, tc.Description, expected, actual)
325 }
326 }
327
328 for _, module := range tc.ExpectedConvertedModules {
329 if _, found := b.metrics.convertedModulePathMap[module]; !found {
330 t.Errorf("expected %s to be generated by bp2build, but was not. Map of converted modules: %s", module, b.metrics.convertedModulePathMap)
331 }
332 }
333
334 for _, module := range tc.ExpectedHandcraftedModules {
335 if reason, found := b.metrics.serialized.UnconvertedModules[module]; !found {
336 t.Errorf("expected %s to be marked 'unconverted' by bp2build, but was not found. Full list: %s",
337 module, b.metrics.serialized.UnconvertedModules)
338 } else {
339 if reason.Type != bp2build_metrics_proto.UnconvertedReasonType_DEFINED_IN_BUILD_FILE {
340 t.Errorf("expected %s to be marked 'handcrafted' by bp2build, but was disabled for another reason: %s", module, reason)
341 }
Paul Duffin4c0765a2022-10-29 17:48:00 +0100342 }
343 }
344}
345
346func (b BazelTestResult) CompareBazelTargets(t *testing.T, description string, expectedContents []string, actualTargets BazelTargets) {
Liz Kammer748d7072023-01-25 12:07:43 -0500347 t.Helper()
Paul Duffin4c0765a2022-10-29 17:48:00 +0100348 if actualCount, expectedCount := len(actualTargets), len(expectedContents); actualCount != expectedCount {
Sasha Smundak9d2f1742022-08-04 13:28:38 -0700349 t.Errorf("%s: Expected %d bazel target (%s), got %d (%s)",
Paul Duffin4c0765a2022-10-29 17:48:00 +0100350 description, expectedCount, expectedContents, actualCount, actualTargets)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000351 } else {
Zi Wangfba0a212023-03-07 16:48:19 -0800352 sort.SliceStable(actualTargets, func(i, j int) bool {
353 return actualTargets[i].name < actualTargets[j].name
354 })
355 sort.SliceStable(expectedContents, func(i, j int) bool {
356 return getTargetName(expectedContents[i]) < getTargetName(expectedContents[j])
357 })
Paul Duffin4c0765a2022-10-29 17:48:00 +0100358 for i, actualTarget := range actualTargets {
359 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 +0000360 t.Errorf(
Paul Duffin4c0765a2022-10-29 17:48:00 +0100361 "%s[%d]: Expected generated Bazel target to be `%s`, got `%s`",
362 description, i, w, g)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000363 }
364 }
365 }
366}
367
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800368type nestedProps struct {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500369 Nested_prop *string
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800370}
371
Liz Kammer32a03392021-09-14 11:17:21 -0400372type EmbeddedProps struct {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500373 Embedded_prop *string
Liz Kammer32a03392021-09-14 11:17:21 -0400374}
375
376type OtherEmbeddedProps struct {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500377 Other_embedded_prop *string
Liz Kammer32a03392021-09-14 11:17:21 -0400378}
379
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800380type customProps struct {
Liz Kammer32a03392021-09-14 11:17:21 -0400381 EmbeddedProps
382 *OtherEmbeddedProps
383
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800384 Bool_prop bool
385 Bool_ptr_prop *bool
386 // 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 +0000387 Int_prop int `blueprint:"mutated"`
388 Int64_ptr_prop *int64
389 String_prop string
390 String_literal_prop *string `android:"arch_variant"`
391 String_ptr_prop *string
392 String_list_prop []string
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800393
394 Nested_props nestedProps
395 Nested_props_ptr *nestedProps
Liz Kammer4562a3b2021-04-21 18:15:34 -0400396
Liz Kammer32b77cf2021-08-04 15:17:02 -0400397 Arch_paths []string `android:"path,arch_variant"`
398 Arch_paths_exclude []string `android:"path,arch_variant"`
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400399
400 // Prop used to indicate this conversion should be 1 module -> multiple targets
401 One_to_many_prop *bool
Spandan Das5af0bd32022-09-28 20:43:08 +0000402
403 Api *string // File describing the APIs of this module
Spandan Das6a448ec2023-04-19 17:36:12 +0000404
405 Test_config_setting *bool // Used to test generation of config_setting targets
Spandan Das3131d672023-08-03 22:33:47 +0000406
407 Dir *string // Dir in which the Bazel Target will be created
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800408}
409
410type customModule struct {
411 android.ModuleBase
Liz Kammerea6666f2021-02-17 10:17:28 -0500412 android.BazelModuleBase
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800413
414 props customProps
415}
416
417// OutputFiles is needed because some instances of this module use dist with a
418// tag property which requires the module implements OutputFileProducer.
419func (m *customModule) OutputFiles(tag string) (android.Paths, error) {
420 return android.PathsForTesting("path" + tag), nil
421}
422
423func (m *customModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
424 // nothing for now.
425}
426
427func customModuleFactoryBase() android.Module {
428 module := &customModule{}
429 module.AddProperties(&module.props)
Liz Kammerea6666f2021-02-17 10:17:28 -0500430 android.InitBazelModule(module)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800431 return module
432}
433
Liz Kammerdfeb1202022-05-13 17:20:20 -0400434func customModuleFactoryHostAndDevice() android.Module {
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800435 m := customModuleFactoryBase()
Liz Kammer4562a3b2021-04-21 18:15:34 -0400436 android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibBoth)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800437 return m
438}
439
Liz Kammerdfeb1202022-05-13 17:20:20 -0400440func customModuleFactoryDeviceSupported() android.Module {
441 m := customModuleFactoryBase()
442 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibBoth)
443 return m
444}
445
446func customModuleFactoryHostSupported() android.Module {
447 m := customModuleFactoryBase()
448 android.InitAndroidArchModule(m, android.HostSupported, android.MultilibBoth)
449 return m
450}
451
452func customModuleFactoryHostAndDeviceDefault() android.Module {
453 m := customModuleFactoryBase()
454 android.InitAndroidArchModule(m, android.HostAndDeviceDefault, android.MultilibBoth)
455 return m
456}
457
458func customModuleFactoryNeitherHostNorDeviceSupported() android.Module {
459 m := customModuleFactoryBase()
460 android.InitAndroidArchModule(m, android.NeitherHostNorDeviceSupported, android.MultilibBoth)
461 return m
462}
463
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800464type testProps struct {
465 Test_prop struct {
466 Test_string_prop string
467 }
468}
469
470type customTestModule struct {
471 android.ModuleBase
472
473 props customProps
474 test_props testProps
475}
476
477func (m *customTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
478 // nothing for now.
479}
480
481func customTestModuleFactoryBase() android.Module {
482 m := &customTestModule{}
483 m.AddProperties(&m.props)
484 m.AddProperties(&m.test_props)
485 return m
486}
487
488func customTestModuleFactory() android.Module {
489 m := customTestModuleFactoryBase()
490 android.InitAndroidModule(m)
491 return m
492}
493
494type customDefaultsModule struct {
495 android.ModuleBase
496 android.DefaultsModuleBase
497}
498
499func customDefaultsModuleFactoryBase() android.DefaultsModule {
500 module := &customDefaultsModule{}
501 module.AddProperties(&customProps{})
502 return module
503}
504
505func customDefaultsModuleFactoryBasic() android.Module {
506 return customDefaultsModuleFactoryBase()
507}
508
509func customDefaultsModuleFactory() android.Module {
510 m := customDefaultsModuleFactoryBase()
511 android.InitDefaultsModule(m)
512 return m
513}
Jingwen Chen73850672020-12-14 08:25:34 -0500514
Liz Kammer32a03392021-09-14 11:17:21 -0400515type EmbeddedAttr struct {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500516 Embedded_attr *string
Liz Kammer32a03392021-09-14 11:17:21 -0400517}
518
519type OtherEmbeddedAttr struct {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500520 Other_embedded_attr *string
Liz Kammer32a03392021-09-14 11:17:21 -0400521}
522
Jingwen Chen73850672020-12-14 08:25:34 -0500523type customBazelModuleAttributes struct {
Liz Kammer32a03392021-09-14 11:17:21 -0400524 EmbeddedAttr
525 *OtherEmbeddedAttr
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000526 String_literal_prop bazel.StringAttribute
527 String_ptr_prop *string
528 String_list_prop []string
529 Arch_paths bazel.LabelListAttribute
Spandan Das5af0bd32022-09-28 20:43:08 +0000530 Api bazel.LabelAttribute
Jingwen Chen73850672020-12-14 08:25:34 -0500531}
532
Spandan Das3131d672023-08-03 22:33:47 +0000533func (m *customModule) dir() *string {
534 return m.props.Dir
535}
536
Chris Parsons637458d2023-09-19 20:09:00 +0000537func (m *customModule) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400538 if p := m.props.One_to_many_prop; p != nil && *p {
539 customBp2buildOneToMany(ctx, m)
540 return
541 }
Liz Kammer4562a3b2021-04-21 18:15:34 -0400542
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000543 paths := bazel.LabelListAttribute{}
544 strAttr := bazel.StringAttribute{}
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400545 for axis, configToProps := range m.GetArchVariantProperties(ctx, &customProps{}) {
546 for config, props := range configToProps {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000547 if custProps, ok := props.(*customProps); ok {
548 if custProps.Arch_paths != nil {
549 paths.SetSelectValue(axis, config, android.BazelLabelForModuleSrcExcludes(ctx, custProps.Arch_paths, custProps.Arch_paths_exclude))
550 }
551 if custProps.String_literal_prop != nil {
552 strAttr.SetSelectValue(axis, config, custProps.String_literal_prop)
553 }
Liz Kammer4562a3b2021-04-21 18:15:34 -0400554 }
555 }
Jingwen Chen73850672020-12-14 08:25:34 -0500556 }
Liz Kammer9e2a5a72023-09-19 08:41:14 -0400557 productVariableProps, errs := android.ProductVariableProperties(ctx, ctx.Module())
558 for _, err := range errs {
559 ctx.ModuleErrorf("ProductVariableProperties error: %s", err)
560 }
Liz Kammer9d2d4102022-12-21 14:51:37 -0500561 if props, ok := productVariableProps["String_literal_prop"]; ok {
562 for c, p := range props {
563 if val, ok := p.(*string); ok {
564 strAttr.SetSelectValue(c.ConfigurationAxis(), c.SelectKey(), val)
565 }
566 }
567 }
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400568
569 paths.ResolveExcludes()
570
571 attrs := &customBazelModuleAttributes{
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000572 String_literal_prop: strAttr,
573 String_ptr_prop: m.props.String_ptr_prop,
574 String_list_prop: m.props.String_list_prop,
575 Arch_paths: paths,
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400576 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000577
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400578 attrs.Embedded_attr = m.props.Embedded_prop
579 if m.props.OtherEmbeddedProps != nil {
580 attrs.OtherEmbeddedAttr = &OtherEmbeddedAttr{Other_embedded_attr: m.props.OtherEmbeddedProps.Other_embedded_prop}
581 }
582
583 props := bazel.BazelTargetModuleProperties{
584 Rule_class: "custom",
585 }
586
Spandan Das3131d672023-08-03 22:33:47 +0000587 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name(), Dir: m.dir()}, attrs)
Spandan Das6a448ec2023-04-19 17:36:12 +0000588
589 if proptools.Bool(m.props.Test_config_setting) {
590 m.createConfigSetting(ctx)
591 }
592
593}
594
Chris Parsons637458d2023-09-19 20:09:00 +0000595func (m *customModule) createConfigSetting(ctx android.Bp2buildMutatorContext) {
Spandan Das6a448ec2023-04-19 17:36:12 +0000596 csa := bazel.ConfigSettingAttributes{
597 Flag_values: bazel.StringMapAttribute{
598 "//build/bazel/rules/my_string_setting": m.Name(),
599 },
600 }
601 ca := android.CommonAttributes{
602 Name: m.Name() + "_config_setting",
603 }
604 ctx.CreateBazelConfigSetting(
605 csa,
606 ca,
607 ctx.ModuleDir(),
608 )
Jingwen Chen73850672020-12-14 08:25:34 -0500609}
Jingwen Chen40067de2021-01-26 21:58:43 -0500610
611// A bp2build mutator that uses load statements and creates a 1:M mapping from
612// module to target.
Chris Parsons637458d2023-09-19 20:09:00 +0000613func customBp2buildOneToMany(ctx android.Bp2buildMutatorContext, m *customModule) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500614
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400615 baseName := m.Name()
616 attrs := &customBazelModuleAttributes{}
Jingwen Chen1fd14692021-02-05 03:01:50 -0500617
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400618 myLibraryProps := bazel.BazelTargetModuleProperties{
619 Rule_class: "my_library",
620 Bzl_load_location: "//build/bazel/rules:rules.bzl",
Jingwen Chen40067de2021-01-26 21:58:43 -0500621 }
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400622 ctx.CreateBazelTargetModule(myLibraryProps, android.CommonAttributes{Name: baseName}, attrs)
623
624 protoLibraryProps := bazel.BazelTargetModuleProperties{
625 Rule_class: "proto_library",
626 Bzl_load_location: "//build/bazel/rules:proto.bzl",
627 }
628 ctx.CreateBazelTargetModule(protoLibraryProps, android.CommonAttributes{Name: baseName + "_proto_library_deps"}, attrs)
629
630 myProtoLibraryProps := bazel.BazelTargetModuleProperties{
631 Rule_class: "my_proto_library",
632 Bzl_load_location: "//build/bazel/rules:proto.bzl",
633 }
634 ctx.CreateBazelTargetModule(myProtoLibraryProps, android.CommonAttributes{Name: baseName + "_my_proto_library_deps"}, attrs)
Jingwen Chen40067de2021-01-26 21:58:43 -0500635}
Jingwen Chenba369ad2021-02-22 10:19:34 -0500636
637// Helper method for tests to easily access the targets in a dir.
Liz Kammer6eff3232021-08-26 08:37:59 -0400638func generateBazelTargetsForDir(codegenCtx *CodegenContext, dir string) (BazelTargets, []error) {
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400639 // TODO: Set generateFilegroups to true and/or remove the generateFilegroups argument completely
Liz Kammer6eff3232021-08-26 08:37:59 -0400640 res, err := GenerateBazelTargets(codegenCtx, false)
Alix94e26032022-08-16 20:37:33 +0000641 if err != nil {
642 return BazelTargets{}, err
643 }
Liz Kammer6eff3232021-08-26 08:37:59 -0400644 return res.buildFileToTargets[dir], err
Jingwen Chenba369ad2021-02-22 10:19:34 -0500645}
Liz Kammer32b77cf2021-08-04 15:17:02 -0400646
647func registerCustomModuleForBp2buildConversion(ctx *android.TestContext) {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400648 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
Liz Kammer32b77cf2021-08-04 15:17:02 -0400649 ctx.RegisterForBazelConversion()
650}
Liz Kammer7a210ac2021-09-22 15:52:58 -0400651
Chris Parsonscd209032023-09-19 01:12:48 +0000652func simpleModule(typ, name string) string {
Liz Kammer7a210ac2021-09-22 15:52:58 -0400653 return fmt.Sprintf(`
654%s {
655 name: "%s",
Liz Kammer7a210ac2021-09-22 15:52:58 -0400656}`, typ, name)
657}
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500658
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000659type AttrNameToString map[string]string
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500660
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000661func (a AttrNameToString) clone() AttrNameToString {
662 newAttrs := make(AttrNameToString, len(a))
Liz Kammerdfeb1202022-05-13 17:20:20 -0400663 for k, v := range a {
664 newAttrs[k] = v
665 }
666 return newAttrs
667}
668
669// makeBazelTargetNoRestrictions returns bazel target build file definition that can be host or
670// device specific, or independent of host/device.
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000671func makeBazelTargetHostOrDevice(typ, name string, attrs AttrNameToString, hod android.HostOrDeviceSupported) string {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400672 if _, ok := attrs["target_compatible_with"]; !ok {
673 switch hod {
674 case android.HostSupported:
675 attrs["target_compatible_with"] = `select({
676 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
677 "//conditions:default": [],
678 })`
679 case android.DeviceSupported:
680 attrs["target_compatible_with"] = `["//build/bazel/platforms/os:android"]`
681 }
682 }
683
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500684 attrStrings := make([]string, 0, len(attrs)+1)
Sasha Smundakfb589492022-08-04 11:13:27 -0700685 if name != "" {
686 attrStrings = append(attrStrings, fmt.Sprintf(` name = "%s",`, name))
687 }
Cole Faust18994c72023-02-28 16:02:16 -0800688 for _, k := range android.SortedKeys(attrs) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500689 attrStrings = append(attrStrings, fmt.Sprintf(" %s = %s,", k, attrs[k]))
690 }
691 return fmt.Sprintf(`%s(
692%s
693)`, typ, strings.Join(attrStrings, "\n"))
694}
Liz Kammerdfeb1202022-05-13 17:20:20 -0400695
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000696// MakeBazelTargetNoRestrictions returns bazel target build file definition that does not add a
Liz Kammerdfeb1202022-05-13 17:20:20 -0400697// target_compatible_with. This is useful for module types like filegroup and genrule that arch not
698// arch variant
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000699func MakeBazelTargetNoRestrictions(typ, name string, attrs AttrNameToString) string {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400700 return makeBazelTargetHostOrDevice(typ, name, attrs, android.HostAndDeviceDefault)
701}
702
703// makeBazelTargetNoRestrictions returns bazel target build file definition that is device specific
704// as this is the most common default in Soong.
Alixe06d75b2022-08-31 18:28:19 +0000705func MakeBazelTarget(typ, name string, attrs AttrNameToString) string {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400706 return makeBazelTargetHostOrDevice(typ, name, attrs, android.DeviceSupported)
707}
Sasha Smundak9d2f1742022-08-04 13:28:38 -0700708
709type ExpectedRuleTarget struct {
710 Rule string
711 Name string
712 Attrs AttrNameToString
713 Hod android.HostOrDeviceSupported
714}
715
716func (ebr ExpectedRuleTarget) String() string {
717 return makeBazelTargetHostOrDevice(ebr.Rule, ebr.Name, ebr.Attrs, ebr.Hod)
718}
Trevor Radcliffe087af542022-09-16 15:36:10 +0000719
720func makeCcStubSuiteTargets(name string, attrs AttrNameToString) string {
721 if _, hasStubs := attrs["stubs_symbol_file"]; !hasStubs {
722 return ""
723 }
724 STUB_SUITE_ATTRS := map[string]string{
Spandan Das04f9f4c2023-09-13 23:59:05 +0000725 "api_surface": "api_surface",
Sam Delmerico5f906492023-03-15 18:06:18 -0400726 "stubs_symbol_file": "symbol_file",
727 "stubs_versions": "versions",
728 "soname": "soname",
729 "source_library_label": "source_library_label",
Trevor Radcliffe087af542022-09-16 15:36:10 +0000730 }
731
732 stubSuiteAttrs := AttrNameToString{}
733 for key, _ := range attrs {
734 if _, stubSuiteAttr := STUB_SUITE_ATTRS[key]; stubSuiteAttr {
735 stubSuiteAttrs[STUB_SUITE_ATTRS[key]] = attrs[key]
Sam Delmerico5f906492023-03-15 18:06:18 -0400736 } else {
737 panic(fmt.Sprintf("unused cc_stub_suite attr %q\n", key))
Trevor Radcliffe087af542022-09-16 15:36:10 +0000738 }
739 }
740 return MakeBazelTarget("cc_stub_suite", name+"_stub_libs", stubSuiteAttrs)
741}
Alix341484b2022-10-31 19:08:18 +0000742
743func MakeNeverlinkDuplicateTarget(moduleType string, name string) string {
Liz Kammer02914402023-08-07 13:38:18 -0400744 return MakeNeverlinkDuplicateTargetWithAttrs(moduleType, name, AttrNameToString{
745 "sdk_version": `"current"`, // use as default
746 })
Romain Jobredeaux2eef2e12023-02-24 12:07:08 -0500747}
748
749func MakeNeverlinkDuplicateTargetWithAttrs(moduleType string, name string, extraAttrs AttrNameToString) string {
750 attrs := extraAttrs
751 attrs["neverlink"] = `True`
752 attrs["exports"] = `[":` + name + `"]`
753 return MakeBazelTarget(moduleType, name+"-neverlink", attrs)
Alix341484b2022-10-31 19:08:18 +0000754}
Zi Wangfba0a212023-03-07 16:48:19 -0800755
756func getTargetName(targetContent string) string {
757 data := strings.Split(targetContent, "name = \"")
758 if len(data) < 2 {
759 return ""
760 } else {
761 endIndex := strings.Index(data[1], "\"")
762 return data[1][:endIndex]
763 }
764}