Giter Site home page Giter Site logo

kuhlmannmarkus / bazel-emscripten Goto Github PK

View Code? Open in Web Editor NEW

This project forked from s0l0ist/bazel-emscripten

0.0 1.0 0.0 61 KB

C++ to WASM or JS using Bazel and Emscripten

License: MIT License

Starlark 57.02% C++ 8.96% JavaScript 16.76% Shell 12.64% HTML 4.62%

bazel-emscripten's Introduction

Hello World (C++ to WASM/JS) using Bazel with Emscritpen

This repository demonstrates compiling C++ code with Bazel to either WebAssembly or plain JavaScript using the lastest Emscripten release.

Specifically, it illustrates how to integrate multiple cc_library targets from different packages to compile a JavaScript file containing WASM or just pure JS.

This approach uses a git submodule for emscripten and configures it as an external resource for bazel to use. Other methods such as using http_resource or git_repository are possible, but are not as easily configured. PRs welcome!

The generated outputs have been configured in 'single file' mode. This means instead of handling a separate .wasm file, the wasm binary is encoded as a base64 string and is embedded into a JS file along with emscripten's glue code. The result is a single file that works in almost any environment provided it supports WebAssembly (the JS variant will always work) without the need to host, serve, or initialize a .wasm binary directly. The caveat is that the file size is 33% larger due to the encoding.

Requirements

This project assumes you have bazel

Code structure

Compiling

First, clone the repository

git clone https://github.com/s0l0ist/bazel-emscripten.git
npm run submodule:update

// or

git clone --recurse-submodules https://github.com/s0l0ist/bazel-emscripten.git

Second, you need to set up emscripten using the emsdk:

// Updates emscripten tags
npm run em:update

// Install the latest emscripten release and activate it
npm run em:init

Now, you can simply run a script to build both the JS and the WASM outputs:

npm run build

To build only one variant:

npm run build:js
// or
npm run build:wasm

To bundle:

npm run rollup

To clean the repository:

npm run clean

Example

After you have built and bundled the code, you can choose to run a variant:

  • npm run demo:js to run the JS variant
  • npm run demo:wasm to run the WASM variant
npm run demo:wasm

> RUN_DEMO=wasm node ./hello-world/javascript/examples/demo.js

Fri May 22 10:59:15 2020
Hello, World!

bazel-emscripten's People

Contributors

s0l0ist avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.