1// Copyright 2015 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 java
16
17import (
18	"strconv"
19	"strings"
20
21	"github.com/google/blueprint"
22	"github.com/google/blueprint/pathtools"
23
24	"android/soong/android"
25)
26
27func init() {
28	pctx.SourcePathVariable("logtagsCmd", "build/make/tools/java-event-log-tags.py")
29	pctx.SourcePathVariable("mergeLogtagsCmd", "build/make/tools/merge-event-log-tags.py")
30	pctx.SourcePathVariable("logtagsLib", "build/make/tools/event_log_tags.py")
31}
32
33var (
34	logtags = pctx.AndroidStaticRule("logtags",
35		blueprint.RuleParams{
36			Command:     "$logtagsCmd -o $out $in",
37			CommandDeps: []string{"$logtagsCmd", "$logtagsLib"},
38		})
39
40	mergeLogtags = pctx.AndroidStaticRule("mergeLogtags",
41		blueprint.RuleParams{
42			Command:     "$mergeLogtagsCmd -o $out $in",
43			CommandDeps: []string{"$mergeLogtagsCmd", "$logtagsLib"},
44		})
45)
46
47func genAidl(ctx android.ModuleContext, aidlFiles android.Paths, aidlFlags string, deps android.Paths) android.Paths {
48	// Shard aidl files into groups of 50 to avoid having to recompile all of them if one changes and to avoid
49	// hitting command line length limits.
50	shards := android.ShardPaths(aidlFiles, 50)
51
52	srcJarFiles := make(android.Paths, 0, len(shards))
53
54	for i, shard := range shards {
55		srcJarFile := android.PathForModuleGen(ctx, "aidl", "aidl"+strconv.Itoa(i)+".srcjar")
56		srcJarFiles = append(srcJarFiles, srcJarFile)
57
58		outDir := srcJarFile.ReplaceExtension(ctx, "tmp")
59
60		rule := android.NewRuleBuilder(pctx, ctx)
61
62		rule.Command().Text("rm -rf").Flag(outDir.String())
63		rule.Command().Text("mkdir -p").Flag(outDir.String())
64		rule.Command().Text("FLAGS=' " + aidlFlags + "'")
65
66		for _, aidlFile := range shard {
67			depFile := srcJarFile.InSameDir(ctx, aidlFile.String()+".d")
68			javaFile := outDir.Join(ctx, pathtools.ReplaceExtension(aidlFile.String(), "java"))
69			rule.Command().
70				Tool(ctx.Config().HostToolPath(ctx, "aidl")).
71				FlagWithDepFile("-d", depFile).
72				Flag("$FLAGS").
73				Input(aidlFile).
74				Output(javaFile).
75				Implicits(deps)
76			rule.Temporary(javaFile)
77		}
78
79		rule.Command().
80			Tool(ctx.Config().HostToolPath(ctx, "soong_zip")).
81			Flag("-srcjar").
82			Flag("-write_if_changed").
83			FlagWithOutput("-o ", srcJarFile).
84			FlagWithArg("-C ", outDir.String()).
85			FlagWithArg("-D ", outDir.String())
86
87		rule.Command().Text("rm -rf").Flag(outDir.String())
88
89		rule.Restat()
90
91		ruleName := "aidl"
92		ruleDesc := "aidl"
93		if len(shards) > 1 {
94			ruleName += "_" + strconv.Itoa(i)
95			ruleDesc += " " + strconv.Itoa(i)
96		}
97
98		rule.Build(ruleName, ruleDesc)
99	}
100
101	return srcJarFiles
102}
103
104func genLogtags(ctx android.ModuleContext, logtagsFile android.Path) android.Path {
105	javaFile := android.GenPathWithExt(ctx, "logtags", logtagsFile, "java")
106
107	ctx.Build(pctx, android.BuildParams{
108		Rule:        logtags,
109		Description: "logtags " + logtagsFile.Rel(),
110		Output:      javaFile,
111		Input:       logtagsFile,
112	})
113
114	return javaFile
115}
116
117func genAidlIncludeFlags(srcFiles android.Paths) string {
118	var baseDirs []string
119	for _, srcFile := range srcFiles {
120		if srcFile.Ext() == ".aidl" {
121			baseDir := strings.TrimSuffix(srcFile.String(), srcFile.Rel())
122			if baseDir != "" && !android.InList(baseDir, baseDirs) {
123				baseDirs = append(baseDirs, baseDir)
124			}
125		}
126	}
127	return android.JoinWithPrefix(baseDirs, " -I")
128}
129
130func (j *Module) genSources(ctx android.ModuleContext, srcFiles android.Paths,
131	flags javaBuilderFlags) android.Paths {
132
133	outSrcFiles := make(android.Paths, 0, len(srcFiles))
134	var protoSrcs android.Paths
135	var aidlSrcs android.Paths
136
137	aidlIncludeFlags := genAidlIncludeFlags(srcFiles)
138
139	for _, srcFile := range srcFiles {
140		switch srcFile.Ext() {
141		case ".aidl":
142			aidlSrcs = append(aidlSrcs, srcFile)
143		case ".logtags":
144			j.logtagsSrcs = append(j.logtagsSrcs, srcFile)
145			javaFile := genLogtags(ctx, srcFile)
146			outSrcFiles = append(outSrcFiles, javaFile)
147		case ".proto":
148			protoSrcs = append(protoSrcs, srcFile)
149		default:
150			outSrcFiles = append(outSrcFiles, srcFile)
151		}
152	}
153
154	// Process all proto files together to support sharding them into one or more rules that produce srcjars.
155	if len(protoSrcs) > 0 {
156		srcJarFiles := genProto(ctx, protoSrcs, flags.proto)
157		outSrcFiles = append(outSrcFiles, srcJarFiles...)
158	}
159
160	// Process all aidl files together to support sharding them into one or more rules that produce srcjars.
161	if len(aidlSrcs) > 0 {
162		srcJarFiles := genAidl(ctx, aidlSrcs, flags.aidlFlags+aidlIncludeFlags, flags.aidlDeps)
163		outSrcFiles = append(outSrcFiles, srcJarFiles...)
164	}
165
166	return outSrcFiles
167}
168
169func LogtagsSingleton() android.Singleton {
170	return &logtagsSingleton{}
171}
172
173type logtagsProducer interface {
174	logtags() android.Paths
175}
176
177func (j *Module) logtags() android.Paths {
178	return j.logtagsSrcs
179}
180
181var _ logtagsProducer = (*Module)(nil)
182
183type logtagsSingleton struct{}
184
185func (l *logtagsSingleton) GenerateBuildActions(ctx android.SingletonContext) {
186	var allLogtags android.Paths
187	ctx.VisitAllModules(func(module android.Module) {
188		if logtags, ok := module.(logtagsProducer); ok {
189			allLogtags = append(allLogtags, logtags.logtags()...)
190		}
191	})
192
193	ctx.Build(pctx, android.BuildParams{
194		Rule:        mergeLogtags,
195		Description: "merge logtags",
196		Output:      android.PathForIntermediates(ctx, "all-event-log-tags.txt"),
197		Inputs:      allLogtags,
198	})
199}
200