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 | |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 33 | const ( |
| 34 | // Environment variables used to control the behavior of this singleton. |
| 35 | envVariableCollectRustDeps = "SOONG_GEN_RUST_PROJECT" |
| 36 | rustProjectJsonFileName = "rust-project.json" |
| 37 | ) |
| 38 | |
| 39 | // The format of rust-project.json is not yet finalized. A current description is available at: |
| 40 | // https://github.com/rust-analyzer/rust-analyzer/blob/master/docs/user/manual.adoc#non-cargo-based-projects |
| 41 | type rustProjectDep struct { |
Thiébaud Weksteen | 3805f5c | 2020-09-28 14:42:07 +0200 | [diff] [blame^] | 42 | // The Crate attribute is the index of the dependency in the Crates array in rustProjectJson. |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 43 | Crate int `json:"crate"` |
| 44 | Name string `json:"name"` |
| 45 | } |
| 46 | |
| 47 | type rustProjectCrate struct { |
| 48 | RootModule string `json:"root_module"` |
| 49 | Edition string `json:"edition,omitempty"` |
| 50 | Deps []rustProjectDep `json:"deps"` |
| 51 | Cfgs []string `json:"cfgs"` |
| 52 | } |
| 53 | |
| 54 | type rustProjectJson struct { |
| 55 | Roots []string `json:"roots"` |
| 56 | Crates []rustProjectCrate `json:"crates"` |
| 57 | } |
| 58 | |
| 59 | // crateInfo is used during the processing to keep track of the known crates. |
| 60 | type crateInfo struct { |
| 61 | ID int |
| 62 | Deps map[string]int |
| 63 | } |
| 64 | |
Thiébaud Weksteen | 3805f5c | 2020-09-28 14:42:07 +0200 | [diff] [blame^] | 65 | type projectGeneratorSingleton struct { |
| 66 | project rustProjectJson |
| 67 | knownCrates map[string]crateInfo |
| 68 | } |
| 69 | |
| 70 | func rustProjectGeneratorSingleton() android.Singleton { |
| 71 | return &projectGeneratorSingleton{} |
| 72 | } |
| 73 | |
| 74 | func init() { |
| 75 | android.RegisterSingletonType("rust_project_generator", rustProjectGeneratorSingleton) |
| 76 | } |
| 77 | |
| 78 | func (singleton *projectGeneratorSingleton) mergeDependencies(ctx android.SingletonContext, |
| 79 | module android.Module, crate *rustProjectCrate, deps map[string]int) { |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 80 | |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 81 | ctx.VisitDirectDeps(module, func(child android.Module) { |
Thiébaud Weksteen | 3805f5c | 2020-09-28 14:42:07 +0200 | [diff] [blame^] | 82 | childId, childCrateName, ok := singleton.appendLibraryAndDeps(ctx, child) |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 83 | if !ok { |
| 84 | return |
| 85 | } |
Thiébaud Weksteen | 2f628ba | 2020-08-05 14:27:32 +0200 | [diff] [blame] | 86 | if _, ok = deps[ctx.ModuleName(child)]; ok { |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 87 | return |
| 88 | } |
Thiébaud Weksteen | 2f628ba | 2020-08-05 14:27:32 +0200 | [diff] [blame] | 89 | crate.Deps = append(crate.Deps, rustProjectDep{Crate: childId, Name: childCrateName}) |
| 90 | deps[ctx.ModuleName(child)] = childId |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 91 | }) |
| 92 | } |
| 93 | |
Thiébaud Weksteen | 3805f5c | 2020-09-28 14:42:07 +0200 | [diff] [blame^] | 94 | // appendLibraryAndDeps creates a rustProjectCrate for the module argument and appends it to singleton.project. |
| 95 | // It visits the dependencies of the module depth-first so the dependency ID can be added to the current module. If the |
| 96 | // current module is already in singleton.knownCrates, its dependencies are merged. Returns a tuple (id, crate_name, ok). |
| 97 | func (singleton *projectGeneratorSingleton) appendLibraryAndDeps(ctx android.SingletonContext, module android.Module) (int, string, bool) { |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 98 | rModule, ok := module.(*Module) |
| 99 | if !ok { |
| 100 | return 0, "", false |
| 101 | } |
| 102 | if rModule.compiler == nil { |
| 103 | return 0, "", false |
| 104 | } |
| 105 | rustLib, ok := rModule.compiler.(*libraryDecorator) |
| 106 | if !ok { |
| 107 | return 0, "", false |
| 108 | } |
Thiébaud Weksteen | 2f628ba | 2020-08-05 14:27:32 +0200 | [diff] [blame] | 109 | moduleName := ctx.ModuleName(module) |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 110 | crateName := rModule.CrateName() |
Thiébaud Weksteen | 3805f5c | 2020-09-28 14:42:07 +0200 | [diff] [blame^] | 111 | if cInfo, ok := singleton.knownCrates[moduleName]; ok { |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 112 | // We have seen this crate already; merge any new dependencies. |
Thiébaud Weksteen | 3805f5c | 2020-09-28 14:42:07 +0200 | [diff] [blame^] | 113 | crate := singleton.project.Crates[cInfo.ID] |
| 114 | singleton.mergeDependencies(ctx, module, &crate, cInfo.Deps) |
| 115 | singleton.project.Crates[cInfo.ID] = crate |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 116 | return cInfo.ID, crateName, true |
| 117 | } |
| 118 | crate := rustProjectCrate{Deps: make([]rustProjectDep, 0), Cfgs: make([]string, 0)} |
Thiébaud Weksteen | 83ee52f | 2020-08-05 09:29:23 +0200 | [diff] [blame] | 119 | srcs := rustLib.baseCompiler.Properties.Srcs |
| 120 | if len(srcs) == 0 { |
| 121 | return 0, "", false |
| 122 | } |
| 123 | crate.RootModule = path.Join(ctx.ModuleDir(rModule), srcs[0]) |
Thiébaud Weksteen | e81c924 | 2020-08-03 10:46:28 +0200 | [diff] [blame] | 124 | crate.Edition = rustLib.baseCompiler.edition() |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 125 | |
| 126 | deps := make(map[string]int) |
Thiébaud Weksteen | 3805f5c | 2020-09-28 14:42:07 +0200 | [diff] [blame^] | 127 | singleton.mergeDependencies(ctx, module, &crate, deps) |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 128 | |
Thiébaud Weksteen | 3805f5c | 2020-09-28 14:42:07 +0200 | [diff] [blame^] | 129 | id := len(singleton.project.Crates) |
| 130 | singleton.knownCrates[moduleName] = crateInfo{ID: id, Deps: deps} |
| 131 | singleton.project.Crates = append(singleton.project.Crates, crate) |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 132 | // rust-analyzer requires that all crates belong to at least one root: |
| 133 | // https://github.com/rust-analyzer/rust-analyzer/issues/4735. |
Thiébaud Weksteen | 3805f5c | 2020-09-28 14:42:07 +0200 | [diff] [blame^] | 134 | singleton.project.Roots = append(singleton.project.Roots, path.Dir(crate.RootModule)) |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 135 | return id, crateName, true |
| 136 | } |
| 137 | |
Thiébaud Weksteen | 3805f5c | 2020-09-28 14:42:07 +0200 | [diff] [blame^] | 138 | func (singleton *projectGeneratorSingleton) GenerateBuildActions(ctx android.SingletonContext) { |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 139 | if !ctx.Config().IsEnvTrue(envVariableCollectRustDeps) { |
| 140 | return |
| 141 | } |
| 142 | |
Thiébaud Weksteen | 3805f5c | 2020-09-28 14:42:07 +0200 | [diff] [blame^] | 143 | singleton.knownCrates = make(map[string]crateInfo) |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 144 | ctx.VisitAllModules(func(module android.Module) { |
Thiébaud Weksteen | 3805f5c | 2020-09-28 14:42:07 +0200 | [diff] [blame^] | 145 | singleton.appendLibraryAndDeps(ctx, module) |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 146 | }) |
| 147 | |
| 148 | path := android.PathForOutput(ctx, rustProjectJsonFileName) |
Thiébaud Weksteen | 3805f5c | 2020-09-28 14:42:07 +0200 | [diff] [blame^] | 149 | err := createJsonFile(singleton.project, path) |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 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 | } |