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