Add embind example to Bazel docs (#910)
* Add embind example to Bazel docs * address feedback
This commit is contained in:
@@ -7,33 +7,26 @@ In `WORKSPACE` file, put:
|
||||
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
|
||||
http_archive(
|
||||
name = "emsdk",
|
||||
strip_prefix = "emsdk-c1589b55641787d55d53e883852035beea9aec3f/bazel",
|
||||
url = "https://github.com/emscripten-core/emsdk/archive/c1589b55641787d55d53e883852035beea9aec3f.tar.gz",
|
||||
sha256 = "7a58a9996b113d3e0675df30b5f17e28aa47de2e684a844f05394fe2f6f12e8e",
|
||||
sha256 = "d55e3c73fc4f8d1fecb7aabe548de86bdb55080fe6b12ce593d63b8bade54567",
|
||||
strip_prefix = "emsdk-3891e7b04bf8cbb3bc62758e9c575ae096a9a518/bazel",
|
||||
url = "https://github.com/emscripten-core/emsdk/archive/3891e7b04bf8cbb3bc62758e9c575ae096a9a518.tar.gz",
|
||||
)
|
||||
|
||||
load("@emsdk//:deps.bzl", emsdk_deps = "deps")
|
||||
emsdk_deps()
|
||||
|
||||
load("@emsdk//:emscripten_deps.bzl", emsdk_emscripten_deps = "emscripten_deps")
|
||||
emsdk_emscripten_deps()
|
||||
emsdk_emscripten_deps(emscripten_version = "2.0.31")
|
||||
```
|
||||
The SHA1 hash in the above `strip_prefix` and `url` parameters correspond to the git revision of
|
||||
[emsdk 2.0.31](https://github.com/emscripten-core/emsdk/releases/tag/2.0.31). To get access to
|
||||
newer versions, you'll need to update those. To make use of older versions, change the
|
||||
parameter of `emsdk_emscripten_deps()`. Supported versions are listed in `revisions.bzl`
|
||||
|
||||
|
||||
## Building
|
||||
|
||||
### Using --config=wasm
|
||||
|
||||
Put the following lines into your `.bazelrc`:
|
||||
```
|
||||
build:wasm --crosstool_top=@emsdk//emscripten_toolchain:everything
|
||||
build:wasm --cpu=wasm
|
||||
build:wasm --host_crosstool_top=@bazel_tools//tools/cpp:toolchain
|
||||
```
|
||||
|
||||
Simply pass `--config=wasm` when building a normal `cc_binary`. The result of
|
||||
this build will be a tar archive containing any files produced by emscripten.
|
||||
|
||||
### Using wasm_cc_binary
|
||||
### Using wasm_cc_binary (preferred)
|
||||
First, write a new rule wrapping your `cc_binary`.
|
||||
|
||||
```
|
||||
@@ -59,3 +52,19 @@ rules.
|
||||
`wasm_cc_binary` uses transition to use emscripten toolchain on `cc_target`
|
||||
and all of its dependencies, and does not require amending `.bazelrc`. This
|
||||
is the preferred way, since it also unpacks the resulting tarball.
|
||||
|
||||
See `test_external/` for an example using [embind](https://emscripten.org/docs/porting/connecting_cpp_and_javascript/embind.html).
|
||||
|
||||
### Using --config=wasm
|
||||
|
||||
Put the following lines into your `.bazelrc`:
|
||||
```
|
||||
build:wasm --crosstool_top=@emsdk//emscripten_toolchain:everything
|
||||
build:wasm --cpu=wasm
|
||||
build:wasm --host_crosstool_top=@bazel_tools//tools/cpp:toolchain
|
||||
```
|
||||
|
||||
Simply pass `--config=wasm` when building a normal `cc_binary`. The result of
|
||||
this build will be a tar archive containing any files produced by emscripten.
|
||||
See the [Bazel documentation](https://docs.bazel.build/versions/main/tutorial/cc-toolchain-config.html)
|
||||
for more details
|
||||
|
||||
4
bazel/test_external/.gitignore
vendored
Normal file
4
bazel/test_external/.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
bazel-bin
|
||||
bazel-out
|
||||
bazel-test_external
|
||||
bazel-testlogs
|
||||
@@ -10,3 +10,22 @@ wasm_cc_binary(
|
||||
cc_target = ":hello-world",
|
||||
)
|
||||
|
||||
cc_binary(
|
||||
name = "hello-embind",
|
||||
srcs = ["hello-embind.cc"],
|
||||
# It's probably a good idea to use select() and config_setting
|
||||
# to add different linkopts to BASE_LINKOPTS depending on things
|
||||
# like a "opt" or "dbg" compilation mode.
|
||||
linkopts = [
|
||||
"--bind", # Enable embind
|
||||
"-sMODULARIZE",
|
||||
],
|
||||
# This target won't build successfully on its own because of missing emscripten
|
||||
# headers etc. Therefore, we hide it from wildcards.
|
||||
tags = ["manual"],
|
||||
)
|
||||
|
||||
wasm_cc_binary(
|
||||
name = "hello-embind-wasm",
|
||||
cc_target = ":hello-embind",
|
||||
)
|
||||
|
||||
16
bazel/test_external/hello-embind.cc
Normal file
16
bazel/test_external/hello-embind.cc
Normal file
@@ -0,0 +1,16 @@
|
||||
#include <emscripten/bind.h>
|
||||
|
||||
using namespace emscripten;
|
||||
|
||||
class HelloClass {
|
||||
public:
|
||||
static std::string SayHello(const std::string &name) {
|
||||
return "Yo! " + name;
|
||||
};
|
||||
};
|
||||
|
||||
EMSCRIPTEN_BINDINGS(Hello) {
|
||||
emscripten::class_<HelloClass>("HelloClass")
|
||||
.constructor<>()
|
||||
.class_function("SayHello", &HelloClass::SayHello);
|
||||
}
|
||||
@@ -26,3 +26,4 @@ bazel build //hello-world:hello-world-wasm-simd
|
||||
|
||||
cd test_external
|
||||
bazel build //:hello-world-wasm
|
||||
bazel build //:hello-embind-wasm
|
||||
|
||||
Reference in New Issue
Block a user