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

Commit 26ecd6c5 authored by Ivan Lozano's avatar Ivan Lozano
Browse files

[rust] Add SourceProviders as crates support.

This allows SourceProvider modules to create rust_library variants so
that generated source can be referenced as an external crate rather than
via an include macro. This is done by including rust_bindgen modules
like any other library, as a dependency in either rlibs, dylibs, or
rustlibs.

This renames the stem and flags properties for rust_bindgen modules to
source_stem and bindgen_flags, respectively. This deconflicts with the
usage in baseCompiler.

This also removes 'subName' from the Module struct and moves it over to
SourceProvider, which was the only user. This allows us to set it in
baseSourceProvider's AndroidMk; setting it in Module's AndroidMk was
causing problems finding NOTICE files for bindgen library variants.

Bug: 159064919
Test: New Soong tests pass.
Test: Local test rust_binary can use rust_bindgen module as a crate.

Change-Id: Ieb2cb614c2dd0b5aa7120541d77f6f822a6a1806
parent d118b1c2
Loading
Loading
Loading
Loading
+4 −2
Original line number Diff line number Diff line
@@ -55,7 +55,6 @@ func (mod *Module) AndroidMk() android.AndroidMkData {
	ret := android.AndroidMkData{
		OutputFile: mod.outputFile,
		Include:    "$(BUILD_SYSTEM)/soong_rust_prebuilt.mk",
		SubName:    mod.subName,
		Extra: []android.AndroidMkExtraFunc{
			func(w io.Writer, outputFile android.Path) {
				if len(mod.Properties.AndroidMkRlibs) > 0 {
@@ -76,9 +75,11 @@ func (mod *Module) AndroidMk() android.AndroidMkData {
			},
		},
	}
	if mod.compiler != nil {

	if mod.compiler != nil && !mod.compiler.Disabled() {
		mod.subAndroidMk(&ret, mod.compiler)
	} else if mod.sourceProvider != nil {
		// If the compiler is disabled, this is a SourceProvider.
		mod.subAndroidMk(&ret, mod.sourceProvider)
	}
	ret.SubName += mod.Properties.SubName
@@ -162,6 +163,7 @@ func (sourceProvider *baseSourceProvider) AndroidMk(ctx AndroidMkContext, ret *a
	outFile := sourceProvider.outputFile
	ret.Class = "ETC"
	ret.OutputFile = android.OptionalPathForPath(outFile)
	ret.SubName += sourceProvider.subName
	ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) {
		_, file := filepath.Split(outFile.String())
		stem, suffix, _ := android.SplitFileExt(file)
+8 −2
Original line number Diff line number Diff line
@@ -61,7 +61,7 @@ type BindgenProperties struct {
	Wrapper_src *string `android:"path,arch_variant"`

	// list of bindgen-specific flags and options
	Flags []string `android:"arch_variant"`
	Bindgen_flags []string `android:"arch_variant"`

	// list of clang flags required to correctly interpret the headers.
	Cflags []string `android:"arch_variant"`
@@ -121,7 +121,7 @@ func (b *bindgenDecorator) generateSource(ctx android.ModuleContext, deps PathDe
	}

	bindgenFlags := defaultBindgenFlags
	bindgenFlags = append(bindgenFlags, strings.Join(b.Properties.Flags, " "))
	bindgenFlags = append(bindgenFlags, strings.Join(b.Properties.Bindgen_flags, " "))

	wrapperFile := android.OptionalPathForModuleSrc(ctx, b.Properties.Wrapper_src)
	if !wrapperFile.Valid() {
@@ -170,7 +170,13 @@ func NewRustBindgen(hod android.HostOrDeviceSupported) (*Module, *bindgenDecorat
		baseSourceProvider: NewSourceProvider(),
		Properties:         BindgenProperties{},
	}

	_, library := NewRustLibrary(hod)
	library.BuildOnlyRust()
	library.sourceProvider = bindgen

	module.sourceProvider = bindgen
	module.compiler = library

	return module, bindgen
}
+4 −3
Original line number Diff line number Diff line
@@ -24,8 +24,10 @@ func TestRustBindgen(t *testing.T) {
		rust_bindgen {
			name: "libbindgen",
			wrapper_src: "src/any.h",
			stem: "bindings",
			flags: ["--bindgen-flag"],
			crate_name: "bindgen",
			stem: "libbindgen",
			source_stem: "bindings",
			bindgen_flags: ["--bindgen-flag"],
			cflags: ["--clang-flag"],
			shared_libs: ["libfoo_shared"],
			static_libs: ["libfoo_static"],
@@ -38,7 +40,6 @@ func TestRustBindgen(t *testing.T) {
			name: "libfoo_static",
			export_include_dirs: ["static_include"],
		}

	`)
	libbindgen := ctx.ModuleForTests("libbindgen", "android_arm64_armv8-a").Output("bindings.rs")
	if !strings.Contains(libbindgen.Args["flags"], "--bindgen-flag") {
+4 −2
Original line number Diff line number Diff line
@@ -28,12 +28,14 @@ func TestSourceProviderCollision(t *testing.T) {
		}
		rust_bindgen {
			name: "libbindings1",
			stem: "bindings",
			source_stem: "bindings",
			crate_name: "bindings1",
			wrapper_src: "src/any.h",
		}
		rust_bindgen {
			name: "libbindings2",
			stem: "bindings",
			source_stem: "bindings",
			crate_name: "bindings2",
			wrapper_src: "src/any.h",
		}
	`)
+12 −1
Original line number Diff line number Diff line
@@ -81,7 +81,10 @@ type BaseCompilerProperties struct {
	// list of C static library dependencies
	Static_libs []string `android:"arch_variant"`

	// crate name, required for libraries. This must be the expected extern crate name used in source
	// crate name, required for modules which produce Rust libraries: rust_library, rust_ffi and SourceProvider
	// modules which create library variants (rust_bindgen). This must be the expected extern crate name used in
	// source, and is required to conform to an enforced format matching library output files (if the output file is
	// lib<someName><suffix>, the crate_name property must be <someName>).
	Crate_name string `android:"arch_variant"`

	// list of features to enable for this crate
@@ -120,6 +123,14 @@ type baseCompiler struct {
	distFile              android.OptionalPath
}

func (compiler *baseCompiler) Disabled() bool {
	return false
}

func (compiler *baseCompiler) SetDisabled() {
	panic("baseCompiler does not implement SetDisabled()")
}

func (compiler *baseCompiler) coverageOutputZipPath() android.OptionalPath {
	panic("baseCompiler does not implement coverageOutputZipPath()")
}
Loading