blob: 0d25a2f86dced7a391cfd4f5a7920a8ed9fe0003 [file] [log] [blame]
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +02001// 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
15package rust
16
17import (
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 Weksteene4d12a02020-06-05 11:09:27 +020033const (
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
41type rustProjectDep struct {
Thiébaud Weksteen3805f5c2020-09-28 14:42:07 +020042 // The Crate attribute is the index of the dependency in the Crates array in rustProjectJson.
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +020043 Crate int `json:"crate"`
44 Name string `json:"name"`
45}
46
47type 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
54type 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.
60type crateInfo struct {
61 ID int
62 Deps map[string]int
63}
64
Thiébaud Weksteen3805f5c2020-09-28 14:42:07 +020065type projectGeneratorSingleton struct {
66 project rustProjectJson
67 knownCrates map[string]crateInfo
68}
69
70func rustProjectGeneratorSingleton() android.Singleton {
71 return &projectGeneratorSingleton{}
72}
73
74func init() {
75 android.RegisterSingletonType("rust_project_generator", rustProjectGeneratorSingleton)
76}
77
78func (singleton *projectGeneratorSingleton) mergeDependencies(ctx android.SingletonContext,
79 module android.Module, crate *rustProjectCrate, deps map[string]int) {
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +020080
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +020081 ctx.VisitDirectDeps(module, func(child android.Module) {
Thiébaud Weksteen3805f5c2020-09-28 14:42:07 +020082 childId, childCrateName, ok := singleton.appendLibraryAndDeps(ctx, child)
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +020083 if !ok {
84 return
85 }
Thiébaud Weksteen2f628ba2020-08-05 14:27:32 +020086 if _, ok = deps[ctx.ModuleName(child)]; ok {
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +020087 return
88 }
Thiébaud Weksteen2f628ba2020-08-05 14:27:32 +020089 crate.Deps = append(crate.Deps, rustProjectDep{Crate: childId, Name: childCrateName})
90 deps[ctx.ModuleName(child)] = childId
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +020091 })
92}
93
Thiébaud Weksteen3805f5c2020-09-28 14:42:07 +020094// appendLibraryAndDeps creates a rustProjectCrate for the module argument and appends it to singleton.project.
95// It visits the dependencies of the module depth-first so the dependency ID can be added to the current module. If the
96// current module is already in singleton.knownCrates, its dependencies are merged. Returns a tuple (id, crate_name, ok).
97func (singleton *projectGeneratorSingleton) appendLibraryAndDeps(ctx android.SingletonContext, module android.Module) (int, string, bool) {
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +020098 rModule, ok := module.(*Module)
99 if !ok {
100 return 0, "", false
101 }
102 if rModule.compiler == nil {
103 return 0, "", false
104 }
105 rustLib, ok := rModule.compiler.(*libraryDecorator)
106 if !ok {
107 return 0, "", false
108 }
Thiébaud Weksteen2f628ba2020-08-05 14:27:32 +0200109 moduleName := ctx.ModuleName(module)
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +0200110 crateName := rModule.CrateName()
Thiébaud Weksteen3805f5c2020-09-28 14:42:07 +0200111 if cInfo, ok := singleton.knownCrates[moduleName]; ok {
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +0200112 // We have seen this crate already; merge any new dependencies.
Thiébaud Weksteen3805f5c2020-09-28 14:42:07 +0200113 crate := singleton.project.Crates[cInfo.ID]
114 singleton.mergeDependencies(ctx, module, &crate, cInfo.Deps)
115 singleton.project.Crates[cInfo.ID] = crate
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +0200116 return cInfo.ID, crateName, true
117 }
118 crate := rustProjectCrate{Deps: make([]rustProjectDep, 0), Cfgs: make([]string, 0)}
Thiébaud Weksteen83ee52f2020-08-05 09:29:23 +0200119 srcs := rustLib.baseCompiler.Properties.Srcs
120 if len(srcs) == 0 {
121 return 0, "", false
122 }
123 crate.RootModule = path.Join(ctx.ModuleDir(rModule), srcs[0])
Thiébaud Weksteene81c9242020-08-03 10:46:28 +0200124 crate.Edition = rustLib.baseCompiler.edition()
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +0200125
126 deps := make(map[string]int)
Thiébaud Weksteen3805f5c2020-09-28 14:42:07 +0200127 singleton.mergeDependencies(ctx, module, &crate, deps)
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +0200128
Thiébaud Weksteen3805f5c2020-09-28 14:42:07 +0200129 id := len(singleton.project.Crates)
130 singleton.knownCrates[moduleName] = crateInfo{ID: id, Deps: deps}
131 singleton.project.Crates = append(singleton.project.Crates, crate)
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +0200132 // rust-analyzer requires that all crates belong to at least one root:
133 // https://github.com/rust-analyzer/rust-analyzer/issues/4735.
Thiébaud Weksteen3805f5c2020-09-28 14:42:07 +0200134 singleton.project.Roots = append(singleton.project.Roots, path.Dir(crate.RootModule))
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +0200135 return id, crateName, true
136}
137
Thiébaud Weksteen3805f5c2020-09-28 14:42:07 +0200138func (singleton *projectGeneratorSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +0200139 if !ctx.Config().IsEnvTrue(envVariableCollectRustDeps) {
140 return
141 }
142
Thiébaud Weksteen3805f5c2020-09-28 14:42:07 +0200143 singleton.knownCrates = make(map[string]crateInfo)
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +0200144 ctx.VisitAllModules(func(module android.Module) {
Thiébaud Weksteen3805f5c2020-09-28 14:42:07 +0200145 singleton.appendLibraryAndDeps(ctx, module)
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +0200146 })
147
148 path := android.PathForOutput(ctx, rustProjectJsonFileName)
Thiébaud Weksteen3805f5c2020-09-28 14:42:07 +0200149 err := createJsonFile(singleton.project, path)
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +0200150 if err != nil {
151 ctx.Errorf(err.Error())
152 }
153}
154
155func createJsonFile(project rustProjectJson, rustProjectPath android.WritablePath) error {
156 buf, err := json.MarshalIndent(project, "", " ")
157 if err != nil {
158 return fmt.Errorf("JSON marshal of rustProjectJson failed: %s", err)
159 }
160 err = android.WriteFileToOutputDir(rustProjectPath, buf, 0666)
161 if err != nil {
162 return fmt.Errorf("Writing rust-project to %s failed: %s", rustProjectPath.String(), err)
163 }
164 return nil
165}