Skip to content

Commit

Permalink
Add builtin rust platform decls with examples (#144)
Browse files Browse the repository at this point in the history
* Add builtin rust platform decls with examples

* Remove empty files

* Tidy up

* Add aliases for each OS and each CPU arch as well

* Tidy up
  • Loading branch information
acmcarther committed Oct 25, 2018
1 parent 2231755 commit 5ec0b83
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 10 deletions.
36 changes: 36 additions & 0 deletions examples/per_platform_printer/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package(default_visibility = ["//visibility:public"])

load(
"@io_bazel_rules_rust//rust:rust.bzl",
"rust_binary",
"rust_library",
"rust_test",
)

rust_binary(
name = "print",
srcs = ["main.rs"],
deps = [
":printer",
],
)

rust_library(
name = "printer",
srcs = [
"lib.rs",
"print_generic.rs",
] + select({
"@io_bazel_rules_rust//rust/platform:linux": [
":print_linux.rs",
],
"@io_bazel_rules_rust//rust/platform:osx": [
":print_osx.rs",
],
}),
)

rust_test(
name = "printer_test",
deps = [":printer"],
)
41 changes: 41 additions & 0 deletions examples/per_platform_printer/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
mod print_generic;

#[cfg(target_os = "linux")]
mod print_linux;

#[cfg(target_os="macos")]
mod print_osx;

pub fn print() -> Vec<String> {
let mut outs = Vec::new();

outs.push(print_generic::print());

#[cfg(target_os = "linux")]
outs.push(print_linux::print());

#[cfg(target_os="macos")]
outs.push(print_osx::print());

outs
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn prints_correctly() {
let outs = print();

assert_eq!(
outs,
vec![
"Hello Generic!",
#[cfg(target_os = "linux")]
"Hello Linux!",
#[cfg(target_os = "macos")]
"Hello OSX!",
]);
}
}
5 changes: 5 additions & 0 deletions examples/per_platform_printer/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
extern crate printer;

fn main() {
printer::print();
}
3 changes: 3 additions & 0 deletions examples/per_platform_printer/print_generic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn print() -> String {
"Hello Generic!".to_owned()
}
3 changes: 3 additions & 0 deletions examples/per_platform_printer/print_linux.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn print() -> String {
"Hello Linux!".to_owned()
}
3 changes: 3 additions & 0 deletions examples/per_platform_printer/print_osx.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn print() -> String {
"Hello OSX!".to_owned()
}
1 change: 0 additions & 1 deletion rust/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ exports_files([
"repositories.bzl",
"rust.bzl",
"toolchain.bzl",
"triple_mappings.bzl",
])

toolchain_type(
Expand Down
5 changes: 5 additions & 0 deletions rust/platform/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package(default_visibility = ["//visibility:public"])

load(":platform.bzl", "declare_config_settings")

declare_config_settings()
81 changes: 81 additions & 0 deletions rust/platform/platform.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
load(
":triple_mappings.bzl",
"cpu_arch_to_constraints",
"system_to_constraints",
"triple_to_constraint_set",
)

# All T1 Platforms should be supported, but aren't, see inline notes.
_SUPPORTED_T1_PLATFORM_TRIPLES = [
"i686-apple-darwin",
"i686-pc-windows-gnu",
"i686-unknown-linux-gnu",
"x86_64-apple-darwin",
"x86_64-pc-windows-gnu",
"x86_64-unknown-linux-gnu",
# N.B. These "alternative" envs are not supported, as bazel cannot distinguish between them
# and others using existing @bazel_tools//platforms config_values
#
#"i686-pc-windows-msvc",
#"x86_64-pc-windows-msvc",
]

# Some T2 Platforms are supported, provided we have mappings to @bazel_tools//platforms entries.
# See @io_bazel_rules_rust//rust/platform:triple_mappings.bzl for the complete list.
_SUPPORTED_T2_PLATFORM_TRIPLES = [
"aarch64-apple-ios",
"aarch64-linux-android",
"aarch64-unknown-linux-gnu",
"powerpc-unknown-linux-gnu",
"arm-unknown-linux-gnueabi",
"s390x-unknown-linux-gnu",
"i686-linux-android",
"i686-unknown-freebsd",
"x86_64-apple-ios",
"x86_64-linux-android",
"x86_64-unknown-freebsd",
]

_SUPPORTED_CPU_ARCH = [
"x86_64",
"powerpc",
"aarch64",
"arm",
"i686",
"s390x",
]

_SUPPORTED_SYSTEMS = [
"android",
"freebsd",
"darwin",
"ios",
"linux",
"windows",
]

def declare_config_settings():
for cpu_arch in _SUPPORTED_CPU_ARCH:
native.config_setting(
name = cpu_arch,
constraint_values = cpu_arch_to_constraints(cpu_arch),
)

for system in _SUPPORTED_SYSTEMS:
native.config_setting(
name = system,
constraint_values = system_to_constraints(system),
)

# Add alias for OSX to "darwin" to match what users will be expecting.
native.alias(
name = "osx",
actual = ":darwin",
)

all_supported_triples = _SUPPORTED_T1_PLATFORM_TRIPLES + _SUPPORTED_T2_PLATFORM_TRIPLES
for triple in all_supported_triples:
native.config_setting(
name = triple,
constraint_values = triple_to_constraint_set(triple),
)
16 changes: 8 additions & 8 deletions rust/triple_mappings.bzl → rust/platform/triple_mappings.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -63,30 +63,30 @@ _SYSTEM_TO_DYLIB_EXT = {
"emscripten": ".js",
}

def _cpu_arch_to_constraints(cpu_arch):
def cpu_arch_to_constraints(cpu_arch):
plat_suffix = _CPU_ARCH_TO_BUILTIN_PLAT_SUFFIX[cpu_arch]

if not plat_suffix:
fail("CPU architecture \"{}\" is not supported by rules_rust".format(cpu_arch))

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

def _vendor_to_constraints(vendor):
def vendor_to_constraints(vendor):
# TODO(acmcarther): Review:
#
# My current understanding is that vendors can't have a material impact on
# constraint sets.
return []

def _system_to_constraints(system):
def system_to_constraints(system):
sys_suffix = _SYSTEM_TO_BUILTIN_SYS_SUFFIX[system]

if not sys_suffix:
fail("System \"{}\" is not supported by rules_rust".format(sys_suffix))

return ["@bazel_tools//platforms:{}".format(sys_suffix)]

def _abi_to_constraints(abi):
def abi_to_constraints(abi):
# TODO(acmcarther): Implement when C++ toolchain is more mature and we
# figure out how they're doing this
return []
Expand Down Expand Up @@ -121,9 +121,9 @@ def triple_to_constraint_set(triple):
abi = component_parts[3]

constraint_set = []
constraint_set += _cpu_arch_to_constraints(cpu_arch)
constraint_set += _vendor_to_constraints(vendor)
constraint_set += _system_to_constraints(system)
constraint_set += _abi_to_constraints(abi)
constraint_set += cpu_arch_to_constraints(cpu_arch)
constraint_set += vendor_to_constraints(vendor)
constraint_set += system_to_constraints(system)
constraint_set += abi_to_constraints(abi)

return constraint_set
2 changes: 1 addition & 1 deletion rust/repositories.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load(":known_shas.bzl", "FILE_KEY_TO_SHA")
load(":triple_mappings.bzl", "system_to_binary_ext", "system_to_dylib_ext", "system_to_staticlib_ext", "triple_to_constraint_set", "triple_to_system")
load(":platform/triple_mappings.bzl", "system_to_binary_ext", "system_to_dylib_ext", "system_to_staticlib_ext", "triple_to_constraint_set", "triple_to_system")

DEFAULT_TOOLCHAIN_NAME_PREFIX = "toolchain_for"

Expand Down

0 comments on commit 5ec0b83

Please sign in to comment.