blob: 95c0574dac86d8b239f1769f2d980bf203518e4f [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)
atrostdb25ac02019-08-05 12:26:07 +010024 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
53type platformCompatConfigIntf interface {
54 compatConfigMetadata() android.OutputPath
55}
56
57var _ platformCompatConfigIntf = (*platformCompatConfig)(nil)
58
59// compat singleton rules
60func (p *platformCompatConfigSingleton) GenerateBuildActions(ctx android.SingletonContext) {
61
62 var compatConfigMetadata android.Paths
63
64 ctx.VisitAllModules(func(module android.Module) {
65 if c, ok := module.(platformCompatConfigIntf); ok {
66 metadata := c.compatConfigMetadata()
67 compatConfigMetadata = append(compatConfigMetadata, metadata)
68 }
69 })
70
71 if compatConfigMetadata == nil {
72 // nothing to do.
73 return
74 }
75
76 rule := android.NewRuleBuilder()
Mathew Inwoodabd49ab2019-12-19 14:27:08 +000077 outputPath := platformCompatConfigPath(ctx)
Mathew Inwood4d0c19c2019-12-09 11:32:29 +000078
79 rule.Command().
80 BuiltTool(ctx, "process-compat-config").
81 FlagForEachInput("--xml ", compatConfigMetadata).
82 FlagWithOutput("--merged-config ", outputPath)
83
84 rule.Build(pctx, ctx, "merged-compat-config", "Merge compat config")
85
86 p.metadata = outputPath
87}
88
Mathew Inwood653c78a2019-12-19 12:38:10 +000089func (p *platformCompatConfigSingleton) MakeVars(ctx android.MakeVarsContext) {
90 if p.metadata != nil {
91 ctx.Strict("INTERNAL_PLATFORM_MERGED_COMPAT_CONFIG", p.metadata.String())
92 }
93}
94
atrostdb25ac02019-08-05 12:26:07 +010095func (p *platformCompatConfig) GenerateAndroidBuildActions(ctx android.ModuleContext) {
96 rule := android.NewRuleBuilder()
97
atrost87901b02019-08-29 12:48:43 +010098 configFileName := p.Name() + ".xml"
Mathew Inwood0dd06f62019-12-17 11:14:42 +000099 metadataFileName := p.Name() + "_meta.xml"
atrostdb25ac02019-08-05 12:26:07 +0100100 p.configFile = android.PathForModuleOut(ctx, configFileName).OutputPath
Mathew Inwood0dd06f62019-12-17 11:14:42 +0000101 p.metadataFile = android.PathForModuleOut(ctx, metadataFileName).OutputPath
atrostdb25ac02019-08-05 12:26:07 +0100102 path := android.PathForModuleSrc(ctx, String(p.properties.Src))
103
atrostdb25ac02019-08-05 12:26:07 +0100104 rule.Command().
Mathew Inwood2471c082019-12-16 12:55:26 +0000105 BuiltTool(ctx, "process-compat-config").
Mathew Inwood0dd06f62019-12-17 11:14:42 +0000106 FlagWithInput("--jar ", path).
107 FlagWithOutput("--device-config ", p.configFile).
108 FlagWithOutput("--merged-config ", p.metadataFile)
atrostdb25ac02019-08-05 12:26:07 +0100109
atrost87901b02019-08-29 12:48:43 +0100110 p.installDirPath = android.PathForModuleInstall(ctx, "etc", "compatconfig")
atrostdb25ac02019-08-05 12:26:07 +0100111 rule.Build(pctx, ctx, configFileName, "Extract compat/compat_config.xml and install it")
112
113}
114
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900115func (p *platformCompatConfig) AndroidMkEntries() []android.AndroidMkEntries {
116 return []android.AndroidMkEntries{android.AndroidMkEntries{
atrostdb25ac02019-08-05 12:26:07 +0100117 Class: "ETC",
118 OutputFile: android.OptionalPathForPath(p.configFile),
119 Include: "$(BUILD_PREBUILT)",
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700120 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
121 func(entries *android.AndroidMkEntries) {
Colin Crossff6c33d2019-10-02 16:01:35 -0700122 entries.SetString("LOCAL_MODULE_PATH", p.installDirPath.ToMakePath().String())
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700123 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", p.configFile.Base())
124 },
atrostdb25ac02019-08-05 12:26:07 +0100125 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900126 }}
atrostdb25ac02019-08-05 12:26:07 +0100127}
128
Mathew Inwood4d0c19c2019-12-09 11:32:29 +0000129func platformCompatConfigSingletonFactory() android.Singleton {
130 return &platformCompatConfigSingleton{}
131}
132
atrostdb25ac02019-08-05 12:26:07 +0100133func platformCompatConfigFactory() android.Module {
134 module := &platformCompatConfig{}
135 module.AddProperties(&module.properties)
136 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
137 return module
138}
Mathew Inwoodabd49ab2019-12-19 14:27:08 +0000139
140//============== merged_compat_config =================
141type globalCompatConfigProperties struct {
142 // name of the file into which the metadata will be copied.
143 Filename *string
144}
145
146type globalCompatConfig struct {
147 android.ModuleBase
148
149 properties globalCompatConfigProperties
150
151 outputFilePath android.OutputPath
152}
153
154func (c *globalCompatConfig) GenerateAndroidBuildActions(ctx android.ModuleContext) {
155 filename := String(c.properties.Filename)
156
157 inputPath := platformCompatConfigPath(ctx)
158 c.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath
159
160 // This ensures that outputFilePath has the correct name for others to
161 // use, as the source file may have a different name.
162 ctx.Build(pctx, android.BuildParams{
163 Rule: android.Cp,
164 Output: c.outputFilePath,
165 Input: inputPath,
166 })
167}
168
169func (h *globalCompatConfig) OutputFiles(tag string) (android.Paths, error) {
170 switch tag {
171 case "":
172 return android.Paths{h.outputFilePath}, nil
173 default:
174 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
175 }
176}
177
178// global_compat_config provides access to the merged compat config xml file generated by the build.
179func globalCompatConfigFactory() android.Module {
180 module := &globalCompatConfig{}
181 module.AddProperties(&module.properties)
182 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
183 return module
184}