blob: 3a77d0e231463f90d79cc3019f928e7a60d5e295 [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
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +000039func errored(t *testing.T, desc string, errs []error) bool {
40 t.Helper()
41 if len(errs) > 0 {
42 for _, err := range errs {
43 t.Errorf("%s: %s", desc, err)
44 }
45 return true
46 }
47 return false
48}
49
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxce0a07e2021-08-23 16:17:32 +000050func runBp2BuildTestCaseSimple(t *testing.T, tc bp2buildTestCase) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +000051 t.Helper()
52 runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, tc)
53}
54
55type bp2buildTestCase struct {
56 description string
57 moduleTypeUnderTest string
58 moduleTypeUnderTestFactory android.ModuleFactory
59 moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
60 blueprint string
61 expectedBazelTargets []string
62 filesystem map[string]string
63 dir string
64}
65
66func runBp2BuildTestCase(t *testing.T, registerModuleTypes func(ctx android.RegistrationContext), tc bp2buildTestCase) {
67 t.Helper()
68 dir := "."
69 filesystem := make(map[string][]byte)
70 toParse := []string{
71 "Android.bp",
72 }
73 for f, content := range tc.filesystem {
74 if strings.HasSuffix(f, "Android.bp") {
75 toParse = append(toParse, f)
76 }
77 filesystem[f] = []byte(content)
78 }
79 config := android.TestConfig(buildDir, nil, tc.blueprint, filesystem)
80 ctx := android.NewTestContext(config)
81
82 registerModuleTypes(ctx)
83 ctx.RegisterModuleType(tc.moduleTypeUnderTest, tc.moduleTypeUnderTestFactory)
84 ctx.RegisterBp2BuildConfig(bp2buildConfig)
85 ctx.RegisterBp2BuildMutator(tc.moduleTypeUnderTest, tc.moduleTypeUnderTestBp2BuildMutator)
86 ctx.RegisterForBazelConversion()
87
88 _, errs := ctx.ParseFileList(dir, toParse)
89 if errored(t, tc.description, errs) {
90 return
91 }
92 _, errs = ctx.ResolveDependencies(config)
93 if errored(t, tc.description, errs) {
94 return
95 }
96
97 checkDir := dir
98 if tc.dir != "" {
99 checkDir = tc.dir
100 }
101 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
102 bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir)
103 if actualCount, expectedCount := len(bazelTargets), len(tc.expectedBazelTargets); actualCount != expectedCount {
104 t.Errorf("%s: Expected %d bazel target, got %d", tc.description, expectedCount, actualCount)
105 } else {
106 for i, target := range bazelTargets {
107 if w, g := tc.expectedBazelTargets[i], target.content; w != g {
108 t.Errorf(
109 "%s: Expected generated Bazel target to be '%s', got '%s'",
110 tc.description,
111 w,
112 g,
113 )
114 }
115 }
116 }
117}
118
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800119type nestedProps struct {
120 Nested_prop string
121}
122
123type customProps struct {
124 Bool_prop bool
125 Bool_ptr_prop *bool
126 // Ensure that properties tagged `blueprint:mutated` are omitted
127 Int_prop int `blueprint:"mutated"`
128 Int64_ptr_prop *int64
129 String_prop string
130 String_ptr_prop *string
131 String_list_prop []string
132
133 Nested_props nestedProps
134 Nested_props_ptr *nestedProps
Liz Kammer4562a3b2021-04-21 18:15:34 -0400135
Liz Kammer32b77cf2021-08-04 15:17:02 -0400136 Arch_paths []string `android:"path,arch_variant"`
137 Arch_paths_exclude []string `android:"path,arch_variant"`
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800138}
139
140type customModule struct {
141 android.ModuleBase
Liz Kammerea6666f2021-02-17 10:17:28 -0500142 android.BazelModuleBase
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800143
144 props customProps
145}
146
147// OutputFiles is needed because some instances of this module use dist with a
148// tag property which requires the module implements OutputFileProducer.
149func (m *customModule) OutputFiles(tag string) (android.Paths, error) {
150 return android.PathsForTesting("path" + tag), nil
151}
152
153func (m *customModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
154 // nothing for now.
155}
156
157func customModuleFactoryBase() android.Module {
158 module := &customModule{}
159 module.AddProperties(&module.props)
Liz Kammerea6666f2021-02-17 10:17:28 -0500160 android.InitBazelModule(module)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800161 return module
162}
163
164func customModuleFactory() android.Module {
165 m := customModuleFactoryBase()
Liz Kammer4562a3b2021-04-21 18:15:34 -0400166 android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibBoth)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800167 return m
168}
169
170type testProps struct {
171 Test_prop struct {
172 Test_string_prop string
173 }
174}
175
176type customTestModule struct {
177 android.ModuleBase
178
179 props customProps
180 test_props testProps
181}
182
183func (m *customTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
184 // nothing for now.
185}
186
187func customTestModuleFactoryBase() android.Module {
188 m := &customTestModule{}
189 m.AddProperties(&m.props)
190 m.AddProperties(&m.test_props)
191 return m
192}
193
194func customTestModuleFactory() android.Module {
195 m := customTestModuleFactoryBase()
196 android.InitAndroidModule(m)
197 return m
198}
199
200type customDefaultsModule struct {
201 android.ModuleBase
202 android.DefaultsModuleBase
203}
204
205func customDefaultsModuleFactoryBase() android.DefaultsModule {
206 module := &customDefaultsModule{}
207 module.AddProperties(&customProps{})
208 return module
209}
210
211func customDefaultsModuleFactoryBasic() android.Module {
212 return customDefaultsModuleFactoryBase()
213}
214
215func customDefaultsModuleFactory() android.Module {
216 m := customDefaultsModuleFactoryBase()
217 android.InitDefaultsModule(m)
218 return m
219}
Jingwen Chen73850672020-12-14 08:25:34 -0500220
221type customBazelModuleAttributes struct {
Jingwen Chen73850672020-12-14 08:25:34 -0500222 String_prop string
223 String_list_prop []string
Liz Kammer4562a3b2021-04-21 18:15:34 -0400224 Arch_paths bazel.LabelListAttribute
Jingwen Chen73850672020-12-14 08:25:34 -0500225}
226
227type customBazelModule struct {
228 android.BazelTargetModuleBase
229 customBazelModuleAttributes
230}
231
Jingwen Chen73850672020-12-14 08:25:34 -0500232func customBp2BuildMutator(ctx android.TopDownMutatorContext) {
233 if m, ok := ctx.Module().(*customModule); ok {
Jingwen Chen12b4c272021-03-10 02:05:59 -0500234 if !m.ConvertWithBp2build(ctx) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500235 return
236 }
237
Liz Kammer32b77cf2021-08-04 15:17:02 -0400238 paths := bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrcExcludes(ctx, m.props.Arch_paths, m.props.Arch_paths_exclude))
Liz Kammer4562a3b2021-04-21 18:15:34 -0400239
Liz Kammer9abd62d2021-05-21 08:37:59 -0400240 for axis, configToProps := range m.GetArchVariantProperties(ctx, &customProps{}) {
241 for config, props := range configToProps {
242 if archProps, ok := props.(*customProps); ok && archProps.Arch_paths != nil {
Liz Kammer32b77cf2021-08-04 15:17:02 -0400243 paths.SetSelectValue(axis, config, android.BazelLabelForModuleSrcExcludes(ctx, archProps.Arch_paths, archProps.Arch_paths_exclude))
Liz Kammer9abd62d2021-05-21 08:37:59 -0400244 }
Liz Kammer4562a3b2021-04-21 18:15:34 -0400245 }
246 }
247
Liz Kammer32b77cf2021-08-04 15:17:02 -0400248 paths.ResolveExcludes()
249
Jingwen Chen1fd14692021-02-05 03:01:50 -0500250 attrs := &customBazelModuleAttributes{
Jingwen Chen73850672020-12-14 08:25:34 -0500251 String_prop: m.props.String_prop,
252 String_list_prop: m.props.String_list_prop,
Liz Kammer4562a3b2021-04-21 18:15:34 -0400253 Arch_paths: paths,
Jingwen Chen1fd14692021-02-05 03:01:50 -0500254 }
255
Liz Kammerfc46bc12021-02-19 11:06:17 -0500256 props := bazel.BazelTargetModuleProperties{
257 Rule_class: "custom",
258 }
Jingwen Chen1fd14692021-02-05 03:01:50 -0500259
Liz Kammer2ada09a2021-08-11 00:17:36 -0400260 ctx.CreateBazelTargetModule(m.Name(), props, attrs)
Jingwen Chen73850672020-12-14 08:25:34 -0500261 }
262}
Jingwen Chen40067de2021-01-26 21:58:43 -0500263
264// A bp2build mutator that uses load statements and creates a 1:M mapping from
265// module to target.
266func customBp2BuildMutatorFromStarlark(ctx android.TopDownMutatorContext) {
267 if m, ok := ctx.Module().(*customModule); ok {
Jingwen Chen12b4c272021-03-10 02:05:59 -0500268 if !m.ConvertWithBp2build(ctx) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500269 return
270 }
271
Jingwen Chen1fd14692021-02-05 03:01:50 -0500272 baseName := m.Name()
273 attrs := &customBazelModuleAttributes{}
274
Liz Kammerfc46bc12021-02-19 11:06:17 -0500275 myLibraryProps := bazel.BazelTargetModuleProperties{
276 Rule_class: "my_library",
277 Bzl_load_location: "//build/bazel/rules:rules.bzl",
278 }
Liz Kammer2ada09a2021-08-11 00:17:36 -0400279 ctx.CreateBazelTargetModule(baseName, myLibraryProps, attrs)
Jingwen Chen1fd14692021-02-05 03:01:50 -0500280
Liz Kammerfc46bc12021-02-19 11:06:17 -0500281 protoLibraryProps := bazel.BazelTargetModuleProperties{
282 Rule_class: "proto_library",
283 Bzl_load_location: "//build/bazel/rules:proto.bzl",
284 }
Liz Kammer2ada09a2021-08-11 00:17:36 -0400285 ctx.CreateBazelTargetModule(baseName+"_proto_library_deps", protoLibraryProps, attrs)
Jingwen Chen1fd14692021-02-05 03:01:50 -0500286
Liz Kammerfc46bc12021-02-19 11:06:17 -0500287 myProtoLibraryProps := bazel.BazelTargetModuleProperties{
288 Rule_class: "my_proto_library",
289 Bzl_load_location: "//build/bazel/rules:proto.bzl",
290 }
Liz Kammer2ada09a2021-08-11 00:17:36 -0400291 ctx.CreateBazelTargetModule(baseName+"_my_proto_library_deps", myProtoLibraryProps, attrs)
Jingwen Chen40067de2021-01-26 21:58:43 -0500292 }
293}
Jingwen Chenba369ad2021-02-22 10:19:34 -0500294
295// Helper method for tests to easily access the targets in a dir.
Liz Kammerba3ea162021-02-17 13:22:03 -0500296func generateBazelTargetsForDir(codegenCtx *CodegenContext, dir string) BazelTargets {
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400297 // TODO: Set generateFilegroups to true and/or remove the generateFilegroups argument completely
Jingwen Chenc63677b2021-06-17 05:43:19 +0000298 buildFileToTargets, _, _ := GenerateBazelTargets(codegenCtx, false)
Jingwen Chenba369ad2021-02-22 10:19:34 -0500299 return buildFileToTargets[dir]
300}
Liz Kammer32b77cf2021-08-04 15:17:02 -0400301
302func registerCustomModuleForBp2buildConversion(ctx *android.TestContext) {
303 ctx.RegisterModuleType("custom", customModuleFactory)
304 ctx.RegisterBp2BuildMutator("custom", customBp2BuildMutator)
305 ctx.RegisterForBazelConversion()
306}