-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(build) add bazel rules to build ngx-wasm-rs and ngx-rust #392
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,7 @@ | ||
build --incompatible_enable_cc_toolchain_resolution | ||
|
||
common --color=yes | ||
common --curses=auto | ||
|
||
build --show_timestamps | ||
build --worker_verbose |
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 @@ | ||
6.1.0 |
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,35 @@ | ||
name: Bazel build | ||
|
||
on: | ||
pull_request: | ||
paths-ignore: | ||
# ignore markdown files (CHANGELOG.md, README.md, etc.) | ||
- '**/*.md' | ||
push: | ||
paths-ignore: | ||
# ignore markdown files (CHANGELOG.md, README.md, etc.) | ||
- '**/*.md' | ||
branches: | ||
- main | ||
|
||
concurrency: | ||
group: ${{ github.workflow }}-${{ github.ref }} | ||
cancel-in-progress: ${{ github.event_name == 'pull_request' }} | ||
|
||
jobs: | ||
tests: | ||
name: Tests | ||
strategy: | ||
matrix: | ||
os: [ubuntu-latest] | ||
runs-on: ${{ matrix.os }} | ||
|
||
steps: | ||
- name: Checkout source code | ||
uses: actions/checkout@v3 | ||
|
||
- name: Build | ||
run: bazel build :ngx_rust :ngx_wasm_rs_static :ngx_wasm_rs_shared --verbose_failures | ||
|
||
- name: Display artifacts | ||
run: ls -lh bazel-bin/ | ||
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
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,145 @@ | ||
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_shared_library", "rust_static_library") | ||
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag") | ||
load("@ngx_rust_crate_index//:defs.bzl", ngx_rust_aliases = "aliases", ngx_rust_all_crate_deps = "all_crate_deps") | ||
load("@ngx_wasm_rs_crate_index//:defs.bzl", ngx_wasm_rs_aliases = "aliases", ngx_wasm_rs_all_crate_deps = "all_crate_deps") | ||
load("//build:deps.bzl", "normalize_name") | ||
|
||
filegroup( | ||
name = "nginx_module_srcs", | ||
srcs = glob(["src/**"]), | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
filegroup( | ||
name = "lua_libs", | ||
srcs = glob(["lib/resty/**"]), | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
filegroup( | ||
name = "v8bridge_srcs", | ||
srcs = glob(["lib/v8bridge/**"]), | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
filegroup( | ||
name = "ngx_wasm_rs_headers", | ||
srcs = glob(["lib/ngx-wasm-rs/include/**"]), | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
# --//:ngx-wasm-rust-feature-wat=false | ||
bool_flag( | ||
name = "ngx-wasm-rust-feature-wat", | ||
build_setting_default = False, | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
config_setting( | ||
name = "ngx-wasm-rust-feature-wat_flag", | ||
flag_values = { | ||
":ngx-wasm-rust-feature-wat": "true", | ||
}, | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
# ngx-rust | ||
|
||
filegroup( | ||
name = "ngx_rust_srcs", | ||
srcs = glob( | ||
include = ["lib/ngx-rust/**"], | ||
exclude = ["*.bazel"], | ||
), | ||
) | ||
|
||
rust_library( | ||
name = "ngx_rust", | ||
srcs = [":ngx_rust_srcs"], | ||
aliases = ngx_rust_aliases(), | ||
proc_macro_deps = ngx_rust_all_crate_deps( | ||
proc_macro = True, | ||
), | ||
visibility = ["//visibility:public"], | ||
deps = ngx_rust_all_crate_deps( | ||
normal = True, | ||
), | ||
) | ||
|
||
# ngx-wasm-rs deps | ||
|
||
# crate: the dependencies of the crate | ||
ngx_wasm_rs_deps = { | ||
"backtrace": ["c-api"], | ||
"c-api": [], | ||
"wat": ["c-api"], | ||
} | ||
|
||
[filegroup( | ||
name = "ngx_wasm_rs_deps_%s_srcs" % normalize_name(dep), | ||
srcs = glob( | ||
include = ["lib/ngx-wasm-rs/lib/%s/**" % dep], | ||
exclude = ["*.bazel"], | ||
), | ||
) for dep in ngx_wasm_rs_deps] | ||
|
||
[rust_library( | ||
name = "ngx_wasm_rs_deps_%s" % normalize_name(dep), | ||
srcs = [":ngx_wasm_rs_deps_%s_srcs" % normalize_name(dep)], | ||
aliases = ngx_wasm_rs_aliases() | { | ||
":ngx_wasm_rs_deps_%s" % normalize_name(d): "ngx_wasm_%s" % normalize_name(d) | ||
for d in ngx_wasm_rs_deps[dep] | ||
}, | ||
proc_macro_deps = ngx_wasm_rs_all_crate_deps( | ||
proc_macro = True, | ||
), | ||
deps = ngx_wasm_rs_all_crate_deps( | ||
normal = True, | ||
) + [":ngx_wasm_rs_deps_%s" % normalize_name(d) for d in ngx_wasm_rs_deps[dep]], | ||
) for dep in ngx_wasm_rs_deps] | ||
|
||
filegroup( | ||
name = "ngx_wasm_rs_srcs", | ||
srcs = glob( | ||
include = ["lib/ngx-wasm-rs/src/**"], | ||
exclude = ["*.bazel"], | ||
), | ||
) | ||
|
||
# ngx-wasm-rs | ||
|
||
rust_library_types = { | ||
"static": rust_static_library, | ||
"shared": rust_shared_library, | ||
} | ||
|
||
[rust_library_types[_type]( | ||
name = _type == "static" and "ngx_wasm_rs_" + _type or "ngx_wasm_rs", | ||
srcs = [":ngx_wasm_rs_srcs"], | ||
aliases = ngx_wasm_rs_aliases() | { | ||
":ngx_wasm_rs_deps_backtrace": "ngx_wasm_backtrace", | ||
} | select({ | ||
":ngx-wasm-rust-feature-wat_flag": { | ||
":ngx_wasm_rs_deps_wat": "ngx_wasm_wat", | ||
}, | ||
"//conditions:default": {}, | ||
}), | ||
crate_name = "ngx_wasm_rs", # force output filename to be libngx_wasm_rs.{a,so} | ||
proc_macro_deps = ngx_wasm_rs_all_crate_deps( | ||
proc_macro = True, | ||
), | ||
visibility = ["//visibility:public"], | ||
deps = ngx_wasm_rs_all_crate_deps( | ||
build = True, | ||
normal = True, | ||
) + [":ngx_wasm_rs_deps_backtrace"] + select({ | ||
":ngx-wasm-rust-feature-wat_flag": [":ngx_wasm_rs_deps_wat"], | ||
"//conditions:default": [], | ||
}), | ||
) for _type in rust_library_types] | ||
|
||
alias( | ||
name = "ngx_wasm_rs_shared", | ||
actual = ":ngx_wasm_rs", | ||
visibility = ["//visibility:public"], | ||
) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,13 @@ | ||
workspace(name = "ngx_wasm_module") | ||
|
||
load("//build:repos.bzl", "ngx_wasm_module_repositories") | ||
|
||
ngx_wasm_module_repositories() | ||
|
||
load("//build:deps.bzl", "ngx_wasm_module_dependencies") | ||
|
||
ngx_wasm_module_dependencies(cargo_home_isolated = False) # use system `$CARGO_HOME` to speed up builds | ||
|
||
load("//build:crates.bzl", "ngx_wasm_module_crates") | ||
|
||
ngx_wasm_module_crates() |
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 @@ | ||
package(default_visibility = ["//visibility:public"]) |
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,55 @@ | ||
# Bazel project for ngx_wasm_module Rust crates | ||
|
||
Note that you only need to import this rule when using either `wasmer` or `v8` runtime, which | ||
requires the `ngx_wasm_rs` library. This rule is not required for `wasmtime` runtime. | ||
|
||
## Integration | ||
|
||
To use in other Bazel projects, add the following to your WORKSPACE file: | ||
|
||
```python | ||
|
||
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository") | ||
|
||
git_repository( | ||
name = "ngx_wasm_module", | ||
branch = "some-tag", | ||
remote = "https://github.com/Kong/ngx_wasm_module", | ||
) | ||
|
||
load("@ngx_wasm_module//build:repos.bzl", "ngx_wasm_module_repositories") | ||
|
||
ngx_wasm_module_repositories() | ||
|
||
load("@ngx_wasm_module//build:deps.bzl", "ngx_wasm_module_dependencies") | ||
|
||
ngx_wasm_module_dependencies(cargo_home_isolated = False) # use system `$CARGO_HOME` to speed up builds | ||
|
||
load("@ngx_wasm_module//build:crates.bzl", "ngx_wasm_module_crates") | ||
|
||
ngx_wasm_module_crates() | ||
|
||
``` | ||
|
||
The following example shows how to build OpenResty with ngx_wasm_module with `wasmer` as runtime and link dynamically | ||
to the `ngx_wasm_rs` library: | ||
|
||
```python | ||
configure_make( | ||
name = "openresty", | ||
env = { | ||
"NGX_WASM_RUNTIME": "wasmer", | ||
"NGX_WASM_CARGO": "0", | ||
} | ||
configure_options = [ | ||
"--with-cc-opt=\"-I$$EXT_BUILD_DEPS$$/ngx_wasm_rs/include\"", | ||
"--with-ld-opt=\"-L$$EXT_BUILD_DEPS$$/ngx_wasm_rs/lib\"", | ||
], | ||
# ... | ||
deps = [ | ||
"@ngx_wasm_module//:ngx_wasm_rs", | ||
], | ||
) | ||
``` | ||
|
||
When building this library in Bazel, use the `-c opt` flag to ensure optimal performance. The default fastbuild mode produces a less performant binary. | ||
fffonion marked this conversation as resolved.
Show resolved
Hide resolved
|
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,8 @@ | ||
"""Setup Crates repostories """ | ||
|
||
load("@ngx_rust_crate_index//:defs.bzl", ngx_rust_crate_repositories = "crate_repositories") | ||
load("@ngx_wasm_rs_crate_index//:defs.bzl", ngx_wasm_rs_crate_repositories = "crate_repositories") | ||
|
||
def ngx_wasm_module_crates(): | ||
ngx_rust_crate_repositories() | ||
ngx_wasm_rs_crate_repositories() |
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,52 @@ | ||
"""Setup dependencies after repostories are downloaded.""" | ||
|
||
load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains", "rust_repository_set") | ||
load("@rules_rust//crate_universe:repositories.bzl", "crate_universe_dependencies") | ||
load("@rules_rust//crate_universe:defs.bzl", "crates_repository") | ||
|
||
def ngx_wasm_module_dependencies(cargo_home_isolated = True): | ||
""" | ||
ngx_wasm_module_dependencies setup rust toolchain and cargo dependency repositories. | ||
|
||
Args: | ||
cargo_home_isolated (bool): cargo_home_isolated to False to reuse system CARGO_HOME | ||
for faster builds. cargo_home_isolated is default False and will use isolated | ||
Cargo home, which takes about 2 minutes to bootstrap. | ||
""" | ||
rules_rust_dependencies() | ||
|
||
rust_register_toolchains( | ||
edition = "2018", | ||
extra_target_triples = ["aarch64-unknown-linux-gnu"], | ||
) | ||
|
||
rust_repository_set( | ||
name = "rust_linux_arm64_linux_tuple", | ||
edition = "2018", | ||
exec_triple = "x86_64-unknown-linux-gnu", | ||
extra_target_triples = ["aarch64-unknown-linux-gnu"], | ||
versions = ["stable"], | ||
) | ||
|
||
crate_universe_dependencies() | ||
|
||
crates_repository( | ||
name = "ngx_rust_crate_index", | ||
cargo_lockfile = "@ngx_wasm_module//:lib/ngx-rust/Cargo.lock", | ||
manifests = [ | ||
"@ngx_wasm_module//:lib/ngx-rust/Cargo.toml", | ||
], | ||
isolated = cargo_home_isolated, | ||
) | ||
|
||
crates_repository( | ||
name = "ngx_wasm_rs_crate_index", | ||
cargo_lockfile = "@ngx_wasm_module//:lib/ngx-wasm-rs/Cargo.lock", | ||
manifests = [ | ||
"@ngx_wasm_module//:lib/ngx-wasm-rs/Cargo.toml", | ||
], | ||
isolated = cargo_home_isolated, | ||
) | ||
|
||
def normalize_name(n): | ||
return n.replace("-", "_") |
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,8 @@ | ||
platform( | ||
name = "linux-aarch64", | ||
constraint_values = [ | ||
"@platforms//os:linux", | ||
"@platforms//cpu:arm64", | ||
], | ||
visibility = ["//visibility:public"], | ||
) |
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,19 @@ | ||
"""Setup repostories.""" | ||
|
||
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") | ||
|
||
def ngx_wasm_module_repositories(): | ||
http_archive( | ||
name = "bazel_skylib", | ||
sha256 = "66ffd9315665bfaafc96b52278f57c7e2dd09f5ede279ea6d39b2be471e7e3aa", | ||
urls = [ | ||
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.4.2/bazel-skylib-1.4.2.tar.gz", | ||
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.4.2/bazel-skylib-1.4.2.tar.gz", | ||
], | ||
) | ||
|
||
http_archive( | ||
name = "rules_rust", | ||
sha256 = "9d04e658878d23f4b00163a72da3db03ddb451273eb347df7d7c50838d698f49", | ||
urls = ["https://github.com/bazelbuild/rules_rust/releases/download/0.26.0/rules_rust-v0.26.0.tar.gz"], | ||
) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On second thought, we will need this to be a reusable workflow that we can invoke from
ci.yml
andci-large.yml
. Please follow the example withjob-unit-tests.yml
(or any of thejob-*.yml
reusable workflows we already have).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will leave this as a TODO for this PR once I finish the bazel part of it.