blob: 8a90246be66adf651925033e58ac1dc4577472f6 [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 (
Jiyong Park29074592019-07-07 16:27:47 +090018 "strings"
19
Colin Crossc0b06f12015-04-08 13:03:43 -070020 "github.com/google/blueprint"
Colin Crossc0b06f12015-04-08 13:03:43 -070021
Colin Cross635c3b02016-05-18 15:37:25 -070022 "android/soong/android"
Colin Crossc0b06f12015-04-08 13:03:43 -070023)
24
25func init() {
Dan Willemsenaad19602019-04-07 09:44:35 -070026 pctx.SourcePathVariable("logtagsCmd", "build/make/tools/java-event-log-tags.py")
27 pctx.SourcePathVariable("mergeLogtagsCmd", "build/make/tools/merge-event-log-tags.py")
28 pctx.SourcePathVariable("logtagsLib", "build/make/tools/event_log_tags.py")
Colin Crossc0b06f12015-04-08 13:03:43 -070029}
30
31var (
Colin Cross9d45bb72016-08-29 16:14:13 -070032 aidl = pctx.AndroidStaticRule("aidl",
Colin Crossc0b06f12015-04-08 13:03:43 -070033 blueprint.RuleParams{
Colin Crossbdf95142019-08-08 12:56:34 -070034 Command: "${config.AidlCmd} -d$depFile $aidlFlags $in $out",
35 CommandDeps: []string{"${config.AidlCmd}"},
Colin Crossc0b06f12015-04-08 13:03:43 -070036 },
37 "depFile", "aidlFlags")
Colin Crossf05fe972015-04-10 17:45:20 -070038
Colin Cross9d45bb72016-08-29 16:14:13 -070039 logtags = pctx.AndroidStaticRule("logtags",
Colin Crossf05fe972015-04-10 17:45:20 -070040 blueprint.RuleParams{
Colin Crossb1bd1aa2017-11-15 22:47:16 -080041 Command: "$logtagsCmd -o $out $in",
Dan Willemsenaad19602019-04-07 09:44:35 -070042 CommandDeps: []string{"$logtagsCmd", "$logtagsLib"},
Colin Crossf05fe972015-04-10 17:45:20 -070043 })
44
Colin Cross9d45bb72016-08-29 16:14:13 -070045 mergeLogtags = pctx.AndroidStaticRule("mergeLogtags",
Colin Crossf05fe972015-04-10 17:45:20 -070046 blueprint.RuleParams{
47 Command: "$mergeLogtagsCmd -o $out $in",
Dan Willemsenaad19602019-04-07 09:44:35 -070048 CommandDeps: []string{"$mergeLogtagsCmd", "$logtagsLib"},
Colin Crossf05fe972015-04-10 17:45:20 -070049 })
Colin Crossc0b06f12015-04-08 13:03:43 -070050)
51
Colin Cross3047fa22019-04-18 10:56:44 -070052func genAidl(ctx android.ModuleContext, aidlFile android.Path, aidlFlags string, deps android.Paths) android.Path {
Dan Willemsen21ec4902016-11-02 20:43:13 -070053 javaFile := android.GenPathWithExt(ctx, "aidl", aidlFile, "java")
Dan Willemsen34cc69e2015-09-23 15:26:20 -070054 depFile := javaFile.String() + ".d"
Colin Crossc0b06f12015-04-08 13:03:43 -070055
Colin Crossae887032017-10-23 17:16:14 -070056 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -070057 Rule: aidl,
58 Description: "aidl " + aidlFile.Rel(),
59 Output: javaFile,
60 Input: aidlFile,
Colin Cross3047fa22019-04-18 10:56:44 -070061 Implicits: deps,
Colin Crossc0b06f12015-04-08 13:03:43 -070062 Args: map[string]string{
63 "depFile": depFile,
64 "aidlFlags": aidlFlags,
65 },
66 })
67
68 return javaFile
69}
70
Colin Cross635c3b02016-05-18 15:37:25 -070071func genLogtags(ctx android.ModuleContext, logtagsFile android.Path) android.Path {
Dan Willemsen21ec4902016-11-02 20:43:13 -070072 javaFile := android.GenPathWithExt(ctx, "logtags", logtagsFile, "java")
Colin Crossf05fe972015-04-10 17:45:20 -070073
Colin Crossae887032017-10-23 17:16:14 -070074 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -070075 Rule: logtags,
76 Description: "logtags " + logtagsFile.Rel(),
77 Output: javaFile,
78 Input: logtagsFile,
Colin Crossf05fe972015-04-10 17:45:20 -070079 })
80
81 return javaFile
82}
83
Jiyong Park1112c4c2019-08-16 21:12:10 +090084func genAidlIncludeFlags(srcFiles android.Paths) string {
85 var baseDirs []string
86 for _, srcFile := range srcFiles {
87 if srcFile.Ext() == ".aidl" {
88 baseDir := strings.TrimSuffix(srcFile.String(), srcFile.Rel())
89 if baseDir != "" && !android.InList(baseDir, baseDirs) {
90 baseDirs = append(baseDirs, baseDir)
91 }
92 }
93 }
94 return android.JoinWithPrefix(baseDirs, " -I")
95}
96
Colin Cross46c9b8b2017-06-22 16:51:17 -070097func (j *Module) genSources(ctx android.ModuleContext, srcFiles android.Paths,
Colin Crossaf050172017-11-15 23:01:59 -080098 flags javaBuilderFlags) android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -070099
Colin Cross6af17aa2017-09-20 12:59:05 -0700100 outSrcFiles := make(android.Paths, 0, len(srcFiles))
Colin Cross9516a6c2019-06-14 18:51:12 -0700101 var protoSrcs android.Paths
Colin Cross6af17aa2017-09-20 12:59:05 -0700102
Jiyong Park1112c4c2019-08-16 21:12:10 +0900103 aidlIncludeFlags := genAidlIncludeFlags(srcFiles)
104
Colin Cross6af17aa2017-09-20 12:59:05 -0700105 for _, srcFile := range srcFiles {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700106 switch srcFile.Ext() {
Colin Crossc0b06f12015-04-08 13:03:43 -0700107 case ".aidl":
Jiyong Park1112c4c2019-08-16 21:12:10 +0900108 javaFile := genAidl(ctx, srcFile, flags.aidlFlags+aidlIncludeFlags, flags.aidlDeps)
Colin Cross6af17aa2017-09-20 12:59:05 -0700109 outSrcFiles = append(outSrcFiles, javaFile)
Colin Crossf05fe972015-04-10 17:45:20 -0700110 case ".logtags":
111 j.logtagsSrcs = append(j.logtagsSrcs, srcFile)
112 javaFile := genLogtags(ctx, srcFile)
Colin Cross6af17aa2017-09-20 12:59:05 -0700113 outSrcFiles = append(outSrcFiles, javaFile)
114 case ".proto":
Colin Cross9516a6c2019-06-14 18:51:12 -0700115 protoSrcs = append(protoSrcs, srcFile)
Colin Cross6af17aa2017-09-20 12:59:05 -0700116 default:
117 outSrcFiles = append(outSrcFiles, srcFile)
Colin Crossc0b06f12015-04-08 13:03:43 -0700118 }
119 }
120
Colin Cross9516a6c2019-06-14 18:51:12 -0700121 // Process all proto files together to support sharding them into one or more rules that produce srcjars.
122 if len(protoSrcs) > 0 {
123 srcJarFiles := genProto(ctx, protoSrcs, flags.proto)
124 outSrcFiles = append(outSrcFiles, srcJarFiles...)
125 }
126
Colin Crossaf050172017-11-15 23:01:59 -0800127 return outSrcFiles
Colin Crossc0b06f12015-04-08 13:03:43 -0700128}
Colin Crossf05fe972015-04-10 17:45:20 -0700129
Colin Cross0875c522017-11-28 17:34:01 -0800130func LogtagsSingleton() android.Singleton {
Colin Crossf05fe972015-04-10 17:45:20 -0700131 return &logtagsSingleton{}
132}
133
134type logtagsProducer interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700135 logtags() android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700136}
137
138type logtagsSingleton struct{}
139
Colin Cross0875c522017-11-28 17:34:01 -0800140func (l *logtagsSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700141 var allLogtags android.Paths
Colin Cross0875c522017-11-28 17:34:01 -0800142 ctx.VisitAllModules(func(module android.Module) {
Colin Crossf05fe972015-04-10 17:45:20 -0700143 if logtags, ok := module.(logtagsProducer); ok {
144 allLogtags = append(allLogtags, logtags.logtags()...)
145 }
146 })
147
Colin Cross0875c522017-11-28 17:34:01 -0800148 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700149 Rule: mergeLogtags,
150 Description: "merge logtags",
Colin Cross0875c522017-11-28 17:34:01 -0800151 Output: android.PathForIntermediates(ctx, "all-event-log-tags.txt"),
152 Inputs: allLogtags,
Colin Crossf05fe972015-04-10 17:45:20 -0700153 })
154}