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 ( |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 23 | "strings" |
Rupert Shuttleworth | 06559d0 | 2021-05-19 09:14:26 -0400 | [diff] [blame] | 24 | "testing" |
| 25 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 26 | "android/soong/android" |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 27 | "android/soong/bazel" |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 28 | ) |
| 29 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 30 | var ( |
| 31 | // A default configuration for tests to not have to specify bp2build_available on top level targets. |
| 32 | bp2buildConfig = android.Bp2BuildConfig{ |
| 33 | android.BP2BUILD_TOPLEVEL: android.Bp2BuildDefaultTrueRecursively, |
| 34 | } |
Rupert Shuttleworth | 06559d0 | 2021-05-19 09:14:26 -0400 | [diff] [blame] | 35 | |
| 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 | |
| 42 | // expectedErr is not nil, find it in the list of errors |
| 43 | if len(errs) != 1 { |
| 44 | t.Errorf("Expected only 1 error, got %d: %q", len(errs), errs) |
| 45 | } |
| 46 | if errs[0].Error() == expectedErr.Error() { |
| 47 | return true |
| 48 | } |
| 49 | |
| 50 | return false |
| 51 | } |
| 52 | |
| 53 | func errored(t *testing.T, tc bp2buildTestCase, errs []error) bool { |
| 54 | t.Helper() |
| 55 | if tc.expectedErr != nil { |
| 56 | // Rely on checkErrors, as this test case is expected to have an error. |
| 57 | return false |
| 58 | } |
| 59 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 60 | if len(errs) > 0 { |
| 61 | for _, err := range errs { |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame^] | 62 | 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] | 63 | } |
| 64 | return true |
| 65 | } |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame^] | 66 | |
| 67 | // 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] | 68 | return false |
| 69 | } |
| 70 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | ce0a07e | 2021-08-23 16:17:32 +0000 | [diff] [blame] | 71 | 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] | 72 | t.Helper() |
| 73 | runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, tc) |
| 74 | } |
| 75 | |
| 76 | type bp2buildTestCase struct { |
| 77 | description string |
| 78 | moduleTypeUnderTest string |
| 79 | moduleTypeUnderTestFactory android.ModuleFactory |
| 80 | moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext) |
| 81 | blueprint string |
| 82 | expectedBazelTargets []string |
| 83 | filesystem map[string]string |
| 84 | dir string |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame^] | 85 | expectedErr error |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | func runBp2BuildTestCase(t *testing.T, registerModuleTypes func(ctx android.RegistrationContext), tc bp2buildTestCase) { |
| 89 | t.Helper() |
| 90 | dir := "." |
| 91 | filesystem := make(map[string][]byte) |
| 92 | toParse := []string{ |
| 93 | "Android.bp", |
| 94 | } |
| 95 | for f, content := range tc.filesystem { |
| 96 | if strings.HasSuffix(f, "Android.bp") { |
| 97 | toParse = append(toParse, f) |
| 98 | } |
| 99 | filesystem[f] = []byte(content) |
| 100 | } |
| 101 | config := android.TestConfig(buildDir, nil, tc.blueprint, filesystem) |
| 102 | ctx := android.NewTestContext(config) |
| 103 | |
| 104 | registerModuleTypes(ctx) |
| 105 | ctx.RegisterModuleType(tc.moduleTypeUnderTest, tc.moduleTypeUnderTestFactory) |
| 106 | ctx.RegisterBp2BuildConfig(bp2buildConfig) |
| 107 | ctx.RegisterBp2BuildMutator(tc.moduleTypeUnderTest, tc.moduleTypeUnderTestBp2BuildMutator) |
| 108 | ctx.RegisterForBazelConversion() |
| 109 | |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame^] | 110 | _, parseErrs := ctx.ParseFileList(dir, toParse) |
| 111 | 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] | 112 | return |
| 113 | } |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame^] | 114 | _, resolveDepsErrs := ctx.ResolveDependencies(config) |
| 115 | if errored(t, tc, resolveDepsErrs) { |
| 116 | return |
| 117 | } |
| 118 | |
| 119 | errs := append(parseErrs, resolveDepsErrs...) |
| 120 | if tc.expectedErr != nil && checkError(t, errs, tc.expectedErr) { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 121 | return |
| 122 | } |
| 123 | |
| 124 | checkDir := dir |
| 125 | if tc.dir != "" { |
| 126 | checkDir = tc.dir |
| 127 | } |
| 128 | codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build) |
| 129 | bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir) |
| 130 | if actualCount, expectedCount := len(bazelTargets), len(tc.expectedBazelTargets); actualCount != expectedCount { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 560cb66 | 2021-08-26 20:13:29 +0000 | [diff] [blame] | 131 | t.Errorf("%s: Expected %d bazel target, got %d; %v", |
| 132 | tc.description, expectedCount, actualCount, bazelTargets) |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 133 | } else { |
| 134 | for i, target := range bazelTargets { |
| 135 | if w, g := tc.expectedBazelTargets[i], target.content; w != g { |
| 136 | t.Errorf( |
| 137 | "%s: Expected generated Bazel target to be '%s', got '%s'", |
| 138 | tc.description, |
| 139 | w, |
| 140 | g, |
| 141 | ) |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 147 | type nestedProps struct { |
| 148 | Nested_prop string |
| 149 | } |
| 150 | |
| 151 | type customProps struct { |
| 152 | Bool_prop bool |
| 153 | Bool_ptr_prop *bool |
| 154 | // Ensure that properties tagged `blueprint:mutated` are omitted |
| 155 | Int_prop int `blueprint:"mutated"` |
| 156 | Int64_ptr_prop *int64 |
| 157 | String_prop string |
| 158 | String_ptr_prop *string |
| 159 | String_list_prop []string |
| 160 | |
| 161 | Nested_props nestedProps |
| 162 | Nested_props_ptr *nestedProps |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 163 | |
Liz Kammer | 32b77cf | 2021-08-04 15:17:02 -0400 | [diff] [blame] | 164 | Arch_paths []string `android:"path,arch_variant"` |
| 165 | Arch_paths_exclude []string `android:"path,arch_variant"` |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | type customModule struct { |
| 169 | android.ModuleBase |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 170 | android.BazelModuleBase |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 171 | |
| 172 | props customProps |
| 173 | } |
| 174 | |
| 175 | // OutputFiles is needed because some instances of this module use dist with a |
| 176 | // tag property which requires the module implements OutputFileProducer. |
| 177 | func (m *customModule) OutputFiles(tag string) (android.Paths, error) { |
| 178 | return android.PathsForTesting("path" + tag), nil |
| 179 | } |
| 180 | |
| 181 | func (m *customModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 182 | // nothing for now. |
| 183 | } |
| 184 | |
| 185 | func customModuleFactoryBase() android.Module { |
| 186 | module := &customModule{} |
| 187 | module.AddProperties(&module.props) |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 188 | android.InitBazelModule(module) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 189 | return module |
| 190 | } |
| 191 | |
| 192 | func customModuleFactory() android.Module { |
| 193 | m := customModuleFactoryBase() |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 194 | android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibBoth) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 195 | return m |
| 196 | } |
| 197 | |
| 198 | type testProps struct { |
| 199 | Test_prop struct { |
| 200 | Test_string_prop string |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | type customTestModule struct { |
| 205 | android.ModuleBase |
| 206 | |
| 207 | props customProps |
| 208 | test_props testProps |
| 209 | } |
| 210 | |
| 211 | func (m *customTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 212 | // nothing for now. |
| 213 | } |
| 214 | |
| 215 | func customTestModuleFactoryBase() android.Module { |
| 216 | m := &customTestModule{} |
| 217 | m.AddProperties(&m.props) |
| 218 | m.AddProperties(&m.test_props) |
| 219 | return m |
| 220 | } |
| 221 | |
| 222 | func customTestModuleFactory() android.Module { |
| 223 | m := customTestModuleFactoryBase() |
| 224 | android.InitAndroidModule(m) |
| 225 | return m |
| 226 | } |
| 227 | |
| 228 | type customDefaultsModule struct { |
| 229 | android.ModuleBase |
| 230 | android.DefaultsModuleBase |
| 231 | } |
| 232 | |
| 233 | func customDefaultsModuleFactoryBase() android.DefaultsModule { |
| 234 | module := &customDefaultsModule{} |
| 235 | module.AddProperties(&customProps{}) |
| 236 | return module |
| 237 | } |
| 238 | |
| 239 | func customDefaultsModuleFactoryBasic() android.Module { |
| 240 | return customDefaultsModuleFactoryBase() |
| 241 | } |
| 242 | |
| 243 | func customDefaultsModuleFactory() android.Module { |
| 244 | m := customDefaultsModuleFactoryBase() |
| 245 | android.InitDefaultsModule(m) |
| 246 | return m |
| 247 | } |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 248 | |
| 249 | type customBazelModuleAttributes struct { |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 250 | String_prop string |
| 251 | String_list_prop []string |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 252 | Arch_paths bazel.LabelListAttribute |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | type customBazelModule struct { |
| 256 | android.BazelTargetModuleBase |
| 257 | customBazelModuleAttributes |
| 258 | } |
| 259 | |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 260 | func customBp2BuildMutator(ctx android.TopDownMutatorContext) { |
| 261 | if m, ok := ctx.Module().(*customModule); ok { |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 262 | if !m.ConvertWithBp2build(ctx) { |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 263 | return |
| 264 | } |
| 265 | |
Liz Kammer | 32b77cf | 2021-08-04 15:17:02 -0400 | [diff] [blame] | 266 | paths := bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrcExcludes(ctx, m.props.Arch_paths, m.props.Arch_paths_exclude)) |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 267 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 268 | for axis, configToProps := range m.GetArchVariantProperties(ctx, &customProps{}) { |
| 269 | for config, props := range configToProps { |
| 270 | if archProps, ok := props.(*customProps); ok && archProps.Arch_paths != nil { |
Liz Kammer | 32b77cf | 2021-08-04 15:17:02 -0400 | [diff] [blame] | 271 | paths.SetSelectValue(axis, config, android.BazelLabelForModuleSrcExcludes(ctx, archProps.Arch_paths, archProps.Arch_paths_exclude)) |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 272 | } |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
Liz Kammer | 32b77cf | 2021-08-04 15:17:02 -0400 | [diff] [blame] | 276 | paths.ResolveExcludes() |
| 277 | |
Jingwen Chen | 1fd1469 | 2021-02-05 03:01:50 -0500 | [diff] [blame] | 278 | attrs := &customBazelModuleAttributes{ |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 279 | String_prop: m.props.String_prop, |
| 280 | String_list_prop: m.props.String_list_prop, |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 281 | Arch_paths: paths, |
Jingwen Chen | 1fd1469 | 2021-02-05 03:01:50 -0500 | [diff] [blame] | 282 | } |
| 283 | |
Liz Kammer | fc46bc1 | 2021-02-19 11:06:17 -0500 | [diff] [blame] | 284 | props := bazel.BazelTargetModuleProperties{ |
| 285 | Rule_class: "custom", |
| 286 | } |
Jingwen Chen | 1fd1469 | 2021-02-05 03:01:50 -0500 | [diff] [blame] | 287 | |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 288 | ctx.CreateBazelTargetModule(m.Name(), props, attrs) |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 289 | } |
| 290 | } |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 291 | |
| 292 | // A bp2build mutator that uses load statements and creates a 1:M mapping from |
| 293 | // module to target. |
| 294 | func customBp2BuildMutatorFromStarlark(ctx android.TopDownMutatorContext) { |
| 295 | if m, ok := ctx.Module().(*customModule); ok { |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 296 | if !m.ConvertWithBp2build(ctx) { |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 297 | return |
| 298 | } |
| 299 | |
Jingwen Chen | 1fd1469 | 2021-02-05 03:01:50 -0500 | [diff] [blame] | 300 | baseName := m.Name() |
| 301 | attrs := &customBazelModuleAttributes{} |
| 302 | |
Liz Kammer | fc46bc1 | 2021-02-19 11:06:17 -0500 | [diff] [blame] | 303 | myLibraryProps := bazel.BazelTargetModuleProperties{ |
| 304 | Rule_class: "my_library", |
| 305 | Bzl_load_location: "//build/bazel/rules:rules.bzl", |
| 306 | } |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 307 | ctx.CreateBazelTargetModule(baseName, myLibraryProps, attrs) |
Jingwen Chen | 1fd1469 | 2021-02-05 03:01:50 -0500 | [diff] [blame] | 308 | |
Liz Kammer | fc46bc1 | 2021-02-19 11:06:17 -0500 | [diff] [blame] | 309 | protoLibraryProps := bazel.BazelTargetModuleProperties{ |
| 310 | Rule_class: "proto_library", |
| 311 | Bzl_load_location: "//build/bazel/rules:proto.bzl", |
| 312 | } |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 313 | ctx.CreateBazelTargetModule(baseName+"_proto_library_deps", protoLibraryProps, attrs) |
Jingwen Chen | 1fd1469 | 2021-02-05 03:01:50 -0500 | [diff] [blame] | 314 | |
Liz Kammer | fc46bc1 | 2021-02-19 11:06:17 -0500 | [diff] [blame] | 315 | myProtoLibraryProps := bazel.BazelTargetModuleProperties{ |
| 316 | Rule_class: "my_proto_library", |
| 317 | Bzl_load_location: "//build/bazel/rules:proto.bzl", |
| 318 | } |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 319 | ctx.CreateBazelTargetModule(baseName+"_my_proto_library_deps", myProtoLibraryProps, attrs) |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 320 | } |
| 321 | } |
Jingwen Chen | ba369ad | 2021-02-22 10:19:34 -0500 | [diff] [blame] | 322 | |
| 323 | // Helper method for tests to easily access the targets in a dir. |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 324 | func generateBazelTargetsForDir(codegenCtx *CodegenContext, dir string) BazelTargets { |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 325 | // TODO: Set generateFilegroups to true and/or remove the generateFilegroups argument completely |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 326 | buildFileToTargets, _, _ := GenerateBazelTargets(codegenCtx, false) |
Jingwen Chen | ba369ad | 2021-02-22 10:19:34 -0500 | [diff] [blame] | 327 | return buildFileToTargets[dir] |
| 328 | } |
Liz Kammer | 32b77cf | 2021-08-04 15:17:02 -0400 | [diff] [blame] | 329 | |
| 330 | func registerCustomModuleForBp2buildConversion(ctx *android.TestContext) { |
| 331 | ctx.RegisterModuleType("custom", customModuleFactory) |
| 332 | ctx.RegisterBp2BuildMutator("custom", customBp2BuildMutator) |
| 333 | ctx.RegisterForBazelConversion() |
| 334 | } |