From bdb1f05ffe0e31198ba31a9e13fb42bd4a855421 Mon Sep 17 00:00:00 2001 From: Wangchong Zhou Date: Tue, 1 Aug 2023 22:38:25 -0700 Subject: [PATCH] chore(build): improve wasmx build --- .github/matrix-full.yml | 1 + .requirements | 2 +- BUILD.bazel | 103 +------ build/BUILD.bazel | 78 ++---- build/kong_bindings.bzl | 20 +- build/openresty/BUILD.openresty.bazel | 209 ++------------ build/openresty/repositories.bzl | 2 +- build/openresty/wasmx/BUILD.bazel | 60 ++++ build/openresty/wasmx/rules.bzl | 80 ++++++ build/openresty/wasmx/wasmx_repositories.bzl | 279 +++++++++---------- scripts/explain_manifest/config.py | 17 +- 11 files changed, 345 insertions(+), 506 deletions(-) create mode 100644 build/openresty/wasmx/BUILD.bazel create mode 100644 build/openresty/wasmx/rules.bzl diff --git a/.github/matrix-full.yml b/.github/matrix-full.yml index c56eab07e2d5..b32ca5effd54 100644 --- a/.github/matrix-full.yml +++ b/.github/matrix-full.yml @@ -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 diff --git a/.requirements b/.requirements index 3d084a1c56d5..93e2084f4ede 100644 --- a/.requirements +++ b/.requirements @@ -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 diff --git a/BUILD.bazel b/BUILD.bazel index 9cd555609fca..2ca22d6d1d52 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -180,6 +180,7 @@ config_setting( bool_flag( name = "wasmx", build_setting_default = True, + visibility = ["//visibility:public"], ) # --//:wasmx_module_flag=dynamic @@ -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") diff --git a/build/BUILD.bazel b/build/BUILD.bazel index f5617cf4c0a8..f4fee1595097 100644 --- a/build/BUILD.bazel +++ b/build/BUILD.bazel @@ -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([ @@ -45,36 +46,7 @@ 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/} @@ -82,36 +54,28 @@ wasm_libs_install = select({ 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": "", @@ -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 { @@ -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 diff --git a/build/kong_bindings.bzl b/build/kong_bindings.bzl index 39691b7464db..982f221337e0 100644 --- a/build/kong_bindings.bzl +++ b/build/kong_bindings.bzl @@ -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}") @@ -108,5 +93,6 @@ load_bindings = repository_rule( "INSTALL_DESTDIR", "RPM_SIGNING_KEY_FILE", "NFPM_RPM_PASSPHRASE", + "NGX_WASM_MODULE_REMOTE", ], ) diff --git a/build/openresty/BUILD.openresty.bazel b/build/openresty/BUILD.openresty.bazel index 33103d99edba..564f3743c687 100644 --- a/build/openresty/BUILD.openresty.bazel +++ b/build/openresty/BUILD.openresty.bazel @@ -1,44 +1,8 @@ load("@rules_foreign_cc//foreign_cc:defs.bzl", "configure_make", "make") -load("@kong_bindings//:variables.bzl", "KONG_VAR") load("@bazel_skylib//lib:selects.bzl", "selects") load("@openresty_binding//:variables.bzl", "LUAJIT_VERSION") -load("@bazel_skylib//rules:write_file.bzl", "write_file") - -# 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 -# "cq" is a positional filename parameter-- casuing the install of the wabt-sys -# rust crate to fail when compiling wabt -# -# this works by impersonating ar, and only passing along 'rcsD' when it detects -# 'rcsd cq' as the first 2 positional parameters passed to "ar" -# -# this workaround is specifically only enabeld when targetting the v8 wasm -# runtime to minimize impact to the rest fo the build -# -# note that this dummy ar is technically in use for the entire openresty build, -# since we build wasm as part of that -write_file( - name = "wasmx_v8_ar", - out = "ar", - content = ["""#!/usr/bin/env bash - -if [[ "${1} ${2}" == 'rcsD cq' ]]; then - - touch /tmp/log - echo "before: $@" >> /tmp/log - - shift 2 - extra='rcsD' - - echo "after: $@" >> /tmp/log -fi - -/usr/bin/ar ${extra:-} $@ -"""], - is_executable = True, - visibility = ["//visibility:public"], -) +load("@kong_bindings//:variables.bzl", "KONG_VAR") +load("@kong//build/openresty/wasmx:rules.bzl", "wasm_runtime", "wasmx_configure_options", "wasmx_env") filegroup( name = "luajit_srcs", @@ -251,109 +215,13 @@ CONFIGURE_OPTIONS = [ "--with-ld-opt=\"-L$$EXT_BUILD_DEPS$$/lib -Wl,-Bsymbolic-functions -Wl,-z,relro\"", ], "//conditions:default": [], -}) + select({ - "@platforms//os:linux": [ - # neded for wasmx module - # although this is centos7 specific, the flag will work on any GNU linker - # we place it here to skip macos, which uses darwin ld - # https://github.com/Kong/ngx_wasm_module/commit/e70a19f53e1dda99d016c5cfa393652720959afd - "--with-ld-opt=\"-Wl,--allow-multiple-definition\"", - ], - "//conditions:default": [], }) + select({ # some distros name "nogroup" group name as "nobody" ":nogroup-name-as-nobody": [ "--group=nobody", ], "//conditions:default": [], -}) + select({ - "@kong//:wasmx_flag": [ - "--with-cc-opt=\"-DNGX_WASM_HOST_PROPERTY_NAMESPACE=kong\"", - ], - "//conditions:default": [], -}) + select({ - "@kong//:wasmx_static_mod": [ - "--add-module=$$EXT_BUILD_ROOT$$/external/ngx_wasm_module", - ], - "@kong//:wasmx_dynamic_mod": [ - "--with-compat", - "--add-dynamic-module=$$EXT_BUILD_ROOT$$/external/ngx_wasm_module", - ], - "//conditions:default": [], -}) + select({ - "@kong//:wasmx_v8_x86_64": [ - "--with-cc-opt=\"-I$$EXT_BUILD_ROOT$$/external/v8-x86_64/include\"", - "--with-ld-opt=\"-L$$EXT_BUILD_ROOT$$/external/v8-x86_64/lib\"", - ], - "//conditions:default": [], -}) + select({ - "@kong//:wasmx_v8_aarch64": [ - "--with-cc-opt=\"-I$$EXT_BUILD_ROOT$$/external/v8-aarch64/include\"", - "--with-ld-opt=\"-L$$EXT_BUILD_ROOT$$/external/v8-aarch64/lib\"", - ], - "//conditions:default": [], -}) + select({ - "@kong//:wasmx_wasmer_x86_64": [ - "--with-cc-opt=\"-I$$EXT_BUILD_ROOT$$/external/wasmer-x86_64/include\"", - "--with-ld-opt=\"-L$$EXT_BUILD_ROOT$$/external/wasmer-x86_64/lib\"", - ], - "//conditions:default": [], -}) + select({ - "@kong//:wasmx_wasmer_aarch64": [ - "--with-cc-opt=\"-I$$EXT_BUILD_ROOT$$/external/wasmer-aarch64/include\"", - "--with-ld-opt=\"-L$$EXT_BUILD_ROOT$$/external/wasmer-aarch64/lib\"", - ], - "//conditions:default": [], -}) + select({ - "@kong//:wasmx_wasmtime_x86_64": [ - "--with-cc-opt=\"-I$$EXT_BUILD_ROOT$$/external/wasmtime-x86_64/include\"", - "--with-ld-opt=\"-L$$EXT_BUILD_ROOT$$/external/wasmtime-x86_64/lib\"", - ], - "//conditions:default": [], -}) + select({ - "@kong//:wasmx_wasmtime_aarch64": [ - "--with-cc-opt=\"-I$$EXT_BUILD_ROOT$$/external/wasmtime-aarch64/include\"", - "--with-ld-opt=\"-L$$EXT_BUILD_ROOT$$/external/wasmtime-aarch64/lib\"", - ], - "//conditions:default": [], -}) - -wasmx_build_data = select({ - "@kong//:wasmx_flag": [ - "@ngx_wasm_module//:all_srcs", - ], - "//conditions:default": [], -}) + select({ - "@kong//:wasmx_v8_x86_64": [ - "@v8-x86_64//:all_srcs", - ], - "//conditions:default": [], -}) + select({ - "@kong//:wasmx_v8_aarch64": [ - "@v8-aarch64//:all_srcs", - ], - "//conditions:default": [], -}) + select({ - "@kong//:wasmx_wasmer_x86_64": [ - "@wasmer-x86_64//:all_srcs", - ], - "//conditions:default": [], -}) + select({ - "@kong//:wasmx_wasmer_aarch64": [ - "@wasmer-aarch64//:all_srcs", - ], - "//conditions:default": [], -}) + select({ - "@kong//:wasmx_wasmtime_x86_64": [ - "@wasmtime-x86_64//:all_srcs", - ], - "//conditions:default": [], -}) + select({ - "@kong//:wasmx_wasmtime_aarch64": [ - "@wasmtime-aarch64//:all_srcs", - ], - "//conditions:default": [], -}) +}) + wasmx_configure_options # TODO: set prefix to populate pid_path, conf_path, log_path etc @@ -370,66 +238,31 @@ filegroup( ), ) +wasm_runtime( + name = "wasm_runtime", + visibility = ["//visibility:public"], +) + configure_make( name = "openresty", - build_data = [ + configure_command = "configure", + configure_in_place = True, + configure_options = CONFIGURE_OPTIONS, + data = [ "@lua-kong-nginx-module//:all_srcs", "@lua-resty-lmdb//:all_srcs", "@lua-resty-events//:all_srcs", "@openresty_binding//:all_srcs", - ] + wasmx_build_data, - configure_command = "configure", - configure_in_place = True, - configure_options = CONFIGURE_OPTIONS, - env = select({ - "@kong//:wasmx_v8_x86_64": { - "NGX_WASM_RUNTIME": "v8", - "NGX_WASM_RUNTIME_LIB": "$$EXT_BUILD_ROOT$$/external/v8-x86_64/lib", - "NGX_WASM_RUNTIME_INC": "$$EXT_BUILD_ROOT$$/external/v8-x86_64/include", - # https://github.com/Kong/ngx_wasm_module/blob/0f07c712c48d410190ec5e0cc0b34fdfd190387d/t/10-build/003-dynamic_module.t#L43 - "NGX_WASM_RUNTIME_LD_OPT": "$$EXT_BUILD_ROOT$$/external/v8-x86_64/lib/libwee8.a -lv8bridge -lstdc++ -lm -ldl -lpthread", - # see the above comments and source for this dummy ar script - "AR": "$(execpath @openresty//:wasmx_v8_ar)", - }, - "@kong//:wasmx_v8_aarch64": { - "NGX_WASM_RUNTIME": "v8", - "NGX_WASM_RUNTIME_LIB": "$$EXT_BUILD_ROOT$$/external/v8-aarch64/lib", - "NGX_WASM_RUNTIME_INC": "$$EXT_BUILD_ROOT$$/external/v8-aarch64/include", - # https://github.com/Kong/ngx_wasm_module/blob/0f07c712c48d410190ec5e0cc0b34fdfd190387d/t/10-build/003-dynamic_module.t#L43 - "NGX_WASM_RUNTIME_LD_OPT": "$$EXT_BUILD_ROOT$$/external/v8-aarch64/lib/libwee8.a -lv8bridge -lstdc++ -lm -ldl -lpthread", - # see the above comments and source for this dummy ar script - "AR": "$(execpath @openresty//:wasmx_v8_ar)", - }, - "@kong//:wasmx_wasmer_x86_64": { - "NGX_WASM_RUNTIME": "wasmer", - "NGX_WASM_RUNTIME_LIB": "$$EXT_BUILD_ROOT$$/external/wasmer-x86_64/lib", - "NGX_WASM_RUNTIME_INC": "$$EXT_BUILD_ROOT$$/external/wasmer-x86_64/include", - # https://github.com/Kong/ngx_wasm_module/blob/0f07c712c48d410190ec5e0cc0b34fdfd190387d/t/10-build/003-dynamic_module.t#L30 - "NGX_WASM_RUNTIME_LD_OPT": "$$EXT_BUILD_ROOT$$/external/wasmer-x86_64/lib/libwasmer.a -lm -ldl -lpthread", - }, - "@kong//:wasmx_wasmer_aarch64": { - "NGX_WASM_RUNTIME": "wasmer", - "NGX_WASM_RUNTIME_LIB": "$$EXT_BUILD_ROOT$$/external/wasmer-aarch64/lib", - "NGX_WASM_RUNTIME_INC": "$$EXT_BUILD_ROOT$$/external/wasmer-aarch64/include", - # https://github.com/Kong/ngx_wasm_module/blob/0f07c712c48d410190ec5e0cc0b34fdfd190387d/t/10-build/003-dynamic_module.t#L30 - "NGX_WASM_RUNTIME_LD_OPT": "$$EXT_BUILD_ROOT$$/external/wasmer-aarch64/lib/libwasmer.a -lm -ldl -lpthread", - }, - "@kong//:wasmx_wasmtime_x86_64": { - "NGX_WASM_RUNTIME": "wasmtime", - "NGX_WASM_RUNTIME_LIB": "$$EXT_BUILD_ROOT$$/external/wasmtime-x86_64/lib", - "NGX_WASM_RUNTIME_INC": "$$EXT_BUILD_ROOT$$/external/wasmtime-x86_64/include", - # https://github.com/Kong/ngx_wasm_module/blob/0f07c712c48d410190ec5e0cc0b34fdfd190387d/t/10-build/003-dynamic_module.t#L30 - "NGX_WASM_RUNTIME_LD_OPT": "$$EXT_BUILD_ROOT$$/external/wasmtime-x86_64/lib/libwasmtime.a -lm -ldl -lpthread", - }, - "@kong//:wasmx_wasmtime_aarch64": { - "NGX_WASM_RUNTIME": "wasmtime", - "NGX_WASM_RUNTIME_LIB": "$$EXT_BUILD_ROOT$$/external/wasmtime-aarch64/lib", - "NGX_WASM_RUNTIME_INC": "$$EXT_BUILD_ROOT$$/external/wasmtime-aarch64/include", - # https://github.com/Kong/ngx_wasm_module/blob/0f07c712c48d410190ec5e0cc0b34fdfd190387d/t/10-build/003-dynamic_module.t#L30 - "NGX_WASM_RUNTIME_LD_OPT": "$$EXT_BUILD_ROOT$$/external/wasmtime-aarch64/lib/libwasmtime.a -lm -ldl -lpthread", - }, - "//conditions:default": {}, + ] + 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": [], }), + env = wasmx_env, lib_source = ":all_srcs", out_bin_dir = "", out_binaries = [ diff --git a/build/openresty/repositories.bzl b/build/openresty/repositories.bzl index b0b563828b78..d1bb6f53cacc 100644 --- a/build/openresty/repositories.bzl +++ b/build/openresty/repositories.bzl @@ -7,7 +7,7 @@ load("@kong_bindings//:variables.bzl", "KONG_VAR") load("//build/openresty/pcre:pcre_repositories.bzl", "pcre_repositories") load("//build/openresty/openssl:openssl_repositories.bzl", "openssl_repositories") load("//build/openresty/atc_router:atc_router_repositories.bzl", "atc_router_repositories") -load("//build/openresty:wasmx/wasmx_repositories.bzl", "wasmx_repositories") +load("//build/openresty/wasmx:wasmx_repositories.bzl", "wasmx_repositories") # This is a dummy file to export the module's repository. _NGINX_MODULE_DUMMY_FILE = """ diff --git a/build/openresty/wasmx/BUILD.bazel b/build/openresty/wasmx/BUILD.bazel new file mode 100644 index 000000000000..0299a7c8bfe7 --- /dev/null +++ b/build/openresty/wasmx/BUILD.bazel @@ -0,0 +1,60 @@ +load("@bazel_skylib//rules:write_file.bzl", "write_file") + +config_setting( + name = "use_v8", + flag_values = { + "@kong//:wasmx": "true", + "@kong//:wasm_runtime": "v8", + }, +) + +config_setting( + name = "use_wasmer", + flag_values = { + "@kong//:wasmx": "true", + "@kong//:wasm_runtime": "wasmer", + }, +) + +config_setting( + name = "use_wasmtime", + flag_values = { + "@kong//:wasmx": "true", + "@kong//:wasm_runtime": "wasmtime", + }, +) + +# 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 +# "cq" is a positional filename parameter-- casuing the install of the wabt-sys +# rust crate to fail when compiling wabt +# +# this works by impersonating ar, and only passing along 'rcsD' when it detects +# 'rcsd cq' as the first 2 positional parameters passed to "ar" +# +# this workaround is specifically only enabeld when targetting the v8 wasm +# runtime to minimize impact to the rest fo the build +# +# note that this dummy ar is technically in use for the entire openresty build, +# since we build wasm as part of that +write_file( + name = "wasmx_v8_ar", + out = "ar", + content = ["""#!/usr/bin/env bash + +if [[ "${1} ${2}" == 'rcsD cq' ]]; then + + touch /tmp/log + echo "before: $@" >> /tmp/log + + shift 2 + extra='rcsD' + + echo "after: $@" >> /tmp/log +fi + +/usr/bin/ar ${extra:-} $@ +"""], + is_executable = True, +) diff --git a/build/openresty/wasmx/rules.bzl b/build/openresty/wasmx/rules.bzl new file mode 100644 index 000000000000..4e2b2efc570f --- /dev/null +++ b/build/openresty/wasmx/rules.bzl @@ -0,0 +1,80 @@ +load("//build/openresty/wasmx:wasmx_repositories.bzl", "wasm_runtimes") + +wasmx_configure_options = select({ + "@kong//:wasmx_el7_workaround_flag": [ + # bypass "multiple definitions of 'assertions'" linker error from wasm.h: + # https://github.com/WebAssembly/wasm-c-api/blob/master/include/wasm.h#L29 + # and ensure a more recent libstdc++ is found + # https://github.com/Kong/ngx_wasm_module/blob/main/assets/release/Dockerfiles/Dockerfile.amd64.centos7#L28-L31 + "--with-ld-opt=\"-Wl,--allow-multiple-definition -L/opt/rh/devtoolset-8/root/usr/lib/gcc/x86_64-redhat-linux/8\"", + ], + "//conditions:default": [], +}) + select({ + "@kong//:wasmx_flag": [ + "--with-cc-opt=\"-DNGX_WASM_HOST_PROPERTY_NAMESPACE=kong\"", + ], + "//conditions:default": [], +}) + select({ + "@kong//:wasmx_static_mod": [ + "--add-module=$$EXT_BUILD_ROOT$$/external/ngx_wasm_module", + ], + "@kong//:wasmx_dynamic_mod": [ + "--with-compat", + "--add-dynamic-module=$$EXT_BUILD_ROOT$$/external/ngx_wasm_module", + ], + "//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)", + }, + "@kong//build/openresty/wasmx:use_wasmer": { + "NGX_WASM_RUNTIME": "wasmer", + }, + "@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", + }, + "//conditions:default": {}, +}) + +def _wasm_runtime_link_impl(ctx): + symlinks = [] + for file in ctx.files.runtime: + # strip ../REPO_NAME/ from the path + path = "/".join(file.short_path.split("/")[2:]) + symlink = ctx.actions.declare_file(ctx.attr.prefix + "/" + path) + symlinks.append(symlink) + ctx.actions.symlink(output = symlink, target_file = file) + + return [DefaultInfo(files = depset(symlinks))] + +_wasm_runtime_link = rule( + implementation = _wasm_runtime_link_impl, + attrs = { + "prefix": attr.string(), + "runtime": attr.label(), + }, +) + +def wasm_runtime(**kwargs): + select_conds = {} + for runtime in wasm_runtimes: + for os in wasm_runtimes[runtime]: + for arch in wasm_runtimes[runtime][os]: + select_conds["@wasmx_config_settings//:use_%s_%s_%s" % (runtime, os, arch)] = \ + "@%s-%s-%s//:all_srcs" % (runtime, os, arch) + + _wasm_runtime_link( + prefix = kwargs["name"], + runtime = select(select_conds), + **kwargs + ) diff --git a/build/openresty/wasmx/wasmx_repositories.bzl b/build/openresty/wasmx/wasmx_repositories.bzl index 9096b5d84d53..d2fb0098a886 100644 --- a/build/openresty/wasmx/wasmx_repositories.bzl +++ b/build/openresty/wasmx/wasmx_repositories.bzl @@ -2,181 +2,170 @@ load("@bazel_tools//tools/build_defs/repo:git.bzl", "new_git_repository") load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") -load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") load("@kong_bindings//:variables.bzl", "KONG_VAR") -def wasmx_repositories(): - wasmtime_version = KONG_VAR["WASMTIME"] - wasmer_version = KONG_VAR["WASMER"] - v8_version = KONG_VAR["V8"] - wasmtime_os = KONG_VAR["WASMTIME_OS"] - wasmer_os = KONG_VAR["WASMER_OS"] - v8_os = KONG_VAR["V8_OS"] - - ngx_wasm_module_tag = KONG_VAR["NGX_WASM_MODULE"] - ngx_wasm_module_branch = KONG_VAR.get("NGX_WASM_MODULE_BRANCH") - if ngx_wasm_module_branch: - ngx_wasm_module_tag = None - - maybe( - new_git_repository, - name = "ngx_wasm_module", - branch = ngx_wasm_module_branch, - tag = ngx_wasm_module_tag, - remote = KONG_VAR.get("NGX_WASM_MODULE_REMOTE", "https://github.com/Kong/ngx_wasm_module.git"), - build_file_content = """ +wasm_runtime_build_file = """ 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"] -) -""", - ) - - maybe( - http_archive, - name = "v8-x86_64", - urls = [ - "https://github.com/Kong/ngx_wasm_runtimes/releases/download/v8-" + - v8_version + "/ngx_wasm_runtime-v8-" + v8_version + "-" + v8_os + "-x86_64.tar.gz", - ], - strip_prefix = "v8-" + v8_version + "-" + v8_os + "-x86_64", - build_file_content = """ -filegroup( - name = "all_srcs", - srcs = glob(["include/**", "lib/**"]), - visibility = ["//visibility:public"] -) - -filegroup( - name = "lib", - srcs = glob(["**/*.a"]), - visibility = ["//visibility:public"] -) -""", - ) - - maybe( - http_archive, - name = "v8-aarch64", - urls = [ - "https://github.com/Kong/ngx_wasm_runtimes/releases/download/v8-" + - v8_version + "/ngx_wasm_runtime-v8-" + v8_version + "-" + v8_os + "-aarch64.tar.gz", - ], - strip_prefix = "v8-" + v8_version + "-" + v8_os + "-aarch64", - build_file_content = """ -filegroup( - name = "all_srcs", - srcs = glob(["include/**", "lib/**"]), + # note: we do static link only for runtimes + srcs = glob(["include/**", "lib/*.a"]), visibility = ["//visibility:public"] ) filegroup( name = "lib", - srcs = glob(["**/*.a"]), + srcs = glob(["**/*.so", "**/*.dylib"]), visibility = ["//visibility:public"] ) -""", - ) +""" + +wasm_runtimes = { + "wasmer": { + "linux": { + "x86_64": "3db46d2974b2c91aba2f0311dc26f59c1def473591768cddee8cdbf4783bf2c4", + "aarch64": "c212eebdf1cc6bf71e7b56d7421eb3494c3a6ab1faf50a55150b7522183d1d36", + }, + "macos": { + "x86_64": "008610ddefdd3e04af9733969da616f9a344017db451476a1ee1cf6702895f02", + "aarch64": "8534b278c1006ccc7f128bd1611636e12a33b9e625344331f9be3b56a5bb3286", + }, + }, + "v8": { + "linux": { + "x86_64": "41309c43d0f06334c8cf553b34973c42cfbdc3aadc1ca374723d4b6de48992d9", + "aarch64": "4e8b3881762b31078299ff1be2d48280c32ab4e1d48a0ce9ec267c4e302bb9ea", + }, + "macos": { + "x86_64": "862260d74f39a96aac556f821c28beb4def5ad5d1e5c70a0aa80d1af7d581f8b", + # "aarch64": None, no aarch64 v8 runtime release yet + }, + }, + "wasmtime": { + "linux": { + "x86_64": "1f40c3c91b8d82b7960921df94c43cf542fbb4e60522aaef02c150b421dfb34f", + "aarch64": "35585833923556d757c34f5e3293a8fb68ae9b327774d0eb160d424a02446e70", + }, + "macos": { + "x86_64": "1f90d4120b155ef351a11c49c62803d63aae99f25573ca8f5202dac5c3286eb0", + "aarch64": "193640f63ed7f58e13277030cc5f3e8be8cb6846d01f691cc1bfbbffca25bd5c", + }, + }, +} - maybe( - http_archive, - name = "wasmer-x86_64", - urls = [ - "https://github.com/wasmerio/wasmer/releases/download/v" + - wasmer_version + "/wasmer-" + wasmer_os + "-x86_64.tar.gz", - ], +def wasmx_repositories(): + new_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(["include/**", "lib/**"]), - visibility = ["//visibility:public"] -) - -filegroup( - name = "lib", - srcs = glob(["**/*.so", "**/*.dylib"]), + srcs = glob(["src/**"]), visibility = ["//visibility:public"] ) -""", - ) - maybe( - http_archive, - name = "wasmer-aarch64", - urls = [ - "https://github.com/wasmerio/wasmer/releases/download/v" + - wasmer_version + "/wasmer-" + wasmer_os + "-aarch64.tar.gz", - ], - build_file_content = """ filegroup( - name = "all_srcs", - srcs = glob(["include/**", "lib/**"]), + name = "lua_libs", + srcs = glob(["lib/resty/**"]), visibility = ["//visibility:public"] ) filegroup( - name = "lib", - srcs = glob(["**/*.so", "**/*.dylib"]), + name = "v8bridge_srcs", + srcs = glob(["lib/v8bridge/**"]), visibility = ["//visibility:public"] ) """, ) - maybe( - http_archive, - name = "wasmtime-x86_64", - urls = [ - "https://github.com/bytecodealliance/wasmtime/releases/download/v" + - wasmtime_version + "/wasmtime-v" + wasmtime_version + "-x86_64-" + wasmtime_os + "-c-api.tar.xz", - ], - strip_prefix = "wasmtime-v" + wasmtime_version + "-x86_64-" + wasmtime_os + "-c-api", - build_file_content = """ -filegroup( - name = "all_srcs", - srcs = glob(["include/**", "lib/**"]), - visibility = ["//visibility:public"] -) + wasmtime_version = KONG_VAR["WASMTIME"] + wasmer_version = KONG_VAR["WASMER"] + v8_version = KONG_VAR["V8"] -filegroup( - name = "lib", - srcs = glob(["**/*.so", "**/*.dylib"]), - visibility = ["//visibility:public"] + for os in wasm_runtimes["v8"]: + for arch in wasm_runtimes["v8"][os]: + # normalize macos to darwin used in url + url_os = os + if os == "macos": + url_os = "darwin" + url_arch = arch + if arch == "aarch64": + url_arch = "arm64" + + http_archive( + name = "v8-%s-%s" % (os, arch), + urls = [ + "https://github.com/Kong/ngx_wasm_runtimes/releases/download/v8-" + + v8_version + "/ngx_wasm_runtime-v8-%s-%s-%s.tar.gz" % (v8_version, url_os, url_arch), + ], + sha256 = wasm_runtimes["v8"][os][arch], + strip_prefix = "v8-%s-%s-%s" % (v8_version, url_os, url_arch), + build_file_content = wasm_runtime_build_file, + ) + + for os in wasm_runtimes["wasmer"]: + for arch in wasm_runtimes["wasmer"][os]: + # normalize macos to darwin used in url + url_os = os + if os == "macos": + url_os = "darwin" + url_arch = arch + if arch == "aarch64" and os == "macos": + url_arch = "arm64" + + http_archive( + name = "wasmer-%s-%s" % (os, arch), + urls = [ + "https://github.com/wasmerio/wasmer/releases/download/v" + + 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, + ) + + for os in wasm_runtimes["wasmtime"]: + for arch in wasm_runtimes["wasmtime"][os]: + http_archive( + name = "wasmtime-%s-%s" % (os, arch), + urls = [ + "https://github.com/bytecodealliance/wasmtime/releases/download/v" + + wasmtime_version + "/wasmtime-v%s-%s-%s-c-api.tar.xz" % (wasmtime_version, arch, os), + ], + strip_prefix = "wasmtime-v%s-%s-%s-c-api" % (wasmtime_version, arch, os), + sha256 = wasm_runtimes["wasmtime"][os][arch], + build_file_content = wasm_runtime_build_file, + ) + + wasmx_config_settings(name = "wasmx_config_settings") + +# generate boilerplate config_settings +def _wasmx_config_settings_impl(ctx): + content = "" + for runtime in wasm_runtimes: + for os in wasm_runtimes[runtime]: + for arch in wasm_runtimes[runtime][os]: + content += (""" +config_setting( + name = "use_{runtime}_{os}_{arch}", + constraint_values = [ + "@platforms//cpu:{arch}", + "@platforms//os:{os}", + ], + flag_values = {{ + "@kong//:wasmx": "true", + "@kong//:wasm_runtime": "{runtime}", + }}, + visibility = ["//visibility:public"], ) -""", - ) + """.format( + os = os, + arch = arch, + runtime = runtime, + )) - maybe( - http_archive, - name = "wasmtime-aarch64", - urls = [ - "https://github.com/bytecodealliance/wasmtime/releases/download/v" + - wasmtime_version + "/wasmtime-v" + wasmtime_version + "-aarch64-" + wasmtime_os + "-c-api.tar.xz", - ], - strip_prefix = "wasmtime-v" + wasmtime_version + "-aarch64-" + wasmtime_os + "-c-api", - build_file_content = """ -filegroup( - name = "all_srcs", - srcs = glob(["include/**", "lib/**"]), - visibility = ["//visibility:public"] -) + ctx.file("BUILD.bazel", content) -filegroup( - name = "lib", - srcs = glob(["**/*.so", "**/*.dylib"]), - visibility = ["//visibility:public"] +wasmx_config_settings = repository_rule( + implementation = _wasmx_config_settings_impl, ) -""", - ) diff --git a/scripts/explain_manifest/config.py b/scripts/explain_manifest/config.py index f0b7925d70e3..3128ab456e5c 100644 --- a/scripts/explain_manifest/config.py +++ b/scripts/explain_manifest/config.py @@ -15,10 +15,19 @@ def transform(f: FileInfo): # paths created by bazel. if glob_match(f.path, ["**/kong/lib/libxslt.so*", "**/kong/lib/libexslt.so*"]): - if f.rpath and "/usr/local/kong/lib" in f.rpath: - f.rpath = "/usr/local/kong/lib" - elif f.runpath and "/usr/local/kong/lib" in f.runpath: - f.runpath = "/usr/local/kong/lib" + expected_rpath = "/usr/local/kong/lib" + if f.rpath and expected_rpath in f.rpath: + f.rpath = expected_rpath + elif f.runpath and expected_rpath in f.runpath: + f.runpath = expected_rpath + # otherwise remain unmodified + + if f.path.endswith("/modules/ngx_wasm_module.so"): + expected_rpath = "/usr/local/openresty/luajit/lib:/usr/local/kong/lib" + if f.rpath and expected_rpath in f.rpath: + f.rpath = expected_rpath + elif f.runpath and expected_rpath in f.runpath: + f.runpath = expected_rpath # otherwise remain unmodified