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