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

Commit dd7107c8 authored by Ivan Lozano's avatar Ivan Lozano Committed by Gerrit Code Review
Browse files

Merge "rust: Add support to emit certain Cargo env vars."

parents fc30cfc7 a9a1fc07
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -269,6 +269,17 @@ func transformSrctoCrate(ctx ModuleContext, main android.Path, deps PathDeps, fl

	envVars = append(envVars, "ANDROID_RUST_VERSION="+config.RustDefaultVersion)

	if ctx.RustModule().compiler.CargoEnvCompat() {
		if _, ok := ctx.RustModule().compiler.(*binaryDecorator); ok {
			envVars = append(envVars, "CARGO_BIN_NAME="+strings.TrimSuffix(outputFile.Base(), outputFile.Ext()))
		}
		envVars = append(envVars, "CARGO_CRATE_NAME="+ctx.RustModule().CrateName())
		pkgVersion := ctx.RustModule().compiler.CargoPkgVersion()
		if pkgVersion != "" {
			envVars = append(envVars, "CARGO_PKG_VERSION="+pkgVersion)
		}
	}

	if flags.Clippy {
		clippyFile := android.PathForModuleOut(ctx, outputFile.Base()+".clippy")
		ctx.Build(pctx, android.BuildParams{
+16 −0
Original line number Diff line number Diff line
@@ -154,6 +154,14 @@ type BaseCompilerProperties struct {
	// linkage if all dependencies of the root binary module do not link against libstd\
	// the same way.
	Prefer_rlib *bool `android:"arch_variant"`

	// Enables emitting certain Cargo environment variables. Only intended to be used for compatibility purposes.
	// Will set CARGO_CRATE_NAME to the crate_name property's value.
	// Will set CARGO_BIN_NAME to the output filename value without the extension.
	Cargo_env_compat *bool

	// If cargo_env_compat is true, sets the CARGO_PKG_VERSION env var to this value.
	Cargo_pkg_version *string
}

type baseCompiler struct {
@@ -309,6 +317,14 @@ func (compiler *baseCompiler) CargoOutDir() android.OptionalPath {
	return android.OptionalPathForPath(compiler.cargoOutDir)
}

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

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

func (compiler *baseCompiler) strippedOutputFilePath() android.OptionalPath {
	return compiler.strippedOutputFile
}
+24 −0
Original line number Diff line number Diff line
@@ -98,6 +98,30 @@ func TestEnforceSingleSourceFile(t *testing.T) {
		}`)
}

// Test environment vars for Cargo compat are set.
func TestCargoCompat(t *testing.T) {
	ctx := testRust(t, `
		rust_binary {
			name: "fizz",
			srcs: ["foo.rs"],
			crate_name: "foo",
			cargo_env_compat: true,
			cargo_pkg_version: "1.0.0"
		}`)

	fizz := ctx.ModuleForTests("fizz", "android_arm64_armv8-a").Rule("rustc")

	if !strings.Contains(fizz.Args["envVars"], "CARGO_BIN_NAME=fizz") {
		t.Fatalf("expected 'CARGO_BIN_NAME=fizz' in envVars, actual envVars: %#v", fizz.Args["envVars"])
	}
	if !strings.Contains(fizz.Args["envVars"], "CARGO_CRATE_NAME=foo") {
		t.Fatalf("expected 'CARGO_CRATE_NAME=foo' in envVars, actual envVars: %#v", fizz.Args["envVars"])
	}
	if !strings.Contains(fizz.Args["envVars"], "CARGO_PKG_VERSION=1.0.0") {
		t.Fatalf("expected 'CARGO_PKG_VERSION=1.0.0' in envVars, actual envVars: %#v", fizz.Args["envVars"])
	}
}

func TestInstallDir(t *testing.T) {
	ctx := testRust(t, `
		rust_library_dylib {
+6 −0
Original line number Diff line number Diff line
@@ -434,6 +434,12 @@ type compiler interface {
	// 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