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