Support compiling demos in wasm build #17

Open
mmcomando wants to merge 6 commits from wasm_demos into master
11 changed files with 156 additions and 43 deletions

1
.gitignore vendored
View file

@ -9,6 +9,7 @@
!skeleton.html !skeleton.html
!**/meson.build !**/meson.build
!**/*.wrap !**/*.wrap
!**/*.ini
!meson_options.txt !meson_options.txt
!compile_wasm.py !compile_wasm.py
!compile_android.py !compile_android.py

View file

@ -1,4 +1,4 @@
module mmutils.thread_pool; module mmutils.thread_pool;
import std.algorithm : map; import std.algorithm : map;
@ -11,7 +11,6 @@ version (WebAssembly)
version = MM_USE_POSIX_THREADS; version = MM_USE_POSIX_THREADS;
extern(C) struct FILE extern(C) struct FILE
{ {
} }
} }
else else

View file

@ -8,28 +8,73 @@ demos_inc = include_directories('source/')
external_inc = include_directories('external/sources/') external_inc = include_directories('external/sources/')
# Argumesnts # Argumesnts
not_wasm = host_machine.cpu_family() != 'wasm32'
versions = ['BindSDL_Image','SDL_208', 'BindBC_Static', 'BindSDL_Static'] versions = ['BindSDL_Image','SDL_208', 'BindBC_Static', 'BindSDL_Static']
# Dependencies # Dependencies
bindbc_loader_dep = dependency('bindbc-loader') bindbc_loader_dep = dependency('bindbc-loader')
bindbc_sdl_dep = dependency('bindbc-sdl') bindbc_sdl_dep = dependency('bindbc-sdl')
cimgui_dep = dependency('cimgui') cimgui_dep = dependency('cimgui')
sdl2_dep = dependency('SDL2') sdl2_dep = dependency('SDL2', required: not_wasm) # On wasm delivered by Emscripten
sdl2_image_dep = dependency('SDL2_image') sdl2_image_dep = dependency('SDL2_image', required: not_wasm)
subdir('utils') # Utils library subdir('utils') # Utils library
executable('decs-demos', [demos_src, external_src], deps = [
include_directories : [demos_inc, external_inc], bindbc_loader_dep,
d_module_versions : versions, bindbc_sdl_dep,
link_with : [ecs_lib, ecs_utils_lib], cimgui_dep,
dependencies : [ decs_dep,
bindbc_loader_dep, ecs_utils_dep,
bindbc_sdl_dep, sdl2_dep,
cimgui_dep, sdl2_image_dep,
decs_dep, ]
ecs_utils_dep,
sdl2_dep, if not_wasm
sdl2_image_dep, exe = executable('decs-demos', [demos_src, external_src],
], include_directories : [demos_inc, external_inc],
) d_module_versions : versions,
link_with : [ecs_lib, ecs_utils_lib],
dependencies : deps,
)
test('basic-tests', exe)
else
versions += ['MM_USE_POSIX_THREADS', 'ECSEmscripten']
decs_demos_lib = library('decs-demos', [demos_src, external_src],
include_directories : [demos_inc, external_inc],
d_module_versions : versions,
dependencies : deps,
)
cimgui_lib = subproject('cimgui').get_variable('lib')
sdl2_lib = subproject('bindbc-sdl').get_variable('lib')
file_packager = find_program('file_packager')
# Pack assets
assets_path = meson.source_root() / 'demos/assets@assets'
packed_assets = custom_target('packed-assets-web',
command: [file_packager, 'demos/assets.data', '--preload', assets_path, '--js-output=@OUTPUT@'],
output: ['assets.js'],
build_by_default: true,
)
# build single-threaded demos with HTML output
wasm_web = custom_target('decs-demos-wasm-web',
command: [emcc, args_wasm, '--pre-js', packed_assets, '-o', '@OUTPUT@', '@INPUT@'],
input: [sdl2_lib, ecs_lib, decs_demos_lib, ecs_utils_lib, cimgui_lib],
output: ['ecs_demo.html'],
build_by_default: true,
)
# build multi-threded demos with JS output. Custom shellfile detects if web browser threads are enabled and select proper JS and WASM.
wasm_web_mt = custom_target('decs-demos-wasm-web-mt',
command: [emcc, args_wasm, '-s', 'USE_PTHREADS=1', '--pre-js', packed_assets, '-o', '@OUTPUT@', '@INPUT@'],
input: [sdl2_lib, ecs_lib, decs_demos_lib, ecs_utils_lib, cimgui_lib],
output: ['ecs_demo_mt.js'],
build_by_default: true,
)
summary('decs-demoswasm-index', wasm_web.full_path())
endif

View file

@ -52,12 +52,22 @@ endif
add_global_arguments(args, language : 'd') add_global_arguments(args, language : 'd')
add_global_link_arguments(link_args, language : 'd') add_global_link_arguments(link_args, language : 'd')
versions = []
if host_machine.cpu_family() == 'wasm32'
add_global_arguments('--output-bc', language : 'd') # Adding it in cross files breaks linker detection
emcc = find_program('emcc')
args_wasm = ['-O3', '-s', 'FORCE_FILESYSTEM=1', '-s', 'USE_SDL=2', '-s', 'USE_SDL_IMAGE=2', '-s', 'SDL2_IMAGE_FORMATS=["png"]', '-s', 'ERROR_ON_UNDEFINED_SYMBOLS=0', '-s', 'ALLOW_MEMORY_GROWTH=1', '-s', 'WASM_MEM_MAX=1024MB', '-s', 'MALLOC=dlmalloc', '-s', 'WASM=1', '--shell-file', '../demos/emscripten_multi_shell.html', '-s', 'DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR=1']
versions = ['ECSEmscripten']
endif
# Dependencies # Dependencies
threads_dep = dependency('threads') threads_dep = dependency('threads')
ecs_lib = library('decs', ecs_lib = library('decs', src,
src,
include_directories : [inc], include_directories : [inc],
d_module_versions : versions
) )
decs_dep = declare_dependency( decs_dep = declare_dependency(

View file

@ -0,0 +1,3 @@
[constants]
emscripten_path = '<change-path>/emsdk/upstream/emscripten'
ldc_path = '<change-path>/ldc-1.25.0/bin'

View file

@ -0,0 +1,28 @@
; Ex. build:
; meson setup b_wasm -DbetterC=true -DBuildDemos=true --cross-file=meson_crosscompile_base.ini --cross-file=meson_crosscompile_wasm.ini
; ninja -C b_wasm
; emrun --browser chromium-browser ./b_wasm/demos/game.h
[binaries]
emcc = emscripten_path / 'emcc'
file_packager = [ 'python3', emscripten_path / 'tools/file_packager.py']
c = emscripten_path / 'emcc'
cpp = emscripten_path / 'em++'
ar = emscripten_path / 'emar'
d = ldc_path / 'ldc2'
; exe_wrapper = emscripten_path / 'emrun'
[properties]
needs_exe_wrapper = true
[built-in options]
d_args = ['-mtriple=wasm32-unknown-unknown-wasm', '--d-version=ECSEmscripten']
c_args = ['-s', 'FORCE_FILESYSTEM=1','-s', 'USE_PTHREADS=1', '-s', 'USE_SDL=2', '-s', 'ALLOW_MEMORY_GROWTH=1', '-s', 'WASM_MEM_MAX=1024MB', '-s', 'MALLOC=dlmalloc', '-s', 'WASM=1']
cpp_args = ['-s', 'FORCE_FILESYSTEM=1','-s', 'USE_PTHREADS=1', '-s', 'USE_SDL=2', '-s', 'ALLOW_MEMORY_GROWTH=1', '-s', 'WASM_MEM_MAX=1024MB', '-s', 'MALLOC=dlmalloc', '-s', 'WASM=1']
default_library = 'static'
[host_machine]
system = 'Emscripten'
cpu_family = 'wasm32'
cpu = 'wasm32'
endian = 'little'

View file

@ -1,6 +1,7 @@
[wrap-git] [wrap-git]
url = https://github.com/BindBC/bindbc-loader.git url = https://github.com/mmcomando/bindbc-loader.git
revision = 9a51af991acce3c67e51695c07bf3fa6419ef938 push-url = https://github.com/BindBC/bindbc-loader.git
revision = wasm
patch_directory = bindbc-loader patch_directory = bindbc-loader
[provide] [provide]

View file

@ -1,6 +1,7 @@
[wrap-git] [wrap-git]
url = https://github.com/BindBC/bindbc-sdl.git url = https://github.com/mmcomando/bindbc-sdl.git
revision = 5c936064b7226630f5080f4b12b77ee39c8ac64b push-url = https://github.com/BindBC/bindbc-sdl.git
revision = wasm
patch_directory = bindbc-sdl patch_directory = bindbc-sdl
[provide] [provide]

View file

@ -3,12 +3,17 @@ project('bindbc-loader', 'd', version : '0.3.2', default_options: ['default_libr
# Files # Files
src = files( src = files(
'source/bindbc/loader/package.d', 'source/bindbc/loader/package.d',
'source/bindbc/loader/sharedlib.d', # 'source/bindbc/loader/sharedlib.d', # Not implemented for wasm
'source/bindbc/loader/system.d', 'source/bindbc/loader/system.d',
) )
inc = include_directories('source') inc = include_directories('source')
if host_machine.cpu_family() != 'wasm32'
src = files(
'source/bindbc/loader/sharedlib.d',
)
endif
# Dependencies # Dependencies
lib = library('bindbc-loader', src, lib = library('bindbc-loader', src,
include_directories : [inc], include_directories : [inc],

View file

@ -1,24 +1,33 @@
project('cimgui', 'cpp', version : '1.73.0', default_options: ['default_library=shared', 'warning_level=1']) project('cimgui', 'cpp', version : '1.73.0', default_options: ['default_library=static', 'warning_level=1'])
# Files # Files
src = [ src = files(
'cimgui.cpp', 'cimgui.cpp',
'imgui/imgui.cpp', 'imgui/imgui.cpp',
'imgui/imgui_draw.cpp', 'imgui/imgui_draw.cpp',
'imgui/imgui_demo.cpp', 'imgui/imgui_demo.cpp',
'imgui/imgui_widgets.cpp', 'imgui/imgui_widgets.cpp',
] )
inc = [ '.' ] inc = [ '.' ]
inc +='imgui/examples/'
pub_inc = [ 'imgui' ] pub_inc = [ 'imgui' ]
# Dependencies if host_machine.cpu_family() == 'wasm32'
# bindbc_loader_dep = dependency('bindbc-loader') # For wasm we use C++ events/image handling implementation
src += files(
'imgui/examples/imgui_impl_opengl3.cpp',
'imgui/examples/imgui_impl_sdl.cpp',
)
endif
lib = shared_library('cimgui', src,
# dependencies : bindbc_loader_dep, add_project_arguments('-DIMGUI_IMPL_API=extern "C" ', language : 'cpp')
# Dependencies
lib = library('cimgui', src,
include_directories : [inc, pub_inc], include_directories : [inc, pub_inc],
# pic : true, pic : true,
) )
cimgui_dep = declare_dependency( cimgui_dep = declare_dependency(

View file

@ -10,12 +10,23 @@ tests_src = files(
'vector.d' 'vector.d'
) )
exe = executable('decs-tests', if host_machine.cpu_family() != 'wasm32'
tests_src, exe = executable('decs-tests', tests_src,
include_directories : [inc, include_directories('..')], include_directories : [inc, '..'],
d_args : args, dependencies : decs_dep,
link_args : link_args, )
dependencies : decs_dep,
)
test('basic-tests', exe) test('basic-tests', exe)
else
tests_lib = library('decs-tests', tests_src,
include_directories : [inc],
d_module_versions : ['ECSEmscripten'],
)
wasm_web = custom_target('wasm-web',
command: [emcc, args_wasm, '-o', '@OUTPUT@', '@INPUT@'],
input: [ecs_lib, tests_lib],
output: ['index.html'],
build_by_default: true,
)
summary('wasm-index', wasm_web.full_path())
endif