blob: 04b8056378e6c21143791d1e07e713fc622cc957 [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
Jingwen Chen5146ac02021-09-02 11:44:42 +000042 if len(errs) != 1 {
Liz Kammer6eff3232021-08-26 08:37:59 -040043 return false
Jingwen Chen5146ac02021-09-02 11:44:42 +000044 }
45 if errs[0].Error() == expectedErr.Error() {
46 return true
47 }
48
49 return false
50}
51
52func errored(t *testing.T, tc bp2buildTestCase, errs []error) bool {
53 t.Helper()
54 if tc.expectedErr != nil {
55 // Rely on checkErrors, as this test case is expected to have an error.
56 return false
57 }
58
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +000059 if len(errs) > 0 {
60 for _, err := range errs {
Jingwen Chen5146ac02021-09-02 11:44:42 +000061 t.Errorf("%s: %s", tc.description, err)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +000062 }
63 return true
64 }
Jingwen Chen5146ac02021-09-02 11:44:42 +000065
66 // All good, continue execution.
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +000067 return false
68}
69
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxce0a07e2021-08-23 16:17:32 +000070func runBp2BuildTestCaseSimple(t *testing.T, tc bp2buildTestCase) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +000071 t.Helper()
72 runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, tc)
73}
74
75type bp2buildTestCase struct {
76 description string
77 moduleTypeUnderTest string
78 moduleTypeUnderTestFactory android.ModuleFactory
79 moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
80 blueprint string
81 expectedBazelTargets []string
82 filesystem map[string]string
83 dir string
Jingwen Chen5146ac02021-09-02 11:44:42 +000084 expectedErr error
Liz Kammer6eff3232021-08-26 08:37:59 -040085 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)
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)
Liz Kammer6eff3232021-08-26 08:37:59 -0400129 codegenCtx.unconvertedDepMode = tc.unconvertedDepsMode
130 bazelTargets, errs := generateBazelTargetsForDir(codegenCtx, checkDir)
131 if tc.expectedErr != nil && checkError(t, errs, tc.expectedErr) {
132 return
133 } else {
134 android.FailIfErrored(t, errs)
135 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000136 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 +0000137 t.Errorf("%s: Expected %d bazel target, got %d; %v",
138 tc.description, expectedCount, actualCount, bazelTargets)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000139 } else {
140 for i, target := range bazelTargets {
141 if w, g := tc.expectedBazelTargets[i], target.content; w != g {
142 t.Errorf(
143 "%s: Expected generated Bazel target to be '%s', got '%s'",
144 tc.description,
145 w,
146 g,
147 )
148 }
149 }
150 }
151}
152
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800153type nestedProps struct {
154 Nested_prop string
155}
156
157type customProps struct {
158 Bool_prop bool
159 Bool_ptr_prop *bool
160 // Ensure that properties tagged `blueprint:mutated` are omitted
161 Int_prop int `blueprint:"mutated"`
162 Int64_ptr_prop *int64
163 String_prop string
164 String_ptr_prop *string
165 String_list_prop []string
166
167 Nested_props nestedProps
168 Nested_props_ptr *nestedProps
Liz Kammer4562a3b2021-04-21 18:15:34 -0400169
Liz Kammer32b77cf2021-08-04 15:17:02 -0400170 Arch_paths []string `android:"path,arch_variant"`
171 Arch_paths_exclude []string `android:"path,arch_variant"`
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800172}
173
174type customModule struct {
175 android.ModuleBase
Liz Kammerea6666f2021-02-17 10:17:28 -0500176 android.BazelModuleBase
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800177
178 props customProps
179}
180
181// OutputFiles is needed because some instances of this module use dist with a
182// tag property which requires the module implements OutputFileProducer.
183func (m *customModule) OutputFiles(tag string) (android.Paths, error) {
184 return android.PathsForTesting("path" + tag), nil
185}
186
187func (m *customModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
188 // nothing for now.
189}
190
191func customModuleFactoryBase() android.Module {
192 module := &customModule{}
193 module.AddProperties(&module.props)
Liz Kammerea6666f2021-02-17 10:17:28 -0500194 android.InitBazelModule(module)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800195 return module
196}
197
198func customModuleFactory() android.Module {
199 m := customModuleFactoryBase()
Liz Kammer4562a3b2021-04-21 18:15:34 -0400200 android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibBoth)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800201 return m
202}
203
204type testProps struct {
205 Test_prop struct {
206 Test_string_prop string
207 }
208}
209
210type customTestModule struct {
211 android.ModuleBase
212
213 props customProps
214 test_props testProps
215}
216
217func (m *customTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
218 // nothing for now.
219}
220
221func customTestModuleFactoryBase() android.Module {
222 m := &customTestModule{}
223 m.AddProperties(&m.props)
224 m.AddProperties(&m.test_props)
225 return m
226}
227
228func customTestModuleFactory() android.Module {
229 m := customTestModuleFactoryBase()
230 android.InitAndroidModule(m)
231 return m
232}
233
234type customDefaultsModule struct {
235 android.ModuleBase
236 android.DefaultsModuleBase
237}
238
239func customDefaultsModuleFactoryBase() android.DefaultsModule {
240 module := &customDefaultsModule{}
241 module.AddProperties(&customProps{})
242 return module
243}
244
245func customDefaultsModuleFactoryBasic() android.Module {
246 return customDefaultsModuleFactoryBase()
247}
248
249func customDefaultsModuleFactory() android.Module {
250 m := customDefaultsModuleFactoryBase()
251 android.InitDefaultsModule(m)
252 return m
253}
Jingwen Chen73850672020-12-14 08:25:34 -0500254
255type customBazelModuleAttributes struct {
Jingwen Chen73850672020-12-14 08:25:34 -0500256 String_prop string
257 String_list_prop []string
Liz Kammer4562a3b2021-04-21 18:15:34 -0400258 Arch_paths bazel.LabelListAttribute
Jingwen Chen73850672020-12-14 08:25:34 -0500259}
260
Jingwen Chen73850672020-12-14 08:25:34 -0500261func customBp2BuildMutator(ctx android.TopDownMutatorContext) {
262 if m, ok := ctx.Module().(*customModule); ok {
Jingwen Chen12b4c272021-03-10 02:05:59 -0500263 if !m.ConvertWithBp2build(ctx) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500264 return
265 }
266
Liz Kammer32b77cf2021-08-04 15:17:02 -0400267 paths := bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrcExcludes(ctx, m.props.Arch_paths, m.props.Arch_paths_exclude))
Liz Kammer4562a3b2021-04-21 18:15:34 -0400268
Liz Kammer9abd62d2021-05-21 08:37:59 -0400269 for axis, configToProps := range m.GetArchVariantProperties(ctx, &customProps{}) {
270 for config, props := range configToProps {
271 if archProps, ok := props.(*customProps); ok && archProps.Arch_paths != nil {
Liz Kammer32b77cf2021-08-04 15:17:02 -0400272 paths.SetSelectValue(axis, config, android.BazelLabelForModuleSrcExcludes(ctx, archProps.Arch_paths, archProps.Arch_paths_exclude))
Liz Kammer9abd62d2021-05-21 08:37:59 -0400273 }
Liz Kammer4562a3b2021-04-21 18:15:34 -0400274 }
275 }
276
Liz Kammer32b77cf2021-08-04 15:17:02 -0400277 paths.ResolveExcludes()
278
Jingwen Chen1fd14692021-02-05 03:01:50 -0500279 attrs := &customBazelModuleAttributes{
Jingwen Chen73850672020-12-14 08:25:34 -0500280 String_prop: m.props.String_prop,
281 String_list_prop: m.props.String_list_prop,
Liz Kammer4562a3b2021-04-21 18:15:34 -0400282 Arch_paths: paths,
Jingwen Chen1fd14692021-02-05 03:01:50 -0500283 }
284
Liz Kammerfc46bc12021-02-19 11:06:17 -0500285 props := bazel.BazelTargetModuleProperties{
286 Rule_class: "custom",
287 }
Jingwen Chen1fd14692021-02-05 03:01:50 -0500288
Liz Kammer2ada09a2021-08-11 00:17:36 -0400289 ctx.CreateBazelTargetModule(m.Name(), props, attrs)
Jingwen Chen73850672020-12-14 08:25:34 -0500290 }
291}
Jingwen Chen40067de2021-01-26 21:58:43 -0500292
293// A bp2build mutator that uses load statements and creates a 1:M mapping from
294// module to target.
295func customBp2BuildMutatorFromStarlark(ctx android.TopDownMutatorContext) {
296 if m, ok := ctx.Module().(*customModule); ok {
Jingwen Chen12b4c272021-03-10 02:05:59 -0500297 if !m.ConvertWithBp2build(ctx) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500298 return
299 }
300
Jingwen Chen1fd14692021-02-05 03:01:50 -0500301 baseName := m.Name()
302 attrs := &customBazelModuleAttributes{}
303
Liz Kammerfc46bc12021-02-19 11:06:17 -0500304 myLibraryProps := bazel.BazelTargetModuleProperties{
305 Rule_class: "my_library",
306 Bzl_load_location: "//build/bazel/rules:rules.bzl",
307 }
Liz Kammer2ada09a2021-08-11 00:17:36 -0400308 ctx.CreateBazelTargetModule(baseName, myLibraryProps, attrs)
Jingwen Chen1fd14692021-02-05 03:01:50 -0500309
Liz Kammerfc46bc12021-02-19 11:06:17 -0500310 protoLibraryProps := bazel.BazelTargetModuleProperties{
311 Rule_class: "proto_library",
312 Bzl_load_location: "//build/bazel/rules:proto.bzl",
313 }
Liz Kammer2ada09a2021-08-11 00:17:36 -0400314 ctx.CreateBazelTargetModule(baseName+"_proto_library_deps", protoLibraryProps, attrs)
Jingwen Chen1fd14692021-02-05 03:01:50 -0500315
Liz Kammerfc46bc12021-02-19 11:06:17 -0500316 myProtoLibraryProps := bazel.BazelTargetModuleProperties{
317 Rule_class: "my_proto_library",
318 Bzl_load_location: "//build/bazel/rules:proto.bzl",
319 }
Liz Kammer2ada09a2021-08-11 00:17:36 -0400320 ctx.CreateBazelTargetModule(baseName+"_my_proto_library_deps", myProtoLibraryProps, attrs)
Jingwen Chen40067de2021-01-26 21:58:43 -0500321 }
322}
Jingwen Chenba369ad2021-02-22 10:19:34 -0500323
324// Helper method for tests to easily access the targets in a dir.
Liz Kammer6eff3232021-08-26 08:37:59 -0400325func generateBazelTargetsForDir(codegenCtx *CodegenContext, dir string) (BazelTargets, []error) {
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400326 // TODO: Set generateFilegroups to true and/or remove the generateFilegroups argument completely
Liz Kammer6eff3232021-08-26 08:37:59 -0400327 res, err := GenerateBazelTargets(codegenCtx, false)
328 return res.buildFileToTargets[dir], err
Jingwen Chenba369ad2021-02-22 10:19:34 -0500329}
Liz Kammer32b77cf2021-08-04 15:17:02 -0400330
331func registerCustomModuleForBp2buildConversion(ctx *android.TestContext) {
332 ctx.RegisterModuleType("custom", customModuleFactory)
333 ctx.RegisterBp2BuildMutator("custom", customBp2BuildMutator)
334 ctx.RegisterForBazelConversion()
335}