Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | f5a3eac | 2021-08-23 17:05:17 +0000 | [diff] [blame] | 1 | // 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 Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 15 | package bp2build |
| 16 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0da7ce6 | 2021-08-23 17:04:20 +0000 | [diff] [blame] | 17 | /* |
| 18 | For shareable/common bp2build testing functionality and dumping ground for |
| 19 | specific-but-shared functionality among tests in package |
| 20 | */ |
| 21 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 22 | import ( |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 23 | "fmt" |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 24 | "strings" |
Rupert Shuttleworth | 06559d0 | 2021-05-19 09:14:26 -0400 | [diff] [blame] | 25 | "testing" |
| 26 | |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 27 | "github.com/google/blueprint/proptools" |
| 28 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 29 | "android/soong/android" |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 30 | "android/soong/android/allowlists" |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 31 | "android/soong/bazel" |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 32 | ) |
| 33 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 34 | var ( |
Rupert Shuttleworth | 06559d0 | 2021-05-19 09:14:26 -0400 | [diff] [blame] | 35 | buildDir string |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 36 | ) |
| 37 | |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 38 | func checkError(t *testing.T, errs []error, expectedErr error) bool { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 39 | t.Helper() |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 40 | |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 41 | if len(errs) != 1 { |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 42 | return false |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 43 | } |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 44 | if strings.Contains(errs[0].Error(), expectedErr.Error()) { |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 45 | return true |
| 46 | } |
| 47 | |
| 48 | return false |
| 49 | } |
| 50 | |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 51 | func errored(t *testing.T, tc Bp2buildTestCase, errs []error) bool { |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 52 | t.Helper() |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 53 | if tc.ExpectedErr != nil { |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 54 | // 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 Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 58 | if len(errs) > 0 { |
| 59 | for _, err := range errs { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 60 | t.Errorf("%s: %s", tc.Description, err) |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 61 | } |
| 62 | return true |
| 63 | } |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 64 | |
| 65 | // All good, continue execution. |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 66 | return false |
| 67 | } |
| 68 | |
Trevor Radcliffe | 1b4b2d9 | 2022-09-01 18:57:01 +0000 | [diff] [blame] | 69 | func RunBp2BuildTestCaseSimple(t *testing.T, tc Bp2buildTestCase) { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 70 | t.Helper() |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 71 | RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, tc) |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 74 | type 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 Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 82 | // An error with a string contained within the string of the expected error |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 83 | ExpectedErr error |
| 84 | UnconvertedDepsMode unconvertedDepsMode |
Jingwen Chen | 0eeaeb8 | 2022-09-21 10:27:42 +0000 | [diff] [blame] | 85 | |
| 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 Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 92 | func RunBp2BuildTestCase(t *testing.T, registerModuleTypes func(ctx android.RegistrationContext), tc Bp2buildTestCase) { |
Liz Kammer | ffc17e4 | 2022-11-23 09:42:05 -0500 | [diff] [blame] | 93 | t.Helper() |
Paul Duffin | 4c0765a | 2022-10-29 17:48:00 +0100 | [diff] [blame] | 94 | bp2buildSetup := android.GroupFixturePreparers( |
| 95 | android.FixtureRegisterWithContext(registerModuleTypes), |
| 96 | SetBp2BuildTestRunner, |
| 97 | ) |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 98 | runBp2BuildTestCaseWithSetup(t, bp2buildSetup, tc) |
| 99 | } |
| 100 | |
| 101 | func RunApiBp2BuildTestCase(t *testing.T, registerModuleTypes func(ctx android.RegistrationContext), tc Bp2buildTestCase) { |
Liz Kammer | ffc17e4 | 2022-11-23 09:42:05 -0500 | [diff] [blame] | 102 | t.Helper() |
Paul Duffin | 4c0765a | 2022-10-29 17:48:00 +0100 | [diff] [blame] | 103 | apiBp2BuildSetup := android.GroupFixturePreparers( |
| 104 | android.FixtureRegisterWithContext(registerModuleTypes), |
| 105 | SetApiBp2BuildTestRunner, |
| 106 | ) |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 107 | runBp2BuildTestCaseWithSetup(t, apiBp2BuildSetup, tc) |
| 108 | } |
| 109 | |
Paul Duffin | 4c0765a | 2022-10-29 17:48:00 +0100 | [diff] [blame] | 110 | func runBp2BuildTestCaseWithSetup(t *testing.T, extraPreparer android.FixturePreparer, tc Bp2buildTestCase) { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 111 | t.Helper() |
| 112 | dir := "." |
| 113 | filesystem := make(map[string][]byte) |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 114 | for f, content := range tc.Filesystem { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 115 | filesystem[f] = []byte(content) |
| 116 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 117 | |
Paul Duffin | 4c0765a | 2022-10-29 17:48:00 +0100 | [diff] [blame] | 118 | 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 Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Paul Duffin | 4c0765a | 2022-10-29 17:48:00 +0100 | [diff] [blame] | 147 | 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 Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 154 | return |
| 155 | } |
| 156 | |
| 157 | checkDir := dir |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 158 | if tc.Dir != "" { |
| 159 | checkDir = tc.Dir |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 160 | } |
Paul Duffin | 4c0765a | 2022-10-29 17:48:00 +0100 | [diff] [blame] | 161 | expectedTargets := map[string][]string{ |
| 162 | checkDir: tc.ExpectedBazelTargets, |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 163 | } |
Paul Duffin | 4c0765a | 2022-10-29 17:48:00 +0100 | [diff] [blame] | 164 | |
| 165 | result.CompareAllBazelTargets(t, tc.Description, expectedTargets, true) |
| 166 | } |
| 167 | |
| 168 | // SetBp2BuildTestRunner customizes the test fixture mechanism to run tests in Bp2Build mode. |
| 169 | var SetBp2BuildTestRunner = android.FixtureSetTestRunner(&bazelTestRunner{Bp2Build}) |
| 170 | |
| 171 | // SetApiBp2BuildTestRunner customizes the test fixture mechanism to run tests in ApiBp2build mode. |
| 172 | var SetApiBp2BuildTestRunner = android.FixtureSetTestRunner(&bazelTestRunner{ApiBp2build}) |
| 173 | |
| 174 | // bazelTestRunner customizes the test fixture mechanism to run tests of the bp2build and |
| 175 | // apiBp2build build modes. |
| 176 | type bazelTestRunner struct { |
| 177 | mode CodegenMode |
| 178 | } |
| 179 | |
| 180 | func (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 | |
| 194 | func (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 | |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 203 | codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "") |
Paul Duffin | 4c0765a | 2022-10-29 17:48:00 +0100 | [diff] [blame] | 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. |
| 215 | type 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. |
| 229 | func (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 | |
| 260 | func (b BazelTestResult) CompareBazelTargets(t *testing.T, description string, expectedContents []string, actualTargets BazelTargets) { |
Liz Kammer | 748d707 | 2023-01-25 12:07:43 -0500 | [diff] [blame^] | 261 | t.Helper() |
Paul Duffin | 4c0765a | 2022-10-29 17:48:00 +0100 | [diff] [blame] | 262 | if actualCount, expectedCount := len(actualTargets), len(expectedContents); actualCount != expectedCount { |
Sasha Smundak | 9d2f174 | 2022-08-04 13:28:38 -0700 | [diff] [blame] | 263 | t.Errorf("%s: Expected %d bazel target (%s), got %d (%s)", |
Paul Duffin | 4c0765a | 2022-10-29 17:48:00 +0100 | [diff] [blame] | 264 | description, expectedCount, expectedContents, actualCount, actualTargets) |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 265 | } else { |
Paul Duffin | 4c0765a | 2022-10-29 17:48:00 +0100 | [diff] [blame] | 266 | for i, actualTarget := range actualTargets { |
| 267 | if w, g := expectedContents[i], actualTarget.content; w != g { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 268 | t.Errorf( |
Paul Duffin | 4c0765a | 2022-10-29 17:48:00 +0100 | [diff] [blame] | 269 | "%s[%d]: Expected generated Bazel target to be `%s`, got `%s`", |
| 270 | description, i, w, g) |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 276 | type nestedProps struct { |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 277 | Nested_prop *string |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 278 | } |
| 279 | |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 280 | type EmbeddedProps struct { |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 281 | Embedded_prop *string |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | type OtherEmbeddedProps struct { |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 285 | Other_embedded_prop *string |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 286 | } |
| 287 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 288 | type customProps struct { |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 289 | EmbeddedProps |
| 290 | *OtherEmbeddedProps |
| 291 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 292 | Bool_prop bool |
| 293 | Bool_ptr_prop *bool |
| 294 | // Ensure that properties tagged `blueprint:mutated` are omitted |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 3a019a6 | 2022-06-23 16:02:44 +0000 | [diff] [blame] | 295 | Int_prop int `blueprint:"mutated"` |
| 296 | Int64_ptr_prop *int64 |
| 297 | String_prop string |
| 298 | String_literal_prop *string `android:"arch_variant"` |
| 299 | String_ptr_prop *string |
| 300 | String_list_prop []string |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 301 | |
| 302 | Nested_props nestedProps |
| 303 | Nested_props_ptr *nestedProps |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 304 | |
Liz Kammer | 32b77cf | 2021-08-04 15:17:02 -0400 | [diff] [blame] | 305 | Arch_paths []string `android:"path,arch_variant"` |
| 306 | Arch_paths_exclude []string `android:"path,arch_variant"` |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 307 | |
| 308 | // Prop used to indicate this conversion should be 1 module -> multiple targets |
| 309 | One_to_many_prop *bool |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 310 | |
| 311 | Api *string // File describing the APIs of this module |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | type customModule struct { |
| 315 | android.ModuleBase |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 316 | android.BazelModuleBase |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 317 | |
| 318 | props customProps |
| 319 | } |
| 320 | |
| 321 | // OutputFiles is needed because some instances of this module use dist with a |
| 322 | // tag property which requires the module implements OutputFileProducer. |
| 323 | func (m *customModule) OutputFiles(tag string) (android.Paths, error) { |
| 324 | return android.PathsForTesting("path" + tag), nil |
| 325 | } |
| 326 | |
| 327 | func (m *customModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 328 | // nothing for now. |
| 329 | } |
| 330 | |
| 331 | func customModuleFactoryBase() android.Module { |
| 332 | module := &customModule{} |
| 333 | module.AddProperties(&module.props) |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 334 | android.InitBazelModule(module) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 335 | return module |
| 336 | } |
| 337 | |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 338 | func customModuleFactoryHostAndDevice() android.Module { |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 339 | m := customModuleFactoryBase() |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 340 | android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibBoth) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 341 | return m |
| 342 | } |
| 343 | |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 344 | func customModuleFactoryDeviceSupported() android.Module { |
| 345 | m := customModuleFactoryBase() |
| 346 | android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibBoth) |
| 347 | return m |
| 348 | } |
| 349 | |
| 350 | func customModuleFactoryHostSupported() android.Module { |
| 351 | m := customModuleFactoryBase() |
| 352 | android.InitAndroidArchModule(m, android.HostSupported, android.MultilibBoth) |
| 353 | return m |
| 354 | } |
| 355 | |
| 356 | func customModuleFactoryHostAndDeviceDefault() android.Module { |
| 357 | m := customModuleFactoryBase() |
| 358 | android.InitAndroidArchModule(m, android.HostAndDeviceDefault, android.MultilibBoth) |
| 359 | return m |
| 360 | } |
| 361 | |
| 362 | func customModuleFactoryNeitherHostNorDeviceSupported() android.Module { |
| 363 | m := customModuleFactoryBase() |
| 364 | android.InitAndroidArchModule(m, android.NeitherHostNorDeviceSupported, android.MultilibBoth) |
| 365 | return m |
| 366 | } |
| 367 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 368 | type testProps struct { |
| 369 | Test_prop struct { |
| 370 | Test_string_prop string |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | type customTestModule struct { |
| 375 | android.ModuleBase |
| 376 | |
| 377 | props customProps |
| 378 | test_props testProps |
| 379 | } |
| 380 | |
| 381 | func (m *customTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 382 | // nothing for now. |
| 383 | } |
| 384 | |
| 385 | func customTestModuleFactoryBase() android.Module { |
| 386 | m := &customTestModule{} |
| 387 | m.AddProperties(&m.props) |
| 388 | m.AddProperties(&m.test_props) |
| 389 | return m |
| 390 | } |
| 391 | |
| 392 | func customTestModuleFactory() android.Module { |
| 393 | m := customTestModuleFactoryBase() |
| 394 | android.InitAndroidModule(m) |
| 395 | return m |
| 396 | } |
| 397 | |
| 398 | type customDefaultsModule struct { |
| 399 | android.ModuleBase |
| 400 | android.DefaultsModuleBase |
| 401 | } |
| 402 | |
| 403 | func customDefaultsModuleFactoryBase() android.DefaultsModule { |
| 404 | module := &customDefaultsModule{} |
| 405 | module.AddProperties(&customProps{}) |
| 406 | return module |
| 407 | } |
| 408 | |
| 409 | func customDefaultsModuleFactoryBasic() android.Module { |
| 410 | return customDefaultsModuleFactoryBase() |
| 411 | } |
| 412 | |
| 413 | func customDefaultsModuleFactory() android.Module { |
| 414 | m := customDefaultsModuleFactoryBase() |
| 415 | android.InitDefaultsModule(m) |
| 416 | return m |
| 417 | } |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 418 | |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 419 | type EmbeddedAttr struct { |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 420 | Embedded_attr *string |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | type OtherEmbeddedAttr struct { |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 424 | Other_embedded_attr *string |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 425 | } |
| 426 | |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 427 | type customBazelModuleAttributes struct { |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 428 | EmbeddedAttr |
| 429 | *OtherEmbeddedAttr |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 3a019a6 | 2022-06-23 16:02:44 +0000 | [diff] [blame] | 430 | String_literal_prop bazel.StringAttribute |
| 431 | String_ptr_prop *string |
| 432 | String_list_prop []string |
| 433 | Arch_paths bazel.LabelListAttribute |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 434 | Api bazel.LabelAttribute |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 435 | } |
| 436 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 437 | func (m *customModule) ConvertWithBp2build(ctx android.TopDownMutatorContext) { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 438 | if p := m.props.One_to_many_prop; p != nil && *p { |
| 439 | customBp2buildOneToMany(ctx, m) |
| 440 | return |
| 441 | } |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 442 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 3a019a6 | 2022-06-23 16:02:44 +0000 | [diff] [blame] | 443 | paths := bazel.LabelListAttribute{} |
| 444 | strAttr := bazel.StringAttribute{} |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 445 | for axis, configToProps := range m.GetArchVariantProperties(ctx, &customProps{}) { |
| 446 | for config, props := range configToProps { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 3a019a6 | 2022-06-23 16:02:44 +0000 | [diff] [blame] | 447 | if custProps, ok := props.(*customProps); ok { |
| 448 | if custProps.Arch_paths != nil { |
| 449 | paths.SetSelectValue(axis, config, android.BazelLabelForModuleSrcExcludes(ctx, custProps.Arch_paths, custProps.Arch_paths_exclude)) |
| 450 | } |
| 451 | if custProps.String_literal_prop != nil { |
| 452 | strAttr.SetSelectValue(axis, config, custProps.String_literal_prop) |
| 453 | } |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 454 | } |
| 455 | } |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 456 | } |
Liz Kammer | 9d2d410 | 2022-12-21 14:51:37 -0500 | [diff] [blame] | 457 | productVariableProps := android.ProductVariableProperties(ctx) |
| 458 | if props, ok := productVariableProps["String_literal_prop"]; ok { |
| 459 | for c, p := range props { |
| 460 | if val, ok := p.(*string); ok { |
| 461 | strAttr.SetSelectValue(c.ConfigurationAxis(), c.SelectKey(), val) |
| 462 | } |
| 463 | } |
| 464 | } |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 465 | |
| 466 | paths.ResolveExcludes() |
| 467 | |
| 468 | attrs := &customBazelModuleAttributes{ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 3a019a6 | 2022-06-23 16:02:44 +0000 | [diff] [blame] | 469 | String_literal_prop: strAttr, |
| 470 | String_ptr_prop: m.props.String_ptr_prop, |
| 471 | String_list_prop: m.props.String_list_prop, |
| 472 | Arch_paths: paths, |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 473 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 3a019a6 | 2022-06-23 16:02:44 +0000 | [diff] [blame] | 474 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 475 | attrs.Embedded_attr = m.props.Embedded_prop |
| 476 | if m.props.OtherEmbeddedProps != nil { |
| 477 | attrs.OtherEmbeddedAttr = &OtherEmbeddedAttr{Other_embedded_attr: m.props.OtherEmbeddedProps.Other_embedded_prop} |
| 478 | } |
| 479 | |
| 480 | props := bazel.BazelTargetModuleProperties{ |
| 481 | Rule_class: "custom", |
| 482 | } |
| 483 | |
| 484 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, attrs) |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 485 | } |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 486 | |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 487 | var _ android.ApiProvider = (*customModule)(nil) |
| 488 | |
| 489 | func (c *customModule) ConvertWithApiBp2build(ctx android.TopDownMutatorContext) { |
| 490 | props := bazel.BazelTargetModuleProperties{ |
| 491 | Rule_class: "custom_api_contribution", |
| 492 | } |
| 493 | apiAttribute := bazel.MakeLabelAttribute( |
| 494 | android.BazelLabelForModuleSrcSingle(ctx, proptools.String(c.props.Api)).Label, |
| 495 | ) |
| 496 | attrs := &customBazelModuleAttributes{ |
| 497 | Api: *apiAttribute, |
| 498 | } |
| 499 | ctx.CreateBazelTargetModule(props, |
| 500 | android.CommonAttributes{Name: c.Name()}, |
| 501 | attrs) |
| 502 | } |
| 503 | |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 504 | // A bp2build mutator that uses load statements and creates a 1:M mapping from |
| 505 | // module to target. |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 506 | func customBp2buildOneToMany(ctx android.TopDownMutatorContext, m *customModule) { |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 507 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 508 | baseName := m.Name() |
| 509 | attrs := &customBazelModuleAttributes{} |
Jingwen Chen | 1fd1469 | 2021-02-05 03:01:50 -0500 | [diff] [blame] | 510 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 511 | myLibraryProps := bazel.BazelTargetModuleProperties{ |
| 512 | Rule_class: "my_library", |
| 513 | Bzl_load_location: "//build/bazel/rules:rules.bzl", |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 514 | } |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 515 | ctx.CreateBazelTargetModule(myLibraryProps, android.CommonAttributes{Name: baseName}, attrs) |
| 516 | |
| 517 | protoLibraryProps := bazel.BazelTargetModuleProperties{ |
| 518 | Rule_class: "proto_library", |
| 519 | Bzl_load_location: "//build/bazel/rules:proto.bzl", |
| 520 | } |
| 521 | ctx.CreateBazelTargetModule(protoLibraryProps, android.CommonAttributes{Name: baseName + "_proto_library_deps"}, attrs) |
| 522 | |
| 523 | myProtoLibraryProps := bazel.BazelTargetModuleProperties{ |
| 524 | Rule_class: "my_proto_library", |
| 525 | Bzl_load_location: "//build/bazel/rules:proto.bzl", |
| 526 | } |
| 527 | ctx.CreateBazelTargetModule(myProtoLibraryProps, android.CommonAttributes{Name: baseName + "_my_proto_library_deps"}, attrs) |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 528 | } |
Jingwen Chen | ba369ad | 2021-02-22 10:19:34 -0500 | [diff] [blame] | 529 | |
| 530 | // Helper method for tests to easily access the targets in a dir. |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 531 | func generateBazelTargetsForDir(codegenCtx *CodegenContext, dir string) (BazelTargets, []error) { |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 532 | // TODO: Set generateFilegroups to true and/or remove the generateFilegroups argument completely |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 533 | res, err := GenerateBazelTargets(codegenCtx, false) |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 534 | if err != nil { |
| 535 | return BazelTargets{}, err |
| 536 | } |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 537 | return res.buildFileToTargets[dir], err |
Jingwen Chen | ba369ad | 2021-02-22 10:19:34 -0500 | [diff] [blame] | 538 | } |
Liz Kammer | 32b77cf | 2021-08-04 15:17:02 -0400 | [diff] [blame] | 539 | |
| 540 | func registerCustomModuleForBp2buildConversion(ctx *android.TestContext) { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 541 | ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) |
Liz Kammer | 32b77cf | 2021-08-04 15:17:02 -0400 | [diff] [blame] | 542 | ctx.RegisterForBazelConversion() |
| 543 | } |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 544 | |
| 545 | func simpleModuleDoNotConvertBp2build(typ, name string) string { |
| 546 | return fmt.Sprintf(` |
| 547 | %s { |
| 548 | name: "%s", |
| 549 | bazel_module: { bp2build_available: false }, |
| 550 | }`, typ, name) |
| 551 | } |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 552 | |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 553 | type AttrNameToString map[string]string |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 554 | |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 555 | func (a AttrNameToString) clone() AttrNameToString { |
| 556 | newAttrs := make(AttrNameToString, len(a)) |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 557 | for k, v := range a { |
| 558 | newAttrs[k] = v |
| 559 | } |
| 560 | return newAttrs |
| 561 | } |
| 562 | |
| 563 | // makeBazelTargetNoRestrictions returns bazel target build file definition that can be host or |
| 564 | // device specific, or independent of host/device. |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 565 | func makeBazelTargetHostOrDevice(typ, name string, attrs AttrNameToString, hod android.HostOrDeviceSupported) string { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 566 | if _, ok := attrs["target_compatible_with"]; !ok { |
| 567 | switch hod { |
| 568 | case android.HostSupported: |
| 569 | attrs["target_compatible_with"] = `select({ |
| 570 | "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], |
| 571 | "//conditions:default": [], |
| 572 | })` |
| 573 | case android.DeviceSupported: |
| 574 | attrs["target_compatible_with"] = `["//build/bazel/platforms/os:android"]` |
| 575 | } |
| 576 | } |
| 577 | |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 578 | attrStrings := make([]string, 0, len(attrs)+1) |
Sasha Smundak | fb58949 | 2022-08-04 11:13:27 -0700 | [diff] [blame] | 579 | if name != "" { |
| 580 | attrStrings = append(attrStrings, fmt.Sprintf(` name = "%s",`, name)) |
| 581 | } |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 582 | for _, k := range android.SortedStringKeys(attrs) { |
| 583 | attrStrings = append(attrStrings, fmt.Sprintf(" %s = %s,", k, attrs[k])) |
| 584 | } |
| 585 | return fmt.Sprintf(`%s( |
| 586 | %s |
| 587 | )`, typ, strings.Join(attrStrings, "\n")) |
| 588 | } |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 589 | |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 590 | // MakeBazelTargetNoRestrictions returns bazel target build file definition that does not add a |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 591 | // target_compatible_with. This is useful for module types like filegroup and genrule that arch not |
| 592 | // arch variant |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 593 | func MakeBazelTargetNoRestrictions(typ, name string, attrs AttrNameToString) string { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 594 | return makeBazelTargetHostOrDevice(typ, name, attrs, android.HostAndDeviceDefault) |
| 595 | } |
| 596 | |
| 597 | // makeBazelTargetNoRestrictions returns bazel target build file definition that is device specific |
| 598 | // as this is the most common default in Soong. |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 599 | func MakeBazelTarget(typ, name string, attrs AttrNameToString) string { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 600 | return makeBazelTargetHostOrDevice(typ, name, attrs, android.DeviceSupported) |
| 601 | } |
Sasha Smundak | 9d2f174 | 2022-08-04 13:28:38 -0700 | [diff] [blame] | 602 | |
| 603 | type ExpectedRuleTarget struct { |
| 604 | Rule string |
| 605 | Name string |
| 606 | Attrs AttrNameToString |
| 607 | Hod android.HostOrDeviceSupported |
| 608 | } |
| 609 | |
| 610 | func (ebr ExpectedRuleTarget) String() string { |
| 611 | return makeBazelTargetHostOrDevice(ebr.Rule, ebr.Name, ebr.Attrs, ebr.Hod) |
| 612 | } |
Trevor Radcliffe | 087af54 | 2022-09-16 15:36:10 +0000 | [diff] [blame] | 613 | |
| 614 | func makeCcStubSuiteTargets(name string, attrs AttrNameToString) string { |
| 615 | if _, hasStubs := attrs["stubs_symbol_file"]; !hasStubs { |
| 616 | return "" |
| 617 | } |
| 618 | STUB_SUITE_ATTRS := map[string]string{ |
| 619 | "stubs_symbol_file": "symbol_file", |
| 620 | "stubs_versions": "versions", |
| 621 | "soname": "soname", |
| 622 | "source_library": "source_library", |
| 623 | } |
| 624 | |
| 625 | stubSuiteAttrs := AttrNameToString{} |
| 626 | for key, _ := range attrs { |
| 627 | if _, stubSuiteAttr := STUB_SUITE_ATTRS[key]; stubSuiteAttr { |
| 628 | stubSuiteAttrs[STUB_SUITE_ATTRS[key]] = attrs[key] |
| 629 | } |
| 630 | } |
| 631 | return MakeBazelTarget("cc_stub_suite", name+"_stub_libs", stubSuiteAttrs) |
| 632 | } |
Alix | 341484b | 2022-10-31 19:08:18 +0000 | [diff] [blame] | 633 | |
| 634 | func MakeNeverlinkDuplicateTarget(moduleType string, name string) string { |
| 635 | return MakeBazelTarget(moduleType, name+"-neverlink", AttrNameToString{ |
| 636 | "neverlink": `True`, |
| 637 | "exports": `[":` + name + `"]`, |
| 638 | }) |
| 639 | } |