blob: 1572bf00a1423bdacda87c28a1f92e44095384a3 [file] [log] [blame]
Colin Crossc0b06f12015-04-08 13:03:43 -07001// 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
Colin Crossc0b06f12015-04-08 13:03:43 -070017import (
Colin Crossc0806172019-06-14 18:51:47 -070018 "strconv"
Jiyong Park29074592019-07-07 16:27:47 +090019 "strings"
20
Colin Crossc0b06f12015-04-08 13:03:43 -070021 "github.com/google/blueprint"
Colin Crossc0806172019-06-14 18:51:47 -070022 "github.com/google/blueprint/pathtools"
Colin Crossc0b06f12015-04-08 13:03:43 -070023
Colin Cross635c3b02016-05-18 15:37:25 -070024 "android/soong/android"
Colin Crossc0b06f12015-04-08 13:03:43 -070025)
26
27func init() {
Dan Willemsenaad19602019-04-07 09:44:35 -070028 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")
Colin Crossc0b06f12015-04-08 13:03:43 -070031}
32
33var (
Colin Cross9d45bb72016-08-29 16:14:13 -070034 logtags = pctx.AndroidStaticRule("logtags",
Colin Crossf05fe972015-04-10 17:45:20 -070035 blueprint.RuleParams{
Colin Crossb1bd1aa2017-11-15 22:47:16 -080036 Command: "$logtagsCmd -o $out $in",
Dan Willemsenaad19602019-04-07 09:44:35 -070037 CommandDeps: []string{"$logtagsCmd", "$logtagsLib"},
Colin Crossf05fe972015-04-10 17:45:20 -070038 })
39
Colin Cross9d45bb72016-08-29 16:14:13 -070040 mergeLogtags = pctx.AndroidStaticRule("mergeLogtags",
Colin Crossf05fe972015-04-10 17:45:20 -070041 blueprint.RuleParams{
42 Command: "$mergeLogtagsCmd -o $out $in",
Dan Willemsenaad19602019-04-07 09:44:35 -070043 CommandDeps: []string{"$mergeLogtagsCmd", "$logtagsLib"},
Colin Crossf05fe972015-04-10 17:45:20 -070044 })
Colin Crossc0b06f12015-04-08 13:03:43 -070045)
46
Thiébaud Weksteende8417c2022-02-10 15:41:46 +110047func genAidl(ctx android.ModuleContext, aidlFiles android.Paths, aidlGlobalFlags string, aidlIndividualFlags map[string]string, deps android.Paths) android.Paths {
Colin Crossc0806172019-06-14 18:51:47 -070048 // 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)
Colin Crossc0b06f12015-04-08 13:03:43 -070051
Colin Crossc0806172019-06-14 18:51:47 -070052 srcJarFiles := make(android.Paths, 0, len(shards))
Colin Crossc0b06f12015-04-08 13:03:43 -070053
Colin Crossc0806172019-06-14 18:51:47 -070054 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
Colin Crossf1a035e2020-11-16 17:32:30 -080060 rule := android.NewRuleBuilder(pctx, ctx)
Colin Crossc0806172019-06-14 18:51:47 -070061
62 rule.Command().Text("rm -rf").Flag(outDir.String())
63 rule.Command().Text("mkdir -p").Flag(outDir.String())
Thiébaud Weksteende8417c2022-02-10 15:41:46 +110064 rule.Command().Text("FLAGS=' " + aidlGlobalFlags + "'")
Colin Crossc0806172019-06-14 18:51:47 -070065
66 for _, aidlFile := range shard {
Thiébaud Weksteende8417c2022-02-10 15:41:46 +110067 localFlag := aidlIndividualFlags[aidlFile.String()]
Colin Crossc0806172019-06-14 18:51:47 -070068 depFile := srcJarFile.InSameDir(ctx, aidlFile.String()+".d")
69 javaFile := outDir.Join(ctx, pathtools.ReplaceExtension(aidlFile.String(), "java"))
70 rule.Command().
71 Tool(ctx.Config().HostToolPath(ctx, "aidl")).
72 FlagWithDepFile("-d", depFile).
73 Flag("$FLAGS").
Thiébaud Weksteende8417c2022-02-10 15:41:46 +110074 Flag(localFlag).
Colin Crossc0806172019-06-14 18:51:47 -070075 Input(aidlFile).
76 Output(javaFile).
77 Implicits(deps)
78 rule.Temporary(javaFile)
79 }
80
81 rule.Command().
82 Tool(ctx.Config().HostToolPath(ctx, "soong_zip")).
Colin Crosscf02ec82020-12-23 17:13:16 -080083 Flag("-srcjar").
Colin Crossc0806172019-06-14 18:51:47 -070084 Flag("-write_if_changed").
85 FlagWithOutput("-o ", srcJarFile).
86 FlagWithArg("-C ", outDir.String()).
87 FlagWithArg("-D ", outDir.String())
88
89 rule.Command().Text("rm -rf").Flag(outDir.String())
90
91 rule.Restat()
92
93 ruleName := "aidl"
94 ruleDesc := "aidl"
95 if len(shards) > 1 {
96 ruleName += "_" + strconv.Itoa(i)
97 ruleDesc += " " + strconv.Itoa(i)
98 }
99
Colin Crossf1a035e2020-11-16 17:32:30 -0800100 rule.Build(ruleName, ruleDesc)
Colin Crossc0806172019-06-14 18:51:47 -0700101 }
102
103 return srcJarFiles
Colin Crossc0b06f12015-04-08 13:03:43 -0700104}
105
Colin Cross635c3b02016-05-18 15:37:25 -0700106func genLogtags(ctx android.ModuleContext, logtagsFile android.Path) android.Path {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700107 javaFile := android.GenPathWithExt(ctx, "logtags", logtagsFile, "java")
Colin Crossf05fe972015-04-10 17:45:20 -0700108
Colin Crossae887032017-10-23 17:16:14 -0700109 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700110 Rule: logtags,
111 Description: "logtags " + logtagsFile.Rel(),
112 Output: javaFile,
113 Input: logtagsFile,
Colin Crossf05fe972015-04-10 17:45:20 -0700114 })
115
116 return javaFile
117}
118
Jiyong Park1112c4c2019-08-16 21:12:10 +0900119func genAidlIncludeFlags(srcFiles android.Paths) string {
120 var baseDirs []string
121 for _, srcFile := range srcFiles {
122 if srcFile.Ext() == ".aidl" {
123 baseDir := strings.TrimSuffix(srcFile.String(), srcFile.Rel())
124 if baseDir != "" && !android.InList(baseDir, baseDirs) {
125 baseDirs = append(baseDirs, baseDir)
126 }
127 }
128 }
129 return android.JoinWithPrefix(baseDirs, " -I")
130}
131
Colin Cross46c9b8b2017-06-22 16:51:17 -0700132func (j *Module) genSources(ctx android.ModuleContext, srcFiles android.Paths,
Colin Crossaf050172017-11-15 23:01:59 -0800133 flags javaBuilderFlags) android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700134
Colin Cross6af17aa2017-09-20 12:59:05 -0700135 outSrcFiles := make(android.Paths, 0, len(srcFiles))
Colin Cross9516a6c2019-06-14 18:51:12 -0700136 var protoSrcs android.Paths
Colin Crossc0806172019-06-14 18:51:47 -0700137 var aidlSrcs android.Paths
Colin Cross6af17aa2017-09-20 12:59:05 -0700138
Jiyong Park1112c4c2019-08-16 21:12:10 +0900139 aidlIncludeFlags := genAidlIncludeFlags(srcFiles)
140
Colin Cross6af17aa2017-09-20 12:59:05 -0700141 for _, srcFile := range srcFiles {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700142 switch srcFile.Ext() {
Colin Crossc0b06f12015-04-08 13:03:43 -0700143 case ".aidl":
Colin Crossc0806172019-06-14 18:51:47 -0700144 aidlSrcs = append(aidlSrcs, srcFile)
Colin Crossf05fe972015-04-10 17:45:20 -0700145 case ".logtags":
146 j.logtagsSrcs = append(j.logtagsSrcs, srcFile)
147 javaFile := genLogtags(ctx, srcFile)
Colin Cross6af17aa2017-09-20 12:59:05 -0700148 outSrcFiles = append(outSrcFiles, javaFile)
149 case ".proto":
Colin Cross9516a6c2019-06-14 18:51:12 -0700150 protoSrcs = append(protoSrcs, srcFile)
Colin Cross6af17aa2017-09-20 12:59:05 -0700151 default:
152 outSrcFiles = append(outSrcFiles, srcFile)
Colin Crossc0b06f12015-04-08 13:03:43 -0700153 }
154 }
155
Colin Cross9516a6c2019-06-14 18:51:12 -0700156 // Process all proto files together to support sharding them into one or more rules that produce srcjars.
157 if len(protoSrcs) > 0 {
158 srcJarFiles := genProto(ctx, protoSrcs, flags.proto)
159 outSrcFiles = append(outSrcFiles, srcJarFiles...)
160 }
161
Colin Crossc0806172019-06-14 18:51:47 -0700162 // Process all aidl files together to support sharding them into one or more rules that produce srcjars.
163 if len(aidlSrcs) > 0 {
Thiébaud Weksteende8417c2022-02-10 15:41:46 +1100164 individualFlags := make(map[string]string)
165 for _, aidlSrc := range aidlSrcs {
166 flags := j.individualAidlFlags(ctx, aidlSrc)
167 if flags != "" {
168 individualFlags[aidlSrc.String()] = flags
169 }
170 }
171 srcJarFiles := genAidl(ctx, aidlSrcs, flags.aidlFlags+aidlIncludeFlags, individualFlags, flags.aidlDeps)
Colin Crossc0806172019-06-14 18:51:47 -0700172 outSrcFiles = append(outSrcFiles, srcJarFiles...)
173 }
174
Colin Crossaf050172017-11-15 23:01:59 -0800175 return outSrcFiles
Colin Crossc0b06f12015-04-08 13:03:43 -0700176}
Colin Crossf05fe972015-04-10 17:45:20 -0700177
Colin Cross0875c522017-11-28 17:34:01 -0800178func LogtagsSingleton() android.Singleton {
Colin Crossf05fe972015-04-10 17:45:20 -0700179 return &logtagsSingleton{}
180}
181
182type logtagsProducer interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700183 logtags() android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700184}
185
Jaewoong Jung26342642021-03-17 15:56:23 -0700186func (j *Module) logtags() android.Paths {
187 return j.logtagsSrcs
188}
189
190var _ logtagsProducer = (*Module)(nil)
191
Colin Crossf05fe972015-04-10 17:45:20 -0700192type logtagsSingleton struct{}
193
Colin Cross0875c522017-11-28 17:34:01 -0800194func (l *logtagsSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700195 var allLogtags android.Paths
Colin Cross0875c522017-11-28 17:34:01 -0800196 ctx.VisitAllModules(func(module android.Module) {
Colin Crossf05fe972015-04-10 17:45:20 -0700197 if logtags, ok := module.(logtagsProducer); ok {
198 allLogtags = append(allLogtags, logtags.logtags()...)
199 }
200 })
201
Colin Cross0875c522017-11-28 17:34:01 -0800202 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700203 Rule: mergeLogtags,
204 Description: "merge logtags",
Colin Cross0875c522017-11-28 17:34:01 -0800205 Output: android.PathForIntermediates(ctx, "all-event-log-tags.txt"),
206 Inputs: allLogtags,
Colin Crossf05fe972015-04-10 17:45:20 -0700207 })
208}