Skip to content

Commit 3beda60

Browse files
committed
meson: Add wasm build in build scripts
has_int128_type is set to false on emscripten as of now to avoid errors by libffi. Tests are disabled on emscripten because they rely on host features that aren't supported by emscripten (e.g. fork and unix socket). Signed-off-by: Kohei Tokunaga <[email protected]>
1 parent 08c9dcd commit 3beda60

File tree

6 files changed

+42
-7
lines changed

6 files changed

+42
-7
lines changed

MAINTAINERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,7 @@ S: Maintained
625625
F: include/system/os-wasm.h
626626
F: os-wasm.c
627627
F: util/coroutine-wasm.c
628+
F: configs/meson/emscripten.txt
628629

629630
Alpha Machines
630631
--------------

configs/meson/emscripten.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[built-in options]
2+
c_args = ['-pthread']
3+
cpp_args = ['-pthread']
4+
objc_args = ['-pthread']
5+
# -sPROXY_TO_PTHREAD link time flag always requires -pthread even during
6+
# configuration so explicitly add the flag here.
7+
c_link_args = ['-pthread','-sASYNCIFY=1','-sPROXY_TO_PTHREAD=1','-sFORCE_FILESYSTEM','-sALLOW_TABLE_GROWTH','-sTOTAL_MEMORY=2GB','-sWASM_BIGINT','-sEXPORT_ES6=1','-sASYNCIFY_IMPORTS=ffi_call_js','-sEXPORTED_RUNTIME_METHODS=addFunction,removeFunction,TTY,FS']
8+
cpp_link_args = ['-pthread','-sASYNCIFY=1','-sPROXY_TO_PTHREAD=1','-sFORCE_FILESYSTEM','-sALLOW_TABLE_GROWTH','-sTOTAL_MEMORY=2GB','-sWASM_BIGINT','-sEXPORT_ES6=1','-sASYNCIFY_IMPORTS=ffi_call_js','-sEXPORTED_RUNTIME_METHODS=addFunction,removeFunction,TTY,FS']

configure

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,10 @@ elif check_define __NetBSD__; then
360360
host_os=netbsd
361361
elif check_define __APPLE__; then
362362
host_os=darwin
363+
elif check_define EMSCRIPTEN ; then
364+
host_os=emscripten
365+
cpu=wasm32
366+
cross_compile="yes"
363367
else
364368
# This is a fatal error, but don't report it yet, because we
365369
# might be going to just print the --help text, or it might
@@ -526,6 +530,9 @@ case "$cpu" in
526530
linux_arch=x86
527531
CPU_CFLAGS="-m64"
528532
;;
533+
wasm32)
534+
CPU_CFLAGS="-m32"
535+
;;
529536
esac
530537

531538
if test -n "$host_arch" && {

meson.build

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ genh = []
5050
qapi_trace_events = []
5151

5252
bsd_oses = ['gnu/kfreebsd', 'freebsd', 'netbsd', 'openbsd', 'dragonfly', 'darwin']
53-
supported_oses = ['windows', 'freebsd', 'netbsd', 'openbsd', 'darwin', 'sunos', 'linux']
53+
supported_oses = ['windows', 'freebsd', 'netbsd', 'openbsd', 'darwin', 'sunos', 'linux', 'emscripten']
5454
supported_cpus = ['ppc', 'ppc64', 's390x', 'riscv32', 'riscv64', 'x86', 'x86_64',
55-
'arm', 'aarch64', 'loongarch64', 'mips', 'mips64', 'sparc64']
55+
'arm', 'aarch64', 'loongarch64', 'mips', 'mips64', 'sparc64', 'wasm32']
5656

5757
cpu = host_machine.cpu_family()
5858

@@ -353,6 +353,8 @@ foreach lang : all_languages
353353
# endif
354354
#endif''')
355355
# ok
356+
elif compiler.get_id() == 'emscripten'
357+
# ok
356358
else
357359
error('You either need GCC v7.4 or Clang v10.0 (or XCode Clang v15.0) to compile QEMU')
358360
endif
@@ -470,7 +472,10 @@ endif
470472
# instead, we can't add -no-pie because it overrides -shared: the linker then
471473
# tries to build an executable instead of a shared library and fails. So
472474
# don't add -no-pie anywhere and cross fingers. :(
473-
if not get_option('b_pie')
475+
#
476+
# Emscripten doesn't support -no-pie but meson can't catch the compiler
477+
# warning. So explicitly omit the flag for Emscripten.
478+
if not get_option('b_pie') and host_os != 'emscripten'
474479
qemu_common_flags += cc.get_supported_arguments('-fno-pie', '-no-pie')
475480
endif
476481

