From 5e2a50e05fea9bbb697bb430a1f2d6484a89a44b Mon Sep 17 00:00:00 2001 From: Piotr Sikora Date: Wed, 4 Nov 2020 23:21:23 +0000 Subject: [PATCH] Show getrandom and chrono/time usage in examples. Signed-off-by: Piotr Sikora --- Cargo.toml | 4 + bazel/cargo/BUILD.bazel | 18 ++++ bazel/cargo/Cargo.lock | 31 +++++- bazel/cargo/crates.bzl | 30 ++++++ bazel/cargo/remote/BUILD.cfg-if-1.0.0.bazel | 54 +++++++++++ .../cargo/remote/BUILD.getrandom-0.2.0.bazel | 96 +++++++++++++++++++ ...LD.wasi-0.9.0+wasi-snapshot-preview1.bazel | 54 +++++++++++ examples/BUILD | 6 +- examples/hello_world.rs | 18 +++- 9 files changed, 305 insertions(+), 6 deletions(-) create mode 100644 bazel/cargo/remote/BUILD.cfg-if-1.0.0.bazel create mode 100644 bazel/cargo/remote/BUILD.getrandom-0.2.0.bazel create mode 100644 bazel/cargo/remote/BUILD.wasi-0.9.0+wasi-snapshot-preview1.bazel diff --git a/Cargo.toml b/Cargo.toml index 299166e0..cfbcaefd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,8 +17,12 @@ log = "0.4" wee_alloc = { version = "0.4", optional = true } [dev-dependencies] +cfg-if = "1.0" chrono = "0.4" +[target.'cfg(not(all(target_arch = "wasm32", target_os = "unknown")))'.dev-dependencies] +getrandom = "0.2" + [profile.release] lto = true opt-level = 3 diff --git a/bazel/cargo/BUILD.bazel b/bazel/cargo/BUILD.bazel index 1bc21aa1..bd1ed422 100644 --- a/bazel/cargo/BUILD.bazel +++ b/bazel/cargo/BUILD.bazel @@ -12,6 +12,15 @@ licenses([ ]) # Aliased targets +alias( + name = "cfg_if", + actual = "@raze__cfg_if__1_0_0//:cfg_if", + tags = [ + "cargo-raze", + "manual", + ], +) + alias( name = "chrono", actual = "@raze__chrono__0_4_19//:chrono", @@ -21,6 +30,15 @@ alias( ], ) +alias( + name = "getrandom", + actual = "@raze__getrandom__0_2_0//:getrandom", + tags = [ + "cargo-raze", + "manual", + ], +) + alias( name = "hashbrown", actual = "@raze__hashbrown__0_9_1//:hashbrown", diff --git a/bazel/cargo/Cargo.lock b/bazel/cargo/Cargo.lock index 9d618368..65fd749b 100644 --- a/bazel/cargo/Cargo.lock +++ b/bazel/cargo/Cargo.lock @@ -18,6 +18,12 @@ version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + [[package]] name = "chrono" version = "0.4.19" @@ -31,6 +37,17 @@ dependencies = [ "winapi", ] +[[package]] +name = "getrandom" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee8025cf36f917e6a52cce185b7c7177689b838b7ec138364e50cc2277a56cf4" +dependencies = [ + "cfg-if 0.1.10", + "libc", + "wasi 0.9.0+wasi-snapshot-preview1", +] + [[package]] name = "hashbrown" version = "0.9.1" @@ -52,7 +69,7 @@ version = "0.4.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", ] [[package]] @@ -84,7 +101,9 @@ dependencies = [ name = "proxy-wasm" version = "0.1.2" dependencies = [ + "cfg-if 1.0.0", "chrono", + "getrandom", "hashbrown", "log", "wee_alloc", @@ -97,10 +116,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" dependencies = [ "libc", - "wasi", + "wasi 0.10.0+wasi-snapshot-preview1", "winapi", ] +[[package]] +name = "wasi" +version = "0.9.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" + [[package]] name = "wasi" version = "0.10.0+wasi-snapshot-preview1" @@ -113,7 +138,7 @@ version = "0.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dbb3b5a6b2bb17cb6ad44a2e68a43e8d2722c997da10e928665c72ec6c0a0b8e" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", "libc", "memory_units", "winapi", diff --git a/bazel/cargo/crates.bzl b/bazel/cargo/crates.bzl index 15831dc0..0d580df1 100644 --- a/bazel/cargo/crates.bzl +++ b/bazel/cargo/crates.bzl @@ -41,6 +41,16 @@ def raze_fetch_remote_crates(): build_file = Label("//bazel/cargo/remote:BUILD.cfg-if-0.1.10.bazel"), ) + maybe( + http_archive, + name = "raze__cfg_if__1_0_0", + url = "https://crates.io/api/v1/crates/cfg-if/1.0.0/download", + type = "tar.gz", + sha256 = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd", + strip_prefix = "cfg-if-1.0.0", + build_file = Label("//bazel/cargo/remote:BUILD.cfg-if-1.0.0.bazel"), + ) + maybe( http_archive, name = "raze__chrono__0_4_19", @@ -51,6 +61,16 @@ def raze_fetch_remote_crates(): build_file = Label("//bazel/cargo/remote:BUILD.chrono-0.4.19.bazel"), ) + maybe( + http_archive, + name = "raze__getrandom__0_2_0", + url = "https://crates.io/api/v1/crates/getrandom/0.2.0/download", + type = "tar.gz", + sha256 = "ee8025cf36f917e6a52cce185b7c7177689b838b7ec138364e50cc2277a56cf4", + strip_prefix = "getrandom-0.2.0", + build_file = Label("//bazel/cargo/remote:BUILD.getrandom-0.2.0.bazel"), + ) + maybe( http_archive, name = "raze__hashbrown__0_9_1", @@ -121,6 +141,16 @@ def raze_fetch_remote_crates(): build_file = Label("//bazel/cargo/remote:BUILD.wasi-0.10.0+wasi-snapshot-preview1.bazel"), ) + maybe( + http_archive, + name = "raze__wasi__0_9_0_wasi_snapshot_preview1", + url = "https://crates.io/api/v1/crates/wasi/0.9.0+wasi-snapshot-preview1/download", + type = "tar.gz", + sha256 = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519", + strip_prefix = "wasi-0.9.0+wasi-snapshot-preview1", + build_file = Label("//bazel/cargo/remote:BUILD.wasi-0.9.0+wasi-snapshot-preview1.bazel"), + ) + maybe( http_archive, name = "raze__winapi__0_3_9", diff --git a/bazel/cargo/remote/BUILD.cfg-if-1.0.0.bazel b/bazel/cargo/remote/BUILD.cfg-if-1.0.0.bazel new file mode 100644 index 00000000..6e3db87e --- /dev/null +++ b/bazel/cargo/remote/BUILD.cfg-if-1.0.0.bazel @@ -0,0 +1,54 @@ +""" +@generated +cargo-raze crate build file. + +DO NOT EDIT! Replaced on runs of cargo-raze +""" + +# buildifier: disable=load +load( + "@io_bazel_rules_rust//rust:rust.bzl", + "rust_binary", + "rust_library", + "rust_test", +) + +# buildifier: disable=load +load("@bazel_skylib//lib:selects.bzl", "selects") + +package(default_visibility = [ + # Public for visibility by "@raze__crate__version//" targets. + # + # Prefer access through "//bazel/cargo", which limits external + # visibility to explicit Cargo.toml dependencies. + "//visibility:public", +]) + +licenses([ + "notice", # MIT from expression "MIT OR Apache-2.0" +]) + +# Generated Targets + +rust_library( + name = "cfg_if", + srcs = glob(["**/*.rs"]), + crate_features = [ + ], + crate_root = "src/lib.rs", + crate_type = "lib", + edition = "2018", + rustc_flags = [ + "--cap-lints=allow", + ], + tags = [ + "cargo-raze", + "manual", + ], + version = "1.0.0", + # buildifier: leave-alone + deps = [ + ], +) + +# Unsupported target "xcrate" with type "test" omitted diff --git a/bazel/cargo/remote/BUILD.getrandom-0.2.0.bazel b/bazel/cargo/remote/BUILD.getrandom-0.2.0.bazel new file mode 100644 index 00000000..d91deb02 --- /dev/null +++ b/bazel/cargo/remote/BUILD.getrandom-0.2.0.bazel @@ -0,0 +1,96 @@ +""" +@generated +cargo-raze crate build file. + +DO NOT EDIT! Replaced on runs of cargo-raze +""" + +# buildifier: disable=load +load( + "@io_bazel_rules_rust//rust:rust.bzl", + "rust_binary", + "rust_library", + "rust_test", +) + +# buildifier: disable=load +load("@bazel_skylib//lib:selects.bzl", "selects") + +package(default_visibility = [ + # Public for visibility by "@raze__crate__version//" targets. + # + # Prefer access through "//bazel/cargo", which limits external + # visibility to explicit Cargo.toml dependencies. + "//visibility:public", +]) + +licenses([ + "notice", # MIT from expression "MIT OR Apache-2.0" +]) + +# Generated Targets + +# Unsupported target "mod" with type "bench" omitted + +# Unsupported target "build-script-build" with type "custom-build" omitted + +rust_library( + name = "getrandom", + srcs = glob(["**/*.rs"]), + aliases = { + }, + crate_features = [ + ], + crate_root = "src/lib.rs", + crate_type = "lib", + edition = "2018", + rustc_flags = [ + "--cap-lints=allow", + ], + tags = [ + "cargo-raze", + "manual", + ], + version = "0.2.0", + # buildifier: leave-alone + deps = [ + "@raze__cfg_if__0_1_10//:cfg_if", + ] + selects.with_or({ + # cfg(all(target_arch = "wasm32", target_os = "unknown", not(cargo_web))) + ( + "@io_bazel_rules_rust//rust/platform:wasm32-unknown-unknown", + ): [ + ], + "//conditions:default": [], + }) + selects.with_or({ + # cfg(target_os = "wasi") + ( + "@io_bazel_rules_rust//rust/platform:wasm32-wasi", + ): [ + "@raze__wasi__0_9_0_wasi_snapshot_preview1//:wasi", + ], + "//conditions:default": [], + }) + selects.with_or({ + # cfg(unix) + ( + "@io_bazel_rules_rust//rust/platform:aarch64-apple-ios", + "@io_bazel_rules_rust//rust/platform:aarch64-linux-android", + "@io_bazel_rules_rust//rust/platform:aarch64-unknown-linux-gnu", + "@io_bazel_rules_rust//rust/platform:arm-unknown-linux-gnueabi", + "@io_bazel_rules_rust//rust/platform:i686-apple-darwin", + "@io_bazel_rules_rust//rust/platform:i686-linux-android", + "@io_bazel_rules_rust//rust/platform:i686-unknown-freebsd", + "@io_bazel_rules_rust//rust/platform:i686-unknown-linux-gnu", + "@io_bazel_rules_rust//rust/platform:powerpc-unknown-linux-gnu", + "@io_bazel_rules_rust//rust/platform:s390x-unknown-linux-gnu", + "@io_bazel_rules_rust//rust/platform:x86_64-apple-darwin", + "@io_bazel_rules_rust//rust/platform:x86_64-apple-ios", + "@io_bazel_rules_rust//rust/platform:x86_64-linux-android", + "@io_bazel_rules_rust//rust/platform:x86_64-unknown-freebsd", + "@io_bazel_rules_rust//rust/platform:x86_64-unknown-linux-gnu", + ): [ + "@raze__libc__0_2_80//:libc", + ], + "//conditions:default": [], + }), +) diff --git a/bazel/cargo/remote/BUILD.wasi-0.9.0+wasi-snapshot-preview1.bazel b/bazel/cargo/remote/BUILD.wasi-0.9.0+wasi-snapshot-preview1.bazel new file mode 100644 index 00000000..013a1c1c --- /dev/null +++ b/bazel/cargo/remote/BUILD.wasi-0.9.0+wasi-snapshot-preview1.bazel @@ -0,0 +1,54 @@ +""" +@generated +cargo-raze crate build file. + +DO NOT EDIT! Replaced on runs of cargo-raze +""" + +# buildifier: disable=load +load( + "@io_bazel_rules_rust//rust:rust.bzl", + "rust_binary", + "rust_library", + "rust_test", +) + +# buildifier: disable=load +load("@bazel_skylib//lib:selects.bzl", "selects") + +package(default_visibility = [ + # Public for visibility by "@raze__crate__version//" targets. + # + # Prefer access through "//bazel/cargo", which limits external + # visibility to explicit Cargo.toml dependencies. + "//visibility:public", +]) + +licenses([ + "notice", # Apache-2.0 from expression "Apache-2.0 OR (Apache-2.0 OR MIT)" +]) + +# Generated Targets + +rust_library( + name = "wasi", + srcs = glob(["**/*.rs"]), + crate_features = [ + "default", + "std", + ], + crate_root = "src/lib.rs", + crate_type = "lib", + edition = "2018", + rustc_flags = [ + "--cap-lints=allow", + ], + tags = [ + "cargo-raze", + "manual", + ], + version = "0.9.0+wasi-snapshot-preview1", + # buildifier: leave-alone + deps = [ + ], +) diff --git a/examples/BUILD b/examples/BUILD index 4901dd94..047cae40 100644 --- a/examples/BUILD +++ b/examples/BUILD @@ -8,9 +8,13 @@ rust_binary( out_binary = True, deps = [ "//:proxy_wasm", + "//bazel/cargo:cfg_if", "//bazel/cargo:chrono", "//bazel/cargo:log", - ], + ] + select({ + "@io_bazel_rules_rust//rust/platform:wasm32-unknown-unknown": [], + "//conditions:default": ["//bazel/cargo:getrandom"], + }), ) rust_binary( diff --git a/examples/hello_world.rs b/examples/hello_world.rs index 8f439540..b493aa1b 100644 --- a/examples/hello_world.rs +++ b/examples/hello_world.rs @@ -12,12 +12,16 @@ // See the License for the specific language governing permissions and // limitations under the License. +use cfg_if::cfg_if; use chrono::{DateTime, Utc}; use log::info; use proxy_wasm::traits::*; use proxy_wasm::types::*; use std::time::Duration; +#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))] +use getrandom::getrandom; + #[no_mangle] pub fn _start() { proxy_wasm::set_log_level(LogLevel::Trace); @@ -36,7 +40,17 @@ impl RootContext for HelloWorld { } fn on_tick(&mut self) { - let datetime: DateTime = self.get_current_time().into(); - info!("It's {}", datetime); + cfg_if! { + if #[cfg(all(target_arch = "wasm32", target_os = "unknown"))] { + let now: DateTime = self.get_current_time().into(); + info!("It's {}, there is no lucky number.", now); + + } else { + let now: DateTime = Utc::now(); + let mut buf = [0u8; 1]; + getrandom(&mut buf).unwrap(); + info!("It's {}, your lucky number is {}.", now, buf[0]); + } + } } }