From a0120cd6c4c584a807c8bedc8c534a0068007d7c Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Sun, 18 Jan 2026 17:37:10 +0100 Subject: [PATCH] chore: update WasmEdge from proxy-wasm/0.13.1 to 0.16.1 Signed-off-by: Matthieu MOREL --- bazel/external/fmt.BUILD | 39 ++++++++++++++ bazel/external/spdlog.BUILD | 32 +++++++++++ bazel/external/wasmedge.BUILD | 8 ++- bazel/external/wasmedge.patch | 99 +++++++++++++++++++++++++++++++++++ bazel/repositories.bzl | 32 +++++++++-- src/wasmedge/wasmedge.cc | 88 +++++++++++++++++-------------- 6 files changed, 254 insertions(+), 44 deletions(-) create mode 100644 bazel/external/fmt.BUILD create mode 100644 bazel/external/spdlog.BUILD create mode 100644 bazel/external/wasmedge.patch diff --git a/bazel/external/fmt.BUILD b/bazel/external/fmt.BUILD new file mode 100644 index 000000000..22314d478 --- /dev/null +++ b/bazel/external/fmt.BUILD @@ -0,0 +1,39 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Based on Bazel Central Registry fmt module BUILD file +# https://github.com/bazelbuild/bazel-central-registry/tree/main/modules/fmt/11.0.2 + +load("@rules_cc//cc:defs.bzl", "cc_library") + +package(default_visibility = ["//visibility:public"]) + +licenses(["notice"]) # MIT + +cc_library( + name = "fmt", + srcs = [ + "src/format.cc", + "src/os.cc", + ], + hdrs = glob([ + "include/fmt/*.h", + ]), + copts = select({ + "@platforms//os:windows": ["-utf-8"], + "//conditions:default": [], + }), + includes = ["include"], + strip_include_prefix = "include", +) diff --git a/bazel/external/spdlog.BUILD b/bazel/external/spdlog.BUILD new file mode 100644 index 000000000..7c16ec7a5 --- /dev/null +++ b/bazel/external/spdlog.BUILD @@ -0,0 +1,32 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Based on Bazel Central Registry spdlog module BUILD file +# https://github.com/bazelbuild/bazel-central-registry/tree/main/modules/spdlog/1.13.0 + +load("@rules_cc//cc:defs.bzl", "cc_library") + +package(default_visibility = ["//visibility:public"]) + +licenses(["notice"]) # MIT + +cc_library( + name = "spdlog", + hdrs = glob([ + "include/**/*.h", + ]), + defines = ["SPDLOG_FMT_EXTERNAL"], + includes = ["include"], + deps = ["@com_github_fmtlib_fmt//:fmt"], +) diff --git a/bazel/external/wasmedge.BUILD b/bazel/external/wasmedge.BUILD index e8fba783e..3c7abc0b7 100644 --- a/bazel/external/wasmedge.BUILD +++ b/bazel/external/wasmedge.BUILD @@ -26,11 +26,13 @@ filegroup( cmake( name = "wasmedge_lib", cache_entries = { - "WASMEDGE_BUILD_AOT_RUNTIME": "Off", + "WASMEDGE_USE_LLVM": "Off", "WASMEDGE_BUILD_SHARED_LIB": "Off", "WASMEDGE_BUILD_STATIC_LIB": "On", "WASMEDGE_BUILD_TOOLS": "Off", "WASMEDGE_FORCE_DISABLE_LTO": "On", + # Provide spdlog and fmt as external dependencies via Bazel (not CMake FetchContent) + "CMAKE_PREFIX_PATH": "$$EXT_BUILD_DEPS$$/spdlog;$$EXT_BUILD_DEPS$$/fmt", }, env = { "CXXFLAGS": "-Wno-error=dangling-reference -Wno-error=maybe-uninitialized -Wno-error=array-bounds= -Wno-error=deprecated-declarations -std=c++20", @@ -38,4 +40,8 @@ cmake( generate_args = ["-GNinja"], lib_source = ":srcs", out_static_libs = ["libwasmedge.a"], + deps = [ + "@com_github_fmtlib_fmt//:fmt", + "@com_github_gabime_spdlog//:spdlog", + ], ) diff --git a/bazel/external/wasmedge.patch b/bazel/external/wasmedge.patch new file mode 100644 index 000000000..d25f30b80 --- /dev/null +++ b/bazel/external/wasmedge.patch @@ -0,0 +1,99 @@ +diff --git a/CMakeLists.txt b/CMakeLists.txt +index 1111111..2222222 100644 +--- a/CMakeLists.txt ++++ b/CMakeLists.txt +@@ -186,6 +186,59 @@ endif() + include(Helper) + include(GNUInstallDirs) + ++# When building under Bazel with rules_foreign_cc, dependencies are provided via EXT_BUILD_DEPS ++# EXT_BUILD_DEPS environment variable contains the path to the merged dependency directory ++# created by rules_foreign_cc, with headers in include/ and libraries in lib/ ++# All dependencies listed in the cmake() rule's deps attribute are merged into a single directory structure ++# Create IMPORTED targets for fmt and spdlog to prevent CMake FetchContent from trying to download them ++if(DEFINED ENV{EXT_BUILD_DEPS}) ++ set(BAZEL_EXT_BUILD_DEPS "$ENV{EXT_BUILD_DEPS}") ++ message(STATUS "Bazel build detected, using dependencies from: ${BAZEL_EXT_BUILD_DEPS}") ++ ++ # Create fmt::fmt IMPORTED target ++ # fmt is a library with compiled sources (not header-only) ++ if(NOT TARGET fmt::fmt AND EXISTS "${BAZEL_EXT_BUILD_DEPS}/include/fmt") ++ # Look for the fmt library file ++ find_library(FMT_LIBRARY ++ NAMES fmt libfmt ++ PATHS "${BAZEL_EXT_BUILD_DEPS}/lib" ++ NO_DEFAULT_PATH ++ ) ++ ++ if(FMT_LIBRARY) ++ # Create STATIC IMPORTED library ++ add_library(fmt::fmt STATIC IMPORTED) ++ set_target_properties(fmt::fmt PROPERTIES ++ IMPORTED_LOCATION "${FMT_LIBRARY}" ++ INTERFACE_INCLUDE_DIRECTORIES "${BAZEL_EXT_BUILD_DEPS}/include" ++ ) ++ message(STATUS "Created fmt::fmt IMPORTED target from Bazel deps: ${FMT_LIBRARY}") ++ else() ++ message(WARNING "fmt headers found at ${BAZEL_EXT_BUILD_DEPS}/include/fmt but compiled library not found in ${BAZEL_EXT_BUILD_DEPS}/lib. Check that fmt dependency is properly configured in Bazel BUILD file.") ++ endif() ++ ++ # Create non-namespaced alias for backward compatibility with code that references fmt without the namespace ++ if(TARGET fmt::fmt) ++ add_library(fmt ALIAS fmt::fmt) ++ endif() ++ endif() ++ ++ # Create spdlog::spdlog IMPORTED target ++ # spdlog is header-only when using external fmt ++ if(NOT TARGET spdlog::spdlog AND EXISTS "${BAZEL_EXT_BUILD_DEPS}/include/spdlog") ++ add_library(spdlog::spdlog INTERFACE IMPORTED) ++ set_target_properties(spdlog::spdlog PROPERTIES ++ INTERFACE_INCLUDE_DIRECTORIES "${BAZEL_EXT_BUILD_DEPS}/include" ++ INTERFACE_COMPILE_DEFINITIONS "SPDLOG_FMT_EXTERNAL" ++ ) ++ # spdlog depends on fmt ++ if(TARGET fmt::fmt) ++ set_property(TARGET spdlog::spdlog APPEND PROPERTY INTERFACE_LINK_LIBRARIES fmt::fmt) ++ endif() ++ message(STATUS "Created spdlog::spdlog IMPORTED target from Bazel deps") ++ endif() ++endif() ++ + set(CPACK_PACKAGE_VENDOR Second State LLC) + set(CPACK_PACKAGE_FILE_NAME "${CMAKE_PROJECT_NAME}-${CPACK_PACKAGE_VERSION}-${CMAKE_SYSTEM_NAME}") + set(CPACK_STRIP_FILES ON) +diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt +index 1111111..2222222 100644 +--- a/include/CMakeLists.txt ++++ b/include/CMakeLists.txt +@@ -39,6 +39,8 @@ configure_file(common/enum_configure.h api/wasmedge/enum_configure.h COPYONLY) + configure_file(common/enum_errcode.h api/wasmedge/enum_errcode.h COPYONLY) + configure_file(common/enum_types.h api/wasmedge/enum_types.h COPYONLY) + configure_file(common/version.h.in common/version.h) ++install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/api/wasmedge ++ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) + unset(WASMEDGE_VERSION_STRING) + unset(WASMEDGE_VERSION_LIST) + unset(WASMEDGE_VERSION_MAJOR) +diff --git a/lib/api/CMakeLists.txt b/lib/api/CMakeLists.txt +index 1111111..2222222 100644 +--- a/lib/api/CMakeLists.txt ++++ b/lib/api/CMakeLists.txt +@@ -151,8 +151,15 @@ if(WASMEDGE_BUILD_SHARED_LIB) + endif() + + if(WASMEDGE_BUILD_STATIC_LIB) +- wasmedge_add_static_lib_component_command(fmt::fmt) +- wasmedge_add_static_lib_component_command(spdlog::spdlog) ++ # When using Bazel, skip packaging fmt/spdlog into libwasmedge.a ++ # Bazel will handle linking these dependencies directly from the deps attribute ++ # This allows fmt to be either static or shared, and avoids the need to extract ++ # object files from libraries (which fails for shared libraries) ++ if(NOT DEFINED ENV{EXT_BUILD_DEPS}) ++ # In non-Bazel builds, package fmt and spdlog into the static library ++ wasmedge_add_static_lib_component_command(fmt::fmt) ++ wasmedge_add_static_lib_component_command(spdlog::spdlog) ++ endif() + wasmedge_add_static_lib_component_command(wasmedgeSystem) + wasmedge_add_static_lib_component_command(wasmedgeCommon) + wasmedge_add_static_lib_component_command(wasmedgePO) diff --git a/bazel/repositories.bzl b/bazel/repositories.bzl index 1e3bfd427..e034faac0 100644 --- a/bazel/repositories.bzl +++ b/bazel/repositories.bzl @@ -339,14 +339,40 @@ def proxy_wasm_cpp_host_repositories(): ) # WasmEdge with dependencies. + # Using native Bazel cc_library rules based on Bazel Central Registry (BCR) definitions + # to avoid CMake FetchContent network access during build. + + # fmt dependency for spdlog (version 11.0.2, based on BCR) + # https://github.com/bazelbuild/bazel-central-registry/tree/main/modules/fmt/11.0.2 + maybe( + http_archive, + name = "com_github_fmtlib_fmt", + sha256 = "6cb1e6d37bdcb756dbbe59be438790db409cdb4868c66e888d5df9f13f7c027f", + strip_prefix = "fmt-11.0.2", + urls = ["https://github.com/fmtlib/fmt/archive/refs/tags/11.0.2.tar.gz"], + build_file = "@proxy_wasm_cpp_host//bazel/external:fmt.BUILD", + ) + + # spdlog dependency for WasmEdge (version 1.13.0, based on BCR) + # https://github.com/bazelbuild/bazel-central-registry/tree/main/modules/spdlog/1.13.0 + maybe( + http_archive, + name = "com_github_gabime_spdlog", + sha256 = "534f2ee1a4dcbeb22249856edfb2be76a1cf4f708a20b0ac2ed090ee24cfdbc9", + strip_prefix = "spdlog-1.13.0", + urls = ["https://github.com/gabime/spdlog/archive/refs/tags/v1.13.0.tar.gz"], + build_file = "@proxy_wasm_cpp_host//bazel/external:spdlog.BUILD", + ) maybe( http_archive, name = "com_github_wasmedge_wasmedge", build_file = "@proxy_wasm_cpp_host//bazel/external:wasmedge.BUILD", - sha256 = "7ab8a0df37c8d282ecff72d0f0bff8db63fd92df1645d5a014a9dbed4b7f9025", - strip_prefix = "WasmEdge-proxy-wasm-0.13.1", - url = "https://github.com/WasmEdge/WasmEdge/archive/refs/tags/proxy-wasm/0.13.1.tar.gz", + sha256 = "2354d90a67e3eb396179663bdc0b457abbbc70dca967ec4528f211599a49f62a", + strip_prefix = "WasmEdge-0.16.1", + url = "https://github.com/WasmEdge/WasmEdge/archive/refs/tags/0.16.1.tar.gz", + patches = ["@proxy_wasm_cpp_host//bazel/external:wasmedge.patch"], + patch_args = ["-p1"], ) # Wasmtime with dependencies. diff --git a/src/wasmedge/wasmedge.cc b/src/wasmedge/wasmedge.cc index 30fe78f5e..acfe15b3c 100644 --- a/src/wasmedge/wasmedge.cc +++ b/src/wasmedge/wasmedge.cc @@ -52,16 +52,15 @@ template <> WasmEdge_Value makeVal(double t) { return WasmEdge_ValueGenF64(t); } // Helper function to print values. std::string printValue(const WasmEdge_Value &value) { - switch (value.Type) { - case WasmEdge_ValType_I32: + if (WasmEdge_ValTypeIsI32(value.Type)) { return std::to_string(WasmEdge_ValueGetI32(value)); - case WasmEdge_ValType_I64: + } else if (WasmEdge_ValTypeIsI64(value.Type)) { return std::to_string(WasmEdge_ValueGetI64(value)); - case WasmEdge_ValType_F32: + } else if (WasmEdge_ValTypeIsF32(value.Type)) { return std::to_string(WasmEdge_ValueGetF32(value)); - case WasmEdge_ValType_F64: + } else if (WasmEdge_ValTypeIsF64(value.Type)) { return std::to_string(WasmEdge_ValueGetF64(value)); - default: + } else { return "unknown"; } } @@ -83,20 +82,19 @@ std::string printValues(const WasmEdge_Value *values, size_t size) { // Helper function to print valtype. const char *printValType(WasmEdge_ValType kind) { - switch (kind) { - case WasmEdge_ValType_I32: + if (WasmEdge_ValTypeIsI32(kind)) { return "i32"; - case WasmEdge_ValType_I64: + } else if (WasmEdge_ValTypeIsI64(kind)) { return "i64"; - case WasmEdge_ValType_F32: + } else if (WasmEdge_ValTypeIsF32(kind)) { return "f32"; - case WasmEdge_ValType_F64: + } else if (WasmEdge_ValTypeIsF64(kind)) { return "f64"; - case WasmEdge_ValType_ExternRef: + } else if (WasmEdge_ValTypeIsExternRef(kind)) { return "anyref"; - case WasmEdge_ValType_FuncRef: + } else if (WasmEdge_ValTypeIsFuncRef(kind)) { return "funcref"; - default: + } else { return "unknown"; } } @@ -118,6 +116,20 @@ std::string printValTypes(const WasmEdge_ValType *types, size_t size) { return s; } +// Helper function to compare two vectors of WasmEdge_ValType +bool valtypeVectorsEqual(const std::vector &v1, + const std::vector &v2) { + if (v1.size() != v2.size()) { + return false; + } + for (size_t i = 0; i < v1.size(); i++) { + if (!WasmEdge_ValTypeIsEqual(v1[i], v2[i])) { + return false; + } + } + return true; +} + template struct ConvertWordType { using type = T; // NOLINT(readability-identifier-naming) }; @@ -126,12 +138,12 @@ template <> struct ConvertWordType { }; // Helper templates to convert arg to valtype. -template enum WasmEdge_ValType convArgToValType(); -template <> enum WasmEdge_ValType convArgToValType() { return WasmEdge_ValType_I32; } -template <> enum WasmEdge_ValType convArgToValType() { return WasmEdge_ValType_I32; } -template <> enum WasmEdge_ValType convArgToValType() { return WasmEdge_ValType_I64; } -template <> enum WasmEdge_ValType convArgToValType() { return WasmEdge_ValType_I64; } -template <> enum WasmEdge_ValType convArgToValType() { return WasmEdge_ValType_F64; } +template WasmEdge_ValType convArgToValType(); +template <> WasmEdge_ValType convArgToValType() { return WasmEdge_ValTypeGenI32(); } +template <> WasmEdge_ValType convArgToValType() { return WasmEdge_ValTypeGenI32(); } +template <> WasmEdge_ValType convArgToValType() { return WasmEdge_ValTypeGenI64(); } +template <> WasmEdge_ValType convArgToValType() { return WasmEdge_ValTypeGenI64(); } +template <> WasmEdge_ValType convArgToValType() { return WasmEdge_ValTypeGenF64(); } // Helper templates to convert valtype to arg. template T convValTypeToArg(WasmEdge_Value val); @@ -164,11 +176,11 @@ constexpr T convValTypesToArgsTuple(const WasmEdge_Value *arr) { // Helper templates to convert args tuple to valtypes. template -uint32_t convArgsTupleToValTypesImpl(std::vector &types, +uint32_t convArgsTupleToValTypesImpl(std::vector &types, std::index_sequence /*comptime*/) { auto size = std::tuple_size::value; if (size > 0) { - auto ps = std::array::value>{ + auto ps = std::array::value>{ convArgToValType::type>()...}; types.resize(size); std::copy_n(ps.data(), size, types.data()); @@ -177,14 +189,14 @@ uint32_t convArgsTupleToValTypesImpl(std::vector &types, } template ::value>> -uint32_t convArgsTupleToValTypes(std::vector &types) { +uint32_t convArgsTupleToValTypes(std::vector &types) { return convArgsTupleToValTypesImpl(types, Is()); } // Helper templates to create WasmEdge_FunctionTypeContext. template WasmEdge_FunctionTypeContext *newWasmEdgeFuncType() { - std::vector params; - std::vector returns; + std::vector params; + std::vector returns; uint32_t param_nums = convArgsTupleToValTypes(params); uint32_t return_nums = convArgsTupleToValTypes>(returns); auto *ftype = WasmEdge_FunctionTypeCreate(params.data(), param_nums, returns.data(), return_nums); @@ -192,7 +204,7 @@ template WasmEdge_FunctionTypeContext *newWasmEdgeFuncT } template WasmEdge_FunctionTypeContext *newWasmEdgeFuncType() { - std::vector params; + std::vector params; uint32_t param_nums = convArgsTupleToValTypes(params); auto *ftype = WasmEdge_FunctionTypeCreate(params.data(), param_nums, nullptr, 0); return ftype; @@ -521,19 +533,17 @@ void WasmEdge::getModuleFunctionImpl(std::string_view function_name, return; } - std::vector exp_args; - std::vector exp_returns; + std::vector exp_args; + std::vector exp_returns; convArgsTupleToValTypes>(exp_args); convArgsTupleToValTypes>(exp_returns); const auto *functype_cxt = WasmEdge_FunctionInstanceGetFunctionType(func_cxt); - std::vector act_args( - WasmEdge_FunctionTypeGetParametersLength(functype_cxt)); - std::vector act_returns( - WasmEdge_FunctionTypeGetReturnsLength(functype_cxt)); + std::vector act_args(WasmEdge_FunctionTypeGetParametersLength(functype_cxt)); + std::vector act_returns(WasmEdge_FunctionTypeGetReturnsLength(functype_cxt)); WasmEdge_FunctionTypeGetParameters(functype_cxt, act_args.data(), act_args.size()); WasmEdge_FunctionTypeGetReturns(functype_cxt, act_returns.data(), act_returns.size()); - if (exp_args != act_args || exp_returns != act_returns) { + if (!valtypeVectorsEqual(exp_args, act_args) || !valtypeVectorsEqual(exp_returns, act_returns)) { fail(FailState::UnableToInitializeCode, "Bad function signature for: " + std::string(function_name) + ", want: " + printValTypes(exp_args.data(), exp_args.size()) + " -> " + @@ -574,19 +584,17 @@ void WasmEdge::getModuleFunctionImpl(std::string_view function_name, return; } - std::vector exp_args; - std::vector exp_returns; + std::vector exp_args; + std::vector exp_returns; convArgsTupleToValTypes>(exp_args); convArgsTupleToValTypes>(exp_returns); const auto *functype_cxt = WasmEdge_FunctionInstanceGetFunctionType(func_cxt); - std::vector act_args( - WasmEdge_FunctionTypeGetParametersLength(functype_cxt)); - std::vector act_returns( - WasmEdge_FunctionTypeGetReturnsLength(functype_cxt)); + std::vector act_args(WasmEdge_FunctionTypeGetParametersLength(functype_cxt)); + std::vector act_returns(WasmEdge_FunctionTypeGetReturnsLength(functype_cxt)); WasmEdge_FunctionTypeGetParameters(functype_cxt, act_args.data(), act_args.size()); WasmEdge_FunctionTypeGetReturns(functype_cxt, act_returns.data(), act_returns.size()); - if (exp_args != act_args || exp_returns != act_returns) { + if (!valtypeVectorsEqual(exp_args, act_args) || !valtypeVectorsEqual(exp_returns, act_returns)) { fail(FailState::UnableToInitializeCode, "Bad function signature for: " + std::string(function_name) + ", want: " + printValTypes(exp_args.data(), exp_args.size()) + " -> " +