Skip to content

Commit

Permalink
Download Wasmer library for module
Browse files Browse the repository at this point in the history
Use .a library suffix for Windows MinGW build
  • Loading branch information
ashtonmeuser committed May 4, 2023
1 parent 6a4ca70 commit e6bb0ee
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 36 deletions.
40 changes: 5 additions & 35 deletions SConstruct
Original file line number Diff line number Diff line change
@@ -1,54 +1,24 @@
#!python
import os
import shutil
import re
from urllib import request
import tarfile
from utils import download_wasmer, VERSION_DEFAULT

# Initial options inheriting from CLI args
opts = Variables([], ARGUMENTS)

# Define options
opts.Add(BoolVariable('download_wasmer', 'Download Wasmer library', 'no'))
opts.Add('wasmer_version', 'Wasmer library version', 'v3.1.1')
opts.Add('wasmer_version', 'Wasmer library version', VERSION_DEFAULT)

# SConstruct environment from Godot CPP
env = SConscript("godot-cpp/SConstruct")
opts.Update(env)

# Wasmer download
def download_wasmer(env):
def download_tarfile(url, dest, rename={}):
filename = 'tmp.tar.gz'
os.makedirs(dest, exist_ok=True)
request.urlretrieve(url, filename)
file = tarfile.open(filename)
file.extractall(dest)
file.close()
for k, v in rename.items(): os.rename(k, v)
os.remove(filename)
base_url = 'https://github.com/wasmerio/wasmer/releases/download/{}/wasmer-{}.tar.gz'
if env['platform'] in ['osx', 'macos']:
# For macOS, we need to universalize the AMD and ARM libraries
download_tarfile(base_url.format(env['wasmer_version'], 'darwin-amd64'), 'wasmer', {'wasmer/lib/libwasmer.a': 'wasmer/lib/libwasmer.amd64.a'})
download_tarfile(base_url.format(env['wasmer_version'], 'darwin-arm64'), 'wasmer', {'wasmer/lib/libwasmer.a': 'wasmer/lib/libwasmer.arm64.a'})
os.system('lipo wasmer/lib/libwasmer.*64.a -output wasmer/lib/libwasmer.a -create')
elif env['platform'] == 'linux':
download_tarfile(base_url.format(env['wasmer_version'], 'linux-amd64'), 'wasmer')
elif env['platform'] == 'windows':
download_tarfile(base_url.format(env['wasmer_version'], 'windows-amd64'), 'wasmer')
# Download Wasmer if required
download_wasmer(env, env['download_wasmer'], env['wasmer_version'])

# Process some arguments
if not re.fullmatch(r'v\d+\.\d+\.\d+(-.+)?', env['wasmer_version']):
exit('Invalid Wasmer version')

if env['download_wasmer'] or not os.path.isdir('wasmer'):
print('Downloading Wasmer {}'.format(env['wasmer_version']))
shutil.rmtree('wasmer', True)
download_wasmer(env)

if env['platform'] == 'windows':
env['LIBWASMERSUFFIX'] = '.dll.lib' # Requires special suffix
env['LIBWASMERSUFFIX'] = '.a' if env.get('use_mingw') else '.dll.lib'
env.Append(LIBS=['bcrypt', 'userenv', 'ws2_32', 'advapi32'])

# Defines for GDExtension specific API
Expand Down
7 changes: 6 additions & 1 deletion SCsub
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
from utils import download_wasmer

# Import env and create module-specific clone
Import('env')
module_env = env.Clone()

# Download Wasmer if required
download_wasmer(env)

# Check platform specifics
if env['platform'] in ['linux', 'linuxbsd']:
module_env['LIBWASMERSUFFIX'] = '.a'
Expand All @@ -11,7 +16,7 @@ elif env['platform'] in ['osx', 'macos']:
module_env.Append(CXXFLAGS=['-std=c++17'])
env.Append(LINKFLAGS=['-framework', 'Security'])
elif env['platform'] == 'windows':
module_env['LIBWASMERSUFFIX'] = '.dll.lib'
module_env['LIBWASMERSUFFIX'] = '.a' if env.get('use_mingw') else '.dll.lib'

# Explicit static libraries
wasmer_library = env.File('wasmer/lib/{}wasmer{}'.format(env['LIBPREFIX'], module_env.get('LIBWASMERSUFFIX', module_env['LIBSUFFIX'])))
Expand Down
37 changes: 37 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import os
import re
import shutil
from urllib import request
import tarfile

BASE_URL = 'https://github.com/wasmerio/wasmer/releases/download/{}/wasmer-{}.tar.gz'
VERSION_DEFAULT = 'v3.1.1'

def validate_version(v):
if not re.fullmatch(r'v\d+\.\d+\.\d+(-.+)?', v):
raise(ValueError('Invalid Wasmer version'))

def download_tarfile(url, dest, rename={}):
filename = 'tmp.tar.gz'
os.makedirs(dest, exist_ok=True)
request.urlretrieve(url, filename)
file = tarfile.open(filename)
file.extractall(dest)
file.close()
for k, v in rename.items(): os.rename(k, v)
os.remove(filename)

def download_wasmer(env, force=False, version=VERSION_DEFAULT):
validate_version(version)
if not force and os.path.isdir('wasmer'): return # Skip download
print('Downloading Wasmer library {}'.format(version))
shutil.rmtree('wasmer', True) # Remove old library
if env['platform'] in ['osx', 'macos']:
# For macOS, we need to universalize the AMD and ARM libraries
download_tarfile(BASE_URL.format(version, 'darwin-amd64'), 'wasmer', {'wasmer/lib/libwasmer.a': 'wasmer/lib/libwasmer.amd64.a'})
download_tarfile(BASE_URL.format(version, 'darwin-arm64'), 'wasmer', {'wasmer/lib/libwasmer.a': 'wasmer/lib/libwasmer.arm64.a'})
os.system('lipo wasmer/lib/libwasmer.*64.a -output wasmer/lib/libwasmer.a -create')
elif env['platform'] in ['linux', 'linuxbsd']:
download_tarfile(BASE_URL.format(version, 'linux-amd64'), 'wasmer')
elif env['platform'] == 'windows':
download_tarfile(BASE_URL.format(version, 'windows-amd64'), 'wasmer')

0 comments on commit e6bb0ee

Please sign in to comment.