blob: 218154e96b22b5c311844e475d568ad840771c0d [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
Mathew Inwood4d0c19c2019-12-09 11:32:29 +000038type platformCompatConfigSingleton struct {
39 metadata android.Path
40}
41
atrostdb25ac02019-08-05 12:26:07 +010042type platformCompatConfigProperties struct {
atrost87901b02019-08-29 12:48:43 +010043 Src *string `android:"path"`
atrostdb25ac02019-08-05 12:26:07 +010044}
45
46type platformCompatConfig struct {
47 android.ModuleBase
48
49 properties platformCompatConfigProperties
Colin Cross70dda7e2019-10-01 22:05:35 -070050 installDirPath android.InstallPath
atrostdb25ac02019-08-05 12:26:07 +010051 configFile android.OutputPath
Mathew Inwood0dd06f62019-12-17 11:14:42 +000052 metadataFile android.OutputPath
atrostdb25ac02019-08-05 12:26:07 +010053}
54
Mathew Inwood4d0c19c2019-12-09 11:32:29 +000055func (p *platformCompatConfig) compatConfigMetadata() android.OutputPath {
56 return p.metadataFile
57}
58
atrost6e126252020-01-27 17:01:16 +000059func (p *platformCompatConfig) CompatConfig() android.OutputPath {
60 return p.configFile
Mathew Inwood4d0c19c2019-12-09 11:32:29 +000061}
62
atrost6e126252020-01-27 17:01:16 +000063func (p *platformCompatConfig) SubDir() string {
64 return "compatconfig"
65}
66
67type PlatformCompatConfigIntf interface {
68 android.Module
69
70 compatConfigMetadata() android.OutputPath
71 CompatConfig() android.OutputPath
72 // Sub dir under etc dir.
73 SubDir() string
74}
75
76var _ PlatformCompatConfigIntf = (*platformCompatConfig)(nil)
Mathew Inwood4d0c19c2019-12-09 11:32:29 +000077
78// compat singleton rules
79func (p *platformCompatConfigSingleton) GenerateBuildActions(ctx android.SingletonContext) {
80
81 var compatConfigMetadata android.Paths
82
83 ctx.VisitAllModules(func(module android.Module) {
atrost6e126252020-01-27 17:01:16 +000084 if c, ok := module.(PlatformCompatConfigIntf); ok {
Mathew Inwood4d0c19c2019-12-09 11:32:29 +000085 metadata := c.compatConfigMetadata()
86 compatConfigMetadata = append(compatConfigMetadata, metadata)
87 }
88 })
89
90 if compatConfigMetadata == nil {
91 // nothing to do.
92 return
93 }
94
Colin Crossf1a035e2020-11-16 17:32:30 -080095 rule := android.NewRuleBuilder(pctx, ctx)
Mathew Inwoodabd49ab2019-12-19 14:27:08 +000096 outputPath := platformCompatConfigPath(ctx)
Mathew Inwood4d0c19c2019-12-09 11:32:29 +000097
98 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -080099 BuiltTool("process-compat-config").
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000100 FlagForEachInput("--xml ", compatConfigMetadata).
101 FlagWithOutput("--merged-config ", outputPath)
102
Colin Crossf1a035e2020-11-16 17:32:30 -0800103 rule.Build("merged-compat-config", "Merge compat config")
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000104
105 p.metadata = outputPath
106}
107
Mathew Inwood653c78a2019-12-19 12:38:10 +0000108func (p *platformCompatConfigSingleton) MakeVars(ctx android.MakeVarsContext) {
109 if p.metadata != nil {
110 ctx.Strict("INTERNAL_PLATFORM_MERGED_COMPAT_CONFIG", p.metadata.String())
111 }
112}
113
atrostdb25ac02019-08-05 12:26:07 +0100114func (p *platformCompatConfig) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossf1a035e2020-11-16 17:32:30 -0800115 rule := android.NewRuleBuilder(pctx, ctx)
atrostdb25ac02019-08-05 12:26:07 +0100116
atrost87901b02019-08-29 12:48:43 +0100117 configFileName := p.Name() + ".xml"
Mathew Inwood0dd06f62019-12-17 11:14:42 +0000118 metadataFileName := p.Name() + "_meta.xml"
atrostdb25ac02019-08-05 12:26:07 +0100119 p.configFile = android.PathForModuleOut(ctx, configFileName).OutputPath
Mathew Inwood0dd06f62019-12-17 11:14:42 +0000120 p.metadataFile = android.PathForModuleOut(ctx, metadataFileName).OutputPath
atrostdb25ac02019-08-05 12:26:07 +0100121 path := android.PathForModuleSrc(ctx, String(p.properties.Src))
122
atrostdb25ac02019-08-05 12:26:07 +0100123 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800124 BuiltTool("process-compat-config").
Mathew Inwood0dd06f62019-12-17 11:14:42 +0000125 FlagWithInput("--jar ", path).
126 FlagWithOutput("--device-config ", p.configFile).
127 FlagWithOutput("--merged-config ", p.metadataFile)
atrostdb25ac02019-08-05 12:26:07 +0100128
atrost87901b02019-08-29 12:48:43 +0100129 p.installDirPath = android.PathForModuleInstall(ctx, "etc", "compatconfig")
Colin Crossf1a035e2020-11-16 17:32:30 -0800130 rule.Build(configFileName, "Extract compat/compat_config.xml and install it")
atrostdb25ac02019-08-05 12:26:07 +0100131
132}
133
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900134func (p *platformCompatConfig) AndroidMkEntries() []android.AndroidMkEntries {
135 return []android.AndroidMkEntries{android.AndroidMkEntries{
atrostdb25ac02019-08-05 12:26:07 +0100136 Class: "ETC",
137 OutputFile: android.OptionalPathForPath(p.configFile),
138 Include: "$(BUILD_PREBUILT)",
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700139 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700140 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossff6c33d2019-10-02 16:01:35 -0700141 entries.SetString("LOCAL_MODULE_PATH", p.installDirPath.ToMakePath().String())
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700142 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", p.configFile.Base())
143 },
atrostdb25ac02019-08-05 12:26:07 +0100144 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900145 }}
atrostdb25ac02019-08-05 12:26:07 +0100146}
147
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000148func platformCompatConfigSingletonFactory() android.Singleton {
149 return &platformCompatConfigSingleton{}
150}
151
atrost6e126252020-01-27 17:01:16 +0000152func PlatformCompatConfigFactory() android.Module {
atrostdb25ac02019-08-05 12:26:07 +0100153 module := &platformCompatConfig{}
154 module.AddProperties(&module.properties)
155 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
156 return module
157}
Mathew Inwoodabd49ab2019-12-19 14:27:08 +0000158
159//============== 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}