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): support build wasmx module with wasmx and v8 runtime #11446

Closed
wants to merge 2 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
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ on:

env:
BUILD_ROOT: ${{ github.workspace }}/${{ inputs.relative-build-root }}
NGX_WASM_MODULE_REMOTE: https://github.com/fffonion/ngx_wasm_module

jobs:
build:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ env:
GHA_CACHE: ${{ github.event_name == 'pull_request' }}

HAS_ACCESS_TO_GITHUB_TOKEN: ${{ github.event_name != 'pull_request' || (github.event.pull_request.head.repo.full_name == github.repository && github.actor != 'dependabot[bot]') }}
NGX_WASM_MODULE_REMOTE: https://github.com/fffonion/ngx_wasm_module

jobs:
metadata:
Expand Down
3 changes: 2 additions & 1 deletion .requirements
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ LUA_RESTY_WEBSOCKET=60eafc3d7153bceb16e6327074e0afc3d94b1316 # 0.4.0
ATC_ROUTER=7a2ad42d4246598ba1f753b6ae79cb1456040afa # 1.3.1

KONG_MANAGER=nightly
NGX_WASM_MODULE=ddb3fa8f7cacc81557144cf22706484eabd79a84
# TOOD: test
NGX_WASM_MODULE=bazel-cargo # prerelease-0.1.0
WASMER=3.1.1
WASMTIME=14.0.3
V8=10.5.18
12 changes: 12 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ load("@atc_router//build:crates.bzl", "atc_router_crates")

atc_router_crates()

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) # TODO: set cargo_home_isolated=True for release

load("@ngx_wasm_module//build:crates.bzl", "ngx_wasm_module_crates")

ngx_wasm_module_crates()

load("//build:repositories.bzl", "build_repositories")

build_repositories()
Expand Down
25 changes: 14 additions & 11 deletions build/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,14 @@ install_wasm_deps_cmd = select({
mkdir -p "$(dirname "$dest")"
cp -v "$fname" "$dest"
done

if [[ -d ${BUILD_DESTDIR}/openresty/nginx/lib ]]; then
# both v8 and wasmer currently depend on openresty/nginx/lib/libngx_wasm_rs.so,
# but in the case of wasmtime it is statically linked and thus not needed in
# the final package
if [[ -e ${BUILD_DESTDIR}/openresty/nginx/lib/libngx_wasm_rs.so ]]; then
copy_with_filter ${BUILD_DESTDIR}/openresty/nginx/lib ${BUILD_DESTDIR}/kong/lib
fi
rm -rf ${BUILD_DESTDIR}/openresty/nginx/lib
fi
""",
"//conditions:default": "\n",
}) + select({
"@kong//build/openresty/wasmx:needs_ngx_wasm_rs": """
# both v8 and wasmer currently depend on openresty/nginx/lib/libngx_wasm_rs.so,
# but in the case of wasmtime it is statically linked and thus not needed in
# the final package
cp $(location @ngx_wasm_module//:ngx_wasm_rs) ${BUILD_DESTDIR}/kong/lib
""",
"//conditions:default": "\n",
})
Expand Down Expand Up @@ -98,7 +96,12 @@ kong_directory_genrule(
}) + select({
"@kong//:wasmx_flag": [
"@ngx_wasm_module//:lua_libs",
"@openresty//:wasm_runtime",
"@kong//build/openresty/wasmx:wasm_runtime",
],
"//conditions:default": [],
}) + select({
"@kong//build/openresty/wasmx:needs_ngx_wasm_rs": [
"@ngx_wasm_module//:ngx_wasm_rs",
],
"//conditions:default": [],
}) + lib_deps + lualib_deps,
Expand Down
19 changes: 3 additions & 16 deletions build/openresty/BUILD.openresty.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
load("@rules_foreign_cc//foreign_cc:defs.bzl", "configure_make", "make")
load("@bazel_skylib//lib:selects.bzl", "selects")
load("@kong//build/openresty/wasmx:rules.bzl", "wasm_runtime", "wasmx_configure_options", "wasmx_env")
load("@kong//build/openresty/wasmx:rules.bzl", "wasm_runtime", "wasmx_configure_options", "wasmx_data", "wasmx_deps", "wasmx_env")
load("@openresty_binding//:variables.bzl", "LUAJIT_VERSION")
load("@kong_bindings//:variables.bzl", "KONG_VAR")

Expand Down Expand Up @@ -246,11 +246,6 @@ filegroup(
),
)

wasm_runtime(
name = "wasm_runtime",
visibility = ["//visibility:public"],
)

configure_make(
name = "openresty",
configure_command = "configure",
Expand All @@ -261,15 +256,7 @@ configure_make(
"@lua-resty-lmdb//:all_srcs",
"@lua-resty-events//:all_srcs",
"@openresty_binding//:all_srcs",
] + select({
"@kong//:wasmx_flag": [
"@ngx_wasm_module//:all_srcs",
# wasm_runtime has to be a "data" (target) instead of "build_data" (exec)
# to be able to lookup by its path (relative to INSTALLDIR)
":wasm_runtime",
],
"//conditions:default": [],
}),
] + wasmx_data,
env = wasmx_env,
lib_source = ":all_srcs",
out_bin_dir = "",
Expand Down Expand Up @@ -297,5 +284,5 @@ configure_make(
"@cross_deps_libxcrypt//:libxcrypt",
],
"//conditions:default": [],
}),
}) + wasmx_deps,
)
27 changes: 27 additions & 0 deletions build/openresty/wasmx/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("@bazel_skylib//lib:selects.bzl", "selects")
load("@kong//build/openresty/wasmx:rules.bzl", "wasm_runtime")

config_setting(
name = "use_v8",
Expand All @@ -24,6 +26,30 @@ config_setting(
},
)

config_setting(
name = "wasmx_on_linux",
constraint_values = [
"@platforms//os:linux",
],
flag_values = {
"@kong//:wasmx": "true",
},
)

selects.config_setting_group(
name = "needs_ngx_wasm_rs",
match_any = [
":use_v8",
":use_wasmer",
],
visibility = ["//visibility:public"],
)

wasm_runtime(
name = "wasm_runtime",
visibility = ["//visibility:public"],
)

# this works around an issue that occurs when installing/compiling the v8 wasm
# runtime engine, specifically: cargo/bazel/rules_foreign_cc decide ARFLAGS
# should be "rcsD cq ...", which is incorrect and results in ar thinking
Expand Down Expand Up @@ -57,4 +83,5 @@ fi
/usr/bin/ar ${extra:-} $@
"""],
is_executable = True,
visibility = ["@openresty//:__pkg__"],
)
43 changes: 39 additions & 4 deletions build/openresty/wasmx/rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,64 @@ wasmx_configure_options = select({
"--add-dynamic-module=$$EXT_BUILD_ROOT$$/external/ngx_wasm_module",
],
"//conditions:default": [],
}) + select({
"@kong//build/openresty/wasmx:needs_ngx_wasm_rs": [
"--with-cc-opt=\"-I$$EXT_BUILD_ROOT$$/external/ngx_wasm_module/lib/ngx-wasm-rs/include\"",
"--with-ld-opt=\"-L$$EXT_BUILD_DEPS$$/lib -lngx_wasm_rs\"",
],
"//conditions:default": {},
})

