Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package java |
| 16 | |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 17 | import ( |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 18 | "strconv" |
Jiyong Park | 2907459 | 2019-07-07 16:27:47 +0900 | [diff] [blame] | 19 | "strings" |
| 20 | |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 21 | "github.com/google/blueprint" |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 22 | "github.com/google/blueprint/pathtools" |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 23 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 24 | "android/soong/android" |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | func init() { |
Dan Willemsen | aad1960 | 2019-04-07 09:44:35 -0700 | [diff] [blame] | 28 | 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 Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | var ( |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 34 | logtags = pctx.AndroidStaticRule("logtags", |
Colin Cross | f05fe97 | 2015-04-10 17:45:20 -0700 | [diff] [blame] | 35 | blueprint.RuleParams{ |
Colin Cross | b1bd1aa | 2017-11-15 22:47:16 -0800 | [diff] [blame] | 36 | Command: "$logtagsCmd -o $out $in", |
Dan Willemsen | aad1960 | 2019-04-07 09:44:35 -0700 | [diff] [blame] | 37 | CommandDeps: []string{"$logtagsCmd", "$logtagsLib"}, |
Colin Cross | f05fe97 | 2015-04-10 17:45:20 -0700 | [diff] [blame] | 38 | }) |
| 39 | |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 40 | mergeLogtags = pctx.AndroidStaticRule("mergeLogtags", |
Colin Cross | f05fe97 | 2015-04-10 17:45:20 -0700 | [diff] [blame] | 41 | blueprint.RuleParams{ |
| 42 | Command: "$mergeLogtagsCmd -o $out $in", |
Dan Willemsen | aad1960 | 2019-04-07 09:44:35 -0700 | [diff] [blame] | 43 | CommandDeps: []string{"$mergeLogtagsCmd", "$logtagsLib"}, |
Colin Cross | f05fe97 | 2015-04-10 17:45:20 -0700 | [diff] [blame] | 44 | }) |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 45 | ) |
| 46 | |
Thiébaud Weksteen | de8417c | 2022-02-10 15:41:46 +1100 | [diff] [blame] | 47 | func genAidl(ctx android.ModuleContext, aidlFiles android.Paths, aidlGlobalFlags string, aidlIndividualFlags map[string]string, deps android.Paths) android.Paths { |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 48 | // 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 Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 51 | |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 52 | srcJarFiles := make(android.Paths, 0, len(shards)) |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 53 | |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 54 | 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 Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 60 | rule := android.NewRuleBuilder(pctx, ctx) |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 61 | |
| 62 | rule.Command().Text("rm -rf").Flag(outDir.String()) |
| 63 | rule.Command().Text("mkdir -p").Flag(outDir.String()) |
Thiébaud Weksteen | de8417c | 2022-02-10 15:41:46 +1100 | [diff] [blame] | 64 | rule.Command().Text("FLAGS=' " + aidlGlobalFlags + "'") |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 65 | |
| 66 | for _, aidlFile := range shard { |
Thiébaud Weksteen | de8417c | 2022-02-10 15:41:46 +1100 | [diff] [blame] | 67 | localFlag := aidlIndividualFlags[aidlFile.String()] |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 68 | 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 Weksteen | de8417c | 2022-02-10 15:41:46 +1100 | [diff] [blame] | 74 | Flag(localFlag). |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 75 | 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 Cross | cf02ec8 | 2020-12-23 17:13:16 -0800 | [diff] [blame] | 83 | Flag("-srcjar"). |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 84 | 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 Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 100 | rule.Build(ruleName, ruleDesc) |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | return srcJarFiles |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 106 | func genLogtags(ctx android.ModuleContext, logtagsFile android.Path) android.Path { |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 107 | javaFile := android.GenPathWithExt(ctx, "logtags", logtagsFile, "java") |
Colin Cross | f05fe97 | 2015-04-10 17:45:20 -0700 | [diff] [blame] | 108 | |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 109 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 110 | Rule: logtags, |
| 111 | Description: "logtags " + logtagsFile.Rel(), |
| 112 | Output: javaFile, |
| 113 | Input: logtagsFile, |
Colin Cross | f05fe97 | 2015-04-10 17:45:20 -0700 | [diff] [blame] | 114 | }) |
| 115 | |
| 116 | return javaFile |
| 117 | } |
| 118 | |
Jiyong Park | 1112c4c | 2019-08-16 21:12:10 +0900 | [diff] [blame] | 119 | func 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 Cross | 46c9b8b | 2017-06-22 16:51:17 -0700 | [diff] [blame] | 132 | func (j *Module) genSources(ctx android.ModuleContext, srcFiles android.Paths, |
Colin Cross | af05017 | 2017-11-15 23:01:59 -0800 | [diff] [blame] | 133 | flags javaBuilderFlags) android.Paths { |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 134 | |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 135 | outSrcFiles := make(android.Paths, 0, len(srcFiles)) |
Colin Cross | 9516a6c | 2019-06-14 18:51:12 -0700 | [diff] [blame] | 136 | var protoSrcs android.Paths |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 137 | var aidlSrcs android.Paths |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 138 | |
Jiyong Park | 1112c4c | 2019-08-16 21:12:10 +0900 | [diff] [blame] | 139 | aidlIncludeFlags := genAidlIncludeFlags(srcFiles) |
| 140 | |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 141 | for _, srcFile := range srcFiles { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 142 | switch srcFile.Ext() { |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 143 | case ".aidl": |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 144 | aidlSrcs = append(aidlSrcs, srcFile) |
Colin Cross | f05fe97 | 2015-04-10 17:45:20 -0700 | [diff] [blame] | 145 | case ".logtags": |
| 146 | j.logtagsSrcs = append(j.logtagsSrcs, srcFile) |
| 147 | javaFile := genLogtags(ctx, srcFile) |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 148 | outSrcFiles = append(outSrcFiles, javaFile) |
| 149 | case ".proto": |
Colin Cross | 9516a6c | 2019-06-14 18:51:12 -0700 | [diff] [blame] | 150 | protoSrcs = append(protoSrcs, srcFile) |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 151 | default: |
| 152 | outSrcFiles = append(outSrcFiles, srcFile) |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
Colin Cross | 9516a6c | 2019-06-14 18:51:12 -0700 | [diff] [blame] | 156 | // 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 Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 162 | // Process all aidl files together to support sharding them into one or more rules that produce srcjars. |
| 163 | if len(aidlSrcs) > 0 { |
Thiébaud Weksteen | de8417c | 2022-02-10 15:41:46 +1100 | [diff] [blame] | 164 | 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 Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 172 | outSrcFiles = append(outSrcFiles, srcJarFiles...) |
| 173 | } |
| 174 | |
Colin Cross | af05017 | 2017-11-15 23:01:59 -0800 | [diff] [blame] | 175 | return outSrcFiles |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 176 | } |
Colin Cross | f05fe97 | 2015-04-10 17:45:20 -0700 | [diff] [blame] | 177 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 178 | func LogtagsSingleton() android.Singleton { |
Colin Cross | f05fe97 | 2015-04-10 17:45:20 -0700 | [diff] [blame] | 179 | return &logtagsSingleton{} |
| 180 | } |
| 181 | |
| 182 | type logtagsProducer interface { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 183 | logtags() android.Paths |
Colin Cross | f05fe97 | 2015-04-10 17:45:20 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Jaewoong Jung | 2634264 | 2021-03-17 15:56:23 -0700 | [diff] [blame] | 186 | func (j *Module) logtags() android.Paths { |
| 187 | return j.logtagsSrcs |
| 188 | } |
| 189 | |
| 190 | var _ logtagsProducer = (*Module)(nil) |
| 191 | |
Colin Cross | f05fe97 | 2015-04-10 17:45:20 -0700 | [diff] [blame] | 192 | type logtagsSingleton struct{} |
| 193 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 194 | func (l *logtagsSingleton) GenerateBuildActions(ctx android.SingletonContext) { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 195 | var allLogtags android.Paths |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 196 | ctx.VisitAllModules(func(module android.Module) { |
Colin Cross | f05fe97 | 2015-04-10 17:45:20 -0700 | [diff] [blame] | 197 | if logtags, ok := module.(logtagsProducer); ok { |
| 198 | allLogtags = append(allLogtags, logtags.logtags()...) |
| 199 | } |
| 200 | }) |
| 201 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 202 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 203 | Rule: mergeLogtags, |
| 204 | Description: "merge logtags", |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 205 | Output: android.PathForIntermediates(ctx, "all-event-log-tags.txt"), |
| 206 | Inputs: allLogtags, |
Colin Cross | f05fe97 | 2015-04-10 17:45:20 -0700 | [diff] [blame] | 207 | }) |
| 208 | } |