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() |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 94 | bp2buildSetup := func(ctx *android.TestContext) { |
| 95 | registerModuleTypes(ctx) |
| 96 | ctx.RegisterForBazelConversion() |
| 97 | } |
| 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() |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 103 | apiBp2BuildSetup := func(ctx *android.TestContext) { |
| 104 | registerModuleTypes(ctx) |
| 105 | ctx.RegisterForApiBazelConversion() |
| 106 | } |
| 107 | runBp2BuildTestCaseWithSetup(t, apiBp2BuildSetup, tc) |
| 108 | } |
| 109 | |
| 110 | func runBp2BuildTestCaseWithSetup(t *testing.T, setup func(ctx *android.TestContext), 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) |
| 114 | toParse := []string{ |
| 115 | "Android.bp", |
| 116 | } |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 117 | 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] | 118 | if strings.HasSuffix(f, "Android.bp") { |
| 119 | toParse = append(toParse, f) |
| 120 | } |
| 121 | filesystem[f] = []byte(content) |
| 122 | } |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 123 | config := android.TestConfig(buildDir, nil, tc.Blueprint, filesystem) |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 124 | ctx := android.NewTestContext(config) |
| 125 | |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 126 | setup(ctx) |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 127 | ctx.RegisterModuleType(tc.ModuleTypeUnderTest, tc.ModuleTypeUnderTestFactory) |
Jingwen Chen | 0eeaeb8 | 2022-09-21 10:27:42 +0000 | [diff] [blame] | 128 | |
| 129 | // A default configuration for tests to not have to specify bp2build_available on top level targets. |
| 130 | bp2buildConfig := android.NewBp2BuildAllowlist().SetDefaultConfig( |
| 131 | allowlists.Bp2BuildConfig{ |
| 132 | android.Bp2BuildTopLevel: allowlists.Bp2BuildDefaultTrueRecursively, |
| 133 | }, |
| 134 | ) |
| 135 | for _, f := range tc.KeepBuildFileForDirs { |
| 136 | bp2buildConfig.SetKeepExistingBuildFile(map[string]bool{ |
| 137 | f: /*recursive=*/ false, |
| 138 | }) |
| 139 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 140 | ctx.RegisterBp2BuildConfig(bp2buildConfig) |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 141 | |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 142 | _, parseErrs := ctx.ParseFileList(dir, toParse) |
| 143 | if errored(t, tc, parseErrs) { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 144 | return |
| 145 | } |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 146 | _, resolveDepsErrs := ctx.ResolveDependencies(config) |
| 147 | if errored(t, tc, resolveDepsErrs) { |
| 148 | return |
| 149 | } |
| 150 | |
Cole Faust | b09da7e | 2022-05-18 10:57:33 -0700 | [diff] [blame] | 151 | parseAndResolveErrs := append(parseErrs, resolveDepsErrs...) |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 152 | if tc.ExpectedErr != nil && checkError(t, parseAndResolveErrs, tc.ExpectedErr) { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 153 | return |
| 154 | } |
| 155 | |
| 156 | checkDir := dir |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 157 | if tc.Dir != "" { |
| 158 | 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] | 159 | } |
Paul Duffin | c639059 | 2022-11-04 13:35:21 +0000 | [diff] [blame] | 160 | codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build) |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 161 | codegenCtx.unconvertedDepMode = tc.UnconvertedDepsMode |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 162 | bazelTargets, errs := generateBazelTargetsForDir(codegenCtx, checkDir) |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 163 | if tc.ExpectedErr != nil { |
| 164 | if checkError(t, errs, tc.ExpectedErr) { |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 165 | return |
| 166 | } else { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 167 | t.Errorf("Expected error: %q, got: %q and %q", tc.ExpectedErr, errs, parseAndResolveErrs) |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 168 | } |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 169 | } else { |
| 170 | android.FailIfErrored(t, errs) |
| 171 | } |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 172 | if actualCount, expectedCount := len(bazelTargets), len(tc.ExpectedBazelTargets); actualCount != expectedCount { |
Sasha Smundak | 9d2f174 | 2022-08-04 13:28:38 -0700 | [diff] [blame] | 173 | t.Errorf("%s: Expected %d bazel target (%s), got %d (%s)", |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 174 | tc.Description, expectedCount, tc.ExpectedBazelTargets, actualCount, bazelTargets) |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 175 | } else { |
| 176 | for i, target := range bazelTargets { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 177 | if w, g := tc.ExpectedBazelTargets[i], target.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] | 178 | t.Errorf( |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 179 | "%s: Expected generated Bazel target to be `%s`, got `%s`", |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 180 | tc.Description, w, g) |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 186 | type nestedProps struct { |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 187 | Nested_prop *string |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 188 | } |
| 189 | |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 190 | type EmbeddedProps struct { |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 191 | Embedded_prop *string |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | type OtherEmbeddedProps struct { |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 195 | Other_embedded_prop *string |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 196 | } |
| 197 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 198 | type customProps struct { |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 199 | EmbeddedProps |
| 200 | *OtherEmbeddedProps |
| 201 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 202 | Bool_prop bool |
| 203 | Bool_ptr_prop *bool |
| 204 | // 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] | 205 | Int_prop int `blueprint:"mutated"` |
| 206 | Int64_ptr_prop *int64 |
| 207 | String_prop string |
| 208 | String_literal_prop *string `android:"arch_variant"` |
| 209 | String_ptr_prop *string |
| 210 | String_list_prop []string |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 211 | |
| 212 | Nested_props nestedProps |
| 213 | Nested_props_ptr *nestedProps |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 214 | |
Liz Kammer | 32b77cf | 2021-08-04 15:17:02 -0400 | [diff] [blame] | 215 | Arch_paths []string `android:"path,arch_variant"` |
| 216 | Arch_paths_exclude []string `android:"path,arch_variant"` |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 217 | |
| 218 | // Prop used to indicate this conversion should be 1 module -> multiple targets |
| 219 | One_to_many_prop *bool |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 220 | |
| 221 | Api *string // File describing the APIs of this module |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | type customModule struct { |
| 225 | android.ModuleBase |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 226 | android.BazelModuleBase |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 227 | |
| 228 | props customProps |
| 229 | } |
| 230 | |
| 231 | // OutputFiles is needed because some instances of this module use dist with a |
| 232 | // tag property which requires the module implements OutputFileProducer. |
| 233 | func (m *customModule) OutputFiles(tag string) (android.Paths, error) { |
| 234 | return android.PathsForTesting("path" + tag), nil |
| 235 | } |
| 236 | |
| 237 | func (m *customModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 238 | // nothing for now. |
| 239 | } |
| 240 | |
| 241 | func customModuleFactoryBase() android.Module { |
| 242 | module := &customModule{} |
| 243 | module.AddProperties(&module.props) |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 244 | android.InitBazelModule(module) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 245 | return module |
| 246 | } |
| 247 | |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 248 | func customModuleFactoryHostAndDevice() android.Module { |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 249 | m := customModuleFactoryBase() |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 250 | android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibBoth) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 251 | return m |
| 252 | } |
| 253 | |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 254 | func customModuleFactoryDeviceSupported() android.Module { |
| 255 | m := customModuleFactoryBase() |
| 256 | android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibBoth) |
| 257 | return m |
| 258 | } |
| 259 | |
| 260 | func customModuleFactoryHostSupported() android.Module { |
| 261 | m := customModuleFactoryBase() |
| 262 | android.InitAndroidArchModule(m, android.HostSupported, android.MultilibBoth) |
| 263 | return m |
| 264 | } |
| 265 | |
| 266 | func customModuleFactoryHostAndDeviceDefault() android.Module { |
| 267 | m := customModuleFactoryBase() |
| 268 | android.InitAndroidArchModule(m, android.HostAndDeviceDefault, android.MultilibBoth) |
| 269 | return m |
| 270 | } |
| 271 | |
| 272 | func customModuleFactoryNeitherHostNorDeviceSupported() android.Module { |
| 273 | m := customModuleFactoryBase() |
| 274 | android.InitAndroidArchModule(m, android.NeitherHostNorDeviceSupported, android.MultilibBoth) |
| 275 | return m |
| 276 | } |
| 277 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 278 | type testProps struct { |
| 279 | Test_prop struct { |
| 280 | Test_string_prop string |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | type customTestModule struct { |
| 285 | android.ModuleBase |
| 286 | |
| 287 | props customProps |
| 288 | test_props testProps |
| 289 | } |
| 290 | |
| 291 | func (m *customTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 292 | // nothing for now. |
| 293 | } |
| 294 | |
| 295 | func customTestModuleFactoryBase() android.Module { |
| 296 | m := &customTestModule{} |
| 297 | m.AddProperties(&m.props) |
| 298 | m.AddProperties(&m.test_props) |
| 299 | return m |
| 300 | } |
| 301 | |
| 302 | func customTestModuleFactory() android.Module { |
| 303 | m := customTestModuleFactoryBase() |
| 304 | android.InitAndroidModule(m) |
| 305 | return m |
| 306 | } |
| 307 | |
| 308 | type customDefaultsModule struct { |
| 309 | android.ModuleBase |
| 310 | android.DefaultsModuleBase |
| 311 | } |
| 312 | |
| 313 | func customDefaultsModuleFactoryBase() android.DefaultsModule { |
| 314 | module := &customDefaultsModule{} |
| 315 | module.AddProperties(&customProps{}) |
| 316 | return module |
| 317 | } |
| 318 | |
| 319 | func customDefaultsModuleFactoryBasic() android.Module { |
| 320 | return customDefaultsModuleFactoryBase() |
| 321 | } |
| 322 | |
| 323 | func customDefaultsModuleFactory() android.Module { |
| 324 | m := customDefaultsModuleFactoryBase() |
| 325 | android.InitDefaultsModule(m) |
| 326 | return m |
| 327 | } |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 328 | |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 329 | type EmbeddedAttr struct { |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 330 | Embedded_attr *string |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | type OtherEmbeddedAttr struct { |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 334 | Other_embedded_attr *string |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 335 | } |
| 336 | |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 337 | type customBazelModuleAttributes struct { |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 338 | EmbeddedAttr |
| 339 | *OtherEmbeddedAttr |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 3a019a6 | 2022-06-23 16:02:44 +0000 | [diff] [blame] | 340 | String_literal_prop bazel.StringAttribute |
| 341 | String_ptr_prop *string |
| 342 | String_list_prop []string |
| 343 | Arch_paths bazel.LabelListAttribute |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 344 | Api bazel.LabelAttribute |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 345 | } |
| 346 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 347 | func (m *customModule) ConvertWithBp2build(ctx android.TopDownMutatorContext) { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 348 | if p := m.props.One_to_many_prop; p != nil && *p { |
| 349 | customBp2buildOneToMany(ctx, m) |
| 350 | return |
| 351 | } |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 352 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 3a019a6 | 2022-06-23 16:02:44 +0000 | [diff] [blame] | 353 | paths := bazel.LabelListAttribute{} |
| 354 | strAttr := bazel.StringAttribute{} |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 355 | for axis, configToProps := range m.GetArchVariantProperties(ctx, &customProps{}) { |
| 356 | 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] | 357 | if custProps, ok := props.(*customProps); ok { |
| 358 | if custProps.Arch_paths != nil { |
| 359 | paths.SetSelectValue(axis, config, android.BazelLabelForModuleSrcExcludes(ctx, custProps.Arch_paths, custProps.Arch_paths_exclude)) |
| 360 | } |
| 361 | if custProps.String_literal_prop != nil { |
| 362 | strAttr.SetSelectValue(axis, config, custProps.String_literal_prop) |
| 363 | } |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 364 | } |
| 365 | } |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 366 | } |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 367 | |
| 368 | paths.ResolveExcludes() |
| 369 | |
| 370 | attrs := &customBazelModuleAttributes{ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 3a019a6 | 2022-06-23 16:02:44 +0000 | [diff] [blame] | 371 | String_literal_prop: strAttr, |
| 372 | String_ptr_prop: m.props.String_ptr_prop, |
| 373 | String_list_prop: m.props.String_list_prop, |
| 374 | Arch_paths: paths, |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 375 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 3a019a6 | 2022-06-23 16:02:44 +0000 | [diff] [blame] | 376 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 377 | attrs.Embedded_attr = m.props.Embedded_prop |
| 378 | if m.props.OtherEmbeddedProps != nil { |
| 379 | attrs.OtherEmbeddedAttr = &OtherEmbeddedAttr{Other_embedded_attr: m.props.OtherEmbeddedProps.Other_embedded_prop} |
| 380 | } |
| 381 | |
| 382 | props := bazel.BazelTargetModuleProperties{ |
| 383 | Rule_class: "custom", |
| 384 | } |
| 385 | |
| 386 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, attrs) |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 387 | } |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 388 | |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 389 | var _ android.ApiProvider = (*customModule)(nil) |
| 390 | |
| 391 | func (c *customModule) ConvertWithApiBp2build(ctx android.TopDownMutatorContext) { |
| 392 | props := bazel.BazelTargetModuleProperties{ |
| 393 | Rule_class: "custom_api_contribution", |
| 394 | } |
| 395 | apiAttribute := bazel.MakeLabelAttribute( |
| 396 | android.BazelLabelForModuleSrcSingle(ctx, proptools.String(c.props.Api)).Label, |
| 397 | ) |
| 398 | attrs := &customBazelModuleAttributes{ |
| 399 | Api: *apiAttribute, |
| 400 | } |
| 401 | ctx.CreateBazelTargetModule(props, |
| 402 | android.CommonAttributes{Name: c.Name()}, |
| 403 | attrs) |
| 404 | } |
| 405 | |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 406 | // A bp2build mutator that uses load statements and creates a 1:M mapping from |
| 407 | // module to target. |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 408 | func customBp2buildOneToMany(ctx android.TopDownMutatorContext, m *customModule) { |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 409 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 410 | baseName := m.Name() |
| 411 | attrs := &customBazelModuleAttributes{} |
Jingwen Chen | 1fd1469 | 2021-02-05 03:01:50 -0500 | [diff] [blame] | 412 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 413 | myLibraryProps := bazel.BazelTargetModuleProperties{ |
| 414 | Rule_class: "my_library", |
| 415 | Bzl_load_location: "//build/bazel/rules:rules.bzl", |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 416 | } |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 417 | ctx.CreateBazelTargetModule(myLibraryProps, android.CommonAttributes{Name: baseName}, attrs) |
| 418 | |
| 419 | protoLibraryProps := bazel.BazelTargetModuleProperties{ |
| 420 | Rule_class: "proto_library", |
| 421 | Bzl_load_location: "//build/bazel/rules:proto.bzl", |
| 422 | } |
| 423 | ctx.CreateBazelTargetModule(protoLibraryProps, android.CommonAttributes{Name: baseName + "_proto_library_deps"}, attrs) |
| 424 | |
| 425 | myProtoLibraryProps := bazel.BazelTargetModuleProperties{ |
| 426 | Rule_class: "my_proto_library", |
| 427 | Bzl_load_location: "//build/bazel/rules:proto.bzl", |
| 428 | } |
| 429 | ctx.CreateBazelTargetModule(myProtoLibraryProps, android.CommonAttributes{Name: baseName + "_my_proto_library_deps"}, attrs) |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 430 | } |
Jingwen Chen | ba369ad | 2021-02-22 10:19:34 -0500 | [diff] [blame] | 431 | |
| 432 | // Helper method for tests to easily access the targets in a dir. |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 433 | func generateBazelTargetsForDir(codegenCtx *CodegenContext, dir string) (BazelTargets, []error) { |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 434 | // TODO: Set generateFilegroups to true and/or remove the generateFilegroups argument completely |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 435 | res, err := GenerateBazelTargets(codegenCtx, false) |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 436 | if err != nil { |
| 437 | return BazelTargets{}, err |
| 438 | } |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 439 | return res.buildFileToTargets[dir], err |
Jingwen Chen | ba369ad | 2021-02-22 10:19:34 -0500 | [diff] [blame] | 440 | } |
Liz Kammer | 32b77cf | 2021-08-04 15:17:02 -0400 | [diff] [blame] | 441 | |
| 442 | func registerCustomModuleForBp2buildConversion(ctx *android.TestContext) { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 443 | ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) |
Liz Kammer | 32b77cf | 2021-08-04 15:17:02 -0400 | [diff] [blame] | 444 | ctx.RegisterForBazelConversion() |
| 445 | } |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 446 | |
| 447 | func simpleModuleDoNotConvertBp2build(typ, name string) string { |
| 448 | return fmt.Sprintf(` |
| 449 | %s { |
| 450 | name: "%s", |
| 451 | bazel_module: { bp2build_available: false }, |
| 452 | }`, typ, name) |
| 453 | } |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 454 | |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 455 | type AttrNameToString map[string]string |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 456 | |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 457 | func (a AttrNameToString) clone() AttrNameToString { |
| 458 | newAttrs := make(AttrNameToString, len(a)) |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 459 | for k, v := range a { |
| 460 | newAttrs[k] = v |
| 461 | } |
| 462 | return newAttrs |
| 463 | } |
| 464 | |
| 465 | // makeBazelTargetNoRestrictions returns bazel target build file definition that can be host or |
| 466 | // device specific, or independent of host/device. |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 467 | func makeBazelTargetHostOrDevice(typ, name string, attrs AttrNameToString, hod android.HostOrDeviceSupported) string { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 468 | if _, ok := attrs["target_compatible_with"]; !ok { |
| 469 | switch hod { |
| 470 | case android.HostSupported: |
| 471 | attrs["target_compatible_with"] = `select({ |
| 472 | "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], |
| 473 | "//conditions:default": [], |
| 474 | })` |
| 475 | case android.DeviceSupported: |
| 476 | attrs["target_compatible_with"] = `["//build/bazel/platforms/os:android"]` |
| 477 | } |
| 478 | } |
| 479 | |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 480 | attrStrings := make([]string, 0, len(attrs)+1) |
Sasha Smundak | fb58949 | 2022-08-04 11:13:27 -0700 | [diff] [blame] | 481 | if name != "" { |
| 482 | attrStrings = append(attrStrings, fmt.Sprintf(` name = "%s",`, name)) |
| 483 | } |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 484 | for _, k := range android.SortedStringKeys(attrs) { |
| 485 | attrStrings = append(attrStrings, fmt.Sprintf(" %s = %s,", k, attrs[k])) |
| 486 | } |
| 487 | return fmt.Sprintf(`%s( |
| 488 | %s |
| 489 | )`, typ, strings.Join(attrStrings, "\n")) |
| 490 | } |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 491 | |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 492 | // MakeBazelTargetNoRestrictions returns bazel target build file definition that does not add a |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 493 | // target_compatible_with. This is useful for module types like filegroup and genrule that arch not |
| 494 | // arch variant |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 495 | func MakeBazelTargetNoRestrictions(typ, name string, attrs AttrNameToString) string { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 496 | return makeBazelTargetHostOrDevice(typ, name, attrs, android.HostAndDeviceDefault) |
| 497 | } |
| 498 | |
| 499 | // makeBazelTargetNoRestrictions returns bazel target build file definition that is device specific |
| 500 | // as this is the most common default in Soong. |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 501 | func MakeBazelTarget(typ, name string, attrs AttrNameToString) string { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 502 | return makeBazelTargetHostOrDevice(typ, name, attrs, android.DeviceSupported) |
| 503 | } |
Sasha Smundak | 9d2f174 | 2022-08-04 13:28:38 -0700 | [diff] [blame] | 504 | |
| 505 | type ExpectedRuleTarget struct { |
| 506 | Rule string |
| 507 | Name string |
| 508 | Attrs AttrNameToString |
| 509 | Hod android.HostOrDeviceSupported |
| 510 | } |
| 511 | |
| 512 | func (ebr ExpectedRuleTarget) String() string { |
| 513 | return makeBazelTargetHostOrDevice(ebr.Rule, ebr.Name, ebr.Attrs, ebr.Hod) |
| 514 | } |
Trevor Radcliffe | 087af54 | 2022-09-16 15:36:10 +0000 | [diff] [blame] | 515 | |
| 516 | func makeCcStubSuiteTargets(name string, attrs AttrNameToString) string { |
| 517 | if _, hasStubs := attrs["stubs_symbol_file"]; !hasStubs { |
| 518 | return "" |
| 519 | } |
| 520 | STUB_SUITE_ATTRS := map[string]string{ |
| 521 | "stubs_symbol_file": "symbol_file", |
| 522 | "stubs_versions": "versions", |
| 523 | "soname": "soname", |
| 524 | "source_library": "source_library", |
| 525 | } |
| 526 | |
| 527 | stubSuiteAttrs := AttrNameToString{} |
| 528 | for key, _ := range attrs { |
| 529 | if _, stubSuiteAttr := STUB_SUITE_ATTRS[key]; stubSuiteAttr { |
| 530 | stubSuiteAttrs[STUB_SUITE_ATTRS[key]] = attrs[key] |
| 531 | } |
| 532 | } |
| 533 | return MakeBazelTarget("cc_stub_suite", name+"_stub_libs", stubSuiteAttrs) |
| 534 | } |