From 73c3e48c56ad9ea670b1bf2ec72334ba5a6f00fc Mon Sep 17 00:00:00 2001 From: mmcomando Date: Thu, 25 Feb 2021 21:59:20 +0100 Subject: [PATCH] Building for web using meson --- .gitignore | 2 ++ emscripten.ini | 24 ++++++++++++++++++++++++ install_emscripten.sh | 19 +++++++++++++++++++ meson.build | 8 ++++++-- meson_options.txt | 2 +- tests/meson.build | 30 ++++++++++++++++++++++-------- 6 files changed, 74 insertions(+), 11 deletions(-) create mode 100644 emscripten.ini create mode 100644 install_emscripten.sh diff --git a/.gitignore b/.gitignore index f2e30a3..ad61594 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,8 @@ !skeleton.html !**/meson.build !**/*.wrap +!**/*.ini +!**/*.sh !meson_options.txt !compile_wasm.py !compile_android.py \ No newline at end of file diff --git a/emscripten.ini b/emscripten.ini new file mode 100644 index 0000000..48421e7 --- /dev/null +++ b/emscripten.ini @@ -0,0 +1,24 @@ +[constants] +emscripten_path = '/home/pc/prog/ecs/emsdk/upstream/emscripten' +ldc_path = '/home/pc/dlang/ldc-1.25.0/bin' + +[binaries] +emcc = emscripten_path / 'emcc' +c = emscripten_path / 'emcc' +cpp = emscripten_path / 'em++' +ar = emscripten_path / 'emar' +d = ldc_path / 'ldc2' + +[properties] +needs_exe_wrapper = true + + +[built-in options] +d_args = ['-mtriple=wasm32-unknown-unknown-wasm', '--d-version=ECSEmscripten'] +default_library = 'static' + +[host_machine] +system = 'linux' +cpu_family = 'wasm32' +cpu = 'wasm32' +endian = 'little' \ No newline at end of file diff --git a/install_emscripten.sh b/install_emscripten.sh new file mode 100644 index 0000000..836603a --- /dev/null +++ b/install_emscripten.sh @@ -0,0 +1,19 @@ +cd .. +# Get the emsdk repo +git clone https://github.com/emscripten-core/emsdk.git + +# Enter that directory +cd emsdk + +# Fetch the latest version of the emsdk (not needed the first time you clone) +git pull + +# Download and install the latest SDK tools. +./emsdk install latest + +# Make the "latest" SDK "active" for the current user. (writes .emscripten file) +./emsdk activate latest + +# Activate PATH and other environment variables in the current terminal +source ./emsdk_env.sh + diff --git a/meson.build b/meson.build index d5cb683..b8726f8 100644 --- a/meson.build +++ b/meson.build @@ -50,11 +50,15 @@ endif add_global_arguments(args, language : 'd') add_global_link_arguments(link_args, language : 'd') +if host_machine.cpu_family() == 'wasm32' + add_global_arguments('--output-bc', language : 'd') # Adding it in cross files breaks linker detection +endif + + # Dependencies threads_dep = dependency('threads') -ecs_lib = library('decs', - src, +ecs_lib = library('decs', src, include_directories : [inc], ) diff --git a/meson_options.txt b/meson_options.txt index ba79c90..48d5d54 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -1,3 +1,3 @@ option('betterC', type: 'boolean', value: true) -option('BuildDemos', type: 'boolean', value: true) +option('BuildDemos', type: 'boolean', value: false) option('LTO', type: 'boolean', value: false) \ No newline at end of file diff --git a/tests/meson.build b/tests/meson.build index dae8536..8b94c62 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -2,12 +2,26 @@ tests_src = files( 'tests.d', ) -exe = executable('decs-tests', - tests_src, - include_directories : [inc], - d_args : args, - link_args : link_args, - dependencies : decs_dep, -) -test('basic-tests', exe) +if host_machine.cpu_family() != 'wasm32' + exe = executable('decs-tests', tests_src, + include_directories : [inc], + dependencies : decs_dep, + ) + + test('basic-tests', exe) +else + tests_lib = library('decs-tests', tests_src, + include_directories : [inc], + ) + emcc = find_program('emcc') + args_wasm_web = ['-v', '-s', 'ALLOW_MEMORY_GROWTH=1', '-s', 'WASM_MEM_MAX=1024MB', '-s', 'MALLOC=dlmalloc', '-s', 'WASM=1'] + wasm_web = custom_target('wasm-web', + command: [emcc, args_wasm_web, '-o', '@OUTPUT@', '@INPUT@'], + input: [ecs_lib, tests_lib], + output: ['index.html'], + build_by_default: true, + ) + summary('wasm-index', wasm_web.full_path()) + +endif -- 2.47.2