Add Rust support to Soong.
Adds support to Soong for building rust modules. This currently only
supports x86_64 device and x86 linux host targets. The functionality
is sufficient to build crosvm.
Bug: 136189233
Test: Test module builds.
Test: crosvm builds.
Change-Id: I6ea04615834a6d673578ab10ea1a2eb04259fe09
diff --git a/rust/config/global.go b/rust/config/global.go
new file mode 100644
index 0000000..2e08a8c
--- /dev/null
+++ b/rust/config/global.go
@@ -0,0 +1,65 @@
+// Copyright 2019 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package config
+
+import (
+ "android/soong/android"
+ _ "android/soong/cc/config"
+)
+
+var pctx = android.NewPackageContext("android/soong/rust/config")
+
+var (
+ RustDefaultVersion = "1.35.0"
+ RustDefaultBase = "prebuilts/rust/"
+ DefaultEdition = "2018"
+ Stdlibs = []string{
+ "libarena",
+ "libfmt_macros",
+ "libgraphviz",
+ "libserialize",
+ "libstd",
+ "libsyntax",
+ "libsyntax_ext",
+ "libsyntax_pos",
+ "libterm",
+ }
+)
+
+func init() {
+ pctx.SourcePathVariable("RustDefaultBase", RustDefaultBase)
+ pctx.VariableConfigMethod("HostPrebuiltTag", android.Config.PrebuiltOS)
+
+ pctx.VariableFunc("RustBase", func(ctx android.PackageVarContext) string {
+ if override := ctx.Config().Getenv("RUST_PREBUILTS_BASE"); override != "" {
+ return override
+ }
+ return "${RustDefaultBase}"
+ })
+
+ pctx.VariableFunc("RustVersion", func(ctx android.PackageVarContext) string {
+ if override := ctx.Config().Getenv("RUST_PREBUILTS_VERSION"); override != "" {
+ return override
+ }
+ return RustDefaultVersion
+ })
+
+ pctx.StaticVariable("RustPath", "${RustBase}/${HostPrebuiltTag}/${RustVersion}")
+ pctx.StaticVariable("RustBin", "${RustPath}/bin")
+
+ pctx.ImportAs("ccConfig", "android/soong/cc/config")
+ pctx.StaticVariable("RustLinker", "${ccConfig.ClangBin}/clang++")
+ pctx.StaticVariable("RustLinkerArgs", "-B ${ccConfig.ClangBin} -fuse-ld=lld")
+}
diff --git a/rust/config/toolchain.go b/rust/config/toolchain.go
new file mode 100644
index 0000000..a36d61b
--- /dev/null
+++ b/rust/config/toolchain.go
@@ -0,0 +1,124 @@
+// Copyright 2019 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package config
+
+import (
+ "android/soong/android"
+)
+
+type Toolchain interface {
+ RustTriple() string
+ ToolchainRustFlags() string
+ ToolchainLinkFlags() string
+
+ SharedLibSuffix() string
+ StaticLibSuffix() string
+ RlibSuffix() string
+ DylibSuffix() string
+ ProcMacroSuffix() string
+ ExecutableSuffix() string
+
+ Is64Bit() bool
+ Supported() bool
+}
+
+type toolchainBase struct {
+}
+
+func (toolchainBase) RustTriple() string {
+ panic("toolchainBase does not define a triple.")
+}
+
+func (toolchainBase) ToolchainRustFlags() string {
+ panic("toolchainBase does not provide rust flags.")
+}
+
+func (toolchainBase) ToolchainLinkFlags() string {
+ panic("toolchainBase does not provide link flags.")
+}
+
+func (toolchainBase) Is64Bit() bool {
+ panic("toolchainBase cannot determine datapath width.")
+}
+
+type toolchain64Bit struct {
+ toolchainBase
+}
+
+func (toolchain64Bit) Is64Bit() bool {
+ return true
+}
+
+type toolchain32Bit struct {
+ toolchainBase
+}
+
+func (toolchain32Bit) Is64Bit() bool {
+ return false
+}
+
+func (toolchain32Bit) Bionic() bool {
+ return true
+}
+
+func (toolchainBase) ExecutableSuffix() string {
+ return ""
+}
+
+func (toolchainBase) SharedLibSuffix() string {
+ return ".so"
+}
+
+func (toolchainBase) StaticLibSuffix() string {
+ return ".a"
+}
+
+func (toolchainBase) RlibSuffix() string {
+ return ".rlib"
+}
+func (toolchainBase) DylibSuffix() string {
+ return ".so"
+}
+
+func (toolchainBase) ProcMacroSuffix() string {
+ return ".so"
+}
+
+func (toolchainBase) Supported() bool {
+ return false
+}
+
+func toolchainBaseFactory() Toolchain {
+ return &toolchainBase{}
+}
+
+type toolchainFactory func(arch android.Arch) Toolchain
+
+var toolchainFactories = make(map[android.OsType]map[android.ArchType]toolchainFactory)
+
+func registerToolchainFactory(os android.OsType, arch android.ArchType, factory toolchainFactory) {
+ if toolchainFactories[os] == nil {
+ toolchainFactories[os] = make(map[android.ArchType]toolchainFactory)
+ }
+ toolchainFactories[os][arch] = factory
+}
+
+func FindToolchain(os android.OsType, arch android.Arch) Toolchain {
+ factory := toolchainFactories[os][arch.ArchType]
+ if factory == nil {
+ return toolchainBaseFactory()
+ }
+ return factory(arch)
+}
diff --git a/rust/config/x86_64_device.go b/rust/config/x86_64_device.go
new file mode 100644
index 0000000..2aca56a
--- /dev/null
+++ b/rust/config/x86_64_device.go
@@ -0,0 +1,88 @@
+// Copyright 2019 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package config
+
+import (
+ "strings"
+
+ "android/soong/android"
+)
+
+var (
+ x86_64RustFlags = []string{}
+ x86_64ArchFeatureRustFlags = map[string][]string{}
+ x86_64LinkFlags = []string{}
+
+ x86_64ArchVariantRustFlags = map[string][]string{
+ "": []string{},
+ "broadwell": []string{"-C target-cpu=broadwell"},
+ "haswell": []string{"-C target-cpu=haswell"},
+ "ivybridge": []string{"-C target-cpu=ivybridge"},
+ "sandybridge": []string{"-C target-cpu=sandybridge"},
+ "silvermont": []string{"-C target-cpu=silvermont"},
+ "skylake": []string{"-C target-cpu=skylake"},
+ //TODO: Add target-cpu=stoneyridge when rustc supports it.
+ "stoneyridge": []string{""},
+ }
+)
+
+func init() {
+ registerToolchainFactory(android.Android, android.X86_64, x86_64ToolchainFactory)
+
+ pctx.StaticVariable("x86_64ToolchainRustFlags", strings.Join(x86_64RustFlags, " "))
+ pctx.StaticVariable("x86_64ToolchainLinkFlags", strings.Join(x86_64LinkFlags, " "))
+
+ for variant, rustFlags := range x86_64ArchVariantRustFlags {
+ pctx.StaticVariable("X86_64"+variant+"VariantRustFlags",
+ strings.Join(rustFlags, " "))
+ }
+
+}
+
+type toolchainX86_64 struct {
+ toolchain64Bit
+ toolchainRustFlags string
+}
+
+func (t *toolchainX86_64) RustTriple() string {
+ return "x86_64-unknown-linux-gnu"
+}
+
+func (t *toolchainX86_64) ToolchainLinkFlags() string {
+ return "${config.x86_64ToolchainLinkFlags}"
+}
+
+func (t *toolchainX86_64) ToolchainRustFlags() string {
+ return t.toolchainRustFlags
+}
+
+func (t *toolchainX86_64) RustFlags() string {
+ return "${config.x86_64ToolchainRustFlags}"
+}
+
+func x86_64ToolchainFactory(arch android.Arch) Toolchain {
+ toolchainRustFlags := []string{
+ "${config.x86_64ToolchainRustFlags}",
+ "${config.X86_64" + arch.ArchVariant + "VariantRustFlags}",
+ }
+
+ for _, feature := range arch.ArchFeatures {
+ toolchainRustFlags = append(toolchainRustFlags, x86_64ArchFeatureRustFlags[feature]...)
+ }
+
+ return &toolchainX86_64{
+ toolchainRustFlags: strings.Join(toolchainRustFlags, " "),
+ }
+}
diff --git a/rust/config/x86_linux_host.go b/rust/config/x86_linux_host.go
new file mode 100644
index 0000000..cb6bf1a
--- /dev/null
+++ b/rust/config/x86_linux_host.go
@@ -0,0 +1,109 @@
+// Copyright 2019 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package config
+
+import (
+ "strings"
+
+ "android/soong/android"
+)
+
+var (
+ LinuxRustFlags = []string{}
+ LinuxRustLinkFlags = []string{}
+ linuxX86Rustflags = []string{}
+ linuxX86Linkflags = []string{}
+ linuxX8664Rustflags = []string{}
+ linuxX8664Linkflags = []string{}
+)
+
+func init() {
+ registerToolchainFactory(android.Linux, android.X86_64, linuxX8664ToolchainFactory)
+ registerToolchainFactory(android.Linux, android.X86, linuxX86ToolchainFactory)
+
+ pctx.StaticVariable("LinuxToolchainRustFlags", strings.Join(LinuxRustFlags, " "))
+ pctx.StaticVariable("LinuxToolchainLinkFlags", strings.Join(LinuxRustLinkFlags, " "))
+ pctx.StaticVariable("LinuxToolchainX86RustFlags", strings.Join(linuxX86Rustflags, " "))
+ pctx.StaticVariable("LinuxToolchainX86LinkFlags", strings.Join(linuxX86Linkflags, " "))
+ pctx.StaticVariable("LinuxToolchainX8664RustFlags", strings.Join(linuxX8664Rustflags, " "))
+ pctx.StaticVariable("LinuxToolchainX8664LinkFlags", strings.Join(linuxX8664Linkflags, " "))
+
+}
+
+type toolchainLinux struct {
+ toolchainRustFlags string
+ toolchainLinkFlags string
+}
+
+type toolchainLinuxX86 struct {
+ toolchain32Bit
+ toolchainLinux
+}
+
+type toolchainLinuxX8664 struct {
+ toolchain64Bit
+ toolchainLinux
+}
+
+func (toolchainLinuxX8664) Supported() bool {
+ return true
+}
+
+func (t *toolchainLinuxX8664) Name() string {
+ return "x86_64"
+}
+
+func (t *toolchainLinuxX8664) RustTriple() string {
+ return "x86_64-unknown-linux-gnu"
+}
+
+func (t *toolchainLinuxX8664) ToolchainLinkFlags() string {
+ return "${config.LinuxToolchainLinkFlags} ${config.LinuxToolchainX8664LinkFlags}"
+}
+
+func (t *toolchainLinuxX8664) ToolchainRustFlags() string {
+ return "${config.LinuxToolchainRustFlags} ${config.LinuxToolchainX8664RustFlags}"
+}
+
+func linuxX8664ToolchainFactory(arch android.Arch) Toolchain {
+ return toolchainLinuxX8664Singleton
+}
+
+func (toolchainLinuxX86) Supported() bool {
+ return true
+}
+
+func (t *toolchainLinuxX86) Name() string {
+ return "x86"
+}
+
+func (t *toolchainLinuxX86) RustTriple() string {
+ return "i686-unknown-linux-gnu"
+}
+
+func (t *toolchainLinuxX86) ToolchainLinkFlags() string {
+ return "${config.LinuxToolchainLinkFlags} ${config.LinuxToolchainX86LinkFlags}"
+}
+
+func (t *toolchainLinuxX86) ToolchainRustFlags() string {
+ return "${config.LinuxToolchainRustFlags} ${config.LinuxToolchainX86RustFlags}"
+}
+
+func linuxX86ToolchainFactory(arch android.Arch) Toolchain {
+ return toolchainLinuxX86Singleton
+}
+
+var toolchainLinuxX8664Singleton Toolchain = &toolchainLinuxX8664{}
+var toolchainLinuxX86Singleton Toolchain = &toolchainLinuxX86{}