| 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() { | 
|  | 29 | android.RegisterSingletonType("jdeps_generator", jDepsGeneratorSingleton) | 
|  | 30 | } | 
|  | 31 |  | 
|  | 32 | func jDepsGeneratorSingleton() android.Singleton { | 
|  | 33 | return &jdepsGeneratorSingleton{} | 
|  | 34 | } | 
|  | 35 |  | 
|  | 36 | type jdepsGeneratorSingleton struct { | 
|  | 37 | } | 
|  | 38 |  | 
|  | 39 | const ( | 
|  | 40 | // Environment variables used to modify behavior of this singleton. | 
|  | 41 | envVariableCollectJavaDeps = "SOONG_COLLECT_JAVA_DEPS" | 
|  | 42 | jdepsJsonFileName          = "module_bp_java_deps.json" | 
|  | 43 | ) | 
|  | 44 |  | 
|  | 45 | func (j *jdepsGeneratorSingleton) GenerateBuildActions(ctx android.SingletonContext) { | 
|  | 46 | if !ctx.Config().IsEnvTrue(envVariableCollectJavaDeps) { | 
|  | 47 | return | 
|  | 48 | } | 
|  | 49 |  | 
|  | 50 | moduleInfos := make(map[string]android.IdeInfo) | 
|  | 51 |  | 
|  | 52 | ctx.VisitAllModules(func(module android.Module) { | 
| Colin Cross | c48428a | 2019-03-22 21:39:34 -0700 | [diff] [blame] | 53 | if !module.Enabled() { | 
|  | 54 | return | 
|  | 55 | } | 
|  | 56 |  | 
| Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 57 | ideInfoProvider, ok := module.(android.IDEInfo) | 
|  | 58 | if !ok { | 
|  | 59 | return | 
|  | 60 | } | 
|  | 61 | name := ideInfoProvider.BaseModuleName() | 
|  | 62 | ideModuleNameProvider, ok := module.(android.IDECustomizedModuleName) | 
|  | 63 | if ok { | 
|  | 64 | name = ideModuleNameProvider.IDECustomizedModuleName() | 
|  | 65 | } | 
|  | 66 |  | 
|  | 67 | dpInfo := moduleInfos[name] | 
|  | 68 | ideInfoProvider.IDEInfo(&dpInfo) | 
|  | 69 | dpInfo.Deps = android.FirstUniqueStrings(dpInfo.Deps) | 
|  | 70 | dpInfo.Srcs = android.FirstUniqueStrings(dpInfo.Srcs) | 
|  | 71 | dpInfo.Aidl_include_dirs = android.FirstUniqueStrings(dpInfo.Aidl_include_dirs) | 
|  | 72 | dpInfo.Jarjar_rules = android.FirstUniqueStrings(dpInfo.Jarjar_rules) | 
|  | 73 | dpInfo.Jars = android.FirstUniqueStrings(dpInfo.Jars) | 
| patricktu | 18c82ff | 2019-05-10 15:48:50 +0800 | [diff] [blame] | 74 | dpInfo.SrcJars = android.FirstUniqueStrings(dpInfo.SrcJars) | 
| Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 75 | moduleInfos[name] = dpInfo | 
|  | 76 |  | 
|  | 77 | mkProvider, ok := module.(android.AndroidMkDataProvider) | 
|  | 78 | if !ok { | 
|  | 79 | return | 
|  | 80 | } | 
|  | 81 | data := mkProvider.AndroidMk() | 
|  | 82 | if data.Class != "" { | 
|  | 83 | dpInfo.Classes = append(dpInfo.Classes, data.Class) | 
|  | 84 | } | 
| shinwang | 7f1b38f | 2018-12-21 14:52:21 +0800 | [diff] [blame] | 85 |  | 
|  | 86 | if dep, ok := module.(Dependency); ok { | 
|  | 87 | dpInfo.Installed_paths = append(dpInfo.Installed_paths, dep.ImplementationJars().Strings()...) | 
| Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 88 | } | 
|  | 89 | dpInfo.Classes = android.FirstUniqueStrings(dpInfo.Classes) | 
|  | 90 | dpInfo.Installed_paths = android.FirstUniqueStrings(dpInfo.Installed_paths) | 
|  | 91 | moduleInfos[name] = dpInfo | 
|  | 92 | }) | 
|  | 93 |  | 
| Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 94 | jfpath := android.PathForOutput(ctx, jdepsJsonFileName) | 
| Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 95 | err := createJsonFile(moduleInfos, jfpath) | 
|  | 96 | if err != nil { | 
|  | 97 | ctx.Errorf(err.Error()) | 
|  | 98 | } | 
|  | 99 | } | 
|  | 100 |  | 
| Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 101 | func createJsonFile(moduleInfos map[string]android.IdeInfo, jfpath android.WritablePath) error { | 
| Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 102 | buf, err := json.MarshalIndent(moduleInfos, "", "\t") | 
|  | 103 | if err != nil { | 
| Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 104 | return fmt.Errorf("JSON marshal of java deps failed: %s", err) | 
| Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 105 | } | 
| Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 106 | err = android.WriteFileToOutputDir(jfpath, buf, 0666) | 
|  | 107 | if err != nil { | 
|  | 108 | return fmt.Errorf("Writing java deps to %s failed: %s", jfpath.String(), err) | 
|  | 109 | } | 
| Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 110 | return nil | 
|  | 111 | } |