blob: f3cd7f07f9821d947b2acf8369ae903d8f9edeb2 [file] [log] [blame]
Liz Kammer2dd9ca42020-11-25 16:06:39 -08001package bp2build
2
3import (
Rupert Shuttleworth06559d02021-05-19 09:14:26 -04004 "testing"
5
Liz Kammer2dd9ca42020-11-25 16:06:39 -08006 "android/soong/android"
Jingwen Chen73850672020-12-14 08:25:34 -05007 "android/soong/bazel"
Liz Kammer2dd9ca42020-11-25 16:06:39 -08008)
9
Jingwen Chen91220d72021-03-24 02:18:33 -040010var (
11 // A default configuration for tests to not have to specify bp2build_available on top level targets.
12 bp2buildConfig = android.Bp2BuildConfig{
13 android.BP2BUILD_TOPLEVEL: android.Bp2BuildDefaultTrueRecursively,
14 }
Rupert Shuttleworth06559d02021-05-19 09:14:26 -040015
16 buildDir string
Jingwen Chen91220d72021-03-24 02:18:33 -040017)
18
Liz Kammer2dd9ca42020-11-25 16:06:39 -080019type nestedProps struct {
20 Nested_prop string
21}
22
23type customProps struct {
24 Bool_prop bool
25 Bool_ptr_prop *bool
26 // Ensure that properties tagged `blueprint:mutated` are omitted
27 Int_prop int `blueprint:"mutated"`
28 Int64_ptr_prop *int64
29 String_prop string
30 String_ptr_prop *string
31 String_list_prop []string
32
33 Nested_props nestedProps
34 Nested_props_ptr *nestedProps
Liz Kammer4562a3b2021-04-21 18:15:34 -040035
36 Arch_paths []string `android:"path,arch_variant"`
Liz Kammer2dd9ca42020-11-25 16:06:39 -080037}
38
39type customModule struct {
40 android.ModuleBase
Liz Kammerea6666f2021-02-17 10:17:28 -050041 android.BazelModuleBase
Liz Kammer2dd9ca42020-11-25 16:06:39 -080042
43 props customProps
44}
45
Rupert Shuttleworth06559d02021-05-19 09:14:26 -040046func errored(t *testing.T, desc string, errs []error) bool {
47 t.Helper()
48 if len(errs) > 0 {
49 for _, err := range errs {
50 t.Errorf("%s: %s", desc, err)
51 }
52 return true
53 }
54 return false
55}
56
Liz Kammer2dd9ca42020-11-25 16:06:39 -080057// OutputFiles is needed because some instances of this module use dist with a
58// tag property which requires the module implements OutputFileProducer.
59func (m *customModule) OutputFiles(tag string) (android.Paths, error) {
60 return android.PathsForTesting("path" + tag), nil
61}
62
63func (m *customModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
64 // nothing for now.
65}
66
67func customModuleFactoryBase() android.Module {
68 module := &customModule{}
69 module.AddProperties(&module.props)
Liz Kammerea6666f2021-02-17 10:17:28 -050070 android.InitBazelModule(module)
Liz Kammer2dd9ca42020-11-25 16:06:39 -080071 return module
72}
73
74func customModuleFactory() android.Module {
75 m := customModuleFactoryBase()
Liz Kammer4562a3b2021-04-21 18:15:34 -040076 android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibBoth)
Liz Kammer2dd9ca42020-11-25 16:06:39 -080077 return m
78}
79
80type testProps struct {
81 Test_prop struct {
82 Test_string_prop string
83 }
84}
85
86type customTestModule struct {
87 android.ModuleBase
88
89 props customProps
90 test_props testProps
91}
92
93func (m *customTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
94 // nothing for now.
95}
96
97func customTestModuleFactoryBase() android.Module {
98 m := &customTestModule{}
99 m.AddProperties(&m.props)
100 m.AddProperties(&m.test_props)
101 return m
102}
103
104func customTestModuleFactory() android.Module {
105 m := customTestModuleFactoryBase()
106 android.InitAndroidModule(m)
107 return m
108}
109
110type customDefaultsModule struct {
111 android.ModuleBase
112 android.DefaultsModuleBase
113}
114
115func customDefaultsModuleFactoryBase() android.DefaultsModule {
116 module := &customDefaultsModule{}
117 module.AddProperties(&customProps{})
118 return module
119}
120
121func customDefaultsModuleFactoryBasic() android.Module {
122 return customDefaultsModuleFactoryBase()
123}
124
125func customDefaultsModuleFactory() android.Module {
126 m := customDefaultsModuleFactoryBase()
127 android.InitDefaultsModule(m)
128 return m
129}
Jingwen Chen73850672020-12-14 08:25:34 -0500130
131type customBazelModuleAttributes struct {
Jingwen Chen73850672020-12-14 08:25:34 -0500132 String_prop string
133 String_list_prop []string
Liz Kammer4562a3b2021-04-21 18:15:34 -0400134 Arch_paths bazel.LabelListAttribute
Jingwen Chen73850672020-12-14 08:25:34 -0500135}
136
137type customBazelModule struct {
138 android.BazelTargetModuleBase
139 customBazelModuleAttributes
140}
141
142func customBazelModuleFactory() android.Module {
143 module := &customBazelModule{}
144 module.AddProperties(&module.customBazelModuleAttributes)
145 android.InitBazelTargetModule(module)
146 return module
147}
148
149func (m *customBazelModule) Name() string { return m.BaseModuleName() }
150func (m *customBazelModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {}
151
152func customBp2BuildMutator(ctx android.TopDownMutatorContext) {
153 if m, ok := ctx.Module().(*customModule); ok {
Jingwen Chen12b4c272021-03-10 02:05:59 -0500154 if !m.ConvertWithBp2build(ctx) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500155 return
156 }
157
Liz Kammer4562a3b2021-04-21 18:15:34 -0400158 paths := bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrc(ctx, m.props.Arch_paths))
159
Liz Kammer9abd62d2021-05-21 08:37:59 -0400160 for axis, configToProps := range m.GetArchVariantProperties(ctx, &customProps{}) {
161 for config, props := range configToProps {
162 if archProps, ok := props.(*customProps); ok && archProps.Arch_paths != nil {
163 paths.SetSelectValue(axis, config, android.BazelLabelForModuleSrc(ctx, archProps.Arch_paths))
164 }
Liz Kammer4562a3b2021-04-21 18:15:34 -0400165 }
166 }
167
Jingwen Chen1fd14692021-02-05 03:01:50 -0500168 attrs := &customBazelModuleAttributes{
Jingwen Chen73850672020-12-14 08:25:34 -0500169 String_prop: m.props.String_prop,
170 String_list_prop: m.props.String_list_prop,
Liz Kammer4562a3b2021-04-21 18:15:34 -0400171 Arch_paths: paths,
Jingwen Chen1fd14692021-02-05 03:01:50 -0500172 }
173
Liz Kammerfc46bc12021-02-19 11:06:17 -0500174 props := bazel.BazelTargetModuleProperties{
175 Rule_class: "custom",
176 }
Jingwen Chen1fd14692021-02-05 03:01:50 -0500177
Liz Kammerfc46bc12021-02-19 11:06:17 -0500178 ctx.CreateBazelTargetModule(customBazelModuleFactory, m.Name(), props, attrs)
Jingwen Chen73850672020-12-14 08:25:34 -0500179 }
180}
Jingwen Chen40067de2021-01-26 21:58:43 -0500181
182// A bp2build mutator that uses load statements and creates a 1:M mapping from
183// module to target.
184func customBp2BuildMutatorFromStarlark(ctx android.TopDownMutatorContext) {
185 if m, ok := ctx.Module().(*customModule); ok {
Jingwen Chen12b4c272021-03-10 02:05:59 -0500186 if !m.ConvertWithBp2build(ctx) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500187 return
188 }
189
Jingwen Chen1fd14692021-02-05 03:01:50 -0500190 baseName := m.Name()
191 attrs := &customBazelModuleAttributes{}
192
Liz Kammerfc46bc12021-02-19 11:06:17 -0500193 myLibraryProps := bazel.BazelTargetModuleProperties{
194 Rule_class: "my_library",
195 Bzl_load_location: "//build/bazel/rules:rules.bzl",
196 }
197 ctx.CreateBazelTargetModule(customBazelModuleFactory, baseName, myLibraryProps, attrs)
Jingwen Chen1fd14692021-02-05 03:01:50 -0500198
Liz Kammerfc46bc12021-02-19 11:06:17 -0500199 protoLibraryProps := bazel.BazelTargetModuleProperties{
200 Rule_class: "proto_library",
201 Bzl_load_location: "//build/bazel/rules:proto.bzl",
202 }
203 ctx.CreateBazelTargetModule(customBazelModuleFactory, baseName+"_proto_library_deps", protoLibraryProps, attrs)
Jingwen Chen1fd14692021-02-05 03:01:50 -0500204
Liz Kammerfc46bc12021-02-19 11:06:17 -0500205 myProtoLibraryProps := bazel.BazelTargetModuleProperties{
206 Rule_class: "my_proto_library",
207 Bzl_load_location: "//build/bazel/rules:proto.bzl",
208 }
209 ctx.CreateBazelTargetModule(customBazelModuleFactory, baseName+"_my_proto_library_deps", myProtoLibraryProps, attrs)
Jingwen Chen40067de2021-01-26 21:58:43 -0500210 }
211}
Jingwen Chenba369ad2021-02-22 10:19:34 -0500212
213// Helper method for tests to easily access the targets in a dir.
Liz Kammerba3ea162021-02-17 13:22:03 -0500214func generateBazelTargetsForDir(codegenCtx *CodegenContext, dir string) BazelTargets {
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400215 // TODO: Set generateFilegroups to true and/or remove the generateFilegroups argument completely
Jingwen Chenc63677b2021-06-17 05:43:19 +0000216 buildFileToTargets, _, _ := GenerateBazelTargets(codegenCtx, false)
Jingwen Chenba369ad2021-02-22 10:19:34 -0500217 return buildFileToTargets[dir]
218}