blob: 83c16ca3815d6b0477d7b80c6df685ee1cd75129 [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"
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +020020
21 "android/soong/android"
22)
23
24// This singleton collects Rust crate definitions and generates a JSON file
25// (${OUT_DIR}/soong/rust-project.json) which can be use by external tools,
26// such as rust-analyzer. It does so when either make, mm, mma, mmm or mmma is
27// called. This singleton is enabled only if SOONG_GEN_RUST_PROJECT is set.
28// For example,
29//
30// $ SOONG_GEN_RUST_PROJECT=1 m nothing
31
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +020032const (
33 // Environment variables used to control the behavior of this singleton.
34 envVariableCollectRustDeps = "SOONG_GEN_RUST_PROJECT"
35 rustProjectJsonFileName = "rust-project.json"
36)
37
38// The format of rust-project.json is not yet finalized. A current description is available at:
39// https://github.com/rust-analyzer/rust-analyzer/blob/master/docs/user/manual.adoc#non-cargo-based-projects
40type rustProjectDep struct {
Thiébaud Weksteen3805f5c2020-09-28 14:42:07 +020041 // The Crate attribute is the index of the dependency in the Crates array in rustProjectJson.
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +020042 Crate int `json:"crate"`
43 Name string `json:"name"`
44}
45
46type rustProjectCrate struct {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +010047 DisplayName string `json:"display_name"`
48 RootModule string `json:"root_module"`
49 Edition string `json:"edition,omitempty"`
50 Deps []rustProjectDep `json:"deps"`
Thiébaud Weksteene8b0ee72021-03-25 09:26:07 +010051 Cfg []string `json:"cfg"`
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +010052 Env map[string]string `json:"env"`
Seth Mooreaf96f992021-10-06 10:45:34 -070053 ProcMacro bool `json:"is_proc_macro"`
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +020054}
55
56type rustProjectJson struct {
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +020057 Crates []rustProjectCrate `json:"crates"`
58}
59
60// crateInfo is used during the processing to keep track of the known crates.
61type crateInfo struct {
Matthew Maurerdb72f7e2023-11-21 00:20:02 +000062 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.
64 Device bool // True if the crate at idx was a device crate
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +020065}
66
Thiébaud Weksteen3805f5c2020-09-28 14:42:07 +020067type projectGeneratorSingleton struct {
68 project rustProjectJson
Thiébaud Weksteenfa5feae2020-12-07 13:40:19 +010069 knownCrates map[string]crateInfo // Keys are module names.
Thiébaud Weksteen3805f5c2020-09-28 14:42:07 +020070}
71
72func rustProjectGeneratorSingleton() android.Singleton {
73 return &projectGeneratorSingleton{}
74}
75
76func init() {
LaMont Jones0c10e4d2023-05-16 00:58:37 +000077 android.RegisterParallelSingletonType("rust_project_generator", rustProjectGeneratorSingleton)
Thiébaud Weksteen3805f5c2020-09-28 14:42:07 +020078}
79
Thiébaud Weksteenfa5feae2020-12-07 13:40:19 +010080// mergeDependencies visits all the dependencies for module and updates crate and deps
81// with any new dependency.
82func (singleton *projectGeneratorSingleton) mergeDependencies(ctx android.SingletonContext,
83 module *Module, crate *rustProjectCrate, deps map[string]int) {
84
85 ctx.VisitDirectDeps(module, func(child android.Module) {
Thiébaud Weksteen3c5905b2020-11-25 16:09:32 +010086 // Skip intra-module dependencies (i.e., generated-source library depending on the source variant).
87 if module.Name() == child.Name() {
88 return
89 }
Thiébaud Weksteenfa5feae2020-12-07 13:40:19 +010090 // Skip unsupported modules.
Matthew Maurerdb72f7e2023-11-21 00:20:02 +000091 rChild, ok := isModuleSupported(ctx, child)
Thiébaud Weksteenfa5feae2020-12-07 13:40:19 +010092 if !ok {
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +020093 return
94 }
Thiébaud Weksteenfa5feae2020-12-07 13:40:19 +010095 // For unknown dependency, add it first.
96 var childId int
97 cInfo, known := singleton.knownCrates[rChild.Name()]
98 if !known {
Matthew Maurerdb72f7e2023-11-21 00:20:02 +000099 childId, ok = singleton.addCrate(ctx, rChild, make(map[string]int))
Thiébaud Weksteenfa5feae2020-12-07 13:40:19 +0100100 if !ok {
101 return
102 }
103 } else {
104 childId = cInfo.Idx
105 }
106 // Is this dependency known already?
107 if _, ok = deps[child.Name()]; ok {
108 return
109 }
110 crate.Deps = append(crate.Deps, rustProjectDep{Crate: childId, Name: rChild.CrateName()})
111 deps[child.Name()] = childId
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +0200112 })
113}
114
Matthew Maurerdb72f7e2023-11-21 00:20:02 +0000115// isModuleSupported returns the RustModule if the module
Thiébaud Weksteenfa5feae2020-12-07 13:40:19 +0100116// should be considered for inclusion in rust-project.json.
Matthew Maurerdb72f7e2023-11-21 00:20:02 +0000117func isModuleSupported(ctx android.SingletonContext, module android.Module) (*Module, bool) {
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +0200118 rModule, ok := module.(*Module)
119 if !ok {
Matthew Maurerdb72f7e2023-11-21 00:20:02 +0000120 return nil, false
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +0200121 }
Matthew Maurerdb72f7e2023-11-21 00:20:02 +0000122 return rModule, true
Thiébaud Weksteenfa5feae2020-12-07 13:40:19 +0100123}
124
125// addCrate adds a crate to singleton.project.Crates ensuring that required
126// dependencies are also added. It returns the index of the new crate in
127// singleton.project.Crates
Matthew Maurerdb72f7e2023-11-21 00:20:02 +0000128func (singleton *projectGeneratorSingleton) addCrate(ctx android.SingletonContext, rModule *Module, deps map[string]int) (int, bool) {
129 rootModule, err := rModule.compiler.checkedCrateRootPath()
130 if err != nil {
Thiébaud Weksteenfa5feae2020-12-07 13:40:19 +0100131 return 0, false
Thiébaud Weksteen83ee52f2020-08-05 09:29:23 +0200132 }
Thiébaud Weksteenfa5feae2020-12-07 13:40:19 +0100133
Seth Mooreaf96f992021-10-06 10:45:34 -0700134 _, procMacro := rModule.compiler.(*procMacroDecorator)
135
Thiébaud Weksteenfa5feae2020-12-07 13:40:19 +0100136 crate := rustProjectCrate{
137 DisplayName: rModule.Name(),
Matthew Maurerdb72f7e2023-11-21 00:20:02 +0000138 RootModule: rootModule.String(),
139 Edition: rModule.compiler.edition(),
Thiébaud Weksteenfa5feae2020-12-07 13:40:19 +0100140 Deps: make([]rustProjectDep, 0),
Thiébaud Weksteene8b0ee72021-03-25 09:26:07 +0100141 Cfg: make([]string, 0),
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100142 Env: make(map[string]string),
Seth Mooreaf96f992021-10-06 10:45:34 -0700143 ProcMacro: procMacro,
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100144 }
145
Matthew Maurerdb72f7e2023-11-21 00:20:02 +0000146 if rModule.compiler.cargoOutDir().Valid() {
147 crate.Env["OUT_DIR"] = rModule.compiler.cargoOutDir().String()
Thiébaud Weksteenfa5feae2020-12-07 13:40:19 +0100148 }
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +0200149
Matthew Maurerdb72f7e2023-11-21 00:20:02 +0000150 for _, feature := range rModule.compiler.features() {
Thiébaud Weksteene8b0ee72021-03-25 09:26:07 +0100151 crate.Cfg = append(crate.Cfg, "feature=\""+feature+"\"")
152 }
153
Thiébaud Weksteenfa5feae2020-12-07 13:40:19 +0100154 singleton.mergeDependencies(ctx, rModule, &crate, deps)
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +0200155
Matthew Maurerdb72f7e2023-11-21 00:20:02 +0000156 var idx int
157 if cInfo, ok := singleton.knownCrates[rModule.Name()]; ok {
158 idx = cInfo.Idx
159 singleton.project.Crates[idx] = crate
160 } else {
161 idx = len(singleton.project.Crates)
162 singleton.project.Crates = append(singleton.project.Crates, crate)
163 }
164 singleton.knownCrates[rModule.Name()] = crateInfo{Idx: idx, Deps: deps, Device: rModule.Device()}
Thiébaud Weksteenfa5feae2020-12-07 13:40:19 +0100165 return idx, true
166}
167
168// appendCrateAndDependencies creates a rustProjectCrate for the module argument and appends it to singleton.project.
169// It visits the dependencies of the module depth-first so the dependency ID can be added to the current module. If the
170// current module is already in singleton.knownCrates, its dependencies are merged.
171func (singleton *projectGeneratorSingleton) appendCrateAndDependencies(ctx android.SingletonContext, module android.Module) {
Matthew Maurerdb72f7e2023-11-21 00:20:02 +0000172 rModule, ok := isModuleSupported(ctx, module)
Thiébaud Weksteenfa5feae2020-12-07 13:40:19 +0100173 if !ok {
174 return
175 }
176 // If we have seen this crate already; merge any new dependencies.
177 if cInfo, ok := singleton.knownCrates[module.Name()]; ok {
Matthew Maurerdb72f7e2023-11-21 00:20:02 +0000178 // If we have a new device variant, override the old one
179 if !cInfo.Device && rModule.Device() {
180 singleton.addCrate(ctx, rModule, cInfo.Deps)
181 return
182 }
Thiébaud Weksteenfa5feae2020-12-07 13:40:19 +0100183 crate := singleton.project.Crates[cInfo.Idx]
184 singleton.mergeDependencies(ctx, rModule, &crate, cInfo.Deps)
185 singleton.project.Crates[cInfo.Idx] = crate
186 return
187 }
Matthew Maurerdb72f7e2023-11-21 00:20:02 +0000188 singleton.addCrate(ctx, rModule, make(map[string]int))
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +0200189}
190
Thiébaud Weksteen3805f5c2020-09-28 14:42:07 +0200191func (singleton *projectGeneratorSingleton) GenerateBuildActions(ctx android.SingletonContext) {
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +0200192 if !ctx.Config().IsEnvTrue(envVariableCollectRustDeps) {
193 return
194 }
195
Thiébaud Weksteen3805f5c2020-09-28 14:42:07 +0200196 singleton.knownCrates = make(map[string]crateInfo)
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +0200197 ctx.VisitAllModules(func(module android.Module) {
Thiébaud Weksteenfa5feae2020-12-07 13:40:19 +0100198 singleton.appendCrateAndDependencies(ctx, module)
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +0200199 })
200
201 path := android.PathForOutput(ctx, rustProjectJsonFileName)
Thiébaud Weksteen3805f5c2020-09-28 14:42:07 +0200202 err := createJsonFile(singleton.project, path)
Thiébaud Weksteene4d12a02020-06-05 11:09:27 +0200203 if err != nil {
204 ctx.Errorf(err.Error())
205 }
206}
207
208func createJsonFile(project rustProjectJson, rustProjectPath android.WritablePath) error {
209 buf, err := json.MarshalIndent(project, "", " ")
210 if err != nil {
211 return fmt.Errorf("JSON marshal of rustProjectJson failed: %s", err)
212 }
213 err = android.WriteFileToOutputDir(rustProjectPath, buf, 0666)
214 if err != nil {
215 return fmt.Errorf("Writing rust-project to %s failed: %s", rustProjectPath.String(), err)
216 }
217 return nil
218}