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