blob: 15cf486895912d3dcd3e808fc8db5e6da51634c1 [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 }
46 if errs[0].Error() == expectedErr.Error() {
47 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)
130 if tc.expectedErr != nil && checkError(t, errs, tc.expectedErr) {
131 return
132 } else {
133 android.FailIfErrored(t, errs)
134 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000135 if actualCount, expectedCount := len(bazelTargets), len(tc.expectedBazelTargets); actualCount != expectedCount {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000136 t.Errorf("%s: Expected %d bazel target, got `%d``",
137 tc.description, expectedCount, actualCount)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000138 } else {
139 for i, target := range bazelTargets {
140 if w, g := tc.expectedBazelTargets[i], target.content; w != g {
141 t.Errorf(
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000142 "%s: Expected generated Bazel target to be `%s`, got `%s`",
143 tc.description, w, g)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000144 }
145 }
146 }
147}
148
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800149type nestedProps struct {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500150 Nested_prop *string
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800151}
152
Liz Kammer32a03392021-09-14 11:17:21 -0400153type EmbeddedProps struct {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500154 Embedded_prop *string
Liz Kammer32a03392021-09-14 11:17:21 -0400155}
156
157type OtherEmbeddedProps struct {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500158 Other_embedded_prop *string
Liz Kammer32a03392021-09-14 11:17:21 -0400159}
160
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800161type customProps struct {
Liz Kammer32a03392021-09-14 11:17:21 -0400162 EmbeddedProps
163 *OtherEmbeddedProps
164
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800165 Bool_prop bool
166 Bool_ptr_prop *bool
167 // Ensure that properties tagged `blueprint:mutated` are omitted
168 Int_prop int `blueprint:"mutated"`
169 Int64_ptr_prop *int64
170 String_prop string
171 String_ptr_prop *string
172 String_list_prop []string
173
174 Nested_props nestedProps
175 Nested_props_ptr *nestedProps
Liz Kammer4562a3b2021-04-21 18:15:34 -0400176
Liz Kammer32b77cf2021-08-04 15:17:02 -0400177 Arch_paths []string `android:"path,arch_variant"`
178 Arch_paths_exclude []string `android:"path,arch_variant"`
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400179
180 // Prop used to indicate this conversion should be 1 module -> multiple targets
181 One_to_many_prop *bool
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800182}
183
184type customModule struct {
185 android.ModuleBase
Liz Kammerea6666f2021-02-17 10:17:28 -0500186 android.BazelModuleBase
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800187
188 props customProps
189}
190
191// OutputFiles is needed because some instances of this module use dist with a
192// tag property which requires the module implements OutputFileProducer.
193func (m *customModule) OutputFiles(tag string) (android.Paths, error) {
194 return android.PathsForTesting("path" + tag), nil
195}
196
197func (m *customModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
198 // nothing for now.
199}
200
201func customModuleFactoryBase() android.Module {
202 module := &customModule{}
203 module.AddProperties(&module.props)
Liz Kammerea6666f2021-02-17 10:17:28 -0500204 android.InitBazelModule(module)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800205 return module
206}
207
208func customModuleFactory() android.Module {
209 m := customModuleFactoryBase()
Liz Kammer4562a3b2021-04-21 18:15:34 -0400210 android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibBoth)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800211 return m
212}
213
214type testProps struct {
215 Test_prop struct {
216 Test_string_prop string
217 }
218}
219
220type customTestModule struct {
221 android.ModuleBase
222
223 props customProps
224 test_props testProps
225}
226
227func (m *customTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
228 // nothing for now.
229}
230
231func customTestModuleFactoryBase() android.Module {
232 m := &customTestModule{}
233 m.AddProperties(&m.props)
234 m.AddProperties(&m.test_props)
235 return m
236}
237
238func customTestModuleFactory() android.Module {
239 m := customTestModuleFactoryBase()
240 android.InitAndroidModule(m)
241 return m
242}
243
244type customDefaultsModule struct {
245 android.ModuleBase
246 android.DefaultsModuleBase
247}
248
249func customDefaultsModuleFactoryBase() android.DefaultsModule {
250 module := &customDefaultsModule{}
251 module.AddProperties(&customProps{})
252 return module
253}
254
255func customDefaultsModuleFactoryBasic() android.Module {
256 return customDefaultsModuleFactoryBase()
257}
258
259func customDefaultsModuleFactory() android.Module {
260 m := customDefaultsModuleFactoryBase()
261 android.InitDefaultsModule(m)
262 return m
263}
Jingwen Chen73850672020-12-14 08:25:34 -0500264
Liz Kammer32a03392021-09-14 11:17:21 -0400265type EmbeddedAttr struct {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500266 Embedded_attr *string
Liz Kammer32a03392021-09-14 11:17:21 -0400267}
268
269type OtherEmbeddedAttr struct {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500270 Other_embedded_attr *string
Liz Kammer32a03392021-09-14 11:17:21 -0400271}
272
Jingwen Chen73850672020-12-14 08:25:34 -0500273type customBazelModuleAttributes struct {
Liz Kammer32a03392021-09-14 11:17:21 -0400274 EmbeddedAttr
275 *OtherEmbeddedAttr
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500276 String_ptr_prop *string
Jingwen Chen73850672020-12-14 08:25:34 -0500277 String_list_prop []string
Liz Kammer4562a3b2021-04-21 18:15:34 -0400278 Arch_paths bazel.LabelListAttribute
Jingwen Chen73850672020-12-14 08:25:34 -0500279}
280
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400281func (m *customModule) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
282 paths := bazel.LabelListAttribute{}
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500283
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400284 if p := m.props.One_to_many_prop; p != nil && *p {
285 customBp2buildOneToMany(ctx, m)
286 return
287 }
Liz Kammer4562a3b2021-04-21 18:15:34 -0400288
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400289 for axis, configToProps := range m.GetArchVariantProperties(ctx, &customProps{}) {
290 for config, props := range configToProps {
291 if archProps, ok := props.(*customProps); ok && archProps.Arch_paths != nil {
292 paths.SetSelectValue(axis, config, android.BazelLabelForModuleSrcExcludes(ctx, archProps.Arch_paths, archProps.Arch_paths_exclude))
Liz Kammer4562a3b2021-04-21 18:15:34 -0400293 }
294 }
Jingwen Chen73850672020-12-14 08:25:34 -0500295 }
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400296
297 paths.ResolveExcludes()
298
299 attrs := &customBazelModuleAttributes{
300 String_ptr_prop: m.props.String_ptr_prop,
301 String_list_prop: m.props.String_list_prop,
302 Arch_paths: paths,
303 }
304 attrs.Embedded_attr = m.props.Embedded_prop
305 if m.props.OtherEmbeddedProps != nil {
306 attrs.OtherEmbeddedAttr = &OtherEmbeddedAttr{Other_embedded_attr: m.props.OtherEmbeddedProps.Other_embedded_prop}
307 }
308
309 props := bazel.BazelTargetModuleProperties{
310 Rule_class: "custom",
311 }
312
313 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, attrs)
Jingwen Chen73850672020-12-14 08:25:34 -0500314}
Jingwen Chen40067de2021-01-26 21:58:43 -0500315
316// A bp2build mutator that uses load statements and creates a 1:M mapping from
317// module to target.
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400318func customBp2buildOneToMany(ctx android.TopDownMutatorContext, m *customModule) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500319
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400320 baseName := m.Name()
321 attrs := &customBazelModuleAttributes{}
Jingwen Chen1fd14692021-02-05 03:01:50 -0500322
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400323 myLibraryProps := bazel.BazelTargetModuleProperties{
324 Rule_class: "my_library",
325 Bzl_load_location: "//build/bazel/rules:rules.bzl",
Jingwen Chen40067de2021-01-26 21:58:43 -0500326 }
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400327 ctx.CreateBazelTargetModule(myLibraryProps, android.CommonAttributes{Name: baseName}, attrs)
328
329 protoLibraryProps := bazel.BazelTargetModuleProperties{
330 Rule_class: "proto_library",
331 Bzl_load_location: "//build/bazel/rules:proto.bzl",
332 }
333 ctx.CreateBazelTargetModule(protoLibraryProps, android.CommonAttributes{Name: baseName + "_proto_library_deps"}, attrs)
334
335 myProtoLibraryProps := bazel.BazelTargetModuleProperties{
336 Rule_class: "my_proto_library",
337 Bzl_load_location: "//build/bazel/rules:proto.bzl",
338 }
339 ctx.CreateBazelTargetModule(myProtoLibraryProps, android.CommonAttributes{Name: baseName + "_my_proto_library_deps"}, attrs)
Jingwen Chen40067de2021-01-26 21:58:43 -0500340}
Jingwen Chenba369ad2021-02-22 10:19:34 -0500341
342// Helper method for tests to easily access the targets in a dir.
Liz Kammer6eff3232021-08-26 08:37:59 -0400343func generateBazelTargetsForDir(codegenCtx *CodegenContext, dir string) (BazelTargets, []error) {
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400344 // TODO: Set generateFilegroups to true and/or remove the generateFilegroups argument completely
Liz Kammer6eff3232021-08-26 08:37:59 -0400345 res, err := GenerateBazelTargets(codegenCtx, false)
346 return res.buildFileToTargets[dir], err
Jingwen Chenba369ad2021-02-22 10:19:34 -0500347}
Liz Kammer32b77cf2021-08-04 15:17:02 -0400348
349func registerCustomModuleForBp2buildConversion(ctx *android.TestContext) {
350 ctx.RegisterModuleType("custom", customModuleFactory)
Liz Kammer32b77cf2021-08-04 15:17:02 -0400351 ctx.RegisterForBazelConversion()
352}
Liz Kammer7a210ac2021-09-22 15:52:58 -0400353
354func simpleModuleDoNotConvertBp2build(typ, name string) string {
355 return fmt.Sprintf(`
356%s {
357 name: "%s",
358 bazel_module: { bp2build_available: false },
359}`, typ, name)
360}
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500361
362type attrNameToString map[string]string
363
364func makeBazelTarget(typ, name string, attrs attrNameToString) string {
365 attrStrings := make([]string, 0, len(attrs)+1)
366 attrStrings = append(attrStrings, fmt.Sprintf(` name = "%s",`, name))
367 for _, k := range android.SortedStringKeys(attrs) {
368 attrStrings = append(attrStrings, fmt.Sprintf(" %s = %s,", k, attrs[k]))
369 }
370 return fmt.Sprintf(`%s(
371%s
372)`, typ, strings.Join(attrStrings, "\n"))
373}