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" |
Zi Wang | fba0a21 | 2023-03-07 16:48:19 -0800 | [diff] [blame] | 24 | "sort" |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 25 | "strings" |
Rupert Shuttleworth | 06559d0 | 2021-05-19 09:14:26 -0400 | [diff] [blame] | 26 | "testing" |
| 27 | |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 28 | "github.com/google/blueprint/proptools" |
| 29 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 30 | "android/soong/android" |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 31 | "android/soong/android/allowlists" |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 32 | "android/soong/bazel" |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 33 | ) |
| 34 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 35 | var ( |
Rupert Shuttleworth | 06559d0 | 2021-05-19 09:14:26 -0400 | [diff] [blame] | 36 | buildDir string |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 37 | ) |
| 38 | |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 39 | 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] | 40 | t.Helper() |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 41 | |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 42 | if len(errs) != 1 { |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 43 | return false |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 44 | } |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 45 | if strings.Contains(errs[0].Error(), expectedErr.Error()) { |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 46 | return true |
| 47 | } |
| 48 | |
| 49 | return false |
| 50 | } |
| 51 | |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 52 | func errored(t *testing.T, tc Bp2buildTestCase, errs []error) bool { |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 53 | t.Helper() |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 54 | if tc.ExpectedErr != nil { |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 55 | // Rely on checkErrors, as this test case is expected to have an error. |
| 56 | return false |
| 57 | } |
| 58 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 59 | if len(errs) > 0 { |
| 60 | for _, err := range errs { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 61 | 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] | 62 | } |
| 63 | return true |
| 64 | } |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 65 | |
| 66 | // 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] | 67 | return false |
| 68 | } |
| 69 | |
Trevor Radcliffe | 1b4b2d9 | 2022-09-01 18:57:01 +0000 | [diff] [blame] | 70 | 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] | 71 | t.Helper() |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 72 | 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] | 73 | } |
| 74 | |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 75 | type Bp2buildTestCase struct { |
| 76 | Description string |
| 77 | ModuleTypeUnderTest string |
| 78 | ModuleTypeUnderTestFactory android.ModuleFactory |
Sam Delmerico | 5840afc | 2023-06-12 15:44:03 -0400 | [diff] [blame^] | 79 | // Text to add to the toplevel, root Android.bp file. If Dir is not set, all |
| 80 | // ExpectedBazelTargets are assumed to be generated by this file. |
| 81 | Blueprint string |
| 82 | // ExpectedBazelTargets compares the BazelTargets generated in `Dir` (if not empty). |
| 83 | // Otherwise, it checks the BazelTargets generated by `Blueprint` in the root directory. |
| 84 | ExpectedBazelTargets []string |
| 85 | Filesystem map[string]string |
| 86 | // Dir sets the directory which will be compared against the targets in ExpectedBazelTargets. |
| 87 | // This should used in conjunction with the Filesystem property to check for targets |
| 88 | // generated from a directory that is not the root. |
| 89 | // If not set, all ExpectedBazelTargets are assumed to be generated by the text in the |
| 90 | // Blueprint property. |
| 91 | Dir string |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 92 | // 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] | 93 | ExpectedErr error |
| 94 | UnconvertedDepsMode unconvertedDepsMode |
Jingwen Chen | 0eeaeb8 | 2022-09-21 10:27:42 +0000 | [diff] [blame] | 95 | |
| 96 | // For every directory listed here, the BUILD file for that directory will |
| 97 | // be merged with the generated BUILD file. This allows custom BUILD targets |
| 98 | // to be used in tests, or use BUILD files to draw package boundaries. |
| 99 | KeepBuildFileForDirs []string |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 102 | func RunBp2BuildTestCase(t *testing.T, registerModuleTypes func(ctx android.RegistrationContext), tc Bp2buildTestCase) { |
Liz Kammer | ffc17e4 | 2022-11-23 09:42:05 -0500 | [diff] [blame] | 103 | t.Helper() |
Paul Duffin | 4c0765a | 2022-10-29 17:48:00 +0100 | [diff] [blame] | 104 | bp2buildSetup := android.GroupFixturePreparers( |
| 105 | android.FixtureRegisterWithContext(registerModuleTypes), |
| 106 | SetBp2BuildTestRunner, |
| 107 | ) |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 108 | runBp2BuildTestCaseWithSetup(t, bp2buildSetup, tc) |
| 109 | } |
| 110 | |
| 111 | func RunApiBp2BuildTestCase(t *testing.T, registerModuleTypes func(ctx android.RegistrationContext), tc Bp2buildTestCase) { |
Liz Kammer | ffc17e4 | 2022-11-23 09:42:05 -0500 | [diff] [blame] | 112 | t.Helper() |
Paul Duffin | 4c0765a | 2022-10-29 17:48:00 +0100 | [diff] [blame] | 113 | apiBp2BuildSetup := android.GroupFixturePreparers( |
| 114 | android.FixtureRegisterWithContext(registerModuleTypes), |
| 115 | SetApiBp2BuildTestRunner, |
| 116 | ) |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 117 | runBp2BuildTestCaseWithSetup(t, apiBp2BuildSetup, tc) |
| 118 | } |
| 119 | |
Paul Duffin | 4c0765a | 2022-10-29 17:48:00 +0100 | [diff] [blame] | 120 | 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] | 121 | t.Helper() |
| 122 | dir := "." |
| 123 | filesystem := make(map[string][]byte) |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 124 | 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] | 125 | filesystem[f] = []byte(content) |
| 126 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 127 | |
Paul Duffin | 4c0765a | 2022-10-29 17:48:00 +0100 | [diff] [blame] | 128 | preparers := []android.FixturePreparer{ |
| 129 | extraPreparer, |
| 130 | android.FixtureMergeMockFs(filesystem), |
| 131 | android.FixtureWithRootAndroidBp(tc.Blueprint), |
| 132 | android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) { |
| 133 | ctx.RegisterModuleType(tc.ModuleTypeUnderTest, tc.ModuleTypeUnderTestFactory) |
| 134 | }), |
| 135 | android.FixtureModifyContext(func(ctx *android.TestContext) { |
| 136 | // A default configuration for tests to not have to specify bp2build_available on top level |
| 137 | // targets. |
| 138 | bp2buildConfig := android.NewBp2BuildAllowlist().SetDefaultConfig( |
| 139 | allowlists.Bp2BuildConfig{ |
| 140 | android.Bp2BuildTopLevel: allowlists.Bp2BuildDefaultTrueRecursively, |
| 141 | }, |
| 142 | ) |
| 143 | for _, f := range tc.KeepBuildFileForDirs { |
| 144 | bp2buildConfig.SetKeepExistingBuildFile(map[string]bool{ |
| 145 | f: /*recursive=*/ false, |
| 146 | }) |
| 147 | } |
| 148 | ctx.RegisterBp2BuildConfig(bp2buildConfig) |
| 149 | }), |
| 150 | android.FixtureModifyEnv(func(env map[string]string) { |
| 151 | if tc.UnconvertedDepsMode == errorModulesUnconvertedDeps { |
| 152 | env["BP2BUILD_ERROR_UNCONVERTED"] = "true" |
| 153 | } |
| 154 | }), |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Paul Duffin | 4c0765a | 2022-10-29 17:48:00 +0100 | [diff] [blame] | 157 | preparer := android.GroupFixturePreparers(preparers...) |
| 158 | if tc.ExpectedErr != nil { |
| 159 | pattern := "\\Q" + tc.ExpectedErr.Error() + "\\E" |
| 160 | preparer = preparer.ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(pattern)) |
| 161 | } |
| 162 | result := preparer.RunTestWithCustomResult(t).(*BazelTestResult) |
| 163 | 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] | 164 | return |
| 165 | } |
| 166 | |
| 167 | checkDir := dir |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 168 | if tc.Dir != "" { |
| 169 | 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] | 170 | } |
Paul Duffin | 4c0765a | 2022-10-29 17:48:00 +0100 | [diff] [blame] | 171 | expectedTargets := map[string][]string{ |
| 172 | checkDir: tc.ExpectedBazelTargets, |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 173 | } |
Paul Duffin | 4c0765a | 2022-10-29 17:48:00 +0100 | [diff] [blame] | 174 | |
| 175 | result.CompareAllBazelTargets(t, tc.Description, expectedTargets, true) |
| 176 | } |
| 177 | |
| 178 | // SetBp2BuildTestRunner customizes the test fixture mechanism to run tests in Bp2Build mode. |
| 179 | var SetBp2BuildTestRunner = android.FixtureSetTestRunner(&bazelTestRunner{Bp2Build}) |
| 180 | |
| 181 | // SetApiBp2BuildTestRunner customizes the test fixture mechanism to run tests in ApiBp2build mode. |
| 182 | var SetApiBp2BuildTestRunner = android.FixtureSetTestRunner(&bazelTestRunner{ApiBp2build}) |
| 183 | |
| 184 | // bazelTestRunner customizes the test fixture mechanism to run tests of the bp2build and |
| 185 | // apiBp2build build modes. |
| 186 | type bazelTestRunner struct { |
| 187 | mode CodegenMode |
| 188 | } |
| 189 | |
| 190 | func (b *bazelTestRunner) FinalPreparer(result *android.TestResult) android.CustomTestResult { |
| 191 | ctx := result.TestContext |
| 192 | switch b.mode { |
| 193 | case Bp2Build: |
| 194 | ctx.RegisterForBazelConversion() |
| 195 | case ApiBp2build: |
| 196 | ctx.RegisterForApiBazelConversion() |
| 197 | default: |
| 198 | panic(fmt.Errorf("unknown build mode: %d", b.mode)) |
| 199 | } |
| 200 | |
| 201 | return &BazelTestResult{TestResult: result} |
| 202 | } |
| 203 | |
| 204 | func (b *bazelTestRunner) PostParseProcessor(result android.CustomTestResult) { |
| 205 | bazelResult := result.(*BazelTestResult) |
| 206 | ctx := bazelResult.TestContext |
| 207 | config := bazelResult.Config |
| 208 | _, errs := ctx.ResolveDependencies(config) |
| 209 | if bazelResult.CollateErrs(errs) { |
| 210 | return |
| 211 | } |
| 212 | |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 213 | codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "") |
Paul Duffin | 4c0765a | 2022-10-29 17:48:00 +0100 | [diff] [blame] | 214 | res, errs := GenerateBazelTargets(codegenCtx, false) |
| 215 | if bazelResult.CollateErrs(errs) { |
| 216 | return |
| 217 | } |
| 218 | |
| 219 | // Store additional data for access by tests. |
| 220 | bazelResult.conversionResults = res |
| 221 | } |
| 222 | |
| 223 | // BazelTestResult is a wrapper around android.TestResult to provide type safe access to the bazel |
| 224 | // specific data stored by the bazelTestRunner. |
| 225 | type BazelTestResult struct { |
| 226 | *android.TestResult |
| 227 | |
| 228 | // The result returned by the GenerateBazelTargets function. |
| 229 | conversionResults |
| 230 | } |
| 231 | |
| 232 | // CompareAllBazelTargets compares the BazelTargets produced by the test for all the directories |
| 233 | // with the supplied set of expected targets. |
| 234 | // |
| 235 | // If ignoreUnexpected=false then this enforces an exact match where every BazelTarget produced must |
| 236 | // have a corresponding expected BazelTarget. |
| 237 | // |
| 238 | // If ignoreUnexpected=true then it will ignore directories for which there are no expected targets. |
| 239 | func (b BazelTestResult) CompareAllBazelTargets(t *testing.T, description string, expectedTargets map[string][]string, ignoreUnexpected bool) { |
Liz Kammer | 2b3f56e | 2023-03-23 11:51:49 -0400 | [diff] [blame] | 240 | t.Helper() |
Paul Duffin | 4c0765a | 2022-10-29 17:48:00 +0100 | [diff] [blame] | 241 | actualTargets := b.buildFileToTargets |
| 242 | |
| 243 | // Generate the sorted set of directories to check. |
Cole Faust | 18994c7 | 2023-02-28 16:02:16 -0800 | [diff] [blame] | 244 | dirsToCheck := android.SortedKeys(expectedTargets) |
Paul Duffin | 4c0765a | 2022-10-29 17:48:00 +0100 | [diff] [blame] | 245 | if !ignoreUnexpected { |
| 246 | // This needs to perform an exact match so add the directories in which targets were |
| 247 | // produced to the list of directories to check. |
Cole Faust | 18994c7 | 2023-02-28 16:02:16 -0800 | [diff] [blame] | 248 | dirsToCheck = append(dirsToCheck, android.SortedKeys(actualTargets)...) |
Paul Duffin | 4c0765a | 2022-10-29 17:48:00 +0100 | [diff] [blame] | 249 | dirsToCheck = android.SortedUniqueStrings(dirsToCheck) |
| 250 | } |
| 251 | |
| 252 | for _, dir := range dirsToCheck { |
| 253 | expected := expectedTargets[dir] |
| 254 | actual := actualTargets[dir] |
| 255 | |
| 256 | if expected == nil { |
| 257 | if actual != nil { |
| 258 | t.Errorf("did not expect any bazel modules in %q but found %d", dir, len(actual)) |
| 259 | } |
| 260 | } else if actual == nil { |
| 261 | expectedCount := len(expected) |
| 262 | if expectedCount > 0 { |
| 263 | t.Errorf("expected %d bazel modules in %q but did not find any", expectedCount, dir) |
| 264 | } |
| 265 | } else { |
| 266 | b.CompareBazelTargets(t, description, expected, actual) |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | 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] | 272 | t.Helper() |
Paul Duffin | 4c0765a | 2022-10-29 17:48:00 +0100 | [diff] [blame] | 273 | if actualCount, expectedCount := len(actualTargets), len(expectedContents); actualCount != expectedCount { |
Sasha Smundak | 9d2f174 | 2022-08-04 13:28:38 -0700 | [diff] [blame] | 274 | t.Errorf("%s: Expected %d bazel target (%s), got %d (%s)", |
Paul Duffin | 4c0765a | 2022-10-29 17:48:00 +0100 | [diff] [blame] | 275 | 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] | 276 | } else { |
Zi Wang | fba0a21 | 2023-03-07 16:48:19 -0800 | [diff] [blame] | 277 | sort.SliceStable(actualTargets, func(i, j int) bool { |
| 278 | return actualTargets[i].name < actualTargets[j].name |
| 279 | }) |
| 280 | sort.SliceStable(expectedContents, func(i, j int) bool { |
| 281 | return getTargetName(expectedContents[i]) < getTargetName(expectedContents[j]) |
| 282 | }) |
Paul Duffin | 4c0765a | 2022-10-29 17:48:00 +0100 | [diff] [blame] | 283 | for i, actualTarget := range actualTargets { |
| 284 | 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] | 285 | t.Errorf( |
Paul Duffin | 4c0765a | 2022-10-29 17:48:00 +0100 | [diff] [blame] | 286 | "%s[%d]: Expected generated Bazel target to be `%s`, got `%s`", |
| 287 | 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] | 288 | } |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 293 | type nestedProps struct { |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 294 | Nested_prop *string |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 295 | } |
| 296 | |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 297 | type EmbeddedProps struct { |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 298 | Embedded_prop *string |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | type OtherEmbeddedProps struct { |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 302 | Other_embedded_prop *string |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 303 | } |
| 304 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 305 | type customProps struct { |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 306 | EmbeddedProps |
| 307 | *OtherEmbeddedProps |
| 308 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 309 | Bool_prop bool |
| 310 | Bool_ptr_prop *bool |
| 311 | // 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] | 312 | Int_prop int `blueprint:"mutated"` |
| 313 | Int64_ptr_prop *int64 |
| 314 | String_prop string |
| 315 | String_literal_prop *string `android:"arch_variant"` |
| 316 | String_ptr_prop *string |
| 317 | String_list_prop []string |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 318 | |
| 319 | Nested_props nestedProps |
| 320 | Nested_props_ptr *nestedProps |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 321 | |
Liz Kammer | 32b77cf | 2021-08-04 15:17:02 -0400 | [diff] [blame] | 322 | Arch_paths []string `android:"path,arch_variant"` |
| 323 | Arch_paths_exclude []string `android:"path,arch_variant"` |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 324 | |
| 325 | // Prop used to indicate this conversion should be 1 module -> multiple targets |
| 326 | One_to_many_prop *bool |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 327 | |
| 328 | Api *string // File describing the APIs of this module |
Spandan Das | 6a448ec | 2023-04-19 17:36:12 +0000 | [diff] [blame] | 329 | |
| 330 | Test_config_setting *bool // Used to test generation of config_setting targets |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | type customModule struct { |
| 334 | android.ModuleBase |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 335 | android.BazelModuleBase |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 336 | |
| 337 | props customProps |
| 338 | } |
| 339 | |
| 340 | // OutputFiles is needed because some instances of this module use dist with a |
| 341 | // tag property which requires the module implements OutputFileProducer. |
| 342 | func (m *customModule) OutputFiles(tag string) (android.Paths, error) { |
| 343 | return android.PathsForTesting("path" + tag), nil |
| 344 | } |
| 345 | |
| 346 | func (m *customModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 347 | // nothing for now. |
| 348 | } |
| 349 | |
| 350 | func customModuleFactoryBase() android.Module { |
| 351 | module := &customModule{} |
| 352 | module.AddProperties(&module.props) |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 353 | android.InitBazelModule(module) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 354 | return module |
| 355 | } |
| 356 | |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 357 | func customModuleFactoryHostAndDevice() android.Module { |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 358 | m := customModuleFactoryBase() |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 359 | android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibBoth) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 360 | return m |
| 361 | } |
| 362 | |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 363 | func customModuleFactoryDeviceSupported() android.Module { |
| 364 | m := customModuleFactoryBase() |
| 365 | android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibBoth) |
| 366 | return m |
| 367 | } |
| 368 | |
| 369 | func customModuleFactoryHostSupported() android.Module { |
| 370 | m := customModuleFactoryBase() |
| 371 | android.InitAndroidArchModule(m, android.HostSupported, android.MultilibBoth) |
| 372 | return m |
| 373 | } |
| 374 | |
| 375 | func customModuleFactoryHostAndDeviceDefault() android.Module { |
| 376 | m := customModuleFactoryBase() |
| 377 | android.InitAndroidArchModule(m, android.HostAndDeviceDefault, android.MultilibBoth) |
| 378 | return m |
| 379 | } |
| 380 | |
| 381 | func customModuleFactoryNeitherHostNorDeviceSupported() android.Module { |
| 382 | m := customModuleFactoryBase() |
| 383 | android.InitAndroidArchModule(m, android.NeitherHostNorDeviceSupported, android.MultilibBoth) |
| 384 | return m |
| 385 | } |
| 386 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 387 | type testProps struct { |
| 388 | Test_prop struct { |
| 389 | Test_string_prop string |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | type customTestModule struct { |
| 394 | android.ModuleBase |
| 395 | |
| 396 | props customProps |
| 397 | test_props testProps |
| 398 | } |
| 399 | |
| 400 | func (m *customTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 401 | // nothing for now. |
| 402 | } |
| 403 | |
| 404 | func customTestModuleFactoryBase() android.Module { |
| 405 | m := &customTestModule{} |
| 406 | m.AddProperties(&m.props) |
| 407 | m.AddProperties(&m.test_props) |
| 408 | return m |
| 409 | } |
| 410 | |
| 411 | func customTestModuleFactory() android.Module { |
| 412 | m := customTestModuleFactoryBase() |
| 413 | android.InitAndroidModule(m) |
| 414 | return m |
| 415 | } |
| 416 | |
| 417 | type customDefaultsModule struct { |
| 418 | android.ModuleBase |
| 419 | android.DefaultsModuleBase |
| 420 | } |
| 421 | |
| 422 | func customDefaultsModuleFactoryBase() android.DefaultsModule { |
| 423 | module := &customDefaultsModule{} |
| 424 | module.AddProperties(&customProps{}) |
| 425 | return module |
| 426 | } |
| 427 | |
| 428 | func customDefaultsModuleFactoryBasic() android.Module { |
| 429 | return customDefaultsModuleFactoryBase() |
| 430 | } |
| 431 | |
| 432 | func customDefaultsModuleFactory() android.Module { |
| 433 | m := customDefaultsModuleFactoryBase() |
| 434 | android.InitDefaultsModule(m) |
| 435 | return m |
| 436 | } |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 437 | |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 438 | type EmbeddedAttr struct { |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 439 | Embedded_attr *string |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | type OtherEmbeddedAttr struct { |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 443 | Other_embedded_attr *string |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 444 | } |
| 445 | |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 446 | type customBazelModuleAttributes struct { |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 447 | EmbeddedAttr |
| 448 | *OtherEmbeddedAttr |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 3a019a6 | 2022-06-23 16:02:44 +0000 | [diff] [blame] | 449 | String_literal_prop bazel.StringAttribute |
| 450 | String_ptr_prop *string |
| 451 | String_list_prop []string |
| 452 | Arch_paths bazel.LabelListAttribute |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 453 | Api bazel.LabelAttribute |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 454 | } |
| 455 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 456 | func (m *customModule) ConvertWithBp2build(ctx android.TopDownMutatorContext) { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 457 | if p := m.props.One_to_many_prop; p != nil && *p { |
| 458 | customBp2buildOneToMany(ctx, m) |
| 459 | return |
| 460 | } |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 461 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 3a019a6 | 2022-06-23 16:02:44 +0000 | [diff] [blame] | 462 | paths := bazel.LabelListAttribute{} |
| 463 | strAttr := bazel.StringAttribute{} |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 464 | for axis, configToProps := range m.GetArchVariantProperties(ctx, &customProps{}) { |
| 465 | 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] | 466 | if custProps, ok := props.(*customProps); ok { |
| 467 | if custProps.Arch_paths != nil { |
| 468 | paths.SetSelectValue(axis, config, android.BazelLabelForModuleSrcExcludes(ctx, custProps.Arch_paths, custProps.Arch_paths_exclude)) |
| 469 | } |
| 470 | if custProps.String_literal_prop != nil { |
| 471 | strAttr.SetSelectValue(axis, config, custProps.String_literal_prop) |
| 472 | } |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 473 | } |
| 474 | } |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 475 | } |
Cole Faust | 912bc88 | 2023-03-08 12:29:50 -0800 | [diff] [blame] | 476 | productVariableProps := android.ProductVariableProperties(ctx, ctx.Module()) |
Liz Kammer | 9d2d410 | 2022-12-21 14:51:37 -0500 | [diff] [blame] | 477 | if props, ok := productVariableProps["String_literal_prop"]; ok { |
| 478 | for c, p := range props { |
| 479 | if val, ok := p.(*string); ok { |
| 480 | strAttr.SetSelectValue(c.ConfigurationAxis(), c.SelectKey(), val) |
| 481 | } |
| 482 | } |
| 483 | } |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 484 | |
| 485 | paths.ResolveExcludes() |
| 486 | |
| 487 | attrs := &customBazelModuleAttributes{ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 3a019a6 | 2022-06-23 16:02:44 +0000 | [diff] [blame] | 488 | String_literal_prop: strAttr, |
| 489 | String_ptr_prop: m.props.String_ptr_prop, |
| 490 | String_list_prop: m.props.String_list_prop, |
| 491 | Arch_paths: paths, |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 492 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 3a019a6 | 2022-06-23 16:02:44 +0000 | [diff] [blame] | 493 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 494 | attrs.Embedded_attr = m.props.Embedded_prop |
| 495 | if m.props.OtherEmbeddedProps != nil { |
| 496 | attrs.OtherEmbeddedAttr = &OtherEmbeddedAttr{Other_embedded_attr: m.props.OtherEmbeddedProps.Other_embedded_prop} |
| 497 | } |
| 498 | |
| 499 | props := bazel.BazelTargetModuleProperties{ |
| 500 | Rule_class: "custom", |
| 501 | } |
| 502 | |
| 503 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, attrs) |
Spandan Das | 6a448ec | 2023-04-19 17:36:12 +0000 | [diff] [blame] | 504 | |
| 505 | if proptools.Bool(m.props.Test_config_setting) { |
| 506 | m.createConfigSetting(ctx) |
| 507 | } |
| 508 | |
| 509 | } |
| 510 | |
| 511 | func (m *customModule) createConfigSetting(ctx android.TopDownMutatorContext) { |
| 512 | csa := bazel.ConfigSettingAttributes{ |
| 513 | Flag_values: bazel.StringMapAttribute{ |
| 514 | "//build/bazel/rules/my_string_setting": m.Name(), |
| 515 | }, |
| 516 | } |
| 517 | ca := android.CommonAttributes{ |
| 518 | Name: m.Name() + "_config_setting", |
| 519 | } |
| 520 | ctx.CreateBazelConfigSetting( |
| 521 | csa, |
| 522 | ca, |
| 523 | ctx.ModuleDir(), |
| 524 | ) |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 525 | } |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 526 | |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 527 | var _ android.ApiProvider = (*customModule)(nil) |
| 528 | |
| 529 | func (c *customModule) ConvertWithApiBp2build(ctx android.TopDownMutatorContext) { |
| 530 | props := bazel.BazelTargetModuleProperties{ |
| 531 | Rule_class: "custom_api_contribution", |
| 532 | } |
| 533 | apiAttribute := bazel.MakeLabelAttribute( |
| 534 | android.BazelLabelForModuleSrcSingle(ctx, proptools.String(c.props.Api)).Label, |
| 535 | ) |
| 536 | attrs := &customBazelModuleAttributes{ |
| 537 | Api: *apiAttribute, |
| 538 | } |
| 539 | ctx.CreateBazelTargetModule(props, |
| 540 | android.CommonAttributes{Name: c.Name()}, |
| 541 | attrs) |
| 542 | } |
| 543 | |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 544 | // A bp2build mutator that uses load statements and creates a 1:M mapping from |
| 545 | // module to target. |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 546 | func customBp2buildOneToMany(ctx android.TopDownMutatorContext, m *customModule) { |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 547 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 548 | baseName := m.Name() |
| 549 | attrs := &customBazelModuleAttributes{} |
Jingwen Chen | 1fd1469 | 2021-02-05 03:01:50 -0500 | [diff] [blame] | 550 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 551 | myLibraryProps := bazel.BazelTargetModuleProperties{ |
| 552 | Rule_class: "my_library", |
| 553 | Bzl_load_location: "//build/bazel/rules:rules.bzl", |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 554 | } |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 555 | ctx.CreateBazelTargetModule(myLibraryProps, android.CommonAttributes{Name: baseName}, attrs) |
| 556 | |
| 557 | protoLibraryProps := bazel.BazelTargetModuleProperties{ |
| 558 | Rule_class: "proto_library", |
| 559 | Bzl_load_location: "//build/bazel/rules:proto.bzl", |
| 560 | } |
| 561 | ctx.CreateBazelTargetModule(protoLibraryProps, android.CommonAttributes{Name: baseName + "_proto_library_deps"}, attrs) |
| 562 | |
| 563 | myProtoLibraryProps := bazel.BazelTargetModuleProperties{ |
| 564 | Rule_class: "my_proto_library", |
| 565 | Bzl_load_location: "//build/bazel/rules:proto.bzl", |
| 566 | } |
| 567 | ctx.CreateBazelTargetModule(myProtoLibraryProps, android.CommonAttributes{Name: baseName + "_my_proto_library_deps"}, attrs) |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 568 | } |
Jingwen Chen | ba369ad | 2021-02-22 10:19:34 -0500 | [diff] [blame] | 569 | |
| 570 | // Helper method for tests to easily access the targets in a dir. |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 571 | func generateBazelTargetsForDir(codegenCtx *CodegenContext, dir string) (BazelTargets, []error) { |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 572 | // TODO: Set generateFilegroups to true and/or remove the generateFilegroups argument completely |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 573 | res, err := GenerateBazelTargets(codegenCtx, false) |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 574 | if err != nil { |
| 575 | return BazelTargets{}, err |
| 576 | } |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 577 | return res.buildFileToTargets[dir], err |
Jingwen Chen | ba369ad | 2021-02-22 10:19:34 -0500 | [diff] [blame] | 578 | } |
Liz Kammer | 32b77cf | 2021-08-04 15:17:02 -0400 | [diff] [blame] | 579 | |
| 580 | func registerCustomModuleForBp2buildConversion(ctx *android.TestContext) { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 581 | ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) |
Liz Kammer | 32b77cf | 2021-08-04 15:17:02 -0400 | [diff] [blame] | 582 | ctx.RegisterForBazelConversion() |
| 583 | } |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 584 | |
| 585 | func simpleModuleDoNotConvertBp2build(typ, name string) string { |
| 586 | return fmt.Sprintf(` |
| 587 | %s { |
| 588 | name: "%s", |
| 589 | bazel_module: { bp2build_available: false }, |
| 590 | }`, typ, name) |
| 591 | } |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 592 | |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 593 | type AttrNameToString map[string]string |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 594 | |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 595 | func (a AttrNameToString) clone() AttrNameToString { |
| 596 | newAttrs := make(AttrNameToString, len(a)) |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 597 | for k, v := range a { |
| 598 | newAttrs[k] = v |
| 599 | } |
| 600 | return newAttrs |
| 601 | } |
| 602 | |
| 603 | // makeBazelTargetNoRestrictions returns bazel target build file definition that can be host or |
| 604 | // device specific, or independent of host/device. |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 605 | func makeBazelTargetHostOrDevice(typ, name string, attrs AttrNameToString, hod android.HostOrDeviceSupported) string { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 606 | if _, ok := attrs["target_compatible_with"]; !ok { |
| 607 | switch hod { |
| 608 | case android.HostSupported: |
| 609 | attrs["target_compatible_with"] = `select({ |
| 610 | "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], |
| 611 | "//conditions:default": [], |
| 612 | })` |
| 613 | case android.DeviceSupported: |
| 614 | attrs["target_compatible_with"] = `["//build/bazel/platforms/os:android"]` |
| 615 | } |
| 616 | } |
| 617 | |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 618 | attrStrings := make([]string, 0, len(attrs)+1) |
Sasha Smundak | fb58949 | 2022-08-04 11:13:27 -0700 | [diff] [blame] | 619 | if name != "" { |
| 620 | attrStrings = append(attrStrings, fmt.Sprintf(` name = "%s",`, name)) |
| 621 | } |
Cole Faust | 18994c7 | 2023-02-28 16:02:16 -0800 | [diff] [blame] | 622 | for _, k := range android.SortedKeys(attrs) { |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 623 | attrStrings = append(attrStrings, fmt.Sprintf(" %s = %s,", k, attrs[k])) |
| 624 | } |
| 625 | return fmt.Sprintf(`%s( |
| 626 | %s |
| 627 | )`, typ, strings.Join(attrStrings, "\n")) |
| 628 | } |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 629 | |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 630 | // MakeBazelTargetNoRestrictions returns bazel target build file definition that does not add a |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 631 | // target_compatible_with. This is useful for module types like filegroup and genrule that arch not |
| 632 | // arch variant |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 633 | func MakeBazelTargetNoRestrictions(typ, name string, attrs AttrNameToString) string { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 634 | return makeBazelTargetHostOrDevice(typ, name, attrs, android.HostAndDeviceDefault) |
| 635 | } |
| 636 | |
| 637 | // makeBazelTargetNoRestrictions returns bazel target build file definition that is device specific |
| 638 | // as this is the most common default in Soong. |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 639 | func MakeBazelTarget(typ, name string, attrs AttrNameToString) string { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 640 | return makeBazelTargetHostOrDevice(typ, name, attrs, android.DeviceSupported) |
| 641 | } |
Sasha Smundak | 9d2f174 | 2022-08-04 13:28:38 -0700 | [diff] [blame] | 642 | |
| 643 | type ExpectedRuleTarget struct { |
| 644 | Rule string |
| 645 | Name string |
| 646 | Attrs AttrNameToString |
| 647 | Hod android.HostOrDeviceSupported |
| 648 | } |
| 649 | |
| 650 | func (ebr ExpectedRuleTarget) String() string { |
| 651 | return makeBazelTargetHostOrDevice(ebr.Rule, ebr.Name, ebr.Attrs, ebr.Hod) |
| 652 | } |
Trevor Radcliffe | 087af54 | 2022-09-16 15:36:10 +0000 | [diff] [blame] | 653 | |
| 654 | func makeCcStubSuiteTargets(name string, attrs AttrNameToString) string { |
| 655 | if _, hasStubs := attrs["stubs_symbol_file"]; !hasStubs { |
| 656 | return "" |
| 657 | } |
| 658 | STUB_SUITE_ATTRS := map[string]string{ |
Sam Delmerico | 5f90649 | 2023-03-15 18:06:18 -0400 | [diff] [blame] | 659 | "stubs_symbol_file": "symbol_file", |
| 660 | "stubs_versions": "versions", |
| 661 | "soname": "soname", |
| 662 | "source_library_label": "source_library_label", |
Trevor Radcliffe | 087af54 | 2022-09-16 15:36:10 +0000 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | stubSuiteAttrs := AttrNameToString{} |
| 666 | for key, _ := range attrs { |
| 667 | if _, stubSuiteAttr := STUB_SUITE_ATTRS[key]; stubSuiteAttr { |
| 668 | stubSuiteAttrs[STUB_SUITE_ATTRS[key]] = attrs[key] |
Sam Delmerico | 5f90649 | 2023-03-15 18:06:18 -0400 | [diff] [blame] | 669 | } else { |
| 670 | panic(fmt.Sprintf("unused cc_stub_suite attr %q\n", key)) |
Trevor Radcliffe | 087af54 | 2022-09-16 15:36:10 +0000 | [diff] [blame] | 671 | } |
| 672 | } |
| 673 | return MakeBazelTarget("cc_stub_suite", name+"_stub_libs", stubSuiteAttrs) |
| 674 | } |
Alix | 341484b | 2022-10-31 19:08:18 +0000 | [diff] [blame] | 675 | |
| 676 | func MakeNeverlinkDuplicateTarget(moduleType string, name string) string { |
Romain Jobredeaux | 2eef2e1 | 2023-02-24 12:07:08 -0500 | [diff] [blame] | 677 | return MakeNeverlinkDuplicateTargetWithAttrs(moduleType, name, AttrNameToString{}) |
| 678 | } |
| 679 | |
| 680 | func MakeNeverlinkDuplicateTargetWithAttrs(moduleType string, name string, extraAttrs AttrNameToString) string { |
| 681 | attrs := extraAttrs |
| 682 | attrs["neverlink"] = `True` |
| 683 | attrs["exports"] = `[":` + name + `"]` |
| 684 | return MakeBazelTarget(moduleType, name+"-neverlink", attrs) |
Alix | 341484b | 2022-10-31 19:08:18 +0000 | [diff] [blame] | 685 | } |
Zi Wang | fba0a21 | 2023-03-07 16:48:19 -0800 | [diff] [blame] | 686 | |
| 687 | func getTargetName(targetContent string) string { |
| 688 | data := strings.Split(targetContent, "name = \"") |
| 689 | if len(data) < 2 { |
| 690 | return "" |
| 691 | } else { |
| 692 | endIndex := strings.Index(data[1], "\"") |
| 693 | return data[1][:endIndex] |
| 694 | } |
| 695 | } |