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