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 | |
Mathew Inwood | 4d0c19c | 2019-12-09 11:32:29 +0000 | [diff] [blame] | 38 | type platformCompatConfigSingleton struct { |
| 39 | metadata android.Path |
| 40 | } |
| 41 | |
atrost | db25ac0 | 2019-08-05 12:26:07 +0100 | [diff] [blame] | 42 | type platformCompatConfigProperties struct { |
atrost | 87901b0 | 2019-08-29 12:48:43 +0100 | [diff] [blame] | 43 | Src *string `android:"path"` |
atrost | db25ac0 | 2019-08-05 12:26:07 +0100 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | type platformCompatConfig struct { |
| 47 | android.ModuleBase |
| 48 | |
| 49 | properties platformCompatConfigProperties |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 50 | installDirPath android.InstallPath |
atrost | db25ac0 | 2019-08-05 12:26:07 +0100 | [diff] [blame] | 51 | configFile android.OutputPath |
Mathew Inwood | 0dd06f6 | 2019-12-17 11:14:42 +0000 | [diff] [blame] | 52 | metadataFile android.OutputPath |
atrost | db25ac0 | 2019-08-05 12:26:07 +0100 | [diff] [blame] | 53 | } |
| 54 | |
Mathew Inwood | 4d0c19c | 2019-12-09 11:32:29 +0000 | [diff] [blame] | 55 | func (p *platformCompatConfig) compatConfigMetadata() android.OutputPath { |
| 56 | return p.metadataFile |
| 57 | } |
| 58 | |
atrost | 6e12625 | 2020-01-27 17:01:16 +0000 | [diff] [blame] | 59 | func (p *platformCompatConfig) CompatConfig() android.OutputPath { |
| 60 | return p.configFile |
Mathew Inwood | 4d0c19c | 2019-12-09 11:32:29 +0000 | [diff] [blame] | 61 | } |
| 62 | |
atrost | 6e12625 | 2020-01-27 17:01:16 +0000 | [diff] [blame] | 63 | func (p *platformCompatConfig) SubDir() string { |
| 64 | return "compatconfig" |
| 65 | } |
| 66 | |
| 67 | type PlatformCompatConfigIntf interface { |
| 68 | android.Module |
| 69 | |
| 70 | compatConfigMetadata() android.OutputPath |
| 71 | CompatConfig() android.OutputPath |
| 72 | // Sub dir under etc dir. |
| 73 | SubDir() string |
| 74 | } |
| 75 | |
| 76 | var _ PlatformCompatConfigIntf = (*platformCompatConfig)(nil) |
Mathew Inwood | 4d0c19c | 2019-12-09 11:32:29 +0000 | [diff] [blame] | 77 | |
| 78 | // compat singleton rules |
| 79 | func (p *platformCompatConfigSingleton) GenerateBuildActions(ctx android.SingletonContext) { |
| 80 | |
| 81 | var compatConfigMetadata android.Paths |
| 82 | |
| 83 | ctx.VisitAllModules(func(module android.Module) { |
atrost | 6e12625 | 2020-01-27 17:01:16 +0000 | [diff] [blame] | 84 | if c, ok := module.(PlatformCompatConfigIntf); ok { |
Mathew Inwood | 4d0c19c | 2019-12-09 11:32:29 +0000 | [diff] [blame] | 85 | metadata := c.compatConfigMetadata() |
| 86 | compatConfigMetadata = append(compatConfigMetadata, metadata) |
| 87 | } |
| 88 | }) |
| 89 | |
| 90 | if compatConfigMetadata == nil { |
| 91 | // nothing to do. |
| 92 | return |
| 93 | } |
| 94 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 95 | rule := android.NewRuleBuilder(pctx, ctx) |
Mathew Inwood | abd49ab | 2019-12-19 14:27:08 +0000 | [diff] [blame] | 96 | outputPath := platformCompatConfigPath(ctx) |
Mathew Inwood | 4d0c19c | 2019-12-09 11:32:29 +0000 | [diff] [blame] | 97 | |
| 98 | rule.Command(). |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 99 | BuiltTool("process-compat-config"). |
Mathew Inwood | 4d0c19c | 2019-12-09 11:32:29 +0000 | [diff] [blame] | 100 | FlagForEachInput("--xml ", compatConfigMetadata). |
| 101 | FlagWithOutput("--merged-config ", outputPath) |
| 102 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 103 | rule.Build("merged-compat-config", "Merge compat config") |
Mathew Inwood | 4d0c19c | 2019-12-09 11:32:29 +0000 | [diff] [blame] | 104 | |
| 105 | p.metadata = outputPath |
| 106 | } |
| 107 | |
Mathew Inwood | 653c78a | 2019-12-19 12:38:10 +0000 | [diff] [blame] | 108 | func (p *platformCompatConfigSingleton) MakeVars(ctx android.MakeVarsContext) { |
| 109 | if p.metadata != nil { |
| 110 | ctx.Strict("INTERNAL_PLATFORM_MERGED_COMPAT_CONFIG", p.metadata.String()) |
| 111 | } |
| 112 | } |
| 113 | |
atrost | db25ac0 | 2019-08-05 12:26:07 +0100 | [diff] [blame] | 114 | func (p *platformCompatConfig) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 115 | rule := android.NewRuleBuilder(pctx, ctx) |
atrost | db25ac0 | 2019-08-05 12:26:07 +0100 | [diff] [blame] | 116 | |
atrost | 87901b0 | 2019-08-29 12:48:43 +0100 | [diff] [blame] | 117 | configFileName := p.Name() + ".xml" |
Mathew Inwood | 0dd06f6 | 2019-12-17 11:14:42 +0000 | [diff] [blame] | 118 | metadataFileName := p.Name() + "_meta.xml" |
atrost | db25ac0 | 2019-08-05 12:26:07 +0100 | [diff] [blame] | 119 | p.configFile = android.PathForModuleOut(ctx, configFileName).OutputPath |
Mathew Inwood | 0dd06f6 | 2019-12-17 11:14:42 +0000 | [diff] [blame] | 120 | p.metadataFile = android.PathForModuleOut(ctx, metadataFileName).OutputPath |
atrost | db25ac0 | 2019-08-05 12:26:07 +0100 | [diff] [blame] | 121 | path := android.PathForModuleSrc(ctx, String(p.properties.Src)) |
| 122 | |
atrost | db25ac0 | 2019-08-05 12:26:07 +0100 | [diff] [blame] | 123 | rule.Command(). |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 124 | BuiltTool("process-compat-config"). |
Mathew Inwood | 0dd06f6 | 2019-12-17 11:14:42 +0000 | [diff] [blame] | 125 | FlagWithInput("--jar ", path). |
| 126 | FlagWithOutput("--device-config ", p.configFile). |
| 127 | FlagWithOutput("--merged-config ", p.metadataFile) |
atrost | db25ac0 | 2019-08-05 12:26:07 +0100 | [diff] [blame] | 128 | |
atrost | 87901b0 | 2019-08-29 12:48:43 +0100 | [diff] [blame] | 129 | p.installDirPath = android.PathForModuleInstall(ctx, "etc", "compatconfig") |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 130 | rule.Build(configFileName, "Extract compat/compat_config.xml and install it") |
atrost | db25ac0 | 2019-08-05 12:26:07 +0100 | [diff] [blame] | 131 | |
| 132 | } |
| 133 | |
Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 134 | func (p *platformCompatConfig) AndroidMkEntries() []android.AndroidMkEntries { |
| 135 | return []android.AndroidMkEntries{android.AndroidMkEntries{ |
atrost | db25ac0 | 2019-08-05 12:26:07 +0100 | [diff] [blame] | 136 | Class: "ETC", |
| 137 | OutputFile: android.OptionalPathForPath(p.configFile), |
| 138 | Include: "$(BUILD_PREBUILT)", |
Jaewoong Jung | e0dc8df | 2019-08-27 17:33:16 -0700 | [diff] [blame] | 139 | ExtraEntries: []android.AndroidMkExtraEntriesFunc{ |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 140 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { |
Colin Cross | ff6c33d | 2019-10-02 16:01:35 -0700 | [diff] [blame] | 141 | entries.SetString("LOCAL_MODULE_PATH", p.installDirPath.ToMakePath().String()) |
Jaewoong Jung | e0dc8df | 2019-08-27 17:33:16 -0700 | [diff] [blame] | 142 | entries.SetString("LOCAL_INSTALLED_MODULE_STEM", p.configFile.Base()) |
| 143 | }, |
atrost | db25ac0 | 2019-08-05 12:26:07 +0100 | [diff] [blame] | 144 | }, |
Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 145 | }} |
atrost | db25ac0 | 2019-08-05 12:26:07 +0100 | [diff] [blame] | 146 | } |
| 147 | |
Mathew Inwood | 4d0c19c | 2019-12-09 11:32:29 +0000 | [diff] [blame] | 148 | func platformCompatConfigSingletonFactory() android.Singleton { |
| 149 | return &platformCompatConfigSingleton{} |
| 150 | } |
| 151 | |
atrost | 6e12625 | 2020-01-27 17:01:16 +0000 | [diff] [blame] | 152 | func PlatformCompatConfigFactory() android.Module { |
atrost | db25ac0 | 2019-08-05 12:26:07 +0100 | [diff] [blame] | 153 | module := &platformCompatConfig{} |
| 154 | module.AddProperties(&module.properties) |
| 155 | android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst) |
| 156 | return module |
| 157 | } |
Mathew Inwood | abd49ab | 2019-12-19 14:27:08 +0000 | [diff] [blame] | 158 | |
| 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 | } |