blob: 655021fc45abe9eadf384b75f7882e1e5cd0b840 [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"
Paul Duffin001b2342021-03-11 18:43:31 +000021 "github.com/google/blueprint"
22
Mathew Inwoodabd49ab2019-12-19 14:27:08 +000023 "fmt"
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) {
40 ctx.RegisterSingletonType("platform_compat_config_singleton", platformCompatConfigSingletonFactory)
41 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
Paul Duffin001b2342021-03-11 18:43:31 +000058 android.SdkBase
atrostdb25ac02019-08-05 12:26:07 +010059
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
atrostdb25ac02019-08-05 12:26:07 +010064}
65
Paul Duffin29072a92021-03-16 10:12:49 +000066func (p *platformCompatConfig) compatConfigMetadata() android.Path {
Mathew Inwood4d0c19c2019-12-09 11:32:29 +000067 return p.metadataFile
68}
69
atrost6e126252020-01-27 17:01:16 +000070func (p *platformCompatConfig) CompatConfig() android.OutputPath {
71 return p.configFile
Mathew Inwood4d0c19c2019-12-09 11:32:29 +000072}
73
atrost6e126252020-01-27 17:01:16 +000074func (p *platformCompatConfig) SubDir() string {
75 return "compatconfig"
76}
77
Paul Duffin29072a92021-03-16 10:12:49 +000078type platformCompatConfigMetadataProvider interface {
79 compatConfigMetadata() android.Path
80}
81
atrost6e126252020-01-27 17:01:16 +000082type PlatformCompatConfigIntf interface {
83 android.Module
84
atrost6e126252020-01-27 17:01:16 +000085 CompatConfig() android.OutputPath
86 // Sub dir under etc dir.
87 SubDir() string
88}
89
90var _ PlatformCompatConfigIntf = (*platformCompatConfig)(nil)
Paul Duffin29072a92021-03-16 10:12:49 +000091var _ platformCompatConfigMetadataProvider = (*platformCompatConfig)(nil)
Mathew Inwood4d0c19c2019-12-09 11:32:29 +000092
Paul Duffin96154332021-03-15 18:18:22 +000093func (p *platformCompatConfig) GenerateAndroidBuildActions(ctx android.ModuleContext) {
94 rule := android.NewRuleBuilder(pctx, ctx)
95
96 configFileName := p.Name() + ".xml"
97 metadataFileName := p.Name() + "_meta.xml"
98 p.configFile = android.PathForModuleOut(ctx, configFileName).OutputPath
99 p.metadataFile = android.PathForModuleOut(ctx, metadataFileName).OutputPath
100 path := android.PathForModuleSrc(ctx, String(p.properties.Src))
101
102 rule.Command().
103 BuiltTool("process-compat-config").
104 FlagWithInput("--jar ", path).
105 FlagWithOutput("--device-config ", p.configFile).
106 FlagWithOutput("--merged-config ", p.metadataFile)
107
108 p.installDirPath = android.PathForModuleInstall(ctx, "etc", "compatconfig")
109 rule.Build(configFileName, "Extract compat/compat_config.xml and install it")
110
111}
112
113func (p *platformCompatConfig) AndroidMkEntries() []android.AndroidMkEntries {
114 return []android.AndroidMkEntries{android.AndroidMkEntries{
115 Class: "ETC",
116 OutputFile: android.OptionalPathForPath(p.configFile),
117 Include: "$(BUILD_PREBUILT)",
118 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
119 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800120 entries.SetString("LOCAL_MODULE_PATH", p.installDirPath.String())
Paul Duffin96154332021-03-15 18:18:22 +0000121 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", p.configFile.Base())
122 },
123 },
124 }}
125}
126
127func PlatformCompatConfigFactory() android.Module {
128 module := &platformCompatConfig{}
129 module.AddProperties(&module.properties)
Paul Duffin001b2342021-03-11 18:43:31 +0000130 android.InitSdkAwareModule(module)
Paul Duffin96154332021-03-15 18:18:22 +0000131 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
132 return module
133}
134
Paul Duffin001b2342021-03-11 18:43:31 +0000135type compatConfigMemberType struct {
136 android.SdkMemberTypeBase
137}
138
Paul Duffin296701e2021-07-14 10:29:36 +0100139func (b *compatConfigMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
140 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffin001b2342021-03-11 18:43:31 +0000141}
142
143func (b *compatConfigMemberType) IsInstance(module android.Module) bool {
144 _, ok := module.(*platformCompatConfig)
145 return ok
146}
147
148func (b *compatConfigMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
149 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "prebuilt_platform_compat_config")
150}
151
152func (b *compatConfigMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
153 return &compatConfigSdkMemberProperties{}
154}
155
156type compatConfigSdkMemberProperties struct {
157 android.SdkMemberPropertiesBase
158
159 Metadata android.Path
160}
161
162func (b *compatConfigSdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
163 module := variant.(*platformCompatConfig)
164 b.Metadata = module.metadataFile
165}
166
167func (b *compatConfigSdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
168 builder := ctx.SnapshotBuilder()
169 if b.Metadata != nil {
170 snapshotRelativePath := filepath.Join("compat_configs", ctx.Name(), b.Metadata.Base())
171 builder.CopyToSnapshot(b.Metadata, snapshotRelativePath)
172 propertySet.AddProperty("metadata", snapshotRelativePath)
173 }
174}
175
176var _ android.SdkMemberType = (*compatConfigMemberType)(nil)
177
Paul Duffin1b29e002021-03-16 15:06:54 +0000178// A prebuilt version of the platform compat config module.
179type prebuiltCompatConfigModule struct {
180 android.ModuleBase
181 android.SdkBase
182 prebuilt android.Prebuilt
183
184 properties prebuiltCompatConfigProperties
185
186 metadataFile android.Path
187}
188
189type prebuiltCompatConfigProperties struct {
190 Metadata *string `android:"path"`
191}
192
193func (module *prebuiltCompatConfigModule) Prebuilt() *android.Prebuilt {
194 return &module.prebuilt
195}
196
197func (module *prebuiltCompatConfigModule) Name() string {
198 return module.prebuilt.Name(module.ModuleBase.Name())
199}
200
201func (module *prebuiltCompatConfigModule) compatConfigMetadata() android.Path {
202 return module.metadataFile
203}
204
205var _ platformCompatConfigMetadataProvider = (*prebuiltCompatConfigModule)(nil)
206
207func (module *prebuiltCompatConfigModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
208 module.metadataFile = module.prebuilt.SingleSourcePath(ctx)
209}
210
211// A prebuilt version of platform_compat_config that provides the metadata.
212func prebuiltCompatConfigFactory() android.Module {
213 m := &prebuiltCompatConfigModule{}
214 m.AddProperties(&m.properties)
215 android.InitSingleSourcePrebuiltModule(m, &m.properties, "Metadata")
216 android.InitSdkAwareModule(m)
217 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
218 return m
219}
220
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000221// compat singleton rules
Paul Duffin96154332021-03-15 18:18:22 +0000222type platformCompatConfigSingleton struct {
223 metadata android.Path
224}
225
Paul Duffinbb9ff512021-03-24 12:08:53 +0000226// isModulePreferredByCompatConfig checks to see whether the module is preferred for use by
227// platform compat config.
228func isModulePreferredByCompatConfig(module android.Module) bool {
229 // A versioned prebuilt_platform_compat_config, i.e. foo-platform-compat-config@current should be
230 // ignored.
Paul Duffin0c2e0832021-04-28 00:39:52 +0100231 if android.IsModuleInVersionedSdk(module) {
232 return false
Paul Duffinbb9ff512021-03-24 12:08:53 +0000233 }
234
Paul Duffine1d38372021-04-02 10:35:24 +0100235 return android.IsModulePreferred(module)
Paul Duffinbb9ff512021-03-24 12:08:53 +0000236}
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) {
Paul Duffinbb9ff512021-03-24 12:08:53 +0000243 if !module.Enabled() {
244 return
245 }
Paul Duffin29072a92021-03-16 10:12:49 +0000246 if c, ok := module.(platformCompatConfigMetadataProvider); ok {
Paul Duffinbb9ff512021-03-24 12:08:53 +0000247 if !isModulePreferredByCompatConfig(module) {
248 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 {
275 ctx.Strict("INTERNAL_PLATFORM_MERGED_COMPAT_CONFIG", p.metadata.String())
276 }
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}