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 { |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [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 | Cfgs []string `json:"cfgs"` |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | type rustProjectJson struct { |
| 56 | Roots []string `json:"roots"` |
| 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 { |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [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. |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 64 | } |
| 65 | |
Thiébaud Weksteen | 3805f5c | 2020-09-28 14:42:07 +0200 | [diff] [blame] | 66 | type projectGeneratorSingleton struct { |
| 67 | project rustProjectJson |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [diff] [blame^] | 68 | knownCrates map[string]crateInfo // Keys are module names. |
Thiébaud Weksteen | 3805f5c | 2020-09-28 14:42:07 +0200 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | func rustProjectGeneratorSingleton() android.Singleton { |
| 72 | return &projectGeneratorSingleton{} |
| 73 | } |
| 74 | |
| 75 | func init() { |
| 76 | android.RegisterSingletonType("rust_project_generator", rustProjectGeneratorSingleton) |
| 77 | } |
| 78 | |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [diff] [blame^] | 79 | // sourceProviderVariantSource returns the path to the source file if this |
| 80 | // module variant should be used as a priority. |
| 81 | // |
| 82 | // SourceProvider modules may have multiple variants considered as source |
| 83 | // (e.g., x86_64 and armv8). For a module available on device, use the source |
| 84 | // generated for the target. For a host-only module, use the source generated |
| 85 | // for the host. |
| 86 | func sourceProviderVariantSource(ctx android.SingletonContext, rModule *Module) (string, bool) { |
Thiébaud Weksteen | 064f6e9 | 2020-12-03 20:58:32 +0100 | [diff] [blame] | 87 | rustLib, ok := rModule.compiler.(*libraryDecorator) |
| 88 | if !ok { |
| 89 | return "", false |
| 90 | } |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [diff] [blame^] | 91 | if rustLib.source() { |
| 92 | switch rModule.hod { |
| 93 | case android.HostSupported, android.HostSupportedNoCross: |
| 94 | if rModule.Target().String() == ctx.Config().BuildOSTarget.String() { |
| 95 | src := rustLib.sourceProvider.Srcs()[0] |
| 96 | return src.String(), true |
| 97 | } |
| 98 | default: |
| 99 | if rModule.Target().String() == ctx.Config().AndroidFirstDeviceTarget.String() { |
| 100 | src := rustLib.sourceProvider.Srcs()[0] |
| 101 | return src.String(), true |
| 102 | } |
Thiébaud Weksteen | a6351ca | 2020-09-29 09:53:21 +0200 | [diff] [blame] | 103 | } |
| 104 | } |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [diff] [blame^] | 105 | return "", false |
Thiébaud Weksteen | a6351ca | 2020-09-29 09:53:21 +0200 | [diff] [blame] | 106 | } |
| 107 | |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [diff] [blame^] | 108 | // sourceProviderSource finds the main source file of a source-provider crate. |
| 109 | func sourceProviderSource(ctx android.SingletonContext, rModule *Module) (string, bool) { |
| 110 | rustLib, ok := rModule.compiler.(*libraryDecorator) |
| 111 | if !ok { |
| 112 | return "", false |
| 113 | } |
| 114 | if rustLib.source() { |
| 115 | // This is a source-variant, check if we are the right variant |
| 116 | // depending on the module configuration. |
| 117 | if src, ok := sourceProviderVariantSource(ctx, rModule); ok { |
| 118 | return src, true |
| 119 | } |
| 120 | } |
| 121 | foundSource := false |
| 122 | sourceSrc := "" |
| 123 | // Find the variant with the source and return its. |
| 124 | ctx.VisitAllModuleVariants(rModule, func(variant android.Module) { |
| 125 | if foundSource { |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 126 | return |
| 127 | } |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [diff] [blame^] | 128 | // All variants of a source provider library are libraries. |
| 129 | rVariant, _ := variant.(*Module) |
| 130 | variantLib, _ := rVariant.compiler.(*libraryDecorator) |
| 131 | if variantLib.source() { |
| 132 | sourceSrc, ok = sourceProviderVariantSource(ctx, rVariant) |
| 133 | if ok { |
| 134 | foundSource = true |
| 135 | } |
| 136 | } |
| 137 | }) |
| 138 | if !foundSource { |
| 139 | fmt.Errorf("No valid source for source provider found: %v\n", rModule) |
| 140 | } |
| 141 | return sourceSrc, foundSource |
| 142 | } |
| 143 | |
| 144 | // crateSource finds the main source file (.rs) for a crate. |
| 145 | func crateSource(ctx android.SingletonContext, rModule *Module, comp *baseCompiler) (string, bool) { |
| 146 | // Basic libraries, executables and tests. |
| 147 | srcs := comp.Properties.Srcs |
| 148 | if len(srcs) != 0 { |
| 149 | return path.Join(ctx.ModuleDir(rModule), srcs[0]), true |
| 150 | } |
| 151 | // SourceProvider libraries. |
| 152 | if rModule.sourceProvider != nil { |
| 153 | return sourceProviderSource(ctx, rModule) |
| 154 | } |
| 155 | return "", false |
| 156 | } |
| 157 | |
| 158 | // mergeDependencies visits all the dependencies for module and updates crate and deps |
| 159 | // with any new dependency. |
| 160 | func (singleton *projectGeneratorSingleton) mergeDependencies(ctx android.SingletonContext, |
| 161 | module *Module, crate *rustProjectCrate, deps map[string]int) { |
| 162 | |
| 163 | ctx.VisitDirectDeps(module, func(child android.Module) { |
Thiébaud Weksteen | 3c5905b | 2020-11-25 16:09:32 +0100 | [diff] [blame] | 164 | // Skip intra-module dependencies (i.e., generated-source library depending on the source variant). |
| 165 | if module.Name() == child.Name() { |
| 166 | return |
| 167 | } |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [diff] [blame^] | 168 | // Skip unsupported modules. |
| 169 | rChild, compChild, ok := isModuleSupported(ctx, child) |
| 170 | if !ok { |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 171 | return |
| 172 | } |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [diff] [blame^] | 173 | // For unknown dependency, add it first. |
| 174 | var childId int |
| 175 | cInfo, known := singleton.knownCrates[rChild.Name()] |
| 176 | if !known { |
| 177 | childId, ok = singleton.addCrate(ctx, rChild, compChild) |
| 178 | if !ok { |
| 179 | return |
| 180 | } |
| 181 | } else { |
| 182 | childId = cInfo.Idx |
| 183 | } |
| 184 | // Is this dependency known already? |
| 185 | if _, ok = deps[child.Name()]; ok { |
| 186 | return |
| 187 | } |
| 188 | crate.Deps = append(crate.Deps, rustProjectDep{Crate: childId, Name: rChild.CrateName()}) |
| 189 | deps[child.Name()] = childId |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 190 | }) |
| 191 | } |
| 192 | |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [diff] [blame^] | 193 | // isModuleSupported returns the RustModule and baseCompiler if the module |
| 194 | // should be considered for inclusion in rust-project.json. |
| 195 | func isModuleSupported(ctx android.SingletonContext, module android.Module) (*Module, *baseCompiler, bool) { |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 196 | rModule, ok := module.(*Module) |
| 197 | if !ok { |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [diff] [blame^] | 198 | return nil, nil, false |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 199 | } |
| 200 | if rModule.compiler == nil { |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [diff] [blame^] | 201 | return nil, nil, false |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 202 | } |
Thiébaud Weksteen | 064f6e9 | 2020-12-03 20:58:32 +0100 | [diff] [blame] | 203 | var comp *baseCompiler |
| 204 | switch c := rModule.compiler.(type) { |
| 205 | case *libraryDecorator: |
| 206 | comp = c.baseCompiler |
| 207 | case *binaryDecorator: |
| 208 | comp = c.baseCompiler |
| 209 | case *testDecorator: |
| 210 | comp = c.binaryDecorator.baseCompiler |
| 211 | default: |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [diff] [blame^] | 212 | return nil, nil, false |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 213 | } |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [diff] [blame^] | 214 | return rModule, comp, true |
| 215 | } |
| 216 | |
| 217 | // addCrate adds a crate to singleton.project.Crates ensuring that required |
| 218 | // dependencies are also added. It returns the index of the new crate in |
| 219 | // singleton.project.Crates |
| 220 | func (singleton *projectGeneratorSingleton) addCrate(ctx android.SingletonContext, rModule *Module, comp *baseCompiler) (int, bool) { |
Thiébaud Weksteen | 064f6e9 | 2020-12-03 20:58:32 +0100 | [diff] [blame] | 221 | rootModule, ok := crateSource(ctx, rModule, comp) |
Thiébaud Weksteen | a6351ca | 2020-09-29 09:53:21 +0200 | [diff] [blame] | 222 | if !ok { |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [diff] [blame^] | 223 | fmt.Errorf("Unable to find source for valid module: %v", rModule) |
| 224 | return 0, false |
Thiébaud Weksteen | 83ee52f | 2020-08-05 09:29:23 +0200 | [diff] [blame] | 225 | } |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [diff] [blame^] | 226 | |
| 227 | crate := rustProjectCrate{ |
| 228 | DisplayName: rModule.Name(), |
| 229 | RootModule: rootModule, |
| 230 | Edition: comp.edition(), |
| 231 | Deps: make([]rustProjectDep, 0), |
| 232 | Cfgs: make([]string, 0), |
| 233 | } |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 234 | |
| 235 | deps := make(map[string]int) |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [diff] [blame^] | 236 | singleton.mergeDependencies(ctx, rModule, &crate, deps) |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 237 | |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [diff] [blame^] | 238 | idx := len(singleton.project.Crates) |
| 239 | singleton.knownCrates[rModule.Name()] = crateInfo{Idx: idx, Deps: deps} |
Thiébaud Weksteen | 3805f5c | 2020-09-28 14:42:07 +0200 | [diff] [blame] | 240 | singleton.project.Crates = append(singleton.project.Crates, crate) |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 241 | // rust-analyzer requires that all crates belong to at least one root: |
| 242 | // https://github.com/rust-analyzer/rust-analyzer/issues/4735. |
Thiébaud Weksteen | 3805f5c | 2020-09-28 14:42:07 +0200 | [diff] [blame] | 243 | singleton.project.Roots = append(singleton.project.Roots, path.Dir(crate.RootModule)) |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [diff] [blame^] | 244 | return idx, true |
| 245 | } |
| 246 | |
| 247 | // appendCrateAndDependencies creates a rustProjectCrate for the module argument and appends it to singleton.project. |
| 248 | // It visits the dependencies of the module depth-first so the dependency ID can be added to the current module. If the |
| 249 | // current module is already in singleton.knownCrates, its dependencies are merged. |
| 250 | func (singleton *projectGeneratorSingleton) appendCrateAndDependencies(ctx android.SingletonContext, module android.Module) { |
| 251 | rModule, comp, ok := isModuleSupported(ctx, module) |
| 252 | if !ok { |
| 253 | return |
| 254 | } |
| 255 | // If we have seen this crate already; merge any new dependencies. |
| 256 | if cInfo, ok := singleton.knownCrates[module.Name()]; ok { |
| 257 | crate := singleton.project.Crates[cInfo.Idx] |
| 258 | singleton.mergeDependencies(ctx, rModule, &crate, cInfo.Deps) |
| 259 | singleton.project.Crates[cInfo.Idx] = crate |
| 260 | return |
| 261 | } |
| 262 | singleton.addCrate(ctx, rModule, comp) |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 263 | } |
| 264 | |
Thiébaud Weksteen | 3805f5c | 2020-09-28 14:42:07 +0200 | [diff] [blame] | 265 | func (singleton *projectGeneratorSingleton) GenerateBuildActions(ctx android.SingletonContext) { |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 266 | if !ctx.Config().IsEnvTrue(envVariableCollectRustDeps) { |
| 267 | return |
| 268 | } |
| 269 | |
Thiébaud Weksteen | 3805f5c | 2020-09-28 14:42:07 +0200 | [diff] [blame] | 270 | singleton.knownCrates = make(map[string]crateInfo) |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 271 | ctx.VisitAllModules(func(module android.Module) { |
Thiébaud Weksteen | fa5feae | 2020-12-07 13:40:19 +0100 | [diff] [blame^] | 272 | singleton.appendCrateAndDependencies(ctx, module) |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 273 | }) |
| 274 | |
| 275 | path := android.PathForOutput(ctx, rustProjectJsonFileName) |
Thiébaud Weksteen | 3805f5c | 2020-09-28 14:42:07 +0200 | [diff] [blame] | 276 | err := createJsonFile(singleton.project, path) |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 277 | if err != nil { |
| 278 | ctx.Errorf(err.Error()) |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | func createJsonFile(project rustProjectJson, rustProjectPath android.WritablePath) error { |
| 283 | buf, err := json.MarshalIndent(project, "", " ") |
| 284 | if err != nil { |
| 285 | return fmt.Errorf("JSON marshal of rustProjectJson failed: %s", err) |
| 286 | } |
| 287 | err = android.WriteFileToOutputDir(rustProjectPath, buf, 0666) |
| 288 | if err != nil { |
| 289 | return fmt.Errorf("Writing rust-project to %s failed: %s", rustProjectPath.String(), err) |
| 290 | } |
| 291 | return nil |
| 292 | } |