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" |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 20 | |
| 21 | "android/soong/android" |
| 22 | ) |
| 23 | |
| 24 | // This singleton collects Rust crate definitions and generates a JSON file |
| 25 | // (${OUT_DIR}/soong/rust-project.json) which can be use by external tools, |
| 26 | // such as rust-analyzer. It does so when either make, mm, mma, mmm or mmma is |
| 27 | // called. This singleton is enabled only if SOONG_GEN_RUST_PROJECT is set. |
| 28 | // For example, |
| 29 | // |
| 30 | // $ SOONG_GEN_RUST_PROJECT=1 m nothing |
| 31 | |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 32 | const ( |
| 33 | // Environment variables used to control the behavior of this singleton. |
| 34 | envVariableCollectRustDeps = "SOONG_GEN_RUST_PROJECT" |
| 35 | rustProjectJsonFileName = "rust-project.json" |
| 36 | ) |
| 37 | |
| 38 | // The format of rust-project.json is not yet finalized. A current description is available at: |
| 39 | // https://github.com/rust-analyzer/rust-analyzer/blob/master/docs/user/manual.adoc#non-cargo-based-projects |
| 40 | type rustProjectDep struct { |
Thiébaud Weksteen | 3805f5c | 2020-09-28 14:42:07 +0200 | [diff] [blame] | 41 | // 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] | 42 | Crate int `json:"crate"` |
| 43 | Name string `json:"name"` |
| 44 | } |
| 45 | |
| 46 | type rustProjectCrate struct { |
Thiébaud Weksteen | ee6a89b | 2021-02-25 16:30:57 +0100 | [diff] [blame] | 47 | DisplayName string `json:"display_name"` |
| 48 | RootModule string `json:"root_module"` |
| 49 | Edition string `json:"edition,omitempty"` |
| 50 | Deps []rustProjectDep `json:"deps"` |
Thiébaud Weksteen | e8b0ee7 | 2021-03-25 09:26:07 +0100 | [diff] [blame] | 51 | Cfg []string `json:"cfg"` |
Thiébaud Weksteen | ee6a89b | 2021-02-25 16:30:57 +0100 | [diff] [blame] | 52 | Env map[string]string `json:"env"` |
Seth Moore | af96f99 | 2021-10-06 10:45:34 -0700 | [diff] [blame] | 53 | ProcMacro bool `json:"is_proc_macro"` |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | type rustProjectJson struct { |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 57 | Crates []rustProjectCrate `json:"crates"` |
| 58 | } |
| 59 | |
| 60 | // crateInfo is used during the processing to keep track of the known crates. |
| 61 | type crateInfo struct { |
Matthew Maurer | db72f7e | 2023-11-21 00:20:02 +0000 | [diff] [blame] | 62 | Idx int // Index of the crate in rustProjectJson.Crates slice. |
| 63 | Deps map[string]int // The keys are the module names and not the crate names. |
| 64 | Device bool // True if the crate at idx was a device crate |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 65 | } |
| 66 | |
Thiébaud Weksteen | 3805f5c | 2020-09-28 14:42:07 +0200 | [diff] [blame] | 67 | type projectGeneratorSingleton struct { |
| 68 | project rustProjectJson |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [diff] [blame] | 69 | knownCrates map[string]crateInfo // Keys are module names. |
Thiébaud Weksteen | 3805f5c | 2020-09-28 14:42:07 +0200 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | func rustProjectGeneratorSingleton() android.Singleton { |
| 73 | return &projectGeneratorSingleton{} |
| 74 | } |
| 75 | |
| 76 | func init() { |
LaMont Jones | 0c10e4d | 2023-05-16 00:58:37 +0000 | [diff] [blame] | 77 | android.RegisterParallelSingletonType("rust_project_generator", rustProjectGeneratorSingleton) |
Thiébaud Weksteen | 3805f5c | 2020-09-28 14:42:07 +0200 | [diff] [blame] | 78 | } |
| 79 | |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [diff] [blame] | 80 | // mergeDependencies visits all the dependencies for module and updates crate and deps |
| 81 | // with any new dependency. |
| 82 | func (singleton *projectGeneratorSingleton) mergeDependencies(ctx android.SingletonContext, |
| 83 | module *Module, crate *rustProjectCrate, deps map[string]int) { |
| 84 | |
| 85 | ctx.VisitDirectDeps(module, func(child android.Module) { |
Thiébaud Weksteen | 3c5905b | 2020-11-25 16:09:32 +0100 | [diff] [blame] | 86 | // Skip intra-module dependencies (i.e., generated-source library depending on the source variant). |
| 87 | if module.Name() == child.Name() { |
| 88 | return |
| 89 | } |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [diff] [blame] | 90 | // Skip unsupported modules. |
Matthew Maurer | db72f7e | 2023-11-21 00:20:02 +0000 | [diff] [blame] | 91 | rChild, ok := isModuleSupported(ctx, child) |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [diff] [blame] | 92 | if !ok { |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 93 | return |
| 94 | } |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [diff] [blame] | 95 | // For unknown dependency, add it first. |
| 96 | var childId int |
| 97 | cInfo, known := singleton.knownCrates[rChild.Name()] |
| 98 | if !known { |
Matthew Maurer | db72f7e | 2023-11-21 00:20:02 +0000 | [diff] [blame] | 99 | childId, ok = singleton.addCrate(ctx, rChild, make(map[string]int)) |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [diff] [blame] | 100 | if !ok { |
| 101 | return |
| 102 | } |
| 103 | } else { |
| 104 | childId = cInfo.Idx |
| 105 | } |
| 106 | // Is this dependency known already? |
| 107 | if _, ok = deps[child.Name()]; ok { |
| 108 | return |
| 109 | } |
| 110 | crate.Deps = append(crate.Deps, rustProjectDep{Crate: childId, Name: rChild.CrateName()}) |
| 111 | deps[child.Name()] = childId |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 112 | }) |
| 113 | } |
| 114 | |
Matthew Maurer | db72f7e | 2023-11-21 00:20:02 +0000 | [diff] [blame] | 115 | // isModuleSupported returns the RustModule if the module |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [diff] [blame] | 116 | // should be considered for inclusion in rust-project.json. |
Matthew Maurer | db72f7e | 2023-11-21 00:20:02 +0000 | [diff] [blame] | 117 | func isModuleSupported(ctx android.SingletonContext, module android.Module) (*Module, bool) { |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 118 | rModule, ok := module.(*Module) |
| 119 | if !ok { |
Matthew Maurer | db72f7e | 2023-11-21 00:20:02 +0000 | [diff] [blame] | 120 | return nil, false |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 121 | } |
Matthew Maurer | 5a3c71c | 2023-11-27 17:51:58 +0000 | [diff] [blame^] | 122 | if !rModule.Enabled() { |
| 123 | return nil, false |
| 124 | } |
Matthew Maurer | db72f7e | 2023-11-21 00:20:02 +0000 | [diff] [blame] | 125 | return rModule, true |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | // addCrate adds a crate to singleton.project.Crates ensuring that required |
| 129 | // dependencies are also added. It returns the index of the new crate in |
| 130 | // singleton.project.Crates |
Matthew Maurer | db72f7e | 2023-11-21 00:20:02 +0000 | [diff] [blame] | 131 | func (singleton *projectGeneratorSingleton) addCrate(ctx android.SingletonContext, rModule *Module, deps map[string]int) (int, bool) { |
| 132 | rootModule, err := rModule.compiler.checkedCrateRootPath() |
| 133 | if err != nil { |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [diff] [blame] | 134 | return 0, false |
Thiébaud Weksteen | 83ee52f | 2020-08-05 09:29:23 +0200 | [diff] [blame] | 135 | } |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [diff] [blame] | 136 | |
Seth Moore | af96f99 | 2021-10-06 10:45:34 -0700 | [diff] [blame] | 137 | _, procMacro := rModule.compiler.(*procMacroDecorator) |
| 138 | |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [diff] [blame] | 139 | crate := rustProjectCrate{ |
| 140 | DisplayName: rModule.Name(), |
Matthew Maurer | db72f7e | 2023-11-21 00:20:02 +0000 | [diff] [blame] | 141 | RootModule: rootModule.String(), |
| 142 | Edition: rModule.compiler.edition(), |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [diff] [blame] | 143 | Deps: make([]rustProjectDep, 0), |
Thiébaud Weksteen | e8b0ee7 | 2021-03-25 09:26:07 +0100 | [diff] [blame] | 144 | Cfg: make([]string, 0), |
Thiébaud Weksteen | ee6a89b | 2021-02-25 16:30:57 +0100 | [diff] [blame] | 145 | Env: make(map[string]string), |
Seth Moore | af96f99 | 2021-10-06 10:45:34 -0700 | [diff] [blame] | 146 | ProcMacro: procMacro, |
Thiébaud Weksteen | ee6a89b | 2021-02-25 16:30:57 +0100 | [diff] [blame] | 147 | } |
| 148 | |
Matthew Maurer | db72f7e | 2023-11-21 00:20:02 +0000 | [diff] [blame] | 149 | if rModule.compiler.cargoOutDir().Valid() { |
| 150 | crate.Env["OUT_DIR"] = rModule.compiler.cargoOutDir().String() |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [diff] [blame] | 151 | } |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 152 | |
Matthew Maurer | db72f7e | 2023-11-21 00:20:02 +0000 | [diff] [blame] | 153 | for _, feature := range rModule.compiler.features() { |
Thiébaud Weksteen | e8b0ee7 | 2021-03-25 09:26:07 +0100 | [diff] [blame] | 154 | crate.Cfg = append(crate.Cfg, "feature=\""+feature+"\"") |
| 155 | } |
| 156 | |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [diff] [blame] | 157 | singleton.mergeDependencies(ctx, rModule, &crate, deps) |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 158 | |
Matthew Maurer | db72f7e | 2023-11-21 00:20:02 +0000 | [diff] [blame] | 159 | var idx int |
| 160 | if cInfo, ok := singleton.knownCrates[rModule.Name()]; ok { |
| 161 | idx = cInfo.Idx |
| 162 | singleton.project.Crates[idx] = crate |
| 163 | } else { |
| 164 | idx = len(singleton.project.Crates) |
| 165 | singleton.project.Crates = append(singleton.project.Crates, crate) |
| 166 | } |
| 167 | singleton.knownCrates[rModule.Name()] = crateInfo{Idx: idx, Deps: deps, Device: rModule.Device()} |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [diff] [blame] | 168 | return idx, true |
| 169 | } |
| 170 | |
| 171 | // appendCrateAndDependencies creates a rustProjectCrate for the module argument and appends it to singleton.project. |
| 172 | // It visits the dependencies of the module depth-first so the dependency ID can be added to the current module. If the |
| 173 | // current module is already in singleton.knownCrates, its dependencies are merged. |
| 174 | func (singleton *projectGeneratorSingleton) appendCrateAndDependencies(ctx android.SingletonContext, module android.Module) { |
Matthew Maurer | db72f7e | 2023-11-21 00:20:02 +0000 | [diff] [blame] | 175 | rModule, ok := isModuleSupported(ctx, module) |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [diff] [blame] | 176 | if !ok { |
| 177 | return |
| 178 | } |
| 179 | // If we have seen this crate already; merge any new dependencies. |
| 180 | if cInfo, ok := singleton.knownCrates[module.Name()]; ok { |
Matthew Maurer | db72f7e | 2023-11-21 00:20:02 +0000 | [diff] [blame] | 181 | // If we have a new device variant, override the old one |
| 182 | if !cInfo.Device && rModule.Device() { |
| 183 | singleton.addCrate(ctx, rModule, cInfo.Deps) |
| 184 | return |
| 185 | } |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [diff] [blame] | 186 | crate := singleton.project.Crates[cInfo.Idx] |
| 187 | singleton.mergeDependencies(ctx, rModule, &crate, cInfo.Deps) |
| 188 | singleton.project.Crates[cInfo.Idx] = crate |
| 189 | return |
| 190 | } |
Matthew Maurer | db72f7e | 2023-11-21 00:20:02 +0000 | [diff] [blame] | 191 | singleton.addCrate(ctx, rModule, make(map[string]int)) |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 192 | } |
| 193 | |
Thiébaud Weksteen | 3805f5c | 2020-09-28 14:42:07 +0200 | [diff] [blame] | 194 | func (singleton *projectGeneratorSingleton) GenerateBuildActions(ctx android.SingletonContext) { |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 195 | if !ctx.Config().IsEnvTrue(envVariableCollectRustDeps) { |
| 196 | return |
| 197 | } |
| 198 | |
Thiébaud Weksteen | 3805f5c | 2020-09-28 14:42:07 +0200 | [diff] [blame] | 199 | singleton.knownCrates = make(map[string]crateInfo) |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 200 | ctx.VisitAllModules(func(module android.Module) { |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [diff] [blame] | 201 | singleton.appendCrateAndDependencies(ctx, module) |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 202 | }) |
| 203 | |
| 204 | path := android.PathForOutput(ctx, rustProjectJsonFileName) |
Thiébaud Weksteen | 3805f5c | 2020-09-28 14:42:07 +0200 | [diff] [blame] | 205 | err := createJsonFile(singleton.project, path) |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 206 | if err != nil { |
| 207 | ctx.Errorf(err.Error()) |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | func createJsonFile(project rustProjectJson, rustProjectPath android.WritablePath) error { |
| 212 | buf, err := json.MarshalIndent(project, "", " ") |
| 213 | if err != nil { |
| 214 | return fmt.Errorf("JSON marshal of rustProjectJson failed: %s", err) |
| 215 | } |
| 216 | err = android.WriteFileToOutputDir(rustProjectPath, buf, 0666) |
| 217 | if err != nil { |
| 218 | return fmt.Errorf("Writing rust-project to %s failed: %s", rustProjectPath.String(), err) |
| 219 | } |
| 220 | return nil |
| 221 | } |