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 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 27 | "android/soong/android" |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 28 | "android/soong/android/allowlists" |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 29 | "android/soong/bazel" |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 30 | ) |
| 31 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 32 | var ( |
| 33 | // A default configuration for tests to not have to specify bp2build_available on top level targets. |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 34 | bp2buildConfig = android.NewBp2BuildAllowlist().SetDefaultConfig( |
| 35 | allowlists.Bp2BuildConfig{ |
| 36 | android.Bp2BuildTopLevel: allowlists.Bp2BuildDefaultTrueRecursively, |
| 37 | }, |
| 38 | ) |
Rupert Shuttleworth | 06559d0 | 2021-05-19 09:14:26 -0400 | [diff] [blame] | 39 | |
| 40 | buildDir string |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 41 | ) |
| 42 | |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 43 | 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] | 44 | t.Helper() |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 45 | |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 46 | if len(errs) != 1 { |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 47 | return false |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 48 | } |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 49 | if strings.Contains(errs[0].Error(), expectedErr.Error()) { |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 50 | return true |
| 51 | } |
| 52 | |
| 53 | return false |
| 54 | } |
| 55 | |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame^] | 56 | func errored(t *testing.T, tc Bp2buildTestCase, errs []error) bool { |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 57 | t.Helper() |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame^] | 58 | if tc.ExpectedErr != nil { |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 59 | // Rely on checkErrors, as this test case is expected to have an error. |
| 60 | return false |
| 61 | } |
| 62 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 63 | if len(errs) > 0 { |
| 64 | for _, err := range errs { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame^] | 65 | 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] | 66 | } |
| 67 | return true |
| 68 | } |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 69 | |
| 70 | // 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] | 71 | return false |
| 72 | } |
| 73 | |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame^] | 74 | 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] | 75 | t.Helper() |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame^] | 76 | 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] | 77 | } |
| 78 | |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame^] | 79 | type Bp2buildTestCase struct { |
| 80 | Description string |
| 81 | ModuleTypeUnderTest string |
| 82 | ModuleTypeUnderTestFactory android.ModuleFactory |
| 83 | Blueprint string |
| 84 | ExpectedBazelTargets []string |
| 85 | Filesystem map[string]string |
| 86 | Dir string |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 87 | // 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^] | 88 | ExpectedErr error |
| 89 | UnconvertedDepsMode unconvertedDepsMode |
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) { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 93 | t.Helper() |
| 94 | dir := "." |
| 95 | filesystem := make(map[string][]byte) |
| 96 | toParse := []string{ |
| 97 | "Android.bp", |
| 98 | } |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame^] | 99 | 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] | 100 | if strings.HasSuffix(f, "Android.bp") { |
| 101 | toParse = append(toParse, f) |
| 102 | } |
| 103 | filesystem[f] = []byte(content) |
| 104 | } |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame^] | 105 | 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] | 106 | ctx := android.NewTestContext(config) |
| 107 | |
| 108 | registerModuleTypes(ctx) |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame^] | 109 | ctx.RegisterModuleType(tc.ModuleTypeUnderTest, tc.ModuleTypeUnderTestFactory) |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 110 | 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] | 111 | ctx.RegisterForBazelConversion() |
| 112 | |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 113 | _, parseErrs := ctx.ParseFileList(dir, toParse) |
| 114 | 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] | 115 | return |
| 116 | } |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 117 | _, resolveDepsErrs := ctx.ResolveDependencies(config) |
| 118 | if errored(t, tc, resolveDepsErrs) { |
| 119 | return |
| 120 | } |
| 121 | |
Cole Faust | b09da7e | 2022-05-18 10:57:33 -0700 | [diff] [blame] | 122 | parseAndResolveErrs := append(parseErrs, resolveDepsErrs...) |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame^] | 123 | 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] | 124 | return |
| 125 | } |
| 126 | |
| 127 | checkDir := dir |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame^] | 128 | if tc.Dir != "" { |
| 129 | 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] | 130 | } |
| 131 | codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build) |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame^] | 132 | codegenCtx.unconvertedDepMode = tc.UnconvertedDepsMode |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 133 | bazelTargets, errs := generateBazelTargetsForDir(codegenCtx, checkDir) |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame^] | 134 | if tc.ExpectedErr != nil { |
| 135 | if checkError(t, errs, tc.ExpectedErr) { |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 136 | return |
| 137 | } else { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame^] | 138 | 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] | 139 | } |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 140 | } else { |
| 141 | android.FailIfErrored(t, errs) |
| 142 | } |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame^] | 143 | if actualCount, expectedCount := len(bazelTargets), len(tc.ExpectedBazelTargets); actualCount != expectedCount { |
Liz Kammer | aabfb5d | 2021-12-08 15:25:06 -0500 | [diff] [blame] | 144 | t.Errorf("%s: Expected %d bazel target (%s), got `%d`` (%s)", |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame^] | 145 | 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] | 146 | } else { |
| 147 | for i, target := range bazelTargets { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame^] | 148 | 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] | 149 | t.Errorf( |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 150 | "%s: Expected generated Bazel target to be `%s`, got `%s`", |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame^] | 151 | 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] | 152 | } |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 157 | type nestedProps struct { |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 158 | Nested_prop *string |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 159 | } |
| 160 | |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 161 | type EmbeddedProps struct { |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 162 | Embedded_prop *string |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | type OtherEmbeddedProps struct { |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 166 | Other_embedded_prop *string |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 167 | } |
| 168 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 169 | type customProps struct { |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 170 | EmbeddedProps |
| 171 | *OtherEmbeddedProps |
| 172 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 173 | Bool_prop bool |
| 174 | Bool_ptr_prop *bool |
| 175 | // Ensure that properties tagged `blueprint:mutated` are omitted |
| 176 | Int_prop int `blueprint:"mutated"` |
| 177 | Int64_ptr_prop *int64 |
| 178 | String_prop string |
| 179 | String_ptr_prop *string |
| 180 | String_list_prop []string |
| 181 | |
| 182 | Nested_props nestedProps |
| 183 | Nested_props_ptr *nestedProps |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 184 | |
Liz Kammer | 32b77cf | 2021-08-04 15:17:02 -0400 | [diff] [blame] | 185 | Arch_paths []string `android:"path,arch_variant"` |
| 186 | Arch_paths_exclude []string `android:"path,arch_variant"` |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 187 | |
| 188 | // Prop used to indicate this conversion should be 1 module -> multiple targets |
| 189 | One_to_many_prop *bool |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | type customModule struct { |
| 193 | android.ModuleBase |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 194 | android.BazelModuleBase |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 195 | |
| 196 | props customProps |
| 197 | } |
| 198 | |
| 199 | // OutputFiles is needed because some instances of this module use dist with a |
| 200 | // tag property which requires the module implements OutputFileProducer. |
| 201 | func (m *customModule) OutputFiles(tag string) (android.Paths, error) { |
| 202 | return android.PathsForTesting("path" + tag), nil |
| 203 | } |
| 204 | |
| 205 | func (m *customModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 206 | // nothing for now. |
| 207 | } |
| 208 | |
| 209 | func customModuleFactoryBase() android.Module { |
| 210 | module := &customModule{} |
| 211 | module.AddProperties(&module.props) |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 212 | android.InitBazelModule(module) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 213 | return module |
| 214 | } |
| 215 | |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 216 | func customModuleFactoryHostAndDevice() android.Module { |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 217 | m := customModuleFactoryBase() |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 218 | android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibBoth) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 219 | return m |
| 220 | } |
| 221 | |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 222 | func customModuleFactoryDeviceSupported() android.Module { |
| 223 | m := customModuleFactoryBase() |
| 224 | android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibBoth) |
| 225 | return m |
| 226 | } |
| 227 | |
| 228 | func customModuleFactoryHostSupported() android.Module { |
| 229 | m := customModuleFactoryBase() |
| 230 | android.InitAndroidArchModule(m, android.HostSupported, android.MultilibBoth) |
| 231 | return m |
| 232 | } |
| 233 | |
| 234 | func customModuleFactoryHostAndDeviceDefault() android.Module { |
| 235 | m := customModuleFactoryBase() |
| 236 | android.InitAndroidArchModule(m, android.HostAndDeviceDefault, android.MultilibBoth) |
| 237 | return m |
| 238 | } |
| 239 | |
| 240 | func customModuleFactoryNeitherHostNorDeviceSupported() android.Module { |
| 241 | m := customModuleFactoryBase() |
| 242 | android.InitAndroidArchModule(m, android.NeitherHostNorDeviceSupported, android.MultilibBoth) |
| 243 | return m |
| 244 | } |
| 245 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 246 | type testProps struct { |
| 247 | Test_prop struct { |
| 248 | Test_string_prop string |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | type customTestModule struct { |
| 253 | android.ModuleBase |
| 254 | |
| 255 | props customProps |
| 256 | test_props testProps |
| 257 | } |
| 258 | |
| 259 | func (m *customTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 260 | // nothing for now. |
| 261 | } |
| 262 | |
| 263 | func customTestModuleFactoryBase() android.Module { |
| 264 | m := &customTestModule{} |
| 265 | m.AddProperties(&m.props) |
| 266 | m.AddProperties(&m.test_props) |
| 267 | return m |
| 268 | } |
| 269 | |
| 270 | func customTestModuleFactory() android.Module { |
| 271 | m := customTestModuleFactoryBase() |
| 272 | android.InitAndroidModule(m) |
| 273 | return m |
| 274 | } |
| 275 | |
| 276 | type customDefaultsModule struct { |
| 277 | android.ModuleBase |
| 278 | android.DefaultsModuleBase |
| 279 | } |
| 280 | |
| 281 | func customDefaultsModuleFactoryBase() android.DefaultsModule { |
| 282 | module := &customDefaultsModule{} |
| 283 | module.AddProperties(&customProps{}) |
| 284 | return module |
| 285 | } |
| 286 | |
| 287 | func customDefaultsModuleFactoryBasic() android.Module { |
| 288 | return customDefaultsModuleFactoryBase() |
| 289 | } |
| 290 | |
| 291 | func customDefaultsModuleFactory() android.Module { |
| 292 | m := customDefaultsModuleFactoryBase() |
| 293 | android.InitDefaultsModule(m) |
| 294 | return m |
| 295 | } |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 296 | |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 297 | type EmbeddedAttr struct { |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 298 | Embedded_attr *string |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | type OtherEmbeddedAttr struct { |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 302 | Other_embedded_attr *string |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 303 | } |
| 304 | |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 305 | type customBazelModuleAttributes struct { |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 306 | EmbeddedAttr |
| 307 | *OtherEmbeddedAttr |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 308 | String_ptr_prop *string |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 309 | String_list_prop []string |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 310 | Arch_paths bazel.LabelListAttribute |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 311 | } |
| 312 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 313 | func (m *customModule) ConvertWithBp2build(ctx android.TopDownMutatorContext) { |
| 314 | paths := bazel.LabelListAttribute{} |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 315 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 316 | if p := m.props.One_to_many_prop; p != nil && *p { |
| 317 | customBp2buildOneToMany(ctx, m) |
| 318 | return |
| 319 | } |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 320 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 321 | for axis, configToProps := range m.GetArchVariantProperties(ctx, &customProps{}) { |
| 322 | for config, props := range configToProps { |
| 323 | if archProps, ok := props.(*customProps); ok && archProps.Arch_paths != nil { |
| 324 | paths.SetSelectValue(axis, config, android.BazelLabelForModuleSrcExcludes(ctx, archProps.Arch_paths, archProps.Arch_paths_exclude)) |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 325 | } |
| 326 | } |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 327 | } |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 328 | |
| 329 | paths.ResolveExcludes() |
| 330 | |
| 331 | attrs := &customBazelModuleAttributes{ |
| 332 | String_ptr_prop: m.props.String_ptr_prop, |
| 333 | String_list_prop: m.props.String_list_prop, |
| 334 | Arch_paths: paths, |
| 335 | } |
| 336 | attrs.Embedded_attr = m.props.Embedded_prop |
| 337 | if m.props.OtherEmbeddedProps != nil { |
| 338 | attrs.OtherEmbeddedAttr = &OtherEmbeddedAttr{Other_embedded_attr: m.props.OtherEmbeddedProps.Other_embedded_prop} |
| 339 | } |
| 340 | |
| 341 | props := bazel.BazelTargetModuleProperties{ |
| 342 | Rule_class: "custom", |
| 343 | } |
| 344 | |
| 345 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, attrs) |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 346 | } |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 347 | |
| 348 | // A bp2build mutator that uses load statements and creates a 1:M mapping from |
| 349 | // module to target. |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 350 | func customBp2buildOneToMany(ctx android.TopDownMutatorContext, m *customModule) { |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 351 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 352 | baseName := m.Name() |
| 353 | attrs := &customBazelModuleAttributes{} |
Jingwen Chen | 1fd1469 | 2021-02-05 03:01:50 -0500 | [diff] [blame] | 354 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 355 | myLibraryProps := bazel.BazelTargetModuleProperties{ |
| 356 | Rule_class: "my_library", |
| 357 | Bzl_load_location: "//build/bazel/rules:rules.bzl", |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 358 | } |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 359 | ctx.CreateBazelTargetModule(myLibraryProps, android.CommonAttributes{Name: baseName}, attrs) |
| 360 | |
| 361 | protoLibraryProps := bazel.BazelTargetModuleProperties{ |
| 362 | Rule_class: "proto_library", |
| 363 | Bzl_load_location: "//build/bazel/rules:proto.bzl", |
| 364 | } |
| 365 | ctx.CreateBazelTargetModule(protoLibraryProps, android.CommonAttributes{Name: baseName + "_proto_library_deps"}, attrs) |
| 366 | |
| 367 | myProtoLibraryProps := bazel.BazelTargetModuleProperties{ |
| 368 | Rule_class: "my_proto_library", |
| 369 | Bzl_load_location: "//build/bazel/rules:proto.bzl", |
| 370 | } |
| 371 | ctx.CreateBazelTargetModule(myProtoLibraryProps, android.CommonAttributes{Name: baseName + "_my_proto_library_deps"}, attrs) |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 372 | } |
Jingwen Chen | ba369ad | 2021-02-22 10:19:34 -0500 | [diff] [blame] | 373 | |
| 374 | // Helper method for tests to easily access the targets in a dir. |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 375 | func generateBazelTargetsForDir(codegenCtx *CodegenContext, dir string) (BazelTargets, []error) { |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 376 | // TODO: Set generateFilegroups to true and/or remove the generateFilegroups argument completely |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 377 | res, err := GenerateBazelTargets(codegenCtx, false) |
| 378 | return res.buildFileToTargets[dir], err |
Jingwen Chen | ba369ad | 2021-02-22 10:19:34 -0500 | [diff] [blame] | 379 | } |
Liz Kammer | 32b77cf | 2021-08-04 15:17:02 -0400 | [diff] [blame] | 380 | |
| 381 | func registerCustomModuleForBp2buildConversion(ctx *android.TestContext) { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 382 | ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) |
Liz Kammer | 32b77cf | 2021-08-04 15:17:02 -0400 | [diff] [blame] | 383 | ctx.RegisterForBazelConversion() |
| 384 | } |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 385 | |
| 386 | func simpleModuleDoNotConvertBp2build(typ, name string) string { |
| 387 | return fmt.Sprintf(` |
| 388 | %s { |
| 389 | name: "%s", |
| 390 | bazel_module: { bp2build_available: false }, |
| 391 | }`, typ, name) |
| 392 | } |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 393 | |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame^] | 394 | type AttrNameToString map[string]string |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 395 | |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame^] | 396 | func (a AttrNameToString) clone() AttrNameToString { |
| 397 | newAttrs := make(AttrNameToString, len(a)) |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 398 | for k, v := range a { |
| 399 | newAttrs[k] = v |
| 400 | } |
| 401 | return newAttrs |
| 402 | } |
| 403 | |
| 404 | // makeBazelTargetNoRestrictions returns bazel target build file definition that can be host or |
| 405 | // device specific, or independent of host/device. |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame^] | 406 | func makeBazelTargetHostOrDevice(typ, name string, attrs AttrNameToString, hod android.HostOrDeviceSupported) string { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 407 | if _, ok := attrs["target_compatible_with"]; !ok { |
| 408 | switch hod { |
| 409 | case android.HostSupported: |
| 410 | attrs["target_compatible_with"] = `select({ |
| 411 | "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], |
| 412 | "//conditions:default": [], |
| 413 | })` |
| 414 | case android.DeviceSupported: |
| 415 | attrs["target_compatible_with"] = `["//build/bazel/platforms/os:android"]` |
| 416 | } |
| 417 | } |
| 418 | |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 419 | attrStrings := make([]string, 0, len(attrs)+1) |
| 420 | attrStrings = append(attrStrings, fmt.Sprintf(` name = "%s",`, name)) |
| 421 | for _, k := range android.SortedStringKeys(attrs) { |
| 422 | attrStrings = append(attrStrings, fmt.Sprintf(" %s = %s,", k, attrs[k])) |
| 423 | } |
| 424 | return fmt.Sprintf(`%s( |
| 425 | %s |
| 426 | )`, typ, strings.Join(attrStrings, "\n")) |
| 427 | } |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 428 | |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame^] | 429 | // MakeBazelTargetNoRestrictions returns bazel target build file definition that does not add a |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 430 | // target_compatible_with. This is useful for module types like filegroup and genrule that arch not |
| 431 | // arch variant |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame^] | 432 | func MakeBazelTargetNoRestrictions(typ, name string, attrs AttrNameToString) string { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 433 | return makeBazelTargetHostOrDevice(typ, name, attrs, android.HostAndDeviceDefault) |
| 434 | } |
| 435 | |
| 436 | // makeBazelTargetNoRestrictions returns bazel target build file definition that is device specific |
| 437 | // as this is the most common default in Soong. |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame^] | 438 | func makeBazelTarget(typ, name string, attrs AttrNameToString) string { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 439 | return makeBazelTargetHostOrDevice(typ, name, attrs, android.DeviceSupported) |
| 440 | } |