blob: 53b60fa81a1e159fcafa97ffa432e515781bdadf [file] [log] [blame]
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxf5a3eac2021-08-23 17:05:17 +00001// 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 Kammer2dd9ca42020-11-25 16:06:39 -080015package bp2build
16
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0da7ce62021-08-23 17:04:20 +000017/*
18For shareable/common bp2build testing functionality and dumping ground for
19specific-but-shared functionality among tests in package
20*/
21
Liz Kammer2dd9ca42020-11-25 16:06:39 -080022import (
Liz Kammer7a210ac2021-09-22 15:52:58 -040023 "fmt"
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +000024 "strings"
Rupert Shuttleworth06559d02021-05-19 09:14:26 -040025 "testing"
26
Liz Kammer2dd9ca42020-11-25 16:06:39 -080027 "android/soong/android"
Jingwen Chen73850672020-12-14 08:25:34 -050028 "android/soong/bazel"
Liz Kammer2dd9ca42020-11-25 16:06:39 -080029)
30
Jingwen Chen91220d72021-03-24 02:18:33 -040031var (
32 // A default configuration for tests to not have to specify bp2build_available on top level targets.
33 bp2buildConfig = android.Bp2BuildConfig{
34 android.BP2BUILD_TOPLEVEL: android.Bp2BuildDefaultTrueRecursively,
35 }
Rupert Shuttleworth06559d02021-05-19 09:14:26 -040036
37 buildDir string
Jingwen Chen91220d72021-03-24 02:18:33 -040038)
39
Jingwen Chen5146ac02021-09-02 11:44:42 +000040func checkError(t *testing.T, errs []error, expectedErr error) bool {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +000041 t.Helper()
Jingwen Chen5146ac02021-09-02 11:44:42 +000042
Jingwen Chen5146ac02021-09-02 11:44:42 +000043 if len(errs) != 1 {
Liz Kammer6eff3232021-08-26 08:37:59 -040044 return false
Jingwen Chen5146ac02021-09-02 11:44:42 +000045 }
Liz Kammer54309532021-12-14 12:21:22 -050046 if strings.Contains(errs[0].Error(), expectedErr.Error()) {
Jingwen Chen5146ac02021-09-02 11:44:42 +000047 return true
48 }
49
50 return false
51}
52
53func 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 Thaureaux1c92aef2021-08-23 16:10:00 +000060 if len(errs) > 0 {
61 for _, err := range errs {
Jingwen Chen5146ac02021-09-02 11:44:42 +000062 t.Errorf("%s: %s", tc.description, err)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +000063 }
64 return true
65 }
Jingwen Chen5146ac02021-09-02 11:44:42 +000066
67 // All good, continue execution.
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +000068 return false
69}
70
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxce0a07e2021-08-23 16:17:32 +000071func runBp2BuildTestCaseSimple(t *testing.T, tc bp2buildTestCase) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +000072 t.Helper()
73 runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, tc)
74}
75
76type bp2buildTestCase struct {
Liz Kammerbe46fcc2021-11-01 15:32:43 -040077 description string
78 moduleTypeUnderTest string
79 moduleTypeUnderTestFactory android.ModuleFactory
80 blueprint string
81 expectedBazelTargets []string
82 filesystem map[string]string
83 dir string
84 expectedErr error
85 unconvertedDepsMode unconvertedDepsMode
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +000086}
87
88func 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)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000107 ctx.RegisterForBazelConversion()
108
Jingwen Chen5146ac02021-09-02 11:44:42 +0000109 _, parseErrs := ctx.ParseFileList(dir, toParse)
110 if errored(t, tc, parseErrs) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000111 return
112 }
Jingwen Chen5146ac02021-09-02 11:44:42 +0000113 _, resolveDepsErrs := ctx.ResolveDependencies(config)
114 if errored(t, tc, resolveDepsErrs) {
115 return
116 }
117
118 errs := append(parseErrs, resolveDepsErrs...)
119 if tc.expectedErr != nil && checkError(t, errs, tc.expectedErr) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000120 return
121 }
122
123 checkDir := dir
124 if tc.dir != "" {
125 checkDir = tc.dir
126 }
127 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Liz Kammer6eff3232021-08-26 08:37:59 -0400128 codegenCtx.unconvertedDepMode = tc.unconvertedDepsMode
129 bazelTargets, errs := generateBazelTargetsForDir(codegenCtx, checkDir)
Liz Kammer54309532021-12-14 12:21:22 -0500130 if tc.expectedErr != nil {
131 if checkError(t, errs, tc.expectedErr) {
132 return
133 } else {
134 t.Errorf("Expected error: %q, got: %q", tc.expectedErr, errs)
135 }
Liz Kammer6eff3232021-08-26 08:37:59 -0400136 } else {
137 android.FailIfErrored(t, errs)
138 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000139 if actualCount, expectedCount := len(bazelTargets), len(tc.expectedBazelTargets); actualCount != expectedCount {
Liz Kammeraabfb5d2021-12-08 15:25:06 -0500140 t.Errorf("%s: Expected %d bazel target (%s), got `%d`` (%s)",
141 tc.description, expectedCount, tc.expectedBazelTargets, actualCount, bazelTargets)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000142 } else {
143 for i, target := range bazelTargets {
144 if w, g := tc.expectedBazelTargets[i], target.content; w != g {
145 t.Errorf(
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000146 "%s: Expected generated Bazel target to be `%s`, got `%s`",
147 tc.description, w, g)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000148 }
149 }
150 }
151}
152
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800153type nestedProps struct {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500154 Nested_prop *string
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800155}
156
Liz Kammer32a03392021-09-14 11:17:21 -0400157type EmbeddedProps struct {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500158 Embedded_prop *string
Liz Kammer32a03392021-09-14 11:17:21 -0400159}
160
161type OtherEmbeddedProps struct {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500162 Other_embedded_prop *string
Liz Kammer32a03392021-09-14 11:17:21 -0400163}
164
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800165type customProps struct {
Liz Kammer32a03392021-09-14 11:17:21 -0400166 EmbeddedProps
167 *OtherEmbeddedProps
168
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800169 Bool_prop bool
170 Bool_ptr_prop *bool
171 // Ensure that properties tagged `blueprint:mutated` are omitted
172 Int_prop int `blueprint:"mutated"`
173 Int64_ptr_prop *int64
174 String_prop string
175 String_ptr_prop *string
176 String_list_prop []string
177
178 Nested_props nestedProps
179 Nested_props_ptr *nestedProps
Liz Kammer4562a3b2021-04-21 18:15:34 -0400180
Liz Kammer32b77cf2021-08-04 15:17:02 -0400181 Arch_paths []string `android:"path,arch_variant"`
182 Arch_paths_exclude []string `android:"path,arch_variant"`
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400183
184 // Prop used to indicate this conversion should be 1 module -> multiple targets
185 One_to_many_prop *bool
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800186}
187
188type customModule struct {
189 android.ModuleBase
Liz Kammerea6666f2021-02-17 10:17:28 -0500190 android.BazelModuleBase
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800191
192 props customProps
193}
194
195// OutputFiles is needed because some instances of this module use dist with a
196// tag property which requires the module implements OutputFileProducer.
197func (m *customModule) OutputFiles(tag string) (android.Paths, error) {
198 return android.PathsForTesting("path" + tag), nil
199}
200
201func (m *customModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
202 // nothing for now.
203}
204
205func customModuleFactoryBase() android.Module {
206 module := &customModule{}
207 module.AddProperties(&module.props)
Liz Kammerea6666f2021-02-17 10:17:28 -0500208 android.InitBazelModule(module)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800209 return module
210}
211
212func customModuleFactory() android.Module {
213 m := customModuleFactoryBase()
Liz Kammer4562a3b2021-04-21 18:15:34 -0400214 android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibBoth)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800215 return m
216}
217
218type testProps struct {
219 Test_prop struct {
220 Test_string_prop string
221 }
222}
223
224type customTestModule struct {
225 android.ModuleBase
226
227 props customProps
228 test_props testProps
229}
230
231func (m *customTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
232 // nothing for now.
233}
234
235func customTestModuleFactoryBase() android.Module {
236 m := &customTestModule{}
237 m.AddProperties(&m.props)
238 m.AddProperties(&m.test_props)
239 return m
240}
241
242func customTestModuleFactory() android.Module {
243 m := customTestModuleFactoryBase()
244 android.InitAndroidModule(m)
245 return m
246}
247
248type customDefaultsModule struct {
249 android.ModuleBase
250 android.DefaultsModuleBase
251}
252
253func customDefaultsModuleFactoryBase() android.DefaultsModule {
254 module := &customDefaultsModule{}
255 module.AddProperties(&customProps{})
256 return module
257}
258
259func customDefaultsModuleFactoryBasic() android.Module {
260 return customDefaultsModuleFactoryBase()
261}
262
263func customDefaultsModuleFactory() android.Module {
264 m := customDefaultsModuleFactoryBase()
265 android.InitDefaultsModule(m)
266 return m
267}
Jingwen Chen73850672020-12-14 08:25:34 -0500268
Liz Kammer32a03392021-09-14 11:17:21 -0400269type EmbeddedAttr struct {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500270 Embedded_attr *string
Liz Kammer32a03392021-09-14 11:17:21 -0400271}
272
273type OtherEmbeddedAttr struct {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500274 Other_embedded_attr *string
Liz Kammer32a03392021-09-14 11:17:21 -0400275}
276
Jingwen Chen73850672020-12-14 08:25:34 -0500277type customBazelModuleAttributes struct {
Liz Kammer32a03392021-09-14 11:17:21 -0400278 EmbeddedAttr
279 *OtherEmbeddedAttr
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500280 String_ptr_prop *string
Jingwen Chen73850672020-12-14 08:25:34 -0500281 String_list_prop []string
Liz Kammer4562a3b2021-04-21 18:15:34 -0400282 Arch_paths bazel.LabelListAttribute
Jingwen Chen73850672020-12-14 08:25:34 -0500283}
284
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400285func (m *customModule) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
286 paths := bazel.LabelListAttribute{}
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500287
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400288 if p := m.props.One_to_many_prop; p != nil && *p {
289 customBp2buildOneToMany(ctx, m)
290 return
291 }
Liz Kammer4562a3b2021-04-21 18:15:34 -0400292
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400293 for axis, configToProps := range m.GetArchVariantProperties(ctx, &customProps{}) {
294 for config, props := range configToProps {
295 if archProps, ok := props.(*customProps); ok && archProps.Arch_paths != nil {
296 paths.SetSelectValue(axis, config, android.BazelLabelForModuleSrcExcludes(ctx, archProps.Arch_paths, archProps.Arch_paths_exclude))
Liz Kammer4562a3b2021-04-21 18:15:34 -0400297 }
298 }
Jingwen Chen73850672020-12-14 08:25:34 -0500299 }
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400300
301 paths.ResolveExcludes()
302
303 attrs := &customBazelModuleAttributes{
304 String_ptr_prop: m.props.String_ptr_prop,
305 String_list_prop: m.props.String_list_prop,
306 Arch_paths: paths,
307 }
308 attrs.Embedded_attr = m.props.Embedded_prop
309 if m.props.OtherEmbeddedProps != nil {
310 attrs.OtherEmbeddedAttr = &OtherEmbeddedAttr{Other_embedded_attr: m.props.OtherEmbeddedProps.Other_embedded_prop}
311 }
312
313 props := bazel.BazelTargetModuleProperties{
314 Rule_class: "custom",
315 }
316
317 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, attrs)
Jingwen Chen73850672020-12-14 08:25:34 -0500318}
Jingwen Chen40067de2021-01-26 21:58:43 -0500319
320// A bp2build mutator that uses load statements and creates a 1:M mapping from
321// module to target.
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400322func customBp2buildOneToMany(ctx android.TopDownMutatorContext, m *customModule) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500323
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400324 baseName := m.Name()
325 attrs := &customBazelModuleAttributes{}
Jingwen Chen1fd14692021-02-05 03:01:50 -0500326
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400327 myLibraryProps := bazel.BazelTargetModuleProperties{
328 Rule_class: "my_library",
329 Bzl_load_location: "//build/bazel/rules:rules.bzl",
Jingwen Chen40067de2021-01-26 21:58:43 -0500330 }
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400331 ctx.CreateBazelTargetModule(myLibraryProps, android.CommonAttributes{Name: baseName}, attrs)
332
333 protoLibraryProps := bazel.BazelTargetModuleProperties{
334 Rule_class: "proto_library",
335 Bzl_load_location: "//build/bazel/rules:proto.bzl",
336 }
337 ctx.CreateBazelTargetModule(protoLibraryProps, android.CommonAttributes{Name: baseName + "_proto_library_deps"}, attrs)
338
339 myProtoLibraryProps := bazel.BazelTargetModuleProperties{
340 Rule_class: "my_proto_library",
341 Bzl_load_location: "//build/bazel/rules:proto.bzl",
342 }
343 ctx.CreateBazelTargetModule(myProtoLibraryProps, android.CommonAttributes{Name: baseName + "_my_proto_library_deps"}, attrs)
Jingwen Chen40067de2021-01-26 21:58:43 -0500344}
Jingwen Chenba369ad2021-02-22 10:19:34 -0500345
346// Helper method for tests to easily access the targets in a dir.
Liz Kammer6eff3232021-08-26 08:37:59 -0400347func generateBazelTargetsForDir(codegenCtx *CodegenContext, dir string) (BazelTargets, []error) {
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400348 // TODO: Set generateFilegroups to true and/or remove the generateFilegroups argument completely
Liz Kammer6eff3232021-08-26 08:37:59 -0400349 res, err := GenerateBazelTargets(codegenCtx, false)
350 return res.buildFileToTargets[dir], err
Jingwen Chenba369ad2021-02-22 10:19:34 -0500351}
Liz Kammer32b77cf2021-08-04 15:17:02 -0400352
353func registerCustomModuleForBp2buildConversion(ctx *android.TestContext) {
354 ctx.RegisterModuleType("custom", customModuleFactory)
Liz Kammer32b77cf2021-08-04 15:17:02 -0400355 ctx.RegisterForBazelConversion()
356}
Liz Kammer7a210ac2021-09-22 15:52:58 -0400357
358func simpleModuleDoNotConvertBp2build(typ, name string) string {
359 return fmt.Sprintf(`
360%s {
361 name: "%s",
362 bazel_module: { bp2build_available: false },
363}`, typ, name)
364}
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500365
366type attrNameToString map[string]string
367
368func makeBazelTarget(typ, name string, attrs attrNameToString) string {
369 attrStrings := make([]string, 0, len(attrs)+1)
370 attrStrings = append(attrStrings, fmt.Sprintf(` name = "%s",`, name))
371 for _, k := range android.SortedStringKeys(attrs) {
372 attrStrings = append(attrStrings, fmt.Sprintf(" %s = %s,", k, attrs[k]))
373 }
374 return fmt.Sprintf(`%s(
375%s
376)`, typ, strings.Join(attrStrings, "\n"))
377}