• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
15package java
16
17import (
18	"encoding/json"
19	"fmt"
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
28func init() {
29	android.RegisterSingletonType("jdeps_generator", jDepsGeneratorSingleton)
30}
31
32func jDepsGeneratorSingleton() android.Singleton {
33	return &jdepsGeneratorSingleton{}
34}
35
36type jdepsGeneratorSingleton struct {
37	outputPath android.Path
38}
39
40var _ android.SingletonMakeVarsProvider = (*jdepsGeneratorSingleton)(nil)
41
42const (
43	jdepsJsonFileName = "module_bp_java_deps.json"
44)
45
46func (j *jdepsGeneratorSingleton) GenerateBuildActions(ctx android.SingletonContext) {
47	// (b/204397180) Generate module_bp_java_deps.json by default.
48	moduleInfos := make(map[string]android.IdeInfo)
49
50	ctx.VisitAllModules(func(module android.Module) {
51		if !module.Enabled() {
52			return
53		}
54
55		// Prevent including both prebuilts and matching source modules when one replaces the other.
56		if !android.IsModulePreferred(module) {
57			return
58		}
59
60		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)
77		dpInfo.SrcJars = android.FirstUniqueStrings(dpInfo.SrcJars)
78		dpInfo.Paths = android.FirstUniqueStrings(dpInfo.Paths)
79		dpInfo.Static_libs = android.FirstUniqueStrings(dpInfo.Static_libs)
80		dpInfo.Libs = android.FirstUniqueStrings(dpInfo.Libs)
81		moduleInfos[name] = dpInfo
82
83		mkProvider, ok := module.(android.AndroidMkDataProvider)
84		if !ok {
85			return
86		}
87		data := mkProvider.AndroidMk()
88		if data.Class != "" {
89			dpInfo.Classes = append(dpInfo.Classes, data.Class)
90		}
91
92		if ctx.ModuleHasProvider(module, JavaInfoProvider) {
93			dep := ctx.ModuleProvider(module, JavaInfoProvider).(JavaInfo)
94			dpInfo.Installed_paths = append(dpInfo.Installed_paths, dep.ImplementationJars.Strings()...)
95		}
96		dpInfo.Classes = android.FirstUniqueStrings(dpInfo.Classes)
97		dpInfo.Installed_paths = android.FirstUniqueStrings(dpInfo.Installed_paths)
98		moduleInfos[name] = dpInfo
99	})
100
101	jfpath := android.PathForOutput(ctx, jdepsJsonFileName)
102	err := createJsonFile(moduleInfos, jfpath)
103	if err != nil {
104		ctx.Errorf(err.Error())
105	}
106	j.outputPath = jfpath
107
108	// This is necessary to satisfy the dangling rules check as this file is written by Soong rather than a rule.
109	ctx.Build(pctx, android.BuildParams{
110		Rule:   android.Touch,
111		Output: jfpath,
112	})
113}
114
115func (j *jdepsGeneratorSingleton) MakeVars(ctx android.MakeVarsContext) {
116	if j.outputPath == nil {
117		return
118	}
119
120	ctx.DistForGoal("general-tests", j.outputPath)
121}
122
123func createJsonFile(moduleInfos map[string]android.IdeInfo, jfpath android.WritablePath) error {
124	buf, err := json.MarshalIndent(moduleInfos, "", "\t")
125	if err != nil {
126		return fmt.Errorf("JSON marshal of java deps failed: %s", err)
127	}
128	err = android.WriteFileToOutputDir(jfpath, buf, 0666)
129	if err != nil {
130		return fmt.Errorf("Writing java deps to %s failed: %s", jfpath.String(), err)
131	}
132	return nil
133}
134