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