From 6f72876b26de8b6cca0cd4f56df1c08cdbbf18d0 Mon Sep 17 00:00:00 2001 From: Yuval Kohavi Date: Mon, 5 Oct 2020 10:47:54 -0400 Subject: [PATCH 01/12] allow building on non-wasm archs Signed-off-by: Yuval Kohavi --- BUILD | 14 ++++++++++++-- Cargo.toml | 4 +++- src/lib.rs | 1 + 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/BUILD b/BUILD index f323a4bf..1f829a0b 100644 --- a/BUILD +++ b/BUILD @@ -1,5 +1,13 @@ load("@io_bazel_rules_rust//rust:rust.bzl", "rust_library") +config_setting( + name = "wasm", + values = { + "platforms": "@io_bazel_rules_rust//rust/platform:wasm", + }, +) + + rust_library( name = "proxy_wasm", srcs = glob(["src/*.rs"]), @@ -8,6 +16,8 @@ rust_library( deps = [ "//bazel/cargo:hashbrown", "//bazel/cargo:log", - "//bazel/cargo:wee_alloc", - ], + ] + select({ + ":wasm": ["//bazel/cargo:wee_alloc"], + "//conditions:default": [] + }), ) diff --git a/Cargo.toml b/Cargo.toml index e47b572f..c7cb3f3b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,9 @@ edition = "2018" [dependencies] hashbrown = { version = "0.8", default-features = false, features = ["ahash", "inline-more"] } log = "0.4" -wee_alloc = "0.4" + +[target.'cfg(target_arch = "wasm32")'.dependencies.wee_alloc] +version = "0.4" [dev-dependencies] chrono = "0.4" diff --git a/src/lib.rs b/src/lib.rs index cee98b72..54d3a5b3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,6 +16,7 @@ pub mod hostcalls; pub mod traits; pub mod types; +#[cfg(target_arch = "wasm32")] mod allocator; mod dispatcher; mod logger; From 0441a6df171ed9e2d69bb8e70b3f2edcdbd49182 Mon Sep 17 00:00:00 2001 From: Yuval Kohavi Date: Mon, 5 Oct 2020 11:46:55 -0400 Subject: [PATCH 02/12] format fix Signed-off-by: Yuval Kohavi --- BUILD | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/BUILD b/BUILD index 1f829a0b..84d25266 100644 --- a/BUILD +++ b/BUILD @@ -7,7 +7,6 @@ config_setting( }, ) - rust_library( name = "proxy_wasm", srcs = glob(["src/*.rs"]), @@ -18,6 +17,6 @@ rust_library( "//bazel/cargo:log", ] + select({ ":wasm": ["//bazel/cargo:wee_alloc"], - "//conditions:default": [] + "//conditions:default": [], }), ) From 61a5295eb9bda08a159f33c54b02ded356c579a5 Mon Sep 17 00:00:00 2001 From: Yuval Kohavi Date: Tue, 3 Nov 2020 16:02:09 -0500 Subject: [PATCH 03/12] wee alloc as feature Signed-off-by: Yuval Kohavi --- Cargo.toml | 5 +++-- src/lib.rs | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c7cb3f3b..4ddd3d66 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,9 +11,10 @@ edition = "2018" [dependencies] hashbrown = { version = "0.8", default-features = false, features = ["ahash", "inline-more"] } log = "0.4" +wee_alloc = { version = "0.4", optional = true } -[target.'cfg(target_arch = "wasm32")'.dependencies.wee_alloc] -version = "0.4" +[features] +wee_alloc = ["wee_alloc"] [dev-dependencies] chrono = "0.4" diff --git a/src/lib.rs b/src/lib.rs index 54d3a5b3..af371644 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,7 +16,7 @@ pub mod hostcalls; pub mod traits; pub mod types; -#[cfg(target_arch = "wasm32")] +#[cfg(feature = "wee_alloc")] mod allocator; mod dispatcher; mod logger; From a9dd4b03d2e93b96066ce1dac2b41673660d48de Mon Sep 17 00:00:00 2001 From: Yuval Kohavi Date: Tue, 3 Nov 2020 16:06:16 -0500 Subject: [PATCH 04/12] fix name Signed-off-by: Yuval Kohavi --- Cargo.toml | 2 +- src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4ddd3d66..3861e6b9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,7 @@ log = "0.4" wee_alloc = { version = "0.4", optional = true } [features] -wee_alloc = ["wee_alloc"] +wee-alloc = ["wee_alloc"] [dev-dependencies] chrono = "0.4" diff --git a/src/lib.rs b/src/lib.rs index af371644..6ea21d94 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,7 +16,7 @@ pub mod hostcalls; pub mod traits; pub mod types; -#[cfg(feature = "wee_alloc")] +#[cfg(feature = "wee-alloc")] mod allocator; mod dispatcher; mod logger; From 7f330a97c586b3fa5e7a74dffc238be6b8c6f4d5 Mon Sep 17 00:00:00 2001 From: Yuval Kohavi Date: Wed, 4 Nov 2020 10:53:57 -0500 Subject: [PATCH 05/12] pr comments Signed-off-by: Yuval Kohavi --- Cargo.toml | 6 +++--- README.md | 11 +++++++++++ src/allocator.rs | 1 + src/lib.rs | 1 - 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3861e6b9..54bbefd6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,14 +8,14 @@ license = "Apache-2.0" repository = "https://github.com/proxy-wasm/proxy-wasm-rust-sdk" edition = "2018" +[features] +wee-alloc = ["wee_alloc"] + [dependencies] hashbrown = { version = "0.8", default-features = false, features = ["ahash", "inline-more"] } log = "0.4" wee_alloc = { version = "0.4", optional = true } -[features] -wee-alloc = ["wee_alloc"] - [dev-dependencies] chrono = "0.4" diff --git a/README.md b/README.md index c2823fe7..b6a16d8a 100644 --- a/README.md +++ b/README.md @@ -25,3 +25,14 @@ + [Extending Envoy with WASM and Rust](https://antweiss.com/blog/extending-envoy-with-wasm-and-rust/) + [Extending Istio with Rust and WebAssembly](https://blog.red-badger.com/extending-istio-with-rust-and-webassembly) + +# Updating dependencies + +When updating dependencies, You need to regenerate `BUILD` files to match updated `Cargo.toml`: +``` +cargo install cargo-raze --version 0.3.8 +rm -rf bazel/cargo/ +cargo generate-lockfile +cargo raze --output=bazel/cargo +mv Cargo.lock bazel/cargo/ +``` \ No newline at end of file diff --git a/src/allocator.rs b/src/allocator.rs index 5e66543b..134a1f18 100644 --- a/src/allocator.rs +++ b/src/allocator.rs @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +#[cfg(feature = "wee-alloc")] #[global_allocator] static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; diff --git a/src/lib.rs b/src/lib.rs index 6ea21d94..cee98b72 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,7 +16,6 @@ pub mod hostcalls; pub mod traits; pub mod types; -#[cfg(feature = "wee-alloc")] mod allocator; mod dispatcher; mod logger; From e312307f6a73248281d29532e020b77bbc61d4b4 Mon Sep 17 00:00:00 2001 From: Yuval Kohavi Date: Wed, 4 Nov 2020 10:54:39 -0500 Subject: [PATCH 06/12] regen raze Signed-off-by: Yuval Kohavi --- bazel/cargo/BUILD | 5 --- bazel/cargo/crates.bzl | 18 -------- bazel/cargo/remote/memory_units-0.4.0.BUILD | 43 ------------------ bazel/cargo/remote/wee_alloc-0.4.5.BUILD | 48 --------------------- bazel/cargo/remote/winapi-0.3.9.BUILD | 3 -- 5 files changed, 117 deletions(-) delete mode 100644 bazel/cargo/remote/memory_units-0.4.0.BUILD delete mode 100644 bazel/cargo/remote/wee_alloc-0.4.5.BUILD diff --git a/bazel/cargo/BUILD b/bazel/cargo/BUILD index 4a095b98..b72a27f6 100644 --- a/bazel/cargo/BUILD +++ b/bazel/cargo/BUILD @@ -23,8 +23,3 @@ alias( actual = "@raze__log__0_4_11//:log", tags = ["cargo-raze"], ) -alias( - name = "wee_alloc", - actual = "@raze__wee_alloc__0_4_5//:wee_alloc", - tags = ["cargo-raze"], -) diff --git a/bazel/cargo/crates.bzl b/bazel/cargo/crates.bzl index e133346e..d7dbf9bb 100644 --- a/bazel/cargo/crates.bzl +++ b/bazel/cargo/crates.bzl @@ -79,15 +79,6 @@ def raze_fetch_remote_crates(): build_file = Label("//bazel/cargo/remote:log-0.4.11.BUILD"), ) - _new_http_archive( - name = "raze__memory_units__0_4_0", - url = "https://crates-io.s3-us-west-1.amazonaws.com/crates/memory_units/memory_units-0.4.0.crate", - type = "tar.gz", - sha256 = "8452105ba047068f40ff7093dd1d9da90898e63dd61736462e9cdda6a90ad3c3", - strip_prefix = "memory_units-0.4.0", - build_file = Label("//bazel/cargo/remote:memory_units-0.4.0.BUILD"), - ) - _new_http_archive( name = "raze__num_integer__0_1_44", url = "https://crates-io.s3-us-west-1.amazonaws.com/crates/num-integer/num-integer-0.1.44.crate", @@ -124,15 +115,6 @@ def raze_fetch_remote_crates(): build_file = Label("//bazel/cargo/remote:wasi-0.10.0+wasi-snapshot-preview1.BUILD"), ) - _new_http_archive( - name = "raze__wee_alloc__0_4_5", - url = "https://crates-io.s3-us-west-1.amazonaws.com/crates/wee_alloc/wee_alloc-0.4.5.crate", - type = "tar.gz", - sha256 = "dbb3b5a6b2bb17cb6ad44a2e68a43e8d2722c997da10e928665c72ec6c0a0b8e", - strip_prefix = "wee_alloc-0.4.5", - build_file = Label("//bazel/cargo/remote:wee_alloc-0.4.5.BUILD"), - ) - _new_http_archive( name = "raze__winapi__0_3_9", url = "https://crates-io.s3-us-west-1.amazonaws.com/crates/winapi/winapi-0.3.9.crate", diff --git a/bazel/cargo/remote/memory_units-0.4.0.BUILD b/bazel/cargo/remote/memory_units-0.4.0.BUILD deleted file mode 100644 index a8964bfa..00000000 --- a/bazel/cargo/remote/memory_units-0.4.0.BUILD +++ /dev/null @@ -1,43 +0,0 @@ -""" -cargo-raze crate build file. - -DO NOT EDIT! Replaced on runs of cargo-raze -""" -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([ - "reciprocal", # MPL-2.0 from expression "MPL-2.0" -]) - -load( - "@io_bazel_rules_rust//rust:rust.bzl", - "rust_library", - "rust_binary", - "rust_test", -) - - - -rust_library( - name = "memory_units", - crate_type = "lib", - deps = [ - ], - srcs = glob(["**/*.rs"]), - crate_root = "src/lib.rs", - edition = "2015", - rustc_flags = [ - "--cap-lints=allow", - ], - version = "0.4.0", - tags = ["cargo-raze"], - crate_features = [ - ], -) - diff --git a/bazel/cargo/remote/wee_alloc-0.4.5.BUILD b/bazel/cargo/remote/wee_alloc-0.4.5.BUILD deleted file mode 100644 index 26e2bc58..00000000 --- a/bazel/cargo/remote/wee_alloc-0.4.5.BUILD +++ /dev/null @@ -1,48 +0,0 @@ -""" -cargo-raze crate build file. - -DO NOT EDIT! Replaced on runs of cargo-raze -""" -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([ - "reciprocal", # MPL-2.0 from expression "MPL-2.0" -]) - -load( - "@io_bazel_rules_rust//rust:rust.bzl", - "rust_library", - "rust_binary", - "rust_test", -) - - -# Unsupported target "build-script-build" with type "custom-build" omitted - -rust_library( - name = "wee_alloc", - crate_type = "lib", - deps = [ - "@raze__cfg_if__0_1_10//:cfg_if", - "@raze__memory_units__0_4_0//:memory_units", - ], - srcs = glob(["**/*.rs"]), - crate_root = "src/lib.rs", - edition = "2015", - rustc_flags = [ - "--cap-lints=allow", - ], - version = "0.4.5", - tags = ["cargo-raze"], - crate_features = [ - "default", - "size_classes", - ], -) - diff --git a/bazel/cargo/remote/winapi-0.3.9.BUILD b/bazel/cargo/remote/winapi-0.3.9.BUILD index 49b1e59a..ed409429 100644 --- a/bazel/cargo/remote/winapi-0.3.9.BUILD +++ b/bazel/cargo/remote/winapi-0.3.9.BUILD @@ -39,16 +39,13 @@ rust_library( version = "0.3.9", tags = ["cargo-raze"], crate_features = [ - "memoryapi", "minwinbase", "minwindef", "ntdef", "profileapi", "std", - "synchapi", "sysinfoapi", "timezoneapi", - "winbase", ], ) From 826a9f84789adc2cb74849f93e481cd43c2759f0 Mon Sep 17 00:00:00 2001 From: Yuval Kohavi Date: Wed, 4 Nov 2020 10:56:29 -0500 Subject: [PATCH 07/12] skip wee_alloc Signed-off-by: Yuval Kohavi --- BUILD | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/BUILD b/BUILD index 84d25266..53d433f6 100644 --- a/BUILD +++ b/BUILD @@ -1,12 +1,5 @@ load("@io_bazel_rules_rust//rust:rust.bzl", "rust_library") -config_setting( - name = "wasm", - values = { - "platforms": "@io_bazel_rules_rust//rust/platform:wasm", - }, -) - rust_library( name = "proxy_wasm", srcs = glob(["src/*.rs"]), @@ -15,8 +8,5 @@ rust_library( deps = [ "//bazel/cargo:hashbrown", "//bazel/cargo:log", - ] + select({ - ":wasm": ["//bazel/cargo:wee_alloc"], - "//conditions:default": [], - }), + ], ) From 885451244d10a4643b8846424b43525c74e347a4 Mon Sep 17 00:00:00 2001 From: Yuval Kohavi Date: Wed, 4 Nov 2020 12:08:21 -0500 Subject: [PATCH 08/12] only export malloc on wasm Signed-off-by: Yuval Kohavi --- src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib.rs b/src/lib.rs index cee98b72..1f04745f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,6 +16,7 @@ pub mod hostcalls; pub mod traits; pub mod types; +#[cfg(target_arch = "wasm32")] mod allocator; mod dispatcher; mod logger; From 322c173088290f563f4b4d1091d3a572162cc9c3 Mon Sep 17 00:00:00 2001 From: Yuval Kohavi Date: Wed, 4 Nov 2020 12:11:43 -0500 Subject: [PATCH 09/12] fmt Signed-off-by: Yuval Kohavi --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 1f04745f..54d3a5b3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,7 +16,7 @@ pub mod hostcalls; pub mod traits; pub mod types; -#[cfg(target_arch = "wasm32")] +#[cfg(target_arch = "wasm32")] mod allocator; mod dispatcher; mod logger; From 0e02e1253d1e79171bdb59a51de0645b49b1d9c1 Mon Sep 17 00:00:00 2001 From: Yuval Kohavi Date: Wed, 4 Nov 2020 15:12:46 -0500 Subject: [PATCH 10/12] no need to guard allocator now that there is no malloc collision Signed-off-by: Yuval Kohavi --- src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 54d3a5b3..cee98b72 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,7 +16,6 @@ pub mod hostcalls; pub mod traits; pub mod types; -#[cfg(target_arch = "wasm32")] mod allocator; mod dispatcher; mod logger; From dd21cd621e853eaceecb0cfe24c2594fee95fc9d Mon Sep 17 00:00:00 2001 From: Yuval Kohavi Date: Wed, 4 Nov 2020 15:42:52 -0500 Subject: [PATCH 11/12] Add CI targets Signed-off-by: Yuval Kohavi --- .github/workflows/rust.yml | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 8340af02..3eb26b97 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -106,11 +106,21 @@ jobs: RUSTFLAGS: -D warnings -C link-args=-S run: cargo build --release --all-targets --target=wasm32-wasi + - name: Build (wasm32-wasi with wee-alloc) + env: + RUSTFLAGS: -D warnings -C link-args=-S + run: cargo build --release --all-targets --target=wasm32-wasi --features=wee-alloc + - name: Clippy (wasm32-wasi) env: RUSTFLAGS: -D warnings -C link-args=-S run: cargo clippy --release --all-targets --target=wasm32-wasi + - name: Clippy (wasm32-wasi with wee-alloc)) + env: + RUSTFLAGS: -D warnings -C link-args=-S + run: cargo clippy --release --all-targets --target=wasm32-wasi --features=wee-alloc + - name: Format (rustfmt) run: cargo fmt -- --check @@ -151,11 +161,21 @@ jobs: RUSTFLAGS: -D warnings -C link-args=-S run: cargo build --release --all-targets --target=wasm32-wasi + - name: Build (wasm32-wasi with wee-alloc) + env: + RUSTFLAGS: -D warnings -C link-args=-S + run: cargo build --release --all-targets --target=wasm32-wasi --features=wee-alloc + - name: Clippy (wasm32-wasi) env: RUSTFLAGS: -D warnings -C link-args=-S run: cargo clippy --release --all-targets --target=wasm32-wasi + - name: Clippy (wasm32-wasi with wee-alloc)) + env: + RUSTFLAGS: -D warnings -C link-args=-S + run: cargo clippy --release --all-targets --target=wasm32-wasi --features=wee-alloc + - name: Format (rustfmt) run: cargo fmt -- --check From 0ac354f23c708fe5ed212ef7178c6a8dedb9f65d Mon Sep 17 00:00:00 2001 From: Yuval Kohavi Date: Wed, 4 Nov 2020 16:22:38 -0500 Subject: [PATCH 12/12] pr comment Signed-off-by: Yuval Kohavi --- .github/workflows/rust.yml | 4 ++-- README.md | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 3eb26b97..30086c6c 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -116,7 +116,7 @@ jobs: RUSTFLAGS: -D warnings -C link-args=-S run: cargo clippy --release --all-targets --target=wasm32-wasi - - name: Clippy (wasm32-wasi with wee-alloc)) + - name: Clippy (wasm32-wasi with wee-alloc) env: RUSTFLAGS: -D warnings -C link-args=-S run: cargo clippy --release --all-targets --target=wasm32-wasi --features=wee-alloc @@ -171,7 +171,7 @@ jobs: RUSTFLAGS: -D warnings -C link-args=-S run: cargo clippy --release --all-targets --target=wasm32-wasi - - name: Clippy (wasm32-wasi with wee-alloc)) + - name: Clippy (wasm32-wasi with wee-alloc) env: RUSTFLAGS: -D warnings -C link-args=-S run: cargo clippy --release --all-targets --target=wasm32-wasi --features=wee-alloc diff --git a/README.md b/README.md index b6a16d8a..002b7764 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ # Updating dependencies -When updating dependencies, You need to regenerate `BUILD` files to match updated `Cargo.toml`: +When updating dependencies, you need to regenerate `BUILD` files to match updated `Cargo.toml`: ``` cargo install cargo-raze --version 0.3.8 rm -rf bazel/cargo/