Sundong Ahn | e1fe320 | 2018-10-05 18:21:15 +0900 | [diff] [blame] | 1 | // Copyright 2018 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 xsdc |
| 16 | |
| 17 | import ( |
Sundong Ahn | 851ddf0 | 2018-10-31 21:42:10 +0900 | [diff] [blame] | 18 | "android/soong/android" |
| 19 | "android/soong/java" |
| 20 | "path/filepath" |
Sundong Ahn | f3eb83e | 2018-10-12 12:12:10 +0900 | [diff] [blame] | 21 | "strings" |
Sundong Ahn | e1fe320 | 2018-10-05 18:21:15 +0900 | [diff] [blame] | 22 | |
| 23 | "github.com/google/blueprint" |
| 24 | "github.com/google/blueprint/proptools" |
Sundong Ahn | e1fe320 | 2018-10-05 18:21:15 +0900 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | func init() { |
| 28 | pctx.Import("android/soong/java/config") |
| 29 | android.RegisterModuleType("xsd_config", xsdConfigFactory) |
| 30 | |
| 31 | android.PreArchMutators(func(ctx android.RegisterMutatorsContext) { |
| 32 | ctx.TopDown("xsd_config", xsdConfigMutator).Parallel() |
| 33 | }) |
| 34 | } |
| 35 | |
| 36 | var ( |
| 37 | pctx = android.NewPackageContext("android/xsdc") |
| 38 | |
Sundong Ahn | f3eb83e | 2018-10-12 12:12:10 +0900 | [diff] [blame] | 39 | xsdc = pctx.HostBinToolVariable("xsdcCmd", "xsdc") |
| 40 | xsdcJavaRule = pctx.StaticRule("xsdcJavaRule", blueprint.RuleParams{ |
Sundong Ahn | e1fe320 | 2018-10-05 18:21:15 +0900 | [diff] [blame] | 41 | Command: `rm -rf "${out}.temp" && mkdir -p "${out}.temp" && ` + |
Sundong Ahn | f3eb83e | 2018-10-12 12:12:10 +0900 | [diff] [blame] | 42 | `${xsdcCmd} $in -p $pkgName -o ${out}.temp -j && ` + |
Sundong Ahn | e1fe320 | 2018-10-05 18:21:15 +0900 | [diff] [blame] | 43 | `${config.SoongZipCmd} -jar -o ${out} -C ${out}.temp -D ${out}.temp && ` + |
| 44 | `rm -rf ${out}.temp`, |
Sundong Ahn | e1fe320 | 2018-10-05 18:21:15 +0900 | [diff] [blame] | 45 | CommandDeps: []string{"${xsdcCmd}", "${config.SoongZipCmd}"}, |
| 46 | Description: "xsdc Java ${in} => ${out}", |
| 47 | }, "pkgName") |
Sundong Ahn | f3eb83e | 2018-10-12 12:12:10 +0900 | [diff] [blame] | 48 | |
| 49 | xsdcCppRule = pctx.StaticRule("xsdcCppRule", blueprint.RuleParams{ |
| 50 | Command: `rm -rf "${outDir}" && ` + |
| 51 | `${xsdcCmd} $in -p $pkgName -o ${outDir} -c`, |
Sundong Ahn | f3eb83e | 2018-10-12 12:12:10 +0900 | [diff] [blame] | 52 | CommandDeps: []string{"${xsdcCmd}", "${config.SoongZipCmd}"}, |
Yifan Hong | 70080b7 | 2019-02-01 12:10:06 -0800 | [diff] [blame] | 53 | Description: "xsdc C++ ${in} => ${out}", |
Sundong Ahn | f3eb83e | 2018-10-12 12:12:10 +0900 | [diff] [blame] | 54 | }, "pkgName", "outDir") |
nelsonli | 84ffd34 | 2019-12-18 11:56:41 +0800 | [diff] [blame^] | 55 | |
| 56 | xsdConfigRule = pctx.StaticRule("xsdConfigRule", blueprint.RuleParams{ |
| 57 | Command: "cp -f ${in} ${output}", |
| 58 | Description: "copy the xsd file: ${in} => ${output}", |
| 59 | }, "output") |
Sundong Ahn | e1fe320 | 2018-10-05 18:21:15 +0900 | [diff] [blame] | 60 | ) |
| 61 | |
| 62 | type xsdConfigProperties struct { |
Sundong Ahn | f3eb83e | 2018-10-12 12:12:10 +0900 | [diff] [blame] | 63 | Srcs []string |
Sundong Ahn | e1fe320 | 2018-10-05 18:21:15 +0900 | [diff] [blame] | 64 | Package_name *string |
Paul Duffin | f532ea7 | 2019-06-12 14:25:25 +0100 | [diff] [blame] | 65 | Api_dir *string |
Sundong Ahn | e1fe320 | 2018-10-05 18:21:15 +0900 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | type xsdConfig struct { |
| 69 | android.ModuleBase |
| 70 | |
| 71 | properties xsdConfigProperties |
| 72 | |
| 73 | genOutputDir android.Path |
Sundong Ahn | f3eb83e | 2018-10-12 12:12:10 +0900 | [diff] [blame] | 74 | genOutputs_j android.WritablePath |
| 75 | genOutputs_c android.WritablePath |
| 76 | genOutputs_h android.WritablePath |
| 77 | |
| 78 | docsPath android.Path |
nelsonli | 84ffd34 | 2019-12-18 11:56:41 +0800 | [diff] [blame^] | 79 | |
| 80 | xsdConfigPath android.OptionalPath |
| 81 | genOutputs android.Paths |
Sundong Ahn | e1fe320 | 2018-10-05 18:21:15 +0900 | [diff] [blame] | 82 | } |
| 83 | |
nelsonli | 84ffd34 | 2019-12-18 11:56:41 +0800 | [diff] [blame^] | 84 | var _ android.SourceFileProducer = (*xsdConfig)(nil) |
| 85 | |
Sundong Ahn | e1fe320 | 2018-10-05 18:21:15 +0900 | [diff] [blame] | 86 | type ApiToCheck struct { |
| 87 | Api_file *string |
| 88 | Removed_api_file *string |
| 89 | Args *string |
| 90 | } |
| 91 | |
| 92 | type CheckApi struct { |
| 93 | Last_released ApiToCheck |
| 94 | Current ApiToCheck |
| 95 | } |
| 96 | type DroidstubsProperties struct { |
| 97 | Name *string |
Sundong Ahn | e1fe320 | 2018-10-05 18:21:15 +0900 | [diff] [blame] | 98 | Installable *bool |
| 99 | Srcs []string |
Paul Duffin | f532ea7 | 2019-06-12 14:25:25 +0100 | [diff] [blame] | 100 | Sdk_version *string |
Sundong Ahn | e1fe320 | 2018-10-05 18:21:15 +0900 | [diff] [blame] | 101 | Args *string |
| 102 | Api_filename *string |
| 103 | Removed_api_filename *string |
| 104 | Check_api CheckApi |
| 105 | } |
| 106 | |
| 107 | func (module *xsdConfig) GeneratedSourceFiles() android.Paths { |
Sundong Ahn | f3eb83e | 2018-10-12 12:12:10 +0900 | [diff] [blame] | 108 | return android.Paths{module.genOutputs_c} |
Sundong Ahn | e1fe320 | 2018-10-05 18:21:15 +0900 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | func (module *xsdConfig) Srcs() android.Paths { |
nelsonli | 84ffd34 | 2019-12-18 11:56:41 +0800 | [diff] [blame^] | 112 | return append(module.genOutputs, module.genOutputs_j) |
Sundong Ahn | e1fe320 | 2018-10-05 18:21:15 +0900 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | func (module *xsdConfig) GeneratedDeps() android.Paths { |
Sundong Ahn | f3eb83e | 2018-10-12 12:12:10 +0900 | [diff] [blame] | 116 | return android.Paths{module.genOutputs_h} |
Sundong Ahn | e1fe320 | 2018-10-05 18:21:15 +0900 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | func (module *xsdConfig) GeneratedHeaderDirs() android.Paths { |
| 120 | return android.Paths{module.genOutputDir} |
| 121 | } |
| 122 | |
| 123 | func (module *xsdConfig) DepsMutator(ctx android.BottomUpMutatorContext) { |
Sundong Ahn | ec680e1 | 2019-02-08 14:40:31 +0900 | [diff] [blame] | 124 | android.ExtractSourcesDeps(ctx, module.properties.Srcs) |
Sundong Ahn | e1fe320 | 2018-10-05 18:21:15 +0900 | [diff] [blame] | 125 | } |
| 126 | |
nelsonli | 84ffd34 | 2019-12-18 11:56:41 +0800 | [diff] [blame^] | 127 | func (module *xsdConfig) generateXsdConfig(ctx android.ModuleContext) { |
| 128 | if !module.xsdConfigPath.Valid() { |
| 129 | return |
| 130 | } |
| 131 | |
| 132 | output := android.PathForModuleGen(ctx, module.Name()+".xsd") |
| 133 | module.genOutputs = append(module.genOutputs, output) |
| 134 | |
| 135 | ctx.ModuleBuild(pctx, android.ModuleBuildParams{ |
| 136 | Rule: xsdConfigRule, |
| 137 | Input: module.xsdConfigPath.Path(), |
| 138 | Output: output, |
| 139 | Args: map[string]string{ |
| 140 | "output": output.String(), |
| 141 | }, |
| 142 | }) |
| 143 | } |
| 144 | |
Sundong Ahn | e1fe320 | 2018-10-05 18:21:15 +0900 | [diff] [blame] | 145 | func (module *xsdConfig) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 146 | if len(module.properties.Srcs) != 1 { |
| 147 | ctx.PropertyErrorf("srcs", "xsd_config must be one src") |
| 148 | } |
| 149 | |
Sundong Ahn | f3eb83e | 2018-10-12 12:12:10 +0900 | [diff] [blame] | 150 | ctx.VisitDirectDeps(func(to android.Module) { |
| 151 | if doc, ok := to.(java.ApiFilePath); ok { |
| 152 | module.docsPath = doc.ApiFilePath() |
| 153 | } |
| 154 | }) |
| 155 | |
Sundong Ahn | ec680e1 | 2019-02-08 14:40:31 +0900 | [diff] [blame] | 156 | srcFiles := ctx.ExpandSources(module.properties.Srcs, nil) |
| 157 | xsdFile := srcFiles[0] |
| 158 | |
Sundong Ahn | e1fe320 | 2018-10-05 18:21:15 +0900 | [diff] [blame] | 159 | pkgName := *module.properties.Package_name |
| 160 | |
Sundong Ahn | 851ddf0 | 2018-10-31 21:42:10 +0900 | [diff] [blame] | 161 | module.genOutputs_j = android.PathForModuleGen(ctx, "java", "xsdcgen.srcjar") |
Sundong Ahn | e1fe320 | 2018-10-05 18:21:15 +0900 | [diff] [blame] | 162 | |
| 163 | ctx.Build(pctx, android.BuildParams{ |
Sundong Ahn | f3eb83e | 2018-10-12 12:12:10 +0900 | [diff] [blame] | 164 | Rule: xsdcJavaRule, |
Sundong Ahn | ec680e1 | 2019-02-08 14:40:31 +0900 | [diff] [blame] | 165 | Description: "xsdc " + xsdFile.String(), |
| 166 | Input: xsdFile, |
Sundong Ahn | f3eb83e | 2018-10-12 12:12:10 +0900 | [diff] [blame] | 167 | Implicit: module.docsPath, |
| 168 | Output: module.genOutputs_j, |
Sundong Ahn | e1fe320 | 2018-10-05 18:21:15 +0900 | [diff] [blame] | 169 | Args: map[string]string{ |
| 170 | "pkgName": pkgName, |
| 171 | }, |
| 172 | }) |
Sundong Ahn | f3eb83e | 2018-10-12 12:12:10 +0900 | [diff] [blame] | 173 | |
Jooyung Han | 0ec2238 | 2019-11-01 17:52:36 +0900 | [diff] [blame] | 174 | filenameStem := strings.Replace(pkgName, ".", "_", -1) |
| 175 | module.genOutputs_c = android.PathForModuleGen(ctx, "cpp", filenameStem+".cpp") |
| 176 | module.genOutputs_h = android.PathForModuleGen(ctx, "cpp", "include/"+filenameStem+".h") |
Sundong Ahn | 851ddf0 | 2018-10-31 21:42:10 +0900 | [diff] [blame] | 177 | module.genOutputDir = android.PathForModuleGen(ctx, "cpp", "include") |
Sundong Ahn | f3eb83e | 2018-10-12 12:12:10 +0900 | [diff] [blame] | 178 | |
| 179 | ctx.Build(pctx, android.BuildParams{ |
| 180 | Rule: xsdcCppRule, |
Sundong Ahn | ec680e1 | 2019-02-08 14:40:31 +0900 | [diff] [blame] | 181 | Description: "xsdc " + xsdFile.String(), |
| 182 | Input: xsdFile, |
Sundong Ahn | f3eb83e | 2018-10-12 12:12:10 +0900 | [diff] [blame] | 183 | Implicit: module.docsPath, |
| 184 | Output: module.genOutputs_c, |
| 185 | ImplicitOutput: module.genOutputs_h, |
| 186 | Args: map[string]string{ |
| 187 | "pkgName": pkgName, |
Sundong Ahn | 851ddf0 | 2018-10-31 21:42:10 +0900 | [diff] [blame] | 188 | "outDir": android.PathForModuleGen(ctx, "cpp").String(), |
Sundong Ahn | f3eb83e | 2018-10-12 12:12:10 +0900 | [diff] [blame] | 189 | }, |
| 190 | }) |
nelsonli | 84ffd34 | 2019-12-18 11:56:41 +0800 | [diff] [blame^] | 191 | module.xsdConfigPath = android.ExistentPathForSource(ctx, xsdFile.String()) |
| 192 | module.generateXsdConfig(ctx) |
Sundong Ahn | e1fe320 | 2018-10-05 18:21:15 +0900 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | func xsdConfigMutator(mctx android.TopDownMutatorContext) { |
| 196 | if module, ok := mctx.Module().(*xsdConfig); ok { |
| 197 | name := module.BaseModuleName() |
| 198 | |
| 199 | args := " --stub-packages " + *module.properties.Package_name + |
| 200 | " --hide MissingPermission --hide BroadcastBehavior" + |
| 201 | " --hide HiddenSuperclass --hide DeprecationMismatch --hide UnavailableSymbol" + |
| 202 | " --hide SdkConstant --hide HiddenTypeParameter --hide Todo --hide Typo" |
| 203 | |
Sundong Ahn | a12a962 | 2019-02-22 18:17:17 +0900 | [diff] [blame] | 204 | api_dir := proptools.StringDefault(module.properties.Api_dir, "api") |
| 205 | |
| 206 | currentApiFileName := filepath.Join(api_dir, "current.txt") |
| 207 | removedApiFileName := filepath.Join(api_dir, "removed.txt") |
Sundong Ahn | e1fe320 | 2018-10-05 18:21:15 +0900 | [diff] [blame] | 208 | |
| 209 | check_api := CheckApi{} |
| 210 | |
| 211 | check_api.Current.Api_file = proptools.StringPtr(currentApiFileName) |
| 212 | check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName) |
| 213 | |
| 214 | check_api.Last_released.Api_file = proptools.StringPtr( |
Sundong Ahn | a12a962 | 2019-02-22 18:17:17 +0900 | [diff] [blame] | 215 | filepath.Join(api_dir, "last_current.txt")) |
Sundong Ahn | e1fe320 | 2018-10-05 18:21:15 +0900 | [diff] [blame] | 216 | check_api.Last_released.Removed_api_file = proptools.StringPtr( |
Sundong Ahn | a12a962 | 2019-02-22 18:17:17 +0900 | [diff] [blame] | 217 | filepath.Join(api_dir, "last_removed.txt")) |
Sundong Ahn | e1fe320 | 2018-10-05 18:21:15 +0900 | [diff] [blame] | 218 | |
Colin Cross | c67e1e7 | 2019-09-25 15:10:38 -0700 | [diff] [blame] | 219 | mctx.CreateModule(java.DroidstubsFactory, &DroidstubsProperties{ |
Sundong Ahn | f3eb83e | 2018-10-12 12:12:10 +0900 | [diff] [blame] | 220 | Name: proptools.StringPtr(name + ".docs"), |
Sundong Ahn | e1fe320 | 2018-10-05 18:21:15 +0900 | [diff] [blame] | 221 | Srcs: []string{":" + name}, |
| 222 | Args: proptools.StringPtr(args), |
| 223 | Api_filename: proptools.StringPtr(currentApiFileName), |
| 224 | Removed_api_filename: proptools.StringPtr(removedApiFileName), |
| 225 | Check_api: check_api, |
| 226 | Installable: proptools.BoolPtr(false), |
Paul Duffin | f532ea7 | 2019-06-12 14:25:25 +0100 | [diff] [blame] | 227 | Sdk_version: proptools.StringPtr("core_platform"), |
Sundong Ahn | e1fe320 | 2018-10-05 18:21:15 +0900 | [diff] [blame] | 228 | }) |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | func xsdConfigFactory() android.Module { |
| 233 | module := &xsdConfig{} |
| 234 | module.AddProperties(&module.properties) |
| 235 | android.InitAndroidModule(module) |
| 236 | |
| 237 | return module |
| 238 | } |