blob: 4c3143a48fd286dcd064c808a4108ea5c94beb89 [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)
29 ctx.RegisterModuleType("global_compat_config", globalCompatConfigFactory)
30}
31
32var PrepareForTestWithPlatformCompatConfig = android.FixtureRegisterWithContext(registerPlatformCompatConfigBuildComponents)
33
Mathew Inwoodabd49ab2019-12-19 14:27:08 +000034func platformCompatConfigPath(ctx android.PathContext) android.OutputPath {
35 return android.PathForOutput(ctx, "compat_config", "merged_compat_config.xml")
atrostdb25ac02019-08-05 12:26:07 +010036}
37
38type platformCompatConfigProperties struct {
atrost87901b02019-08-29 12:48:43 +010039 Src *string `android:"path"`
atrostdb25ac02019-08-05 12:26:07 +010040}
41
42type platformCompatConfig struct {
43 android.ModuleBase
44
45 properties platformCompatConfigProperties
Colin Cross70dda7e2019-10-01 22:05:35 -070046 installDirPath android.InstallPath
atrostdb25ac02019-08-05 12:26:07 +010047 configFile android.OutputPath
Mathew Inwood0dd06f62019-12-17 11:14:42 +000048 metadataFile android.OutputPath
atrostdb25ac02019-08-05 12:26:07 +010049}
50
Paul Duffin29072a92021-03-16 10:12:49 +000051func (p *platformCompatConfig) compatConfigMetadata() android.Path {
Mathew Inwood4d0c19c2019-12-09 11:32:29 +000052 return p.metadataFile
53}
54
atrost6e126252020-01-27 17:01:16 +000055func (p *platformCompatConfig) CompatConfig() android.OutputPath {
56 return p.configFile
Mathew Inwood4d0c19c2019-12-09 11:32:29 +000057}
58
atrost6e126252020-01-27 17:01:16 +000059func (p *platformCompatConfig) SubDir() string {
60 return "compatconfig"
61}
62
Paul Duffin29072a92021-03-16 10:12:49 +000063type platformCompatConfigMetadataProvider interface {
64 compatConfigMetadata() android.Path
65}
66
atrost6e126252020-01-27 17:01:16 +000067type PlatformCompatConfigIntf interface {
68 android.Module
69
atrost6e126252020-01-27 17:01:16 +000070 CompatConfig() android.OutputPath
71 // Sub dir under etc dir.
72 SubDir() string
73}
74
75var _ PlatformCompatConfigIntf = (*platformCompatConfig)(nil)
Paul Duffin29072a92021-03-16 10:12:49 +000076var _ platformCompatConfigMetadataProvider = (*platformCompatConfig)(nil)
Mathew Inwood4d0c19c2019-12-09 11:32:29 +000077
Paul Duffin96154332021-03-15 18:18:22 +000078func (p *platformCompatConfig) GenerateAndroidBuildActions(ctx android.ModuleContext) {
79 rule := android.NewRuleBuilder(pctx, ctx)
80
81 configFileName := p.Name() + ".xml"
82 metadataFileName := p.Name() + "_meta.xml"
83 p.configFile = android.PathForModuleOut(ctx, configFileName).OutputPath
84 p.metadataFile = android.PathForModuleOut(ctx, metadataFileName).OutputPath
85 path := android.PathForModuleSrc(ctx, String(p.properties.Src))
86
87 rule.Command().
88 BuiltTool("process-compat-config").
89 FlagWithInput("--jar ", path).
90 FlagWithOutput("--device-config ", p.configFile).
91 FlagWithOutput("--merged-config ", p.metadataFile)
92
93 p.installDirPath = android.PathForModuleInstall(ctx, "etc", "compatconfig")
94 rule.Build(configFileName, "Extract compat/compat_config.xml and install it")
95
96}
97
98func (p *platformCompatConfig) AndroidMkEntries() []android.AndroidMkEntries {
99 return []android.AndroidMkEntries{android.AndroidMkEntries{
100 Class: "ETC",
101 OutputFile: android.OptionalPathForPath(p.configFile),
102 Include: "$(BUILD_PREBUILT)",
103 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
104 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
105 entries.SetString("LOCAL_MODULE_PATH", p.installDirPath.ToMakePath().String())
106 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", p.configFile.Base())
107 },
108 },
109 }}
110}
111
112func PlatformCompatConfigFactory() android.Module {
113 module := &platformCompatConfig{}
114 module.AddProperties(&module.properties)
115 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
116 return module
117}
118
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000119// compat singleton rules
Paul Duffin96154332021-03-15 18:18:22 +0000120type platformCompatConfigSingleton struct {
121 metadata android.Path
122}
123
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000124func (p *platformCompatConfigSingleton) GenerateBuildActions(ctx android.SingletonContext) {
125
126 var compatConfigMetadata android.Paths
127
128 ctx.VisitAllModules(func(module android.Module) {
Paul Duffin29072a92021-03-16 10:12:49 +0000129 if c, ok := module.(platformCompatConfigMetadataProvider); ok {
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000130 metadata := c.compatConfigMetadata()
131 compatConfigMetadata = append(compatConfigMetadata, metadata)
132 }
133 })
134
135 if compatConfigMetadata == nil {
136 // nothing to do.
137 return
138 }
139
Colin Crossf1a035e2020-11-16 17:32:30 -0800140 rule := android.NewRuleBuilder(pctx, ctx)
Mathew Inwoodabd49ab2019-12-19 14:27:08 +0000141 outputPath := platformCompatConfigPath(ctx)
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000142
143 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800144 BuiltTool("process-compat-config").
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000145 FlagForEachInput("--xml ", compatConfigMetadata).
146 FlagWithOutput("--merged-config ", outputPath)
147
Colin Crossf1a035e2020-11-16 17:32:30 -0800148 rule.Build("merged-compat-config", "Merge compat config")
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000149
150 p.metadata = outputPath
151}
152
Mathew Inwood653c78a2019-12-19 12:38:10 +0000153func (p *platformCompatConfigSingleton) MakeVars(ctx android.MakeVarsContext) {
154 if p.metadata != nil {
155 ctx.Strict("INTERNAL_PLATFORM_MERGED_COMPAT_CONFIG", p.metadata.String())
156 }
157}
158
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000159func platformCompatConfigSingletonFactory() android.Singleton {
160 return &platformCompatConfigSingleton{}
161}
162
Mathew Inwoodabd49ab2019-12-19 14:27:08 +0000163//============== merged_compat_config =================
164type globalCompatConfigProperties struct {
165 // name of the file into which the metadata will be copied.
166 Filename *string
167}
168
169type globalCompatConfig struct {
170 android.ModuleBase
171
172 properties globalCompatConfigProperties
173
174 outputFilePath android.OutputPath
175}
176
177func (c *globalCompatConfig) GenerateAndroidBuildActions(ctx android.ModuleContext) {
178 filename := String(c.properties.Filename)
179
180 inputPath := platformCompatConfigPath(ctx)
181 c.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath
182
183 // This ensures that outputFilePath has the correct name for others to
184 // use, as the source file may have a different name.
185 ctx.Build(pctx, android.BuildParams{
186 Rule: android.Cp,
187 Output: c.outputFilePath,
188 Input: inputPath,
189 })
190}
191
192func (h *globalCompatConfig) OutputFiles(tag string) (android.Paths, error) {
193 switch tag {
194 case "":
195 return android.Paths{h.outputFilePath}, nil
196 default:
197 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
198 }
199}
200
201// global_compat_config provides access to the merged compat config xml file generated by the build.
202func globalCompatConfigFactory() android.Module {
203 module := &globalCompatConfig{}
204 module.AddProperties(&module.properties)
205 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
206 return module
207}