Skip to content

Commit

Permalink
setup bazel to run emscripten on files
Browse files Browse the repository at this point in the history
it works!
  • Loading branch information
NovaSagittarii committed Sep 23, 2023
1 parent f8fb781 commit a13ece9
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 3 deletions.
1 change: 1 addition & 0 deletions .bazelversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6.3.2
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@
*.cmake
Makefile
CMakeCache.txt

# bazel
bazel-bin
bazel-conway-rts
bazel-out
bazel-testlogs
10 changes: 10 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
load("@rules_cc//cc:defs.bzl", "cc_library")

package(default_visibility = ["//visibility:public"])

cc_library(
name = "vector2",
srcs = ["src/Vector2.cpp"],
hdrs = ["include/Vector2.hpp", "include/ISerializable.hpp"],
copts = ["-I include"],
)
38 changes: 38 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
workspace(
name = "conway_rts",
)

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "aspect_rules_js",
sha256 = "77c4ea46c27f96e4aadcc580cd608369208422cf774988594ae8a01df6642c82",
strip_prefix = "rules_js-1.32.2",
url = "https://github.com/aspect-build/rules_js/releases/download/v1.32.2/rules_js-v1.32.2.tar.gz",
)

load("@aspect_rules_js//js:repositories.bzl", "rules_js_dependencies")
rules_js_dependencies()

load("@rules_nodejs//nodejs:repositories.bzl", "DEFAULT_NODE_VERSION", "nodejs_register_toolchains")
nodejs_register_toolchains(
name = "nodejs",
node_version = DEFAULT_NODE_VERSION,
)

# Emscripten toolchain
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "emsdk",
sha256 = "5dd94e557b720800a60387ec078bf3b3a527cbd916ad74a696fe399f1544474f",
strip_prefix = "emsdk-3.1.46/bazel",
url = "https://github.com/emscripten-core/emsdk/archive/refs/tags/3.1.46.tar.gz",
)

load("@emsdk//:deps.bzl", emsdk_deps = "deps")
emsdk_deps()

load("@emsdk//:emscripten_deps.bzl", emsdk_emscripten_deps = "emscripten_deps")
emsdk_emscripten_deps(emscripten_version = "3.1.46")

load("@emsdk//:toolchains.bzl", "register_emscripten_toolchains")
register_emscripten_toolchains()
40 changes: 40 additions & 0 deletions client/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
load("@emsdk//emscripten_toolchain:wasm_rules.bzl", "wasm_cc_binary")

package(default_visibility = ["//visibility:public"])

DEFAULT_EMSCRIPTEN_LINKOPTS = [
# LTO broken when using hermetic emsdk
# "-flto", # Specify lto (has to be set on for compiler as well)
"--bind", # Compiles the source code using the Embind bindings to connect C/C++ and JavaScript
"--closure 1", # Run the closure compiler
"-s MALLOC=emmalloc", # Switch to using the much smaller implementation
"-s ALLOW_MEMORY_GROWTH=0", # Our example doesn't need memory growth
"-s USE_PTHREADS=0", # Disable pthreads
"-s ASSERTIONS=0", # Turn off assertions
# "-s EXPORT_ES6=1", # Export as es6 module, used for rollup
"-s MODULARIZE=1", # Allows us to manually invoke the initializatio of wasm
"-s EXPORT_NAME=createModule", # Not used, but good to specify
# "-s USE_ES6_IMPORT_META=1", # needed for somethingggg --- Disable loading from import meta since we use rollup
"-s SINGLE_FILE=1", # Pack all webassembly into base64
"-s DISABLE_EXCEPTION_CATCHING=1", # Disable all exception catching
"-s NODEJS_CATCH_EXIT=0", # We don't have a 'main' so disable exit() catching
]

WASM_LINKOPTS = [
"-s WASM=0", # Specify wasm output
]

cc_binary(
name = "lib-wasm.js",
srcs = ["bindings.cpp"],
linkopts = DEFAULT_EMSCRIPTEN_LINKOPTS + WASM_LINKOPTS,
deps = [
"//:vector2",
# "//hello-world/cpp:localtime",
],
)

wasm_cc_binary(
name = "lib-wasm",
cc_target = ":lib-wasm.js",
)
12 changes: 12 additions & 0 deletions client/bindings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "emscripten/bind.h"
#include "include/Vector2.hpp"

EMSCRIPTEN_BINDINGS(Vector2) {
emscripten::class_<Vector2>("Vector2")
.constructor<int, int>()
.function("x", &Vector2::GetX)
.function("y", &Vector2::GetY)
.function("SetX", &Vector2::SetX)
.function("SetY", &Vector2::SetY)
;
}
6 changes: 3 additions & 3 deletions include/ISerializable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
template<typename T>
class ISerializable {
public:
// https://stackoverflow.com/a/8513537
virtual ~ISerializable() {} // all destructors must exist (necessary for bazel)

// https://stackoverflow.com/a/437507/21507383
// friendship is not transitive or inherited, so these have to
// be redeclared if private access is needed
Expand All @@ -21,7 +24,4 @@ class ISerializable {
virtual bool ParseFromIstream(std::istream& in) = 0;
};

// https://stackoverflow.com/a/8513537
// ISerializable<>::~ISerializable() { } // all destructors must exist

#endif // CONWAY_INCLUDE_ISERIALIZABLE_HPP
6 changes: 6 additions & 0 deletions include/Vector2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ class Vector2: public ISerializable<Vector2> {
if constexpr (Index == 0) return x;
if constexpr (Index == 1) return y;
}

// embind stuff
int GetX() const { return _x; }
int GetY() const { return _y; }
void SetX(int x) { _x = x; }
void SetY(int y) { _y = y; }
};

// https://www.albertogramaglia.com/modern-cpp-structured-binding/
Expand Down

0 comments on commit a13ece9

Please sign in to comment.