-
Notifications
You must be signed in to change notification settings - Fork 402
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add builtin rust platform decls with examples (#144)
* 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
1 parent
2231755
commit 5ec0b83
Showing
11 changed files
with
186 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!", | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
extern crate printer; | ||
|
||
fn main() { | ||
printer::print(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub fn print() -> String { | ||
"Hello Generic!".to_owned() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub fn print() -> String { | ||
"Hello Linux!".to_owned() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub fn print() -> String { | ||
"Hello OSX!".to_owned() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters