Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve bazel example #776

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions bazel/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ emsdk_emscripten_deps()

Put the following lines into your `.bazelrc`:
```
build:wasm --crosstool_top=//emscripten_toolchain:everything
build:wasm --crosstool_top=@emsdk//emscripten_toolchain:everything
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a bug.

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.
Simply pass `--config=wasm` when building a normal `cc_binary`, e.g.
```
bazel build --config=wasm :hello-world
```
The result of this build will be a tar archive containing any files produced by emscripten.

### Using wasm_cc_binary
First, write a new rule wrapping your `cc_binary`.
Expand Down
3 changes: 3 additions & 0 deletions bazel/test_external/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build:wasm --crosstool_top=@emsdk//emscripten_toolchain:everything
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

without these, the example would not work for :hello-world.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's ok for the example to not work with --config=wasm right out of the box; we'd rather promote the use of wasm_cc_binary as it produces more useful output anyway. Also, the readme covers adding the appropriate lines to your own .bazelrc file, which would enable --config=wasm builds to function. The big thing here I want to avoid is having this file be identical to bazel/bazelrc and having to keep them both in sync.

build:wasm --cpu=wasm
build:wasm --host_crosstool_top=@bazel_tools//tools/cpp:toolchain
1 change: 1 addition & 0 deletions bazel/test_external/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bazel-*
2 changes: 1 addition & 1 deletion bazel/test_external/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ load("@emsdk//emscripten_toolchain:wasm_rules.bzl", "wasm_cc_binary")
cc_binary(
name = "hello-world",
srcs = ["hello-world.cc"],
linkopts = ["--bind"],
)

wasm_cc_binary(
name = "hello-world-wasm",
cc_target = ":hello-world",
)

16 changes: 16 additions & 0 deletions bazel/test_external/hello-world.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
#include <iostream>

#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#include <emscripten/bind.h>
using namespace emscripten;
#endif

void sayHello() {
std::cout << "hello" << std::endl;
}

#ifdef __EMSCRIPTEN__
EMSCRIPTEN_BINDINGS(hello) {
function("sayHello", &sayHello);
}
#endif

int main(int argc, char** argv) {
std::cout << "hello world!" << std::endl;
return 0;
Expand Down