2020-06-05 17:09:27 +08: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"
|
|
|
|
"path"
|
|
|
|
|
|
|
|
"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 20:42:07 +08:00
|
|
|
// The Crate attribute is the index of the dependency in the Crates array in rustProjectJson.
|
2020-06-05 17:09:27 +08:00
|
|
|
Crate int `json:"crate"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type rustProjectCrate struct {
|
2021-02-25 23:30:57 +08:00
|
|
|
DisplayName string `json:"display_name"`
|
|
|
|
RootModule string `json:"root_module"`
|
|
|
|
Edition string `json:"edition,omitempty"`
|
|
|
|
Deps []rustProjectDep `json:"deps"`
|
|
|
|
Cfgs []string `json:"cfgs"`
|
|
|
|
Env map[string]string `json:"env"`
|
2020-06-05 17:09:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type rustProjectJson struct {
|
|
|
|
Roots []string `json:"roots"`
|
|
|
|
Crates []rustProjectCrate `json:"crates"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// crateInfo is used during the processing to keep track of the known crates.
|
|
|
|
type crateInfo struct {
|
2020-12-07 20:40:19 +08: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.
|
2020-06-05 17:09:27 +08:00
|
|
|
}
|
|
|
|
|
2020-09-28 20:42:07 +08:00
|
|
|
type projectGeneratorSingleton struct {
|
|
|
|
project rustProjectJson
|
2020-12-07 20:40:19 +08:00
|
|
|
knownCrates map[string]crateInfo // Keys are module names.
|
2020-09-28 20:42:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func rustProjectGeneratorSingleton() android.Singleton {
|
|
|
|
return &projectGeneratorSingleton{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
android.RegisterSingletonType("rust_project_generator", rustProjectGeneratorSingleton)
|
|
|
|
}
|
|
|
|
|
2020-12-07 20:40:19 +08:00
|
|
|
// sourceProviderVariantSource returns the path to the source file if this
|
|
|
|
// module variant should be used as a priority.
|
|
|
|
//
|
|
|
|
// SourceProvider modules may have multiple variants considered as source
|
|
|
|
// (e.g., x86_64 and armv8). For a module available on device, use the source
|
|
|
|
// generated for the target. For a host-only module, use the source generated
|
|
|
|
// for the host.
|
|
|
|
func sourceProviderVariantSource(ctx android.SingletonContext, rModule *Module) (string, bool) {
|
2020-12-04 03:58:32 +08:00
|
|
|
rustLib, ok := rModule.compiler.(*libraryDecorator)
|
|
|
|
if !ok {
|
|
|
|
return "", false
|
|
|
|
}
|
2020-12-07 20:40:19 +08:00
|
|
|
if rustLib.source() {
|
|
|
|
switch rModule.hod {
|
|
|
|
case android.HostSupported, android.HostSupportedNoCross:
|
|
|
|
if rModule.Target().String() == ctx.Config().BuildOSTarget.String() {
|
|
|
|
src := rustLib.sourceProvider.Srcs()[0]
|
|
|
|
return src.String(), true
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
if rModule.Target().String() == ctx.Config().AndroidFirstDeviceTarget.String() {
|
|
|
|
src := rustLib.sourceProvider.Srcs()[0]
|
|
|
|
return src.String(), true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
// sourceProviderSource finds the main source file of a source-provider crate.
|
|
|
|
func sourceProviderSource(ctx android.SingletonContext, rModule *Module) (string, bool) {
|
|
|
|
rustLib, ok := rModule.compiler.(*libraryDecorator)
|
|
|
|
if !ok {
|
2020-09-29 15:53:21 +08:00
|
|
|
return "", false
|
|
|
|
}
|
2020-12-07 20:40:19 +08:00
|
|
|
if rustLib.source() {
|
|
|
|
// This is a source-variant, check if we are the right variant
|
|
|
|
// depending on the module configuration.
|
|
|
|
if src, ok := sourceProviderVariantSource(ctx, rModule); ok {
|
|
|
|
return src, true
|
2020-09-29 15:53:21 +08:00
|
|
|
}
|
2020-12-07 20:40:19 +08:00
|
|
|
}
|
|
|
|
foundSource := false
|
|
|
|
sourceSrc := ""
|
|
|
|
// Find the variant with the source and return its.
|
|
|
|
ctx.VisitAllModuleVariants(rModule, func(variant android.Module) {
|
|
|
|
if foundSource {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// All variants of a source provider library are libraries.
|
|
|
|
rVariant, _ := variant.(*Module)
|
|
|
|
variantLib, _ := rVariant.compiler.(*libraryDecorator)
|
|
|
|
if variantLib.source() {
|
|
|
|
sourceSrc, ok = sourceProviderVariantSource(ctx, rVariant)
|
|
|
|
if ok {
|
|
|
|
foundSource = true
|
|
|
|
}
|
2020-09-29 15:53:21 +08:00
|
|
|
}
|
2020-12-07 20:40:19 +08:00
|
|
|
})
|
|
|
|
if !foundSource {
|
2021-02-25 23:30:57 +08:00
|
|
|
ctx.Errorf("No valid source for source provider found: %v\n", rModule)
|
2020-12-07 20:40:19 +08:00
|
|
|
}
|
|
|
|
return sourceSrc, foundSource
|
|
|
|
}
|
|
|
|
|
|
|
|
// crateSource finds the main source file (.rs) for a crate.
|
|
|
|
func crateSource(ctx android.SingletonContext, rModule *Module, comp *baseCompiler) (string, bool) {
|
|
|
|
// Basic libraries, executables and tests.
|
|
|
|
srcs := comp.Properties.Srcs
|
|
|
|
if len(srcs) != 0 {
|
|
|
|
return path.Join(ctx.ModuleDir(rModule), srcs[0]), true
|
|
|
|
}
|
|
|
|
// SourceProvider libraries.
|
|
|
|
if rModule.sourceProvider != nil {
|
|
|
|
return sourceProviderSource(ctx, rModule)
|
2020-09-29 15:53:21 +08:00
|
|
|
}
|
2020-12-07 20:40:19 +08:00
|
|
|
return "", false
|
2020-09-29 15:53:21 +08:00
|
|
|
}
|
|
|
|
|
2020-12-07 20:40:19 +08:00
|
|
|
// mergeDependencies visits all the dependencies for module and updates crate and deps
|
|
|
|
// with any new dependency.
|
2020-09-28 20:42:07 +08:00
|
|
|
func (singleton *projectGeneratorSingleton) mergeDependencies(ctx android.SingletonContext,
|
2020-12-07 20:40:19 +08:00
|
|
|
module *Module, crate *rustProjectCrate, deps map[string]int) {
|
2020-06-05 17:09:27 +08:00
|
|
|
|
|
|
|
ctx.VisitDirectDeps(module, func(child android.Module) {
|
2020-11-25 23:09:32 +08:00
|
|
|
// Skip intra-module dependencies (i.e., generated-source library depending on the source variant).
|
|
|
|
if module.Name() == child.Name() {
|
|
|
|
return
|
|
|
|
}
|
2020-12-07 20:40:19 +08:00
|
|
|
// Skip unsupported modules.
|
|
|
|
rChild, compChild, ok := isModuleSupported(ctx, child)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// For unknown dependency, add it first.
|
|
|
|
var childId int
|
|
|
|
cInfo, known := singleton.knownCrates[rChild.Name()]
|
|
|
|
if !known {
|
|
|
|
childId, ok = singleton.addCrate(ctx, rChild, compChild)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
childId = cInfo.Idx
|
|
|
|
}
|
|
|
|
// Is this dependency known already?
|
|
|
|
if _, ok = deps[child.Name()]; ok {
|
2020-06-05 17:09:27 +08:00
|
|
|
return
|
|
|
|
}
|
2020-12-07 20:40:19 +08:00
|
|
|
crate.Deps = append(crate.Deps, rustProjectDep{Crate: childId, Name: rChild.CrateName()})
|
|
|
|
deps[child.Name()] = childId
|
2020-06-05 17:09:27 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-12-07 20:40:19 +08:00
|
|
|
// isModuleSupported returns the RustModule and baseCompiler if the module
|
|
|
|
// should be considered for inclusion in rust-project.json.
|
|
|
|
func isModuleSupported(ctx android.SingletonContext, module android.Module) (*Module, *baseCompiler, bool) {
|
2020-06-05 17:09:27 +08:00
|
|
|
rModule, ok := module.(*Module)
|
|
|
|
if !ok {
|
2020-12-07 20:40:19 +08:00
|
|
|
return nil, nil, false
|
2020-06-05 17:09:27 +08:00
|
|
|
}
|
|
|
|
if rModule.compiler == nil {
|
2020-12-07 20:40:19 +08:00
|
|
|
return nil, nil, false
|
2020-06-05 17:09:27 +08:00
|
|
|
}
|
2020-12-04 03:58:32 +08:00
|
|
|
var comp *baseCompiler
|
|
|
|
switch c := rModule.compiler.(type) {
|
|
|
|
case *libraryDecorator:
|
|
|
|
comp = c.baseCompiler
|
|
|
|
case *binaryDecorator:
|
|
|
|
comp = c.baseCompiler
|
|
|
|
case *testDecorator:
|
|
|
|
comp = c.binaryDecorator.baseCompiler
|
|
|
|
default:
|
2020-12-07 20:40:19 +08:00
|
|
|
return nil, nil, false
|
|
|
|
}
|
|
|
|
return rModule, comp, true
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
func (singleton *projectGeneratorSingleton) addCrate(ctx android.SingletonContext, rModule *Module, comp *baseCompiler) (int, bool) {
|
2020-12-04 03:58:32 +08:00
|
|
|
rootModule, ok := crateSource(ctx, rModule, comp)
|
2020-09-29 15:53:21 +08:00
|
|
|
if !ok {
|
2021-02-25 23:30:57 +08:00
|
|
|
ctx.Errorf("Unable to find source for valid module: %v", rModule)
|
2020-12-07 20:40:19 +08:00
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
|
|
|
|
crate := rustProjectCrate{
|
|
|
|
DisplayName: rModule.Name(),
|
|
|
|
RootModule: rootModule,
|
|
|
|
Edition: comp.edition(),
|
|
|
|
Deps: make([]rustProjectDep, 0),
|
|
|
|
Cfgs: make([]string, 0),
|
2021-02-25 23:30:57 +08:00
|
|
|
Env: make(map[string]string),
|
|
|
|
}
|
|
|
|
|
|
|
|
if comp.CargoOutDir().Valid() {
|
|
|
|
crate.Env["OUT_DIR"] = comp.CargoOutDir().String()
|
2020-08-05 15:29:23 +08:00
|
|
|
}
|
2020-06-05 17:09:27 +08:00
|
|
|
|
|
|
|
deps := make(map[string]int)
|
2020-12-07 20:40:19 +08:00
|
|
|
singleton.mergeDependencies(ctx, rModule, &crate, deps)
|
2020-06-05 17:09:27 +08:00
|
|
|
|
2020-12-07 20:40:19 +08:00
|
|
|
idx := len(singleton.project.Crates)
|
|
|
|
singleton.knownCrates[rModule.Name()] = crateInfo{Idx: idx, Deps: deps}
|
2020-09-28 20:42:07 +08:00
|
|
|
singleton.project.Crates = append(singleton.project.Crates, crate)
|
2020-06-05 17:09:27 +08:00
|
|
|
// rust-analyzer requires that all crates belong to at least one root:
|
|
|
|
// https://github.com/rust-analyzer/rust-analyzer/issues/4735.
|
2020-09-28 20:42:07 +08:00
|
|
|
singleton.project.Roots = append(singleton.project.Roots, path.Dir(crate.RootModule))
|
2020-12-07 20:40:19 +08: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) {
|
|
|
|
rModule, comp, ok := isModuleSupported(ctx, module)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// If we have seen this crate already; merge any new dependencies.
|
|
|
|
if cInfo, ok := singleton.knownCrates[module.Name()]; ok {
|
|
|
|
crate := singleton.project.Crates[cInfo.Idx]
|
|
|
|
singleton.mergeDependencies(ctx, rModule, &crate, cInfo.Deps)
|
|
|
|
singleton.project.Crates[cInfo.Idx] = crate
|
|
|
|
return
|
|
|
|
}
|
|
|
|
singleton.addCrate(ctx, rModule, comp)
|
2020-06-05 17:09:27 +08:00
|
|
|
}
|
|
|
|
|
2020-09-28 20:42:07 +08:00
|
|
|
func (singleton *projectGeneratorSingleton) GenerateBuildActions(ctx android.SingletonContext) {
|
2020-06-05 17:09:27 +08:00
|
|
|
if !ctx.Config().IsEnvTrue(envVariableCollectRustDeps) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-28 20:42:07 +08:00
|
|
|
singleton.knownCrates = make(map[string]crateInfo)
|
2020-06-05 17:09:27 +08:00
|
|
|
ctx.VisitAllModules(func(module android.Module) {
|
2020-12-07 20:40:19 +08:00
|
|
|
singleton.appendCrateAndDependencies(ctx, module)
|
2020-06-05 17:09:27 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
path := android.PathForOutput(ctx, rustProjectJsonFileName)
|
2020-09-28 20:42:07 +08:00
|
|
|
err := createJsonFile(singleton.project, path)
|
2020-06-05 17:09:27 +08: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
|
|
|
|
}
|