blob: c9cabc8218058e3a40884fa45bca27f6ee27058f [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 Willemsen34cc69e2015-09-23 15:26:20 -070026 pctx.HostBinToolVariable("aidlCmd", "aidl")
Inseob Kim108be542018-10-04 19:39:35 +090027 pctx.HostBinToolVariable("syspropCmd", "sysprop_java")
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 aidl = pctx.AndroidStaticRule("aidl",
Colin Crossc0b06f12015-04-08 13:03:43 -070035 blueprint.RuleParams{
36 Command: "$aidlCmd -d$depFile $aidlFlags $in $out",
Dan Willemsenc94a7682015-11-17 15:27:28 -080037 CommandDeps: []string{"$aidlCmd"},
Colin Crossc0b06f12015-04-08 13:03:43 -070038 },
39 "depFile", "aidlFlags")
Colin Crossf05fe972015-04-10 17:45:20 -070040
Colin Cross9d45bb72016-08-29 16:14:13 -070041 logtags = pctx.AndroidStaticRule("logtags",
Colin Crossf05fe972015-04-10 17:45:20 -070042 blueprint.RuleParams{
Colin Crossb1bd1aa2017-11-15 22:47:16 -080043 Command: "$logtagsCmd -o $out $in",
Dan Willemsenaad19602019-04-07 09:44:35 -070044 CommandDeps: []string{"$logtagsCmd", "$logtagsLib"},
Colin Crossf05fe972015-04-10 17:45:20 -070045 })
46
Colin Cross9d45bb72016-08-29 16:14:13 -070047 mergeLogtags = pctx.AndroidStaticRule("mergeLogtags",
Colin Crossf05fe972015-04-10 17:45:20 -070048 blueprint.RuleParams{
49 Command: "$mergeLogtagsCmd -o $out $in",
Dan Willemsenaad19602019-04-07 09:44:35 -070050 CommandDeps: []string{"$mergeLogtagsCmd", "$logtagsLib"},
Colin Crossf05fe972015-04-10 17:45:20 -070051 })
Inseob Kim108be542018-10-04 19:39:35 +090052
53 sysprop = pctx.AndroidStaticRule("sysprop",
54 blueprint.RuleParams{
55 Command: `rm -rf $out.tmp && mkdir -p $out.tmp && ` +
56 `$syspropCmd --java-output-dir $out.tmp $in && ` +
57 `${config.SoongZipCmd} -jar -o $out -C $out.tmp -D $out.tmp && rm -rf $out.tmp`,
58 CommandDeps: []string{
59 "$syspropCmd",
60 "${config.SoongZipCmd}",
61 },
62 })
Colin Crossc0b06f12015-04-08 13:03:43 -070063)
64
Colin Cross3047fa22019-04-18 10:56:44 -070065func genAidl(ctx android.ModuleContext, aidlFile android.Path, aidlFlags string, deps android.Paths) android.Path {
Dan Willemsen21ec4902016-11-02 20:43:13 -070066 javaFile := android.GenPathWithExt(ctx, "aidl", aidlFile, "java")
Dan Willemsen34cc69e2015-09-23 15:26:20 -070067 depFile := javaFile.String() + ".d"
Colin Crossc0b06f12015-04-08 13:03:43 -070068
Colin Crossae887032017-10-23 17:16:14 -070069 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -070070 Rule: aidl,
71 Description: "aidl " + aidlFile.Rel(),
72 Output: javaFile,
73 Input: aidlFile,
Colin Cross3047fa22019-04-18 10:56:44 -070074 Implicits: deps,
Colin Crossc0b06f12015-04-08 13:03:43 -070075 Args: map[string]string{
76 "depFile": depFile,
77 "aidlFlags": aidlFlags,
78 },
79 })
80
81 return javaFile
82}
83
Colin Cross635c3b02016-05-18 15:37:25 -070084func genLogtags(ctx android.ModuleContext, logtagsFile android.Path) android.Path {
Dan Willemsen21ec4902016-11-02 20:43:13 -070085 javaFile := android.GenPathWithExt(ctx, "logtags", logtagsFile, "java")
Colin Crossf05fe972015-04-10 17:45:20 -070086
Colin Crossae887032017-10-23 17:16:14 -070087 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -070088 Rule: logtags,
89 Description: "logtags " + logtagsFile.Rel(),
90 Output: javaFile,
91 Input: logtagsFile,
Colin Crossf05fe972015-04-10 17:45:20 -070092 })
93
94 return javaFile
95}
96
Inseob Kim108be542018-10-04 19:39:35 +090097func genSysprop(ctx android.ModuleContext, syspropFile android.Path) android.Path {
98 srcJarFile := android.GenPathWithExt(ctx, "sysprop", syspropFile, "srcjar")
99
100 ctx.Build(pctx, android.BuildParams{
101 Rule: sysprop,
102 Description: "sysprop_java " + syspropFile.Rel(),
103 Output: srcJarFile,
104 Input: syspropFile,
105 })
106
107 return srcJarFile
108}
109
Jiyong Park1112c4c2019-08-16 21:12:10 +0900110func genAidlIncludeFlags(srcFiles android.Paths) string {
111 var baseDirs []string
112 for _, srcFile := range srcFiles {
113 if srcFile.Ext() == ".aidl" {
114 baseDir := strings.TrimSuffix(srcFile.String(), srcFile.Rel())
115 if baseDir != "" && !android.InList(baseDir, baseDirs) {
116 baseDirs = append(baseDirs, baseDir)
117 }
118 }
119 }
120 return android.JoinWithPrefix(baseDirs, " -I")
121}
122
Colin Cross46c9b8b2017-06-22 16:51:17 -0700123func (j *Module) genSources(ctx android.ModuleContext, srcFiles android.Paths,
Colin Crossaf050172017-11-15 23:01:59 -0800124 flags javaBuilderFlags) android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700125
Colin Cross6af17aa2017-09-20 12:59:05 -0700126 outSrcFiles := make(android.Paths, 0, len(srcFiles))
127
Jiyong Park1112c4c2019-08-16 21:12:10 +0900128 aidlIncludeFlags := genAidlIncludeFlags(srcFiles)
129
Colin Cross6af17aa2017-09-20 12:59:05 -0700130 for _, srcFile := range srcFiles {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700131 switch srcFile.Ext() {
Colin Crossc0b06f12015-04-08 13:03:43 -0700132 case ".aidl":
Jiyong Park1112c4c2019-08-16 21:12:10 +0900133 javaFile := genAidl(ctx, srcFile, flags.aidlFlags+aidlIncludeFlags, flags.aidlDeps)
Colin Cross6af17aa2017-09-20 12:59:05 -0700134 outSrcFiles = append(outSrcFiles, javaFile)
Colin Crossf05fe972015-04-10 17:45:20 -0700135 case ".logtags":
136 j.logtagsSrcs = append(j.logtagsSrcs, srcFile)
137 javaFile := genLogtags(ctx, srcFile)
Colin Cross6af17aa2017-09-20 12:59:05 -0700138 outSrcFiles = append(outSrcFiles, javaFile)
139 case ".proto":
Colin Crossfe17f6f2019-03-28 19:30:56 -0700140 srcJarFile := genProto(ctx, srcFile, flags.proto)
Dan Willemsenab9f4262018-02-14 13:58:34 -0800141 outSrcFiles = append(outSrcFiles, srcJarFile)
Inseob Kim108be542018-10-04 19:39:35 +0900142 case ".sysprop":
143 srcJarFile := genSysprop(ctx, srcFile)
144 outSrcFiles = append(outSrcFiles, srcJarFile)
Colin Cross6af17aa2017-09-20 12:59:05 -0700145 default:
146 outSrcFiles = append(outSrcFiles, srcFile)
Colin Crossc0b06f12015-04-08 13:03:43 -0700147 }
148 }
149
Colin Crossaf050172017-11-15 23:01:59 -0800150 return outSrcFiles
Colin Crossc0b06f12015-04-08 13:03:43 -0700151}
Colin Crossf05fe972015-04-10 17:45:20 -0700152
Colin Cross0875c522017-11-28 17:34:01 -0800153func LogtagsSingleton() android.Singleton {
Colin Crossf05fe972015-04-10 17:45:20 -0700154 return &logtagsSingleton{}
155}
156
157type logtagsProducer interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700158 logtags() android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700159}
160
161type logtagsSingleton struct{}
162
Colin Cross0875c522017-11-28 17:34:01 -0800163func (l *logtagsSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700164 var allLogtags android.Paths
Colin Cross0875c522017-11-28 17:34:01 -0800165 ctx.VisitAllModules(func(module android.Module) {
Colin Crossf05fe972015-04-10 17:45:20 -0700166 if logtags, ok := module.(logtagsProducer); ok {
167 allLogtags = append(allLogtags, logtags.logtags()...)
168 }
169 })
170
Colin Cross0875c522017-11-28 17:34:01 -0800171 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700172 Rule: mergeLogtags,
173 Description: "merge logtags",
Colin Cross0875c522017-11-28 17:34:01 -0800174 Output: android.PathForIntermediates(ctx, "all-event-log-tags.txt"),
175 Inputs: allLogtags,
Colin Crossf05fe972015-04-10 17:45:20 -0700176 })
177}