blob: 9bc821dfe4a13498598241a0c010b110e03ab16a [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() {
Mathew Inwood4d0c19c2019-12-09 11:32:29 +000023 android.RegisterSingletonType("platform_compat_config_singleton", platformCompatConfigSingletonFactory)
atrost6e126252020-01-27 17:01:16 +000024 android.RegisterModuleType("platform_compat_config", PlatformCompatConfigFactory)
Mathew Inwoodabd49ab2019-12-19 14:27:08 +000025 android.RegisterModuleType("global_compat_config", globalCompatConfigFactory)
26}
27
28func platformCompatConfigPath(ctx android.PathContext) android.OutputPath {
29 return android.PathForOutput(ctx, "compat_config", "merged_compat_config.xml")
atrostdb25ac02019-08-05 12:26:07 +010030}
31
Mathew Inwood4d0c19c2019-12-09 11:32:29 +000032type platformCompatConfigSingleton struct {
33 metadata android.Path
34}
35
atrostdb25ac02019-08-05 12:26:07 +010036type platformCompatConfigProperties struct {
atrost87901b02019-08-29 12:48:43 +010037 Src *string `android:"path"`
atrostdb25ac02019-08-05 12:26:07 +010038}
39
40type platformCompatConfig struct {
41 android.ModuleBase
42
43 properties platformCompatConfigProperties
Colin Cross70dda7e2019-10-01 22:05:35 -070044 installDirPath android.InstallPath
atrostdb25ac02019-08-05 12:26:07 +010045 configFile android.OutputPath
Mathew Inwood0dd06f62019-12-17 11:14:42 +000046 metadataFile android.OutputPath
atrostdb25ac02019-08-05 12:26:07 +010047}
48
Mathew Inwood4d0c19c2019-12-09 11:32:29 +000049func (p *platformCompatConfig) compatConfigMetadata() android.OutputPath {
50 return p.metadataFile
51}
52
atrost6e126252020-01-27 17:01:16 +000053func (p *platformCompatConfig) CompatConfig() android.OutputPath {
54 return p.configFile
Mathew Inwood4d0c19c2019-12-09 11:32:29 +000055}
56
atrost6e126252020-01-27 17:01:16 +000057func (p *platformCompatConfig) SubDir() string {
58 return "compatconfig"
59}
60
61type PlatformCompatConfigIntf interface {
62 android.Module
63
64 compatConfigMetadata() android.OutputPath
65 CompatConfig() android.OutputPath
66 // Sub dir under etc dir.
67 SubDir() string
68}
69
70var _ PlatformCompatConfigIntf = (*platformCompatConfig)(nil)
Mathew Inwood4d0c19c2019-12-09 11:32:29 +000071
72// compat singleton rules
73func (p *platformCompatConfigSingleton) GenerateBuildActions(ctx android.SingletonContext) {
74
75 var compatConfigMetadata android.Paths
76
77 ctx.VisitAllModules(func(module android.Module) {
atrost6e126252020-01-27 17:01:16 +000078 if c, ok := module.(PlatformCompatConfigIntf); ok {
Mathew Inwood4d0c19c2019-12-09 11:32:29 +000079 metadata := c.compatConfigMetadata()
80 compatConfigMetadata = append(compatConfigMetadata, metadata)
81 }
82 })
83
84 if compatConfigMetadata == nil {
85 // nothing to do.
86 return
87 }
88
Colin Crossf1a035e2020-11-16 17:32:30 -080089 rule := android.NewRuleBuilder(pctx, ctx)
Mathew Inwoodabd49ab2019-12-19 14:27:08 +000090 outputPath := platformCompatConfigPath(ctx)
Mathew Inwood4d0c19c2019-12-09 11:32:29 +000091
92 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -080093 BuiltTool("process-compat-config").
Mathew Inwood4d0c19c2019-12-09 11:32:29 +000094 FlagForEachInput("--xml ", compatConfigMetadata).
95 FlagWithOutput("--merged-config ", outputPath)
96
Colin Crossf1a035e2020-11-16 17:32:30 -080097 rule.Build("merged-compat-config", "Merge compat config")
Mathew Inwood4d0c19c2019-12-09 11:32:29 +000098
99 p.metadata = outputPath
100}
101
Mathew Inwood653c78a2019-12-19 12:38:10 +0000102func (p *platformCompatConfigSingleton) MakeVars(ctx android.MakeVarsContext) {
103 if p.metadata != nil {
104 ctx.Strict("INTERNAL_PLATFORM_MERGED_COMPAT_CONFIG", p.metadata.String())
105 }
106}
107
atrostdb25ac02019-08-05 12:26:07 +0100108func (p *platformCompatConfig) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossf1a035e2020-11-16 17:32:30 -0800109 rule := android.NewRuleBuilder(pctx, ctx)
atrostdb25ac02019-08-05 12:26:07 +0100110
atrost87901b02019-08-29 12:48:43 +0100111 configFileName := p.Name() + ".xml"
Mathew Inwood0dd06f62019-12-17 11:14:42 +0000112 metadataFileName := p.Name() + "_meta.xml"
atrostdb25ac02019-08-05 12:26:07 +0100113 p.configFile = android.PathForModuleOut(ctx, configFileName).OutputPath
Mathew Inwood0dd06f62019-12-17 11:14:42 +0000114 p.metadataFile = android.PathForModuleOut(ctx, metadataFileName).OutputPath
atrostdb25ac02019-08-05 12:26:07 +0100115 path := android.PathForModuleSrc(ctx, String(p.properties.Src))
116
atrostdb25ac02019-08-05 12:26:07 +0100117 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800118 BuiltTool("process-compat-config").
Mathew Inwood0dd06f62019-12-17 11:14:42 +0000119 FlagWithInput("--jar ", path).
120 FlagWithOutput("--device-config ", p.configFile).
121 FlagWithOutput("--merged-config ", p.metadataFile)
atrostdb25ac02019-08-05 12:26:07 +0100122
atrost87901b02019-08-29 12:48:43 +0100123 p.installDirPath = android.PathForModuleInstall(ctx, "etc", "compatconfig")
Colin Crossf1a035e2020-11-16 17:32:30 -0800124 rule.Build(configFileName, "Extract compat/compat_config.xml and install it")
atrostdb25ac02019-08-05 12:26:07 +0100125
126}
127
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900128func (p *platformCompatConfig) AndroidMkEntries() []android.AndroidMkEntries {
129 return []android.AndroidMkEntries{android.AndroidMkEntries{
atrostdb25ac02019-08-05 12:26:07 +0100130 Class: "ETC",
131 OutputFile: android.OptionalPathForPath(p.configFile),
132 Include: "$(BUILD_PREBUILT)",
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700133 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
134 func(entries *android.AndroidMkEntries) {
Colin Crossff6c33d2019-10-02 16:01:35 -0700135 entries.SetString("LOCAL_MODULE_PATH", p.installDirPath.ToMakePath().String())
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700136 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", p.configFile.Base())
137 },
atrostdb25ac02019-08-05 12:26:07 +0100138 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900139 }}
atrostdb25ac02019-08-05 12:26:07 +0100140}
141
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000142func platformCompatConfigSingletonFactory() android.Singleton {
143 return &platformCompatConfigSingleton{}
144}
145
atrost6e126252020-01-27 17:01:16 +0000146func PlatformCompatConfigFactory() android.Module {
atrostdb25ac02019-08-05 12:26:07 +0100147 module := &platformCompatConfig{}
148 module.AddProperties(&module.properties)
149 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
150 return module
151}
Mathew Inwoodabd49ab2019-12-19 14:27:08 +0000152
153//============== merged_compat_config =================
154type globalCompatConfigProperties struct {
155 // name of the file into which the metadata will be copied.
156 Filename *string
157}
158
159type globalCompatConfig struct {
160 android.ModuleBase
161
162 properties globalCompatConfigProperties
163
164 outputFilePath android.OutputPath
165}
166
167func (c *globalCompatConfig) GenerateAndroidBuildActions(ctx android.ModuleContext) {
168 filename := String(c.properties.Filename)
169
170 inputPath := platformCompatConfigPath(ctx)
171 c.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath
172
173 // This ensures that outputFilePath has the correct name for others to
174 // use, as the source file may have a different name.
175 ctx.Build(pctx, android.BuildParams{
176 Rule: android.Cp,
177 Output: c.outputFilePath,
178 Input: inputPath,
179 })
180}
181
182func (h *globalCompatConfig) OutputFiles(tag string) (android.Paths, error) {
183 switch tag {
184 case "":
185 return android.Paths{h.outputFilePath}, nil
186 default:
187 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
188 }
189}
190
191// global_compat_config provides access to the merged compat config xml file generated by the build.
192func globalCompatConfigFactory() android.Module {
193 module := &globalCompatConfig{}
194 module.AddProperties(&module.properties)
195 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
196 return module
197}