blob: 3c43a8e55479971715afc50367397dc836ebf62b [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 (
18 "android/soong/android"
Mathew Inwoodabd49ab2019-12-19 14:27:08 +000019 "fmt"
atrostdb25ac02019-08-05 12:26:07 +010020)
21
22func init() {
Paul Duffin4eb4b412021-03-09 02:59:25 +000023 registerPlatformCompatConfigBuildComponents(android.InitRegistrationContext)
Mathew Inwoodabd49ab2019-12-19 14:27:08 +000024}
25
Paul Duffin4eb4b412021-03-09 02:59:25 +000026func registerPlatformCompatConfigBuildComponents(ctx android.RegistrationContext) {
27 ctx.RegisterSingletonType("platform_compat_config_singleton", platformCompatConfigSingletonFactory)
28 ctx.RegisterModuleType("platform_compat_config", PlatformCompatConfigFactory)
Paul Duffin1b29e002021-03-16 15:06:54 +000029 ctx.RegisterModuleType("prebuilt_platform_compat_config", prebuiltCompatConfigFactory)
Paul Duffin4eb4b412021-03-09 02:59:25 +000030 ctx.RegisterModuleType("global_compat_config", globalCompatConfigFactory)
31}
32
33var PrepareForTestWithPlatformCompatConfig = android.FixtureRegisterWithContext(registerPlatformCompatConfigBuildComponents)
34
Mathew Inwoodabd49ab2019-12-19 14:27:08 +000035func platformCompatConfigPath(ctx android.PathContext) android.OutputPath {
36 return android.PathForOutput(ctx, "compat_config", "merged_compat_config.xml")
atrostdb25ac02019-08-05 12:26:07 +010037}
38
39type platformCompatConfigProperties struct {
atrost87901b02019-08-29 12:48:43 +010040 Src *string `android:"path"`
atrostdb25ac02019-08-05 12:26:07 +010041}
42
43type platformCompatConfig struct {
44 android.ModuleBase
45
46 properties platformCompatConfigProperties
Colin Cross70dda7e2019-10-01 22:05:35 -070047 installDirPath android.InstallPath
atrostdb25ac02019-08-05 12:26:07 +010048 configFile android.OutputPath
Mathew Inwood0dd06f62019-12-17 11:14:42 +000049 metadataFile android.OutputPath
atrostdb25ac02019-08-05 12:26:07 +010050}
51
Paul Duffin29072a92021-03-16 10:12:49 +000052func (p *platformCompatConfig) compatConfigMetadata() android.Path {
Mathew Inwood4d0c19c2019-12-09 11:32:29 +000053 return p.metadataFile
54}
55
atrost6e126252020-01-27 17:01:16 +000056func (p *platformCompatConfig) CompatConfig() android.OutputPath {
57 return p.configFile
Mathew Inwood4d0c19c2019-12-09 11:32:29 +000058}
59
atrost6e126252020-01-27 17:01:16 +000060func (p *platformCompatConfig) SubDir() string {
61 return "compatconfig"
62}
63
Paul Duffin29072a92021-03-16 10:12:49 +000064type platformCompatConfigMetadataProvider interface {
65 compatConfigMetadata() android.Path
66}
67
atrost6e126252020-01-27 17:01:16 +000068type PlatformCompatConfigIntf interface {
69 android.Module
70
atrost6e126252020-01-27 17:01:16 +000071 CompatConfig() android.OutputPath
72 // Sub dir under etc dir.
73 SubDir() string
74}
75
76var _ PlatformCompatConfigIntf = (*platformCompatConfig)(nil)
Paul Duffin29072a92021-03-16 10:12:49 +000077var _ platformCompatConfigMetadataProvider = (*platformCompatConfig)(nil)
Mathew Inwood4d0c19c2019-12-09 11:32:29 +000078
Paul Duffin96154332021-03-15 18:18:22 +000079func (p *platformCompatConfig) GenerateAndroidBuildActions(ctx android.ModuleContext) {
80 rule := android.NewRuleBuilder(pctx, ctx)
81
82 configFileName := p.Name() + ".xml"
83 metadataFileName := p.Name() + "_meta.xml"
84 p.configFile = android.PathForModuleOut(ctx, configFileName).OutputPath
85 p.metadataFile = android.PathForModuleOut(ctx, metadataFileName).OutputPath
86 path := android.PathForModuleSrc(ctx, String(p.properties.Src))
87
88 rule.Command().
89 BuiltTool("process-compat-config").
90 FlagWithInput("--jar ", path).
91 FlagWithOutput("--device-config ", p.configFile).
92 FlagWithOutput("--merged-config ", p.metadataFile)
93
94 p.installDirPath = android.PathForModuleInstall(ctx, "etc", "compatconfig")
95 rule.Build(configFileName, "Extract compat/compat_config.xml and install it")
96
97}
98
99func (p *platformCompatConfig) AndroidMkEntries() []android.AndroidMkEntries {
100 return []android.AndroidMkEntries{android.AndroidMkEntries{
101 Class: "ETC",
102 OutputFile: android.OptionalPathForPath(p.configFile),
103 Include: "$(BUILD_PREBUILT)",
104 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
105 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
106 entries.SetString("LOCAL_MODULE_PATH", p.installDirPath.ToMakePath().String())
107 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", p.configFile.Base())
108 },
109 },
110 }}
111}
112
113func PlatformCompatConfigFactory() android.Module {
114 module := &platformCompatConfig{}
115 module.AddProperties(&module.properties)
116 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
117 return module
118}
119
Paul Duffin1b29e002021-03-16 15:06:54 +0000120// A prebuilt version of the platform compat config module.
121type prebuiltCompatConfigModule struct {
122 android.ModuleBase
123 android.SdkBase
124 prebuilt android.Prebuilt
125
126 properties prebuiltCompatConfigProperties
127
128 metadataFile android.Path
129}
130
131type prebuiltCompatConfigProperties struct {
132 Metadata *string `android:"path"`
133}
134
135func (module *prebuiltCompatConfigModule) Prebuilt() *android.Prebuilt {
136 return &module.prebuilt
137}
138
139func (module *prebuiltCompatConfigModule) Name() string {
140 return module.prebuilt.Name(module.ModuleBase.Name())
141}
142
143func (module *prebuiltCompatConfigModule) compatConfigMetadata() android.Path {
144 return module.metadataFile
145}
146
147var _ platformCompatConfigMetadataProvider = (*prebuiltCompatConfigModule)(nil)
148
149func (module *prebuiltCompatConfigModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
150 module.metadataFile = module.prebuilt.SingleSourcePath(ctx)
151}
152
153// A prebuilt version of platform_compat_config that provides the metadata.
154func prebuiltCompatConfigFactory() android.Module {
155 m := &prebuiltCompatConfigModule{}
156 m.AddProperties(&m.properties)
157 android.InitSingleSourcePrebuiltModule(m, &m.properties, "Metadata")
158 android.InitSdkAwareModule(m)
159 android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
160 return m
161}
162
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000163// compat singleton rules
Paul Duffin96154332021-03-15 18:18:22 +0000164type platformCompatConfigSingleton struct {
165 metadata android.Path
166}
167
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000168func (p *platformCompatConfigSingleton) GenerateBuildActions(ctx android.SingletonContext) {
169
170 var compatConfigMetadata android.Paths
171
172 ctx.VisitAllModules(func(module android.Module) {
Paul Duffin29072a92021-03-16 10:12:49 +0000173 if c, ok := module.(platformCompatConfigMetadataProvider); ok {
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000174 metadata := c.compatConfigMetadata()
175 compatConfigMetadata = append(compatConfigMetadata, metadata)
176 }
177 })
178
179 if compatConfigMetadata == nil {
180 // nothing to do.
181 return
182 }
183
Colin Crossf1a035e2020-11-16 17:32:30 -0800184 rule := android.NewRuleBuilder(pctx, ctx)
Mathew Inwoodabd49ab2019-12-19 14:27:08 +0000185 outputPath := platformCompatConfigPath(ctx)
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000186
187 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800188 BuiltTool("process-compat-config").
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000189 FlagForEachInput("--xml ", compatConfigMetadata).
190 FlagWithOutput("--merged-config ", outputPath)
191
Colin Crossf1a035e2020-11-16 17:32:30 -0800192 rule.Build("merged-compat-config", "Merge compat config")
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000193
194 p.metadata = outputPath
195}
196
Mathew Inwood653c78a2019-12-19 12:38:10 +0000197func (p *platformCompatConfigSingleton) MakeVars(ctx android.MakeVarsContext) {
198 if p.metadata != nil {
199 ctx.Strict("INTERNAL_PLATFORM_MERGED_COMPAT_CONFIG", p.metadata.String())
200 }
201}
202
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000203func platformCompatConfigSingletonFactory() android.Singleton {
204 return &platformCompatConfigSingleton{}
205}
206
Mathew Inwoodabd49ab2019-12-19 14:27:08 +0000207//============== merged_compat_config =================
208type globalCompatConfigProperties struct {
209 // name of the file into which the metadata will be copied.
210 Filename *string
211}
212
213type globalCompatConfig struct {
214 android.ModuleBase
215
216 properties globalCompatConfigProperties
217
218 outputFilePath android.OutputPath
219}
220
221func (c *globalCompatConfig) GenerateAndroidBuildActions(ctx android.ModuleContext) {
222 filename := String(c.properties.Filename)
223
224 inputPath := platformCompatConfigPath(ctx)
225 c.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath
226
227 // This ensures that outputFilePath has the correct name for others to
228 // use, as the source file may have a different name.
229 ctx.Build(pctx, android.BuildParams{
230 Rule: android.Cp,
231 Output: c.outputFilePath,
232 Input: inputPath,
233 })
234}
235
236func (h *globalCompatConfig) OutputFiles(tag string) (android.Paths, error) {
237 switch tag {
238 case "":
239 return android.Paths{h.outputFilePath}, nil
240 default:
241 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
242 }
243}
244
245// global_compat_config provides access to the merged compat config xml file generated by the build.
246func globalCompatConfigFactory() android.Module {
247 module := &globalCompatConfig{}
248 module.AddProperties(&module.properties)
249 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
250 return module
251}