Skip to content
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
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .bazelrc
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
1 change: 1 addition & 0 deletions .bazelversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6.1.0
35 changes: 35 additions & 0 deletions .github/workflows/bazel.yml
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/
Copy link
Member

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 and ci-large.yml. Please follow the example with job-unit-tests.yml (or any of the job-*.yml reusable workflows we already have).

Copy link
Author

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.

5 changes: 5 additions & 0 deletions .github/workflows/ci-large.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ defaults:
run:
shell: bash

# cancel previous runs if new commits are pushed to the PR, but run for each commit on master
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

jobs:
unit-large:
name: 'Unit'
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ defaults:
run:
shell: bash

# cancel previous runs if new commits are pushed to the PR, but run for each commit on master
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

jobs:
unit:
name: 'Unit'
Expand Down
145 changes: 145 additions & 0 deletions BUILD.bazel
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"],
)
6 changes: 0 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[workspace]
fffonion marked this conversation as resolved.
Show resolved Hide resolved

members = [
"lib/ngx-rust",
"t/lib/ngx-rust-tests",
Expand Down
13 changes: 13 additions & 0 deletions WORKSPACE
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()
1 change: 1 addition & 0 deletions build/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package(default_visibility = ["//visibility:public"])
55 changes: 55 additions & 0 deletions build/README.md
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
8 changes: 8 additions & 0 deletions build/crates.bzl
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()
52 changes: 52 additions & 0 deletions build/deps.bzl
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("-", "_")
8 changes: 8 additions & 0 deletions build/platform/BUILD.bazel
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"],
)
19 changes: 19 additions & 0 deletions build/repos.bzl
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"],
)
Loading
Loading