blob: 07f8c43788426c7f2bc2438a119fffeee3b67be2 [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() {
LaMont Jones0c10e4d2023-05-16 00:58:37 +000029 android.RegisterParallelSingletonType("jdeps_generator", jDepsGeneratorSingleton)
Brandon Lee5d45c6f2018-08-15 15:35:38 -070030}
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
40const (
Jim Tangc44ba2a2021-11-03 15:55:01 +080041 jdepsJsonFileName = "module_bp_java_deps.json"
Brandon Lee5d45c6f2018-08-15 15:35:38 -070042)
43
44func (j *jdepsGeneratorSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Jim Tangc44ba2a2021-11-03 15:55:01 +080045 // (b/204397180) Generate module_bp_java_deps.json by default.
Brandon Lee5d45c6f2018-08-15 15:35:38 -070046 moduleInfos := make(map[string]android.IdeInfo)
47
48 ctx.VisitAllModules(func(module android.Module) {
Cole Fausta963b942024-04-11 17:43:00 -070049 if !module.Enabled(ctx) {
Colin Crossc48428a2019-03-22 21:39:34 -070050 return
51 }
52
Inseob Kim48aa2232023-01-11 11:30:42 +090053 // Prevent including both prebuilts and matching source modules when one replaces the other.
54 if !android.IsModulePreferred(module) {
55 return
56 }
57
Cole Faust08c7f862024-08-27 15:03:59 -070058 ideInfoProvider, ok := android.OtherModuleProvider(ctx, module, android.IdeInfoProviderKey)
Brandon Lee5d45c6f2018-08-15 15:35:38 -070059 if !ok {
60 return
61 }
Cole Faust08c7f862024-08-27 15:03:59 -070062 name := ideInfoProvider.BaseModuleName
Brandon Lee5d45c6f2018-08-15 15:35:38 -070063 ideModuleNameProvider, ok := module.(android.IDECustomizedModuleName)
64 if ok {
65 name = ideModuleNameProvider.IDECustomizedModuleName()
66 }
67
68 dpInfo := moduleInfos[name]
Cole Faust08c7f862024-08-27 15:03:59 -070069 dpInfo = dpInfo.Merge(ideInfoProvider)
Colin Crossa644c262023-11-15 11:38:36 -080070 dpInfo.Paths = []string{ctx.ModuleDir(module)}
Brandon Lee5d45c6f2018-08-15 15:35:38 -070071 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 }
shinwang7f1b38f2018-12-21 14:52:21 +080081
Yu Liu663e4502024-08-12 18:23:59 +000082 if dep, ok := android.OtherModuleProvider(ctx, module, JavaInfoProvider); ok {
Colin Crossdcf71b22021-02-01 13:59:03 -080083 dpInfo.Installed_paths = append(dpInfo.Installed_paths, dep.ImplementationJars.Strings()...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -070084 }
85 dpInfo.Classes = android.FirstUniqueStrings(dpInfo.Classes)
86 dpInfo.Installed_paths = android.FirstUniqueStrings(dpInfo.Installed_paths)
87 moduleInfos[name] = dpInfo
88 })
89
Colin Cross988414c2020-01-11 01:11:46 +000090 jfpath := android.PathForOutput(ctx, jdepsJsonFileName)
Brandon Lee5d45c6f2018-08-15 15:35:38 -070091 err := createJsonFile(moduleInfos, jfpath)
92 if err != nil {
93 ctx.Errorf(err.Error())
94 }
Liz Kammer5e07d0c2020-06-30 14:37:22 -070095 j.outputPath = jfpath
96
97 // This is necessary to satisfy the dangling rules check as this file is written by Soong rather than a rule.
98 ctx.Build(pctx, android.BuildParams{
99 Rule: android.Touch,
100 Output: jfpath,
101 })
Cole Faust3fbe0802025-02-20 17:37:33 -0800102 ctx.DistForGoals([]string{"general-tests", "dist_files"}, j.outputPath)
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700103}
104
Colin Cross988414c2020-01-11 01:11:46 +0000105func createJsonFile(moduleInfos map[string]android.IdeInfo, jfpath android.WritablePath) error {
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700106 buf, err := json.MarshalIndent(moduleInfos, "", "\t")
107 if err != nil {
Colin Cross988414c2020-01-11 01:11:46 +0000108 return fmt.Errorf("JSON marshal of java deps failed: %s", err)
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700109 }
Colin Cross988414c2020-01-11 01:11:46 +0000110 err = android.WriteFileToOutputDir(jfpath, buf, 0666)
111 if err != nil {
112 return fmt.Errorf("Writing java deps to %s failed: %s", jfpath.String(), err)
113 }
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700114 return nil
115}