blob: f442ddfd43fe6e058a57bcc60a3441b7ed5599d2 [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
29 android.RegisterSdkMemberType(&compatConfigMemberType{
30 SdkMemberTypeBase: android.SdkMemberTypeBase{
31 PropertyName: "compat_configs",
32 SupportsSdk: true,
33 },
34 })
Mathew Inwoodabd49ab2019-12-19 14:27:08 +000035}
36
Paul Duffin4eb4b412021-03-09 02:59:25 +000037func registerPlatformCompatConfigBuildComponents(ctx android.RegistrationContext) {
38 ctx.RegisterSingletonType("platform_compat_config_singleton", platformCompatConfigSingletonFactory)
39 ctx.RegisterModuleType("platform_compat_config", PlatformCompatConfigFactory)
Paul Duffin1b29e002021-03-16 15:06:54 +000040 ctx.RegisterModuleType("prebuilt_platform_compat_config", prebuiltCompatConfigFactory)
Paul Duffin4eb4b412021-03-09 02:59:25 +000041 ctx.RegisterModuleType("global_compat_config", globalCompatConfigFactory)
42}
43
44var PrepareForTestWithPlatformCompatConfig = android.FixtureRegisterWithContext(registerPlatformCompatConfigBuildComponents)
45
Mathew Inwoodabd49ab2019-12-19 14:27:08 +000046func platformCompatConfigPath(ctx android.PathContext) android.OutputPath {
47 return android.PathForOutput(ctx, "compat_config", "merged_compat_config.xml")
atrostdb25ac02019-08-05 12:26:07 +010048}
49
50type platformCompatConfigProperties struct {
atrost87901b02019-08-29 12:48:43 +010051 Src *string `android:"path"`
atrostdb25ac02019-08-05 12:26:07 +010052}
53
54type platformCompatConfig struct {
55 android.ModuleBase
Paul Duffin001b2342021-03-11 18:43:31 +000056 android.SdkBase
atrostdb25ac02019-08-05 12:26:07 +010057
58 properties platformCompatConfigProperties
Colin Cross70dda7e2019-10-01 22:05:35 -070059 installDirPath android.InstallPath
atrostdb25ac02019-08-05 12:26:07 +010060 configFile android.OutputPath
Mathew Inwood0dd06f62019-12-17 11:14:42 +000061 metadataFile android.OutputPath
atrostdb25ac02019-08-05 12:26:07 +010062}
63
Paul Duffin29072a92021-03-16 10:12:49 +000064func (p *platformCompatConfig) compatConfigMetadata() android.Path {
Mathew Inwood4d0c19c2019-12-09 11:32:29 +000065 return p.metadataFile
66}
67
atrost6e126252020-01-27 17:01:16 +000068func (p *platformCompatConfig) CompatConfig() android.OutputPath {
69 return p.configFile
Mathew Inwood4d0c19c2019-12-09 11:32:29 +000070}
71
atrost6e126252020-01-27 17:01:16 +000072func (p *platformCompatConfig) SubDir() string {
73 return "compatconfig"
74}
75
Paul Duffin29072a92021-03-16 10:12:49 +000076type platformCompatConfigMetadataProvider interface {
77 compatConfigMetadata() android.Path
78}
79
atrost6e126252020-01-27 17:01:16 +000080type PlatformCompatConfigIntf interface {
81 android.Module
82
atrost6e126252020-01-27 17:01:16 +000083 CompatConfig() android.OutputPath
84 // Sub dir under etc dir.
85 SubDir() string
86}
87
88var _ PlatformCompatConfigIntf = (*platformCompatConfig)(nil)
Paul Duffin29072a92021-03-16 10:12:49 +000089var _ platformCompatConfigMetadataProvider = (*platformCompatConfig)(nil)
Mathew Inwood4d0c19c2019-12-09 11:32:29 +000090
Paul Duffin96154332021-03-15 18:18:22 +000091func (p *platformCompatConfig) GenerateAndroidBuildActions(ctx android.ModuleContext) {
92 rule := android.NewRuleBuilder(pctx, ctx)
93
94 configFileName := p.Name() + ".xml"
95 metadataFileName := p.Name() + "_meta.xml"
96 p.configFile = android.PathForModuleOut(ctx, configFileName).OutputPath
97 p.metadataFile = android.PathForModuleOut(ctx, metadataFileName).OutputPath
98 path := android.PathForModuleSrc(ctx, String(p.properties.Src))
99
100 rule.Command().
101 BuiltTool("process-compat-config").
102 FlagWithInput("--jar ", path).
103 FlagWithOutput("--device-config ", p.configFile).
104 FlagWithOutput("--merged-config ", p.metadataFile)
105
106 p.installDirPath = android.PathForModuleInstall(ctx, "etc", "compatconfig")
107 rule.Build(configFileName, "Extract compat/compat_config.xml and install it")
108
109}
110
111func (p *platformCompatConfig) AndroidMkEntries() []android.AndroidMkEntries {
112 return []android.AndroidMkEntries{android.AndroidMkEntries{
113 Class: "ETC",
114 OutputFile: android.OptionalPathForPath(p.configFile),
115 Include: "$(BUILD_PREBUILT)",
116 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
117 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800118 entries.SetString("LOCAL_MODULE_PATH", p.installDirPath.String())
Paul Duffin96154332021-03-15 18:18:22 +0000119 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", p.configFile.Base())
120 },
121 },
122 }}
123}
124
125func PlatformCompatConfigFactory() android.Module {
126 module := &platformCompatConfig{}
127 module.AddProperties(&module.properties)
Paul Duffin001b2342021-03-11 18:43:31 +0000128 android.InitSdkAwareModule(module)
Paul Duffin96154332021-03-15 18:18:22 +0000129 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
130 return module
131}
132
Paul Duffin001b2342021-03-11 18:43:31 +0000133type compatConfigMemberType struct {
134 android.SdkMemberTypeBase
135}
136
Paul Duffin296701e2021-07-14 10:29:36 +0100137func (b *compatConfigMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
138 ctx.AddVariationDependencies(nil, dependencyTag, names...)
Paul Duffin001b2342021-03-11 18:43:31 +0000139}
140
141func (b *compatConfigMemberType) IsInstance(module android.Module) bool {
142 _, ok := module.(*platformCompatConfig)
143 return ok
144}
145
146func (b *compatConfigMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
147 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "prebuilt_platform_compat_config")
148}
149
150func (b *compatConfigMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
151 return &compatConfigSdkMemberProperties{}
152}
153
154type compatConfigSdkMemberProperties struct {
155 android.SdkMemberPropertiesBase
156
157 Metadata android.Path
158}
159
160func (b *compatConfigSdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
161 module := variant.(*platformCompatConfig)
162 b.Metadata = module.metadataFile
163}
164
165func (b *compatConfigSdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
166 builder := ctx.SnapshotBuilder()
167 if b.Metadata != nil {
168 snapshotRelativePath := filepath.Join("compat_configs", ctx.Name(), b.Metadata.Base())
169 builder.CopyToSnapshot(b.Metadata, snapshotRelativePath)
170 propertySet.AddProperty("metadata", snapshotRelativePath)
171 }
172}
173
174var _ android.SdkMemberType = (*compatConfigMemberType)(nil)
175
Paul Duffin1b29e002021-03-16 15:06:54 +0000176// A prebuilt version of the platform compat config module.
177type prebuiltCompatConfigModule struct {
178 android.ModuleBase
179 android.SdkBase
180 prebuilt android.Prebuilt
181
182 properties prebuiltCompatConfigProperties
183
184 metadataFile android.Path
185}
186
187type prebuiltCompatConfigProperties struct {
188 Metadata *string `android:"path"`
189}
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
203var _ platformCompatConfigMetadataProvider = (*prebuiltCompatConfigModule)(nil)
204
205func (module *prebuiltCompatConfigModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
206 module.metadataFile = module.prebuilt.SingleSourcePath(ctx)
207}
208
209// A prebuilt version of platform_compat_config that provides the metadata.
210func prebuiltCompatConfigFactory() android.Module {
211 m := &prebuiltCompatConfigModule{}
212 m.AddProperties(&m.properties)
213 android.InitSingleSourcePrebuiltModule(m, &m.properties, "Metadata")
214 android.InitSdkAwareModule(m)
215 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
216 return m
217}
218
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000219// compat singleton rules
Paul Duffin96154332021-03-15 18:18:22 +0000220type platformCompatConfigSingleton struct {
221 metadata android.Path
222}
223
Paul Duffinbb9ff512021-03-24 12:08:53 +0000224// isModulePreferredByCompatConfig checks to see whether the module is preferred for use by
225// platform compat config.
226func isModulePreferredByCompatConfig(module android.Module) bool {
227 // A versioned prebuilt_platform_compat_config, i.e. foo-platform-compat-config@current should be
228 // ignored.
Paul Duffin0c2e0832021-04-28 00:39:52 +0100229 if android.IsModuleInVersionedSdk(module) {
230 return false
Paul Duffinbb9ff512021-03-24 12:08:53 +0000231 }
232
Paul Duffine1d38372021-04-02 10:35:24 +0100233 return android.IsModulePreferred(module)
Paul Duffinbb9ff512021-03-24 12:08:53 +0000234}
235
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000236func (p *platformCompatConfigSingleton) GenerateBuildActions(ctx android.SingletonContext) {
237
238 var compatConfigMetadata android.Paths
239
240 ctx.VisitAllModules(func(module android.Module) {
Paul Duffinbb9ff512021-03-24 12:08:53 +0000241 if !module.Enabled() {
242 return
243 }
Paul Duffin29072a92021-03-16 10:12:49 +0000244 if c, ok := module.(platformCompatConfigMetadataProvider); ok {
Paul Duffinbb9ff512021-03-24 12:08:53 +0000245 if !isModulePreferredByCompatConfig(module) {
246 return
247 }
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000248 metadata := c.compatConfigMetadata()
249 compatConfigMetadata = append(compatConfigMetadata, metadata)
250 }
251 })
252
253 if compatConfigMetadata == nil {
254 // nothing to do.
255 return
256 }
257
Colin Crossf1a035e2020-11-16 17:32:30 -0800258 rule := android.NewRuleBuilder(pctx, ctx)
Mathew Inwoodabd49ab2019-12-19 14:27:08 +0000259 outputPath := platformCompatConfigPath(ctx)
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000260
261 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800262 BuiltTool("process-compat-config").
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000263 FlagForEachInput("--xml ", compatConfigMetadata).
264 FlagWithOutput("--merged-config ", outputPath)
265
Colin Crossf1a035e2020-11-16 17:32:30 -0800266 rule.Build("merged-compat-config", "Merge compat config")
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000267
268 p.metadata = outputPath
269}
270
Mathew Inwood653c78a2019-12-19 12:38:10 +0000271func (p *platformCompatConfigSingleton) MakeVars(ctx android.MakeVarsContext) {
272 if p.metadata != nil {
273 ctx.Strict("INTERNAL_PLATFORM_MERGED_COMPAT_CONFIG", p.metadata.String())
274 }
275}
276
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000277func platformCompatConfigSingletonFactory() android.Singleton {
278 return &platformCompatConfigSingleton{}
279}
280
Mathew Inwoodabd49ab2019-12-19 14:27:08 +0000281//============== merged_compat_config =================
282type globalCompatConfigProperties struct {
283 // name of the file into which the metadata will be copied.
284 Filename *string
285}
286
287type globalCompatConfig struct {
288 android.ModuleBase
289
290 properties globalCompatConfigProperties
291
292 outputFilePath android.OutputPath
293}
294
295func (c *globalCompatConfig) GenerateAndroidBuildActions(ctx android.ModuleContext) {
296 filename := String(c.properties.Filename)
297
298 inputPath := platformCompatConfigPath(ctx)
299 c.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath
300
301 // This ensures that outputFilePath has the correct name for others to
302 // use, as the source file may have a different name.
303 ctx.Build(pctx, android.BuildParams{
304 Rule: android.Cp,
305 Output: c.outputFilePath,
306 Input: inputPath,
307 })
308}
309
310func (h *globalCompatConfig) OutputFiles(tag string) (android.Paths, error) {
311 switch tag {
312 case "":
313 return android.Paths{h.outputFilePath}, nil
314 default:
315 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
316 }
317}
318
319// global_compat_config provides access to the merged compat config xml file generated by the build.
320func globalCompatConfigFactory() android.Module {
321 module := &globalCompatConfig{}
322 module.AddProperties(&module.properties)
323 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
324 return module
325}