blob: 01e651bfb53ee33087f87c395723c7b611b76c7a [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
Mathew Inwood4d0c19c2019-12-09 11:32:29 +000051func (p *platformCompatConfig) compatConfigMetadata() android.OutputPath {
52 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
63type PlatformCompatConfigIntf interface {
64 android.Module
65
66 compatConfigMetadata() android.OutputPath
67 CompatConfig() android.OutputPath
68 // Sub dir under etc dir.
69 SubDir() string
70}
71
72var _ PlatformCompatConfigIntf = (*platformCompatConfig)(nil)
Mathew Inwood4d0c19c2019-12-09 11:32:29 +000073
Paul Duffin96154332021-03-15 18:18:22 +000074func (p *platformCompatConfig) GenerateAndroidBuildActions(ctx android.ModuleContext) {
75 rule := android.NewRuleBuilder(pctx, ctx)
76
77 configFileName := p.Name() + ".xml"
78 metadataFileName := p.Name() + "_meta.xml"
79 p.configFile = android.PathForModuleOut(ctx, configFileName).OutputPath
80 p.metadataFile = android.PathForModuleOut(ctx, metadataFileName).OutputPath
81 path := android.PathForModuleSrc(ctx, String(p.properties.Src))
82
83 rule.Command().
84 BuiltTool("process-compat-config").
85 FlagWithInput("--jar ", path).
86 FlagWithOutput("--device-config ", p.configFile).
87 FlagWithOutput("--merged-config ", p.metadataFile)
88
89 p.installDirPath = android.PathForModuleInstall(ctx, "etc", "compatconfig")
90 rule.Build(configFileName, "Extract compat/compat_config.xml and install it")
91
92}
93
94func (p *platformCompatConfig) AndroidMkEntries() []android.AndroidMkEntries {
95 return []android.AndroidMkEntries{android.AndroidMkEntries{
96 Class: "ETC",
97 OutputFile: android.OptionalPathForPath(p.configFile),
98 Include: "$(BUILD_PREBUILT)",
99 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
100 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
101 entries.SetString("LOCAL_MODULE_PATH", p.installDirPath.ToMakePath().String())
102 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", p.configFile.Base())
103 },
104 },
105 }}
106}
107
108func PlatformCompatConfigFactory() android.Module {
109 module := &platformCompatConfig{}
110 module.AddProperties(&module.properties)
111 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
112 return module
113}
114
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000115// compat singleton rules
Paul Duffin96154332021-03-15 18:18:22 +0000116type platformCompatConfigSingleton struct {
117 metadata android.Path
118}
119
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000120func (p *platformCompatConfigSingleton) GenerateBuildActions(ctx android.SingletonContext) {
121
122 var compatConfigMetadata android.Paths
123
124 ctx.VisitAllModules(func(module android.Module) {
atrost6e126252020-01-27 17:01:16 +0000125 if c, ok := module.(PlatformCompatConfigIntf); ok {
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000126 metadata := c.compatConfigMetadata()
127 compatConfigMetadata = append(compatConfigMetadata, metadata)
128 }
129 })
130
131 if compatConfigMetadata == nil {
132 // nothing to do.
133 return
134 }
135
Colin Crossf1a035e2020-11-16 17:32:30 -0800136 rule := android.NewRuleBuilder(pctx, ctx)
Mathew Inwoodabd49ab2019-12-19 14:27:08 +0000137 outputPath := platformCompatConfigPath(ctx)
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000138
139 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800140 BuiltTool("process-compat-config").
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000141 FlagForEachInput("--xml ", compatConfigMetadata).
142 FlagWithOutput("--merged-config ", outputPath)
143
Colin Crossf1a035e2020-11-16 17:32:30 -0800144 rule.Build("merged-compat-config", "Merge compat config")
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000145
146 p.metadata = outputPath
147}
148
Mathew Inwood653c78a2019-12-19 12:38:10 +0000149func (p *platformCompatConfigSingleton) MakeVars(ctx android.MakeVarsContext) {
150 if p.metadata != nil {
151 ctx.Strict("INTERNAL_PLATFORM_MERGED_COMPAT_CONFIG", p.metadata.String())
152 }
153}
154
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000155func platformCompatConfigSingletonFactory() android.Singleton {
156 return &platformCompatConfigSingleton{}
157}
158
Mathew Inwoodabd49ab2019-12-19 14:27:08 +0000159//============== merged_compat_config =================
160type globalCompatConfigProperties struct {
161 // name of the file into which the metadata will be copied.
162 Filename *string
163}
164
165type globalCompatConfig struct {
166 android.ModuleBase
167
168 properties globalCompatConfigProperties
169
170 outputFilePath android.OutputPath
171}
172
173func (c *globalCompatConfig) GenerateAndroidBuildActions(ctx android.ModuleContext) {
174 filename := String(c.properties.Filename)
175
176 inputPath := platformCompatConfigPath(ctx)
177 c.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath
178
179 // This ensures that outputFilePath has the correct name for others to
180 // use, as the source file may have a different name.
181 ctx.Build(pctx, android.BuildParams{
182 Rule: android.Cp,
183 Output: c.outputFilePath,
184 Input: inputPath,
185 })
186}
187
188func (h *globalCompatConfig) OutputFiles(tag string) (android.Paths, error) {
189 switch tag {
190 case "":
191 return android.Paths{h.outputFilePath}, nil
192 default:
193 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
194 }
195}
196
197// global_compat_config provides access to the merged compat config xml file generated by the build.
198func globalCompatConfigFactory() android.Module {
199 module := &globalCompatConfig{}
200 module.AddProperties(&module.properties)
201 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
202 return module
203}