blob: e327980cfa778032dc131d199e76832e7909c0a2 [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 Ahnf3eb83e2018-10-12 12:12:10 +090018 "strings"
Sundong Ahne1fe3202018-10-05 18:21:15 +090019
20 "github.com/google/blueprint"
21 "github.com/google/blueprint/proptools"
22
23 "android/soong/android"
24 "android/soong/java"
Sundong Ahnf3eb83e2018-10-12 12:12:10 +090025 "path/filepath"
Sundong Ahne1fe3202018-10-05 18:21:15 +090026)
27
28func init() {
29 pctx.Import("android/soong/java/config")
30 android.RegisterModuleType("xsd_config", xsdConfigFactory)
31
32 android.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
33 ctx.TopDown("xsd_config", xsdConfigMutator).Parallel()
34 })
35}
36
37var (
38 pctx = android.NewPackageContext("android/xsdc")
39
Sundong Ahnf3eb83e2018-10-12 12:12:10 +090040 xsdc = pctx.HostBinToolVariable("xsdcCmd", "xsdc")
41 xsdcJavaRule = pctx.StaticRule("xsdcJavaRule", blueprint.RuleParams{
Sundong Ahne1fe3202018-10-05 18:21:15 +090042 Command: `rm -rf "${out}.temp" && mkdir -p "${out}.temp" && ` +
Sundong Ahnf3eb83e2018-10-12 12:12:10 +090043 `${xsdcCmd} $in -p $pkgName -o ${out}.temp -j && ` +
Sundong Ahne1fe3202018-10-05 18:21:15 +090044 `${config.SoongZipCmd} -jar -o ${out} -C ${out}.temp -D ${out}.temp && ` +
45 `rm -rf ${out}.temp`,
46 Depfile: "${out}.d",
47 Deps: blueprint.DepsGCC,
48 CommandDeps: []string{"${xsdcCmd}", "${config.SoongZipCmd}"},
49 Description: "xsdc Java ${in} => ${out}",
50 }, "pkgName")
Sundong Ahnf3eb83e2018-10-12 12:12:10 +090051
52 xsdcCppRule = pctx.StaticRule("xsdcCppRule", blueprint.RuleParams{
53 Command: `rm -rf "${outDir}" && ` +
54 `${xsdcCmd} $in -p $pkgName -o ${outDir} -c`,
55 Depfile: "${out}.d",
56 Deps: blueprint.DepsGCC,
57 CommandDeps: []string{"${xsdcCmd}", "${config.SoongZipCmd}"},
58 Description: "xsdc Java ${in} => ${out}",
59 }, "pkgName", "outDir")
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
65}
66
67type xsdConfig struct {
68 android.ModuleBase
69
70 properties xsdConfigProperties
71
72 genOutputDir android.Path
Sundong Ahnf3eb83e2018-10-12 12:12:10 +090073 genOutputs_j android.WritablePath
74 genOutputs_c android.WritablePath
75 genOutputs_h android.WritablePath
76
77 docsPath android.Path
Sundong Ahne1fe3202018-10-05 18:21:15 +090078}
79
80type ApiToCheck struct {
81 Api_file *string
82 Removed_api_file *string
83 Args *string
84}
85
86type CheckApi struct {
87 Last_released ApiToCheck
88 Current ApiToCheck
89}
90type DroidstubsProperties struct {
91 Name *string
92 No_framework_libs *bool
93 Installable *bool
94 Srcs []string
95 Args *string
96 Api_filename *string
97 Removed_api_filename *string
98 Check_api CheckApi
99}
100
101func (module *xsdConfig) GeneratedSourceFiles() android.Paths {
Sundong Ahnf3eb83e2018-10-12 12:12:10 +0900102 return android.Paths{module.genOutputs_c}
Sundong Ahne1fe3202018-10-05 18:21:15 +0900103}
104
105func (module *xsdConfig) Srcs() android.Paths {
Sundong Ahnf3eb83e2018-10-12 12:12:10 +0900106 return android.Paths{module.genOutputs_j}
Sundong Ahne1fe3202018-10-05 18:21:15 +0900107}
108
109func (module *xsdConfig) GeneratedDeps() android.Paths {
Sundong Ahnf3eb83e2018-10-12 12:12:10 +0900110 return android.Paths{module.genOutputs_h}
Sundong Ahne1fe3202018-10-05 18:21:15 +0900111}
112
113func (module *xsdConfig) GeneratedHeaderDirs() android.Paths {
114 return android.Paths{module.genOutputDir}
115}
116
117func (module *xsdConfig) DepsMutator(ctx android.BottomUpMutatorContext) {
118 // no need to implement
119}
120
121func (module *xsdConfig) GenerateAndroidBuildActions(ctx android.ModuleContext) {
122 if len(module.properties.Srcs) != 1 {
123 ctx.PropertyErrorf("srcs", "xsd_config must be one src")
124 }
125
Sundong Ahnf3eb83e2018-10-12 12:12:10 +0900126 ctx.VisitDirectDeps(func(to android.Module) {
127 if doc, ok := to.(java.ApiFilePath); ok {
128 module.docsPath = doc.ApiFilePath()
129 }
130 })
131
Sundong Ahne1fe3202018-10-05 18:21:15 +0900132 xsdFile := module.properties.Srcs[0]
133 pkgName := *module.properties.Package_name
134
Sundong Ahnf3eb83e2018-10-12 12:12:10 +0900135 module.genOutputs_j = android.PathForModuleGen(ctx, "xsdcgen.srcjar")
Sundong Ahne1fe3202018-10-05 18:21:15 +0900136
137 ctx.Build(pctx, android.BuildParams{
Sundong Ahnf3eb83e2018-10-12 12:12:10 +0900138 Rule: xsdcJavaRule,
139 Description: "xsdc " + xsdFile,
140 Input: android.PathForModuleSrc(ctx, xsdFile),
141 Implicit: module.docsPath,
142 Output: module.genOutputs_j,
Sundong Ahne1fe3202018-10-05 18:21:15 +0900143 Args: map[string]string{
144 "pkgName": pkgName,
145 },
146 })
Sundong Ahnf3eb83e2018-10-12 12:12:10 +0900147
148 pkgName = strings.Replace(pkgName, ".", "_", -1)
149 module.genOutputs_c = android.PathForModuleGen(ctx, pkgName+".cpp")
150 module.genOutputs_h = android.PathForModuleGen(ctx, "include/"+pkgName+".h")
151 module.genOutputDir = android.PathForModuleGen(ctx, "include")
152
153 ctx.Build(pctx, android.BuildParams{
154 Rule: xsdcCppRule,
155 Description: "xsdc " + xsdFile,
156 Input: android.PathForModuleSrc(ctx, xsdFile),
157 Implicit: module.docsPath,
158 Output: module.genOutputs_c,
159 ImplicitOutput: module.genOutputs_h,
160 Args: map[string]string{
161 "pkgName": pkgName,
162 "outDir": android.PathForModuleGen(ctx, "").String(),
163 },
164 })
Sundong Ahne1fe3202018-10-05 18:21:15 +0900165}
166
167func xsdConfigMutator(mctx android.TopDownMutatorContext) {
168 if module, ok := mctx.Module().(*xsdConfig); ok {
169 name := module.BaseModuleName()
170
171 args := " --stub-packages " + *module.properties.Package_name +
172 " --hide MissingPermission --hide BroadcastBehavior" +
173 " --hide HiddenSuperclass --hide DeprecationMismatch --hide UnavailableSymbol" +
174 " --hide SdkConstant --hide HiddenTypeParameter --hide Todo --hide Typo"
175
176 currentApiFileName := filepath.Join("api", "current.txt")
177 removedApiFileName := filepath.Join("api", "removed.txt")
178
179 check_api := CheckApi{}
180
181 check_api.Current.Api_file = proptools.StringPtr(currentApiFileName)
182 check_api.Current.Removed_api_file = proptools.StringPtr(removedApiFileName)
183
184 check_api.Last_released.Api_file = proptools.StringPtr(
185 filepath.Join("api", "last_current.txt"))
186 check_api.Last_released.Removed_api_file = proptools.StringPtr(
187 filepath.Join("api", "last_removed.txt"))
188
Sundong Ahne1fe3202018-10-05 18:21:15 +0900189 mctx.CreateModule(android.ModuleFactoryAdaptor(java.DroidstubsFactory), &DroidstubsProperties{
Sundong Ahnf3eb83e2018-10-12 12:12:10 +0900190 Name: proptools.StringPtr(name + ".docs"),
Sundong Ahne1fe3202018-10-05 18:21:15 +0900191 Srcs: []string{":" + name},
192 Args: proptools.StringPtr(args),
193 Api_filename: proptools.StringPtr(currentApiFileName),
194 Removed_api_filename: proptools.StringPtr(removedApiFileName),
195 Check_api: check_api,
196 Installable: proptools.BoolPtr(false),
197 No_framework_libs: proptools.BoolPtr(true),
198 })
199 }
200}
201
202func xsdConfigFactory() android.Module {
203 module := &xsdConfig{}
204 module.AddProperties(&module.properties)
205 android.InitAndroidModule(module)
206
207 return module
208}