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