| 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 ( | 
| Paul Duffin | d796f6f | 2022-11-23 23:06:05 +0000 | [diff] [blame] | 18 | "fmt" | 
| Paul Duffin | 001b234 | 2021-03-11 18:43:31 +0000 | [diff] [blame] | 19 | "path/filepath" | 
|  | 20 |  | 
| atrost | db25ac0 | 2019-08-05 12:26:07 +0100 | [diff] [blame] | 21 | "android/soong/android" | 
| Paul Duffin | 001b234 | 2021-03-11 18:43:31 +0000 | [diff] [blame] | 22 | "github.com/google/blueprint" | 
| Spandan Das | e4c911e | 2024-01-19 00:22:22 +0000 | [diff] [blame] | 23 | "github.com/google/blueprint/proptools" | 
| atrost | db25ac0 | 2019-08-05 12:26:07 +0100 | [diff] [blame] | 24 | ) | 
|  | 25 |  | 
|  | 26 | func init() { | 
| Paul Duffin | 4eb4b41 | 2021-03-09 02:59:25 +0000 | [diff] [blame] | 27 | registerPlatformCompatConfigBuildComponents(android.InitRegistrationContext) | 
| Paul Duffin | 001b234 | 2021-03-11 18:43:31 +0000 | [diff] [blame] | 28 |  | 
| Paul Duffin | fcf7985 | 2022-07-20 14:18:24 +0000 | [diff] [blame] | 29 | android.RegisterSdkMemberType(CompatConfigSdkMemberType) | 
|  | 30 | } | 
|  | 31 |  | 
|  | 32 | var CompatConfigSdkMemberType = &compatConfigMemberType{ | 
|  | 33 | SdkMemberTypeBase: android.SdkMemberTypeBase{ | 
|  | 34 | PropertyName: "compat_configs", | 
|  | 35 | SupportsSdk:  true, | 
|  | 36 | }, | 
| Mathew Inwood | abd49ab | 2019-12-19 14:27:08 +0000 | [diff] [blame] | 37 | } | 
|  | 38 |  | 
| Paul Duffin | 4eb4b41 | 2021-03-09 02:59:25 +0000 | [diff] [blame] | 39 | func registerPlatformCompatConfigBuildComponents(ctx android.RegistrationContext) { | 
| LaMont Jones | 0c10e4d | 2023-05-16 00:58:37 +0000 | [diff] [blame] | 40 | ctx.RegisterParallelSingletonType("platform_compat_config_singleton", platformCompatConfigSingletonFactory) | 
| Paul Duffin | 4eb4b41 | 2021-03-09 02:59:25 +0000 | [diff] [blame] | 41 | ctx.RegisterModuleType("platform_compat_config", PlatformCompatConfigFactory) | 
| Paul Duffin | 1b29e00 | 2021-03-16 15:06:54 +0000 | [diff] [blame] | 42 | ctx.RegisterModuleType("prebuilt_platform_compat_config", prebuiltCompatConfigFactory) | 
| Paul Duffin | 4eb4b41 | 2021-03-09 02:59:25 +0000 | [diff] [blame] | 43 | ctx.RegisterModuleType("global_compat_config", globalCompatConfigFactory) | 
|  | 44 | } | 
|  | 45 |  | 
|  | 46 | var PrepareForTestWithPlatformCompatConfig = android.FixtureRegisterWithContext(registerPlatformCompatConfigBuildComponents) | 
|  | 47 |  | 
| Mathew Inwood | abd49ab | 2019-12-19 14:27:08 +0000 | [diff] [blame] | 48 | func platformCompatConfigPath(ctx android.PathContext) android.OutputPath { | 
|  | 49 | return android.PathForOutput(ctx, "compat_config", "merged_compat_config.xml") | 
| atrost | db25ac0 | 2019-08-05 12:26:07 +0100 | [diff] [blame] | 50 | } | 
|  | 51 |  | 
|  | 52 | type platformCompatConfigProperties struct { | 
| atrost | 87901b0 | 2019-08-29 12:48:43 +0100 | [diff] [blame] | 53 | Src *string `android:"path"` | 
| atrost | db25ac0 | 2019-08-05 12:26:07 +0100 | [diff] [blame] | 54 | } | 
|  | 55 |  | 
|  | 56 | type platformCompatConfig struct { | 
|  | 57 | android.ModuleBase | 
|  | 58 |  | 
|  | 59 | properties     platformCompatConfigProperties | 
| Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 60 | installDirPath android.InstallPath | 
| atrost | db25ac0 | 2019-08-05 12:26:07 +0100 | [diff] [blame] | 61 | configFile     android.OutputPath | 
| Mathew Inwood | 0dd06f6 | 2019-12-17 11:14:42 +0000 | [diff] [blame] | 62 | metadataFile   android.OutputPath | 
| atrost | db25ac0 | 2019-08-05 12:26:07 +0100 | [diff] [blame] | 63 | } | 
|  | 64 |  | 
| Paul Duffin | 29072a9 | 2021-03-16 10:12:49 +0000 | [diff] [blame] | 65 | func (p *platformCompatConfig) compatConfigMetadata() android.Path { | 
| Mathew Inwood | 4d0c19c | 2019-12-09 11:32:29 +0000 | [diff] [blame] | 66 | return p.metadataFile | 
|  | 67 | } | 
|  | 68 |  | 
| atrost | 6e12625 | 2020-01-27 17:01:16 +0000 | [diff] [blame] | 69 | func (p *platformCompatConfig) CompatConfig() android.OutputPath { | 
|  | 70 | return p.configFile | 
| Mathew Inwood | 4d0c19c | 2019-12-09 11:32:29 +0000 | [diff] [blame] | 71 | } | 
|  | 72 |  | 
| atrost | 6e12625 | 2020-01-27 17:01:16 +0000 | [diff] [blame] | 73 | func (p *platformCompatConfig) SubDir() string { | 
|  | 74 | return "compatconfig" | 
|  | 75 | } | 
|  | 76 |  | 
| Paul Duffin | 29072a9 | 2021-03-16 10:12:49 +0000 | [diff] [blame] | 77 | type platformCompatConfigMetadataProvider interface { | 
|  | 78 | compatConfigMetadata() android.Path | 
|  | 79 | } | 
|  | 80 |  | 
| atrost | 6e12625 | 2020-01-27 17:01:16 +0000 | [diff] [blame] | 81 | type PlatformCompatConfigIntf interface { | 
|  | 82 | android.Module | 
|  | 83 |  | 
| atrost | 6e12625 | 2020-01-27 17:01:16 +0000 | [diff] [blame] | 84 | CompatConfig() android.OutputPath | 
|  | 85 | // Sub dir under etc dir. | 
|  | 86 | SubDir() string | 
|  | 87 | } | 
|  | 88 |  | 
|  | 89 | var _ PlatformCompatConfigIntf = (*platformCompatConfig)(nil) | 
| Paul Duffin | 29072a9 | 2021-03-16 10:12:49 +0000 | [diff] [blame] | 90 | var _ platformCompatConfigMetadataProvider = (*platformCompatConfig)(nil) | 
| Mathew Inwood | 4d0c19c | 2019-12-09 11:32:29 +0000 | [diff] [blame] | 91 |  | 
| Paul Duffin | 9615433 | 2021-03-15 18:18:22 +0000 | [diff] [blame] | 92 | func (p *platformCompatConfig) GenerateAndroidBuildActions(ctx android.ModuleContext) { | 
|  | 93 | rule := android.NewRuleBuilder(pctx, ctx) | 
|  | 94 |  | 
|  | 95 | configFileName := p.Name() + ".xml" | 
|  | 96 | metadataFileName := p.Name() + "_meta.xml" | 
|  | 97 | p.configFile = android.PathForModuleOut(ctx, configFileName).OutputPath | 
|  | 98 | p.metadataFile = android.PathForModuleOut(ctx, metadataFileName).OutputPath | 
|  | 99 | path := android.PathForModuleSrc(ctx, String(p.properties.Src)) | 
|  | 100 |  | 
|  | 101 | rule.Command(). | 
|  | 102 | BuiltTool("process-compat-config"). | 
|  | 103 | FlagWithInput("--jar ", path). | 
|  | 104 | FlagWithOutput("--device-config ", p.configFile). | 
|  | 105 | FlagWithOutput("--merged-config ", p.metadataFile) | 
|  | 106 |  | 
|  | 107 | p.installDirPath = android.PathForModuleInstall(ctx, "etc", "compatconfig") | 
|  | 108 | rule.Build(configFileName, "Extract compat/compat_config.xml and install it") | 
|  | 109 |  | 
|  | 110 | } | 
|  | 111 |  | 
|  | 112 | func (p *platformCompatConfig) AndroidMkEntries() []android.AndroidMkEntries { | 
|  | 113 | return []android.AndroidMkEntries{android.AndroidMkEntries{ | 
|  | 114 | Class:      "ETC", | 
|  | 115 | OutputFile: android.OptionalPathForPath(p.configFile), | 
|  | 116 | Include:    "$(BUILD_PREBUILT)", | 
|  | 117 | ExtraEntries: []android.AndroidMkExtraEntriesFunc{ | 
|  | 118 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { | 
| Colin Cross | c68db4b | 2021-11-11 18:59:15 -0800 | [diff] [blame] | 119 | entries.SetString("LOCAL_MODULE_PATH", p.installDirPath.String()) | 
| Paul Duffin | 9615433 | 2021-03-15 18:18:22 +0000 | [diff] [blame] | 120 | entries.SetString("LOCAL_INSTALLED_MODULE_STEM", p.configFile.Base()) | 
|  | 121 | }, | 
|  | 122 | }, | 
|  | 123 | }} | 
|  | 124 | } | 
|  | 125 |  | 
|  | 126 | func PlatformCompatConfigFactory() android.Module { | 
|  | 127 | module := &platformCompatConfig{} | 
|  | 128 | module.AddProperties(&module.properties) | 
|  | 129 | android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon) | 
|  | 130 | return module | 
|  | 131 | } | 
|  | 132 |  | 
| Paul Duffin | 001b234 | 2021-03-11 18:43:31 +0000 | [diff] [blame] | 133 | type compatConfigMemberType struct { | 
|  | 134 | android.SdkMemberTypeBase | 
|  | 135 | } | 
|  | 136 |  | 
| Paul Duffin | 296701e | 2021-07-14 10:29:36 +0100 | [diff] [blame] | 137 | func (b *compatConfigMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) { | 
|  | 138 | ctx.AddVariationDependencies(nil, dependencyTag, names...) | 
| Paul Duffin | 001b234 | 2021-03-11 18:43:31 +0000 | [diff] [blame] | 139 | } | 
|  | 140 |  | 
|  | 141 | func (b *compatConfigMemberType) IsInstance(module android.Module) bool { | 
|  | 142 | _, ok := module.(*platformCompatConfig) | 
|  | 143 | return ok | 
|  | 144 | } | 
|  | 145 |  | 
|  | 146 | func (b *compatConfigMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule { | 
|  | 147 | return ctx.SnapshotBuilder().AddPrebuiltModule(member, "prebuilt_platform_compat_config") | 
|  | 148 | } | 
|  | 149 |  | 
|  | 150 | func (b *compatConfigMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties { | 
|  | 151 | return &compatConfigSdkMemberProperties{} | 
|  | 152 | } | 
|  | 153 |  | 
|  | 154 | type compatConfigSdkMemberProperties struct { | 
|  | 155 | android.SdkMemberPropertiesBase | 
|  | 156 |  | 
|  | 157 | Metadata android.Path | 
|  | 158 | } | 
|  | 159 |  | 
|  | 160 | func (b *compatConfigSdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) { | 
|  | 161 | module := variant.(*platformCompatConfig) | 
|  | 162 | b.Metadata = module.metadataFile | 
|  | 163 | } | 
|  | 164 |  | 
|  | 165 | func (b *compatConfigSdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) { | 
|  | 166 | builder := ctx.SnapshotBuilder() | 
|  | 167 | if b.Metadata != nil { | 
|  | 168 | snapshotRelativePath := filepath.Join("compat_configs", ctx.Name(), b.Metadata.Base()) | 
|  | 169 | builder.CopyToSnapshot(b.Metadata, snapshotRelativePath) | 
|  | 170 | propertySet.AddProperty("metadata", snapshotRelativePath) | 
|  | 171 | } | 
|  | 172 | } | 
|  | 173 |  | 
|  | 174 | var _ android.SdkMemberType = (*compatConfigMemberType)(nil) | 
|  | 175 |  | 
| Paul Duffin | 1b29e00 | 2021-03-16 15:06:54 +0000 | [diff] [blame] | 176 | // A prebuilt version of the platform compat config module. | 
|  | 177 | type prebuiltCompatConfigModule struct { | 
|  | 178 | android.ModuleBase | 
| Paul Duffin | 1b29e00 | 2021-03-16 15:06:54 +0000 | [diff] [blame] | 179 | prebuilt android.Prebuilt | 
|  | 180 |  | 
|  | 181 | properties prebuiltCompatConfigProperties | 
|  | 182 |  | 
|  | 183 | metadataFile android.Path | 
|  | 184 | } | 
|  | 185 |  | 
|  | 186 | type prebuiltCompatConfigProperties struct { | 
|  | 187 | Metadata *string `android:"path"` | 
| Spandan Das | e4c911e | 2024-01-19 00:22:22 +0000 | [diff] [blame] | 188 |  | 
|  | 189 | // Name of the source soong module that gets shadowed by this prebuilt | 
|  | 190 | // If unspecified, follows the naming convention that the source module of | 
|  | 191 | // the prebuilt is Name() without "prebuilt_" prefix | 
|  | 192 | Source_module_name *string | 
| Paul Duffin | 1b29e00 | 2021-03-16 15:06:54 +0000 | [diff] [blame] | 193 | } | 
|  | 194 |  | 
|  | 195 | func (module *prebuiltCompatConfigModule) Prebuilt() *android.Prebuilt { | 
|  | 196 | return &module.prebuilt | 
|  | 197 | } | 
|  | 198 |  | 
|  | 199 | func (module *prebuiltCompatConfigModule) Name() string { | 
|  | 200 | return module.prebuilt.Name(module.ModuleBase.Name()) | 
|  | 201 | } | 
|  | 202 |  | 
|  | 203 | func (module *prebuiltCompatConfigModule) compatConfigMetadata() android.Path { | 
|  | 204 | return module.metadataFile | 
|  | 205 | } | 
|  | 206 |  | 
| Spandan Das | e4c911e | 2024-01-19 00:22:22 +0000 | [diff] [blame] | 207 | func (module *prebuiltCompatConfigModule) BaseModuleName() string { | 
|  | 208 | return proptools.StringDefault(module.properties.Source_module_name, module.ModuleBase.Name()) | 
|  | 209 | } | 
|  | 210 |  | 
| Paul Duffin | 1b29e00 | 2021-03-16 15:06:54 +0000 | [diff] [blame] | 211 | var _ platformCompatConfigMetadataProvider = (*prebuiltCompatConfigModule)(nil) | 
|  | 212 |  | 
|  | 213 | func (module *prebuiltCompatConfigModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { | 
|  | 214 | module.metadataFile = module.prebuilt.SingleSourcePath(ctx) | 
|  | 215 | } | 
|  | 216 |  | 
|  | 217 | // A prebuilt version of platform_compat_config that provides the metadata. | 
|  | 218 | func prebuiltCompatConfigFactory() android.Module { | 
|  | 219 | m := &prebuiltCompatConfigModule{} | 
|  | 220 | m.AddProperties(&m.properties) | 
|  | 221 | android.InitSingleSourcePrebuiltModule(m, &m.properties, "Metadata") | 
| Paul Duffin | 1b29e00 | 2021-03-16 15:06:54 +0000 | [diff] [blame] | 222 | android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon) | 
|  | 223 | return m | 
|  | 224 | } | 
|  | 225 |  | 
| Mathew Inwood | 4d0c19c | 2019-12-09 11:32:29 +0000 | [diff] [blame] | 226 | // compat singleton rules | 
| Paul Duffin | 9615433 | 2021-03-15 18:18:22 +0000 | [diff] [blame] | 227 | type platformCompatConfigSingleton struct { | 
|  | 228 | metadata android.Path | 
|  | 229 | } | 
|  | 230 |  | 
| Mathew Inwood | 4d0c19c | 2019-12-09 11:32:29 +0000 | [diff] [blame] | 231 | func (p *platformCompatConfigSingleton) GenerateBuildActions(ctx android.SingletonContext) { | 
|  | 232 |  | 
|  | 233 | var compatConfigMetadata android.Paths | 
|  | 234 |  | 
|  | 235 | ctx.VisitAllModules(func(module android.Module) { | 
| Paul Duffin | bb9ff51 | 2021-03-24 12:08:53 +0000 | [diff] [blame] | 236 | if !module.Enabled() { | 
|  | 237 | return | 
|  | 238 | } | 
| Paul Duffin | 29072a9 | 2021-03-16 10:12:49 +0000 | [diff] [blame] | 239 | if c, ok := module.(platformCompatConfigMetadataProvider); ok { | 
| Paul Duffin | 458a15b | 2022-11-25 12:18:24 +0000 | [diff] [blame] | 240 | if !android.IsModulePreferred(module) { | 
| Paul Duffin | bb9ff51 | 2021-03-24 12:08:53 +0000 | [diff] [blame] | 241 | return | 
|  | 242 | } | 
| Mathew Inwood | 4d0c19c | 2019-12-09 11:32:29 +0000 | [diff] [blame] | 243 | metadata := c.compatConfigMetadata() | 
|  | 244 | compatConfigMetadata = append(compatConfigMetadata, metadata) | 
|  | 245 | } | 
|  | 246 | }) | 
|  | 247 |  | 
|  | 248 | if compatConfigMetadata == nil { | 
|  | 249 | // nothing to do. | 
|  | 250 | return | 
|  | 251 | } | 
|  | 252 |  | 
| Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 253 | rule := android.NewRuleBuilder(pctx, ctx) | 
| Mathew Inwood | abd49ab | 2019-12-19 14:27:08 +0000 | [diff] [blame] | 254 | outputPath := platformCompatConfigPath(ctx) | 
| Mathew Inwood | 4d0c19c | 2019-12-09 11:32:29 +0000 | [diff] [blame] | 255 |  | 
|  | 256 | rule.Command(). | 
| Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 257 | BuiltTool("process-compat-config"). | 
| Mathew Inwood | 4d0c19c | 2019-12-09 11:32:29 +0000 | [diff] [blame] | 258 | FlagForEachInput("--xml ", compatConfigMetadata). | 
|  | 259 | FlagWithOutput("--merged-config ", outputPath) | 
|  | 260 |  | 
| Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 261 | rule.Build("merged-compat-config", "Merge compat config") | 
| Mathew Inwood | 4d0c19c | 2019-12-09 11:32:29 +0000 | [diff] [blame] | 262 |  | 
|  | 263 | p.metadata = outputPath | 
|  | 264 | } | 
|  | 265 |  | 
| Mathew Inwood | 653c78a | 2019-12-19 12:38:10 +0000 | [diff] [blame] | 266 | func (p *platformCompatConfigSingleton) MakeVars(ctx android.MakeVarsContext) { | 
|  | 267 | if p.metadata != nil { | 
|  | 268 | ctx.Strict("INTERNAL_PLATFORM_MERGED_COMPAT_CONFIG", p.metadata.String()) | 
|  | 269 | } | 
|  | 270 | } | 
|  | 271 |  | 
| Mathew Inwood | 4d0c19c | 2019-12-09 11:32:29 +0000 | [diff] [blame] | 272 | func platformCompatConfigSingletonFactory() android.Singleton { | 
|  | 273 | return &platformCompatConfigSingleton{} | 
|  | 274 | } | 
|  | 275 |  | 
| Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 276 | // ============== merged_compat_config ================= | 
| Mathew Inwood | abd49ab | 2019-12-19 14:27:08 +0000 | [diff] [blame] | 277 | type globalCompatConfigProperties struct { | 
|  | 278 | // name of the file into which the metadata will be copied. | 
|  | 279 | Filename *string | 
|  | 280 | } | 
|  | 281 |  | 
|  | 282 | type globalCompatConfig struct { | 
|  | 283 | android.ModuleBase | 
|  | 284 |  | 
|  | 285 | properties globalCompatConfigProperties | 
|  | 286 |  | 
|  | 287 | outputFilePath android.OutputPath | 
|  | 288 | } | 
|  | 289 |  | 
|  | 290 | func (c *globalCompatConfig) GenerateAndroidBuildActions(ctx android.ModuleContext) { | 
|  | 291 | filename := String(c.properties.Filename) | 
|  | 292 |  | 
|  | 293 | inputPath := platformCompatConfigPath(ctx) | 
|  | 294 | c.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath | 
|  | 295 |  | 
|  | 296 | // This ensures that outputFilePath has the correct name for others to | 
|  | 297 | // use, as the source file may have a different name. | 
|  | 298 | ctx.Build(pctx, android.BuildParams{ | 
|  | 299 | Rule:   android.Cp, | 
|  | 300 | Output: c.outputFilePath, | 
|  | 301 | Input:  inputPath, | 
|  | 302 | }) | 
|  | 303 | } | 
|  | 304 |  | 
|  | 305 | func (h *globalCompatConfig) OutputFiles(tag string) (android.Paths, error) { | 
|  | 306 | switch tag { | 
|  | 307 | case "": | 
|  | 308 | return android.Paths{h.outputFilePath}, nil | 
|  | 309 | default: | 
|  | 310 | return nil, fmt.Errorf("unsupported module reference tag %q", tag) | 
|  | 311 | } | 
|  | 312 | } | 
|  | 313 |  | 
|  | 314 | // global_compat_config provides access to the merged compat config xml file generated by the build. | 
|  | 315 | func globalCompatConfigFactory() android.Module { | 
|  | 316 | module := &globalCompatConfig{} | 
|  | 317 | module.AddProperties(&module.properties) | 
|  | 318 | android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon) | 
|  | 319 | return module | 
|  | 320 | } |