compiling_for_web.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 SDK <http://emscripten.org/>`__ (Install in a path without
  9. spaces, i.e. not on "Program Files")
  10. - `Python 2.7+ <https://www.python.org/>`__ (3.0 is
  11. untested as of now)
  12. - `SCons <http://www.scons.org>`__ build system
  13. Compiling
  14. ---------
  15. Start a terminal and set the environment variable ``EMSCRIPTEN_ROOT`` to the
  16. installation directory of Emscripten::
  17. export EMSCRIPTEN_ROOT=~/emsdk/emscripten/master
  18. If you are on Windows, start a regular prompt or the Emscripten Command Prompt.
  19. Do **not** use the Developer Command Prompt nor any of the ones that come with
  20. Visual Studio. You can set the environment variable in the system settings or
  21. in the prompt itself::
  22. set EMSCRIPTEN_ROOT=C:\emsdk\emscripten\master
  23. Now go to the root directory of the engine source code and instruct SCons to
  24. compile for JavaScript. Specify ``target`` as either ``release`` for a release
  25. 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. The engine will now be compiled to JavaScript by Emscripten. If all goes well,
  29. the resulting file will be placed in the ``bin`` subdirectory. Its name is
  30. ``godot.javascript.opt.asm.js`` for release or
  31. ``godot.javascript.opt.debug.asm.js`` for debug. Additionally, three files of
  32. the same name but with the extensions ``.html``, ``.html.mem`` and ``.js`` will
  33. be generated.
  34. Building export templates
  35. -------------------------
  36. After compiling, further steps are required to build the template.
  37. The actual web export template has the form of a zip file containing at least
  38. these 5 files:
  39. 1. ``godot.asm.js`` — This is the file that was just compiled, but under a
  40. different name.
  41. For the release template::
  42. cp bin/godot.javascript.opt.asm.js godot.asm.js
  43. For the debug template::
  44. cp bin/godot.javascript.opt.debug.asm.js godot.asm.js
  45. 2. ``godot.js``
  46. 3. ``godot.mem``
  47. 4. ``godot.html`` — other files created during compilation, initially with the
  48. same name as the ``.asm.js`` file, except ``.asm.js`` is replaced by
  49. ``.js`` for ``godot.js``, ``.html`` for ``godot.html`` and ``.html.mem`` for
  50. ``godot.mem``.
  51. For the release template::
  52. cp bin/godot.javascript.opt.js godot.js
  53. cp bin/godot.javascript.opt.html godot.html
  54. cp bin/godot.javascript.opt.html.mem godot.mem
  55. For the debug template::
  56. cp bin/godot.javascript.opt.debug.js godot.js
  57. cp bin/godot.javascript.opt.debug.html godot.html
  58. cp bin/godot.javascript.opt.debug.html.mem godot.mem
  59. 5. ``godotfs.js`` — This file is located within the Godot Engine repository,
  60. under ``/tools/dist/html_fs/``::
  61. cp tools/dist/html_fs/godotfs.js .
  62. Once these 5 files are assembled, zip them up and your export template is ready
  63. to go. The correct name for the template file is ``javascript_release.zip`` for
  64. the release template::
  65. zip javascript_release.zip godot.asm.js godot.js godot.mem godotfs.js godot.html
  66. And ``javascript_debug.zip`` for the debug template::
  67. zip javascript_debug.zip godot.asm.js godot.js godot.mem godotfs.js godot.html
  68. The resulting files must be placed in the ``templates`` directory in your Godot
  69. user directory::
  70. mv javascript_release.zip ~/.godot/templates
  71. mv javascript_debug.zip ~/.godot/templates
  72. If you are writing custom modules or using custom C++ code, you may want to
  73. configure your zip files as custom export templates. This can be done in the
  74. export GUI, using the "Custom Package" option.
  75. There's no need to copy the templates in this case — you can simply reference
  76. the resulting files in your Godot source folder, so the next time you build,
  77. the custom templates will already be referenced.
  78. Compiling to WebAssembly
  79. -------------------------
  80. The current default for exporting to the web is to compile to *asm.js*, a
  81. highly optimizable subset of JavaScript.
  82. It is also possible to compile to the experimental *WebAssembly* format, which
  83. should eventually offer better performance and loading times. Its specification
  84. is still in flux and compile tools may sporadically fail to build Godot.
  85. Running a game per WebAssembly requires nightly browser builds with special
  86. flags set. As such, WebAssembly builds are currently not suitable for
  87. publishing.
  88. Compiling to WebAssembly requires using the `incoming branch of Emscripten <http://kripken.github.io/emscripten-site/docs/building_from_source/building_emscripten_from_source_using_the_sdk.html#building-emscripten-from-the-main-repositories>`_.
  89. WebAssembly can be compiled in two ways: The default way is to first
  90. compile to asm.js similarly to the default method, then translate to
  91. WebAssembly using a tool called ``asm2wasm``. Emscripten automatically takes
  92. care of both processes, we simply run SCons.
  93. The other method uses LLVM's WebAssembly backend, which should eventually
  94. produce more performant binaries. To build LLVM with this backend, set the
  95. CMake variable ``LLVM_EXPERIMENTAL_TARGETS_TO_BUILD`` to ``WebAssembly`` when
  96. building LLVM.
  97. Compiling with this backend outputs files in LLVM's ``.s`` format, which is
  98. translated to actual WebAssembly using a tool called ``s2wasm``. Emscripten
  99. manages these processes as well, so we just invoke SCons.
  100. In order to choose one of the two methods, the ``LLVM_ROOT`` variable in the
  101. Emscripten configuration file ``~/.emscripten`` is set. If it points to a
  102. directory containing binaries of Emscripten's *fastcomp* fork of clang,
  103. ``asm2wasm`` is used. This is the default in a normal Emscripten installation.
  104. Otherwise, LLVM binaries built with the WebAssembly backend will be expected
  105. and ``s2wasm`` is used.
  106. With ``LLVM_ROOT`` set up correctly, compiling to WebAssembly is as easy as
  107. adding ``wasm=yes`` to the SCons arguments::
  108. scons platform=javascript target=release wasm=yes
  109. scons platform=javascript target=release_debug wasm=yes
  110. These commands will build WebAssembly binaries in either release or debug mode.
  111. The generated files' names contain ``.webassembly`` as an additional file
  112. suffix before the extension.
  113. In order to build the actual WebAssembly export templates, the WebAssembly
  114. binary file with the ``.wasm`` extension is added to the archive as
  115. ``godot.wasm`` in place of ``godot.asm.js`` alongside the usual files.
  116. For the release template::
  117. cp bin/godot.javascript.opt.webassembly.wasm godot.wasm
  118. For the debug template::
  119. cp bin/godot.javascript.opt.debug.webassembly.wasm godot.wasm
  120. WebAssembly builds do not use a memory initializer file, so do not add a
  121. ``godot.mem`` file to the archive — there is none.
  122. The WebAssembly export templates simply replace the previous asm.js-based web
  123. export templates with the names ``javascript_release.zip`` and
  124. ``javascript_debug.zip``
  125. Customizing the HTML page
  126. -------------------------
  127. Rather than the default HTML file generated when compiling, it is
  128. also possible to use a custom HTML page. This allows drastic customization of
  129. the final web presentation.
  130. This can be done in two ways. The first is to replace the
  131. ``platform/javascript/godot_shell.html`` file. In this case, the HTML file is
  132. used at build time, allowing Emscripten so substitute the ``{{{ SCRIPT }}}``
  133. placeholder by a ``<script>>`` element containing the loader code. This makes
  134. the HTML file usable for both asm.js and WebAssembly templates, since they use
  135. different loading code.
  136. The other method is to simply replace the ``godot.html`` file within the
  137. complete export templates. This method does not require building the engine.
  138. However, in this case, no ``{{{ SCRIPT }}}`` placeholder should be used in the
  139. HTML file, since it would never be replaced — the loader code for either asm.js
  140. or WebAssembly must already be included in the file.
  141. In the HTML page, the JavaScript object ``Module`` is the page's interface to
  142. Emscripten. Check the official documentation for information on how to use it:
  143. https://kripken.github.io/emscripten-site/docs/api_reference/module.html
  144. The default HTML page offers an example to start off with, separating the
  145. Emscripten interface logic in the JavaScript ``Module`` object from the page
  146. logic in the ``Presentation`` object. Emscripten's default ``shell.html`` file
  147. is another example, but does not use Godot's placeholders, listed below.
  148. When exporting a game, several placeholders in the ``godot.html`` file are
  149. substituted by values dependent on the export:
  150. +------------------------------+-----------------------------------------------+
  151. | Placeholder | substituted by |
  152. +==============================+===============================================+
  153. | ``$GODOT_BASE`` | Basename of files referenced within the page, |
  154. | | without file extension or other suffixes |
  155. +------------------------------+-----------------------------------------------+
  156. | ``$GODOT_CANVAS_WIDTH`` | Integer specifying the initial display width |
  157. | | of the game |
  158. +------------------------------+-----------------------------------------------+
  159. | ``$GODOT_CANVAS_HEIGHT`` | Integer specifying the initial display height |
  160. | | of the game |
  161. +------------------------------+-----------------------------------------------+
  162. | ``$GODOT_DEBUG_ENABLED`` | String ``true`` if debugging, ``false`` |
  163. | | otherwise |
  164. +------------------------------+-----------------------------------------------+
  165. | ``$GODOT_CONTROLS_ENABLED`` | String ``true`` if ``html/controls_enabled`` |
  166. | | is enabled, ``false`` otherwise |
  167. +------------------------------+-----------------------------------------------+
  168. | ``$GODOT_HEAD_TITLE`` | Title of the page, normally used as content |
  169. | | of the HTML ``<title>`` element |
  170. +------------------------------+-----------------------------------------------+
  171. | ``$GODOT_HEAD_INCLUDE`` | Custom string to include just before the end |
  172. | | of the HTML ``<head>`` element |
  173. +------------------------------+-----------------------------------------------+
  174. | ``$GODOT_STYLE_FONT_FAMILY`` | CSS format ``font-family`` to use, without |
  175. | | terminating semicolon |
  176. +------------------------------+-----------------------------------------------+
  177. | ``$GODOT_STYLE_INCLUDE`` | Custom string to include just before the end |
  178. | | of the page's CSS |
  179. +------------------------------+-----------------------------------------------+
  180. | ``{{{ SCRIPT }}}`` | ``<script>`` that loads the engine, |
  181. | | substituted only when building, not on export |
  182. +------------------------------+-----------------------------------------------+
  183. The first three of the placeholders listed should always be implemented in the
  184. HTML page, since they are important for the correct presentation of the game.
  185. The last placeholder is important when rewriting the ``godot_shell.html`` file
  186. and is only substituted during build time, not during export time.
  187. The other placeholders are optional.