Skip to content

Commit 9784c23

Browse files
committed
Improve bazel example
1 parent 6162e2e commit 9784c23

File tree

6 files changed

+41
-4
lines changed

6 files changed

+41
-4
lines changed

bazel/README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,16 @@ emsdk_emscripten_deps()
2525

2626
Put the following lines into your `.bazelrc`:
2727
```
28-
build:wasm --crosstool_top=//emscripten_toolchain:everything
28+
build:wasm --crosstool_top=@emsdk//emscripten_toolchain:everything
2929
build:wasm --cpu=wasm
3030
build:wasm --host_crosstool_top=@bazel_tools//tools/cpp:toolchain
3131
```
3232

33-
Simply pass `--config=wasm` when building a normal `cc_binary`. The result of
34-
this build will be a tar archive containing any files produced by emscripten.
33+
Simply pass `--config=wasm` when building a normal `cc_binary`, e.g.
34+
```
35+
bazel build --config=wasm :hello-world
36+
```
37+
The result of this build will be a tar archive containing any files produced by emscripten.
3538

3639
### Using wasm_cc_binary
3740
First, write a new rule wrapping your `cc_binary`.

bazel/test_external/.bazelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
build:wasm --crosstool_top=@emsdk//emscripten_toolchain:everything
2+
build:wasm --cpu=wasm
3+
build:wasm --host_crosstool_top=@bazel_tools//tools/cpp:toolchain

bazel/test_external/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bazel-*
2+
dist/

bazel/test_external/BUILD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ load("@emsdk//emscripten_toolchain:wasm_rules.bzl", "wasm_cc_binary")
33
cc_binary(
44
name = "hello-world",
55
srcs = ["hello-world.cc"],
6+
linkopts = [
7+
"--bind",
8+
],
69
)
710

811
wasm_cc_binary(

bazel/test_external/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
```bash
2+
# Native
3+
bazel run :hello-world
4+
5+
# Build wasm
6+
bazel build --config=wasm :hello-world
7+
bazel build :hello-world-wasm
8+
```

bazel/test_external/hello-world.cc

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
1+
#ifdef __EMSCRIPTEN__
2+
#include <emscripten.h>
3+
#include <emscripten/bind.h>
4+
using namespace emscripten;
5+
#endif
6+
17
#include <iostream>
28

9+
void sayHello() {
10+
std::cout << "hello" << std::endl;
11+
}
12+
13+
#ifdef __EMSCRIPTEN__
14+
EMSCRIPTEN_BINDINGS(aa) {
15+
function("sayHello", &sayHello);
16+
}
17+
#else
318
int main(int argc, char** argv) {
4-
std::cout << "hello world!" << std::endl;
19+
std::cout << "start main function" << std::endl;
20+
sayHello();
21+
std::cout << "end main function" << std::endl;
522
return 0;
623
}
24+
#endif

0 commit comments

Comments
 (0)