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