blob: 638da255ab8395781931cd04ff5d6c6108950491 [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 (
Sam Delmerico2351eac2022-05-24 17:10:02 +000018 "path/filepath"
Colin Crossc0806172019-06-14 18:51:47 -070019 "strconv"
Jiyong Park29074592019-07-07 16:27:47 +090020 "strings"
21
Colin Crossc0b06f12015-04-08 13:03:43 -070022 "github.com/google/blueprint"
Colin Crossc0806172019-06-14 18:51:47 -070023 "github.com/google/blueprint/pathtools"
Colin Crossc0b06f12015-04-08 13:03:43 -070024
Colin Cross635c3b02016-05-18 15:37:25 -070025 "android/soong/android"
Colin Crossc0b06f12015-04-08 13:03:43 -070026)
27
28func init() {
Dan Willemsenaad19602019-04-07 09:44:35 -070029 pctx.SourcePathVariable("logtagsCmd", "build/make/tools/java-event-log-tags.py")
30 pctx.SourcePathVariable("mergeLogtagsCmd", "build/make/tools/merge-event-log-tags.py")
31 pctx.SourcePathVariable("logtagsLib", "build/make/tools/event_log_tags.py")
Colin Crossc0b06f12015-04-08 13:03:43 -070032}
33
34var (
Colin Cross9d45bb72016-08-29 16:14:13 -070035 logtags = pctx.AndroidStaticRule("logtags",
Colin Crossf05fe972015-04-10 17:45:20 -070036 blueprint.RuleParams{
Colin Crossb1bd1aa2017-11-15 22:47:16 -080037 Command: "$logtagsCmd -o $out $in",
Dan Willemsenaad19602019-04-07 09:44:35 -070038 CommandDeps: []string{"$logtagsCmd", "$logtagsLib"},
Colin Crossf05fe972015-04-10 17:45:20 -070039 })
40
Colin Cross9d45bb72016-08-29 16:14:13 -070041 mergeLogtags = pctx.AndroidStaticRule("mergeLogtags",
Colin Crossf05fe972015-04-10 17:45:20 -070042 blueprint.RuleParams{
43 Command: "$mergeLogtagsCmd -o $out $in",
Dan Willemsenaad19602019-04-07 09:44:35 -070044 CommandDeps: []string{"$mergeLogtagsCmd", "$logtagsLib"},
Colin Crossf05fe972015-04-10 17:45:20 -070045 })
Colin Crossc0b06f12015-04-08 13:03:43 -070046)
47
Thiébaud Weksteende8417c2022-02-10 15:41:46 +110048func 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 -070049 // Shard aidl files into groups of 50 to avoid having to recompile all of them if one changes and to avoid
50 // hitting command line length limits.
51 shards := android.ShardPaths(aidlFiles, 50)
Colin Crossc0b06f12015-04-08 13:03:43 -070052
Colin Crossc0806172019-06-14 18:51:47 -070053 srcJarFiles := make(android.Paths, 0, len(shards))
Colin Crossc0b06f12015-04-08 13:03:43 -070054
Colin Crossc0806172019-06-14 18:51:47 -070055 for i, shard := range shards {
56 srcJarFile := android.PathForModuleGen(ctx, "aidl", "aidl"+strconv.Itoa(i)+".srcjar")
57 srcJarFiles = append(srcJarFiles, srcJarFile)
58
59 outDir := srcJarFile.ReplaceExtension(ctx, "tmp")
60
Colin Crossf1a035e2020-11-16 17:32:30 -080061 rule := android.NewRuleBuilder(pctx, ctx)
Colin Crossc0806172019-06-14 18:51:47 -070062
63 rule.Command().Text("rm -rf").Flag(outDir.String())
64 rule.Command().Text("mkdir -p").Flag(outDir.String())
Thiébaud Weksteende8417c2022-02-10 15:41:46 +110065 rule.Command().Text("FLAGS=' " + aidlGlobalFlags + "'")
Colin Crossc0806172019-06-14 18:51:47 -070066
67 for _, aidlFile := range shard {
Thiébaud Weksteende8417c2022-02-10 15:41:46 +110068 localFlag := aidlIndividualFlags[aidlFile.String()]
Colin Crossc0806172019-06-14 18:51:47 -070069 depFile := srcJarFile.InSameDir(ctx, aidlFile.String()+".d")
70 javaFile := outDir.Join(ctx, pathtools.ReplaceExtension(aidlFile.String(), "java"))
71 rule.Command().
72 Tool(ctx.Config().HostToolPath(ctx, "aidl")).
73 FlagWithDepFile("-d", depFile).
74 Flag("$FLAGS").
Thiébaud Weksteende8417c2022-02-10 15:41:46 +110075 Flag(localFlag).
Colin Crossc0806172019-06-14 18:51:47 -070076 Input(aidlFile).
77 Output(javaFile).
78 Implicits(deps)
79 rule.Temporary(javaFile)
80 }
81
82 rule.Command().
83 Tool(ctx.Config().HostToolPath(ctx, "soong_zip")).
Colin Crosscf02ec82020-12-23 17:13:16 -080084 Flag("-srcjar").
Colin Crossc0806172019-06-14 18:51:47 -070085 Flag("-write_if_changed").
86 FlagWithOutput("-o ", srcJarFile).
87 FlagWithArg("-C ", outDir.String()).
88 FlagWithArg("-D ", outDir.String())
89
90 rule.Command().Text("rm -rf").Flag(outDir.String())
91
92 rule.Restat()
93
94 ruleName := "aidl"
95 ruleDesc := "aidl"
96 if len(shards) > 1 {
97 ruleName += "_" + strconv.Itoa(i)
98 ruleDesc += " " + strconv.Itoa(i)
99 }
100
Colin Crossf1a035e2020-11-16 17:32:30 -0800101 rule.Build(ruleName, ruleDesc)
Colin Crossc0806172019-06-14 18:51:47 -0700102 }
103
104 return srcJarFiles
Colin Crossc0b06f12015-04-08 13:03:43 -0700105}
106
Colin Cross635c3b02016-05-18 15:37:25 -0700107func genLogtags(ctx android.ModuleContext, logtagsFile android.Path) android.Path {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700108 javaFile := android.GenPathWithExt(ctx, "logtags", logtagsFile, "java")
Colin Crossf05fe972015-04-10 17:45:20 -0700109
Colin Crossae887032017-10-23 17:16:14 -0700110 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700111 Rule: logtags,
112 Description: "logtags " + logtagsFile.Rel(),
113 Output: javaFile,
114 Input: logtagsFile,
Colin Crossf05fe972015-04-10 17:45:20 -0700115 })
116
117 return javaFile
118}
119
Sam Delmerico2351eac2022-05-24 17:10:02 +0000120// genAidlIncludeFlags returns additional include flags based on the relative path
121// of each .aidl file passed in srcFiles. excludeDirs is a list of paths relative to
122// the Android checkout root that should not be included in the returned flags.
123func genAidlIncludeFlags(ctx android.PathContext, srcFiles android.Paths, excludeDirs android.Paths) string {
Jiyong Park1112c4c2019-08-16 21:12:10 +0900124 var baseDirs []string
Sam Delmerico2351eac2022-05-24 17:10:02 +0000125 excludeDirsStrings := excludeDirs.Strings()
Jiyong Park1112c4c2019-08-16 21:12:10 +0900126 for _, srcFile := range srcFiles {
127 if srcFile.Ext() == ".aidl" {
128 baseDir := strings.TrimSuffix(srcFile.String(), srcFile.Rel())
Sam Delmerico2351eac2022-05-24 17:10:02 +0000129 baseDir = filepath.Clean(baseDir)
130 baseDirSeen := android.InList(baseDir, baseDirs) || android.InList(baseDir, excludeDirsStrings)
131
132 // For go/bp2build mixed builds, a file may be listed under a
133 // directory in the Bazel output tree that is symlinked to a
134 // directory under the android source tree. We should only
135 // include one copy of this directory so that the AIDL tool
136 // doesn't find multiple definitions of the same AIDL class.
137 // This code comes into effect when filegroups are used in mixed builds.
138 bazelPathPrefix := android.PathForBazelOut(ctx, "").String()
139 bazelBaseDir, err := filepath.Rel(bazelPathPrefix, baseDir)
140 bazelBaseDirSeen := err == nil &&
141 android.InList(bazelBaseDir, baseDirs) ||
142 android.InList(bazelBaseDir, excludeDirsStrings)
143
144 if baseDir != "" && !baseDirSeen && !bazelBaseDirSeen {
Jiyong Park1112c4c2019-08-16 21:12:10 +0900145 baseDirs = append(baseDirs, baseDir)
146 }
147 }
148 }
149 return android.JoinWithPrefix(baseDirs, " -I")
150}
151
Colin Cross46c9b8b2017-06-22 16:51:17 -0700152func (j *Module) genSources(ctx android.ModuleContext, srcFiles android.Paths,
Colin Crossaf050172017-11-15 23:01:59 -0800153 flags javaBuilderFlags) android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700154
Colin Cross6af17aa2017-09-20 12:59:05 -0700155 outSrcFiles := make(android.Paths, 0, len(srcFiles))
Colin Cross9516a6c2019-06-14 18:51:12 -0700156 var protoSrcs android.Paths
Colin Crossc0806172019-06-14 18:51:47 -0700157 var aidlSrcs android.Paths
Colin Cross6af17aa2017-09-20 12:59:05 -0700158
159 for _, srcFile := range srcFiles {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700160 switch srcFile.Ext() {
Colin Crossc0b06f12015-04-08 13:03:43 -0700161 case ".aidl":
Colin Crossc0806172019-06-14 18:51:47 -0700162 aidlSrcs = append(aidlSrcs, srcFile)
Colin Crossf05fe972015-04-10 17:45:20 -0700163 case ".logtags":
164 j.logtagsSrcs = append(j.logtagsSrcs, srcFile)
165 javaFile := genLogtags(ctx, srcFile)
Colin Cross6af17aa2017-09-20 12:59:05 -0700166 outSrcFiles = append(outSrcFiles, javaFile)
167 case ".proto":
Colin Cross9516a6c2019-06-14 18:51:12 -0700168 protoSrcs = append(protoSrcs, srcFile)
Colin Cross6af17aa2017-09-20 12:59:05 -0700169 default:
170 outSrcFiles = append(outSrcFiles, srcFile)
Colin Crossc0b06f12015-04-08 13:03:43 -0700171 }
172 }
173
Colin Cross9516a6c2019-06-14 18:51:12 -0700174 // Process all proto files together to support sharding them into one or more rules that produce srcjars.
175 if len(protoSrcs) > 0 {
176 srcJarFiles := genProto(ctx, protoSrcs, flags.proto)
177 outSrcFiles = append(outSrcFiles, srcJarFiles...)
178 }
179
Colin Crossc0806172019-06-14 18:51:47 -0700180 // Process all aidl files together to support sharding them into one or more rules that produce srcjars.
181 if len(aidlSrcs) > 0 {
Thiébaud Weksteende8417c2022-02-10 15:41:46 +1100182 individualFlags := make(map[string]string)
183 for _, aidlSrc := range aidlSrcs {
184 flags := j.individualAidlFlags(ctx, aidlSrc)
185 if flags != "" {
186 individualFlags[aidlSrc.String()] = flags
187 }
188 }
Sam Delmerico2351eac2022-05-24 17:10:02 +0000189 srcJarFiles := genAidl(ctx, aidlSrcs, flags.aidlFlags, individualFlags, flags.aidlDeps)
Colin Crossc0806172019-06-14 18:51:47 -0700190 outSrcFiles = append(outSrcFiles, srcJarFiles...)
191 }
192
Colin Crossaf050172017-11-15 23:01:59 -0800193 return outSrcFiles
Colin Crossc0b06f12015-04-08 13:03:43 -0700194}
Colin Crossf05fe972015-04-10 17:45:20 -0700195
Colin Cross0875c522017-11-28 17:34:01 -0800196func LogtagsSingleton() android.Singleton {
Colin Crossf05fe972015-04-10 17:45:20 -0700197 return &logtagsSingleton{}
198}
199
200type logtagsProducer interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700201 logtags() android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700202}
203
Jaewoong Jung26342642021-03-17 15:56:23 -0700204func (j *Module) logtags() android.Paths {
205 return j.logtagsSrcs
206}
207
208var _ logtagsProducer = (*Module)(nil)
209
Colin Crossf05fe972015-04-10 17:45:20 -0700210type logtagsSingleton struct{}
211
Colin Cross0875c522017-11-28 17:34:01 -0800212func (l *logtagsSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700213 var allLogtags android.Paths
Colin Cross0875c522017-11-28 17:34:01 -0800214 ctx.VisitAllModules(func(module android.Module) {
Colin Crossf05fe972015-04-10 17:45:20 -0700215 if logtags, ok := module.(logtagsProducer); ok {
216 allLogtags = append(allLogtags, logtags.logtags()...)
217 }
218 })
219
Colin Cross0875c522017-11-28 17:34:01 -0800220 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700221 Rule: mergeLogtags,
222 Description: "merge logtags",
Colin Cross0875c522017-11-28 17:34:01 -0800223 Output: android.PathForIntermediates(ctx, "all-event-log-tags.txt"),
224 Inputs: allLogtags,
Colin Crossf05fe972015-04-10 17:45:20 -0700225 })
226}