Skip to content

Commit

Permalink
chore(build): improve wasmx build (#11367)
Browse files Browse the repository at this point in the history
  • Loading branch information
fffonion committed Aug 18, 2023
1 parent e2b301e commit 1cf0551
Show file tree
Hide file tree
Showing 11 changed files with 345 additions and 506 deletions.
1 change: 1 addition & 0 deletions .github/matrix-full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ build-packages:
image: centos:7
package: rpm
package-type: el7
bazel-args: --//:wasmx_el7_workaround=true
check-manifest-suite: el7-amd64
- label: rhel-8
image: rockylinux:8
Expand Down
2 changes: 1 addition & 1 deletion .requirements
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ LUA_RESTY_WEBSOCKET=60eafc3d7153bceb16e6327074e0afc3d94b1316 # 0.4.0
ATC_ROUTER=b0d5e7e2a2ca59bb051959385d3e42d96c93bb98 # 1.2.0

KONG_MANAGER=v3.4.0.0
NGX_WASM_MODULE=prerelease-0.1.0
NGX_WASM_MODULE=abd6a40790e019495de0f1532a8f2312bbdbc820 # prerelease-0.1.0
WASMER=3.1.1
WASMTIME=8.0.1
V8=10.5.18
103 changes: 7 additions & 96 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ config_setting(
bool_flag(
name = "wasmx",
build_setting_default = True,
visibility = ["//visibility:public"],
)

# --//:wasmx_module_flag=dynamic
Expand Down Expand Up @@ -227,113 +228,23 @@ string_flag(
"wasmer",
"wasmtime",
],
)

config_setting(
name = "wasmx_v8",
flag_values = {
":wasmx": "true",
":wasm_runtime": "v8",
},
visibility = ["//visibility:public"],
)

config_setting(
name = "wasmx_v8_x86_64",
constraint_values = [
"@platforms//cpu:x86_64",
],
flag_values = {
":wasmx": "true",
":wasm_runtime": "v8",
},
visibility = ["//visibility:public"],
)

config_setting(
name = "wasmx_v8_aarch64",
constraint_values = [
"@platforms//cpu:aarch64",
],
flag_values = {
":wasmx": "true",
":wasm_runtime": "v8",
},
visibility = ["//visibility:public"],
)

config_setting(
name = "wasmx_wasmer",
flag_values = {
":wasmx": "true",
":wasm_runtime": "wasmer",
},
visibility = ["//visibility:public"],
)

config_setting(
name = "wasmx_wasmer_x86_64",
constraint_values = [
"@platforms//cpu:x86_64",
],
flag_values = {
":wasmx": "true",
":wasm_runtime": "wasmer",
},
visibility = ["//visibility:public"],
)

config_setting(
name = "wasmx_wasmer_aarch64",
constraint_values = [
"@platforms//cpu:aarch64",
],
flag_values = {
":wasmx": "true",
":wasm_runtime": "wasmer",
},
visibility = ["//visibility:public"],
)

config_setting(
name = "wasmx_wasmtime",
flag_values = {
":wasmx": "true",
":wasm_runtime": "wasmtime",
},
visibility = ["//visibility:public"],
)

config_setting(
name = "wasmx_wasmtime_x86_64",
constraint_values = [
"@platforms//cpu:x86_64",
],
flag_values = {
":wasmx": "true",
":wasm_runtime": "wasmtime",
},
visibility = ["//visibility:public"],
# --//:wasmx_el7_workaround=false
bool_flag(
name = "wasmx_el7_workaround",
build_setting_default = False,
)

config_setting(
name = "wasmx_wasmtime_aarch64",
constraint_values = [
"@platforms//cpu:aarch64",
],
name = "wasmx_el7_workaround_flag",
flag_values = {
":wasmx": "true",
":wasm_runtime": "wasmtime",
":wasmx_el7_workaround": "true",
},
visibility = ["//visibility:public"],
)

##### dynamic modules
selects.config_setting_group(
name = "nginx_dynamic_module_support",
match_any = [":wasmx_dynamic_mod"],
)

##### constraints, platforms and config_settings for cross-compile

constraint_setting(name = "cross_build_setting")
Expand Down
78 changes: 24 additions & 54 deletions build/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@kong_bindings//:variables.bzl", "KONG_VAR")
load("@bazel_skylib//lib:selects.bzl", "selects")
load("//build:build_system.bzl", "kong_directory_genrule", "kong_rules_group", "kong_template_file")

exports_files([
Expand Down Expand Up @@ -45,73 +46,36 @@ install_webui_cmd = select({
"@kong//:skip_webui_flags": "\n",
})

wasmx_vm_deps = select({
"@kong//:wasmx_v8_x86_64": [
"@v8-x86_64//:lib",
],
"@kong//:wasmx_v8_aarch64": [
"@v8-aarch64//:lib",
],
"@kong//:wasmx_wasmer_x86_64": [
"@wasmer-x86_64//:lib",
],
"@kong//:wasmx_wasmer_aarch64": [
"@wasmer-aarch64//:lib",
],
"@kong//:wasmx_wasmtime_x86_64": [
"@wasmtime-x86_64//:lib",
],
"@kong//:wasmx_wasmtime_aarch64": [
"@wasmtime-aarch64//:lib",
],
"//conditions:default": [],
})

wasmx_deps = select({
"@kong//:wasmx_flag": [
"@ngx_wasm_module//:lua_libs",
],
"//conditions:default": [],
}) + wasmx_vm_deps

wasm_libs_install = select({
install_wasm_deps_cmd = select({
"@kong//:wasmx_flag": """
for fname in $(locations @ngx_wasm_module//:lua_libs); do
base=${fname##*/ngx_wasm_module/lib/}
dest="${BUILD_DESTDIR}/openresty/lualib/$base"
mkdir -p "$(dirname "$dest")"
cp -v "$fname" "$dest"
done
""",
"//conditions:default": "\n",
})
wasmx_vm_cmd = select({
"@kong//:wasmx_v8": """
if [[ -d ${BUILD_DESTDIR}/openresty/nginx/lib ]]; then
copy_with_filter ${BUILD_DESTDIR}/openresty/nginx/lib ${BUILD_DESTDIR}/kong/lib
rm -rf ${BUILD_DESTDIR}/openresty/nginx/lib
fi
""",
"@kong//:wasmx_wasmer": """
if [[ -d ${BUILD_DESTDIR}/openresty/nginx/lib ]]; then
copy_with_filter ${BUILD_DESTDIR}/openresty/nginx/lib ${BUILD_DESTDIR}/kong/lib
rm -rf ${BUILD_DESTDIR}/openresty/nginx/lib
fi
""",
# 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
"@kong//:wasmx_wasmtime": """
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": "",
"//conditions:default": "\n",
})

##### dynamic modules
selects.config_setting_group(
name = "nginx_dynamic_module_support",
match_any = ["@kong//:wasmx_dynamic_mod"],
)

link_modules_dir = select({
"@kong//:nginx_dynamic_module_support": """
":nginx_dynamic_module_support": """
LN ${BUILD_DESTDIR}/openresty/nginx/modules ${BUILD_DESTDIR}/kong/modules
""",
"//conditions:default": "",
Expand All @@ -130,7 +94,13 @@ kong_directory_genrule(
"//conditions:default": [
"@kong_admin_gui//:dist_files",
],
}) + lib_deps + lualib_deps + wasmx_deps,
}) + select({
"@kong//:wasmx_flag": [
"@ngx_wasm_module//:lua_libs",
"@openresty//:wasm_runtime",
],
"//conditions:default": [],
}) + lib_deps + lualib_deps,
cmd =
""" set -e
function copy_with_filter {
Expand Down Expand Up @@ -177,7 +147,7 @@ kong_directory_genrule(
cp -r $(locations @protoc//:all_srcs) ${BUILD_DESTDIR}/kong/.
""" + install_lib_deps_cmd + install_lualib_deps_cmd + install_webui_cmd + link_modules_dir + wasm_libs_install + wasmx_vm_cmd + """
""" + install_lib_deps_cmd + install_lualib_deps_cmd + install_webui_cmd + link_modules_dir + install_wasm_deps_cmd + """
mkdir -p ${BUILD_DESTDIR}/etc/kong
cp kong.conf.default ${BUILD_DESTDIR}/etc/kong/kong.conf.default
Expand Down
20 changes: 3 additions & 17 deletions build/kong_bindings.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,8 @@ def _load_vars(ctx):

content += '"OPENRESTY_PATCHES": [%s],' % (", ".join(patches))

ngx_wasm_module_remote = ctx.os.environ.get("NGX_WASM_MODULE_REMOTE")
if ngx_wasm_module_remote:
content += '"NGX_WASM_MODULE_REMOTE": "%s",' % ngx_wasm_module_remote

ngx_wasm_module_branch = ctx.os.environ.get("NGX_WASM_MODULE_BRANCH")
if ngx_wasm_module_branch:
content += '"NGX_WASM_MODULE_BRANCH": "%s",' % ngx_wasm_module_branch

# wasm runtime options
if ctx.os.name == "mac os x":
content += '"V8_OS": "darwin",'
content += '"WASMER_OS": "darwin",'
content += '"WASMTIME_OS": "macos",'
elif ctx.os.name == "linux":
content += '"V8_OS": "linux",'
content += '"WASMER_OS": "linux",'
content += '"WASMTIME_OS": "linux",'
ngx_wasm_module_remote = ctx.os.environ.get("NGX_WASM_MODULE_REMOTE", "https://github.com/Kong/ngx_wasm_module.git")
content += '"NGX_WASM_MODULE_REMOTE": "%s",' % ngx_wasm_module_remote

ctx.file("BUILD.bazel", "")
ctx.file("variables.bzl", "KONG_VAR = {\n" + content + "\n}")
Expand Down Expand Up @@ -108,5 +93,6 @@ load_bindings = repository_rule(
"INSTALL_DESTDIR",
"RPM_SIGNING_KEY_FILE",
"NFPM_RPM_PASSPHRASE",
"NGX_WASM_MODULE_REMOTE",
],
)
Loading

0 comments on commit 1cf0551

Please sign in to comment.