blob: 4f636a59fb942ace3808b668b6f0609a425b1fa9 [file] [log] [blame]
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001// 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
15package java
16
17import (
18 "encoding/json"
19 "fmt"
Brandon Lee5d45c6f2018-08-15 15:35:38 -070020
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
28func init() {
29 android.RegisterSingletonType("jdeps_generator", jDepsGeneratorSingleton)
30}
31
32func jDepsGeneratorSingleton() android.Singleton {
33 return &jdepsGeneratorSingleton{}
34}
35
36type jdepsGeneratorSingleton struct {
Liz Kammer63d68792020-06-30 14:37:22 -070037 outputPath android.Path
Brandon Lee5d45c6f2018-08-15 15:35:38 -070038}
39
Liz Kammer63d68792020-06-30 14:37:22 -070040var _ android.SingletonMakeVarsProvider = (*jdepsGeneratorSingleton)(nil)
41
Brandon Lee5d45c6f2018-08-15 15:35:38 -070042const (
43 // Environment variables used to modify behavior of this singleton.
44 envVariableCollectJavaDeps = "SOONG_COLLECT_JAVA_DEPS"
45 jdepsJsonFileName = "module_bp_java_deps.json"
46)
47
48func (j *jdepsGeneratorSingleton) GenerateBuildActions(ctx android.SingletonContext) {
49 if !ctx.Config().IsEnvTrue(envVariableCollectJavaDeps) {
50 return
51 }
52
53 moduleInfos := make(map[string]android.IdeInfo)
54
55 ctx.VisitAllModules(func(module android.Module) {
Colin Crossc48428a2019-03-22 21:39:34 -070056 if !module.Enabled() {
57 return
58 }
59
Brandon Lee5d45c6f2018-08-15 15:35:38 -070060 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)
patricktu18c82ff2019-05-10 15:48:50 +080077 dpInfo.SrcJars = android.FirstUniqueStrings(dpInfo.SrcJars)
Brandon Lee5d45c6f2018-08-15 15:35:38 -070078 moduleInfos[name] = dpInfo
79
80 mkProvider, ok := module.(android.AndroidMkDataProvider)
81 if !ok {
82 return
83 }
84 data := mkProvider.AndroidMk()
85 if data.Class != "" {
86 dpInfo.Classes = append(dpInfo.Classes, data.Class)
87 }
shinwang7f1b38f2018-12-21 14:52:21 +080088
89 if dep, ok := module.(Dependency); ok {
90 dpInfo.Installed_paths = append(dpInfo.Installed_paths, dep.ImplementationJars().Strings()...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -070091 }
92 dpInfo.Classes = android.FirstUniqueStrings(dpInfo.Classes)
93 dpInfo.Installed_paths = android.FirstUniqueStrings(dpInfo.Installed_paths)
94 moduleInfos[name] = dpInfo
95 })
96
Colin Cross988414c2020-01-11 01:11:46 +000097 jfpath := android.PathForOutput(ctx, jdepsJsonFileName)
Brandon Lee5d45c6f2018-08-15 15:35:38 -070098 err := createJsonFile(moduleInfos, jfpath)
99 if err != nil {
100 ctx.Errorf(err.Error())
101 }
Liz Kammer63d68792020-06-30 14:37:22 -0700102 j.outputPath = jfpath
103
104 // This is necessary to satisfy the dangling rules check as this file is written by Soong rather than a rule.
105 ctx.Build(pctx, android.BuildParams{
106 Rule: android.Touch,
107 Output: jfpath,
108 })
109}
110
111func (j *jdepsGeneratorSingleton) MakeVars(ctx android.MakeVarsContext) {
112 if j.outputPath == nil {
113 return
114 }
115
116 ctx.DistForGoal("general-tests", j.outputPath)
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700117}
118
Colin Cross988414c2020-01-11 01:11:46 +0000119func createJsonFile(moduleInfos map[string]android.IdeInfo, jfpath android.WritablePath) error {
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700120 buf, err := json.MarshalIndent(moduleInfos, "", "\t")
121 if err != nil {
Colin Cross988414c2020-01-11 01:11:46 +0000122 return fmt.Errorf("JSON marshal of java deps failed: %s", err)
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700123 }
Colin Cross988414c2020-01-11 01:11:46 +0000124 err = android.WriteFileToOutputDir(jfpath, buf, 0666)
125 if err != nil {
126 return fmt.Errorf("Writing java deps to %s failed: %s", jfpath.String(), err)
127 }
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700128 return nil
129}