blob: 5e6481b328f858a8d1dc3f5029d71a5b0f189cb8 [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"
6
7 "github.com/google/blueprint/proptools"
Liz Kammer2dd9ca42020-11-25 16:06:39 -08008)
9
10type nestedProps struct {
11 Nested_prop string
12}
13
14type customProps struct {
15 Bool_prop bool
16 Bool_ptr_prop *bool
17 // Ensure that properties tagged `blueprint:mutated` are omitted
18 Int_prop int `blueprint:"mutated"`
19 Int64_ptr_prop *int64
20 String_prop string
21 String_ptr_prop *string
22 String_list_prop []string
23
24 Nested_props nestedProps
25 Nested_props_ptr *nestedProps
26}
27
28type customModule struct {
29 android.ModuleBase
30
31 props customProps
32}
33
34// OutputFiles is needed because some instances of this module use dist with a
35// tag property which requires the module implements OutputFileProducer.
36func (m *customModule) OutputFiles(tag string) (android.Paths, error) {
37 return android.PathsForTesting("path" + tag), nil
38}
39
40func (m *customModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
41 // nothing for now.
42}
43
44func customModuleFactoryBase() android.Module {
45 module := &customModule{}
46 module.AddProperties(&module.props)
47 return module
48}
49
50func customModuleFactory() android.Module {
51 m := customModuleFactoryBase()
52 android.InitAndroidModule(m)
53 return m
54}
55
56type testProps struct {
57 Test_prop struct {
58 Test_string_prop string
59 }
60}
61
62type customTestModule struct {
63 android.ModuleBase
64
65 props customProps
66 test_props testProps
67}
68
69func (m *customTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
70 // nothing for now.
71}
72
73func customTestModuleFactoryBase() android.Module {
74 m := &customTestModule{}
75 m.AddProperties(&m.props)
76 m.AddProperties(&m.test_props)
77 return m
78}
79
80func customTestModuleFactory() android.Module {
81 m := customTestModuleFactoryBase()
82 android.InitAndroidModule(m)
83 return m
84}
85
86type customDefaultsModule struct {
87 android.ModuleBase
88 android.DefaultsModuleBase
89}
90
91func customDefaultsModuleFactoryBase() android.DefaultsModule {
92 module := &customDefaultsModule{}
93 module.AddProperties(&customProps{})
94 return module
95}
96
97func customDefaultsModuleFactoryBasic() android.Module {
98 return customDefaultsModuleFactoryBase()
99}
100
101func customDefaultsModuleFactory() android.Module {
102 m := customDefaultsModuleFactoryBase()
103 android.InitDefaultsModule(m)
104 return m
105}
Jingwen Chen73850672020-12-14 08:25:34 -0500106
107type customBazelModuleAttributes struct {
108 Name *string
109 String_prop string
110 String_list_prop []string
111}
112
113type customBazelModule struct {
114 android.BazelTargetModuleBase
115 customBazelModuleAttributes
116}
117
118func customBazelModuleFactory() android.Module {
119 module := &customBazelModule{}
120 module.AddProperties(&module.customBazelModuleAttributes)
121 android.InitBazelTargetModule(module)
122 return module
123}
124
125func (m *customBazelModule) Name() string { return m.BaseModuleName() }
126func (m *customBazelModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {}
127
128func customBp2BuildMutator(ctx android.TopDownMutatorContext) {
129 if m, ok := ctx.Module().(*customModule); ok {
130 name := "__bp2build__" + m.Name()
131 ctx.CreateModule(customBazelModuleFactory, &customBazelModuleAttributes{
132 Name: proptools.StringPtr(name),
133 String_prop: m.props.String_prop,
134 String_list_prop: m.props.String_list_prop,
135 }, &bazel.BazelTargetModuleProperties{
136 Rule_class: "custom",
137 })
138 }
139}
Jingwen Chen40067de2021-01-26 21:58:43 -0500140
141// A bp2build mutator that uses load statements and creates a 1:M mapping from
142// module to target.
143func customBp2BuildMutatorFromStarlark(ctx android.TopDownMutatorContext) {
144 if m, ok := ctx.Module().(*customModule); ok {
145 baseName := "__bp2build__" + m.Name()
146 ctx.CreateModule(customBazelModuleFactory, &customBazelModuleAttributes{
147 Name: proptools.StringPtr(baseName),
148 }, &bazel.BazelTargetModuleProperties{
149 Rule_class: "my_library",
150 Bzl_load_location: "//build/bazel/rules:rules.bzl",
151 })
152 ctx.CreateModule(customBazelModuleFactory, &customBazelModuleAttributes{
153 Name: proptools.StringPtr(baseName + "_proto_library_deps"),
154 }, &bazel.BazelTargetModuleProperties{
155 Rule_class: "proto_library",
156 Bzl_load_location: "//build/bazel/rules:proto.bzl",
157 })
158 ctx.CreateModule(customBazelModuleFactory, &customBazelModuleAttributes{
159 Name: proptools.StringPtr(baseName + "_my_proto_library_deps"),
160 }, &bazel.BazelTargetModuleProperties{
161 Rule_class: "my_proto_library",
162 Bzl_load_location: "//build/bazel/rules:proto.bzl",
163 })
164 }
165}