blob: 67ed84e1ddcc1fdaf0e19e95c394876277423bcd [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 Duffin001b2342021-03-11 18:43:31 +000018 "path/filepath"
19
atrostdb25ac02019-08-05 12:26:07 +010020 "android/soong/android"
Cole Fausta963b942024-04-11 17:43:00 -070021
Paul Duffin001b2342021-03-11 18:43:31 +000022 "github.com/google/blueprint"
Spandan Dase4c911e2024-01-19 00:22:22 +000023 "github.com/google/blueprint/proptools"
atrostdb25ac02019-08-05 12:26:07 +010024)
25
26func init() {
Paul Duffin4eb4b412021-03-09 02:59:25 +000027 registerPlatformCompatConfigBuildComponents(android.InitRegistrationContext)
Paul Duffin001b2342021-03-11 18:43:31 +000028
Paul Duffinfcf79852022-07-20 14:18:24 +000029 android.RegisterSdkMemberType(CompatConfigSdkMemberType)
30}
31
32var CompatConfigSdkMemberType = &compatConfigMemberType{
33 SdkMemberTypeBase: android.SdkMemberTypeBase{
34 PropertyName: "compat_configs",
35 SupportsSdk: true,
36 },
Mathew Inwoodabd49ab2019-12-19 14:27:08 +000037}
38
Paul Duffin4eb4b412021-03-09 02:59:25 +000039func registerPlatformCompatConfigBuildComponents(ctx android.RegistrationContext) {
LaMont Jones0c10e4d2023-05-16 00:58:37 +000040 ctx.RegisterParallelSingletonType("platform_compat_config_singleton", platformCompatConfigSingletonFactory)
Paul Duffin4eb4b412021-03-09 02:59:25 +000041 ctx.RegisterModuleType("platform_compat_config", PlatformCompatConfigFactory)
Paul Duffin1b29e002021-03-16 15:06:54 +000042 ctx.RegisterModuleType("prebuilt_platform_compat_config", prebuiltCompatConfigFactory)
Paul Duffin4eb4b412021-03-09 02:59:25 +000043 ctx.RegisterModuleType("global_compat_config", globalCompatConfigFactory)
44}
45
46var PrepareForTestWithPlatformCompatConfig = android.FixtureRegisterWithContext(registerPlatformCompatConfigBuildComponents)
47
Mathew Inwoodabd49ab2019-12-19 14:27:08 +000048func platformCompatConfigPath(ctx android.PathContext) android.OutputPath {
49 return android.PathForOutput(ctx, "compat_config", "merged_compat_config.xml")
atrostdb25ac02019-08-05 12:26:07 +010050}
51
52type platformCompatConfigProperties struct {
atrost87901b02019-08-29 12:48:43 +010053 Src *string `android:"path"`
atrostdb25ac02019-08-05 12:26:07 +010054}
55
56type platformCompatConfig struct {
57 android.ModuleBase
58
59 properties platformCompatConfigProperties
Colin Cross70dda7e2019-10-01 22:05:35 -070060 installDirPath android.InstallPath
atrostdb25ac02019-08-05 12:26:07 +010061 configFile android.OutputPath
Mathew Inwood0dd06f62019-12-17 11:14:42 +000062 metadataFile android.OutputPath
Wei Li60333152024-06-18 16:38:31 -070063
64 installConfigFile android.InstallPath
atrostdb25ac02019-08-05 12:26:07 +010065}
66
Paul Duffin29072a92021-03-16 10:12:49 +000067func (p *platformCompatConfig) compatConfigMetadata() android.Path {
Mathew Inwood4d0c19c2019-12-09 11:32:29 +000068 return p.metadataFile
69}
70
atrost6e126252020-01-27 17:01:16 +000071func (p *platformCompatConfig) CompatConfig() android.OutputPath {
72 return p.configFile
Mathew Inwood4d0c19c2019-12-09 11:32:29 +000073}
74
atrost6e126252020-01-27 17:01:16 +000075func (p *platformCompatConfig) SubDir() string {
76 return "compatconfig"
77}
78
Paul Duffin29072a92021-03-16 10:12:49 +000079type platformCompatConfigMetadataProvider interface {
80 compatConfigMetadata() android.Path
81}
82
atrost6e126252020-01-27 17:01:16 +000083type PlatformCompatConfigIntf interface {
84 android.Module
85
atrost6e126252020-01-27 17:01:16 +000086 CompatConfig() android.OutputPath
87 // Sub dir under etc dir.
88 SubDir() string
89}
90
91var _ PlatformCompatConfigIntf = (*platformCompatConfig)(nil)
Paul Duffin29072a92021-03-16 10:12:49 +000092var _ platformCompatConfigMetadataProvider = (*platformCompatConfig)(nil)
Mathew Inwood4d0c19c2019-12-09 11:32:29 +000093
Paul Duffin96154332021-03-15 18:18:22 +000094func (p *platformCompatConfig) GenerateAndroidBuildActions(ctx android.ModuleContext) {
95 rule := android.NewRuleBuilder(pctx, ctx)
96
97 configFileName := p.Name() + ".xml"
98 metadataFileName := p.Name() + "_meta.xml"
99 p.configFile = android.PathForModuleOut(ctx, configFileName).OutputPath
100 p.metadataFile = android.PathForModuleOut(ctx, metadataFileName).OutputPath
101 path := android.PathForModuleSrc(ctx, String(p.properties.Src))
102
103 rule.Command().
104 BuiltTool("process-compat-config").
105 FlagWithInput("--jar ", path).
106 FlagWithOutput("--device-config ", p.configFile).
107 FlagWithOutput("--merged-config ", p.metadataFile)
108
109 p.installDirPath = android.PathForModuleInstall(ctx, "etc", "compatconfig")
Wei Li60333152024-06-18 16:38:31 -0700110 p.installConfigFile = android.PathForModuleInstall(ctx, "etc", "compatconfig", p.configFile.Base())
Paul Duffin96154332021-03-15 18:18:22 +0000111 rule.Build(configFileName, "Extract compat/compat_config.xml and install it")
Justin Yun8a7978c2024-06-24 16:28:58 +0900112 ctx.InstallFile(p.installDirPath, p.configFile.Base(), p.configFile)
Paul Duffin96154332021-03-15 18:18:22 +0000113}
114
115func (p *platformCompatConfig) AndroidMkEntries() []android.AndroidMkEntries {
116 return []android.AndroidMkEntries{android.AndroidMkEntries{
117 Class: "ETC",
118 OutputFile: android.OptionalPathForPath(p.configFile),
Paul Duffin96154332021-03-15 18:18:22 +0000119 }}
120}
121
122func PlatformCompatConfigFactory() android.Module {
123 module := &platformCompatConfig{}
124 module.AddProperties(&module.properties)
125 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
126 return module
127}
128
Paul Duffin001b2342021-03-11 18:43:31 +0000129type compatConfigMemberType struct {
130 android.SdkMemberTypeBase
131}
132
Paul Duffin296701e2021-07-14 10:29:36 +0100133func (b *compatConfigMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
134 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffin001b2342021-03-11 18:43:31 +0000135}
136
137func (b *compatConfigMemberType) IsInstance(module android.Module) bool {
138 _, ok := module.(*platformCompatConfig)
139 return ok
140}
141
142func (b *compatConfigMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
143 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "prebuilt_platform_compat_config")
144}
145
146func (b *compatConfigMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
147 return &compatConfigSdkMemberProperties{}
148}
149
150type compatConfigSdkMemberProperties struct {
151 android.SdkMemberPropertiesBase
152
153 Metadata android.Path
154}
155
156func (b *compatConfigSdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
157 module := variant.(*platformCompatConfig)
158 b.Metadata = module.metadataFile
159}
160
161func (b *compatConfigSdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
162 builder := ctx.SnapshotBuilder()
163 if b.Metadata != nil {
164 snapshotRelativePath := filepath.Join("compat_configs", ctx.Name(), b.Metadata.Base())
165 builder.CopyToSnapshot(b.Metadata, snapshotRelativePath)
166 propertySet.AddProperty("metadata", snapshotRelativePath)
167 }
168}
169
170var _ android.SdkMemberType = (*compatConfigMemberType)(nil)
171
Paul Duffin1b29e002021-03-16 15:06:54 +0000172// A prebuilt version of the platform compat config module.
173type prebuiltCompatConfigModule struct {
174 android.ModuleBase
Paul Duffin1b29e002021-03-16 15:06:54 +0000175 prebuilt android.Prebuilt
176
177 properties prebuiltCompatConfigProperties
178
179 metadataFile android.Path
180}
181
182type prebuiltCompatConfigProperties struct {
183 Metadata *string `android:"path"`
Spandan Dase4c911e2024-01-19 00:22:22 +0000184
185 // Name of the source soong module that gets shadowed by this prebuilt
186 // If unspecified, follows the naming convention that the source module of
187 // the prebuilt is Name() without "prebuilt_" prefix
188 Source_module_name *string
Paul Duffin1b29e002021-03-16 15:06:54 +0000189}
190
191func (module *prebuiltCompatConfigModule) Prebuilt() *android.Prebuilt {
192 return &module.prebuilt
193}
194
195func (module *prebuiltCompatConfigModule) Name() string {
196 return module.prebuilt.Name(module.ModuleBase.Name())
197}
198
199func (module *prebuiltCompatConfigModule) compatConfigMetadata() android.Path {
200 return module.metadataFile
201}
202
Spandan Dase4c911e2024-01-19 00:22:22 +0000203func (module *prebuiltCompatConfigModule) BaseModuleName() string {
204 return proptools.StringDefault(module.properties.Source_module_name, module.ModuleBase.Name())
205}
206
Paul Duffin1b29e002021-03-16 15:06:54 +0000207var _ platformCompatConfigMetadataProvider = (*prebuiltCompatConfigModule)(nil)
208
209func (module *prebuiltCompatConfigModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
210 module.metadataFile = module.prebuilt.SingleSourcePath(ctx)
211}
212
213// A prebuilt version of platform_compat_config that provides the metadata.
214func prebuiltCompatConfigFactory() android.Module {
215 m := &prebuiltCompatConfigModule{}
216 m.AddProperties(&m.properties)
217 android.InitSingleSourcePrebuiltModule(m, &m.properties, "Metadata")
Paul Duffin1b29e002021-03-16 15:06:54 +0000218 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
219 return m
220}
221
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000222// compat singleton rules
Paul Duffin96154332021-03-15 18:18:22 +0000223type platformCompatConfigSingleton struct {
224 metadata android.Path
225}
226
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000227func (p *platformCompatConfigSingleton) GenerateBuildActions(ctx android.SingletonContext) {
228
229 var compatConfigMetadata android.Paths
230
231 ctx.VisitAllModules(func(module android.Module) {
Cole Fausta963b942024-04-11 17:43:00 -0700232 if !module.Enabled(ctx) {
Paul Duffinbb9ff512021-03-24 12:08:53 +0000233 return
234 }
Paul Duffin29072a92021-03-16 10:12:49 +0000235 if c, ok := module.(platformCompatConfigMetadataProvider); ok {
Paul Duffin458a15b2022-11-25 12:18:24 +0000236 if !android.IsModulePreferred(module) {
Paul Duffinbb9ff512021-03-24 12:08:53 +0000237 return
238 }
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000239 metadata := c.compatConfigMetadata()
240 compatConfigMetadata = append(compatConfigMetadata, metadata)
241 }
242 })
243
244 if compatConfigMetadata == nil {
245 // nothing to do.
246 return
247 }
248
Colin Crossf1a035e2020-11-16 17:32:30 -0800249 rule := android.NewRuleBuilder(pctx, ctx)
Mathew Inwoodabd49ab2019-12-19 14:27:08 +0000250 outputPath := platformCompatConfigPath(ctx)
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000251
252 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800253 BuiltTool("process-compat-config").
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000254 FlagForEachInput("--xml ", compatConfigMetadata).
255 FlagWithOutput("--merged-config ", outputPath)
256
Colin Crossf1a035e2020-11-16 17:32:30 -0800257 rule.Build("merged-compat-config", "Merge compat config")
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000258
259 p.metadata = outputPath
260}
261
Mathew Inwood653c78a2019-12-19 12:38:10 +0000262func (p *platformCompatConfigSingleton) MakeVars(ctx android.MakeVarsContext) {
263 if p.metadata != nil {
Herbert Xue67a41bc2024-06-06 15:17:10 +0800264 ctx.DistForGoal("droidcore", p.metadata)
Mathew Inwood653c78a2019-12-19 12:38:10 +0000265 }
266}
267
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000268func platformCompatConfigSingletonFactory() android.Singleton {
269 return &platformCompatConfigSingleton{}
270}
271
Colin Crossd079e0b2022-08-16 10:27:33 -0700272// ============== merged_compat_config =================
Mathew Inwoodabd49ab2019-12-19 14:27:08 +0000273type globalCompatConfigProperties struct {
274 // name of the file into which the metadata will be copied.
275 Filename *string
276}
277
278type globalCompatConfig struct {
279 android.ModuleBase
280
281 properties globalCompatConfigProperties
Mathew Inwoodabd49ab2019-12-19 14:27:08 +0000282}
283
284func (c *globalCompatConfig) GenerateAndroidBuildActions(ctx android.ModuleContext) {
285 filename := String(c.properties.Filename)
286
287 inputPath := platformCompatConfigPath(ctx)
mrziwang9e7127f2024-06-18 16:10:47 -0700288 outputFilePath := android.PathForModuleOut(ctx, filename).OutputPath
Mathew Inwoodabd49ab2019-12-19 14:27:08 +0000289
290 // This ensures that outputFilePath has the correct name for others to
291 // use, as the source file may have a different name.
292 ctx.Build(pctx, android.BuildParams{
293 Rule: android.Cp,
mrziwang9e7127f2024-06-18 16:10:47 -0700294 Output: outputFilePath,
Mathew Inwoodabd49ab2019-12-19 14:27:08 +0000295 Input: inputPath,
296 })
Mathew Inwoodabd49ab2019-12-19 14:27:08 +0000297
mrziwang9e7127f2024-06-18 16:10:47 -0700298 ctx.SetOutputFiles(android.Paths{outputFilePath}, "")
Mathew Inwoodabd49ab2019-12-19 14:27:08 +0000299}
300
301// global_compat_config provides access to the merged compat config xml file generated by the build.
302func globalCompatConfigFactory() android.Module {
303 module := &globalCompatConfig{}
304 module.AddProperties(&module.properties)
305 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
306 return module
307}