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" |
| 20 | "os" |
| 21 | |
| 22 | "android/soong/android" |
| 23 | ) |
| 24 | |
| 25 | // This singleton generates android java dependency into to a json file. It does so for each |
| 26 | // blueprint Android.bp resulting in a java.Module when either make, mm, mma, mmm or mmma is |
| 27 | // called. Dependency info file is generated in $OUT/module_bp_java_depend.json. |
| 28 | |
| 29 | func init() { |
| 30 | android.RegisterSingletonType("jdeps_generator", jDepsGeneratorSingleton) |
| 31 | } |
| 32 | |
| 33 | func jDepsGeneratorSingleton() android.Singleton { |
| 34 | return &jdepsGeneratorSingleton{} |
| 35 | } |
| 36 | |
| 37 | type jdepsGeneratorSingleton struct { |
| 38 | } |
| 39 | |
| 40 | const ( |
| 41 | // Environment variables used to modify behavior of this singleton. |
| 42 | envVariableCollectJavaDeps = "SOONG_COLLECT_JAVA_DEPS" |
| 43 | jdepsJsonFileName = "module_bp_java_deps.json" |
| 44 | ) |
| 45 | |
| 46 | func (j *jdepsGeneratorSingleton) GenerateBuildActions(ctx android.SingletonContext) { |
| 47 | if !ctx.Config().IsEnvTrue(envVariableCollectJavaDeps) { |
| 48 | return |
| 49 | } |
| 50 | |
| 51 | moduleInfos := make(map[string]android.IdeInfo) |
| 52 | |
| 53 | ctx.VisitAllModules(func(module android.Module) { |
| 54 | ideInfoProvider, ok := module.(android.IDEInfo) |
| 55 | if !ok { |
| 56 | return |
| 57 | } |
| 58 | name := ideInfoProvider.BaseModuleName() |
| 59 | ideModuleNameProvider, ok := module.(android.IDECustomizedModuleName) |
| 60 | if ok { |
| 61 | name = ideModuleNameProvider.IDECustomizedModuleName() |
| 62 | } |
| 63 | |
| 64 | dpInfo := moduleInfos[name] |
| 65 | ideInfoProvider.IDEInfo(&dpInfo) |
| 66 | dpInfo.Deps = android.FirstUniqueStrings(dpInfo.Deps) |
| 67 | dpInfo.Srcs = android.FirstUniqueStrings(dpInfo.Srcs) |
| 68 | dpInfo.Aidl_include_dirs = android.FirstUniqueStrings(dpInfo.Aidl_include_dirs) |
| 69 | dpInfo.Jarjar_rules = android.FirstUniqueStrings(dpInfo.Jarjar_rules) |
| 70 | dpInfo.Jars = android.FirstUniqueStrings(dpInfo.Jars) |
| 71 | moduleInfos[name] = dpInfo |
| 72 | |
| 73 | mkProvider, ok := module.(android.AndroidMkDataProvider) |
| 74 | if !ok { |
| 75 | return |
| 76 | } |
| 77 | data := mkProvider.AndroidMk() |
| 78 | if data.Class != "" { |
| 79 | dpInfo.Classes = append(dpInfo.Classes, data.Class) |
| 80 | } |
| 81 | out := data.OutputFile.String() |
| 82 | if out != "" { |
| 83 | dpInfo.Installed_paths = append(dpInfo.Installed_paths, out) |
| 84 | } |
| 85 | dpInfo.Classes = android.FirstUniqueStrings(dpInfo.Classes) |
| 86 | dpInfo.Installed_paths = android.FirstUniqueStrings(dpInfo.Installed_paths) |
| 87 | moduleInfos[name] = dpInfo |
| 88 | }) |
| 89 | |
| 90 | jfpath := android.PathForOutput(ctx, jdepsJsonFileName).String() |
| 91 | err := createJsonFile(moduleInfos, jfpath) |
| 92 | if err != nil { |
| 93 | ctx.Errorf(err.Error()) |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | func createJsonFile(moduleInfos map[string]android.IdeInfo, jfpath string) error { |
| 98 | file, err := os.Create(jfpath) |
| 99 | if err != nil { |
| 100 | return fmt.Errorf("Failed to create file: %s, relative: %v", jdepsJsonFileName, err) |
| 101 | } |
| 102 | defer file.Close() |
| 103 | buf, err := json.MarshalIndent(moduleInfos, "", "\t") |
| 104 | if err != nil { |
| 105 | return fmt.Errorf("Write file failed: %s, relative: %v", jdepsJsonFileName, err) |
| 106 | } |
| 107 | fmt.Fprintf(file, string(buf)) |
| 108 | return nil |
| 109 | } |