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