blob: c4661eacaf90413d92ae85798eaab2b84137c891 [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
31}
32
33type customModule struct {
34 android.ModuleBase
Liz Kammerea6666f2021-02-17 10:17:28 -050035 android.BazelModuleBase
Liz Kammer2dd9ca42020-11-25 16:06:39 -080036
37 props customProps
38}
39
40// OutputFiles is needed because some instances of this module use dist with a
41// tag property which requires the module implements OutputFileProducer.
42func (m *customModule) OutputFiles(tag string) (android.Paths, error) {
43 return android.PathsForTesting("path" + tag), nil
44}
45
46func (m *customModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
47 // nothing for now.
48}
49
50func customModuleFactoryBase() android.Module {
51 module := &customModule{}
52 module.AddProperties(&module.props)
Liz Kammerea6666f2021-02-17 10:17:28 -050053 android.InitBazelModule(module)
Liz Kammer2dd9ca42020-11-25 16:06:39 -080054 return module
55}
56
57func customModuleFactory() android.Module {
58 m := customModuleFactoryBase()
59 android.InitAndroidModule(m)
60 return m
61}
62
63type testProps struct {
64 Test_prop struct {
65 Test_string_prop string
66 }
67}
68
69type customTestModule struct {
70 android.ModuleBase
71
72 props customProps
73 test_props testProps
74}
75
76func (m *customTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
77 // nothing for now.
78}
79
80func customTestModuleFactoryBase() android.Module {
81 m := &customTestModule{}
82 m.AddProperties(&m.props)
83 m.AddProperties(&m.test_props)
84 return m
85}
86
87func customTestModuleFactory() android.Module {
88 m := customTestModuleFactoryBase()
89 android.InitAndroidModule(m)
90 return m
91}
92
93type customDefaultsModule struct {
94 android.ModuleBase
95 android.DefaultsModuleBase
96}
97
98func customDefaultsModuleFactoryBase() android.DefaultsModule {
99 module := &customDefaultsModule{}
100 module.AddProperties(&customProps{})
101 return module
102}
103
104func customDefaultsModuleFactoryBasic() android.Module {
105 return customDefaultsModuleFactoryBase()
106}
107
108func customDefaultsModuleFactory() android.Module {
109 m := customDefaultsModuleFactoryBase()
110 android.InitDefaultsModule(m)
111 return m
112}
Jingwen Chen73850672020-12-14 08:25:34 -0500113
114type customBazelModuleAttributes struct {
Jingwen Chen73850672020-12-14 08:25:34 -0500115 String_prop string
116 String_list_prop []string
117}
118
119type customBazelModule struct {
120 android.BazelTargetModuleBase
121 customBazelModuleAttributes
122}
123
124func customBazelModuleFactory() android.Module {
125 module := &customBazelModule{}
126 module.AddProperties(&module.customBazelModuleAttributes)
127 android.InitBazelTargetModule(module)
128 return module
129}
130
131func (m *customBazelModule) Name() string { return m.BaseModuleName() }
132func (m *customBazelModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {}
133
134func customBp2BuildMutator(ctx android.TopDownMutatorContext) {
135 if m, ok := ctx.Module().(*customModule); ok {
Jingwen Chen12b4c272021-03-10 02:05:59 -0500136 if !m.ConvertWithBp2build(ctx) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500137 return
138 }
139
Jingwen Chen1fd14692021-02-05 03:01:50 -0500140 attrs := &customBazelModuleAttributes{
Jingwen Chen73850672020-12-14 08:25:34 -0500141 String_prop: m.props.String_prop,
142 String_list_prop: m.props.String_list_prop,
Jingwen Chen1fd14692021-02-05 03:01:50 -0500143 }
144
Liz Kammerfc46bc12021-02-19 11:06:17 -0500145 props := bazel.BazelTargetModuleProperties{
146 Rule_class: "custom",
147 }
Jingwen Chen1fd14692021-02-05 03:01:50 -0500148
Liz Kammerfc46bc12021-02-19 11:06:17 -0500149 ctx.CreateBazelTargetModule(customBazelModuleFactory, m.Name(), props, attrs)
Jingwen Chen73850672020-12-14 08:25:34 -0500150 }
151}
Jingwen Chen40067de2021-01-26 21:58:43 -0500152
153// A bp2build mutator that uses load statements and creates a 1:M mapping from
154// module to target.
155func customBp2BuildMutatorFromStarlark(ctx android.TopDownMutatorContext) {
156 if m, ok := ctx.Module().(*customModule); ok {
Jingwen Chen12b4c272021-03-10 02:05:59 -0500157 if !m.ConvertWithBp2build(ctx) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500158 return
159 }
160
Jingwen Chen1fd14692021-02-05 03:01:50 -0500161 baseName := m.Name()
162 attrs := &customBazelModuleAttributes{}
163
Liz Kammerfc46bc12021-02-19 11:06:17 -0500164 myLibraryProps := bazel.BazelTargetModuleProperties{
165 Rule_class: "my_library",
166 Bzl_load_location: "//build/bazel/rules:rules.bzl",
167 }
168 ctx.CreateBazelTargetModule(customBazelModuleFactory, baseName, myLibraryProps, attrs)
Jingwen Chen1fd14692021-02-05 03:01:50 -0500169
Liz Kammerfc46bc12021-02-19 11:06:17 -0500170 protoLibraryProps := bazel.BazelTargetModuleProperties{
171 Rule_class: "proto_library",
172 Bzl_load_location: "//build/bazel/rules:proto.bzl",
173 }
174 ctx.CreateBazelTargetModule(customBazelModuleFactory, baseName+"_proto_library_deps", protoLibraryProps, attrs)
Jingwen Chen1fd14692021-02-05 03:01:50 -0500175
Liz Kammerfc46bc12021-02-19 11:06:17 -0500176 myProtoLibraryProps := bazel.BazelTargetModuleProperties{
177 Rule_class: "my_proto_library",
178 Bzl_load_location: "//build/bazel/rules:proto.bzl",
179 }
180 ctx.CreateBazelTargetModule(customBazelModuleFactory, baseName+"_my_proto_library_deps", myProtoLibraryProps, attrs)
Jingwen Chen40067de2021-01-26 21:58:43 -0500181 }
182}
Jingwen Chenba369ad2021-02-22 10:19:34 -0500183
184// Helper method for tests to easily access the targets in a dir.
Liz Kammerba3ea162021-02-17 13:22:03 -0500185func generateBazelTargetsForDir(codegenCtx *CodegenContext, dir string) BazelTargets {
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400186 // TODO: Set generateFilegroups to true and/or remove the generateFilegroups argument completely
187 buildFileToTargets, _ := GenerateBazelTargets(codegenCtx, false)
Jingwen Chenba369ad2021-02-22 10:19:34 -0500188 return buildFileToTargets[dir]
189}