@@ -514,6 +519,8 @@ ucontext_probe = '''
514519
supported_backends = []
515520
if host_os == 'windows'
516521
supported_backends += ['windows']
522+
elif host_os == 'emscripten'
523+
supported_backends += ['wasm']
517524
else
518525
if host_os != 'darwin' and cc.links(ucontext_probe)
519526
supported_backends += ['ucontext']
@@ -902,6 +909,10 @@ if get_option('tcg').allowed()
902909
if not get_option('tcg_interpreter')
903910
error('Unsupported CPU @0@, try --enable-tcg-interpreter'.format(cpu))
904911
endif
912+
elif host_arch == 'wasm32'
913+
if not get_option('tcg_interpreter')
914+
error('WebAssembly host requires --enable-tcg-interpreter')
915+
endif
905916
elif get_option('tcg_interpreter')
906917
warning('Use of the TCG interpreter is not recommended on this host')
907918
warning('architecture. There is a native TCG execution backend available')
@@ -2962,7 +2973,9 @@ config_host_data.set('CONFIG_ATOMIC64', cc.links('''
29622973
return 0;
29632974
}''', args: qemu_isa_flags))
29642975

2965-
has_int128_type = cc.compiles('''
2976+
# has_int128_type is set to false on Emscripten to avoid errors by libffi
2977+
# during runtime.
2978+
has_int128_type = host_os != 'emscripten' and cc.compiles('''
29662979
__int128_t a;
29672980
__uint128_t b;
29682981
int main(void) { b = a; }''')
@@ -3774,6 +3787,8 @@ if have_block
37743787
# os-win32.c does not
37753788
if host_os == 'windows'
37763789
system_ss.add(files('os-win32.c'))
3790+
elif host_os == 'emscripten'
3791+
blockdev_ss.add(files('os-wasm.c'))
37773792
else
37783793
blockdev_ss.add(files('os-posix.c'))
37793794
endif
@@ -4456,7 +4471,11 @@ subdir('scripts')
44564471
subdir('tools')
44574472
subdir('pc-bios')
44584473
subdir('docs')
4459-
subdir('tests')
4474+
# Tests are disabled on emscripten because they rely on host features that aren't
4475+
# supported by emscripten (e.g. fork and unix socket).
4476+
if host_os != 'emscripten'
4477+
subdir('tests')
4478+
endif
44604479
if gtk.found()
44614480
subdir('po')
44624481
endif

meson_options.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ option('fuzzing_engine', type : 'string', value : '',
3434
option('trace_file', type: 'string', value: 'trace',
3535
description: 'Trace file prefix for simple backend')
3636
option('coroutine_backend', type: 'combo',
37-
choices: ['ucontext', 'sigaltstack', 'windows', 'auto'],
37+
choices: ['ucontext', 'sigaltstack', 'windows', 'wasm', 'auto'],
3838
value: 'auto', description: 'coroutine backend to use')
3939

4040
# Everything else can be set via --enable/--disable-* option

scripts/meson-buildoptions.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ meson_options_help() {
8080
printf "%s\n" ' --tls-priority=VALUE Default TLS protocol/cipher priority string'
8181
printf "%s\n" ' [NORMAL]'
8282
printf "%s\n" ' --with-coroutine=CHOICE coroutine backend to use (choices:'
83-
printf "%s\n" ' auto/sigaltstack/ucontext/windows)'
83+
printf "%s\n" ' auto/sigaltstack/ucontext/windows/wasm)'
8484
printf "%s\n" ' --with-pkgversion=VALUE use specified string as sub-version of the'
8585
printf "%s\n" ' package'
8686
printf "%s\n" ' --with-suffix=VALUE Suffix for QEMU data/modules/config directories'

0 commit comments

Comments
 (0)