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 ( |
Sam Delmerico | 2351eac | 2022-05-24 17:10:02 +0000 | [diff] [blame] | 18 | "path/filepath" |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 19 | "strconv" |
Jiyong Park | 2907459 | 2019-07-07 16:27:47 +0900 | [diff] [blame] | 20 | "strings" |
| 21 | |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 22 | "github.com/google/blueprint" |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 23 | "github.com/google/blueprint/pathtools" |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 24 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 25 | "android/soong/android" |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | func init() { |
Dan Willemsen | aad1960 | 2019-04-07 09:44:35 -0700 | [diff] [blame] | 29 | 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 Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | var ( |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 35 | logtags = pctx.AndroidStaticRule("logtags", |
Colin Cross | f05fe97 | 2015-04-10 17:45:20 -0700 | [diff] [blame] | 36 | blueprint.RuleParams{ |
Colin Cross | b1bd1aa | 2017-11-15 22:47:16 -0800 | [diff] [blame] | 37 | Command: "$logtagsCmd -o $out $in", |
Dan Willemsen | aad1960 | 2019-04-07 09:44:35 -0700 | [diff] [blame] | 38 | CommandDeps: []string{"$logtagsCmd", "$logtagsLib"}, |
Colin Cross | f05fe97 | 2015-04-10 17:45:20 -0700 | [diff] [blame] | 39 | }) |
| 40 | |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 41 | mergeLogtags = pctx.AndroidStaticRule("mergeLogtags", |
Colin Cross | f05fe97 | 2015-04-10 17:45:20 -0700 | [diff] [blame] | 42 | blueprint.RuleParams{ |
| 43 | Command: "$mergeLogtagsCmd -o $out $in", |
Dan Willemsen | aad1960 | 2019-04-07 09:44:35 -0700 | [diff] [blame] | 44 | CommandDeps: []string{"$mergeLogtagsCmd", "$logtagsLib"}, |
Colin Cross | f05fe97 | 2015-04-10 17:45:20 -0700 | [diff] [blame] | 45 | }) |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 46 | ) |
| 47 | |
Thiébaud Weksteen | de8417c | 2022-02-10 15:41:46 +1100 | [diff] [blame] | 48 | 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] | 49 | // 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 Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 52 | |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 53 | srcJarFiles := make(android.Paths, 0, len(shards)) |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 54 | |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 55 | 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 Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 61 | rule := android.NewRuleBuilder(pctx, ctx) |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 62 | |
| 63 | rule.Command().Text("rm -rf").Flag(outDir.String()) |
| 64 | rule.Command().Text("mkdir -p").Flag(outDir.String()) |
Thiébaud Weksteen | de8417c | 2022-02-10 15:41:46 +1100 | [diff] [blame] | 65 | rule.Command().Text("FLAGS=' " + aidlGlobalFlags + "'") |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 66 | |
| 67 | for _, aidlFile := range shard { |
Thiébaud Weksteen | de8417c | 2022-02-10 15:41:46 +1100 | [diff] [blame] | 68 | localFlag := aidlIndividualFlags[aidlFile.String()] |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 69 | 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 Weksteen | de8417c | 2022-02-10 15:41:46 +1100 | [diff] [blame] | 75 | Flag(localFlag). |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 76 | 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 Cross | cf02ec8 | 2020-12-23 17:13:16 -0800 | [diff] [blame] | 84 | Flag("-srcjar"). |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 85 | 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 Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 101 | rule.Build(ruleName, ruleDesc) |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | return srcJarFiles |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 107 | func genLogtags(ctx android.ModuleContext, logtagsFile android.Path) android.Path { |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 108 | javaFile := android.GenPathWithExt(ctx, "logtags", logtagsFile, "java") |
Colin Cross | f05fe97 | 2015-04-10 17:45:20 -0700 | [diff] [blame] | 109 | |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 110 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 111 | Rule: logtags, |
| 112 | Description: "logtags " + logtagsFile.Rel(), |
| 113 | Output: javaFile, |
| 114 | Input: logtagsFile, |
Colin Cross | f05fe97 | 2015-04-10 17:45:20 -0700 | [diff] [blame] | 115 | }) |
| 116 | |
| 117 | return javaFile |
| 118 | } |
| 119 | |
Sam Delmerico | 2351eac | 2022-05-24 17:10:02 +0000 | [diff] [blame] | 120 | // 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. |
| 123 | func genAidlIncludeFlags(ctx android.PathContext, srcFiles android.Paths, excludeDirs android.Paths) string { |
Jiyong Park | 1112c4c | 2019-08-16 21:12:10 +0900 | [diff] [blame] | 124 | var baseDirs []string |
Sam Delmerico | 2351eac | 2022-05-24 17:10:02 +0000 | [diff] [blame] | 125 | excludeDirsStrings := excludeDirs.Strings() |
Jiyong Park | 1112c4c | 2019-08-16 21:12:10 +0900 | [diff] [blame] | 126 | for _, srcFile := range srcFiles { |
| 127 | if srcFile.Ext() == ".aidl" { |
| 128 | baseDir := strings.TrimSuffix(srcFile.String(), srcFile.Rel()) |
Sam Delmerico | 2351eac | 2022-05-24 17:10:02 +0000 | [diff] [blame] | 129 | 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 Park | 1112c4c | 2019-08-16 21:12:10 +0900 | [diff] [blame] | 145 | baseDirs = append(baseDirs, baseDir) |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | return android.JoinWithPrefix(baseDirs, " -I") |
| 150 | } |
| 151 | |
Colin Cross | 46c9b8b | 2017-06-22 16:51:17 -0700 | [diff] [blame] | 152 | func (j *Module) genSources(ctx android.ModuleContext, srcFiles android.Paths, |
Colin Cross | af05017 | 2017-11-15 23:01:59 -0800 | [diff] [blame] | 153 | flags javaBuilderFlags) android.Paths { |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 154 | |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 155 | outSrcFiles := make(android.Paths, 0, len(srcFiles)) |
Colin Cross | 9516a6c | 2019-06-14 18:51:12 -0700 | [diff] [blame] | 156 | var protoSrcs android.Paths |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 157 | var aidlSrcs android.Paths |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 158 | |
| 159 | for _, srcFile := range srcFiles { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 160 | switch srcFile.Ext() { |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 161 | case ".aidl": |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 162 | aidlSrcs = append(aidlSrcs, srcFile) |
Colin Cross | f05fe97 | 2015-04-10 17:45:20 -0700 | [diff] [blame] | 163 | case ".logtags": |
| 164 | j.logtagsSrcs = append(j.logtagsSrcs, srcFile) |
| 165 | javaFile := genLogtags(ctx, srcFile) |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 166 | outSrcFiles = append(outSrcFiles, javaFile) |
| 167 | case ".proto": |
Colin Cross | 9516a6c | 2019-06-14 18:51:12 -0700 | [diff] [blame] | 168 | protoSrcs = append(protoSrcs, srcFile) |
Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 169 | default: |
| 170 | outSrcFiles = append(outSrcFiles, srcFile) |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
Colin Cross | 9516a6c | 2019-06-14 18:51:12 -0700 | [diff] [blame] | 174 | // 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 Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 180 | // Process all aidl files together to support sharding them into one or more rules that produce srcjars. |
| 181 | if len(aidlSrcs) > 0 { |
Thiébaud Weksteen | de8417c | 2022-02-10 15:41:46 +1100 | [diff] [blame] | 182 | 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 Delmerico | 2351eac | 2022-05-24 17:10:02 +0000 | [diff] [blame] | 189 | srcJarFiles := genAidl(ctx, aidlSrcs, flags.aidlFlags, individualFlags, flags.aidlDeps) |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 190 | outSrcFiles = append(outSrcFiles, srcJarFiles...) |
| 191 | } |
| 192 | |
Colin Cross | af05017 | 2017-11-15 23:01:59 -0800 | [diff] [blame] | 193 | return outSrcFiles |
Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 194 | } |
Colin Cross | f05fe97 | 2015-04-10 17:45:20 -0700 | [diff] [blame] | 195 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 196 | func LogtagsSingleton() android.Singleton { |
Colin Cross | f05fe97 | 2015-04-10 17:45:20 -0700 | [diff] [blame] | 197 | return &logtagsSingleton{} |
| 198 | } |
| 199 | |
| 200 | type logtagsProducer interface { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 201 | logtags() android.Paths |
Colin Cross | f05fe97 | 2015-04-10 17:45:20 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Jaewoong Jung | 2634264 | 2021-03-17 15:56:23 -0700 | [diff] [blame] | 204 | func (j *Module) logtags() android.Paths { |
| 205 | return j.logtagsSrcs |
| 206 | } |
| 207 | |
| 208 | var _ logtagsProducer = (*Module)(nil) |
| 209 | |
Colin Cross | f05fe97 | 2015-04-10 17:45:20 -0700 | [diff] [blame] | 210 | type logtagsSingleton struct{} |
| 211 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 212 | func (l *logtagsSingleton) GenerateBuildActions(ctx android.SingletonContext) { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 213 | var allLogtags android.Paths |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 214 | ctx.VisitAllModules(func(module android.Module) { |
Colin Cross | f05fe97 | 2015-04-10 17:45:20 -0700 | [diff] [blame] | 215 | if logtags, ok := module.(logtagsProducer); ok { |
| 216 | allLogtags = append(allLogtags, logtags.logtags()...) |
| 217 | } |
| 218 | }) |
| 219 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 220 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 221 | Rule: mergeLogtags, |
| 222 | Description: "merge logtags", |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 223 | Output: android.PathForIntermediates(ctx, "all-event-log-tags.txt"), |
| 224 | Inputs: allLogtags, |
Colin Cross | f05fe97 | 2015-04-10 17:45:20 -0700 | [diff] [blame] | 225 | }) |
| 226 | } |