emscripten_helpers.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import os, json
  2. from SCons.Util import WhereIs
  3. def run_closure_compiler(target, source, env, for_signature):
  4. closure_bin = os.path.join(os.path.dirname(WhereIs("emcc")), "node_modules", ".bin", "google-closure-compiler")
  5. cmd = [WhereIs("node"), closure_bin]
  6. cmd.extend(["--compilation_level", "ADVANCED_OPTIMIZATIONS"])
  7. for f in env["JSEXTERNS"]:
  8. cmd.extend(["--externs", f.get_abspath()])
  9. for f in source:
  10. cmd.extend(["--js", f.get_abspath()])
  11. cmd.extend(["--js_output_file", target[0].get_abspath()])
  12. return " ".join(cmd)
  13. def get_build_version():
  14. import version
  15. name = "custom_build"
  16. if os.getenv("BUILD_NAME") != None:
  17. name = os.getenv("BUILD_NAME")
  18. v = "%d.%d" % (version.major, version.minor)
  19. if version.patch > 0:
  20. v += ".%d" % version.patch
  21. status = version.status
  22. if os.getenv("GODOT_VERSION_STATUS") != None:
  23. status = str(os.getenv("GODOT_VERSION_STATUS"))
  24. v += ".%s.%s" % (status, name)
  25. return v
  26. def create_engine_file(env, target, source, externs):
  27. if env["use_closure_compiler"]:
  28. return env.BuildJS(target, source, JSEXTERNS=externs)
  29. return env.Textfile(target, [env.File(s) for s in source])
  30. def create_template_zip(env, js, wasm, extra):
  31. binary_name = "godot.tools" if env["tools"] else "godot"
  32. zip_dir = env.Dir("#bin/.javascript_zip")
  33. in_files = [
  34. js,
  35. wasm,
  36. "#platform/javascript/js/libs/audio.worklet.js",
  37. ]
  38. out_files = [
  39. zip_dir.File(binary_name + ".js"),
  40. zip_dir.File(binary_name + ".wasm"),
  41. zip_dir.File(binary_name + ".audio.worklet.js"),
  42. ]
  43. # GDNative/Threads specific
  44. if env["gdnative_enabled"]:
  45. in_files.append(extra.pop()) # Runtime
  46. out_files.append(zip_dir.File(binary_name + ".side.wasm"))
  47. if env["threads_enabled"]:
  48. in_files.append(extra.pop()) # Worker
  49. out_files.append(zip_dir.File(binary_name + ".worker.js"))
  50. service_worker = "#misc/dist/html/service-worker.js"
  51. if env["tools"]:
  52. # HTML
  53. html = "#misc/dist/html/editor.html"
  54. cache = [
  55. "godot.tools.html",
  56. "offline.html",
  57. "godot.tools.js",
  58. "godot.tools.worker.js",
  59. "godot.tools.audio.worklet.js",
  60. "logo.svg",
  61. "favicon.png",
  62. ]
  63. opt_cache = ["godot.tools.wasm"]
  64. subst_dict = {
  65. "@GODOT_VERSION@": get_build_version(),
  66. "@GODOT_NAME@": "GodotEngine",
  67. "@GODOT_CACHE@": json.dumps(cache),
  68. "@GODOT_OPT_CACHE@": json.dumps(opt_cache),
  69. "@GODOT_OFFLINE_PAGE@": "offline.html",
  70. }
  71. html = env.Substfile(target="#bin/godot${PROGSUFFIX}.html", source=html, SUBST_DICT=subst_dict)
  72. in_files.append(html)
  73. out_files.append(zip_dir.File(binary_name + ".html"))
  74. # And logo/favicon
  75. in_files.append("#misc/dist/html/logo.svg")
  76. out_files.append(zip_dir.File("logo.svg"))
  77. in_files.append("#icon.png")
  78. out_files.append(zip_dir.File("favicon.png"))
  79. # PWA
  80. service_worker = env.Substfile(
  81. target="#bin/godot${PROGSUFFIX}.service.worker.js", source=service_worker, SUBST_DICT=subst_dict
  82. )
  83. in_files.append(service_worker)
  84. out_files.append(zip_dir.File("service.worker.js"))
  85. in_files.append("#misc/dist/html/manifest.json")
  86. out_files.append(zip_dir.File("manifest.json"))
  87. in_files.append("#misc/dist/html/offline.html")
  88. out_files.append(zip_dir.File("offline.html"))
  89. else:
  90. # HTML
  91. in_files.append("#misc/dist/html/full-size.html")
  92. out_files.append(zip_dir.File(binary_name + ".html"))
  93. in_files.append(service_worker)
  94. out_files.append(zip_dir.File(binary_name + ".service.worker.js"))
  95. in_files.append("#misc/dist/html/offline-export.html")
  96. out_files.append(zip_dir.File("godot.offline.html"))
  97. zip_files = env.InstallAs(out_files, in_files)
  98. env.Zip(
  99. "#bin/godot",
  100. zip_files,
  101. ZIPROOT=zip_dir,
  102. ZIPSUFFIX="${PROGSUFFIX}${ZIPSUFFIX}",
  103. ZIPCOMSTR="Archiving $SOURCES as $TARGET",
  104. )
  105. def add_js_libraries(env, libraries):
  106. env.Append(JS_LIBS=env.File(libraries))
  107. def add_js_pre(env, js_pre):
  108. env.Append(JS_PRE=env.File(js_pre))
  109. def add_js_externs(env, externs):
  110. env.Append(JS_EXTERNS=env.File(externs))