blob: c2c1b197dc3d642e49290282e0d53fe54058c15c [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 (
Rupert Shuttleworth06559d02021-05-19 09:14:26 -040033 buildDir string
Jingwen Chen91220d72021-03-24 02:18:33 -040034)
35
Jingwen Chen5146ac02021-09-02 11:44:42 +000036func 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 +000037 t.Helper()
Jingwen Chen5146ac02021-09-02 11:44:42 +000038
Jingwen Chen5146ac02021-09-02 11:44:42 +000039 if len(errs) != 1 {
Liz Kammer6eff3232021-08-26 08:37:59 -040040 return false
Jingwen Chen5146ac02021-09-02 11:44:42 +000041 }
Liz Kammer54309532021-12-14 12:21:22 -050042 if strings.Contains(errs[0].Error(), expectedErr.Error()) {
Jingwen Chen5146ac02021-09-02 11:44:42 +000043 return true
44 }
45
46 return false
47}
48
Sam Delmerico3177a6e2022-06-21 19:28:33 +000049func errored(t *testing.T, tc Bp2buildTestCase, errs []error) bool {
Jingwen Chen5146ac02021-09-02 11:44:42 +000050 t.Helper()
Sam Delmerico3177a6e2022-06-21 19:28:33 +000051 if tc.ExpectedErr != nil {
Jingwen Chen5146ac02021-09-02 11:44:42 +000052 // Rely on checkErrors, as this test case is expected to have an error.
53 return false
54 }
55
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +000056 if len(errs) > 0 {
57 for _, err := range errs {
Sam Delmerico3177a6e2022-06-21 19:28:33 +000058 t.Errorf("%s: %s", tc.Description, err)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +000059 }
60 return true
61 }
Jingwen Chen5146ac02021-09-02 11:44:42 +000062
63 // All good, continue execution.
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +000064 return false
65}
66
Trevor Radcliffe1b4b2d92022-09-01 18:57:01 +000067func RunBp2BuildTestCaseSimple(t *testing.T, tc Bp2buildTestCase) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +000068 t.Helper()
Sam Delmerico3177a6e2022-06-21 19:28:33 +000069 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, tc)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +000070}
71
Sam Delmerico3177a6e2022-06-21 19:28:33 +000072type Bp2buildTestCase struct {
73 Description string
74 ModuleTypeUnderTest string
75 ModuleTypeUnderTestFactory android.ModuleFactory
76 Blueprint string
77 ExpectedBazelTargets []string
78 Filesystem map[string]string
79 Dir string
Trevor Radcliffe58ea4512022-04-07 20:36:39 +000080 // An error with a string contained within the string of the expected error
Sam Delmerico3177a6e2022-06-21 19:28:33 +000081 ExpectedErr error
82 UnconvertedDepsMode unconvertedDepsMode
Jingwen Chen0eeaeb82022-09-21 10:27:42 +000083
84 // For every directory listed here, the BUILD file for that directory will
85 // be merged with the generated BUILD file. This allows custom BUILD targets
86 // to be used in tests, or use BUILD files to draw package boundaries.
87 KeepBuildFileForDirs []string
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +000088}
89
Sam Delmerico3177a6e2022-06-21 19:28:33 +000090func RunBp2BuildTestCase(t *testing.T, registerModuleTypes func(ctx android.RegistrationContext), tc Bp2buildTestCase) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +000091 t.Helper()
92 dir := "."
93 filesystem := make(map[string][]byte)
94 toParse := []string{
95 "Android.bp",
96 }
Sam Delmerico3177a6e2022-06-21 19:28:33 +000097 for f, content := range tc.Filesystem {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +000098 if strings.HasSuffix(f, "Android.bp") {
99 toParse = append(toParse, f)
100 }
101 filesystem[f] = []byte(content)
102 }
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000103 config := android.TestConfig(buildDir, nil, tc.Blueprint, filesystem)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000104 ctx := android.NewTestContext(config)
105
106 registerModuleTypes(ctx)
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000107 ctx.RegisterModuleType(tc.ModuleTypeUnderTest, tc.ModuleTypeUnderTestFactory)
Jingwen Chen0eeaeb82022-09-21 10:27:42 +0000108
109 // A default configuration for tests to not have to specify bp2build_available on top level targets.
110 bp2buildConfig := android.NewBp2BuildAllowlist().SetDefaultConfig(
111 allowlists.Bp2BuildConfig{
112 android.Bp2BuildTopLevel: allowlists.Bp2BuildDefaultTrueRecursively,
113 },
114 )
115 for _, f := range tc.KeepBuildFileForDirs {
116 bp2buildConfig.SetKeepExistingBuildFile(map[string]bool{
117 f: /*recursive=*/ false,
118 })
119 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000120 ctx.RegisterBp2BuildConfig(bp2buildConfig)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000121 ctx.RegisterForBazelConversion()
122
Jingwen Chen5146ac02021-09-02 11:44:42 +0000123 _, parseErrs := ctx.ParseFileList(dir, toParse)
124 if errored(t, tc, parseErrs) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000125 return
126 }
Jingwen Chen5146ac02021-09-02 11:44:42 +0000127 _, resolveDepsErrs := ctx.ResolveDependencies(config)
128 if errored(t, tc, resolveDepsErrs) {
129 return
130 }
131
Cole Faustb09da7e2022-05-18 10:57:33 -0700132 parseAndResolveErrs := append(parseErrs, resolveDepsErrs...)
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000133 if tc.ExpectedErr != nil && checkError(t, parseAndResolveErrs, tc.ExpectedErr) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000134 return
135 }
136
137 checkDir := dir
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000138 if tc.Dir != "" {
139 checkDir = tc.Dir
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000140 }
141 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000142 codegenCtx.unconvertedDepMode = tc.UnconvertedDepsMode
Liz Kammer6eff3232021-08-26 08:37:59 -0400143 bazelTargets, errs := generateBazelTargetsForDir(codegenCtx, checkDir)
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000144 if tc.ExpectedErr != nil {
145 if checkError(t, errs, tc.ExpectedErr) {
Liz Kammer54309532021-12-14 12:21:22 -0500146 return
147 } else {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000148 t.Errorf("Expected error: %q, got: %q and %q", tc.ExpectedErr, errs, parseAndResolveErrs)
Liz Kammer54309532021-12-14 12:21:22 -0500149 }
Liz Kammer6eff3232021-08-26 08:37:59 -0400150 } else {
151 android.FailIfErrored(t, errs)
152 }
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000153 if actualCount, expectedCount := len(bazelTargets), len(tc.ExpectedBazelTargets); actualCount != expectedCount {
Sasha Smundak9d2f1742022-08-04 13:28:38 -0700154 t.Errorf("%s: Expected %d bazel target (%s), got %d (%s)",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000155 tc.Description, expectedCount, tc.ExpectedBazelTargets, actualCount, bazelTargets)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000156 } else {
157 for i, target := range bazelTargets {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000158 if w, g := tc.ExpectedBazelTargets[i], target.content; w != g {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000159 t.Errorf(
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000160 "%s: Expected generated Bazel target to be `%s`, got `%s`",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000161 tc.Description, w, g)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux1c92aef2021-08-23 16:10:00 +0000162 }
163 }
164 }
165}
166
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800167type nestedProps struct {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500168 Nested_prop *string
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800169}
170
Liz Kammer32a03392021-09-14 11:17:21 -0400171type EmbeddedProps struct {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500172 Embedded_prop *string
Liz Kammer32a03392021-09-14 11:17:21 -0400173}
174
175type OtherEmbeddedProps struct {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500176 Other_embedded_prop *string
Liz Kammer32a03392021-09-14 11:17:21 -0400177}
178
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800179type customProps struct {
Liz Kammer32a03392021-09-14 11:17:21 -0400180 EmbeddedProps
181 *OtherEmbeddedProps
182
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800183 Bool_prop bool
184 Bool_ptr_prop *bool
185 // Ensure that properties tagged `blueprint:mutated` are omitted
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000186 Int_prop int `blueprint:"mutated"`
187 Int64_ptr_prop *int64
188 String_prop string
189 String_literal_prop *string `android:"arch_variant"`
190 String_ptr_prop *string
191 String_list_prop []string
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800192
193 Nested_props nestedProps
194 Nested_props_ptr *nestedProps
Liz Kammer4562a3b2021-04-21 18:15:34 -0400195
Liz Kammer32b77cf2021-08-04 15:17:02 -0400196 Arch_paths []string `android:"path,arch_variant"`
197 Arch_paths_exclude []string `android:"path,arch_variant"`
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400198
199 // Prop used to indicate this conversion should be 1 module -> multiple targets
200 One_to_many_prop *bool
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800201}
202
203type customModule struct {
204 android.ModuleBase
Liz Kammerea6666f2021-02-17 10:17:28 -0500205 android.BazelModuleBase
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800206
207 props customProps
208}
209
210// OutputFiles is needed because some instances of this module use dist with a
211// tag property which requires the module implements OutputFileProducer.
212func (m *customModule) OutputFiles(tag string) (android.Paths, error) {
213 return android.PathsForTesting("path" + tag), nil
214}
215
216func (m *customModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
217 // nothing for now.
218}
219
220func customModuleFactoryBase() android.Module {
221 module := &customModule{}
222 module.AddProperties(&module.props)
Liz Kammerea6666f2021-02-17 10:17:28 -0500223 android.InitBazelModule(module)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800224 return module
225}
226
Liz Kammerdfeb1202022-05-13 17:20:20 -0400227func customModuleFactoryHostAndDevice() android.Module {
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800228 m := customModuleFactoryBase()
Liz Kammer4562a3b2021-04-21 18:15:34 -0400229 android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibBoth)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800230 return m
231}
232
Liz Kammerdfeb1202022-05-13 17:20:20 -0400233func customModuleFactoryDeviceSupported() android.Module {
234 m := customModuleFactoryBase()
235 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibBoth)
236 return m
237}
238
239func customModuleFactoryHostSupported() android.Module {
240 m := customModuleFactoryBase()
241 android.InitAndroidArchModule(m, android.HostSupported, android.MultilibBoth)
242 return m
243}
244
245func customModuleFactoryHostAndDeviceDefault() android.Module {
246 m := customModuleFactoryBase()
247 android.InitAndroidArchModule(m, android.HostAndDeviceDefault, android.MultilibBoth)
248 return m
249}
250
251func customModuleFactoryNeitherHostNorDeviceSupported() android.Module {
252 m := customModuleFactoryBase()
253 android.InitAndroidArchModule(m, android.NeitherHostNorDeviceSupported, android.MultilibBoth)
254 return m
255}
256
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800257type testProps struct {
258 Test_prop struct {
259 Test_string_prop string
260 }
261}
262
263type customTestModule struct {
264 android.ModuleBase
265
266 props customProps
267 test_props testProps
268}
269
270func (m *customTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
271 // nothing for now.
272}
273
274func customTestModuleFactoryBase() android.Module {
275 m := &customTestModule{}
276 m.AddProperties(&m.props)
277 m.AddProperties(&m.test_props)
278 return m
279}
280
281func customTestModuleFactory() android.Module {
282 m := customTestModuleFactoryBase()
283 android.InitAndroidModule(m)
284 return m
285}
286
287type customDefaultsModule struct {
288 android.ModuleBase
289 android.DefaultsModuleBase
290}
291
292func customDefaultsModuleFactoryBase() android.DefaultsModule {
293 module := &customDefaultsModule{}
294 module.AddProperties(&customProps{})
295 return module
296}
297
298func customDefaultsModuleFactoryBasic() android.Module {
299 return customDefaultsModuleFactoryBase()
300}
301
302func customDefaultsModuleFactory() android.Module {
303 m := customDefaultsModuleFactoryBase()
304 android.InitDefaultsModule(m)
305 return m
306}
Jingwen Chen73850672020-12-14 08:25:34 -0500307
Liz Kammer32a03392021-09-14 11:17:21 -0400308type EmbeddedAttr struct {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500309 Embedded_attr *string
Liz Kammer32a03392021-09-14 11:17:21 -0400310}
311
312type OtherEmbeddedAttr struct {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500313 Other_embedded_attr *string
Liz Kammer32a03392021-09-14 11:17:21 -0400314}
315
Jingwen Chen73850672020-12-14 08:25:34 -0500316type customBazelModuleAttributes struct {
Liz Kammer32a03392021-09-14 11:17:21 -0400317 EmbeddedAttr
318 *OtherEmbeddedAttr
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000319 String_literal_prop bazel.StringAttribute
320 String_ptr_prop *string
321 String_list_prop []string
322 Arch_paths bazel.LabelListAttribute
Jingwen Chen73850672020-12-14 08:25:34 -0500323}
324
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400325func (m *customModule) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400326 if p := m.props.One_to_many_prop; p != nil && *p {
327 customBp2buildOneToMany(ctx, m)
328 return
329 }
Liz Kammer4562a3b2021-04-21 18:15:34 -0400330
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000331 paths := bazel.LabelListAttribute{}
332 strAttr := bazel.StringAttribute{}
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400333 for axis, configToProps := range m.GetArchVariantProperties(ctx, &customProps{}) {
334 for config, props := range configToProps {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000335 if custProps, ok := props.(*customProps); ok {
336 if custProps.Arch_paths != nil {
337 paths.SetSelectValue(axis, config, android.BazelLabelForModuleSrcExcludes(ctx, custProps.Arch_paths, custProps.Arch_paths_exclude))
338 }
339 if custProps.String_literal_prop != nil {
340 strAttr.SetSelectValue(axis, config, custProps.String_literal_prop)
341 }
Liz Kammer4562a3b2021-04-21 18:15:34 -0400342 }
343 }
Jingwen Chen73850672020-12-14 08:25:34 -0500344 }
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400345
346 paths.ResolveExcludes()
347
348 attrs := &customBazelModuleAttributes{
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000349 String_literal_prop: strAttr,
350 String_ptr_prop: m.props.String_ptr_prop,
351 String_list_prop: m.props.String_list_prop,
352 Arch_paths: paths,
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400353 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000354
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400355 attrs.Embedded_attr = m.props.Embedded_prop
356 if m.props.OtherEmbeddedProps != nil {
357 attrs.OtherEmbeddedAttr = &OtherEmbeddedAttr{Other_embedded_attr: m.props.OtherEmbeddedProps.Other_embedded_prop}
358 }
359
360 props := bazel.BazelTargetModuleProperties{
361 Rule_class: "custom",
362 }
363
364 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, attrs)
Jingwen Chen73850672020-12-14 08:25:34 -0500365}
Jingwen Chen40067de2021-01-26 21:58:43 -0500366
367// A bp2build mutator that uses load statements and creates a 1:M mapping from
368// module to target.
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400369func customBp2buildOneToMany(ctx android.TopDownMutatorContext, m *customModule) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500370
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400371 baseName := m.Name()
372 attrs := &customBazelModuleAttributes{}
Jingwen Chen1fd14692021-02-05 03:01:50 -0500373
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400374 myLibraryProps := bazel.BazelTargetModuleProperties{
375 Rule_class: "my_library",
376 Bzl_load_location: "//build/bazel/rules:rules.bzl",
Jingwen Chen40067de2021-01-26 21:58:43 -0500377 }
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400378 ctx.CreateBazelTargetModule(myLibraryProps, android.CommonAttributes{Name: baseName}, attrs)
379
380 protoLibraryProps := bazel.BazelTargetModuleProperties{
381 Rule_class: "proto_library",
382 Bzl_load_location: "//build/bazel/rules:proto.bzl",
383 }
384 ctx.CreateBazelTargetModule(protoLibraryProps, android.CommonAttributes{Name: baseName + "_proto_library_deps"}, attrs)
385
386 myProtoLibraryProps := bazel.BazelTargetModuleProperties{
387 Rule_class: "my_proto_library",
388 Bzl_load_location: "//build/bazel/rules:proto.bzl",
389 }
390 ctx.CreateBazelTargetModule(myProtoLibraryProps, android.CommonAttributes{Name: baseName + "_my_proto_library_deps"}, attrs)
Jingwen Chen40067de2021-01-26 21:58:43 -0500391}
Jingwen Chenba369ad2021-02-22 10:19:34 -0500392
393// Helper method for tests to easily access the targets in a dir.
Liz Kammer6eff3232021-08-26 08:37:59 -0400394func generateBazelTargetsForDir(codegenCtx *CodegenContext, dir string) (BazelTargets, []error) {
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400395 // TODO: Set generateFilegroups to true and/or remove the generateFilegroups argument completely
Liz Kammer6eff3232021-08-26 08:37:59 -0400396 res, err := GenerateBazelTargets(codegenCtx, false)
Alix94e26032022-08-16 20:37:33 +0000397 if err != nil {
398 return BazelTargets{}, err
399 }
Liz Kammer6eff3232021-08-26 08:37:59 -0400400 return res.buildFileToTargets[dir], err
Jingwen Chenba369ad2021-02-22 10:19:34 -0500401}
Liz Kammer32b77cf2021-08-04 15:17:02 -0400402
403func registerCustomModuleForBp2buildConversion(ctx *android.TestContext) {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400404 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
Liz Kammer32b77cf2021-08-04 15:17:02 -0400405 ctx.RegisterForBazelConversion()
406}
Liz Kammer7a210ac2021-09-22 15:52:58 -0400407
408func simpleModuleDoNotConvertBp2build(typ, name string) string {
409 return fmt.Sprintf(`
410%s {
411 name: "%s",
412 bazel_module: { bp2build_available: false },
413}`, typ, name)
414}
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500415
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000416type AttrNameToString map[string]string
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500417
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000418func (a AttrNameToString) clone() AttrNameToString {
419 newAttrs := make(AttrNameToString, len(a))
Liz Kammerdfeb1202022-05-13 17:20:20 -0400420 for k, v := range a {
421 newAttrs[k] = v
422 }
423 return newAttrs
424}
425
426// makeBazelTargetNoRestrictions returns bazel target build file definition that can be host or
427// device specific, or independent of host/device.
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000428func makeBazelTargetHostOrDevice(typ, name string, attrs AttrNameToString, hod android.HostOrDeviceSupported) string {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400429 if _, ok := attrs["target_compatible_with"]; !ok {
430 switch hod {
431 case android.HostSupported:
432 attrs["target_compatible_with"] = `select({
433 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
434 "//conditions:default": [],
435 })`
436 case android.DeviceSupported:
437 attrs["target_compatible_with"] = `["//build/bazel/platforms/os:android"]`
438 }
439 }
440
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500441 attrStrings := make([]string, 0, len(attrs)+1)
Sasha Smundakfb589492022-08-04 11:13:27 -0700442 if name != "" {
443 attrStrings = append(attrStrings, fmt.Sprintf(` name = "%s",`, name))
444 }
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500445 for _, k := range android.SortedStringKeys(attrs) {
446 attrStrings = append(attrStrings, fmt.Sprintf(" %s = %s,", k, attrs[k]))
447 }
448 return fmt.Sprintf(`%s(
449%s
450)`, typ, strings.Join(attrStrings, "\n"))
451}
Liz Kammerdfeb1202022-05-13 17:20:20 -0400452
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000453// MakeBazelTargetNoRestrictions returns bazel target build file definition that does not add a
Liz Kammerdfeb1202022-05-13 17:20:20 -0400454// target_compatible_with. This is useful for module types like filegroup and genrule that arch not
455// arch variant
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000456func MakeBazelTargetNoRestrictions(typ, name string, attrs AttrNameToString) string {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400457 return makeBazelTargetHostOrDevice(typ, name, attrs, android.HostAndDeviceDefault)
458}
459
460// makeBazelTargetNoRestrictions returns bazel target build file definition that is device specific
461// as this is the most common default in Soong.
Alixe06d75b2022-08-31 18:28:19 +0000462func MakeBazelTarget(typ, name string, attrs AttrNameToString) string {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400463 return makeBazelTargetHostOrDevice(typ, name, attrs, android.DeviceSupported)
464}
Sasha Smundak9d2f1742022-08-04 13:28:38 -0700465
466type ExpectedRuleTarget struct {
467 Rule string
468 Name string
469 Attrs AttrNameToString
470 Hod android.HostOrDeviceSupported
471}
472
473func (ebr ExpectedRuleTarget) String() string {
474 return makeBazelTargetHostOrDevice(ebr.Rule, ebr.Name, ebr.Attrs, ebr.Hod)
475}
Trevor Radcliffe087af542022-09-16 15:36:10 +0000476
477func makeCcStubSuiteTargets(name string, attrs AttrNameToString) string {
478 if _, hasStubs := attrs["stubs_symbol_file"]; !hasStubs {
479 return ""
480 }
481 STUB_SUITE_ATTRS := map[string]string{
482 "stubs_symbol_file": "symbol_file",
483 "stubs_versions": "versions",
484 "soname": "soname",
485 "source_library": "source_library",
486 }
487
488 stubSuiteAttrs := AttrNameToString{}
489 for key, _ := range attrs {
490 if _, stubSuiteAttr := STUB_SUITE_ATTRS[key]; stubSuiteAttr {
491 stubSuiteAttrs[STUB_SUITE_ATTRS[key]] = attrs[key]
492 }
493 }
494 return MakeBazelTarget("cc_stub_suite", name+"_stub_libs", stubSuiteAttrs)
495}