blob: 3c13bfda7df5c212e887d4378b177bd89dde261e [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")
Sundong Ahne1fe3202018-10-05 18:21:15 +090055)
56
57type xsdConfigProperties struct {
Sundong Ahnf3eb83e2018-10-12 12:12:10 +090058 Srcs []string
Sundong Ahne1fe3202018-10-05 18:21:15 +090059 Package_name *string
60}
61
62type xsdConfig struct {
63 android.ModuleBase
64
65 properties xsdConfigProperties
66
67 genOutputDir android.Path
Sundong Ahnf3eb83e2018-10-12 12:12:10 +090068 genOutputs_j android.WritablePath
69 genOutputs_c android.WritablePath
70 genOutputs_h android.WritablePath
71
72 docsPath android.Path
Sundong Ahne1fe3202018-10-05 18:21:15 +090073}
74
75type ApiToCheck struct {
76 Api_file *string
77 Removed_api_file *string
78 Args *string
79}
80
81type CheckApi struct {
82 Last_released ApiToCheck
83 Current ApiToCheck
84}
85type DroidstubsProperties struct {
86 Name *string
87 No_framework_libs *bool
88 Installable *bool
89 Srcs []string
90 Args *string
91 Api_filename *string
92 Removed_api_filename *string
93 Check_api CheckApi
94}
95
96func (module *xsdConfig) GeneratedSourceFiles() android.Paths {
Sundong Ahnf3eb83e2018-10-12 12:12:10 +090097 return android.Paths{module.genOutputs_c}
Sundong Ahne1fe3202018-10-05 18:21:15 +090098}
99
100func (module *xsdConfig) Srcs() android.Paths {
Sundong Ahnf3eb83e2018-10-12 12:12:10 +0900101 return android.Paths{module.genOutputs_j}
Sundong Ahne1fe3202018-10-05 18:21:15 +0900102}
103
104func (module *xsdConfig) GeneratedDeps() android.Paths {
Sundong Ahnf3eb83e2018-10-12 12:12:10 +0900105 return android.Paths{module.genOutputs_h}
Sundong Ahne1fe3202018-10-05 18:21:15 +0900106}
107
108func (module *xsdConfig) GeneratedHeaderDirs() android.Paths {
109 return android.Paths{module.genOutputDir}
110}
111
112func (module *xsdConfig) DepsMutator(ctx android.BottomUpMutatorContext) {
Sundong Ahnec680e12019-02-08 14:40:31 +0900113 android.ExtractSourcesDeps(ctx, module.properties.Srcs)
Sundong Ahne1fe3202018-10-05 18:21:15 +0900114}
115
116func (module *xsdConfig) GenerateAndroidBuildActions(ctx android.ModuleContext) {
117 if len(module.properties.Srcs) != 1 {
118 ctx.PropertyErrorf("srcs", "xsd_config must be one src")
119 }
120
Sundong Ahnf3eb83e2018-10-12 12:12:10 +0900121 ctx.VisitDirectDeps(func(to android.Module) {
122 if doc, ok := to.(java.ApiFilePath); ok {
123 module.docsPath = doc.ApiFilePath()
124 }
125 })
126
Sundong Ahnec680e12019-02-08 14:40:31 +0900127 srcFiles := ctx.ExpandSources(module.properties.Srcs, nil)
128 xsdFile := srcFiles[0]
129
Sundong Ahne1fe3202018-10-05 18:21:15 +0900130 pkgName := *module.properties.Package_name
131
Sundong Ahn851ddf02018-10-31 21:42:10 +0900132 module.genOutputs_j = android.PathForModuleGen(ctx, "java", "xsdcgen.srcjar")
Sundong Ahne1fe3202018-10-05 18:21:15 +0900133
134 ctx.Build(pctx, android.BuildParams{
Sundong Ahnf3eb83e2018-10-12 12:12:10 +0900135 Rule: xsdcJavaRule,
Sundong Ahnec680e12019-02-08 14:40:31 +0900136 Description: "xsdc " + xsdFile.String(),
137 Input: xsdFile,
Sundong Ahnf3eb83e2018-10-12 12:12:10 +0900138 Implicit: module.docsPath,
139 Output: module.genOutputs_j,
Sundong Ahne1fe3202018-10-05 18:21:15 +0900140 Args: map[string]string{
141 "pkgName": pkgName,
142 },
143 })
Sundong Ahnf3eb83e2018-10-12 12:12:10 +0900144
145 pkgName = strings.Replace(pkgName, ".", "_", -1)
Sundong Ahn851ddf02018-10-31 21:42:10 +0900146 module.genOutputs_c = android.PathForModuleGen(ctx, "cpp", pkgName+".cpp")
147 module.genOutputs_h = android.PathForModuleGen(ctx, "cpp", "include/"+pkgName+".h")
148 module.genOutputDir = android.PathForModuleGen(ctx, "cpp", "include")
Sundong Ahnf3eb83e2018-10-12 12:12:10 +0900149
150 ctx.Build(pctx, android.BuildParams{
151 Rule: xsdcCppRule,
Sundong Ahnec680e12019-02-08 14:40:31 +0900152 Description: "xsdc " + xsdFile.String(),
153 Input: xsdFile,
Sundong Ahnf3eb83e2018-10-12 12:12:10 +0900154 Implicit: module.docsPath,
155 Output: module.genOutputs_c,
156 ImplicitOutput: module.genOutputs_h,
157 Args: map[string]string{
158 "pkgName": pkgName,
Sundong Ahn851ddf02018-10-31 21:42:10 +0900159 "outDir": android.PathForModuleGen(ctx, "cpp").String(),
Sundong Ahnf3eb83e2018-10-12 12:12:10 +0900160 },
161 })
Sundong Ahne1fe3202018-10-05 18:21:15 +0900162}
163
164func xsdConfigMutator(mctx android.TopDownMutatorContext) {
165 if module, ok := mctx.Module().(*xsdConfig); ok {
166 name := module.BaseModuleName()
167
168 args := " --stub-packages " + *module.properties.Package_name +
169 " --hide MissingPermission --hide BroadcastBehavior" +
170 " --hide HiddenSuperclass --hide DeprecationMismatch --hide UnavailableSymbol" +
171 " --hide SdkConstant --hide HiddenTypeParameter --hide Todo --hide Typo"
172
173 currentApiFileName := filepath.Join("api", "current.txt")
174 removedApiFileName := filepath.Join("api", "removed.txt")
175
176 check_api := CheckApi{}
177
178 check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
179 check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
180
181 check_api.Last_released.Api_file = proptools.StringPtr(
182 filepath.Join("api", "last_current.txt"))
183 check_api.Last_released.Removed_api_file = proptools.StringPtr(
184 filepath.Join("api", "last_removed.txt"))
185
Sundong Ahne1fe3202018-10-05 18:21:15 +0900186 mctx.CreateModule(android.ModuleFactoryAdaptor(java.DroidstubsFactory), &DroidstubsProperties{
Sundong Ahnf3eb83e2018-10-12 12:12:10 +0900187 Name: proptools.StringPtr(name + ".docs"),
Sundong Ahne1fe3202018-10-05 18:21:15 +0900188 Srcs: []string{":" + name},
189 Args: proptools.StringPtr(args),
190 Api_filename: proptools.StringPtr(currentApiFileName),
191 Removed_api_filename: proptools.StringPtr(removedApiFileName),
192 Check_api: check_api,
193 Installable: proptools.BoolPtr(false),
194 No_framework_libs: proptools.BoolPtr(true),
195 })
196 }
197}
198
199func xsdConfigFactory() android.Module {
200 module := &xsdConfig{}
201 module.AddProperties(&module.properties)
202 android.InitAndroidModule(module)
203
204 return module
205}