compiling_for_web.rst 3.6 KB

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