Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 1 | // Copyright 2020 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 rust |
| 16 | |
| 17 | import ( |
| 18 | "encoding/json" |
| 19 | "fmt" |
| 20 | "path" |
| 21 | |
| 22 | "android/soong/android" |
| 23 | ) |
| 24 | |
| 25 | // This singleton collects Rust crate definitions and generates a JSON file |
| 26 | // (${OUT_DIR}/soong/rust-project.json) which can be use by external tools, |
| 27 | // such as rust-analyzer. It does so when either make, mm, mma, mmm or mmma is |
| 28 | // called. This singleton is enabled only if SOONG_GEN_RUST_PROJECT is set. |
| 29 | // For example, |
| 30 | // |
| 31 | // $ SOONG_GEN_RUST_PROJECT=1 m nothing |
| 32 | |
| 33 | func init() { |
| 34 | android.RegisterSingletonType("rust_project_generator", rustProjectGeneratorSingleton) |
| 35 | } |
| 36 | |
| 37 | func rustProjectGeneratorSingleton() android.Singleton { |
| 38 | return &projectGeneratorSingleton{} |
| 39 | } |
| 40 | |
| 41 | type projectGeneratorSingleton struct{} |
| 42 | |
| 43 | const ( |
| 44 | // Environment variables used to control the behavior of this singleton. |
| 45 | envVariableCollectRustDeps = "SOONG_GEN_RUST_PROJECT" |
| 46 | rustProjectJsonFileName = "rust-project.json" |
| 47 | ) |
| 48 | |
| 49 | // The format of rust-project.json is not yet finalized. A current description is available at: |
| 50 | // https://github.com/rust-analyzer/rust-analyzer/blob/master/docs/user/manual.adoc#non-cargo-based-projects |
| 51 | type rustProjectDep struct { |
| 52 | Crate int `json:"crate"` |
| 53 | Name string `json:"name"` |
| 54 | } |
| 55 | |
| 56 | type rustProjectCrate struct { |
| 57 | RootModule string `json:"root_module"` |
| 58 | Edition string `json:"edition,omitempty"` |
| 59 | Deps []rustProjectDep `json:"deps"` |
| 60 | Cfgs []string `json:"cfgs"` |
| 61 | } |
| 62 | |
| 63 | type rustProjectJson struct { |
| 64 | Roots []string `json:"roots"` |
| 65 | Crates []rustProjectCrate `json:"crates"` |
| 66 | } |
| 67 | |
| 68 | // crateInfo is used during the processing to keep track of the known crates. |
| 69 | type crateInfo struct { |
| 70 | ID int |
| 71 | Deps map[string]int |
| 72 | } |
| 73 | |
| 74 | func mergeDependencies(ctx android.SingletonContext, project *rustProjectJson, |
| 75 | knownCrates map[string]crateInfo, module android.Module, |
| 76 | crate *rustProjectCrate, deps map[string]int) { |
| 77 | |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 78 | ctx.VisitDirectDeps(module, func(child android.Module) { |
Thiébaud Weksteen | 2f628ba | 2020-08-05 14:27:32 +0200 | [diff] [blame] | 79 | childId, childCrateName, ok := appendLibraryAndDeps(ctx, project, knownCrates, child) |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 80 | if !ok { |
| 81 | return |
| 82 | } |
Thiébaud Weksteen | 2f628ba | 2020-08-05 14:27:32 +0200 | [diff] [blame] | 83 | if _, ok = deps[ctx.ModuleName(child)]; ok { |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 84 | return |
| 85 | } |
Thiébaud Weksteen | 2f628ba | 2020-08-05 14:27:32 +0200 | [diff] [blame] | 86 | crate.Deps = append(crate.Deps, rustProjectDep{Crate: childId, Name: childCrateName}) |
| 87 | deps[ctx.ModuleName(child)] = childId |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 88 | }) |
| 89 | } |
| 90 | |
| 91 | // appendLibraryAndDeps creates a rustProjectCrate for the module argument and |
| 92 | // appends it to the rustProjectJson struct. It visits the dependencies of the |
| 93 | // module depth-first. If the current module is already in knownCrates, its |
Thiébaud Weksteen | 9b7b8f1 | 2020-08-03 10:44:18 +0200 | [diff] [blame] | 94 | // dependencies are merged. Returns a tuple (id, crate_name, ok). |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 95 | func appendLibraryAndDeps(ctx android.SingletonContext, project *rustProjectJson, |
| 96 | knownCrates map[string]crateInfo, module android.Module) (int, string, bool) { |
| 97 | rModule, ok := module.(*Module) |
| 98 | if !ok { |
| 99 | return 0, "", false |
| 100 | } |
| 101 | if rModule.compiler == nil { |
| 102 | return 0, "", false |
| 103 | } |
| 104 | rustLib, ok := rModule.compiler.(*libraryDecorator) |
| 105 | if !ok { |
| 106 | return 0, "", false |
| 107 | } |
Thiébaud Weksteen | 2f628ba | 2020-08-05 14:27:32 +0200 | [diff] [blame] | 108 | moduleName := ctx.ModuleName(module) |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 109 | crateName := rModule.CrateName() |
Thiébaud Weksteen | 2f628ba | 2020-08-05 14:27:32 +0200 | [diff] [blame] | 110 | if cInfo, ok := knownCrates[moduleName]; ok { |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 111 | // We have seen this crate already; merge any new dependencies. |
| 112 | crate := project.Crates[cInfo.ID] |
| 113 | mergeDependencies(ctx, project, knownCrates, module, &crate, cInfo.Deps) |
Thiébaud Weksteen | 9b7b8f1 | 2020-08-03 10:44:18 +0200 | [diff] [blame] | 114 | project.Crates[cInfo.ID] = crate |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 115 | return cInfo.ID, crateName, true |
| 116 | } |
| 117 | crate := rustProjectCrate{Deps: make([]rustProjectDep, 0), Cfgs: make([]string, 0)} |
Thiébaud Weksteen | 83ee52f | 2020-08-05 09:29:23 +0200 | [diff] [blame] | 118 | srcs := rustLib.baseCompiler.Properties.Srcs |
| 119 | if len(srcs) == 0 { |
| 120 | return 0, "", false |
| 121 | } |
| 122 | crate.RootModule = path.Join(ctx.ModuleDir(rModule), srcs[0]) |
Thiébaud Weksteen | e81c924 | 2020-08-03 10:46:28 +0200 | [diff] [blame] | 123 | crate.Edition = rustLib.baseCompiler.edition() |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 124 | |
| 125 | deps := make(map[string]int) |
| 126 | mergeDependencies(ctx, project, knownCrates, module, &crate, deps) |
| 127 | |
| 128 | id := len(project.Crates) |
Thiébaud Weksteen | 2f628ba | 2020-08-05 14:27:32 +0200 | [diff] [blame] | 129 | knownCrates[moduleName] = crateInfo{ID: id, Deps: deps} |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 130 | project.Crates = append(project.Crates, crate) |
| 131 | // rust-analyzer requires that all crates belong to at least one root: |
| 132 | // https://github.com/rust-analyzer/rust-analyzer/issues/4735. |
| 133 | project.Roots = append(project.Roots, path.Dir(crate.RootModule)) |
| 134 | return id, crateName, true |
| 135 | } |
| 136 | |
| 137 | func (r *projectGeneratorSingleton) GenerateBuildActions(ctx android.SingletonContext) { |
| 138 | if !ctx.Config().IsEnvTrue(envVariableCollectRustDeps) { |
| 139 | return |
| 140 | } |
| 141 | |
| 142 | project := rustProjectJson{} |
| 143 | knownCrates := make(map[string]crateInfo) |
| 144 | ctx.VisitAllModules(func(module android.Module) { |
| 145 | appendLibraryAndDeps(ctx, &project, knownCrates, module) |
| 146 | }) |
| 147 | |
| 148 | path := android.PathForOutput(ctx, rustProjectJsonFileName) |
| 149 | err := createJsonFile(project, path) |
| 150 | if err != nil { |
| 151 | ctx.Errorf(err.Error()) |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | func createJsonFile(project rustProjectJson, rustProjectPath android.WritablePath) error { |
| 156 | buf, err := json.MarshalIndent(project, "", " ") |
| 157 | if err != nil { |
| 158 | return fmt.Errorf("JSON marshal of rustProjectJson failed: %s", err) |
| 159 | } |
| 160 | err = android.WriteFileToOutputDir(rustProjectPath, buf, 0666) |
| 161 | if err != nil { |
| 162 | return fmt.Errorf("Writing rust-project to %s failed: %s", rustProjectPath.String(), err) |
| 163 | } |
| 164 | return nil |
| 165 | } |