summaryrefslogtreecommitdiff
path: root/rust/compiler.go
diff options
context:
space:
mode:
author Ivan Lozano <ivanlozano@google.com> 2024-05-06 21:46:39 -0400
committer Ivan Lozano <ivanlozano@google.com> 2024-05-16 10:32:08 -0400
commit28ed8f4f83551ca651dd3831eb5b0a23567f5fa6 (patch)
tree032821bf5872ae175843c8eab8b9afc9d84b1204 /rust/compiler.go
parent08f670ab4a5e65278080a286246d10e886ae8a5f (diff)
rust: refactored transformSrctoCrate
Refactor transformSrctoCrate and the functions it calls to not rely on rust.ModuleContext, preparing it to be callable from soong-cc context to build a Rust staticlib. This also refactors out common default flags used when building libraries, again to prepare for building a Rust staticlib from a soong-cc context. Bug: 254469782 Test: m blueprint_tests && m Change-Id: I678f6fee989c61bb15c340b9887e4d1934991907
Diffstat (limited to 'rust/compiler.go')
-rw-r--r--rust/compiler.go86
1 files changed, 51 insertions, 35 deletions
diff --git a/rust/compiler.go b/rust/compiler.go
index 03fdf2b7a..5033fbac2 100644
--- a/rust/compiler.go
+++ b/rust/compiler.go
@@ -322,9 +322,9 @@ func (compiler *baseCompiler) compilerProps() []interface{} {
return []interface{}{&compiler.Properties}
}
-func (compiler *baseCompiler) cfgsToFlags() []string {
+func cfgsToFlags(cfgs []string) []string {
flags := []string{}
- for _, cfg := range compiler.Properties.Cfgs {
+ for _, cfg := range cfgs {
flags = append(flags, "--cfg '"+cfg+"'")
}
@@ -351,23 +351,61 @@ func (compiler *baseCompiler) featureFlags(ctx ModuleContext, flags Flags) Flags
return flags
}
-func (compiler *baseCompiler) cfgFlags(ctx ModuleContext, flags Flags) Flags {
- if ctx.RustModule().InVendorOrProduct() {
- compiler.Properties.Cfgs = append(compiler.Properties.Cfgs, "android_vndk")
- if ctx.RustModule().InVendor() {
- compiler.Properties.Cfgs = append(compiler.Properties.Cfgs, "android_vendor")
- } else if ctx.RustModule().InProduct() {
- compiler.Properties.Cfgs = append(compiler.Properties.Cfgs, "android_product")
+func CommonDefaultCfgFlags(flags Flags, vendor bool, product bool) Flags {
+ var cfgs []string
+ if vendor || product {
+ cfgs = append(cfgs, "android_vndk")
+ if vendor {
+ cfgs = append(cfgs, "android_vendor")
+ } else if product {
+ cfgs = append(cfgs, "android_product")
}
}
- flags.RustFlags = append(flags.RustFlags, compiler.cfgsToFlags()...)
- flags.RustdocFlags = append(flags.RustdocFlags, compiler.cfgsToFlags()...)
+ flags.RustFlags = append(flags.RustFlags, cfgsToFlags(cfgs)...)
+ flags.RustdocFlags = append(flags.RustdocFlags, cfgsToFlags(cfgs)...)
+ return flags
+}
+
+func (compiler *baseCompiler) cfgFlags(ctx ModuleContext, flags Flags) Flags {
+ flags = CommonDefaultCfgFlags(flags, ctx.RustModule().InVendor(), ctx.RustModule().InProduct())
+
+ flags.RustFlags = append(flags.RustFlags, cfgsToFlags(compiler.Properties.Cfgs)...)
+ flags.RustdocFlags = append(flags.RustdocFlags, cfgsToFlags(compiler.Properties.Cfgs)...)
+
+ return flags
+}
+
+func CommonDefaultFlags(ctx android.ModuleContext, toolchain config.Toolchain, flags Flags) Flags {
+ flags.GlobalRustFlags = append(flags.GlobalRustFlags, config.GlobalRustFlags...)
+ flags.GlobalRustFlags = append(flags.GlobalRustFlags, toolchain.ToolchainRustFlags())
+ flags.GlobalLinkFlags = append(flags.GlobalLinkFlags, toolchain.ToolchainLinkFlags())
+ flags.EmitXrefs = ctx.Config().EmitXrefRules()
+
+ if ctx.Host() && !ctx.Windows() {
+ flags.LinkFlags = append(flags.LinkFlags, cc.RpathFlags(ctx)...)
+ }
+
+ if ctx.Os() == android.Linux {
+ // Add -lc, -lrt, -ldl, -lpthread, -lm and -lgcc_s to glibc builds to match
+ // the default behavior of device builds.
+ flags.LinkFlags = append(flags.LinkFlags, config.LinuxHostGlobalLinkFlags...)
+ } else if ctx.Os() == android.Darwin {
+ // Add -lc, -ldl, -lpthread and -lm to glibc darwin builds to match the default
+ // behavior of device builds.
+ flags.LinkFlags = append(flags.LinkFlags,
+ "-lc",
+ "-ldl",
+ "-lpthread",
+ "-lm",
+ )
+ }
return flags
}
func (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags) Flags {
+ flags = CommonDefaultFlags(ctx, ctx.toolchain(), flags)
lintFlags, err := config.RustcLintsForDir(ctx.ModuleDir(), compiler.Properties.Lints)
if err != nil {
ctx.PropertyErrorf("lints", err.Error())
@@ -396,29 +434,7 @@ func (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags) Flag
flags.RustFlags = append(flags.RustFlags, "--edition="+compiler.edition())
flags.RustdocFlags = append(flags.RustdocFlags, "--edition="+compiler.edition())
flags.LinkFlags = append(flags.LinkFlags, compiler.Properties.Ld_flags...)
- flags.GlobalRustFlags = append(flags.GlobalRustFlags, config.GlobalRustFlags...)
- flags.GlobalRustFlags = append(flags.GlobalRustFlags, ctx.toolchain().ToolchainRustFlags())
- flags.GlobalLinkFlags = append(flags.GlobalLinkFlags, ctx.toolchain().ToolchainLinkFlags())
- flags.EmitXrefs = ctx.Config().EmitXrefRules()
- if ctx.Host() && !ctx.Windows() {
- flags.LinkFlags = append(flags.LinkFlags, cc.RpathFlags(ctx)...)
- }
-
- if ctx.Os() == android.Linux {
- // Add -lc, -lrt, -ldl, -lpthread, -lm and -lgcc_s to glibc builds to match
- // the default behavior of device builds.
- flags.LinkFlags = append(flags.LinkFlags, config.LinuxHostGlobalLinkFlags...)
- } else if ctx.Os() == android.Darwin {
- // Add -lc, -ldl, -lpthread and -lm to glibc darwin builds to match the default
- // behavior of device builds.
- flags.LinkFlags = append(flags.LinkFlags,
- "-lc",
- "-ldl",
- "-lpthread",
- "-lm",
- )
- }
return flags
}
@@ -568,11 +584,11 @@ func (compiler *baseCompiler) installTestData(ctx ModuleContext, data []android.
compiler.installDeps = append(compiler.installDeps, installedData...)
}
-func (compiler *baseCompiler) getStem(ctx ModuleContext) string {
+func (compiler *baseCompiler) getStem(ctx android.ModuleContext) string {
return compiler.getStemWithoutSuffix(ctx) + String(compiler.Properties.Suffix)
}
-func (compiler *baseCompiler) getStemWithoutSuffix(ctx BaseModuleContext) string {
+func (compiler *baseCompiler) getStemWithoutSuffix(ctx android.BaseModuleContext) string {
stem := ctx.ModuleName()
if String(compiler.Properties.Stem) != "" {
stem = String(compiler.Properties.Stem)