From 9784c237ca58d763068d4909c7ebe9aec5a0bf0f Mon Sep 17 00:00:00 2001 From: Jin Huang Date: Sat, 27 Mar 2021 08:45:27 -0700 Subject: [PATCH 1/3] Improve bazel example --- bazel/README.md | 9 ++++++--- bazel/test_external/.bazelrc | 3 +++ bazel/test_external/.gitignore | 2 ++ bazel/test_external/BUILD | 3 +++ bazel/test_external/README.md | 8 ++++++++ bazel/test_external/hello-world.cc | 20 +++++++++++++++++++- 6 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 bazel/test_external/.bazelrc create mode 100644 bazel/test_external/.gitignore create mode 100644 bazel/test_external/README.md diff --git a/bazel/README.md b/bazel/README.md index dc849fd27a..35c40a892e 100644 --- a/bazel/README.md +++ b/bazel/README.md @@ -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 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`. diff --git a/bazel/test_external/.bazelrc b/bazel/test_external/.bazelrc new file mode 100644 index 0000000000..65caa9c974 --- /dev/null +++ b/bazel/test_external/.bazelrc @@ -0,0 +1,3 @@ +build:wasm --crosstool_top=@emsdk//emscripten_toolchain:everything +build:wasm --cpu=wasm +build:wasm --host_crosstool_top=@bazel_tools//tools/cpp:toolchain diff --git a/bazel/test_external/.gitignore b/bazel/test_external/.gitignore new file mode 100644 index 0000000000..787f054a89 --- /dev/null +++ b/bazel/test_external/.gitignore @@ -0,0 +1,2 @@ +bazel-* +dist/ \ No newline at end of file diff --git a/bazel/test_external/BUILD b/bazel/test_external/BUILD index 73568cf388..0392c18361 100644 --- a/bazel/test_external/BUILD +++ b/bazel/test_external/BUILD @@ -3,6 +3,9 @@ load("@emsdk//emscripten_toolchain:wasm_rules.bzl", "wasm_cc_binary") cc_binary( name = "hello-world", srcs = ["hello-world.cc"], + linkopts = [ + "--bind", + ], ) wasm_cc_binary( diff --git a/bazel/test_external/README.md b/bazel/test_external/README.md new file mode 100644 index 0000000000..25854772dc --- /dev/null +++ b/bazel/test_external/README.md @@ -0,0 +1,8 @@ +```bash +# Native +bazel run :hello-world + +# Build wasm +bazel build --config=wasm :hello-world +bazel build :hello-world-wasm +``` \ No newline at end of file diff --git a/bazel/test_external/hello-world.cc b/bazel/test_external/hello-world.cc index ee72c53171..f8916bdcc6 100644 --- a/bazel/test_external/hello-world.cc +++ b/bazel/test_external/hello-world.cc @@ -1,6 +1,24 @@ +#ifdef __EMSCRIPTEN__ +#include +#include +using namespace emscripten; +#endif + #include +void sayHello() { + std::cout << "hello" << std::endl; +} + +#ifdef __EMSCRIPTEN__ +EMSCRIPTEN_BINDINGS(aa) { + function("sayHello", &sayHello); +} +#else int main(int argc, char** argv) { - std::cout << "hello world!" << std::endl; + std::cout << "start main function" << std::endl; + sayHello(); + std::cout << "end main function" << std::endl; return 0; } +#endif From b892f7d90fee0481f286184558fb652ae48b9eef Mon Sep 17 00:00:00 2001 From: Jin Huang Date: Mon, 29 Mar 2021 12:35:24 -0700 Subject: [PATCH 2/3] minor fixes --- bazel/test_external/.gitignore | 1 - bazel/test_external/BUILD | 5 +---- bazel/test_external/README.md | 8 -------- bazel/test_external/hello-world.cc | 14 ++++++-------- 4 files changed, 7 insertions(+), 21 deletions(-) delete mode 100644 bazel/test_external/README.md diff --git a/bazel/test_external/.gitignore b/bazel/test_external/.gitignore index 787f054a89..ac51a054d2 100644 --- a/bazel/test_external/.gitignore +++ b/bazel/test_external/.gitignore @@ -1,2 +1 @@ bazel-* -dist/ \ No newline at end of file diff --git a/bazel/test_external/BUILD b/bazel/test_external/BUILD index 0392c18361..77e2ee8d01 100644 --- a/bazel/test_external/BUILD +++ b/bazel/test_external/BUILD @@ -3,13 +3,10 @@ load("@emsdk//emscripten_toolchain:wasm_rules.bzl", "wasm_cc_binary") cc_binary( name = "hello-world", srcs = ["hello-world.cc"], - linkopts = [ - "--bind", - ], + linkopts = ["--bind"], ) wasm_cc_binary( name = "hello-world-wasm", cc_target = ":hello-world", ) - diff --git a/bazel/test_external/README.md b/bazel/test_external/README.md deleted file mode 100644 index 25854772dc..0000000000 --- a/bazel/test_external/README.md +++ /dev/null @@ -1,8 +0,0 @@ -```bash -# Native -bazel run :hello-world - -# Build wasm -bazel build --config=wasm :hello-world -bazel build :hello-world-wasm -``` \ No newline at end of file diff --git a/bazel/test_external/hello-world.cc b/bazel/test_external/hello-world.cc index f8916bdcc6..94b08bfb7f 100644 --- a/bazel/test_external/hello-world.cc +++ b/bazel/test_external/hello-world.cc @@ -1,24 +1,22 @@ +#include + #ifdef __EMSCRIPTEN__ #include #include using namespace emscripten; #endif -#include - void sayHello() { std::cout << "hello" << std::endl; } #ifdef __EMSCRIPTEN__ -EMSCRIPTEN_BINDINGS(aa) { +EMSCRIPTEN_BINDINGS(hello) { function("sayHello", &sayHello); } -#else +#endif + int main(int argc, char** argv) { - std::cout << "start main function" << std::endl; - sayHello(); - std::cout << "end main function" << std::endl; + std::cout << "hello world!" << std::endl; return 0; } -#endif From d49696511a2d5dc792fc118f1d9e1465da865ee3 Mon Sep 17 00:00:00 2001 From: Jin Huang Date: Tue, 30 Mar 2021 08:14:16 -0700 Subject: [PATCH 3/3] Remove some unwanted changes --- bazel/test_external/BUILD | 2 +- bazel/test_external/hello-world.cc | 16 ---------------- 2 files changed, 1 insertion(+), 17 deletions(-) diff --git a/bazel/test_external/BUILD b/bazel/test_external/BUILD index 77e2ee8d01..73568cf388 100644 --- a/bazel/test_external/BUILD +++ b/bazel/test_external/BUILD @@ -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", ) + diff --git a/bazel/test_external/hello-world.cc b/bazel/test_external/hello-world.cc index 94b08bfb7f..ee72c53171 100644 --- a/bazel/test_external/hello-world.cc +++ b/bazel/test_external/hello-world.cc @@ -1,21 +1,5 @@ #include -#ifdef __EMSCRIPTEN__ -#include -#include -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;