blob: 3734335175067fd1ee04b31e1f88b6b7dc79e6ed [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 Kammer5e07d0c2020-06-30 14:37:22 -070037 outputPath android.Path
Brandon Lee5d45c6f2018-08-15 15:35:38 -070038}
39
Liz Kammer5e07d0c2020-06-30 14:37:22 -070040var _ android.SingletonMakeVarsProvider = (*jdepsGeneratorSingleton)(nil)
41
Brandon Lee5d45c6f2018-08-15 15:35:38 -070042const (
Jim Tangc44ba2a2021-11-03 15:55:01 +080043 jdepsJsonFileName = "module_bp_java_deps.json"
Brandon Lee5d45c6f2018-08-15 15:35:38 -070044)
45
46func (j *jdepsGeneratorSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Jim Tangc44ba2a2021-11-03 15:55:01 +080047 // (b/204397180) Generate module_bp_java_deps.json by default.
Brandon Lee5d45c6f2018-08-15 15:35:38 -070048 moduleInfos := make(map[string]android.IdeInfo)
49
50 ctx.VisitAllModules(func(module android.Module) {
Colin Crossc48428a2019-03-22 21:39:34 -070051 if !module.Enabled() {
52 return
53 }
54
Brandon Lee5d45c6f2018-08-15 15:35:38 -070055 ideInfoProvider, ok := module.(android.IDEInfo)
56 if !ok {
57 return
58 }
59 name := ideInfoProvider.BaseModuleName()
60 ideModuleNameProvider, ok := module.(android.IDECustomizedModuleName)
61 if ok {
62 name = ideModuleNameProvider.IDECustomizedModuleName()
63 }
64
65 dpInfo := moduleInfos[name]
66 ideInfoProvider.IDEInfo(&dpInfo)
67 dpInfo.Deps = android.FirstUniqueStrings(dpInfo.Deps)
68 dpInfo.Srcs = android.FirstUniqueStrings(dpInfo.Srcs)
69 dpInfo.Aidl_include_dirs = android.FirstUniqueStrings(dpInfo.Aidl_include_dirs)
70 dpInfo.Jarjar_rules = android.FirstUniqueStrings(dpInfo.Jarjar_rules)
71 dpInfo.Jars = android.FirstUniqueStrings(dpInfo.Jars)
patricktu18c82ff2019-05-10 15:48:50 +080072 dpInfo.SrcJars = android.FirstUniqueStrings(dpInfo.SrcJars)
bralee1fbf4402020-05-21 10:11:59 +080073 dpInfo.Paths = android.FirstUniqueStrings(dpInfo.Paths)
Yikef6282022022-04-13 20:41:01 +080074 dpInfo.Static_libs = android.FirstUniqueStrings(dpInfo.Static_libs)
75 dpInfo.Libs = android.FirstUniqueStrings(dpInfo.Libs)
Brandon Lee5d45c6f2018-08-15 15:35:38 -070076 moduleInfos[name] = dpInfo
77
78 mkProvider, ok := module.(android.AndroidMkDataProvider)
79 if !ok {
80 return
81 }
82 data := mkProvider.AndroidMk()
83 if data.Class != "" {
84 dpInfo.Classes = append(dpInfo.Classes, data.Class)
85 }
shinwang7f1b38f2018-12-21 14:52:21 +080086
Colin Crossdcf71b22021-02-01 13:59:03 -080087 if ctx.ModuleHasProvider(module, JavaInfoProvider) {
88 dep := ctx.ModuleProvider(module, JavaInfoProvider).(JavaInfo)
89 dpInfo.Installed_paths = append(dpInfo.Installed_paths, dep.ImplementationJars.Strings()...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -070090 }
91 dpInfo.Classes = android.FirstUniqueStrings(dpInfo.Classes)
92 dpInfo.Installed_paths = android.FirstUniqueStrings(dpInfo.Installed_paths)
93 moduleInfos[name] = dpInfo
94 })
95
Colin Cross988414c2020-01-11 01:11:46 +000096 jfpath := android.PathForOutput(ctx, jdepsJsonFileName)
Brandon Lee5d45c6f2018-08-15 15:35:38 -070097 err := createJsonFile(moduleInfos, jfpath)
98 if err != nil {
99 ctx.Errorf(err.Error())
100 }
Liz Kammer5e07d0c2020-06-30 14:37:22 -0700101 j.outputPath = jfpath
102
103 // This is necessary to satisfy the dangling rules check as this file is written by Soong rather than a rule.
104 ctx.Build(pctx, android.BuildParams{
105 Rule: android.Touch,
106 Output: jfpath,
107 })
108}
109
110func (j *jdepsGeneratorSingleton) MakeVars(ctx android.MakeVarsContext) {
111 if j.outputPath == nil {
112 return
113 }
114
115 ctx.DistForGoal("general-tests", j.outputPath)
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700116}
117
Colin Cross988414c2020-01-11 01:11:46 +0000118func createJsonFile(moduleInfos map[string]android.IdeInfo, jfpath android.WritablePath) error {
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700119 buf, err := json.MarshalIndent(moduleInfos, "", "\t")
120 if err != nil {
Colin Cross988414c2020-01-11 01:11:46 +0000121 return fmt.Errorf("JSON marshal of java deps failed: %s", err)
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700122 }
Colin Cross988414c2020-01-11 01:11:46 +0000123 err = android.WriteFileToOutputDir(jfpath, buf, 0666)
124 if err != nil {
125 return fmt.Errorf("Writing java deps to %s failed: %s", jfpath.String(), err)
126 }
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700127 return nil
128}