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 | |
Thiébaud Weksteen | a6351ca | 2020-09-29 09:53:21 +0200 | [diff] [blame] | 78 | // librarySource finds the main source file (.rs) for a crate. |
| 79 | func librarySource(ctx android.SingletonContext, rModule *Module, rustLib *libraryDecorator) (string, bool) { |
| 80 | srcs := rustLib.baseCompiler.Properties.Srcs |
| 81 | if len(srcs) != 0 { |
| 82 | return path.Join(ctx.ModuleDir(rModule), srcs[0]), true |
| 83 | } |
| 84 | if !rustLib.source() { |
| 85 | return "", false |
| 86 | } |
| 87 | // It is a SourceProvider module. If this module is host only, uses the variation for the host. |
| 88 | // Otherwise, use the variation for the primary target. |
| 89 | switch rModule.hod { |
| 90 | case android.HostSupported: |
| 91 | case android.HostSupportedNoCross: |
| 92 | if rModule.Target().String() != ctx.Config().BuildOSTarget.String() { |
| 93 | return "", false |
| 94 | } |
| 95 | default: |
Jaewoong Jung | 642916f | 2020-10-09 17:25:15 -0700 | [diff] [blame^] | 96 | if rModule.Target().String() != ctx.Config().AndroidFirstDeviceTarget.String() { |
Thiébaud Weksteen | a6351ca | 2020-09-29 09:53:21 +0200 | [diff] [blame] | 97 | return "", false |
| 98 | } |
| 99 | } |
| 100 | src := rustLib.sourceProvider.Srcs()[0] |
| 101 | return src.String(), true |
| 102 | } |
| 103 | |
Thiébaud Weksteen | 3805f5c | 2020-09-28 14:42:07 +0200 | [diff] [blame] | 104 | func (singleton *projectGeneratorSingleton) mergeDependencies(ctx android.SingletonContext, |
| 105 | module android.Module, crate *rustProjectCrate, deps map[string]int) { |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 106 | |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 107 | ctx.VisitDirectDeps(module, func(child android.Module) { |
Thiébaud Weksteen | 3805f5c | 2020-09-28 14:42:07 +0200 | [diff] [blame] | 108 | childId, childCrateName, ok := singleton.appendLibraryAndDeps(ctx, child) |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 109 | if !ok { |
| 110 | return |
| 111 | } |
Thiébaud Weksteen | 2f628ba | 2020-08-05 14:27:32 +0200 | [diff] [blame] | 112 | if _, ok = deps[ctx.ModuleName(child)]; ok { |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 113 | return |
| 114 | } |
Thiébaud Weksteen | 2f628ba | 2020-08-05 14:27:32 +0200 | [diff] [blame] | 115 | crate.Deps = append(crate.Deps, rustProjectDep{Crate: childId, Name: childCrateName}) |
| 116 | deps[ctx.ModuleName(child)] = childId |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 117 | }) |
| 118 | } |
| 119 | |
Thiébaud Weksteen | 3805f5c | 2020-09-28 14:42:07 +0200 | [diff] [blame] | 120 | // appendLibraryAndDeps creates a rustProjectCrate for the module argument and appends it to singleton.project. |
| 121 | // It visits the dependencies of the module depth-first so the dependency ID can be added to the current module. If the |
| 122 | // current module is already in singleton.knownCrates, its dependencies are merged. Returns a tuple (id, crate_name, ok). |
| 123 | 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] | 124 | rModule, ok := module.(*Module) |
| 125 | if !ok { |
| 126 | return 0, "", false |
| 127 | } |
| 128 | if rModule.compiler == nil { |
| 129 | return 0, "", false |
| 130 | } |
| 131 | rustLib, ok := rModule.compiler.(*libraryDecorator) |
| 132 | if !ok { |
| 133 | return 0, "", false |
| 134 | } |
Thiébaud Weksteen | 2f628ba | 2020-08-05 14:27:32 +0200 | [diff] [blame] | 135 | moduleName := ctx.ModuleName(module) |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 136 | crateName := rModule.CrateName() |
Thiébaud Weksteen | 3805f5c | 2020-09-28 14:42:07 +0200 | [diff] [blame] | 137 | if cInfo, ok := singleton.knownCrates[moduleName]; ok { |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 138 | // We have seen this crate already; merge any new dependencies. |
Thiébaud Weksteen | 3805f5c | 2020-09-28 14:42:07 +0200 | [diff] [blame] | 139 | crate := singleton.project.Crates[cInfo.ID] |
| 140 | singleton.mergeDependencies(ctx, module, &crate, cInfo.Deps) |
| 141 | singleton.project.Crates[cInfo.ID] = crate |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 142 | return cInfo.ID, crateName, true |
| 143 | } |
| 144 | crate := rustProjectCrate{Deps: make([]rustProjectDep, 0), Cfgs: make([]string, 0)} |
Thiébaud Weksteen | a6351ca | 2020-09-29 09:53:21 +0200 | [diff] [blame] | 145 | rootModule, ok := librarySource(ctx, rModule, rustLib) |
| 146 | if !ok { |
Thiébaud Weksteen | 83ee52f | 2020-08-05 09:29:23 +0200 | [diff] [blame] | 147 | return 0, "", false |
| 148 | } |
Thiébaud Weksteen | a6351ca | 2020-09-29 09:53:21 +0200 | [diff] [blame] | 149 | crate.RootModule = rootModule |
Thiébaud Weksteen | e81c924 | 2020-08-03 10:46:28 +0200 | [diff] [blame] | 150 | crate.Edition = rustLib.baseCompiler.edition() |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 151 | |
| 152 | deps := make(map[string]int) |
Thiébaud Weksteen | 3805f5c | 2020-09-28 14:42:07 +0200 | [diff] [blame] | 153 | singleton.mergeDependencies(ctx, module, &crate, deps) |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 154 | |
Thiébaud Weksteen | 3805f5c | 2020-09-28 14:42:07 +0200 | [diff] [blame] | 155 | id := len(singleton.project.Crates) |
| 156 | singleton.knownCrates[moduleName] = crateInfo{ID: id, Deps: deps} |
| 157 | singleton.project.Crates = append(singleton.project.Crates, crate) |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 158 | // rust-analyzer requires that all crates belong to at least one root: |
| 159 | // https://github.com/rust-analyzer/rust-analyzer/issues/4735. |
Thiébaud Weksteen | 3805f5c | 2020-09-28 14:42:07 +0200 | [diff] [blame] | 160 | singleton.project.Roots = append(singleton.project.Roots, path.Dir(crate.RootModule)) |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 161 | return id, crateName, true |
| 162 | } |
| 163 | |
Thiébaud Weksteen | 3805f5c | 2020-09-28 14:42:07 +0200 | [diff] [blame] | 164 | func (singleton *projectGeneratorSingleton) GenerateBuildActions(ctx android.SingletonContext) { |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 165 | if !ctx.Config().IsEnvTrue(envVariableCollectRustDeps) { |
| 166 | return |
| 167 | } |
| 168 | |
Thiébaud Weksteen | 3805f5c | 2020-09-28 14:42:07 +0200 | [diff] [blame] | 169 | singleton.knownCrates = make(map[string]crateInfo) |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 170 | ctx.VisitAllModules(func(module android.Module) { |
Thiébaud Weksteen | 3805f5c | 2020-09-28 14:42:07 +0200 | [diff] [blame] | 171 | singleton.appendLibraryAndDeps(ctx, module) |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 172 | }) |
| 173 | |
| 174 | path := android.PathForOutput(ctx, rustProjectJsonFileName) |
Thiébaud Weksteen | 3805f5c | 2020-09-28 14:42:07 +0200 | [diff] [blame] | 175 | err := createJsonFile(singleton.project, path) |
Thiébaud Weksteen | e4d12a0 | 2020-06-05 11:09:27 +0200 | [diff] [blame] | 176 | if err != nil { |
| 177 | ctx.Errorf(err.Error()) |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | func createJsonFile(project rustProjectJson, rustProjectPath android.WritablePath) error { |
| 182 | buf, err := json.MarshalIndent(project, "", " ") |
| 183 | if err != nil { |
| 184 | return fmt.Errorf("JSON marshal of rustProjectJson failed: %s", err) |
| 185 | } |
| 186 | err = android.WriteFileToOutputDir(rustProjectPath, buf, 0666) |
| 187 | if err != nil { |
| 188 | return fmt.Errorf("Writing rust-project to %s failed: %s", rustProjectPath.String(), err) |
| 189 | } |
| 190 | return nil |
| 191 | } |