2020-06-05 11:09:27 +02:00
|
|
|
// Copyright 2020 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package rust
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"android/soong/android"
|
|
|
|
)
|
|
|
|
|
|
|
|
// This singleton collects Rust crate definitions and generates a JSON file
|
|
|
|
// (${OUT_DIR}/soong/rust-project.json) which can be use by external tools,
|
|
|
|
// such as rust-analyzer. It does so when either make, mm, mma, mmm or mmma is
|
|
|
|
// called. This singleton is enabled only if SOONG_GEN_RUST_PROJECT is set.
|
|
|
|
// For example,
|
|
|
|
//
|
|
|
|
// $ SOONG_GEN_RUST_PROJECT=1 m nothing
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Environment variables used to control the behavior of this singleton.
|
|
|
|
envVariableCollectRustDeps = "SOONG_GEN_RUST_PROJECT"
|
|
|
|
rustProjectJsonFileName = "rust-project.json"
|
|
|
|
)
|
|
|
|
|
|
|
|
// The format of rust-project.json is not yet finalized. A current description is available at:
|
|
|
|
// https://github.com/rust-analyzer/rust-analyzer/blob/master/docs/user/manual.adoc#non-cargo-based-projects
|
|
|
|
type rustProjectDep struct {
|
2020-09-28 14:42:07 +02:00
|
|
|
// The Crate attribute is the index of the dependency in the Crates array in rustProjectJson.
|
2020-06-05 11:09:27 +02:00
|
|
|
Crate int `json:"crate"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type rustProjectCrate struct {
|
2021-02-25 16:30:57 +01:00
|
|
|
DisplayName string `json:"display_name"`
|
|
|
|
RootModule string `json:"root_module"`
|
|
|
|
Edition string `json:"edition,omitempty"`
|
|
|
|
Deps []rustProjectDep `json:"deps"`
|
2021-03-25 09:26:07 +01:00
|
|
|
Cfg []string `json:"cfg"`
|
2021-02-25 16:30:57 +01:00
|
|
|
Env map[string]string `json:"env"`
|
2021-10-06 19:45:34 +02:00
|
|
|
ProcMacro bool `json:"is_proc_macro"`
|
2020-06-05 11:09:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type rustProjectJson struct {
|
|
|
|
Crates []rustProjectCrate `json:"crates"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// crateInfo is used during the processing to keep track of the known crates.
|
|
|
|
type crateInfo struct {
|
2023-11-21 01:20:02 +01:00
|
|
|
Idx int // Index of the crate in rustProjectJson.Crates slice.
|
|
|
|
Deps map[string]int // The keys are the module names and not the crate names.
|
|
|
|
Device bool // True if the crate at idx was a device crate
|
2020-06-05 11:09:27 +02:00
|
|
|
}
|
|
|
|
|
2020-09-28 14:42:07 +02:00
|
|
|
type projectGeneratorSingleton struct {
|
|
|
|
project rustProjectJson
|
2020-12-07 13:40:19 +01:00
|
|
|
knownCrates map[string]crateInfo // Keys are module names.
|
2020-09-28 14:42:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func rustProjectGeneratorSingleton() android.Singleton {
|
|
|
|
return &projectGeneratorSingleton{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2023-05-16 02:58:37 +02:00
|
|
|
android.RegisterParallelSingletonType("rust_project_generator", rustProjectGeneratorSingleton)
|
2020-09-28 14:42:07 +02:00
|
|
|
}
|
|
|
|
|
2020-12-07 13:40:19 +01:00
|
|
|
// mergeDependencies visits all the dependencies for module and updates crate and deps
|
|
|
|
// with any new dependency.
|
2020-09-28 14:42:07 +02:00
|
|
|
func (singleton *projectGeneratorSingleton) mergeDependencies(ctx android.SingletonContext,
|
2020-12-07 13:40:19 +01:00
|
|
|
module *Module, crate *rustProjectCrate, deps map[string]int) {
|
2020-06-05 11:09:27 +02:00
|
|
|
|
|
|
|
ctx.VisitDirectDeps(module, func(child android.Module) {
|
2020-11-25 16:09:32 +01:00
|
|
|
// Skip intra-module dependencies (i.e., generated-source library depending on the source variant).
|
|
|
|
if module.Name() == child.Name() {
|
|
|
|
return
|
|
|
|
}
|
2020-12-07 13:40:19 +01:00
|
|
|
// Skip unsupported modules.
|
2023-11-21 01:20:02 +01:00
|
|
|
rChild, ok := isModuleSupported(ctx, child)
|
2020-12-07 13:40:19 +01:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// For unknown dependency, add it first.
|
|
|
|
var childId int
|
|
|
|
cInfo, known := singleton.knownCrates[rChild.Name()]
|
|
|
|
if !known {
|
2024-05-01 01:06:05 +02:00
|
|
|
childId, ok = singleton.addCrate(ctx, rChild)
|
2020-12-07 13:40:19 +01:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
childId = cInfo.Idx
|
|
|
|
}
|
|
|
|
// Is this dependency known already?
|
|
|
|
if _, ok = deps[child.Name()]; ok {
|
2020-06-05 11:09:27 +02:00
|
|
|
return
|
|
|
|
}
|
2020-12-07 13:40:19 +01:00
|
|
|
crate.Deps = append(crate.Deps, rustProjectDep{Crate: childId, Name: rChild.CrateName()})
|
|
|
|
deps[child.Name()] = childId
|
2020-06-05 11:09:27 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-11-21 01:20:02 +01:00
|
|
|
// isModuleSupported returns the RustModule if the module
|
2020-12-07 13:40:19 +01:00
|
|
|
// should be considered for inclusion in rust-project.json.
|
2023-11-21 01:20:02 +01:00
|
|
|
func isModuleSupported(ctx android.SingletonContext, module android.Module) (*Module, bool) {
|
2020-06-05 11:09:27 +02:00
|
|
|
rModule, ok := module.(*Module)
|
|
|
|
if !ok {
|
2023-11-21 01:20:02 +01:00
|
|
|
return nil, false
|
2020-06-05 11:09:27 +02:00
|
|
|
}
|
2024-05-02 01:59:00 +02:00
|
|
|
if !rModule.Enabled(ctx) {
|
2023-11-27 18:51:58 +01:00
|
|
|
return nil, false
|
|
|
|
}
|
2023-11-21 01:20:02 +01:00
|
|
|
return rModule, true
|
2020-12-07 13:40:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// addCrate adds a crate to singleton.project.Crates ensuring that required
|
|
|
|
// dependencies are also added. It returns the index of the new crate in
|
|
|
|
// singleton.project.Crates
|
2024-05-01 01:06:05 +02:00
|
|
|
func (singleton *projectGeneratorSingleton) addCrate(ctx android.SingletonContext, rModule *Module) (int, bool) {
|
|
|
|
deps := make(map[string]int)
|
2023-11-21 01:20:02 +01:00
|
|
|
rootModule, err := rModule.compiler.checkedCrateRootPath()
|
|
|
|
if err != nil {
|
2020-12-07 13:40:19 +01:00
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
|
2021-10-06 19:45:34 +02:00
|
|
|
_, procMacro := rModule.compiler.(*procMacroDecorator)
|
|
|
|
|
2020-12-07 13:40:19 +01:00
|
|
|
crate := rustProjectCrate{
|
|
|
|
DisplayName: rModule.Name(),
|
2023-11-21 01:20:02 +01:00
|
|
|
RootModule: rootModule.String(),
|
|
|
|
Edition: rModule.compiler.edition(),
|
2020-12-07 13:40:19 +01:00
|
|
|
Deps: make([]rustProjectDep, 0),
|
2021-03-25 09:26:07 +01:00
|
|
|
Cfg: make([]string, 0),
|
2021-02-25 16:30:57 +01:00
|
|
|
Env: make(map[string]string),
|
2021-10-06 19:45:34 +02:00
|
|
|
ProcMacro: procMacro,
|
2021-02-25 16:30:57 +01:00
|
|
|
}
|
|
|
|
|
2023-11-21 01:20:02 +01:00
|
|
|
if rModule.compiler.cargoOutDir().Valid() {
|
|
|
|
crate.Env["OUT_DIR"] = rModule.compiler.cargoOutDir().String()
|
2020-08-05 09:29:23 +02:00
|
|
|
}
|
2020-06-05 11:09:27 +02:00
|
|
|
|
2023-11-21 01:20:02 +01:00
|
|
|
for _, feature := range rModule.compiler.features() {
|
2021-03-25 09:26:07 +01:00
|
|
|
crate.Cfg = append(crate.Cfg, "feature=\""+feature+"\"")
|
|
|
|
}
|
|
|
|
|
2020-12-07 13:40:19 +01:00
|
|
|
singleton.mergeDependencies(ctx, rModule, &crate, deps)
|
2020-06-05 11:09:27 +02:00
|
|
|
|
2023-11-21 01:20:02 +01:00
|
|
|
var idx int
|
|
|
|
if cInfo, ok := singleton.knownCrates[rModule.Name()]; ok {
|
|
|
|
idx = cInfo.Idx
|
|
|
|
singleton.project.Crates[idx] = crate
|
|
|
|
} else {
|
|
|
|
idx = len(singleton.project.Crates)
|
|
|
|
singleton.project.Crates = append(singleton.project.Crates, crate)
|
|
|
|
}
|
|
|
|
singleton.knownCrates[rModule.Name()] = crateInfo{Idx: idx, Deps: deps, Device: rModule.Device()}
|
2020-12-07 13:40:19 +01:00
|
|
|
return idx, true
|
|
|
|
}
|
|
|
|
|
|
|
|
// appendCrateAndDependencies creates a rustProjectCrate for the module argument and appends it to singleton.project.
|
|
|
|
// It visits the dependencies of the module depth-first so the dependency ID can be added to the current module. If the
|
|
|
|
// current module is already in singleton.knownCrates, its dependencies are merged.
|
|
|
|
func (singleton *projectGeneratorSingleton) appendCrateAndDependencies(ctx android.SingletonContext, module android.Module) {
|
2023-11-21 01:20:02 +01:00
|
|
|
rModule, ok := isModuleSupported(ctx, module)
|
2020-12-07 13:40:19 +01:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// If we have seen this crate already; merge any new dependencies.
|
|
|
|
if cInfo, ok := singleton.knownCrates[module.Name()]; ok {
|
2023-11-21 01:20:02 +01:00
|
|
|
// If we have a new device variant, override the old one
|
|
|
|
if !cInfo.Device && rModule.Device() {
|
2024-05-01 01:06:05 +02:00
|
|
|
singleton.addCrate(ctx, rModule)
|
2023-11-21 01:20:02 +01:00
|
|
|
return
|
|
|
|
}
|
2020-12-07 13:40:19 +01:00
|
|
|
crate := singleton.project.Crates[cInfo.Idx]
|
|
|
|
singleton.mergeDependencies(ctx, rModule, &crate, cInfo.Deps)
|
|
|
|
singleton.project.Crates[cInfo.Idx] = crate
|
|
|
|
return
|
|
|
|
}
|
2024-05-01 01:06:05 +02:00
|
|
|
singleton.addCrate(ctx, rModule)
|
2020-06-05 11:09:27 +02:00
|
|
|
}
|
|
|
|
|
2020-09-28 14:42:07 +02:00
|
|
|
func (singleton *projectGeneratorSingleton) GenerateBuildActions(ctx android.SingletonContext) {
|
2020-06-05 11:09:27 +02:00
|
|
|
if !ctx.Config().IsEnvTrue(envVariableCollectRustDeps) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-28 14:42:07 +02:00
|
|
|
singleton.knownCrates = make(map[string]crateInfo)
|
2020-06-05 11:09:27 +02:00
|
|
|
ctx.VisitAllModules(func(module android.Module) {
|
2020-12-07 13:40:19 +01:00
|
|
|
singleton.appendCrateAndDependencies(ctx, module)
|
2020-06-05 11:09:27 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
path := android.PathForOutput(ctx, rustProjectJsonFileName)
|
2020-09-28 14:42:07 +02:00
|
|
|
err := createJsonFile(singleton.project, path)
|
2020-06-05 11:09:27 +02:00
|
|
|
if err != nil {
|
|
|
|
ctx.Errorf(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createJsonFile(project rustProjectJson, rustProjectPath android.WritablePath) error {
|
|
|
|
buf, err := json.MarshalIndent(project, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("JSON marshal of rustProjectJson failed: %s", err)
|
|
|
|
}
|
|
|
|
err = android.WriteFileToOutputDir(rustProjectPath, buf, 0666)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Writing rust-project to %s failed: %s", rustProjectPath.String(), err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|