diff --git a/BUILD.bazel b/BUILD.bazel index d435f8d..ef198d9 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -5,7 +5,10 @@ package(default_visibility = ["//visibility:public"]) filegroup( name = "headers", - srcs = ["ecsact/wasm.h", "ecsact/wasm.hh"], + srcs = [ + "ecsact/wasm.h", + "ecsact/wasm.hh", + ], ) filegroup( @@ -17,6 +20,19 @@ filegroup( ]), ) +cc_library( + name = "ecsact_si_wasm", + copts = copts, + defines = ["ECSACTSI_WASM_API="], + hdrs = [":headers"], + srcs = [":sources"], + deps = [ + "@wasmer", + "@ecsact_runtime//:common", + "@ecsact_runtime//:dynamic", + ], +) + cc_library( name = "minst", copts = copts, diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index 8be1eea..830214e 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -1,10 +1,4 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") -load("@ecsact_si_wasm//:wasmer.bzl", "wasmer_repo") - -wasmer_repo( - name = "wasmer", - wasmer_version = "3.3.0", -) http_archive( name = "com_grail_bazel_toolchain", diff --git a/bazel/BUILD.bazel b/bazel/BUILD.bazel index e69de29..c9e1c55 100644 --- a/bazel/BUILD.bazel +++ b/bazel/BUILD.bazel @@ -0,0 +1,4 @@ +config_setting( + name = "compiler_emscripten", + flag_values = {"@bazel_tools//tools/cpp:compiler": "emscripten"}, +) diff --git a/bazel/copts.bzl b/bazel/copts.bzl index 8ddeb95..5895e7d 100644 --- a/bazel/copts.bzl +++ b/bazel/copts.bzl @@ -2,6 +2,9 @@ load("@bazel_skylib//lib:selects.bzl", "selects") # Ecsact repositories currently only support clang and cl copts = selects.with_or({ + (Label("//bazel:compiler_emscripten")): [ + "-std=c++20", + ], ("@rules_cc//cc/compiler:clang"): [ "-std=c++2b", "-fexperimental-library", diff --git a/ecsact/wasm.hh b/ecsact/wasm.hh index 0696eba..855234d 100644 --- a/ecsact/wasm.hh +++ b/ecsact/wasm.hh @@ -27,8 +27,8 @@ struct system_load_options { }; inline auto load_file( - std::filesystem::path wasm_file_path, - std::span systems + std::filesystem::path wasm_file_path, + std::span systems ) -> ecsactsi_wasm_error { auto system_ids = std::vector{}; auto wasm_exports = std::vector{}; @@ -50,7 +50,7 @@ inline auto load_file( wasm_file_path.string().c_str(), static_cast(systems.size()), system_ids.data(), - wasm_exports.data() + wasm_exports_c.data() ); } @@ -75,11 +75,11 @@ inline auto load( } return ecsactsi_wasm_load( - static_cast(wasm_data.data()), + reinterpret_cast(wasm_data.data()), static_cast(wasm_data.size()), static_cast(systems.size()), system_ids.data(), - wasm_exports.data() + wasm_exports_c.data() ); } @@ -116,8 +116,8 @@ auto consume_logs(log_consumer auto&& f) -> void { std::int32_t message_length, void* user_data ) { - static_cast(user_data)( - reinterpret_cast(level), + (*static_cast(user_data))( + static_cast(level), std::string_view{message, static_cast(message_length)} ); }, diff --git a/ecsact/wasm/detail/wasm.cc b/ecsact/wasm/detail/wasm.cc index 14fc7f8..b1b7d5b 100644 --- a/ecsact/wasm/detail/wasm.cc +++ b/ecsact/wasm/detail/wasm.cc @@ -20,7 +20,6 @@ #include #include #include "ecsact/runtime/dynamic.h" -#include "ecsact/runtime/meta.h" #include "ecsact/wasm/detail/minst/minst.hh" #include "ecsact/wasm/detail/logger.hh" #include "ecsact/wasm/detail/wasi_fs.hh" diff --git a/example/BUILD.bazel b/example/BUILD.bazel deleted file mode 100644 index be96c49..0000000 --- a/example/BUILD.bazel +++ /dev/null @@ -1,184 +0,0 @@ -load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_test") -load("//bazel:copts.bzl", "copts") -load("@emsdk//emscripten_toolchain:wasm_rules.bzl", "wasm_cc_binary") -load("@ecsact_rt_entt//runtime:index.bzl", "ecsact_entt_runtime") - -package(default_visibility = ["//:__subpackages__"]) - -ecsact_entt_runtime( - name = "runtime", - srcs = ["example.ecsact"], - ECSACT_ENTT_RUNTIME_PACKAGE = "::example::package", - ECSACT_ENTT_RUNTIME_USER_HEADER = "example.ecsact.meta.hh", - system_impls = ["dynamic"], -) - -cc_binary( - name = "system_impls", - srcs = [ - "system_impls.cc", - ], - copts = ["-std=c++20"], - features = [ - "wasm_no_entry", - "-wasm_warnings_as_errors", - "-wasm_error_on_undefined_symbols", - "-exceptions", - ], - linkshared = True, - tags = ["manual"], - deps = [ - ":runtime__public_cc", - "@ecsact_lang_cpp//:execution_context", - "@ecsact_lang_cpp//:support", - "@ecsact_runtime//:common", - "@ecsact_runtime//:dynamic", - ], -) - -_system_names = [ - "ExampleSystem", - "Generator", - "AddsSystem", - "CheckShouldRemove", - "RemovesSystem", -] - -wasm_cc_binary( - name = "system_impls_wasm", - backend = "llvm", - cc_target = ":system_impls", - exported_functions = [ - "_example__%s" % system_name - for system_name in _system_names - ], - outputs = [ - "system_impls.wasm", - "system_impls.debug.wasm", - ], - standalone = True, -) - -cc_binary( - name = "wasmer_example", - srcs = [ - "example.cc", - "//:sources", - "//:headers", - ], - copts = copts, - defines = ["ECSACTSI_WASM_API="], - deps = [ - ":runtime", - "//:minst", - "@ecsact_runtime//:core", - "@ecsact_runtime//:dynamic", - "@ecsact_runtime//:meta", - "@magic_enum", - "@wasmer//:wasmer-c-api", - ], -) - -cc_test( - name = "wasmer_example_test", - srcs = ["test.cc"], - args = [ - "--exe=$(location :wasmer_example)", - "$(location :system_impls.wasm)", - ], - copts = copts, - data = [ - ":system_impls.wasm", - ":wasmer_example", - ], -) - -############################################### -# WASI TESTS # -############################################### - -# keep sorted -_WASI_TESTS = [ - "iostream", - "printf", - "puts", - # Reading files not supported (yet) - # https://github.com/ecsact-dev/ecsact_si_wasm/issues/30 - # "read_file", -] - -ecsact_entt_runtime( - name = "wasi_test_runtime", - srcs = ["wasi_test.ecsact"], - ECSACT_ENTT_RUNTIME_PACKAGE = "::wasi_test::package", - ECSACT_ENTT_RUNTIME_USER_HEADER = "wasi_test.ecsact.meta.hh", - system_impls = ["dynamic"], -) - -[cc_binary( - name = "{}__system_impl".format(wasi_test), - srcs = ["wasi_test_{}.cc".format(wasi_test)], - copts = copts, - features = [ - "wasm_no_entry", - "-wasm_warnings_as_errors", - "-wasm_error_on_undefined_symbols", - "-exceptions", - ], - linkshared = True, - tags = ["manual"], - deps = [ - ":wasi_test_runtime__public_cc", - "@ecsact_lang_cpp//:execution_context", - "@ecsact_lang_cpp//:support", - "@ecsact_runtime//:common", - "@ecsact_runtime//:dynamic", - ], -) for wasi_test in _WASI_TESTS] - -[wasm_cc_binary( - name = "{}__system_impl_wasm".format(wasi_test), - backend = "llvm", - cc_target = ":{}__system_impl".format(wasi_test), - exported_functions = ["_wasi_test__WasiTestSystem"], - outputs = ["{}__system_impl.wasm".format(wasi_test)], - standalone = True, -) for wasi_test in _WASI_TESTS] - -[cc_binary( - name = "{}__wasi".format(wasi_test), - srcs = [ - "wasi_test.cc", - "//:sources", - "//:headers", - ], - copts = copts, - data = [ - ":{}__system_impl.wasm".format(wasi_test), - "wasi_test_file.txt", - ], - defines = ["ECSACTSI_WASM_API="], - deps = [ - ":wasi_test_runtime", - "@ecsact_runtime//:core", - "@ecsact_runtime//:dynamic", - "@ecsact_runtime//:meta", - "@magic_enum", - "@wasmer//:wasmer-c-api", - ], -) for wasi_test in _WASI_TESTS] - -[cc_test( - name = "{}__wasi_test".format(wasi_test), - srcs = ["test.cc"], - args = [ - "--exe=$(location :{}__wasi)".format(wasi_test), - "$(location :{}__system_impl.wasm)".format(wasi_test), - ], - copts = copts, - data = [ - ":{}__system_impl.wasm".format(wasi_test), - ":{}__wasi".format(wasi_test), - "wasi_test_file.txt", - ], -) for wasi_test in _WASI_TESTS] diff --git a/test/BUILD.bazel b/test/BUILD.bazel index 8a47548..872de1b 100644 --- a/test/BUILD.bazel +++ b/test/BUILD.bazel @@ -1,11 +1,11 @@ load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_test") +load("@ecsact_rt_entt//runtime:index.bzl", "ecsact_entt_runtime") load("@emsdk//emscripten_toolchain:wasm_rules.bzl", "wasm_cc_binary") -load("//:copts.bzl", "copts") +load("@ecsact_si_wasm//bazel:copts.bzl", "copts") -config_setting( - name = "compiler_emscripten", - flag_values = {"@bazel_tools//tools/cpp:compiler": "emscripten"}, -) +package(default_visibility = [":__subpackages__"]) + +exports_files(["test.cc"]) cc_test( name = "minst_test", @@ -41,3 +41,88 @@ wasm_cc_binary( outputs = ["minst_test.wasm"], standalone = True, ) + +# keep sorted +_WASI_TESTS = [ + "iostream", + "printf", + "puts", + # Reading files not supported (yet) + # https://github.com/ecsact-dev/ecsact_si_wasm/issues/30 + # "read_file", +] + +ecsact_entt_runtime( + name = "wasi_test_runtime", + srcs = ["wasi_test.ecsact"], + ECSACT_ENTT_RUNTIME_PACKAGE = "::wasi_test::package", + ECSACT_ENTT_RUNTIME_USER_HEADER = "wasi_test.ecsact.meta.hh", + system_impls = ["dynamic"], +) + +[cc_binary( + name = "{}__system_impl".format(wasi_test), + srcs = ["wasi_test_{}.cc".format(wasi_test)], + copts = copts, + features = [ + "wasm_no_entry", + "-wasm_warnings_as_errors", + "-wasm_error_on_undefined_symbols", + "-exceptions", + ], + linkshared = True, + tags = ["manual"], + deps = [ + ":wasi_test_runtime__public_cc", + "@ecsact_lang_cpp//:execution_context", + "@ecsact_lang_cpp//:support", + "@ecsact_runtime//:common", + "@ecsact_runtime//:dynamic", + ], +) for wasi_test in _WASI_TESTS] + +[wasm_cc_binary( + name = "{}__system_impl_wasm".format(wasi_test), + backend = "llvm", + cc_target = ":{}__system_impl".format(wasi_test), + exported_functions = ["_wasi_test__WasiTestSystem"], + outputs = ["{}__system_impl.wasm".format(wasi_test)], + standalone = True, +) for wasi_test in _WASI_TESTS] + +[cc_binary( + name = "{}__wasi".format(wasi_test), + srcs = [ + "wasi_test.cc", + ], + copts = copts, + data = [ + ":{}__system_impl.wasm".format(wasi_test), + "wasi_test_file.txt", + ], + defines = ["ECSACTSI_WASM_API="], + deps = [ + ":wasi_test_runtime", + "@ecsact_runtime//:core", + "@ecsact_runtime//:dynamic", + "@ecsact_runtime//:meta", + "@magic_enum", + "@wasmer", + "@ecsact_si_wasm", + ], +) for wasi_test in _WASI_TESTS] + +[cc_test( + name = "{}__wasi_test".format(wasi_test), + srcs = ["test.cc"], + args = [ + "--exe=$(location :{}__wasi)".format(wasi_test), + "$(location :{}__system_impl.wasm)".format(wasi_test), + ], + copts = copts, + data = [ + ":{}__system_impl.wasm".format(wasi_test), + ":{}__wasi".format(wasi_test), + "wasi_test_file.txt", + ], +) for wasi_test in _WASI_TESTS] diff --git a/test/MODULE.bazel b/test/MODULE.bazel index 029cd73..6a2fb80 100644 --- a/test/MODULE.bazel +++ b/test/MODULE.bazel @@ -1,8 +1,13 @@ module(name = "ecsact_si_wasm_test") -bazel_dep(name = "rules_cc", version = "0.0.8") +bazel_dep(name = "rules_cc", version = "0.0.9") bazel_dep(name = "ecsact_runtime", version = "0.5.1") bazel_dep(name = "rules_wasmer", version = "0.1.0") +bazel_dep(name = "ecsact_rt_entt", version = "0.2.1") +bazel_dep(name = "ecsact_lang_cpp", version = "0.3.1") +bazel_dep(name = "magic_enum", version = "0.9.3") +bazel_dep(name = "boost.mp11", version = "1.83.0.bzl.1") +bazel_dep(name = "entt", version = "3.12.2") bazel_dep(name = "ecsact_si_wasm") local_path_override( diff --git a/test/example/BUILD.bazel b/test/example/BUILD.bazel new file mode 100644 index 0000000..a56e093 --- /dev/null +++ b/test/example/BUILD.bazel @@ -0,0 +1,93 @@ +load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_test") +load("@ecsact_si_wasm//bazel:copts.bzl", "copts") +load("@ecsact_rt_entt//runtime:index.bzl", "ecsact_entt_runtime") +load("@emsdk//emscripten_toolchain:wasm_rules.bzl", "wasm_cc_binary") + +package(default_visibility = ["//:__subpackages__"]) + +ecsact_entt_runtime( + name = "runtime", + srcs = ["example.ecsact"], + ECSACT_ENTT_RUNTIME_PACKAGE = "::example::package", + ECSACT_ENTT_RUNTIME_USER_HEADER = "example.ecsact.meta.hh", + system_impls = ["dynamic"], +) + +cc_binary( + name = "system_impls", + srcs = [ + "system_impls.cc", + ], + copts = ["-std=c++20"], + features = [ + "wasm_no_entry", + "-wasm_warnings_as_errors", + "-wasm_error_on_undefined_symbols", + "-exceptions", + ], + linkshared = True, + tags = ["manual"], + deps = [ + ":runtime__public_cc", + "@ecsact_lang_cpp//:execution_context", + "@ecsact_lang_cpp//:support", + "@ecsact_runtime//:common", + "@ecsact_runtime//:dynamic", + ], +) + +_system_names = [ + "ExampleSystem", + "Generator", + "AddsSystem", + "CheckShouldRemove", + "RemovesSystem", +] + +wasm_cc_binary( + name = "system_impls_wasm", + backend = "llvm", + cc_target = ":system_impls", + exported_functions = [ + "_example__%s" % system_name + for system_name in _system_names + ], + outputs = [ + "system_impls.wasm", + "system_impls.debug.wasm", + ], + standalone = True, +) + +cc_binary( + name = "wasmer_example", + srcs = [ + "example.cc", + ], + copts = copts, + defines = ["ECSACTSI_WASM_API="], + deps = [ + ":runtime", + "@ecsact_si_wasm//:minst", + "@ecsact_runtime//:core", + "@ecsact_runtime//:dynamic", + "@ecsact_runtime//:meta", + "@magic_enum", + "@wasmer", + "@ecsact_si_wasm", + ], +) + +cc_test( + name = "wasmer_example_test", + srcs = ["//:test.cc"], + args = [ + "--exe=$(location :wasmer_example)", + "$(location :system_impls.wasm)", + ], + copts = copts, + data = [ + ":system_impls.wasm", + ":wasmer_example", + ], +) diff --git a/test/example.cc b/test/example/example.cc similarity index 100% rename from test/example.cc rename to test/example/example.cc diff --git a/test/example.ecsact b/test/example/example.ecsact similarity index 100% rename from test/example.ecsact rename to test/example/example.ecsact diff --git a/test/system_impls.cc b/test/example/system_impls.cc similarity index 100% rename from test/system_impls.cc rename to test/example/system_impls.cc