compiling_for_web.rst 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. .. _doc_compiling_for_web:
  2. Compiling for the Web
  3. =====================
  4. .. highlight:: shell
  5. Requirements
  6. ------------
  7. To compile export templates for the Web, the following is required:
  8. - `Emscripten 1.37.9+ <http://kripken.github.io/emscripten-site>`__: If the version available
  9. per package manager is not recent enough, the best alternative is to install
  10. using the `Emscripten SDK <http://kripken.github.io/emscripten-site/docs/getting_started/downloads.html>`__
  11. - `Python 2.7+ or Python 3.5+ <https://www.python.org/>`__
  12. - `SCons <https://www.scons.org>`__ build system
  13. .. seealso:: For a general overview of SCons usage for Godot, see
  14. :ref:`doc_introduction_to_the_buildsystem`.
  15. Building export templates
  16. -------------------------
  17. Before starting, confirm that the Emscripten configuration file exists and
  18. specifies all settings correctly. This file is available as ``~/.emscripten``
  19. on UNIX-like systems and ``%USERPROFILE%\.emscripten`` on Windows. It's usually
  20. written by the Emscripten SDK, e.g. when invoking ``emsdk activate latest``,
  21. or by your package manager. It's also created when starting Emscripten's
  22. ``emcc`` program if the file doesn't exist.
  23. Open a terminal and navigate to the root directory of the engine source code.
  24. Then instruct SCons to build the JavaScript platform. Specify ``target`` as
  25. either ``release`` for a release build or ``release_debug`` for a debug build::
  26. scons platform=javascript tools=no target=release
  27. scons platform=javascript tools=no target=release_debug
  28. By default, the :ref:`JavaScript singleton <doc_javascript_eval>` will be built
  29. into the engine. Since ``eval()`` calls can be a security concern, the
  30. ``javascript_eval`` option can be used to build without the singleton::
  31. scons platform=javascript tools=no target=release javascript_eval=no
  32. scons platform=javascript tools=no target=release_debug javascript_eval=no
  33. The engine will now be compiled to WebAssembly by Emscripten. Once finished,
  34. the resulting file will be placed in the ``bin`` subdirectory. Its name is
  35. ``godot.javascript.opt.zip`` for release or ``godot.javascript.opt.debug.zip``
  36. for debug.
  37. Finally, rename the zip archive to ``webassembly_release.zip`` for the
  38. release template::
  39. mv bin/godot.javascript.opt.zip bin/webassembly_release.zip
  40. And ``webassembly_debug.zip`` for the debug template::
  41. mv bin/godot.javascript.opt.debug.zip bin/webassembly_debug.zip
  42. Building per asm.js translation or LLVM backend
  43. -----------------------------------------------
  44. WebAssembly can be compiled in two ways: The default is to first compile to
  45. asm.js, a highly optimizable subset of JavaScript, using Emscripten's
  46. *fastcomp* fork of LLVM. This code is then translated to WebAssembly using a
  47. tool called ``asm2wasm``. Emscripten automatically takes care of both
  48. processes, we simply run SCons.
  49. The other method uses LLVM's WebAssembly backend. This backend is not yet
  50. available in release versions of LLVM, only in development builds built with
  51. ``LLVM_EXPERIMENTAL_TARGETS_TO_BUILD=WebAssembly``.
  52. Compiling with this backend outputs files in LLVM's ``.s`` format, which is
  53. translated into actual WebAssembly using a tool called ``s2wasm``.
  54. Emscripten manages these processes as well, so we just invoke SCons.
  55. In order to choose one of the two methods, the ``LLVM_ROOT`` variable in the
  56. Emscripten configuration file is used. If it points to a directory containing
  57. binaries of Emscripten's *fastcomp* fork of clang, ``asm2wasm`` is used.
  58. This is the default in a normal Emscripten installation. Otherwise,
  59. LLVM binaries built with the WebAssembly backend will be expected and
  60. ``s2wasm`` is used. On Windows, make sure to escape backslashes of paths within
  61. this file as double backslashes ``\\`` or use Unix-style paths with a single
  62. forward slash ``/``.