Skip to content

Commit

Permalink
Try to add bare-metal and RISC-V support
Browse files Browse the repository at this point in the history
Pursuand to bazelbuild#256.

- Add "cpu" constraint fulfillments for "riscv32imac" and "riscv64imac",
  matching the CPU strings available in the Rust toolchain for
  bare-metal builds.
- Add mappings for the "none" OS constraint, which is available upstream
  in bazelbuild/platforms.
  • Loading branch information
cceckman committed Jan 3, 2021
1 parent df18ddb commit a6ccf5a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
12 changes: 12 additions & 0 deletions rust/platform/cpu/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,15 @@ constraint_value(
constraint_setting = "@platforms//cpu",
visibility = ["//visibility:public"],
)

constraint_value(
name = "riscv32imac",
constraint_setting = "@platforms//cpu",
visibility = ["//visibility:public"],
)

constraint_value(
name = "riscv64imac",
constraint_setting = "@platforms//cpu",
visibility = ["//visibility:public"],
)
5 changes: 5 additions & 0 deletions rust/platform/platform.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@ _SUPPORTED_T2_PLATFORM_TRIPLES = [
"aarch64-apple-ios",
"aarch64-linux-android",
"aarch64-unknown-linux-gnu",
"aarch64-unknown-none",
"arm-unknown-linux-gnueabi",
"i686-linux-android",
"i686-unknown-freebsd",
"powerpc-unknown-linux-gnu",
"riscv32imac-unknown-none-elf",
"riscv64imac-unknown-none-elf",
"s390x-unknown-linux-gnu",
"wasm32-unknown-unknown",
"wasm32-wasi",
Expand All @@ -49,6 +52,8 @@ _SUPPORTED_CPU_ARCH = [
"powerpc",
"s390x",
"x86_64",
"riscv32imac",
"riscv64imac",
]

_SUPPORTED_SYSTEMS = [
Expand Down
23 changes: 19 additions & 4 deletions rust/platform/triple_mappings.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ _SYSTEM_TO_BUILTIN_SYS_SUFFIX = {
"windows": "windows",
"ios": "ios",
"android": "android",
"none": "none",
"emscripten": None,
"unknown": None,
"wasi": None,
Expand All @@ -53,6 +54,7 @@ _SYSTEM_TO_BINARY_EXT = {
# windows target
"unknown": ".wasm",
"wasi": ".wasm",
"none": "",
}

_SYSTEM_TO_STATICLIB_EXT = {
Expand All @@ -64,6 +66,9 @@ _SYSTEM_TO_STATICLIB_EXT = {
"emscripten": ".js",
"unknown": "",
"wasi": "",
# This reflects Rust's behavior for the aarch64-unknown-none target,
# which produces .a files when invoked to build a staticlib.
"none": ".a",
}

_SYSTEM_TO_DYLIB_EXT = {
Expand All @@ -73,8 +78,12 @@ _SYSTEM_TO_DYLIB_EXT = {
"ios": ".dylib",
"windows": ".dll",
"emscripten": ".js",
# This is currently a hack allowing us to have the proper
# generated extension for the wasm target, similarly to the
# windows target
"unknown": ".wasm",
"wasi": ".wasm",
"none": "",
}

# See https://github.com/rust-lang/rust/blob/master/src/libstd/build.rs
Expand Down Expand Up @@ -116,15 +125,21 @@ _SYSTEM_TO_STDLIB_LINKFLAGS = {
"cloudabi": ["-lunwind", "-lc", "-lcompiler_rt"],
"unknown": [],
"wasi": [],
# We don't actually expect the stdlib to build for the "none" system,
# but we don't need extra flags for it.
"none": [],
}

def cpu_arch_to_constraints(cpu_arch):
plat_suffix = _CPU_ARCH_TO_BUILTIN_PLAT_SUFFIX[cpu_arch]
plat_suffix = _CPU_ARCH_TO_BUILTIN_PLAT_SUFFIX.get(cpu_arch)

if plat_suffix:
return ["@platforms//cpu:{}".format(plat_suffix)]

if not plat_suffix:
fail("CPU architecture \"{}\" is not supported by rules_rust".format(cpu_arch))
if cpu_arch.startswith("riscv"):
return ["@io_bazel_rules_rust//rust/platform/cpu:{}".format(cpu_arch)]

return ["@platforms//cpu:{}".format(plat_suffix)]
fail("CPU architecture \"{}\" is not supported by rules_rust".format(cpu_arch))

def vendor_to_constraints(vendor):
# TODO(acmcarther): Review:
Expand Down

0 comments on commit a6ccf5a

Please sign in to comment.