blob: 0546ec43c9cdb441f78871fb2796fb682af138db [file] [log] [blame]
Sundong Ahne1fe3202018-10-05 18:21:15 +09001// 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
15package xsdc
16
17import (
Sundong Ahn851ddf02018-10-31 21:42:10 +090018 "android/soong/android"
19 "android/soong/java"
20 "path/filepath"
Sundong Ahnf3eb83e2018-10-12 12:12:10 +090021 "strings"
Sundong Ahne1fe3202018-10-05 18:21:15 +090022
23 "github.com/google/blueprint"
24 "github.com/google/blueprint/proptools"
Sundong Ahne1fe3202018-10-05 18:21:15 +090025)
26
27func 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
36var (
37 pctx = android.NewPackageContext("android/xsdc")
38
Sundong Ahnf3eb83e2018-10-12 12:12:10 +090039 xsdc = pctx.HostBinToolVariable("xsdcCmd", "xsdc")
40 xsdcJavaRule = pctx.StaticRule("xsdcJavaRule", blueprint.RuleParams{
Sundong Ahne1fe3202018-10-05 18:21:15 +090041 Command: `rm -rf "${out}.temp" && mkdir -p "${out}.temp" && ` +
Sundong Ahnf3eb83e2018-10-12 12:12:10 +090042 `${xsdcCmd} $in -p $pkgName -o ${out}.temp -j && ` +
Sundong Ahne1fe3202018-10-05 18:21:15 +090043 `${config.SoongZipCmd} -jar -o ${out} -C ${out}.temp -D ${out}.temp && ` +
44 `rm -rf ${out}.temp`,
Sundong Ahne1fe3202018-10-05 18:21:15 +090045 CommandDeps: []string{"${xsdcCmd}", "${config.SoongZipCmd}"},
46 Description: "xsdc Java ${in} => ${out}",
47 }, "pkgName")
Sundong Ahnf3eb83e2018-10-12 12:12:10 +090048
49 xsdcCppRule = pctx.StaticRule("xsdcCppRule", blueprint.RuleParams{
50 Command: `rm -rf "${outDir}" && ` +
51 `${xsdcCmd} $in -p $pkgName -o ${outDir} -c`,
Sundong Ahnf3eb83e2018-10-12 12:12:10 +090052 CommandDeps: []string{"${xsdcCmd}", "${config.SoongZipCmd}"},
Yifan Hong70080b72019-02-01 12:10:06 -080053 Description: "xsdc C++ ${in} => ${out}",
Sundong Ahnf3eb83e2018-10-12 12:12:10 +090054 }, "pkgName", "outDir")
nelsonli84ffd342019-12-18 11:56:41 +080055
56 xsdConfigRule = pctx.StaticRule("xsdConfigRule", blueprint.RuleParams{
57 Command: "cp -f ${in} ${output}",
58 Description: "copy the xsd file: ${in} => ${output}",
59 }, "output")
Sundong Ahne1fe3202018-10-05 18:21:15 +090060)
61
62type xsdConfigProperties struct {
Sundong Ahnf3eb83e2018-10-12 12:12:10 +090063 Srcs []string
Sundong Ahne1fe3202018-10-05 18:21:15 +090064 Package_name *string
Paul Duffinf532ea72019-06-12 14:25:25 +010065 Api_dir *string
Sundong Ahne1fe3202018-10-05 18:21:15 +090066}
67
68type xsdConfig struct {
69 android.ModuleBase
70
71 properties xsdConfigProperties
72
73 genOutputDir android.Path
Sundong Ahnf3eb83e2018-10-12 12:12:10 +090074 genOutputs_j android.WritablePath
75 genOutputs_c android.WritablePath
76 genOutputs_h android.WritablePath
77
78 docsPath android.Path
nelsonli84ffd342019-12-18 11:56:41 +080079
80 xsdConfigPath android.OptionalPath
81 genOutputs android.Paths
Sundong Ahne1fe3202018-10-05 18:21:15 +090082}
83
nelsonli84ffd342019-12-18 11:56:41 +080084var _ android.SourceFileProducer = (*xsdConfig)(nil)
85
Sundong Ahne1fe3202018-10-05 18:21:15 +090086type ApiToCheck struct {
87 Api_file *string
88 Removed_api_file *string
89 Args *string
90}
91
92type CheckApi struct {
93 Last_released ApiToCheck
94 Current ApiToCheck
95}
96type DroidstubsProperties struct {
97 Name *string
Sundong Ahne1fe3202018-10-05 18:21:15 +090098 Installable *bool
99 Srcs []string
Paul Duffinf532ea72019-06-12 14:25:25 +0100100 Sdk_version *string
Sundong Ahne1fe3202018-10-05 18:21:15 +0900101 Args *string
102 Api_filename *string
103 Removed_api_filename *string
104 Check_api CheckApi
105}
106
107func (module *xsdConfig) GeneratedSourceFiles() android.Paths {
Sundong Ahnf3eb83e2018-10-12 12:12:10 +0900108 return android.Paths{module.genOutputs_c}
Sundong Ahne1fe3202018-10-05 18:21:15 +0900109}
110
111func (module *xsdConfig) Srcs() android.Paths {
nelsonli84ffd342019-12-18 11:56:41 +0800112 return append(module.genOutputs, module.genOutputs_j)
Sundong Ahne1fe3202018-10-05 18:21:15 +0900113}
114
115func (module *xsdConfig) GeneratedDeps() android.Paths {
Sundong Ahnf3eb83e2018-10-12 12:12:10 +0900116 return android.Paths{module.genOutputs_h}
Sundong Ahne1fe3202018-10-05 18:21:15 +0900117}
118
119func (module *xsdConfig) GeneratedHeaderDirs() android.Paths {
120 return android.Paths{module.genOutputDir}
121}
122
123func (module *xsdConfig) DepsMutator(ctx android.BottomUpMutatorContext) {
Sundong Ahnec680e12019-02-08 14:40:31 +0900124 android.ExtractSourcesDeps(ctx, module.properties.Srcs)
Sundong Ahne1fe3202018-10-05 18:21:15 +0900125}
126
nelsonli84ffd342019-12-18 11:56:41 +0800127func (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 Ahne1fe3202018-10-05 18:21:15 +0900145func (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 Ahnf3eb83e2018-10-12 12:12:10 +0900150 ctx.VisitDirectDeps(func(to android.Module) {
151 if doc, ok := to.(java.ApiFilePath); ok {
152 module.docsPath = doc.ApiFilePath()
153 }
154 })
155
Sundong Ahnec680e12019-02-08 14:40:31 +0900156 srcFiles := ctx.ExpandSources(module.properties.Srcs, nil)
157 xsdFile := srcFiles[0]
158
Sundong Ahne1fe3202018-10-05 18:21:15 +0900159 pkgName := *module.properties.Package_name
Mikhail Naganov735283a2020-01-15 15:11:53 -0800160 filenameStem := strings.Replace(pkgName, ".", "_", -1)
Sundong Ahne1fe3202018-10-05 18:21:15 +0900161
Mikhail Naganov735283a2020-01-15 15:11:53 -0800162 module.genOutputs_j = android.PathForModuleGen(ctx, "java", filenameStem+"_xsdcgen.srcjar")
Sundong Ahne1fe3202018-10-05 18:21:15 +0900163
164 ctx.Build(pctx, android.BuildParams{
Sundong Ahnf3eb83e2018-10-12 12:12:10 +0900165 Rule: xsdcJavaRule,
Sundong Ahnec680e12019-02-08 14:40:31 +0900166 Description: "xsdc " + xsdFile.String(),
167 Input: xsdFile,
Sundong Ahnf3eb83e2018-10-12 12:12:10 +0900168 Implicit: module.docsPath,
169 Output: module.genOutputs_j,
Sundong Ahne1fe3202018-10-05 18:21:15 +0900170 Args: map[string]string{
171 "pkgName": pkgName,
172 },
173 })
Sundong Ahnf3eb83e2018-10-12 12:12:10 +0900174
Jooyung Han0ec22382019-11-01 17:52:36 +0900175 module.genOutputs_c = android.PathForModuleGen(ctx, "cpp", filenameStem+".cpp")
176 module.genOutputs_h = android.PathForModuleGen(ctx, "cpp", "include/"+filenameStem+".h")
Sundong Ahn851ddf02018-10-31 21:42:10 +0900177 module.genOutputDir = android.PathForModuleGen(ctx, "cpp", "include")
Sundong Ahnf3eb83e2018-10-12 12:12:10 +0900178
179 ctx.Build(pctx, android.BuildParams{
180 Rule: xsdcCppRule,
Sundong Ahnec680e12019-02-08 14:40:31 +0900181 Description: "xsdc " + xsdFile.String(),
182 Input: xsdFile,
Sundong Ahnf3eb83e2018-10-12 12:12:10 +0900183 Implicit: module.docsPath,
184 Output: module.genOutputs_c,
185 ImplicitOutput: module.genOutputs_h,
186 Args: map[string]string{
187 "pkgName": pkgName,
Sundong Ahn851ddf02018-10-31 21:42:10 +0900188 "outDir": android.PathForModuleGen(ctx, "cpp").String(),
Sundong Ahnf3eb83e2018-10-12 12:12:10 +0900189 },
190 })
nelsonli84ffd342019-12-18 11:56:41 +0800191 module.xsdConfigPath = android.ExistentPathForSource(ctx, xsdFile.String())
192 module.generateXsdConfig(ctx)
Sundong Ahne1fe3202018-10-05 18:21:15 +0900193}
194
195func 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 Ahna12a9622019-02-22 18:17:17 +0900204 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 Ahne1fe3202018-10-05 18:21:15 +0900208
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 Ahna12a9622019-02-22 18:17:17 +0900215 filepath.Join(api_dir, "last_current.txt"))
Sundong Ahne1fe3202018-10-05 18:21:15 +0900216 check_api.Last_released.Removed_api_file = proptools.StringPtr(
Sundong Ahna12a9622019-02-22 18:17:17 +0900217 filepath.Join(api_dir, "last_removed.txt"))
Sundong Ahne1fe3202018-10-05 18:21:15 +0900218
Colin Crossc67e1e72019-09-25 15:10:38 -0700219 mctx.CreateModule(java.DroidstubsFactory, &DroidstubsProperties{
Sundong Ahnf3eb83e2018-10-12 12:12:10 +0900220 Name: proptools.StringPtr(name + ".docs"),
Sundong Ahne1fe3202018-10-05 18:21:15 +0900221 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 Duffinf532ea72019-06-12 14:25:25 +0100227 Sdk_version: proptools.StringPtr("core_platform"),
Sundong Ahne1fe3202018-10-05 18:21:15 +0900228 })
229 }
230}
231
232func xsdConfigFactory() android.Module {
233 module := &xsdConfig{}
234 module.AddProperties(&module.properties)
235 android.InitAndroidModule(module)
236
237 return module
238}