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