Skip to content
Merged
Changes from all 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
56 changes: 27 additions & 29 deletions bazel/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,34 @@

## Setup Instructions

1. Merge the `WORKSPACE` file in with your own at the root of your bazel
directory structure. If you don't have one, simply copy the file.
2. Merge the `bazelrc` file in with your `.bazelrc` file at the root of your
bazel directory structure. If you don't have one, simply copy the file and
rename it to `.bazelrc`. (Note the `.`)
3. Copy the `emscripten_toolchain` folder along with its contents to the root of
your bazel directory.

Your directory structure should look like this:
In `WORKSPACE` file, put:
```
bazel_root/
├── .bazelrc
├── WORKSPACE
├── emscripten_toolchain/
│ ├── BUILD.bazel
│ ├── builddefs.bzl
│ ├── crosstool.bzl
│ ├── emar.sh
│ ├── emcc.sh
│ ├── emcc_link.sh
│ ├── emscripten.BUILD
│ ├── emscripten_config
│ ├── env.sh
│ ├── link_wrapper.py
│ ├── wasm_binary.py
│ ├── wasm_cc_binary.bzl
│ ├── wasm_rules.bzl
├── your_project_folder/
│ ├── your_project.file
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",
)

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

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

## Building

### Using --config=wasm

Put the following lines into your `.bazelrc`:
```
build:wasm --crosstool_top=//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.

Expand All @@ -44,7 +38,7 @@ First, write a new rule wrapping your `cc_binary`.

```
load("@rules_cc//cc:defs.bzl", "cc_binary")
load("//emscripten_toolchain:wasm_rules.bzl", "wasm_cc_binary")
load("@emsdk//emscripten_toolchain:wasm_rules.bzl", "wasm_cc_binary")

cc_binary(
name = "hello-world",
Expand All @@ -61,3 +55,7 @@ Now you can run `bazel build :hello-world-wasm`. The result of this build will
be the individual files produced by emscripten. Note that some of these files
may be empty. This is because bazel has no concept of optional outputs for
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.