blob: 45b994498fbaf738615d59ff9c5a246bc2bb27dd [file] [log] [blame]
atrostdb25ac02019-08-05 12:26:07 +01001// Copyright 2019 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
15package java
16
17import (
Paul Duffind796f6f2022-11-23 23:06:05 +000018 "fmt"
Paul Duffin001b2342021-03-11 18:43:31 +000019 "path/filepath"
20
atrostdb25ac02019-08-05 12:26:07 +010021 "android/soong/android"
Cole Fausta963b942024-04-11 17:43:00 -070022
Paul Duffin001b2342021-03-11 18:43:31 +000023 "github.com/google/blueprint"
Spandan Dase4c911e2024-01-19 00:22:22 +000024 "github.com/google/blueprint/proptools"
atrostdb25ac02019-08-05 12:26:07 +010025)
26
27func init() {
Paul Duffin4eb4b412021-03-09 02:59:25 +000028 registerPlatformCompatConfigBuildComponents(android.InitRegistrationContext)
Paul Duffin001b2342021-03-11 18:43:31 +000029
Paul Duffinfcf79852022-07-20 14:18:24 +000030 android.RegisterSdkMemberType(CompatConfigSdkMemberType)
31}
32
33var CompatConfigSdkMemberType = &compatConfigMemberType{
34 SdkMemberTypeBase: android.SdkMemberTypeBase{
35 PropertyName: "compat_configs",
36 SupportsSdk: true,
37 },
Mathew Inwoodabd49ab2019-12-19 14:27:08 +000038}
39
Paul Duffin4eb4b412021-03-09 02:59:25 +000040func registerPlatformCompatConfigBuildComponents(ctx android.RegistrationContext) {
LaMont Jones0c10e4d2023-05-16 00:58:37 +000041 ctx.RegisterParallelSingletonType("platform_compat_config_singleton", platformCompatConfigSingletonFactory)
Paul Duffin4eb4b412021-03-09 02:59:25 +000042 ctx.RegisterModuleType("platform_compat_config", PlatformCompatConfigFactory)
Paul Duffin1b29e002021-03-16 15:06:54 +000043 ctx.RegisterModuleType("prebuilt_platform_compat_config", prebuiltCompatConfigFactory)
Paul Duffin4eb4b412021-03-09 02:59:25 +000044 ctx.RegisterModuleType("global_compat_config", globalCompatConfigFactory)
45}
46
47var PrepareForTestWithPlatformCompatConfig = android.FixtureRegisterWithContext(registerPlatformCompatConfigBuildComponents)
48
Mathew Inwoodabd49ab2019-12-19 14:27:08 +000049func platformCompatConfigPath(ctx android.PathContext) android.OutputPath {
50 return android.PathForOutput(ctx, "compat_config", "merged_compat_config.xml")
atrostdb25ac02019-08-05 12:26:07 +010051}
52
53type platformCompatConfigProperties struct {
atrost87901b02019-08-29 12:48:43 +010054 Src *string `android:"path"`
atrostdb25ac02019-08-05 12:26:07 +010055}
56
57type platformCompatConfig struct {
58 android.ModuleBase
59
60 properties platformCompatConfigProperties
Colin Cross70dda7e2019-10-01 22:05:35 -070061 installDirPath android.InstallPath
atrostdb25ac02019-08-05 12:26:07 +010062 configFile android.OutputPath
Mathew Inwood0dd06f62019-12-17 11:14:42 +000063 metadataFile android.OutputPath
Wei Li60333152024-06-18 16:38:31 -070064
65 installConfigFile android.InstallPath
atrostdb25ac02019-08-05 12:26:07 +010066}
67
Paul Duffin29072a92021-03-16 10:12:49 +000068func (p *platformCompatConfig) compatConfigMetadata() android.Path {
Mathew Inwood4d0c19c2019-12-09 11:32:29 +000069 return p.metadataFile
70}
71
atrost6e126252020-01-27 17:01:16 +000072func (p *platformCompatConfig) CompatConfig() android.OutputPath {
73 return p.configFile
Mathew Inwood4d0c19c2019-12-09 11:32:29 +000074}
75
atrost6e126252020-01-27 17:01:16 +000076func (p *platformCompatConfig) SubDir() string {
77 return "compatconfig"
78}
79
Paul Duffin29072a92021-03-16 10:12:49 +000080type platformCompatConfigMetadataProvider interface {
81 compatConfigMetadata() android.Path
82}
83
atrost6e126252020-01-27 17:01:16 +000084type PlatformCompatConfigIntf interface {
85 android.Module
86
atrost6e126252020-01-27 17:01:16 +000087 CompatConfig() android.OutputPath
88 // Sub dir under etc dir.
89 SubDir() string
90}
91
92var _ PlatformCompatConfigIntf = (*platformCompatConfig)(nil)
Paul Duffin29072a92021-03-16 10:12:49 +000093var _ platformCompatConfigMetadataProvider = (*platformCompatConfig)(nil)
Mathew Inwood4d0c19c2019-12-09 11:32:29 +000094
Paul Duffin96154332021-03-15 18:18:22 +000095func (p *platformCompatConfig) GenerateAndroidBuildActions(ctx android.ModuleContext) {
96 rule := android.NewRuleBuilder(pctx, ctx)
97
98 configFileName := p.Name() + ".xml"
99 metadataFileName := p.Name() + "_meta.xml"
100 p.configFile = android.PathForModuleOut(ctx, configFileName).OutputPath
101 p.metadataFile = android.PathForModuleOut(ctx, metadataFileName).OutputPath
102 path := android.PathForModuleSrc(ctx, String(p.properties.Src))
103
104 rule.Command().
105 BuiltTool("process-compat-config").
106 FlagWithInput("--jar ", path).
107 FlagWithOutput("--device-config ", p.configFile).
108 FlagWithOutput("--merged-config ", p.metadataFile)
109
110 p.installDirPath = android.PathForModuleInstall(ctx, "etc", "compatconfig")
Wei Li60333152024-06-18 16:38:31 -0700111 p.installConfigFile = android.PathForModuleInstall(ctx, "etc", "compatconfig", p.configFile.Base())
Paul Duffin96154332021-03-15 18:18:22 +0000112 rule.Build(configFileName, "Extract compat/compat_config.xml and install it")
Wei Li60333152024-06-18 16:38:31 -0700113}
Paul Duffin96154332021-03-15 18:18:22 +0000114
Wei Li60333152024-06-18 16:38:31 -0700115func (p *platformCompatConfig) FilesToInstall() android.InstallPaths {
116 return android.InstallPaths{p.installConfigFile}
Paul Duffin96154332021-03-15 18:18:22 +0000117}
118
119func (p *platformCompatConfig) AndroidMkEntries() []android.AndroidMkEntries {
120 return []android.AndroidMkEntries{android.AndroidMkEntries{
121 Class: "ETC",
122 OutputFile: android.OptionalPathForPath(p.configFile),
123 Include: "$(BUILD_PREBUILT)",
124 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
125 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800126 entries.SetString("LOCAL_MODULE_PATH", p.installDirPath.String())
Paul Duffin96154332021-03-15 18:18:22 +0000127 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", p.configFile.Base())
128 },
129 },
130 }}
131}
132
133func PlatformCompatConfigFactory() android.Module {
134 module := &platformCompatConfig{}
135 module.AddProperties(&module.properties)
136 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
137 return module
138}
139
Paul Duffin001b2342021-03-11 18:43:31 +0000140type compatConfigMemberType struct {
141 android.SdkMemberTypeBase
142}
143
Paul Duffin296701e2021-07-14 10:29:36 +0100144func (b *compatConfigMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
145 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffin001b2342021-03-11 18:43:31 +0000146}
147
148func (b *compatConfigMemberType) IsInstance(module android.Module) bool {
149 _, ok := module.(*platformCompatConfig)
150 return ok
151}
152
153func (b *compatConfigMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
154 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "prebuilt_platform_compat_config")
155}
156
157func (b *compatConfigMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
158 return &compatConfigSdkMemberProperties{}
159}
160
161type compatConfigSdkMemberProperties struct {
162 android.SdkMemberPropertiesBase
163
164 Metadata android.Path
165}
166
167func (b *compatConfigSdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
168 module := variant.(*platformCompatConfig)
169 b.Metadata = module.metadataFile
170}
171
172func (b *compatConfigSdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
173 builder := ctx.SnapshotBuilder()
174 if b.Metadata != nil {
175 snapshotRelativePath := filepath.Join("compat_configs", ctx.Name(), b.Metadata.Base())
176 builder.CopyToSnapshot(b.Metadata, snapshotRelativePath)
177 propertySet.AddProperty("metadata", snapshotRelativePath)
178 }
179}
180
181var _ android.SdkMemberType = (*compatConfigMemberType)(nil)
182
Paul Duffin1b29e002021-03-16 15:06:54 +0000183// A prebuilt version of the platform compat config module.
184type prebuiltCompatConfigModule struct {
185 android.ModuleBase
Paul Duffin1b29e002021-03-16 15:06:54 +0000186 prebuilt android.Prebuilt
187
188 properties prebuiltCompatConfigProperties
189
190 metadataFile android.Path
191}
192
193type prebuiltCompatConfigProperties struct {
194 Metadata *string `android:"path"`
Spandan Dase4c911e2024-01-19 00:22:22 +0000195
196 // Name of the source soong module that gets shadowed by this prebuilt
197 // If unspecified, follows the naming convention that the source module of
198 // the prebuilt is Name() without "prebuilt_" prefix
199 Source_module_name *string
Paul Duffin1b29e002021-03-16 15:06:54 +0000200}
201
202func (module *prebuiltCompatConfigModule) Prebuilt() *android.Prebuilt {
203 return &module.prebuilt
204}
205
206func (module *prebuiltCompatConfigModule) Name() string {
207 return module.prebuilt.Name(module.ModuleBase.Name())
208}
209
210func (module *prebuiltCompatConfigModule) compatConfigMetadata() android.Path {
211 return module.metadataFile
212}
213
Spandan Dase4c911e2024-01-19 00:22:22 +0000214func (module *prebuiltCompatConfigModule) BaseModuleName() string {
215 return proptools.StringDefault(module.properties.Source_module_name, module.ModuleBase.Name())
216}
217
Paul Duffin1b29e002021-03-16 15:06:54 +0000218var _ platformCompatConfigMetadataProvider = (*prebuiltCompatConfigModule)(nil)
219
220func (module *prebuiltCompatConfigModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
221 module.metadataFile = module.prebuilt.SingleSourcePath(ctx)
222}
223
224// A prebuilt version of platform_compat_config that provides the metadata.
225func prebuiltCompatConfigFactory() android.Module {
226 m := &prebuiltCompatConfigModule{}
227 m.AddProperties(&m.properties)
228 android.InitSingleSourcePrebuiltModule(m, &m.properties, "Metadata")
Paul Duffin1b29e002021-03-16 15:06:54 +0000229 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
230 return m
231}
232
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000233// compat singleton rules
Paul Duffin96154332021-03-15 18:18:22 +0000234type platformCompatConfigSingleton struct {
235 metadata android.Path
236}
237
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000238func (p *platformCompatConfigSingleton) GenerateBuildActions(ctx android.SingletonContext) {
239
240 var compatConfigMetadata android.Paths
241
242 ctx.VisitAllModules(func(module android.Module) {
Cole Fausta963b942024-04-11 17:43:00 -0700243 if !module.Enabled(ctx) {
Paul Duffinbb9ff512021-03-24 12:08:53 +0000244 return
245 }
Paul Duffin29072a92021-03-16 10:12:49 +0000246 if c, ok := module.(platformCompatConfigMetadataProvider); ok {
Paul Duffin458a15b2022-11-25 12:18:24 +0000247 if !android.IsModulePreferred(module) {
Paul Duffinbb9ff512021-03-24 12:08:53 +0000248 return
249 }
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000250 metadata := c.compatConfigMetadata()
251 compatConfigMetadata = append(compatConfigMetadata, metadata)
252 }
253 })
254
255 if compatConfigMetadata == nil {
256 // nothing to do.
257 return
258 }
259
Colin Crossf1a035e2020-11-16 17:32:30 -0800260 rule := android.NewRuleBuilder(pctx, ctx)
Mathew Inwoodabd49ab2019-12-19 14:27:08 +0000261 outputPath := platformCompatConfigPath(ctx)
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000262
263 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800264 BuiltTool("process-compat-config").
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000265 FlagForEachInput("--xml ", compatConfigMetadata).
266 FlagWithOutput("--merged-config ", outputPath)
267
Colin Crossf1a035e2020-11-16 17:32:30 -0800268 rule.Build("merged-compat-config", "Merge compat config")
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000269
270 p.metadata = outputPath
271}
272
Mathew Inwood653c78a2019-12-19 12:38:10 +0000273func (p *platformCompatConfigSingleton) MakeVars(ctx android.MakeVarsContext) {
274 if p.metadata != nil {
Herbert Xue67a41bc2024-06-06 15:17:10 +0800275 ctx.DistForGoal("droidcore", p.metadata)
Mathew Inwood653c78a2019-12-19 12:38:10 +0000276 }
277}
278
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000279func platformCompatConfigSingletonFactory() android.Singleton {
280 return &platformCompatConfigSingleton{}
281}
282
Colin Crossd079e0b2022-08-16 10:27:33 -0700283// ============== merged_compat_config =================
Mathew Inwoodabd49ab2019-12-19 14:27:08 +0000284type globalCompatConfigProperties struct {
285 // name of the file into which the metadata will be copied.
286 Filename *string
287}
288
289type globalCompatConfig struct {
290 android.ModuleBase
291
292 properties globalCompatConfigProperties
293
294 outputFilePath android.OutputPath
295}
296
297func (c *globalCompatConfig) GenerateAndroidBuildActions(ctx android.ModuleContext) {
298 filename := String(c.properties.Filename)
299
300 inputPath := platformCompatConfigPath(ctx)
301 c.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath
302
303 // This ensures that outputFilePath has the correct name for others to
304 // use, as the source file may have a different name.
305 ctx.Build(pctx, android.BuildParams{
306 Rule: android.Cp,
307 Output: c.outputFilePath,
308 Input: inputPath,
309 })
310}
311
312func (h *globalCompatConfig) OutputFiles(tag string) (android.Paths, error) {
313 switch tag {
314 case "":
315 return android.Paths{h.outputFilePath}, nil
316 default:
317 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
318 }
319}
320
321// global_compat_config provides access to the merged compat config xml file generated by the build.
322func globalCompatConfigFactory() android.Module {
323 module := &globalCompatConfig{}
324 module.AddProperties(&module.properties)
325 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
326 return module
327}