blob: d65aa0b8fd8393759311c898948b4d4e5ebeadfa [file] [log] [blame]
Liz Kammer2dd9ca42020-11-25 16:06:39 -08001package bp2build
2
3import (
4 "android/soong/android"
Jingwen Chen73850672020-12-14 08:25:34 -05005 "android/soong/bazel"
Liz Kammer2dd9ca42020-11-25 16:06:39 -08006)
7
Jingwen Chen91220d72021-03-24 02:18:33 -04008var (
9 // A default configuration for tests to not have to specify bp2build_available on top level targets.
10 bp2buildConfig = android.Bp2BuildConfig{
11 android.BP2BUILD_TOPLEVEL: android.Bp2BuildDefaultTrueRecursively,
12 }
13)
14
Liz Kammer2dd9ca42020-11-25 16:06:39 -080015type nestedProps struct {
16 Nested_prop string
17}
18
19type customProps struct {
20 Bool_prop bool
21 Bool_ptr_prop *bool
22 // Ensure that properties tagged `blueprint:mutated` are omitted
23 Int_prop int `blueprint:"mutated"`
24 Int64_ptr_prop *int64
25 String_prop string
26 String_ptr_prop *string
27 String_list_prop []string
28
29 Nested_props nestedProps
30 Nested_props_ptr *nestedProps
Liz Kammer4562a3b2021-04-21 18:15:34 -040031
32 Arch_paths []string `android:"path,arch_variant"`
Liz Kammer2dd9ca42020-11-25 16:06:39 -080033}
34
35type customModule struct {
36 android.ModuleBase
Liz Kammerea6666f2021-02-17 10:17:28 -050037 android.BazelModuleBase
Liz Kammer2dd9ca42020-11-25 16:06:39 -080038
39 props customProps
40}
41
42// OutputFiles is needed because some instances of this module use dist with a
43// tag property which requires the module implements OutputFileProducer.
44func (m *customModule) OutputFiles(tag string) (android.Paths, error) {
45 return android.PathsForTesting("path" + tag), nil
46}
47
48func (m *customModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
49 // nothing for now.
50}
51
52func customModuleFactoryBase() android.Module {
53 module := &customModule{}
54 module.AddProperties(&module.props)
Liz Kammerea6666f2021-02-17 10:17:28 -050055 android.InitBazelModule(module)
Liz Kammer2dd9ca42020-11-25 16:06:39 -080056 return module
57}
58
59func customModuleFactory() android.Module {
60 m := customModuleFactoryBase()
Liz Kammer4562a3b2021-04-21 18:15:34 -040061 android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibBoth)
Liz Kammer2dd9ca42020-11-25 16:06:39 -080062 return m
63}
64
65type testProps struct {
66 Test_prop struct {
67 Test_string_prop string
68 }
69}
70
71type customTestModule struct {
72 android.ModuleBase
73
74 props customProps
75 test_props testProps
76}
77
78func (m *customTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
79 // nothing for now.
80}
81
82func customTestModuleFactoryBase() android.Module {
83 m := &customTestModule{}
84 m.AddProperties(&m.props)
85 m.AddProperties(&m.test_props)
86 return m
87}
88
89func customTestModuleFactory() android.Module {
90 m := customTestModuleFactoryBase()
91 android.InitAndroidModule(m)
92 return m
93}
94
95type customDefaultsModule struct {
96 android.ModuleBase
97 android.DefaultsModuleBase
98}
99
100func customDefaultsModuleFactoryBase() android.DefaultsModule {
101 module := &customDefaultsModule{}
102 module.AddProperties(&customProps{})
103 return module
104}
105
106func customDefaultsModuleFactoryBasic() android.Module {
107 return customDefaultsModuleFactoryBase()
108}
109
110func customDefaultsModuleFactory() android.Module {
111 m := customDefaultsModuleFactoryBase()
112 android.InitDefaultsModule(m)
113 return m
114}
Jingwen Chen73850672020-12-14 08:25:34 -0500115
116type customBazelModuleAttributes struct {
Jingwen Chen73850672020-12-14 08:25:34 -0500117 String_prop string
118 String_list_prop []string
Liz Kammer4562a3b2021-04-21 18:15:34 -0400119 Arch_paths bazel.LabelListAttribute
Jingwen Chen73850672020-12-14 08:25:34 -0500120}
121
122type customBazelModule struct {
123 android.BazelTargetModuleBase
124 customBazelModuleAttributes
125}
126
127func customBazelModuleFactory() android.Module {
128 module := &customBazelModule{}
129 module.AddProperties(&module.customBazelModuleAttributes)
130 android.InitBazelTargetModule(module)
131 return module
132}
133
134func (m *customBazelModule) Name() string { return m.BaseModuleName() }
135func (m *customBazelModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {}
136
137func customBp2BuildMutator(ctx android.TopDownMutatorContext) {
138 if m, ok := ctx.Module().(*customModule); ok {
Jingwen Chen12b4c272021-03-10 02:05:59 -0500139 if !m.ConvertWithBp2build(ctx) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500140 return
141 }
142
Liz Kammer4562a3b2021-04-21 18:15:34 -0400143 paths := bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrc(ctx, m.props.Arch_paths))
144
145 for arch, props := range m.GetArchProperties(&customProps{}) {
146 if archProps, ok := props.(*customProps); ok && archProps.Arch_paths != nil {
147 paths.SetValueForArch(arch.Name, android.BazelLabelForModuleSrc(ctx, archProps.Arch_paths))
148 }
149 }
150
Jingwen Chen1fd14692021-02-05 03:01:50 -0500151 attrs := &customBazelModuleAttributes{
Jingwen Chen73850672020-12-14 08:25:34 -0500152 String_prop: m.props.String_prop,
153 String_list_prop: m.props.String_list_prop,
Liz Kammer4562a3b2021-04-21 18:15:34 -0400154 Arch_paths: paths,
Jingwen Chen1fd14692021-02-05 03:01:50 -0500155 }
156
Liz Kammerfc46bc12021-02-19 11:06:17 -0500157 props := bazel.BazelTargetModuleProperties{
158 Rule_class: "custom",
159 }
Jingwen Chen1fd14692021-02-05 03:01:50 -0500160
Liz Kammerfc46bc12021-02-19 11:06:17 -0500161 ctx.CreateBazelTargetModule(customBazelModuleFactory, m.Name(), props, attrs)
Jingwen Chen73850672020-12-14 08:25:34 -0500162 }
163}
Jingwen Chen40067de2021-01-26 21:58:43 -0500164
165// A bp2build mutator that uses load statements and creates a 1:M mapping from
166// module to target.
167func customBp2BuildMutatorFromStarlark(ctx android.TopDownMutatorContext) {
168 if m, ok := ctx.Module().(*customModule); ok {
Jingwen Chen12b4c272021-03-10 02:05:59 -0500169 if !m.ConvertWithBp2build(ctx) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500170 return
171 }
172
Jingwen Chen1fd14692021-02-05 03:01:50 -0500173 baseName := m.Name()
174 attrs := &customBazelModuleAttributes{}
175
Liz Kammerfc46bc12021-02-19 11:06:17 -0500176 myLibraryProps := bazel.BazelTargetModuleProperties{
177 Rule_class: "my_library",
178 Bzl_load_location: "//build/bazel/rules:rules.bzl",
179 }
180 ctx.CreateBazelTargetModule(customBazelModuleFactory, baseName, myLibraryProps, attrs)
Jingwen Chen1fd14692021-02-05 03:01:50 -0500181
Liz Kammerfc46bc12021-02-19 11:06:17 -0500182 protoLibraryProps := bazel.BazelTargetModuleProperties{
183 Rule_class: "proto_library",
184 Bzl_load_location: "//build/bazel/rules:proto.bzl",
185 }
186 ctx.CreateBazelTargetModule(customBazelModuleFactory, baseName+"_proto_library_deps", protoLibraryProps, attrs)
Jingwen Chen1fd14692021-02-05 03:01:50 -0500187
Liz Kammerfc46bc12021-02-19 11:06:17 -0500188 myProtoLibraryProps := bazel.BazelTargetModuleProperties{
189 Rule_class: "my_proto_library",
190 Bzl_load_location: "//build/bazel/rules:proto.bzl",
191 }
192 ctx.CreateBazelTargetModule(customBazelModuleFactory, baseName+"_my_proto_library_deps", myProtoLibraryProps, attrs)
Jingwen Chen40067de2021-01-26 21:58:43 -0500193 }
194}
Jingwen Chenba369ad2021-02-22 10:19:34 -0500195
196// Helper method for tests to easily access the targets in a dir.
Liz Kammerba3ea162021-02-17 13:22:03 -0500197func generateBazelTargetsForDir(codegenCtx *CodegenContext, dir string) BazelTargets {
Jingwen Chenba369ad2021-02-22 10:19:34 -0500198 buildFileToTargets, _ := GenerateBazelTargets(codegenCtx)
199 return buildFileToTargets[dir]
200}