atrost | db25ac0 | 2019-08-05 12:26:07 +0100 | [diff] [blame] | 1 | // 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 | |
| 15 | package java |
| 16 | |
| 17 | import ( |
| 18 | "android/soong/android" |
Mathew Inwood | abd49ab | 2019-12-19 14:27:08 +0000 | [diff] [blame] | 19 | "fmt" |
atrost | db25ac0 | 2019-08-05 12:26:07 +0100 | [diff] [blame] | 20 | ) |
| 21 | |
| 22 | func init() { |
Paul Duffin | 4eb4b41 | 2021-03-09 02:59:25 +0000 | [diff] [blame] | 23 | registerPlatformCompatConfigBuildComponents(android.InitRegistrationContext) |
Mathew Inwood | abd49ab | 2019-12-19 14:27:08 +0000 | [diff] [blame] | 24 | } |
| 25 | |
Paul Duffin | 4eb4b41 | 2021-03-09 02:59:25 +0000 | [diff] [blame] | 26 | func registerPlatformCompatConfigBuildComponents(ctx android.RegistrationContext) { |
| 27 | ctx.RegisterSingletonType("platform_compat_config_singleton", platformCompatConfigSingletonFactory) |
| 28 | ctx.RegisterModuleType("platform_compat_config", PlatformCompatConfigFactory) |
Paul Duffin | 1b29e00 | 2021-03-16 15:06:54 +0000 | [diff] [blame^] | 29 | ctx.RegisterModuleType("prebuilt_platform_compat_config", prebuiltCompatConfigFactory) |
Paul Duffin | 4eb4b41 | 2021-03-09 02:59:25 +0000 | [diff] [blame] | 30 | ctx.RegisterModuleType("global_compat_config", globalCompatConfigFactory) |
| 31 | } |
| 32 | |
| 33 | var PrepareForTestWithPlatformCompatConfig = android.FixtureRegisterWithContext(registerPlatformCompatConfigBuildComponents) |
| 34 | |
Mathew Inwood | abd49ab | 2019-12-19 14:27:08 +0000 | [diff] [blame] | 35 | func platformCompatConfigPath(ctx android.PathContext) android.OutputPath { |
| 36 | return android.PathForOutput(ctx, "compat_config", "merged_compat_config.xml") |
atrost | db25ac0 | 2019-08-05 12:26:07 +0100 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | type platformCompatConfigProperties struct { |
atrost | 87901b0 | 2019-08-29 12:48:43 +0100 | [diff] [blame] | 40 | Src *string `android:"path"` |
atrost | db25ac0 | 2019-08-05 12:26:07 +0100 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | type platformCompatConfig struct { |
| 44 | android.ModuleBase |
| 45 | |
| 46 | properties platformCompatConfigProperties |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 47 | installDirPath android.InstallPath |
atrost | db25ac0 | 2019-08-05 12:26:07 +0100 | [diff] [blame] | 48 | configFile android.OutputPath |
Mathew Inwood | 0dd06f6 | 2019-12-17 11:14:42 +0000 | [diff] [blame] | 49 | metadataFile android.OutputPath |
atrost | db25ac0 | 2019-08-05 12:26:07 +0100 | [diff] [blame] | 50 | } |
| 51 | |
Paul Duffin | 29072a9 | 2021-03-16 10:12:49 +0000 | [diff] [blame] | 52 | func (p *platformCompatConfig) compatConfigMetadata() android.Path { |
Mathew Inwood | 4d0c19c | 2019-12-09 11:32:29 +0000 | [diff] [blame] | 53 | return p.metadataFile |
| 54 | } |
| 55 | |
atrost | 6e12625 | 2020-01-27 17:01:16 +0000 | [diff] [blame] | 56 | func (p *platformCompatConfig) CompatConfig() android.OutputPath { |
| 57 | return p.configFile |
Mathew Inwood | 4d0c19c | 2019-12-09 11:32:29 +0000 | [diff] [blame] | 58 | } |
| 59 | |
atrost | 6e12625 | 2020-01-27 17:01:16 +0000 | [diff] [blame] | 60 | func (p *platformCompatConfig) SubDir() string { |
| 61 | return "compatconfig" |
| 62 | } |
| 63 | |
Paul Duffin | 29072a9 | 2021-03-16 10:12:49 +0000 | [diff] [blame] | 64 | type platformCompatConfigMetadataProvider interface { |
| 65 | compatConfigMetadata() android.Path |
| 66 | } |
| 67 | |
atrost | 6e12625 | 2020-01-27 17:01:16 +0000 | [diff] [blame] | 68 | type PlatformCompatConfigIntf interface { |
| 69 | android.Module |
| 70 | |
atrost | 6e12625 | 2020-01-27 17:01:16 +0000 | [diff] [blame] | 71 | CompatConfig() android.OutputPath |
| 72 | // Sub dir under etc dir. |
| 73 | SubDir() string |
| 74 | } |
| 75 | |
| 76 | var _ PlatformCompatConfigIntf = (*platformCompatConfig)(nil) |
Paul Duffin | 29072a9 | 2021-03-16 10:12:49 +0000 | [diff] [blame] | 77 | var _ platformCompatConfigMetadataProvider = (*platformCompatConfig)(nil) |
Mathew Inwood | 4d0c19c | 2019-12-09 11:32:29 +0000 | [diff] [blame] | 78 | |
Paul Duffin | 9615433 | 2021-03-15 18:18:22 +0000 | [diff] [blame] | 79 | func (p *platformCompatConfig) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 80 | rule := android.NewRuleBuilder(pctx, ctx) |
| 81 | |
| 82 | configFileName := p.Name() + ".xml" |
| 83 | metadataFileName := p.Name() + "_meta.xml" |
| 84 | p.configFile = android.PathForModuleOut(ctx, configFileName).OutputPath |
| 85 | p.metadataFile = android.PathForModuleOut(ctx, metadataFileName).OutputPath |
| 86 | path := android.PathForModuleSrc(ctx, String(p.properties.Src)) |
| 87 | |
| 88 | rule.Command(). |
| 89 | BuiltTool("process-compat-config"). |
| 90 | FlagWithInput("--jar ", path). |
| 91 | FlagWithOutput("--device-config ", p.configFile). |
| 92 | FlagWithOutput("--merged-config ", p.metadataFile) |
| 93 | |
| 94 | p.installDirPath = android.PathForModuleInstall(ctx, "etc", "compatconfig") |
| 95 | rule.Build(configFileName, "Extract compat/compat_config.xml and install it") |
| 96 | |
| 97 | } |
| 98 | |
| 99 | func (p *platformCompatConfig) AndroidMkEntries() []android.AndroidMkEntries { |
| 100 | return []android.AndroidMkEntries{android.AndroidMkEntries{ |
| 101 | Class: "ETC", |
| 102 | OutputFile: android.OptionalPathForPath(p.configFile), |
| 103 | Include: "$(BUILD_PREBUILT)", |
| 104 | ExtraEntries: []android.AndroidMkExtraEntriesFunc{ |
| 105 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { |
| 106 | entries.SetString("LOCAL_MODULE_PATH", p.installDirPath.ToMakePath().String()) |
| 107 | entries.SetString("LOCAL_INSTALLED_MODULE_STEM", p.configFile.Base()) |
| 108 | }, |
| 109 | }, |
| 110 | }} |
| 111 | } |
| 112 | |
| 113 | func PlatformCompatConfigFactory() android.Module { |
| 114 | module := &platformCompatConfig{} |
| 115 | module.AddProperties(&module.properties) |
| 116 | android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon) |
| 117 | return module |
| 118 | } |
| 119 | |
Paul Duffin | 1b29e00 | 2021-03-16 15:06:54 +0000 | [diff] [blame^] | 120 | // A prebuilt version of the platform compat config module. |
| 121 | type prebuiltCompatConfigModule struct { |
| 122 | android.ModuleBase |
| 123 | android.SdkBase |
| 124 | prebuilt android.Prebuilt |
| 125 | |
| 126 | properties prebuiltCompatConfigProperties |
| 127 | |
| 128 | metadataFile android.Path |
| 129 | } |
| 130 | |
| 131 | type prebuiltCompatConfigProperties struct { |
| 132 | Metadata *string `android:"path"` |
| 133 | } |
| 134 | |
| 135 | func (module *prebuiltCompatConfigModule) Prebuilt() *android.Prebuilt { |
| 136 | return &module.prebuilt |
| 137 | } |
| 138 | |
| 139 | func (module *prebuiltCompatConfigModule) Name() string { |
| 140 | return module.prebuilt.Name(module.ModuleBase.Name()) |
| 141 | } |
| 142 | |
| 143 | func (module *prebuiltCompatConfigModule) compatConfigMetadata() android.Path { |
| 144 | return module.metadataFile |
| 145 | } |
| 146 | |
| 147 | var _ platformCompatConfigMetadataProvider = (*prebuiltCompatConfigModule)(nil) |
| 148 | |
| 149 | func (module *prebuiltCompatConfigModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 150 | module.metadataFile = module.prebuilt.SingleSourcePath(ctx) |
| 151 | } |
| 152 | |
| 153 | // A prebuilt version of platform_compat_config that provides the metadata. |
| 154 | func prebuiltCompatConfigFactory() android.Module { |
| 155 | m := &prebuiltCompatConfigModule{} |
| 156 | m.AddProperties(&m.properties) |
| 157 | android.InitSingleSourcePrebuiltModule(m, &m.properties, "Metadata") |
| 158 | android.InitSdkAwareModule(m) |
| 159 | android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon) |
| 160 | return m |
| 161 | } |
| 162 | |
Mathew Inwood | 4d0c19c | 2019-12-09 11:32:29 +0000 | [diff] [blame] | 163 | // compat singleton rules |
Paul Duffin | 9615433 | 2021-03-15 18:18:22 +0000 | [diff] [blame] | 164 | type platformCompatConfigSingleton struct { |
| 165 | metadata android.Path |
| 166 | } |
| 167 | |
Mathew Inwood | 4d0c19c | 2019-12-09 11:32:29 +0000 | [diff] [blame] | 168 | func (p *platformCompatConfigSingleton) GenerateBuildActions(ctx android.SingletonContext) { |
| 169 | |
| 170 | var compatConfigMetadata android.Paths |
| 171 | |
| 172 | ctx.VisitAllModules(func(module android.Module) { |
Paul Duffin | 29072a9 | 2021-03-16 10:12:49 +0000 | [diff] [blame] | 173 | if c, ok := module.(platformCompatConfigMetadataProvider); ok { |
Mathew Inwood | 4d0c19c | 2019-12-09 11:32:29 +0000 | [diff] [blame] | 174 | metadata := c.compatConfigMetadata() |
| 175 | compatConfigMetadata = append(compatConfigMetadata, metadata) |
| 176 | } |
| 177 | }) |
| 178 | |
| 179 | if compatConfigMetadata == nil { |
| 180 | // nothing to do. |
| 181 | return |
| 182 | } |
| 183 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 184 | rule := android.NewRuleBuilder(pctx, ctx) |
Mathew Inwood | abd49ab | 2019-12-19 14:27:08 +0000 | [diff] [blame] | 185 | outputPath := platformCompatConfigPath(ctx) |
Mathew Inwood | 4d0c19c | 2019-12-09 11:32:29 +0000 | [diff] [blame] | 186 | |
| 187 | rule.Command(). |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 188 | BuiltTool("process-compat-config"). |
Mathew Inwood | 4d0c19c | 2019-12-09 11:32:29 +0000 | [diff] [blame] | 189 | FlagForEachInput("--xml ", compatConfigMetadata). |
| 190 | FlagWithOutput("--merged-config ", outputPath) |
| 191 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 192 | rule.Build("merged-compat-config", "Merge compat config") |
Mathew Inwood | 4d0c19c | 2019-12-09 11:32:29 +0000 | [diff] [blame] | 193 | |
| 194 | p.metadata = outputPath |
| 195 | } |
| 196 | |
Mathew Inwood | 653c78a | 2019-12-19 12:38:10 +0000 | [diff] [blame] | 197 | func (p *platformCompatConfigSingleton) MakeVars(ctx android.MakeVarsContext) { |
| 198 | if p.metadata != nil { |
| 199 | ctx.Strict("INTERNAL_PLATFORM_MERGED_COMPAT_CONFIG", p.metadata.String()) |
| 200 | } |
| 201 | } |
| 202 | |
Mathew Inwood | 4d0c19c | 2019-12-09 11:32:29 +0000 | [diff] [blame] | 203 | func platformCompatConfigSingletonFactory() android.Singleton { |
| 204 | return &platformCompatConfigSingleton{} |
| 205 | } |
| 206 | |
Mathew Inwood | abd49ab | 2019-12-19 14:27:08 +0000 | [diff] [blame] | 207 | //============== merged_compat_config ================= |
| 208 | type globalCompatConfigProperties struct { |
| 209 | // name of the file into which the metadata will be copied. |
| 210 | Filename *string |
| 211 | } |
| 212 | |
| 213 | type globalCompatConfig struct { |
| 214 | android.ModuleBase |
| 215 | |
| 216 | properties globalCompatConfigProperties |
| 217 | |
| 218 | outputFilePath android.OutputPath |
| 219 | } |
| 220 | |
| 221 | func (c *globalCompatConfig) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 222 | filename := String(c.properties.Filename) |
| 223 | |
| 224 | inputPath := platformCompatConfigPath(ctx) |
| 225 | c.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath |
| 226 | |
| 227 | // This ensures that outputFilePath has the correct name for others to |
| 228 | // use, as the source file may have a different name. |
| 229 | ctx.Build(pctx, android.BuildParams{ |
| 230 | Rule: android.Cp, |
| 231 | Output: c.outputFilePath, |
| 232 | Input: inputPath, |
| 233 | }) |
| 234 | } |
| 235 | |
| 236 | func (h *globalCompatConfig) OutputFiles(tag string) (android.Paths, error) { |
| 237 | switch tag { |
| 238 | case "": |
| 239 | return android.Paths{h.outputFilePath}, nil |
| 240 | default: |
| 241 | return nil, fmt.Errorf("unsupported module reference tag %q", tag) |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | // global_compat_config provides access to the merged compat config xml file generated by the build. |
| 246 | func globalCompatConfigFactory() android.Module { |
| 247 | module := &globalCompatConfig{} |
| 248 | module.AddProperties(&module.properties) |
| 249 | android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon) |
| 250 | return module |
| 251 | } |