2018-08-16 00:35:38 +02:00
|
|
|
// Copyright 2018 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package java
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"android/soong/android"
|
|
|
|
)
|
|
|
|
|
|
|
|
// This singleton generates android java dependency into to a json file. It does so for each
|
|
|
|
// blueprint Android.bp resulting in a java.Module when either make, mm, mma, mmm or mmma is
|
|
|
|
// called. Dependency info file is generated in $OUT/module_bp_java_depend.json.
|
|
|
|
|
|
|
|
func init() {
|
2023-05-16 02:58:37 +02:00
|
|
|
android.RegisterParallelSingletonType("jdeps_generator", jDepsGeneratorSingleton)
|
2018-08-16 00:35:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func jDepsGeneratorSingleton() android.Singleton {
|
|
|
|
return &jdepsGeneratorSingleton{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type jdepsGeneratorSingleton struct {
|
2020-06-30 23:37:22 +02:00
|
|
|
outputPath android.Path
|
2018-08-16 00:35:38 +02:00
|
|
|
}
|
|
|
|
|
2020-06-30 23:37:22 +02:00
|
|
|
var _ android.SingletonMakeVarsProvider = (*jdepsGeneratorSingleton)(nil)
|
|
|
|
|
2018-08-16 00:35:38 +02:00
|
|
|
const (
|
2021-11-03 08:55:01 +01:00
|
|
|
jdepsJsonFileName = "module_bp_java_deps.json"
|
2018-08-16 00:35:38 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func (j *jdepsGeneratorSingleton) GenerateBuildActions(ctx android.SingletonContext) {
|
2021-11-03 08:55:01 +01:00
|
|
|
// (b/204397180) Generate module_bp_java_deps.json by default.
|
2018-08-16 00:35:38 +02:00
|
|
|
moduleInfos := make(map[string]android.IdeInfo)
|
|
|
|
|
|
|
|
ctx.VisitAllModules(func(module android.Module) {
|
2019-03-23 05:39:34 +01:00
|
|
|
if !module.Enabled() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-11 03:30:42 +01:00
|
|
|
// Prevent including both prebuilts and matching source modules when one replaces the other.
|
|
|
|
if !android.IsModulePreferred(module) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-16 00:35:38 +02:00
|
|
|
ideInfoProvider, ok := module.(android.IDEInfo)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
name := ideInfoProvider.BaseModuleName()
|
|
|
|
ideModuleNameProvider, ok := module.(android.IDECustomizedModuleName)
|
|
|
|
if ok {
|
|
|
|
name = ideModuleNameProvider.IDECustomizedModuleName()
|
|
|
|
}
|
|
|
|
|
|
|
|
dpInfo := moduleInfos[name]
|
|
|
|
ideInfoProvider.IDEInfo(&dpInfo)
|
|
|
|
dpInfo.Deps = android.FirstUniqueStrings(dpInfo.Deps)
|
|
|
|
dpInfo.Srcs = android.FirstUniqueStrings(dpInfo.Srcs)
|
|
|
|
dpInfo.Aidl_include_dirs = android.FirstUniqueStrings(dpInfo.Aidl_include_dirs)
|
|
|
|
dpInfo.Jarjar_rules = android.FirstUniqueStrings(dpInfo.Jarjar_rules)
|
|
|
|
dpInfo.Jars = android.FirstUniqueStrings(dpInfo.Jars)
|
2019-05-10 09:48:50 +02:00
|
|
|
dpInfo.SrcJars = android.FirstUniqueStrings(dpInfo.SrcJars)
|
2023-11-15 20:38:36 +01:00
|
|
|
dpInfo.Paths = []string{ctx.ModuleDir(module)}
|
2022-04-13 14:41:01 +02:00
|
|
|
dpInfo.Static_libs = android.FirstUniqueStrings(dpInfo.Static_libs)
|
|
|
|
dpInfo.Libs = android.FirstUniqueStrings(dpInfo.Libs)
|
2018-08-16 00:35:38 +02:00
|
|
|
moduleInfos[name] = dpInfo
|
|
|
|
|
|
|
|
mkProvider, ok := module.(android.AndroidMkDataProvider)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
data := mkProvider.AndroidMk()
|
|
|
|
if data.Class != "" {
|
|
|
|
dpInfo.Classes = append(dpInfo.Classes, data.Class)
|
|
|
|
}
|
2018-12-21 07:52:21 +01:00
|
|
|
|
2023-12-14 23:46:23 +01:00
|
|
|
if dep, ok := android.SingletonModuleProvider(ctx, module, JavaInfoProvider); ok {
|
2021-02-01 22:59:03 +01:00
|
|
|
dpInfo.Installed_paths = append(dpInfo.Installed_paths, dep.ImplementationJars.Strings()...)
|
2018-08-16 00:35:38 +02:00
|
|
|
}
|
|
|
|
dpInfo.Classes = android.FirstUniqueStrings(dpInfo.Classes)
|
|
|
|
dpInfo.Installed_paths = android.FirstUniqueStrings(dpInfo.Installed_paths)
|
|
|
|
moduleInfos[name] = dpInfo
|
|
|
|
})
|
|
|
|
|
2020-01-11 02:11:46 +01:00
|
|
|
jfpath := android.PathForOutput(ctx, jdepsJsonFileName)
|
2018-08-16 00:35:38 +02:00
|
|
|
err := createJsonFile(moduleInfos, jfpath)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Errorf(err.Error())
|
|
|
|
}
|
2020-06-30 23:37:22 +02:00
|
|
|
j.outputPath = jfpath
|
|
|
|
|
|
|
|
// This is necessary to satisfy the dangling rules check as this file is written by Soong rather than a rule.
|
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: android.Touch,
|
|
|
|
Output: jfpath,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *jdepsGeneratorSingleton) MakeVars(ctx android.MakeVarsContext) {
|
|
|
|
if j.outputPath == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.DistForGoal("general-tests", j.outputPath)
|
2018-08-16 00:35:38 +02:00
|
|
|
}
|
|
|
|
|
2020-01-11 02:11:46 +01:00
|
|
|
func createJsonFile(moduleInfos map[string]android.IdeInfo, jfpath android.WritablePath) error {
|
|
|
|
buf, err := json.MarshalIndent(moduleInfos, "", "\t")
|
2018-08-16 00:35:38 +02:00
|
|
|
if err != nil {
|
2020-01-11 02:11:46 +01:00
|
|
|
return fmt.Errorf("JSON marshal of java deps failed: %s", err)
|
2018-08-16 00:35:38 +02:00
|
|
|
}
|
2020-01-11 02:11:46 +01:00
|
|
|
err = android.WriteFileToOutputDir(jfpath, buf, 0666)
|
2018-08-16 00:35:38 +02:00
|
|
|
if err != nil {
|
2020-01-11 02:11:46 +01:00
|
|
|
return fmt.Errorf("Writing java deps to %s failed: %s", jfpath.String(), err)
|
2018-08-16 00:35:38 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|