• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
15package xsdc
16
17import (
18	"android/soong/android"
19	"android/soong/java"
20	"path/filepath"
21	"strings"
22
23	"github.com/google/blueprint"
24	"github.com/google/blueprint/proptools"
25)
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
39	xsdc         = pctx.HostBinToolVariable("xsdcCmd", "xsdc")
40	xsdcJavaRule = pctx.StaticRule("xsdcJavaRule", blueprint.RuleParams{
41		Command: `rm -rf "${out}.temp" && mkdir -p "${out}.temp" && ` +
42			`${xsdcCmd} $in -p $pkgName -o ${out}.temp -j && ` +
43			`${config.SoongZipCmd} -jar -o ${out} -C ${out}.temp -D ${out}.temp && ` +
44			`rm -rf ${out}.temp`,
45		CommandDeps: []string{"${xsdcCmd}", "${config.SoongZipCmd}"},
46		Description: "xsdc Java ${in} => ${out}",
47	}, "pkgName")
48
49	xsdcCppRule = pctx.StaticRule("xsdcCppRule", blueprint.RuleParams{
50		Command: `rm -rf "${outDir}" && ` +
51			`${xsdcCmd} $in -p $pkgName -o ${outDir} -c`,
52		CommandDeps: []string{"${xsdcCmd}", "${config.SoongZipCmd}"},
53		Description: "xsdc C++ ${in} => ${out}",
54	}, "pkgName", "outDir")
55)
56
57type xsdConfigProperties struct {
58	Srcs         []string
59	Package_name *string
60	Api_dir *string
61}
62
63type xsdConfig struct {
64	android.ModuleBase
65
66	properties xsdConfigProperties
67
68	genOutputDir android.Path
69	genOutputs_j android.WritablePath
70	genOutputs_c android.WritablePath
71	genOutputs_h android.WritablePath
72
73	docsPath android.Path
74}
75
76type ApiToCheck struct {
77	Api_file         *string
78	Removed_api_file *string
79	Args             *string
80}
81
82type CheckApi struct {
83	Last_released ApiToCheck
84	Current       ApiToCheck
85}
86type DroidstubsProperties struct {
87	Name                 *string
88	No_framework_libs    *bool
89	Installable          *bool
90	Srcs                 []string
91	Args                 *string
92	Api_filename         *string
93	Removed_api_filename *string
94	Check_api            CheckApi
95}
96
97func (module *xsdConfig) GeneratedSourceFiles() android.Paths {
98	return android.Paths{module.genOutputs_c}
99}
100
101func (module *xsdConfig) Srcs() android.Paths {
102	return android.Paths{module.genOutputs_j}
103}
104
105func (module *xsdConfig) GeneratedDeps() android.Paths {
106	return android.Paths{module.genOutputs_h}
107}
108
109func (module *xsdConfig) GeneratedHeaderDirs() android.Paths {
110	return android.Paths{module.genOutputDir}
111}
112
113func (module *xsdConfig) DepsMutator(ctx android.BottomUpMutatorContext) {
114	android.ExtractSourcesDeps(ctx, module.properties.Srcs)
115}
116
117func (module *xsdConfig) GenerateAndroidBuildActions(ctx android.ModuleContext) {
118	if len(module.properties.Srcs) != 1 {
119		ctx.PropertyErrorf("srcs", "xsd_config must be one src")
120	}
121
122	ctx.VisitDirectDeps(func(to android.Module) {
123		if doc, ok := to.(java.ApiFilePath); ok {
124			module.docsPath = doc.ApiFilePath()
125		}
126	})
127
128	srcFiles := ctx.ExpandSources(module.properties.Srcs, nil)
129	xsdFile := srcFiles[0]
130
131	pkgName := *module.properties.Package_name
132
133	module.genOutputs_j = android.PathForModuleGen(ctx, "java", "xsdcgen.srcjar")
134
135	ctx.Build(pctx, android.BuildParams{
136		Rule:        xsdcJavaRule,
137		Description: "xsdc " + xsdFile.String(),
138		Input:       xsdFile,
139		Implicit:    module.docsPath,
140		Output:      module.genOutputs_j,
141		Args: map[string]string{
142			"pkgName": pkgName,
143		},
144	})
145
146	pkgName = strings.Replace(pkgName, ".", "_", -1)
147	module.genOutputs_c = android.PathForModuleGen(ctx, "cpp", pkgName+".cpp")
148	module.genOutputs_h = android.PathForModuleGen(ctx, "cpp", "include/"+pkgName+".h")
149	module.genOutputDir = android.PathForModuleGen(ctx, "cpp", "include")
150
151	ctx.Build(pctx, android.BuildParams{
152		Rule:           xsdcCppRule,
153		Description:    "xsdc " + xsdFile.String(),
154		Input:          xsdFile,
155		Implicit:       module.docsPath,
156		Output:         module.genOutputs_c,
157		ImplicitOutput: module.genOutputs_h,
158		Args: map[string]string{
159			"pkgName": pkgName,
160			"outDir":  android.PathForModuleGen(ctx, "cpp").String(),
161		},
162	})
163}
164
165func xsdConfigMutator(mctx android.TopDownMutatorContext) {
166	if module, ok := mctx.Module().(*xsdConfig); ok {
167		name := module.BaseModuleName()
168
169		args := " --stub-packages " + *module.properties.Package_name +
170			" --hide MissingPermission --hide BroadcastBehavior" +
171			" --hide HiddenSuperclass --hide DeprecationMismatch --hide UnavailableSymbol" +
172			" --hide SdkConstant --hide HiddenTypeParameter --hide Todo --hide Typo"
173
174		api_dir := proptools.StringDefault(module.properties.Api_dir, "api")
175
176		currentApiFileName := filepath.Join(api_dir, "current.txt")
177		removedApiFileName := filepath.Join(api_dir, "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_dir, "last_current.txt"))
186		check_api.Last_released.Removed_api_file = proptools.StringPtr(
187			filepath.Join(api_dir, "last_removed.txt"))
188
189		mctx.CreateModule(android.ModuleFactoryAdaptor(java.DroidstubsFactory), &DroidstubsProperties{
190			Name:                 proptools.StringPtr(name + ".docs"),
191			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}
209