| Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 1 | // Copyright 2018 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 |  | 
|  | 17 | import ( | 
|  | 18 | "encoding/json" | 
|  | 19 | "fmt" | 
| Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 20 |  | 
|  | 21 | "android/soong/android" | 
|  | 22 | ) | 
|  | 23 |  | 
|  | 24 | // This singleton generates android java dependency into to a json file. It does so for each | 
|  | 25 | // blueprint Android.bp resulting in a java.Module when either make, mm, mma, mmm or mmma is | 
|  | 26 | // called. Dependency info file is generated in $OUT/module_bp_java_depend.json. | 
|  | 27 |  | 
|  | 28 | func init() { | 
| LaMont Jones | 0c10e4d | 2023-05-16 00:58:37 +0000 | [diff] [blame] | 29 | android.RegisterParallelSingletonType("jdeps_generator", jDepsGeneratorSingleton) | 
| Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 30 | } | 
|  | 31 |  | 
|  | 32 | func jDepsGeneratorSingleton() android.Singleton { | 
|  | 33 | return &jdepsGeneratorSingleton{} | 
|  | 34 | } | 
|  | 35 |  | 
|  | 36 | type jdepsGeneratorSingleton struct { | 
| Liz Kammer | 5e07d0c | 2020-06-30 14:37:22 -0700 | [diff] [blame] | 37 | outputPath android.Path | 
| Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 38 | } | 
|  | 39 |  | 
| Liz Kammer | 5e07d0c | 2020-06-30 14:37:22 -0700 | [diff] [blame] | 40 | var _ android.SingletonMakeVarsProvider = (*jdepsGeneratorSingleton)(nil) | 
|  | 41 |  | 
| Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 42 | const ( | 
| Jim Tang | c44ba2a | 2021-11-03 15:55:01 +0800 | [diff] [blame] | 43 | jdepsJsonFileName = "module_bp_java_deps.json" | 
| Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 44 | ) | 
|  | 45 |  | 
|  | 46 | func (j *jdepsGeneratorSingleton) GenerateBuildActions(ctx android.SingletonContext) { | 
| Jim Tang | c44ba2a | 2021-11-03 15:55:01 +0800 | [diff] [blame] | 47 | // (b/204397180) Generate module_bp_java_deps.json by default. | 
| Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 48 | moduleInfos := make(map[string]android.IdeInfo) | 
|  | 49 |  | 
|  | 50 | ctx.VisitAllModules(func(module android.Module) { | 
| Colin Cross | c48428a | 2019-03-22 21:39:34 -0700 | [diff] [blame] | 51 | if !module.Enabled() { | 
|  | 52 | return | 
|  | 53 | } | 
|  | 54 |  | 
| Inseob Kim | 48aa223 | 2023-01-11 11:30:42 +0900 | [diff] [blame] | 55 | // Prevent including both prebuilts and matching source modules when one replaces the other. | 
|  | 56 | if !android.IsModulePreferred(module) { | 
|  | 57 | return | 
|  | 58 | } | 
|  | 59 |  | 
| Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 60 | ideInfoProvider, ok := module.(android.IDEInfo) | 
|  | 61 | if !ok { | 
|  | 62 | return | 
|  | 63 | } | 
|  | 64 | name := ideInfoProvider.BaseModuleName() | 
|  | 65 | ideModuleNameProvider, ok := module.(android.IDECustomizedModuleName) | 
|  | 66 | if ok { | 
|  | 67 | name = ideModuleNameProvider.IDECustomizedModuleName() | 
|  | 68 | } | 
|  | 69 |  | 
|  | 70 | dpInfo := moduleInfos[name] | 
|  | 71 | ideInfoProvider.IDEInfo(&dpInfo) | 
|  | 72 | dpInfo.Deps = android.FirstUniqueStrings(dpInfo.Deps) | 
|  | 73 | dpInfo.Srcs = android.FirstUniqueStrings(dpInfo.Srcs) | 
|  | 74 | dpInfo.Aidl_include_dirs = android.FirstUniqueStrings(dpInfo.Aidl_include_dirs) | 
|  | 75 | dpInfo.Jarjar_rules = android.FirstUniqueStrings(dpInfo.Jarjar_rules) | 
|  | 76 | dpInfo.Jars = android.FirstUniqueStrings(dpInfo.Jars) | 
| patricktu | 18c82ff | 2019-05-10 15:48:50 +0800 | [diff] [blame] | 77 | dpInfo.SrcJars = android.FirstUniqueStrings(dpInfo.SrcJars) | 
| Colin Cross | a644c26 | 2023-11-15 11:38:36 -0800 | [diff] [blame] | 78 | dpInfo.Paths = []string{ctx.ModuleDir(module)} | 
| Yike | f628202 | 2022-04-13 20:41:01 +0800 | [diff] [blame] | 79 | dpInfo.Static_libs = android.FirstUniqueStrings(dpInfo.Static_libs) | 
|  | 80 | dpInfo.Libs = android.FirstUniqueStrings(dpInfo.Libs) | 
| Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 81 | moduleInfos[name] = dpInfo | 
|  | 82 |  | 
|  | 83 | mkProvider, ok := module.(android.AndroidMkDataProvider) | 
|  | 84 | if !ok { | 
|  | 85 | return | 
|  | 86 | } | 
|  | 87 | data := mkProvider.AndroidMk() | 
|  | 88 | if data.Class != "" { | 
|  | 89 | dpInfo.Classes = append(dpInfo.Classes, data.Class) | 
|  | 90 | } | 
| shinwang | 7f1b38f | 2018-12-21 14:52:21 +0800 | [diff] [blame] | 91 |  | 
| Colin Cross | 5a37718 | 2023-12-14 14:46:23 -0800 | [diff] [blame] | 92 | if dep, ok := android.SingletonModuleProvider(ctx, module, JavaInfoProvider); ok { | 
| Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 93 | dpInfo.Installed_paths = append(dpInfo.Installed_paths, dep.ImplementationJars.Strings()...) | 
| Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 94 | } | 
|  | 95 | dpInfo.Classes = android.FirstUniqueStrings(dpInfo.Classes) | 
|  | 96 | dpInfo.Installed_paths = android.FirstUniqueStrings(dpInfo.Installed_paths) | 
|  | 97 | moduleInfos[name] = dpInfo | 
|  | 98 | }) | 
|  | 99 |  | 
| Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 100 | jfpath := android.PathForOutput(ctx, jdepsJsonFileName) | 
| Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 101 | err := createJsonFile(moduleInfos, jfpath) | 
|  | 102 | if err != nil { | 
|  | 103 | ctx.Errorf(err.Error()) | 
|  | 104 | } | 
| Liz Kammer | 5e07d0c | 2020-06-30 14:37:22 -0700 | [diff] [blame] | 105 | j.outputPath = jfpath | 
|  | 106 |  | 
|  | 107 | // This is necessary to satisfy the dangling rules check as this file is written by Soong rather than a rule. | 
|  | 108 | ctx.Build(pctx, android.BuildParams{ | 
|  | 109 | Rule:   android.Touch, | 
|  | 110 | Output: jfpath, | 
|  | 111 | }) | 
|  | 112 | } | 
|  | 113 |  | 
|  | 114 | func (j *jdepsGeneratorSingleton) MakeVars(ctx android.MakeVarsContext) { | 
|  | 115 | if j.outputPath == nil { | 
|  | 116 | return | 
|  | 117 | } | 
|  | 118 |  | 
|  | 119 | ctx.DistForGoal("general-tests", j.outputPath) | 
| Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 120 | } | 
|  | 121 |  | 
| Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 122 | func createJsonFile(moduleInfos map[string]android.IdeInfo, jfpath android.WritablePath) error { | 
| Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 123 | buf, err := json.MarshalIndent(moduleInfos, "", "\t") | 
|  | 124 | if err != nil { | 
| Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 125 | return fmt.Errorf("JSON marshal of java deps failed: %s", err) | 
| Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 126 | } | 
| Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 127 | err = android.WriteFileToOutputDir(jfpath, buf, 0666) | 
|  | 128 | if err != nil { | 
|  | 129 | return fmt.Errorf("Writing java deps to %s failed: %s", jfpath.String(), err) | 
|  | 130 | } | 
| Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 131 | return nil | 
|  | 132 | } |