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

Commit 22a25d17 authored by Ivan Lozano's avatar Ivan Lozano Committed by Automerger Merge Worker
Browse files

Merge "rust: Add prefer_rlib property for static libstd." am: a7110748

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

Change-Id: I16653294ece344bb13059095427b78e47d57ccf4
parents 446ec378 a7110748
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -24,6 +24,10 @@ func init() {
}

type BinaryCompilerProperties struct {
	// Change the rustlibs linkage to select rlib linkage by default for device targets.
	// Also link libstd as an rlib as well on device targets.
	// Note: This is the default behavior for host targets.
	Prefer_rlib *bool `android:"arch_variant"`
}

type binaryDecorator struct {
@@ -131,9 +135,16 @@ func (binary *binaryDecorator) coverageOutputZipPath() android.OptionalPath {

func (binary *binaryDecorator) autoDep(ctx BaseModuleContext) autoDep {
	// Binaries default to dylib dependencies for device, rlib for host.
	if Bool(binary.Properties.Prefer_rlib) {
		return rlibAutoDep
	}
	if ctx.Device() {
		return dylibAutoDep
	} else {
		return rlibAutoDep
	}
}

func (binary *binaryDecorator) staticStd(ctx *depsContext) bool {
	return binary.baseCompiler.staticStd(ctx) || Bool(binary.Properties.Prefer_rlib)
}
+35 −0
Original line number Diff line number Diff line
@@ -30,6 +30,13 @@ func TestBinaryLinkage(t *testing.T) {
			rustlibs: ["libfoo"],
			host_supported: true,
		}
		rust_binary {
			name: "rlib_linked",
			srcs: ["foo.rs"],
			rustlibs: ["libfoo"],
			host_supported: true,
			prefer_rlib: true,
		}
		rust_library {
			name: "libfoo",
			srcs: ["foo.rs"],
@@ -49,6 +56,34 @@ func TestBinaryLinkage(t *testing.T) {
	}
}

// Test that prefer_rlib links in libstd statically as well as rustlibs.
func TestBinaryPreferRlib(t *testing.T) {
	ctx := testRust(t, `
		rust_binary {
			name: "rlib_linked",
			srcs: ["foo.rs"],
			rustlibs: ["libfoo"],
			host_supported: true,
			prefer_rlib: true,
		}
		rust_library {
			name: "libfoo",
			srcs: ["foo.rs"],
			crate_name: "foo",
			host_supported: true,
		}`)

	mod := ctx.ModuleForTests("rlib_linked", "android_arm64_armv8-a").Module().(*Module)

	if !android.InList("libfoo.rlib-std", mod.Properties.AndroidMkRlibs) {
		t.Errorf("rustlibs dependency libfoo should be an rlib dep when prefer_rlib is defined")
	}

	if !android.InList("libstd", mod.Properties.AndroidMkRlibs) {
		t.Errorf("libstd dependency should be an rlib dep when prefer_rlib is defined")
	}
}

// Test that the path returned by HostToolPath is correct
func TestHostToolPath(t *testing.T) {
	ctx := testRust(t, `