wasmx_data = select({
"@kong//:wasmx_flag": [
"@ngx_wasm_module//:nginx_module_srcs",
# wasm_runtime has to be a "data" (target) instead of "build_data" (exec)
# to be able to lookup by its path (relative to INSTALLDIR)
"@kong//build/openresty/wasmx:wasm_runtime",
],
"//conditions:default": [],
}) + select({
"@kong//build/openresty/wasmx:use_v8": [
"@kong//build/openresty/wasmx:wasmx_v8_ar",
],
"//conditions:default": [],
}) + select({
"@kong//build/openresty/wasmx:needs_ngx_wasm_rs": [
"@ngx_wasm_module//:ngx_wasm_rs_headers",
],
"//conditions:default": {},
})

wasmx_env = select({
"@kong//build/openresty/wasmx:use_v8": {
"NGX_WASM_RUNTIME": "v8",
# see the above comments and source for this dummy ar script
"AR": "$(execpath @kong//build/openresty:wasmx/wasmx_v8_ar)",
"NGX_WASM_CARGO": "0",
# see the comments and source in BUILD.bazel for this dummy ar script
"AR": "$(execpath @kong//build/openresty/wasmx:wasmx_v8_ar)",
},
"@kong//build/openresty/wasmx:use_wasmer": {
"NGX_WASM_RUNTIME": "wasmer",
"NGX_WASM_CARGO": "0",
},
"@kong//build/openresty/wasmx:use_wasmtime": {
"NGX_WASM_RUNTIME": "wasmtime",
},
"//conditions:default": {},
}) | select({
"@kong//:wasmx_flag": {
"NGX_WASM_RUNTIME_LIB": "$$INSTALLDIR/../wasm_runtime/lib",
"NGX_WASM_RUNTIME_INC": "$$INSTALLDIR/../wasm_runtime/include",
"NGX_WASM_RUNTIME_LIB": "$$EXT_BUILD_ROOT$$/$(BINDIR)/build/openresty/wasmx/wasm_runtime/lib",
"NGX_WASM_RUNTIME_INC": "$$EXT_BUILD_ROOT$$/$(BINDIR)/build/openresty/wasmx/wasm_runtime/include",
},
"//conditions:default": {},
})

wasmx_deps = select({
"@kong//build/openresty/wasmx:needs_ngx_wasm_rs": [
"@ngx_wasm_module//:ngx_wasm_rs_shared",
],
"//conditions:default": [],
})

def _wasm_runtime_link_impl(ctx):
symlinks = []
for file in ctx.files.runtime:
Expand Down
28 changes: 5 additions & 23 deletions build/openresty/wasmx/wasmx_repositories.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""A module defining the third party dependency WasmX"""

load("@bazel_tools//tools/build_defs/repo:git.bzl", "new_git_repository")
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@kong_bindings//:variables.bzl", "KONG_VAR")

Expand Down Expand Up @@ -53,29 +53,10 @@ wasm_runtimes = {
}

def wasmx_repositories():
new_git_repository(
git_repository(
name = "ngx_wasm_module",
branch = KONG_VAR["NGX_WASM_MODULE"],
remote = KONG_VAR["NGX_WASM_MODULE_REMOTE"],
build_file_content = """
filegroup(
name = "all_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"]
)
""",
)

wasmtime_version = KONG_VAR["WASMTIME"]
Expand Down Expand Up @@ -110,8 +91,10 @@ filegroup(
if os == "macos":
url_os = "darwin"
url_arch = arch
if arch == "aarch64" and os == "macos":
if arch == "aarch64":
url_arch = "arm64"
elif arch == "x86_64":
url_arch = "amd64"

http_archive(
name = "wasmer-%s-%s" % (os, arch),
Expand All @@ -120,7 +103,6 @@ filegroup(
wasmer_version + "/wasmer-%s-%s.tar.gz" % (url_os, url_arch),
],
sha256 = wasm_runtimes["wasmer"][os][arch],
strip_prefix = "wasmer-%s-%s" % (url_os, url_arch),
build_file_content = wasm_runtime_build_file,
)

Expand Down
Loading