blob: b39d363cb180968d160d79ae6535c23eab13aaca [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 (
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +000023 "strings"
Rupert Shuttleworth06559d02021-05-19 09:14:26 -040024 "testing"
25
Liz Kammer2dd9ca42020-11-25 16:06:39 -080026 "android/soong/android"
Jingwen Chen73850672020-12-14 08:25:34 -050027 "android/soong/bazel"
Liz Kammer2dd9ca42020-11-25 16:06:39 -080028)
29
Jingwen Chen91220d72021-03-24 02:18:33 -040030var (
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 Shuttleworth06559d02021-05-19 09:14:26 -040035
36 buildDir string
Jingwen Chen91220d72021-03-24 02:18:33 -040037)
38
Jingwen Chen5146ac02021-09-02 11:44:42 +000039func 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 +000040 t.Helper()
Jingwen Chen5146ac02021-09-02 11:44:42 +000041
42 // expectedErr is not nil, find it in the list of errors
43 if len(errs) != 1 {
44 t.Errorf("Expected only 1 error, got %d: %q", len(errs), errs)
45 }
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 {
77 description string
78 moduleTypeUnderTest string
79 moduleTypeUnderTestFactory android.ModuleFactory
80 moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
81 blueprint string
82 expectedBazelTargets []string
83 filesystem map[string]string
84 dir string
Jingwen Chen5146ac02021-09-02 11:44:42 +000085 expectedErr error
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)
107 ctx.RegisterBp2BuildMutator(tc.moduleTypeUnderTest, tc.moduleTypeUnderTestBp2BuildMutator)
108 ctx.RegisterForBazelConversion()
109
Jingwen Chen5146ac02021-09-02 11:44:42 +0000110 _, parseErrs := ctx.ParseFileList(dir, toParse)
111 if errored(t, tc, parseErrs) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000112 return
113 }
Jingwen Chen5146ac02021-09-02 11:44:42 +0000114 _, resolveDepsErrs := ctx.ResolveDependencies(config)
115 if errored(t, tc, resolveDepsErrs) {
116 return
117 }
118
119 errs := append(parseErrs, resolveDepsErrs...)
120 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 +0000121 return
122 }
123
124 checkDir := dir
125 if tc.dir != "" {
126 checkDir = tc.dir
127 }
128 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
129 bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir)
130 if actualCount, expectedCount := len(bazelTargets), len(tc.expectedBazelTargets); actualCount != expectedCount {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux560cb662021-08-26 20:13:29 +0000131 t.Errorf("%s: Expected %d bazel target, got %d; %v",
132 tc.description, expectedCount, actualCount, bazelTargets)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000133 } else {
134 for i, target := range bazelTargets {
135 if w, g := tc.expectedBazelTargets[i], target.content; w != g {
136 t.Errorf(
137 "%s: Expected generated Bazel target to be '%s', got '%s'",
138 tc.description,
139 w,
140 g,
141 )
142 }
143 }
144 }
145}
146
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800147type nestedProps struct {
148 Nested_prop string
149}
150
Liz Kammer32a03392021-09-14 11:17:21 -0400151type EmbeddedProps struct {
152 Embedded_prop string
153}
154
155type OtherEmbeddedProps struct {
156 Other_embedded_prop string
157}
158
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800159type customProps struct {
Liz Kammer32a03392021-09-14 11:17:21 -0400160 EmbeddedProps
161 *OtherEmbeddedProps
162
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800163 Bool_prop bool
164 Bool_ptr_prop *bool
165 // Ensure that properties tagged `blueprint:mutated` are omitted
166 Int_prop int `blueprint:"mutated"`
167 Int64_ptr_prop *int64
168 String_prop string
169 String_ptr_prop *string
170 String_list_prop []string
171
172 Nested_props nestedProps
173 Nested_props_ptr *nestedProps
Liz Kammer4562a3b2021-04-21 18:15:34 -0400174
Liz Kammer32b77cf2021-08-04 15:17:02 -0400175 Arch_paths []string `android:"path,arch_variant"`
176 Arch_paths_exclude []string `android:"path,arch_variant"`
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800177}
178
179type customModule struct {
180 android.ModuleBase
Liz Kammerea6666f2021-02-17 10:17:28 -0500181 android.BazelModuleBase
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800182
183 props customProps
184}
185
186// OutputFiles is needed because some instances of this module use dist with a
187// tag property which requires the module implements OutputFileProducer.
188func (m *customModule) OutputFiles(tag string) (android.Paths, error) {
189 return android.PathsForTesting("path" + tag), nil
190}
191
192func (m *customModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
193 // nothing for now.
194}
195
196func customModuleFactoryBase() android.Module {
197 module := &customModule{}
198 module.AddProperties(&module.props)
Liz Kammerea6666f2021-02-17 10:17:28 -0500199 android.InitBazelModule(module)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800200 return module
201}
202
203func customModuleFactory() android.Module {
204 m := customModuleFactoryBase()
Liz Kammer4562a3b2021-04-21 18:15:34 -0400205 android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibBoth)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800206 return m
207}
208
209type testProps struct {
210 Test_prop struct {
211 Test_string_prop string
212 }
213}
214
215type customTestModule struct {
216 android.ModuleBase
217
218 props customProps
219 test_props testProps
220}
221
222func (m *customTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
223 // nothing for now.
224}
225
226func customTestModuleFactoryBase() android.Module {
227 m := &customTestModule{}
228 m.AddProperties(&m.props)
229 m.AddProperties(&m.test_props)
230 return m
231}
232
233func customTestModuleFactory() android.Module {
234 m := customTestModuleFactoryBase()
235 android.InitAndroidModule(m)
236 return m
237}
238
239type customDefaultsModule struct {
240 android.ModuleBase
241 android.DefaultsModuleBase
242}
243
244func customDefaultsModuleFactoryBase() android.DefaultsModule {
245 module := &customDefaultsModule{}
246 module.AddProperties(&customProps{})
247 return module
248}
249
250func customDefaultsModuleFactoryBasic() android.Module {
251 return customDefaultsModuleFactoryBase()
252}
253
254func customDefaultsModuleFactory() android.Module {
255 m := customDefaultsModuleFactoryBase()
256 android.InitDefaultsModule(m)
257 return m
258}
Jingwen Chen73850672020-12-14 08:25:34 -0500259
Liz Kammer32a03392021-09-14 11:17:21 -0400260type EmbeddedAttr struct {
261 Embedded_attr string
262}
263
264type OtherEmbeddedAttr struct {
265 Other_embedded_attr string
266}
267
Jingwen Chen73850672020-12-14 08:25:34 -0500268type customBazelModuleAttributes struct {
Liz Kammer32a03392021-09-14 11:17:21 -0400269 EmbeddedAttr
270 *OtherEmbeddedAttr
Jingwen Chen73850672020-12-14 08:25:34 -0500271 String_prop string
272 String_list_prop []string
Liz Kammer4562a3b2021-04-21 18:15:34 -0400273 Arch_paths bazel.LabelListAttribute
Jingwen Chen73850672020-12-14 08:25:34 -0500274}
275
Jingwen Chen73850672020-12-14 08:25:34 -0500276func customBp2BuildMutator(ctx android.TopDownMutatorContext) {
277 if m, ok := ctx.Module().(*customModule); ok {
Jingwen Chen12b4c272021-03-10 02:05:59 -0500278 if !m.ConvertWithBp2build(ctx) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500279 return
280 }
281
Liz Kammer32b77cf2021-08-04 15:17:02 -0400282 paths := bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrcExcludes(ctx, m.props.Arch_paths, m.props.Arch_paths_exclude))
Liz Kammer4562a3b2021-04-21 18:15:34 -0400283
Liz Kammer9abd62d2021-05-21 08:37:59 -0400284 for axis, configToProps := range m.GetArchVariantProperties(ctx, &customProps{}) {
285 for config, props := range configToProps {
286 if archProps, ok := props.(*customProps); ok && archProps.Arch_paths != nil {
Liz Kammer32b77cf2021-08-04 15:17:02 -0400287 paths.SetSelectValue(axis, config, android.BazelLabelForModuleSrcExcludes(ctx, archProps.Arch_paths, archProps.Arch_paths_exclude))
Liz Kammer9abd62d2021-05-21 08:37:59 -0400288 }
Liz Kammer4562a3b2021-04-21 18:15:34 -0400289 }
290 }
291
Liz Kammer32b77cf2021-08-04 15:17:02 -0400292 paths.ResolveExcludes()
293
Jingwen Chen1fd14692021-02-05 03:01:50 -0500294 attrs := &customBazelModuleAttributes{
Jingwen Chen73850672020-12-14 08:25:34 -0500295 String_prop: m.props.String_prop,
296 String_list_prop: m.props.String_list_prop,
Liz Kammer4562a3b2021-04-21 18:15:34 -0400297 Arch_paths: paths,
Jingwen Chen1fd14692021-02-05 03:01:50 -0500298 }
Liz Kammer32a03392021-09-14 11:17:21 -0400299 attrs.Embedded_attr = m.props.Embedded_prop
300 if m.props.OtherEmbeddedProps != nil {
301 attrs.OtherEmbeddedAttr = &OtherEmbeddedAttr{Other_embedded_attr: m.props.OtherEmbeddedProps.Other_embedded_prop}
302 }
Jingwen Chen1fd14692021-02-05 03:01:50 -0500303
Liz Kammerfc46bc12021-02-19 11:06:17 -0500304 props := bazel.BazelTargetModuleProperties{
305 Rule_class: "custom",
306 }
Jingwen Chen1fd14692021-02-05 03:01:50 -0500307
Liz Kammer2ada09a2021-08-11 00:17:36 -0400308 ctx.CreateBazelTargetModule(m.Name(), props, attrs)
Jingwen Chen73850672020-12-14 08:25:34 -0500309 }
310}
Jingwen Chen40067de2021-01-26 21:58:43 -0500311
312// A bp2build mutator that uses load statements and creates a 1:M mapping from
313// module to target.
314func customBp2BuildMutatorFromStarlark(ctx android.TopDownMutatorContext) {
315 if m, ok := ctx.Module().(*customModule); ok {
Jingwen Chen12b4c272021-03-10 02:05:59 -0500316 if !m.ConvertWithBp2build(ctx) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500317 return
318 }
319
Jingwen Chen1fd14692021-02-05 03:01:50 -0500320 baseName := m.Name()
321 attrs := &customBazelModuleAttributes{}
322
Liz Kammerfc46bc12021-02-19 11:06:17 -0500323 myLibraryProps := bazel.BazelTargetModuleProperties{
324 Rule_class: "my_library",
325 Bzl_load_location: "//build/bazel/rules:rules.bzl",
326 }
Liz Kammer2ada09a2021-08-11 00:17:36 -0400327 ctx.CreateBazelTargetModule(baseName, myLibraryProps, attrs)
Jingwen Chen1fd14692021-02-05 03:01:50 -0500328
Liz Kammerfc46bc12021-02-19 11:06:17 -0500329 protoLibraryProps := bazel.BazelTargetModuleProperties{
330 Rule_class: "proto_library",
331 Bzl_load_location: "//build/bazel/rules:proto.bzl",
332 }
Liz Kammer2ada09a2021-08-11 00:17:36 -0400333 ctx.CreateBazelTargetModule(baseName+"_proto_library_deps", protoLibraryProps, attrs)
Jingwen Chen1fd14692021-02-05 03:01:50 -0500334
Liz Kammerfc46bc12021-02-19 11:06:17 -0500335 myProtoLibraryProps := bazel.BazelTargetModuleProperties{
336 Rule_class: "my_proto_library",
337 Bzl_load_location: "//build/bazel/rules:proto.bzl",
338 }
Liz Kammer2ada09a2021-08-11 00:17:36 -0400339 ctx.CreateBazelTargetModule(baseName+"_my_proto_library_deps", myProtoLibraryProps, attrs)
Jingwen Chen40067de2021-01-26 21:58:43 -0500340 }
341}
Jingwen Chenba369ad2021-02-22 10:19:34 -0500342
343// Helper method for tests to easily access the targets in a dir.
Liz Kammerba3ea162021-02-17 13:22:03 -0500344func generateBazelTargetsForDir(codegenCtx *CodegenContext, dir string) BazelTargets {
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400345 // TODO: Set generateFilegroups to true and/or remove the generateFilegroups argument completely
Jingwen Chenc63677b2021-06-17 05:43:19 +0000346 buildFileToTargets, _, _ := GenerateBazelTargets(codegenCtx, false)
Jingwen Chenba369ad2021-02-22 10:19:34 -0500347 return buildFileToTargets[dir]
348}
Liz Kammer32b77cf2021-08-04 15:17:02 -0400349
350func registerCustomModuleForBp2buildConversion(ctx *android.TestContext) {
351 ctx.RegisterModuleType("custom", customModuleFactory)
352 ctx.RegisterBp2BuildMutator("custom", customBp2BuildMutator)
353 ctx.RegisterForBazelConversion()
354}