Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 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 | |
| 15 | package java |
| 16 | |
| 17 | import ( |
| 18 | "encoding/json" |
| 19 | "fmt" |
Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 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 | |
| 28 | func init() { |
LaMont Jones | 0c10e4d | 2023-05-16 00:58:37 +0000 | [diff] [blame] | 29 | android.RegisterParallelSingletonType("jdeps_generator", jDepsGeneratorSingleton) |
Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | func jDepsGeneratorSingleton() android.Singleton { |
| 33 | return &jdepsGeneratorSingleton{} |
| 34 | } |
| 35 | |
| 36 | type jdepsGeneratorSingleton struct { |
Liz Kammer | 5e07d0c | 2020-06-30 14:37:22 -0700 | [diff] [blame] | 37 | outputPath android.Path |
Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | const ( |
Jim Tang | c44ba2a | 2021-11-03 15:55:01 +0800 | [diff] [blame] | 41 | jdepsJsonFileName = "module_bp_java_deps.json" |
Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 42 | ) |
| 43 | |
| 44 | func (j *jdepsGeneratorSingleton) GenerateBuildActions(ctx android.SingletonContext) { |
Jim Tang | c44ba2a | 2021-11-03 15:55:01 +0800 | [diff] [blame] | 45 | // (b/204397180) Generate module_bp_java_deps.json by default. |
Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 46 | moduleInfos := make(map[string]android.IdeInfo) |
| 47 | |
Yu Liu | 71f1ea3 | 2025-02-26 23:39:20 +0000 | [diff] [blame] | 48 | ctx.VisitAllModuleProxies(func(module android.ModuleProxy) { |
Yu Liu | f22120f | 2025-03-13 18:36:35 +0000 | [diff] [blame] | 49 | if !android.OtherModulePointerProviderOrDefault(ctx, module, android.CommonModuleInfoProvider).Enabled { |
Colin Cross | c48428a | 2019-03-22 21:39:34 -0700 | [diff] [blame] | 50 | return |
| 51 | } |
| 52 | |
Inseob Kim | 48aa223 | 2023-01-11 11:30:42 +0900 | [diff] [blame] | 53 | // Prevent including both prebuilts and matching source modules when one replaces the other. |
Yu Liu | 71f1ea3 | 2025-02-26 23:39:20 +0000 | [diff] [blame] | 54 | if !android.IsModulePreferredProxy(ctx, module) { |
Inseob Kim | 48aa223 | 2023-01-11 11:30:42 +0900 | [diff] [blame] | 55 | return |
| 56 | } |
| 57 | |
Cole Faust | 08c7f86 | 2024-08-27 15:03:59 -0700 | [diff] [blame] | 58 | ideInfoProvider, ok := android.OtherModuleProvider(ctx, module, android.IdeInfoProviderKey) |
Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 59 | if !ok { |
| 60 | return |
| 61 | } |
Cole Faust | 08c7f86 | 2024-08-27 15:03:59 -0700 | [diff] [blame] | 62 | name := ideInfoProvider.BaseModuleName |
Yu Liu | 71f1ea3 | 2025-02-26 23:39:20 +0000 | [diff] [blame] | 63 | if info, ok := android.OtherModuleProvider(ctx, module, JavaLibraryInfoProvider); ok && info.Prebuilt { |
| 64 | // TODO(b/113562217): Extract the base module name from the Import name, often the Import name |
| 65 | // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better |
| 66 | // solution to get the Import name. |
| 67 | name = android.RemoveOptionalPrebuiltPrefix(module.Name()) |
Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | dpInfo := moduleInfos[name] |
Cole Faust | 08c7f86 | 2024-08-27 15:03:59 -0700 | [diff] [blame] | 71 | dpInfo = dpInfo.Merge(ideInfoProvider) |
Colin Cross | a644c26 | 2023-11-15 11:38:36 -0800 | [diff] [blame] | 72 | dpInfo.Paths = []string{ctx.ModuleDir(module)} |
Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 73 | moduleInfos[name] = dpInfo |
| 74 | |
Yu Liu | 71f1ea3 | 2025-02-26 23:39:20 +0000 | [diff] [blame] | 75 | mkProvider, ok := android.OtherModuleProvider(ctx, module, android.AndroidMkDataInfoProvider) |
Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 76 | if !ok { |
| 77 | return |
| 78 | } |
Yu Liu | 71f1ea3 | 2025-02-26 23:39:20 +0000 | [diff] [blame] | 79 | if mkProvider.Class != "" { |
| 80 | dpInfo.Classes = append(dpInfo.Classes, mkProvider.Class) |
Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 81 | } |
shinwang | 7f1b38f | 2018-12-21 14:52:21 +0800 | [diff] [blame] | 82 | |
Yu Liu | 663e450 | 2024-08-12 18:23:59 +0000 | [diff] [blame] | 83 | if dep, ok := android.OtherModuleProvider(ctx, module, JavaInfoProvider); ok { |
Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 84 | dpInfo.Installed_paths = append(dpInfo.Installed_paths, dep.ImplementationJars.Strings()...) |
Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 85 | } |
| 86 | dpInfo.Classes = android.FirstUniqueStrings(dpInfo.Classes) |
| 87 | dpInfo.Installed_paths = android.FirstUniqueStrings(dpInfo.Installed_paths) |
| 88 | moduleInfos[name] = dpInfo |
| 89 | }) |
| 90 | |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 91 | jfpath := android.PathForOutput(ctx, jdepsJsonFileName) |
Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 92 | err := createJsonFile(moduleInfos, jfpath) |
| 93 | if err != nil { |
| 94 | ctx.Errorf(err.Error()) |
| 95 | } |
Liz Kammer | 5e07d0c | 2020-06-30 14:37:22 -0700 | [diff] [blame] | 96 | j.outputPath = jfpath |
| 97 | |
| 98 | // This is necessary to satisfy the dangling rules check as this file is written by Soong rather than a rule. |
| 99 | ctx.Build(pctx, android.BuildParams{ |
| 100 | Rule: android.Touch, |
| 101 | Output: jfpath, |
| 102 | }) |
Cole Faust | 3fbe080 | 2025-02-20 17:37:33 -0800 | [diff] [blame] | 103 | ctx.DistForGoals([]string{"general-tests", "dist_files"}, j.outputPath) |
Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 106 | func createJsonFile(moduleInfos map[string]android.IdeInfo, jfpath android.WritablePath) error { |
Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 107 | buf, err := json.MarshalIndent(moduleInfos, "", "\t") |
| 108 | if err != nil { |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 109 | return fmt.Errorf("JSON marshal of java deps failed: %s", err) |
Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 110 | } |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 111 | err = android.WriteFileToOutputDir(jfpath, buf, 0666) |
| 112 | if err != nil { |
| 113 | return fmt.Errorf("Writing java deps to %s failed: %s", jfpath.String(), err) |
| 114 | } |
Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 115 | return nil |
| 116 | } |