Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 57d0141c authored by Matthew Maurer's avatar Matthew Maurer Committed by Automerger Merge Worker
Browse files

Merge changes I0caddbf6,Iee20b060,I6c92580b,I45028945,Ia7dd5220, ... into main...

Merge changes I0caddbf6,Iee20b060,I6c92580b,I45028945,Ia7dd5220, ... into main am: b103659c am: 7a9add56

Original change: https://android-review.googlesource.com/c/platform/build/soong/+/2839054



Change-Id: I21b1e3ca0ee72f59dce175671691ba5a28da6727
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents 15beff1a 7a9add56
Loading
Loading
Loading
Loading
+1 −6
Original line number Diff line number Diff line
@@ -137,12 +137,7 @@ func (binary *binaryDecorator) compile(ctx ModuleContext, flags Flags, deps Path
	fileName := binary.getStem(ctx) + ctx.toolchain().ExecutableSuffix()
	outputFile := android.PathForModuleOut(ctx, fileName)
	ret := buildOutput{outputFile: outputFile}
	var crateRootPath android.Path
	if binary.baseCompiler.Properties.Crate_root == nil {
		crateRootPath, _ = srcPathFromModuleSrcs(ctx, binary.baseCompiler.Properties.Srcs)
	} else {
		crateRootPath = android.PathForModuleSrc(ctx, *binary.baseCompiler.Properties.Crate_root)
	}
	crateRootPath := crateRootPath(ctx, binary)

	flags.RustFlags = append(flags.RustFlags, deps.depFlags...)
	flags.LinkFlags = append(flags.LinkFlags, deps.depLinkFlags...)
+4 −4
Original line number Diff line number Diff line
@@ -196,7 +196,7 @@ func rustEnvVars(ctx ModuleContext, deps PathDeps) []string {
	}

	if len(deps.SrcDeps) > 0 {
		moduleGenDir := ctx.RustModule().compiler.CargoOutDir()
		moduleGenDir := ctx.RustModule().compiler.cargoOutDir()
		// We must calculate an absolute path for OUT_DIR since Rust's include! macro (which normally consumes this)
		// assumes that paths are relative to the source file.
		var outDirPrefix string
@@ -215,13 +215,13 @@ func rustEnvVars(ctx ModuleContext, deps PathDeps) []string {

	envVars = append(envVars, "ANDROID_RUST_VERSION="+config.GetRustVersion(ctx))

	if ctx.RustModule().compiler.CargoEnvCompat() {
	if ctx.RustModule().compiler.cargoEnvCompat() {
		if bin, ok := ctx.RustModule().compiler.(*binaryDecorator); ok {
			envVars = append(envVars, "CARGO_BIN_NAME="+bin.getStem(ctx))
		}
		envVars = append(envVars, "CARGO_CRATE_NAME="+ctx.RustModule().CrateName())
		envVars = append(envVars, "CARGO_PKG_NAME="+ctx.RustModule().CrateName())
		pkgVersion := ctx.RustModule().compiler.CargoPkgVersion()
		pkgVersion := ctx.RustModule().compiler.cargoPkgVersion()
		if pkgVersion != "" {
			envVars = append(envVars, "CARGO_PKG_VERSION="+pkgVersion)

@@ -327,7 +327,7 @@ func transformSrctoCrate(ctx ModuleContext, main android.Path, deps PathDeps, fl
	orderOnly = append(orderOnly, deps.SharedLibs...)

	if len(deps.SrcDeps) > 0 {
		moduleGenDir := ctx.RustModule().compiler.CargoOutDir()
		moduleGenDir := ctx.RustModule().compiler.cargoOutDir()
		var outputs android.WritablePaths

		for _, genSrc := range deps.SrcDeps {
+88 −15
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@ package rust

import (
	"android/soong/cc"
	"errors"
	"fmt"
	"path/filepath"
	"strings"
@@ -34,6 +35,48 @@ const (
	DylibLinkage
)

type compiler interface {
	initialize(ctx ModuleContext)
	compilerFlags(ctx ModuleContext, flags Flags) Flags
	cfgFlags(ctx ModuleContext, flags Flags) Flags
	featureFlags(ctx ModuleContext, flags Flags) Flags
	compilerProps() []interface{}
	compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput
	compilerDeps(ctx DepsContext, deps Deps) Deps
	crateName() string
	edition() string
	features() []string
	rustdoc(ctx ModuleContext, flags Flags, deps PathDeps) android.OptionalPath

	// Output directory in which source-generated code from dependencies is
	// copied. This is equivalent to Cargo's OUT_DIR variable.
	cargoOutDir() android.OptionalPath

	// cargoPkgVersion returns the value of the Cargo_pkg_version property.
	cargoPkgVersion() string

	// cargoEnvCompat returns whether Cargo environment variables should be used.
	cargoEnvCompat() bool

	inData() bool
	install(ctx ModuleContext)
	relativeInstallPath() string
	everInstallable() bool

	nativeCoverage() bool

	Disabled() bool
	SetDisabled()

	stdLinkage(ctx *depsContext) RustLinkage
	noStdlibs() bool

	unstrippedOutputFilePath() android.Path
	strippedOutputFilePath() android.OptionalPath

	checkedCrateRootPath() (android.Path, error)
}

func (compiler *baseCompiler) edition() string {
	return proptools.StringDefault(compiler.Properties.Edition, config.DefaultEdition)
}
@@ -204,7 +247,16 @@ type baseCompiler struct {

	// If a crate has a source-generated dependency, a copy of the source file
	// will be available in cargoOutDir (equivalent to Cargo OUT_DIR).
	cargoOutDir android.ModuleOutPath
	// This is stored internally because it may not be available during
	// singleton-generation passes like rustdoc/rust_project.json, but should
	// be stashed during initial generation.
	cachedCargoOutDir android.ModuleOutPath
	// Calculated crate root cached internally because ModuleContext is not
	// available to singleton targets like rustdoc/rust_project.json
	cachedCrateRootPath android.Path
	// If cachedCrateRootPath is nil after initialization, this will contain
	// an explanation of why
	cachedCrateRootError error
}

func (compiler *baseCompiler) Disabled() bool {
@@ -257,9 +309,13 @@ func (compiler *baseCompiler) cfgsToFlags() []string {
	return flags
}

func (compiler *baseCompiler) features() []string {
	return compiler.Properties.Features
}

func (compiler *baseCompiler) featuresToFlags() []string {
	flags := []string{}
	for _, feature := range compiler.Properties.Features {
	for _, feature := range compiler.features() {
		flags = append(flags, "--cfg 'feature=\""+feature+"\"'")
	}

@@ -355,18 +411,24 @@ func (compiler *baseCompiler) rustdoc(ctx ModuleContext, flags Flags,
}

func (compiler *baseCompiler) initialize(ctx ModuleContext) {
	compiler.cargoOutDir = android.PathForModuleOut(ctx, genSubDir)
	compiler.cachedCargoOutDir = android.PathForModuleOut(ctx, genSubDir)
	if compiler.Properties.Crate_root == nil {
		compiler.cachedCrateRootPath, compiler.cachedCrateRootError = srcPathFromModuleSrcs(ctx, compiler.Properties.Srcs)
	} else {
		compiler.cachedCrateRootPath = android.PathForModuleSrc(ctx, *compiler.Properties.Crate_root)
		compiler.cachedCrateRootError = nil
	}
}

func (compiler *baseCompiler) CargoOutDir() android.OptionalPath {
	return android.OptionalPathForPath(compiler.cargoOutDir)
func (compiler *baseCompiler) cargoOutDir() android.OptionalPath {
	return android.OptionalPathForPath(compiler.cachedCargoOutDir)
}

func (compiler *baseCompiler) CargoEnvCompat() bool {
func (compiler *baseCompiler) cargoEnvCompat() bool {
	return Bool(compiler.Properties.Cargo_env_compat)
}

func (compiler *baseCompiler) CargoPkgVersion() string {
func (compiler *baseCompiler) cargoPkgVersion() string {
	return String(compiler.Properties.Cargo_pkg_version)
}

@@ -496,12 +558,20 @@ func (compiler *baseCompiler) relativeInstallPath() string {
	return String(compiler.Properties.Relative_install_path)
}

// Returns the Path for the main source file along with Paths for generated source files from modules listed in srcs.
func srcPathFromModuleSrcs(ctx ModuleContext, srcs []string) (android.Path, android.Paths) {
	if len(srcs) == 0 {
		ctx.PropertyErrorf("srcs", "srcs must not be empty")
func (compiler *baseCompiler) checkedCrateRootPath() (android.Path, error) {
	return compiler.cachedCrateRootPath, compiler.cachedCrateRootError
}

func crateRootPath(ctx ModuleContext, compiler compiler) android.Path {
	root, err := compiler.checkedCrateRootPath()
	if err != nil {
		ctx.PropertyErrorf("srcs", err.Error())
	}
	return root
}

// Returns the Path for the main source file along with Paths for generated source files from modules listed in srcs.
func srcPathFromModuleSrcs(ctx ModuleContext, srcs []string) (android.Path, error) {
	// The srcs can contain strings with prefix ":".
	// They are dependent modules of this module, with android.SourceDepTag.
	// They are not the main source file compiled by rustc.
@@ -514,19 +584,22 @@ func srcPathFromModuleSrcs(ctx ModuleContext, srcs []string) (android.Path, andr
		}
	}
	if numSrcs > 1 {
		ctx.PropertyErrorf("srcs", incorrectSourcesError)
		return nil, errors.New(incorrectSourcesError)
	}

	// If a main source file is not provided we expect only a single SourceProvider module to be defined
	// within srcs, with the expectation that the first source it provides is the entry point.
	if srcIndex != 0 {
		ctx.PropertyErrorf("srcs", "main source file must be the first in srcs")
		return nil, errors.New("main source file must be the first in srcs")
	} else if numSrcs > 1 {
		ctx.PropertyErrorf("srcs", "only a single generated source module can be defined without a main source file.")
		return nil, errors.New("only a single generated source module can be defined without a main source file.")
	}

	// TODO: b/297264540 - once all modules are sandboxed, we need to select the proper
	// entry point file from Srcs rather than taking the first one
	paths := android.PathsForModuleSrc(ctx, srcs)
	return paths[srcIndex], paths[1:]
	if len(paths) == 0 {
		return nil, errors.New("srcs must not be empty")
	}
	return paths[srcIndex], nil
}
+2 −1
Original line number Diff line number Diff line
@@ -67,6 +67,7 @@ func TestCfgsToFlags(t *testing.T) {
func TestEnforceSingleSourceFile(t *testing.T) {

	singleSrcError := "srcs can only contain one path for a rust file and source providers prefixed by \":\""
	prebuiltSingleSrcError := "prebuilt libraries can only have one entry in srcs"

	// Test libraries
	testRustError(t, singleSrcError, `
@@ -90,7 +91,7 @@ func TestEnforceSingleSourceFile(t *testing.T) {
		}`)

	// Test prebuilts
	testRustError(t, singleSrcError, `
	testRustError(t, prebuiltSingleSrcError, `
		rust_prebuilt_dylib {
			name: "foo-bar-prebuilt",
			srcs: ["liby.so", "libz.so"],
+10 −8
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@
package rust

import (
	"errors"
	"fmt"
	"regexp"
	"strings"
@@ -489,7 +490,7 @@ func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps Pa
	var outputFile android.ModuleOutPath
	var ret buildOutput
	var fileName string
	crateRootPath := library.crateRootPath(ctx, deps)
	crateRootPath := crateRootPath(ctx, library)

	if library.sourceProvider != nil {
		deps.srcProviderFiles = append(deps.srcProviderFiles, library.sourceProvider.Srcs()...)
@@ -584,15 +585,16 @@ func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps Pa
	return ret
}

func (library *libraryDecorator) crateRootPath(ctx ModuleContext, _ PathDeps) android.Path {
func (library *libraryDecorator) checkedCrateRootPath() (android.Path, error) {
	if library.sourceProvider != nil {
		srcs := library.sourceProvider.Srcs()
		if len(srcs) == 0 {
			return nil, errors.New("Source provider generated 0 sources")
		}
		// Assume the first source from the source provider is the library entry point.
		return library.sourceProvider.Srcs()[0]
	} else if library.baseCompiler.Properties.Crate_root == nil {
		path, _ := srcPathFromModuleSrcs(ctx, library.baseCompiler.Properties.Srcs)
		return path
		return srcs[0], nil
	} else {
		return android.PathForModuleSrc(ctx, *library.baseCompiler.Properties.Crate_root)
		return library.baseCompiler.checkedCrateRootPath()
	}
}

@@ -607,7 +609,7 @@ func (library *libraryDecorator) rustdoc(ctx ModuleContext, flags Flags,
		return android.OptionalPath{}
	}

	return android.OptionalPathForPath(Rustdoc(ctx, library.crateRootPath(ctx, deps),
	return android.OptionalPathForPath(Rustdoc(ctx, crateRootPath(ctx, library),
		deps, flags))
}

Loading