blob: a69e9a2b9b4dea07c51b277caf7db62460f890d0 [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() {
Inseob Kim108be542018-10-04 19:39:35 +090026 pctx.HostBinToolVariable("syspropCmd", "sysprop_java")
Dan Willemsenaad19602019-04-07 09:44:35 -070027 pctx.SourcePathVariable("logtagsCmd", "build/make/tools/java-event-log-tags.py")
28 pctx.SourcePathVariable("mergeLogtagsCmd", "build/make/tools/merge-event-log-tags.py")
29 pctx.SourcePathVariable("logtagsLib", "build/make/tools/event_log_tags.py")
Colin Crossc0b06f12015-04-08 13:03:43 -070030}
31
32var (
Colin Cross9d45bb72016-08-29 16:14:13 -070033 aidl = pctx.AndroidStaticRule("aidl",
Colin Crossc0b06f12015-04-08 13:03:43 -070034 blueprint.RuleParams{
Colin Crossbdf95142019-08-08 12:56:34 -070035 Command: "${config.AidlCmd} -d$depFile $aidlFlags $in $out",
36 CommandDeps: []string{"${config.AidlCmd}"},
Colin Crossc0b06f12015-04-08 13:03:43 -070037 },
38 "depFile", "aidlFlags")
Colin Crossf05fe972015-04-10 17:45:20 -070039
Colin Cross9d45bb72016-08-29 16:14:13 -070040 logtags = pctx.AndroidStaticRule("logtags",
Colin Crossf05fe972015-04-10 17:45:20 -070041 blueprint.RuleParams{
Colin Crossb1bd1aa2017-11-15 22:47:16 -080042 Command: "$logtagsCmd -o $out $in",
Dan Willemsenaad19602019-04-07 09:44:35 -070043 CommandDeps: []string{"$logtagsCmd", "$logtagsLib"},
Colin Crossf05fe972015-04-10 17:45:20 -070044 })
45
Colin Cross9d45bb72016-08-29 16:14:13 -070046 mergeLogtags = pctx.AndroidStaticRule("mergeLogtags",
Colin Crossf05fe972015-04-10 17:45:20 -070047 blueprint.RuleParams{
48 Command: "$mergeLogtagsCmd -o $out $in",
Dan Willemsenaad19602019-04-07 09:44:35 -070049 CommandDeps: []string{"$mergeLogtagsCmd", "$logtagsLib"},
Colin Crossf05fe972015-04-10 17:45:20 -070050 })
Inseob Kim108be542018-10-04 19:39:35 +090051
52 sysprop = pctx.AndroidStaticRule("sysprop",
53 blueprint.RuleParams{
54 Command: `rm -rf $out.tmp && mkdir -p $out.tmp && ` +
Inseob Kim42882742019-07-30 17:55:33 +090055 `$syspropCmd --scope $scope --java-output-dir $out.tmp $in && ` +
Inseob Kim108be542018-10-04 19:39:35 +090056 `${config.SoongZipCmd} -jar -o $out -C $out.tmp -D $out.tmp && rm -rf $out.tmp`,
57 CommandDeps: []string{
58 "$syspropCmd",
59 "${config.SoongZipCmd}",
60 },
Inseob Kim42882742019-07-30 17:55:33 +090061 }, "scope")
Colin Crossc0b06f12015-04-08 13:03:43 -070062)
63
Colin Cross3047fa22019-04-18 10:56:44 -070064func genAidl(ctx android.ModuleContext, aidlFile android.Path, aidlFlags string, deps android.Paths) android.Path {
Dan Willemsen21ec4902016-11-02 20:43:13 -070065 javaFile := android.GenPathWithExt(ctx, "aidl", aidlFile, "java")
Dan Willemsen34cc69e2015-09-23 15:26:20 -070066 depFile := javaFile.String() + ".d"
Colin Crossc0b06f12015-04-08 13:03:43 -070067
Colin Crossae887032017-10-23 17:16:14 -070068 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -070069 Rule: aidl,
70 Description: "aidl " + aidlFile.Rel(),
71 Output: javaFile,
72 Input: aidlFile,
Colin Cross3047fa22019-04-18 10:56:44 -070073 Implicits: deps,
Colin Crossc0b06f12015-04-08 13:03:43 -070074 Args: map[string]string{
75 "depFile": depFile,
76 "aidlFlags": aidlFlags,
77 },
78 })
79
80 return javaFile
81}
82
Colin Cross635c3b02016-05-18 15:37:25 -070083func genLogtags(ctx android.ModuleContext, logtagsFile android.Path) android.Path {
Dan Willemsen21ec4902016-11-02 20:43:13 -070084 javaFile := android.GenPathWithExt(ctx, "logtags", logtagsFile, "java")
Colin Crossf05fe972015-04-10 17:45:20 -070085
Colin Crossae887032017-10-23 17:16:14 -070086 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -070087 Rule: logtags,
88 Description: "logtags " + logtagsFile.Rel(),
89 Output: javaFile,
90 Input: logtagsFile,
Colin Crossf05fe972015-04-10 17:45:20 -070091 })
92
93 return javaFile
94}
95
Inseob Kim42882742019-07-30 17:55:33 +090096func genSysprop(ctx android.ModuleContext, syspropFile android.Path, scope string) android.Path {
Inseob Kim108be542018-10-04 19:39:35 +090097 srcJarFile := android.GenPathWithExt(ctx, "sysprop", syspropFile, "srcjar")
98
99 ctx.Build(pctx, android.BuildParams{
100 Rule: sysprop,
101 Description: "sysprop_java " + syspropFile.Rel(),
102 Output: srcJarFile,
103 Input: syspropFile,
Inseob Kim42882742019-07-30 17:55:33 +0900104 Args: map[string]string{
105 "scope": scope,
106 },
Inseob Kim108be542018-10-04 19:39:35 +0900107 })
108
109 return srcJarFile
110}
111
Jiyong Park1112c4c2019-08-16 21:12:10 +0900112func genAidlIncludeFlags(srcFiles android.Paths) string {
113 var baseDirs []string
114 for _, srcFile := range srcFiles {
115 if srcFile.Ext() == ".aidl" {
116 baseDir := strings.TrimSuffix(srcFile.String(), srcFile.Rel())
117 if baseDir != "" && !android.InList(baseDir, baseDirs) {
118 baseDirs = append(baseDirs, baseDir)
119 }
120 }
121 }
122 return android.JoinWithPrefix(baseDirs, " -I")
123}
124
Colin Cross46c9b8b2017-06-22 16:51:17 -0700125func (j *Module) genSources(ctx android.ModuleContext, srcFiles android.Paths,
Colin Crossaf050172017-11-15 23:01:59 -0800126 flags javaBuilderFlags) android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -0700127
Colin Cross6af17aa2017-09-20 12:59:05 -0700128 outSrcFiles := make(android.Paths, 0, len(srcFiles))
129
Jiyong Park1112c4c2019-08-16 21:12:10 +0900130 aidlIncludeFlags := genAidlIncludeFlags(srcFiles)
131
Colin Cross6af17aa2017-09-20 12:59:05 -0700132 for _, srcFile := range srcFiles {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700133 switch srcFile.Ext() {
Colin Crossc0b06f12015-04-08 13:03:43 -0700134 case ".aidl":
Jiyong Park1112c4c2019-08-16 21:12:10 +0900135 javaFile := genAidl(ctx, srcFile, flags.aidlFlags+aidlIncludeFlags, flags.aidlDeps)
Colin Cross6af17aa2017-09-20 12:59:05 -0700136 outSrcFiles = append(outSrcFiles, javaFile)
Colin Crossf05fe972015-04-10 17:45:20 -0700137 case ".logtags":
138 j.logtagsSrcs = append(j.logtagsSrcs, srcFile)
139 javaFile := genLogtags(ctx, srcFile)
Colin Cross6af17aa2017-09-20 12:59:05 -0700140 outSrcFiles = append(outSrcFiles, javaFile)
141 case ".proto":
Colin Crossfe17f6f2019-03-28 19:30:56 -0700142 srcJarFile := genProto(ctx, srcFile, flags.proto)
Dan Willemsenab9f4262018-02-14 13:58:34 -0800143 outSrcFiles = append(outSrcFiles, srcJarFile)
Inseob Kim108be542018-10-04 19:39:35 +0900144 case ".sysprop":
Inseob Kim42882742019-07-30 17:55:33 +0900145 // internal scope contains all properties
146 // public scope only contains public properties
147 // use public if the owner is different from client
148 scope := "internal"
149 if j.properties.Sysprop.Platform != nil {
150 isProduct := ctx.ProductSpecific()
151 isVendor := ctx.SocSpecific()
152 isOwnerPlatform := Bool(j.properties.Sysprop.Platform)
153
154 if isProduct {
155 // product can't own any sysprop_library now, so product must use public scope
156 scope = "public"
157 } else if isVendor && !isOwnerPlatform {
158 // vendor and odm can't use system's internal property.
159 scope = "public"
160 }
161
162 // We don't care about clients under system.
163 // They can't use sysprop_library owned by other partitions.
164 }
165 srcJarFile := genSysprop(ctx, srcFile, scope)
Inseob Kim108be542018-10-04 19:39:35 +0900166 outSrcFiles = append(outSrcFiles, srcJarFile)
Colin Cross6af17aa2017-09-20 12:59:05 -0700167 default:
168 outSrcFiles = append(outSrcFiles, srcFile)
Colin Crossc0b06f12015-04-08 13:03:43 -0700169 }
170 }
171
Colin Crossaf050172017-11-15 23:01:59 -0800172 return outSrcFiles
Colin Crossc0b06f12015-04-08 13:03:43 -0700173}
Colin Crossf05fe972015-04-10 17:45:20 -0700174
Colin Cross0875c522017-11-28 17:34:01 -0800175func LogtagsSingleton() android.Singleton {
Colin Crossf05fe972015-04-10 17:45:20 -0700176 return &logtagsSingleton{}
177}
178
179type logtagsProducer interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700180 logtags() android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700181}
182
183type logtagsSingleton struct{}
184
Colin Cross0875c522017-11-28 17:34:01 -0800185func (l *logtagsSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700186 var allLogtags android.Paths
Colin Cross0875c522017-11-28 17:34:01 -0800187 ctx.VisitAllModules(func(module android.Module) {
Colin Crossf05fe972015-04-10 17:45:20 -0700188 if logtags, ok := module.(logtagsProducer); ok {
189 allLogtags = append(allLogtags, logtags.logtags()...)
190 }
191 })
192
Colin Cross0875c522017-11-28 17:34:01 -0800193 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700194 Rule: mergeLogtags,
195 Description: "merge logtags",
Colin Cross0875c522017-11-28 17:34:01 -0800196 Output: android.PathForIntermediates(ctx, "all-event-log-tags.txt"),
197 Inputs: allLogtags,
Colin Crossf05fe972015-04-10 17:45:20 -0700198 })
199}