Skip to content

Commit

Permalink
Backport Wasmer update
Browse files Browse the repository at this point in the history
Update default Wasmer version
Static Wasm C API flag
Simpler DLL patch
  • Loading branch information
ashtonmeuser committed Sep 14, 2023
1 parent e6b3630 commit 0eb64de
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .github/actions/godot-cpp/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ runs:
shell: bash
run: |
cd godot-cpp
scons platform=${{ inputs.platform }} target=${{ inputs.target }} generate_bindings=yes ${{ inputs.scons-flags }}
scons platform=${{ inputs.platform }} target=${{ inputs.target }} ${{ inputs.scons-flags }}
2 changes: 1 addition & 1 deletion .github/workflows/addon.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ env:
TARGET: release
GODOT_REF: 3.5.2-stable
LIBRARY_PATH: addons/godot-wasm/bin
WASMER_VERSION: v3.1.1

jobs:
build-addon:
Expand Down Expand Up @@ -40,6 +39,7 @@ jobs:
with:
platform: ${{ matrix.platform }}
target: ${{ env.TARGET }}
scons-flags: generate_bindings=yes

- name: Build Addon
uses: ./.github/actions/scons-build
Expand Down
7 changes: 4 additions & 3 deletions SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,20 @@ elif env["platform"] == "linux":
env.Prepend(CXXFLAGS=["-std=gnu++14"])
env.Append(CCFLAGS=["-fPIC", "-g", "-O3"])
elif env["platform"] == "windows":
env.Prepend(CCFLAGS=["/std:c++14", "-W3", "-GR", "-O2", "-EHsc", "-MD"])
env.Prepend(CCFLAGS=["/std:c++14", "-W3", "-GR", "-O2", "-EHsc"])
env.Append(ENV=os.environ) # Keep session env variables to support VS 2017 prompt
env.Append(CPPDEFINES=["WIN32", "_WIN32", "_WINDOWS", "_CRT_SECURE_NO_WARNINGS", "NDEBUG"])
if env.get("use_mingw"): # MinGW
env["LIBRUNTIMESUFFIX"] = ".a"
env.Append(LIBS=["userenv"])
else: # MSVC
env["LIBRUNTIMESUFFIX"] = ".lib"
env.Append(CCFLAGS=["-MD"]) # Dynamic CRT used by Wasmer >= v3.2.0
# Force Windows SDK library suffix (see https://github.com/godotengine/godot/issues/23687)
env.Append(LINKFLAGS=["bcrypt.lib", "userenv.lib", "ws2_32.lib", "advapi32.lib"])
env.Append(LINKFLAGS=["bcrypt.lib", "userenv.lib", "ws2_32.lib", "advapi32.lib", "ntdll.lib"])

# Defines for GDNative specific API
env.Append(CPPDEFINES=["GDNATIVE"])
env.Append(CPPDEFINES=["GDNATIVE", "LIBWASM_STATIC"])

# Explicit static libraries
cpp_lib = env.File("godot-cpp/bin/libgodot-cpp.{}.{}.64{}".format(env["platform"], env["target"], env["LIBSUFFIX"]))
Expand Down
2 changes: 1 addition & 1 deletion SCsub
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ env.Append(LIBS=[runtime_lib])
module_env.Append(CPPPATH=[env.Dir("{}/include".format(module_env["wasm_runtime"])).abspath])

# Defines for module agnosticism
module_env.Append(CPPDEFINES=["GODOT_MODULE"])
module_env.Append(CPPDEFINES=["GODOT_MODULE", "LIBWASM_STATIC"])

# Module sources
module_env.add_source_files(
Expand Down
31 changes: 18 additions & 13 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import tarfile

WASMER_BASE_URL = "https://github.com/wasmerio/wasmer/releases/download/{0}/wasmer-{1}.tar.gz"
WASMER_VER_DEFAULT = "v3.1.1"
WASMER_VER_DEFAULT = "v4.2.0"
WASMTIME_BASE_URL = "https://github.com/bytecodealliance/wasmtime/releases/download/{0}/wasmtime-{0}-{1}-c-api.tar.xz"
WASMTIME_VER_DEFAULT = "v12.0.1"

Expand All @@ -19,16 +19,17 @@ def _validate_version(v):
def _strip_tar_members(f, s=""):
"""Optionally strip tarfile top level directory"""
for member in f.getmembers():
if re.fullmatch(s, member.path): continue # Top level dir
elif re.match(s + r"\/", member.path): # Nested file
member.path = member.path.split('/', 1)[1]
if re.fullmatch(s, member.path):
continue # Top level dir
elif re.match(s + r"\/", member.path): # Nested file
member.path = member.path.split("/", 1)[1]
yield member


def _download_tarfile(url, dest, rename={}):
"""Download and extract tarfile removing redundant top level dir"""
strip = r"^{}[\w\-.]*".format(dest) # Dir of same name as destination
filename = "tmp.tar.gz" # Temporary tarfile name
strip = r"^{}[\w\-.]*".format(dest) # Dir of same name as destination
filename = "tmp.tar.gz" # Temporary tarfile name
os.makedirs(dest, exist_ok=True)
request.urlretrieve(url, filename)
with tarfile.open(filename) as file:
Expand All @@ -38,12 +39,16 @@ def _download_tarfile(url, dest, rename={}):
os.remove(filename)


def _safe_apply_patch(patch):
"""Apply diff patch with shell tool or git"""
if shutil.which("patch") is not None:
os.system("patch -p1 < {}".format(patch))
else:
os.system("git apply {}".format(patch))
def _patch_dll_import():
"""Patch old Wasm C API header (see https://github.com/WebAssembly/wasm-c-api/pull/183)"""
for path in ["wasmer/include/wasm.h", "wasmtime/include/wasm.h"]:
if not os.path.isfile(path):
continue
with open(path, "r") as file:
content = file.read()
content = content.replace("__declspec(dllimport)", "")
with open(path, "w") as file:
file.write(content)


def download_wasmer(env, force=False, version=WASMER_VER_DEFAULT):
Expand Down Expand Up @@ -75,7 +80,7 @@ def download_wasmer(env, force=False, version=WASMER_VER_DEFAULT):
# Temporary workaround for Wasm C API and Wasmer issue
# See https://github.com/ashtonmeuser/godot-wasm/issues/26
# See https://github.com/ashtonmeuser/godot-wasm/issues/29
_safe_apply_patch("wasm-windows.patch")
_patch_dll_import()


def download_wasmtime(env, force=False, version=WASMTIME_VER_DEFAULT):
Expand Down
26 changes: 0 additions & 26 deletions wasm-windows.patch

This file was deleted.

0 comments on commit 0eb64de

Please sign in to comment.