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

Conversation

jinfwhuang
Copy link

Added emscripten bindings to bazel examples.

@@ -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.

@@ -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",
Copy link
Author

Choose a reason for hiding this comment

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

Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is this not formatted on one line like srcs above? Is this the bazel style?

Copy link
Author

Choose a reason for hiding this comment

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

linkopts = ["--bind"],

works as well. Both comply with buildifier.

@walkingeyerobot walkingeyerobot self-requested a review March 27, 2021 19:51
@@ -0,0 +1,2 @@
bazel-*
dist/
Copy link
Collaborator

Choose a reason for hiding this comment

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

missing newline

std::cout << "hello world!" << std::endl;
std::cout << "start main function" << std::endl;
sayHello();
std::cout << "end main function" << std::endl;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Any not include a main function in the program in both cases? This change seem to turn this example from the executable into a library. Any particular reason for doing that?

Don't we actually run the result of building this? (under node?) We probably should.

Copy link
Author

Choose a reason for hiding this comment

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

Both are fine. The example illustrates that we can use EMSCRIPTEN_BINDINGS optionally.

Copy link
Author

Choose a reason for hiding this comment

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

I updated it so it looks more like what you are looking for.

@@ -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",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is this not formatted on one line like srcs above? Is this the bazel style?

@walkingeyerobot
Copy link
Collaborator

Thanks for the PR! I think the changes to bazel/README.md are wonderful and should be merged.

The rest I'm more reluctant to accept. I don't think there's value in adding embind to the hello world example, or in removing the main function when building under emscripten. The addition of bazel/test_external/.bazelrc isn't necessary for the example, and it's already covered by bazel/bazelrc and explained in bazel/README.md. I'd also prefer having a single README instead of multiple in nested directories because there's not so much information that it needs to be split up, and it's easier to maintain with only one.

Again, thank you for your contribution. I'm happy to merge the changes to bazel/README.md if you'd like to revert the rest. I'm also happy to have further discussion concerning the other changes.

@jinfwhuang
Copy link
Author

jinfwhuang commented Mar 29, 2021

Sure. The key addition to the example is that it demonstrates how to pass opt flag to the compiler to bind c functions to javascript. These should be helpful addition to the example for a majority of the users.

I highly question that this would make this more difficult to maintain.

  • Without the .bazelrc, the command bazel build --config=wasm :hello-world would not work.

@jinfwhuang
Copy link
Author

Remove test_external/README.md

@@ -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.

@sbc100
Copy link
Collaborator

sbc100 commented Mar 30, 2021

Sure. The key addition to the example is that it demonstrates how to pass opt flag to the compiler to bind c functions to javascript. These should be helpful addition to the example for a majority of the users.

I highly question that this would make this more difficult to maintain.

  • Without the .bazelrc, the command bazel build --config=wasm :hello-world would not work.

What about keeping hello world as is and adding a new test (hello_embind or test_embind?) that adds this more complex stuff?

@jinfwhuang
Copy link
Author

jinfwhuang commented Mar 30, 2021

Sure. Removed all changes to the example.

@walkingeyerobot
Copy link
Collaborator

Sure. The key addition to the example is that it demonstrates how to pass opt flag to the compiler to bind c functions to javascript. These should be helpful addition to the example for a majority of the users.

Passing linkopts using bazel is not unique to this toolchain. This is something bazel documentation covers just fine. Similarly, using embind with and without this toolchain is the same, and emscripten's embind documentation covers how to use this.

I highly question that this would make this more difficult to maintain.

The more duplication of documentation there is, the more places that need to be updated when something changes, and the more likely it is that they become out of sync, confusing users. I would be in favor of linking to other documentation instead.

@jinfwhuang
Copy link
Author

Sure. The key addition to the example is that it demonstrates how to pass opt flag to the compiler to bind c functions to javascript. These should be helpful addition to the example for a majority of the users.

Passing linkopts using bazel is not unique to this toolchain. This is something bazel documentation covers just fine. Similarly, using embind with and without this toolchain is the same, and emscripten's embind documentation covers how to use this.

I highly question that this would make this more difficult to maintain.

The more duplication of documentation there is, the more places that need to be updated when something changes, and the more likely it is that they become out of sync, confusing users. I would be in favor of linking to other documentation instead.

@walkingeyerobot So you want to keep the example to be broken with respect to with the example hello-world to stay broken?

@jinfwhuang
Copy link
Author

jinfwhuang commented Mar 31, 2021

I already removed all changes to the example. The changes remained:

  1. Updated README b/c there was a typo
  2. Updated bazelrc file so that the example actually run.
  3. gitignore auto generated files when the example is run.

@walkingeyerobot
Copy link
Collaborator

Sure. The key addition to the example is that it demonstrates how to pass opt flag to the compiler to bind c functions to javascript. These should be helpful addition to the example for a majority of the users.

Passing linkopts using bazel is not unique to this toolchain. This is something bazel documentation covers just fine. Similarly, using embind with and without this toolchain is the same, and emscripten's embind documentation covers how to use this.

I highly question that this would make this more difficult to maintain.

The more duplication of documentation there is, the more places that need to be updated when something changes, and the more likely it is that they become out of sync, confusing users. I would be in favor of linking to other documentation instead.

@walkingeyerobot So you want to keep the example to be broken with respect to with the example hello-world to stay broken?

The example is not broken, but I would like to keep it the way it is.. The user can either build the wasm_cc_binary as-is, or they can follow the directions in the readme to build with --config=wasm.

@jinfwhuang
Copy link
Author

The issue under discussions are so minor. Please feel free to push your modifications to the PR as you see necessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants