blob: 156e08249f79e2429206787a13dd6553deeaf248 [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"
Sam Delmerico24c56032022-03-28 19:53:03 +000028 "android/soong/android/allowlists"
Jingwen Chen73850672020-12-14 08:25:34 -050029 "android/soong/bazel"
Liz Kammer2dd9ca42020-11-25 16:06:39 -080030)
31
Jingwen Chen91220d72021-03-24 02:18:33 -040032var (
33 // A default configuration for tests to not have to specify bp2build_available on top level targets.
Sam Delmerico24c56032022-03-28 19:53:03 +000034 bp2buildConfig = android.NewBp2BuildAllowlist().SetDefaultConfig(
35 allowlists.Bp2BuildConfig{
36 android.Bp2BuildTopLevel: allowlists.Bp2BuildDefaultTrueRecursively,
37 },
38 )
Rupert Shuttleworth06559d02021-05-19 09:14:26 -040039
40 buildDir string
Jingwen Chen91220d72021-03-24 02:18:33 -040041)
42
Jingwen Chen5146ac02021-09-02 11:44:42 +000043func 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 +000044 t.Helper()
Jingwen Chen5146ac02021-09-02 11:44:42 +000045
Jingwen Chen5146ac02021-09-02 11:44:42 +000046 if len(errs) != 1 {
Liz Kammer6eff3232021-08-26 08:37:59 -040047 return false
Jingwen Chen5146ac02021-09-02 11:44:42 +000048 }
Liz Kammer54309532021-12-14 12:21:22 -050049 if strings.Contains(errs[0].Error(), expectedErr.Error()) {
Jingwen Chen5146ac02021-09-02 11:44:42 +000050 return true
51 }
52
53 return false
54}
55
56func errored(t *testing.T, tc bp2buildTestCase, errs []error) bool {
57 t.Helper()
58 if tc.expectedErr != nil {
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 Thaureaux1c92aef2021-08-23 16:10:00 +000063 if len(errs) > 0 {
64 for _, err := range errs {
Jingwen Chen5146ac02021-09-02 11:44:42 +000065 t.Errorf("%s: %s", tc.description, err)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +000066 }
67 return true
68 }
Jingwen Chen5146ac02021-09-02 11:44:42 +000069
70 // All good, continue execution.
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +000071 return false
72}
73
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxce0a07e2021-08-23 16:17:32 +000074func runBp2BuildTestCaseSimple(t *testing.T, tc bp2buildTestCase) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +000075 t.Helper()
76 runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, tc)
77}
78
79type bp2buildTestCase struct {
Liz Kammerbe46fcc2021-11-01 15:32:43 -040080 description string
81 moduleTypeUnderTest string
82 moduleTypeUnderTestFactory android.ModuleFactory
83 blueprint string
84 expectedBazelTargets []string
85 filesystem map[string]string
86 dir string
87 expectedErr error
88 unconvertedDepsMode unconvertedDepsMode
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +000089}
90
91func runBp2BuildTestCase(t *testing.T, registerModuleTypes func(ctx android.RegistrationContext), tc bp2buildTestCase) {
92 t.Helper()
93 dir := "."
94 filesystem := make(map[string][]byte)
95 toParse := []string{
96 "Android.bp",
97 }
98 for f, content := range tc.filesystem {
99 if strings.HasSuffix(f, "Android.bp") {
100 toParse = append(toParse, f)
101 }
102 filesystem[f] = []byte(content)
103 }
104 config := android.TestConfig(buildDir, nil, tc.blueprint, filesystem)
105 ctx := android.NewTestContext(config)
106
107 registerModuleTypes(ctx)
108 ctx.RegisterModuleType(tc.moduleTypeUnderTest, tc.moduleTypeUnderTestFactory)
109 ctx.RegisterBp2BuildConfig(bp2buildConfig)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000110 ctx.RegisterForBazelConversion()
111
Jingwen Chen5146ac02021-09-02 11:44:42 +0000112 _, parseErrs := ctx.ParseFileList(dir, toParse)
113 if errored(t, tc, parseErrs) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000114 return
115 }
Jingwen Chen5146ac02021-09-02 11:44:42 +0000116 _, resolveDepsErrs := ctx.ResolveDependencies(config)
117 if errored(t, tc, resolveDepsErrs) {
118 return
119 }
120
121 errs := append(parseErrs, resolveDepsErrs...)
122 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 +0000123 return
124 }
125
126 checkDir := dir
127 if tc.dir != "" {
128 checkDir = tc.dir
129 }
130 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Liz Kammer6eff3232021-08-26 08:37:59 -0400131 codegenCtx.unconvertedDepMode = tc.unconvertedDepsMode
132 bazelTargets, errs := generateBazelTargetsForDir(codegenCtx, checkDir)
Liz Kammer54309532021-12-14 12:21:22 -0500133 if tc.expectedErr != nil {
134 if checkError(t, errs, tc.expectedErr) {
135 return
136 } else {
137 t.Errorf("Expected error: %q, got: %q", tc.expectedErr, errs)
138 }
Liz Kammer6eff3232021-08-26 08:37:59 -0400139 } else {
140 android.FailIfErrored(t, errs)
141 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000142 if actualCount, expectedCount := len(bazelTargets), len(tc.expectedBazelTargets); actualCount != expectedCount {
Liz Kammeraabfb5d2021-12-08 15:25:06 -0500143 t.Errorf("%s: Expected %d bazel target (%s), got `%d`` (%s)",
144 tc.description, expectedCount, tc.expectedBazelTargets, actualCount, bazelTargets)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000145 } else {
146 for i, target := range bazelTargets {
147 if w, g := tc.expectedBazelTargets[i], target.content; w != g {
148 t.Errorf(
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000149 "%s: Expected generated Bazel target to be `%s`, got `%s`",
150 tc.description, w, g)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000151 }
152 }
153 }
154}
155
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800156type nestedProps struct {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500157 Nested_prop *string
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800158}
159
Liz Kammer32a03392021-09-14 11:17:21 -0400160type EmbeddedProps struct {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500161 Embedded_prop *string
Liz Kammer32a03392021-09-14 11:17:21 -0400162}
163
164type OtherEmbeddedProps struct {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500165 Other_embedded_prop *string
Liz Kammer32a03392021-09-14 11:17:21 -0400166}
167
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800168type customProps struct {
Liz Kammer32a03392021-09-14 11:17:21 -0400169 EmbeddedProps
170 *OtherEmbeddedProps
171
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800172 Bool_prop bool
173 Bool_ptr_prop *bool
174 // Ensure that properties tagged `blueprint:mutated` are omitted
175 Int_prop int `blueprint:"mutated"`
176 Int64_ptr_prop *int64
177 String_prop string
178 String_ptr_prop *string
179 String_list_prop []string
180
181 Nested_props nestedProps
182 Nested_props_ptr *nestedProps
Liz Kammer4562a3b2021-04-21 18:15:34 -0400183
Liz Kammer32b77cf2021-08-04 15:17:02 -0400184 Arch_paths []string `android:"path,arch_variant"`
185 Arch_paths_exclude []string `android:"path,arch_variant"`
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400186
187 // Prop used to indicate this conversion should be 1 module -> multiple targets
188 One_to_many_prop *bool
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800189}
190
191type customModule struct {
192 android.ModuleBase
Liz Kammerea6666f2021-02-17 10:17:28 -0500193 android.BazelModuleBase
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800194
195 props customProps
196}
197
198// OutputFiles is needed because some instances of this module use dist with a
199// tag property which requires the module implements OutputFileProducer.
200func (m *customModule) OutputFiles(tag string) (android.Paths, error) {
201 return android.PathsForTesting("path" + tag), nil
202}
203
204func (m *customModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
205 // nothing for now.
206}
207
208func customModuleFactoryBase() android.Module {
209 module := &customModule{}
210 module.AddProperties(&module.props)
Liz Kammerea6666f2021-02-17 10:17:28 -0500211 android.InitBazelModule(module)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800212 return module
213}
214
215func customModuleFactory() android.Module {
216 m := customModuleFactoryBase()
Liz Kammer4562a3b2021-04-21 18:15:34 -0400217 android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibBoth)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800218 return m
219}
220
221type testProps struct {
222 Test_prop struct {
223 Test_string_prop string
224 }
225}
226
227type customTestModule struct {
228 android.ModuleBase
229
230 props customProps
231 test_props testProps
232}
233
234func (m *customTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
235 // nothing for now.
236}
237
238func customTestModuleFactoryBase() android.Module {
239 m := &customTestModule{}
240 m.AddProperties(&m.props)
241 m.AddProperties(&m.test_props)
242 return m
243}
244
245func customTestModuleFactory() android.Module {
246 m := customTestModuleFactoryBase()
247 android.InitAndroidModule(m)
248 return m
249}
250
251type customDefaultsModule struct {
252 android.ModuleBase
253 android.DefaultsModuleBase
254}
255
256func customDefaultsModuleFactoryBase() android.DefaultsModule {
257 module := &customDefaultsModule{}
258 module.AddProperties(&customProps{})
259 return module
260}
261
262func customDefaultsModuleFactoryBasic() android.Module {
263 return customDefaultsModuleFactoryBase()
264}
265
266func customDefaultsModuleFactory() android.Module {
267 m := customDefaultsModuleFactoryBase()
268 android.InitDefaultsModule(m)
269 return m
270}
Jingwen Chen73850672020-12-14 08:25:34 -0500271
Liz Kammer32a03392021-09-14 11:17:21 -0400272type EmbeddedAttr struct {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500273 Embedded_attr *string
Liz Kammer32a03392021-09-14 11:17:21 -0400274}
275
276type OtherEmbeddedAttr struct {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500277 Other_embedded_attr *string
Liz Kammer32a03392021-09-14 11:17:21 -0400278}
279
Jingwen Chen73850672020-12-14 08:25:34 -0500280type customBazelModuleAttributes struct {
Liz Kammer32a03392021-09-14 11:17:21 -0400281 EmbeddedAttr
282 *OtherEmbeddedAttr
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500283 String_ptr_prop *string
Jingwen Chen73850672020-12-14 08:25:34 -0500284 String_list_prop []string
Liz Kammer4562a3b2021-04-21 18:15:34 -0400285 Arch_paths bazel.LabelListAttribute
Jingwen Chen73850672020-12-14 08:25:34 -0500286}
287
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400288func (m *customModule) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
289 paths := bazel.LabelListAttribute{}
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500290
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400291 if p := m.props.One_to_many_prop; p != nil && *p {
292 customBp2buildOneToMany(ctx, m)
293 return
294 }
Liz Kammer4562a3b2021-04-21 18:15:34 -0400295
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400296 for axis, configToProps := range m.GetArchVariantProperties(ctx, &customProps{}) {
297 for config, props := range configToProps {
298 if archProps, ok := props.(*customProps); ok && archProps.Arch_paths != nil {
299 paths.SetSelectValue(axis, config, android.BazelLabelForModuleSrcExcludes(ctx, archProps.Arch_paths, archProps.Arch_paths_exclude))
Liz Kammer4562a3b2021-04-21 18:15:34 -0400300 }
301 }
Jingwen Chen73850672020-12-14 08:25:34 -0500302 }
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400303
304 paths.ResolveExcludes()
305
306 attrs := &customBazelModuleAttributes{
307 String_ptr_prop: m.props.String_ptr_prop,
308 String_list_prop: m.props.String_list_prop,
309 Arch_paths: paths,
310 }
311 attrs.Embedded_attr = m.props.Embedded_prop
312 if m.props.OtherEmbeddedProps != nil {
313 attrs.OtherEmbeddedAttr = &OtherEmbeddedAttr{Other_embedded_attr: m.props.OtherEmbeddedProps.Other_embedded_prop}
314 }
315
316 props := bazel.BazelTargetModuleProperties{
317 Rule_class: "custom",
318 }
319
320 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, attrs)
Jingwen Chen73850672020-12-14 08:25:34 -0500321}
Jingwen Chen40067de2021-01-26 21:58:43 -0500322
323// A bp2build mutator that uses load statements and creates a 1:M mapping from
324// module to target.
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400325func customBp2buildOneToMany(ctx android.TopDownMutatorContext, m *customModule) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500326
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400327 baseName := m.Name()
328 attrs := &customBazelModuleAttributes{}
Jingwen Chen1fd14692021-02-05 03:01:50 -0500329
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400330 myLibraryProps := bazel.BazelTargetModuleProperties{
331 Rule_class: "my_library",
332 Bzl_load_location: "//build/bazel/rules:rules.bzl",
Jingwen Chen40067de2021-01-26 21:58:43 -0500333 }
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400334 ctx.CreateBazelTargetModule(myLibraryProps, android.CommonAttributes{Name: baseName}, attrs)
335
336 protoLibraryProps := bazel.BazelTargetModuleProperties{
337 Rule_class: "proto_library",
338 Bzl_load_location: "//build/bazel/rules:proto.bzl",
339 }
340 ctx.CreateBazelTargetModule(protoLibraryProps, android.CommonAttributes{Name: baseName + "_proto_library_deps"}, attrs)
341
342 myProtoLibraryProps := bazel.BazelTargetModuleProperties{
343 Rule_class: "my_proto_library",
344 Bzl_load_location: "//build/bazel/rules:proto.bzl",
345 }
346 ctx.CreateBazelTargetModule(myProtoLibraryProps, android.CommonAttributes{Name: baseName + "_my_proto_library_deps"}, attrs)
Jingwen Chen40067de2021-01-26 21:58:43 -0500347}
Jingwen Chenba369ad2021-02-22 10:19:34 -0500348
349// Helper method for tests to easily access the targets in a dir.
Liz Kammer6eff3232021-08-26 08:37:59 -0400350func generateBazelTargetsForDir(codegenCtx *CodegenContext, dir string) (BazelTargets, []error) {
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400351 // TODO: Set generateFilegroups to true and/or remove the generateFilegroups argument completely
Liz Kammer6eff3232021-08-26 08:37:59 -0400352 res, err := GenerateBazelTargets(codegenCtx, false)
353 return res.buildFileToTargets[dir], err
Jingwen Chenba369ad2021-02-22 10:19:34 -0500354}
Liz Kammer32b77cf2021-08-04 15:17:02 -0400355
356func registerCustomModuleForBp2buildConversion(ctx *android.TestContext) {
357 ctx.RegisterModuleType("custom", customModuleFactory)
Liz Kammer32b77cf2021-08-04 15:17:02 -0400358 ctx.RegisterForBazelConversion()
359}
Liz Kammer7a210ac2021-09-22 15:52:58 -0400360
361func simpleModuleDoNotConvertBp2build(typ, name string) string {
362 return fmt.Sprintf(`
363%s {
364 name: "%s",
365 bazel_module: { bp2build_available: false },
366}`, typ, name)
367}
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500368
369type attrNameToString map[string]string
370
371func makeBazelTarget(typ, name string, attrs attrNameToString) string {
372 attrStrings := make([]string, 0, len(attrs)+1)
373 attrStrings = append(attrStrings, fmt.Sprintf(` name = "%s",`, name))
374 for _, k := range android.SortedStringKeys(attrs) {
375 attrStrings = append(attrStrings, fmt.Sprintf(" %s = %s,", k, attrs[k]))
376 }
377 return fmt.Sprintf(`%s(
378%s
379)`, typ, strings.Join(attrStrings, "\n"))
380}