From 8fcc02fb813f4e5289a40d2746fef4a6b491d12b Mon Sep 17 00:00:00 2001 From: Adam Powers Date: Mon, 19 Oct 2020 11:45:22 -0700 Subject: [PATCH 1/8] fix stash merge conflict --- sys/libnh/libnhmain.c | 1 - 1 file changed, 1 deletion(-) diff --git a/sys/libnh/libnhmain.c b/sys/libnh/libnhmain.c index fe5ea55e7..2a11d4d25 100644 --- a/sys/libnh/libnhmain.c +++ b/sys/libnh/libnhmain.c @@ -1071,7 +1071,6 @@ void create_global (char *name, void *ptr, char *type); void js_globals_init() { // int i; // char buf[BUFSZ]; - printf("js_globals_init\n"); EM_ASM({ globalThis.nethackGlobal = globalThis.nethackGlobal || {}; From 69264684bd0891f9905611613f08925c400e8490 Mon Sep 17 00:00:00 2001 From: Adam Powers Date: Mon, 19 Oct 2020 11:47:56 -0700 Subject: [PATCH 2/8] fix build path --- sys/libnh/npm-package/package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sys/libnh/npm-package/package.json b/sys/libnh/npm-package/package.json index e39a7d201..0d2e13937 100644 --- a/sys/libnh/npm-package/package.json +++ b/sys/libnh/npm-package/package.json @@ -4,9 +4,9 @@ "description": "The original NetHack rogue-like game built as a WebAssembly module", "main": "src/nethackShim.js", "scripts": { - "test": "node test/test.js", + "test": "npm run build && node test/test.js", "clean": "rm ./build/nethack.js; rm ./build/nethack.wasm; true", - "build": "cp ../../../src/nethack.js ../../../src/nethack.wasm ./build", + "build": "mkdir build; cp ../../../targets/wasm/nethack.js ../../../targets/wasm/nethack.wasm ./build", "prepack": "npm run build" }, "keywords": [ From c91ec0068f192b80c5bc0fa1711a32074dc9919e Mon Sep 17 00:00:00 2001 From: Adam Powers Date: Mon, 19 Oct 2020 11:49:34 -0700 Subject: [PATCH 3/8] update to new build system --- sys/libnh/test/run.sh | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/sys/libnh/test/run.sh b/sys/libnh/test/run.sh index b89ab55d9..e930684c4 100755 --- a/sys/libnh/test/run.sh +++ b/sys/libnh/test/run.sh @@ -2,11 +2,13 @@ if [ x$1 == "xlib" ]; then echo Doing lib... - make spotless - cd sys/lib + if [ -f Makefile ]; then + make spotless + fi + cd sys/unix ./setup.sh hints/macOS.2020 cd ../.. - make + make WANT_LIBNH=1 fi if [ x$1 == "xrunlib" ]; then @@ -19,15 +21,17 @@ fi if [ x$1 == "xwasm" ]; then echo Doing wasm... - make spotless - cd sys/lib - ./setup.sh hints/wasm + if [ -f Makefile ]; then + make spotless + fi + cd sys/unix + ./setup.sh hints/macOS.2020 cd ../.. - make + make CROSS_TO_WASM=1 fi if [ x$1 == "xrunwasm" ]; then - cd sys/lib/npm-package && node test/test.js + cd sys/lib/npm-package && npm run build && node test/test.js fi if [ x$1 == "xbin" ]; then From 6598b3d2d0c3a630aea1325f6483781096d09200 Mon Sep 17 00:00:00 2001 From: Adam Powers Date: Mon, 19 Oct 2020 11:54:42 -0700 Subject: [PATCH 4/8] wasm requires HACKDIR for embedded files --- sys/unix/hints/macOS.2020 | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/sys/unix/hints/macOS.2020 b/sys/unix/hints/macOS.2020 index 85f9b3208..e6fae4c06 100755 --- a/sys/unix/hints/macOS.2020 +++ b/sys/unix/hints/macOS.2020 @@ -281,6 +281,11 @@ POSTINSTALL+= if test -f $(SHELLDIR)/$(GAME); then \ endif # WANT_BUNDLE endif # !WANT_SHARE_INSTALL +ifdef CROSS_TO_WASM +HACKDIR=/ +PREFIX= +endif # !CROSS_TO_WASM + INSTDIR=$(HACKDIR) VARDIR=$(HACKDIR) @@ -294,6 +299,7 @@ VARDIR=$(HACKDIR) #-INCLUDE cross-pre.2020 # #-POST + ifdef WANT_LIBNH libnh.a: $(HOBJ) $(LIBNHSYSOBJ) ../lib/lua/liblua.a $(AR) rcs $@ $(HOBJ) $(LIBNHSYSOBJ) ../lib/lua/liblua.a From 5e5324c25b39ab2993924a70131149961caacdcb Mon Sep 17 00:00:00 2001 From: Adam Powers Date: Mon, 19 Oct 2020 11:55:04 -0700 Subject: [PATCH 5/8] fix build flags --- sys/unix/hints/include/cross-pre.2020 | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/sys/unix/hints/include/cross-pre.2020 b/sys/unix/hints/include/cross-pre.2020 index 0e9816627..73eec2337 100644 --- a/sys/unix/hints/include/cross-pre.2020 +++ b/sys/unix/hints/include/cross-pre.2020 @@ -32,7 +32,7 @@ BUILD_TARGET_LUA=1 override TARGET = wasm override TARGETDIR=../targets/$(TARGET) override TARGETPFX = $(TARGETDIR)/ -override TARGET_LIBS= +override TARGET_LIBS= endif ifdef CROSS @@ -246,7 +246,7 @@ else override TARGET_LINK = $(TARGET_CC) endif override TARGET_LFLAGS= $(TOOLARCH) -#override TARGET_LIBS += +#override TARGET_LIBS += VARDATND += nhtiles.bmp override SYSSRC = ../outdated/sys/amiga/amidos.c ../outdated/sys/amiga/amigst.c \ ../outdated/sys/amiga/amimenu.c ../outdated/sys/amiga/amirip.c \ @@ -298,10 +298,12 @@ ifdef CROSS_TO_WASM # originally from https://github.com/NetHack/NetHack/pull/385 #===============-================================================= # +WASM_DEBUG = 1 WASM_DATA_DIR = $(TARGETPFX)wasm-data WASM_TARGET = $(TARGETPFX)nethack.js EMCC_LFLAGS = #EMCC_LFLAGS += -s SINGLE_FILE=1 +EMCC_LFLAGS += -DHACKDIR=\"$(HACKDIR)\" EMCC_LFLAGS += -s WASM=1 EMCC_LFLAGS += -s ALLOW_TABLE_GROWTH EMCC_LFLAGS += -s ASYNCIFY -s ASYNCIFY_IMPORTS='["local_callback"]' @@ -311,23 +313,28 @@ EMCC_LFLAGS += -s EXPORTED_FUNCTIONS='["_main", "_shim_graphics_set_callback"]' EMCC_LFLAGS += -s EXPORTED_RUNTIME_METHODS='["cwrap", "ccall", "addFunction", \ "removeFunction", "UTF8ToString", "getValue", "setValue"]' EMCC_LFLAGS += -s ERROR_ON_UNDEFINED_SYMBOLS=0 -EMCC_LFLAGS += --embed-file $(WASM_DATA_DIR) +# XXX: the "@/" at the end of "--embed-file" tells emscripten to embed the files +# in the root directory, otherwise they will end up in the $(WASM_DATA_DIR) path +EMCC_LFLAGS += --embed-file $(WASM_DATA_DIR)@/ # For a list of EMCC settings: # https://github.com/emscripten-core/emscripten/blob/master/src/settings.js # # WASM C flags -EMCC_CFLAGS= +EMCC_CFLAGS = EMCC_CFLAGS += -Wall -#EMCC_CFLAGS += -Werror +EMCC_CFLAGS += -Werror +EMCC_CFLAGS #EMCC_CFLAGS += -s DISABLE_EXCEPTION_CATCHING=0 -EMCC_DEBUG_CFLAGS += -s ASSERTIONS=1 -#EMCC_DEBUG_CFLAGS += -s ASSERTIONS=2 +#EMCC_DEBUG_CFLAGS += -s ASSERTIONS=1 +EMCC_DEBUG_CFLAGS += -s ASSERTIONS=2 EMCC_DEBUG_CFLAGS += -s STACK_OVERFLOW_CHECK=2 EMCC_DEBUG_CFLAGS += -s SAFE_HEAP=1 EMCC_DEBUG_CFLAGS += -s LLD_REPORT_UNDEFINED=1 -#EMCC_DEBUG_CFLAGS += -s EXCEPTION_DEBUG=1 +EMCC_DEBUG_CFLAGS += -s EXCEPTION_DEBUG=1 #EMCC_DEBUG_CFLAGS += -fsanitize=undefined -fsanitize=address -fsanitize=leak -#EMCC_DEBUG_CFLAGS += -s EXIT_RUNTIME +EMCC_DEBUG_CFLAGS += -s EXIT_RUNTIME=1 +# XXX: if --profiling isn't included then any error dumps 10MB of WASM to the screen rather than a useful message +EMCC_DEBUG_CFLAGS += --profiling EMCC_PROD_CFLAGS += -O3 ifdef WASM_DEBUG EMCC_CFLAGS += $(EMCC_DEBUG_CFLAGS) From 03e9eb6dd66c08a0f337e752f0e5887f942928be Mon Sep 17 00:00:00 2001 From: Adam Powers Date: Mon, 19 Oct 2020 13:21:07 -0700 Subject: [PATCH 6/8] fix NO_SIGNAL --- src/files.c | 2 +- src/save.c | 2 +- sys/libnh/libnhmain.c | 7 ++++--- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/files.c b/src/files.c index f3c171111..dd7245861 100644 --- a/src/files.c +++ b/src/files.c @@ -630,10 +630,10 @@ clearlocks() #endif #ifndef NO_SIGNAL (void) signal(SIGINT, SIG_IGN); -#endif #if defined(UNIX) || defined(VMS) sethanguphandler((void FDECL((*), (int) )) SIG_IGN); #endif +#endif /* NO_SIGNAL */ /* can't access maxledgerno() before dungeons are created -dlc */ for (x = (g.n_dgns ? maxledgerno() : 0); x >= 0; x--) delete_levelfile(x); /* not all levels need be present */ diff --git a/src/save.c b/src/save.c index c3864d5b0..a15ce5860 100644 --- a/src/save.c +++ b/src/save.c @@ -101,10 +101,10 @@ dosave0() return 0; fq_save = fqname(g.SAVEF, SAVEPREFIX, 1); /* level files take 0 */ +#ifndef NO_SIGNAL #if defined(UNIX) || defined(VMS) sethanguphandler((void FDECL((*), (int) )) SIG_IGN); #endif -#ifndef NO_SIGNAL (void) signal(SIGINT, SIG_IGN); #endif diff --git a/sys/libnh/libnhmain.c b/sys/libnh/libnhmain.c index 2a11d4d25..844f385d0 100644 --- a/sys/libnh/libnhmain.c +++ b/sys/libnh/libnhmain.c @@ -555,11 +555,11 @@ whoami() return FALSE; } +#ifndef NO_SIGNAL void sethanguphandler(handler) void FDECL((*handler), (int)); { -#ifndef NO_SIGNAL #ifdef SA_RESTART /* don't want reads to restart. If SA_RESTART is defined, we know * sigaction exists and can be used to ensure reads won't restart. @@ -581,8 +581,8 @@ void FDECL((*handler), (int)); (void) signal(SIGXCPU, (SIG_RET_TYPE) handler); #endif #endif /* ?SA_RESTART */ -#endif /* !NO_SIGNAL */ } +#endif /* !NO_SIGNAL */ #ifdef PORT_HELP void @@ -1003,7 +1003,8 @@ void js_constants_init() { // copyright SET_CONSTANT_STRING("COPYRIGHT", COPYRIGHT_BANNER_A); SET_CONSTANT_STRING("COPYRIGHT", COPYRIGHT_BANNER_B); - SET_CONSTANT_STRING("COPYRIGHT", COPYRIGHT_BANNER_C); + // XXX: not set for cross-compile + //SET_CONSTANT_STRING("COPYRIGHT", COPYRIGHT_BANNER_C); SET_CONSTANT_STRING("COPYRIGHT", COPYRIGHT_BANNER_D); // glyphs From 6863730b63415861a64822c30f36d1eaf2d5e47b Mon Sep 17 00:00:00 2001 From: Adam Powers Date: Mon, 19 Oct 2020 13:21:40 -0700 Subject: [PATCH 7/8] remove wasm --- sys/unix/hints/macOS.2020 | 5 ----- 1 file changed, 5 deletions(-) diff --git a/sys/unix/hints/macOS.2020 b/sys/unix/hints/macOS.2020 index e6fae4c06..283f1a779 100755 --- a/sys/unix/hints/macOS.2020 +++ b/sys/unix/hints/macOS.2020 @@ -281,11 +281,6 @@ POSTINSTALL+= if test -f $(SHELLDIR)/$(GAME); then \ endif # WANT_BUNDLE endif # !WANT_SHARE_INSTALL -ifdef CROSS_TO_WASM -HACKDIR=/ -PREFIX= -endif # !CROSS_TO_WASM - INSTDIR=$(HACKDIR) VARDIR=$(HACKDIR) From b43b035321dc7ee7d0234a941822e84aaf847726 Mon Sep 17 00:00:00 2001 From: Adam Powers Date: Mon, 19 Oct 2020 13:22:02 -0700 Subject: [PATCH 8/8] fix wasm runtime errors --- sys/unix/hints/include/cross-pre.2020 | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/sys/unix/hints/include/cross-pre.2020 b/sys/unix/hints/include/cross-pre.2020 index 73eec2337..562a1934d 100644 --- a/sys/unix/hints/include/cross-pre.2020 +++ b/sys/unix/hints/include/cross-pre.2020 @@ -29,6 +29,8 @@ endif ifdef CROSS_TO_WASM CROSS=1 BUILD_TARGET_LUA=1 +HACKDIR=/ +PREFIX= override TARGET = wasm override TARGETDIR=../targets/$(TARGET) override TARGETPFX = $(TARGETDIR)/ @@ -309,7 +311,7 @@ EMCC_LFLAGS += -s ALLOW_TABLE_GROWTH EMCC_LFLAGS += -s ASYNCIFY -s ASYNCIFY_IMPORTS='["local_callback"]' EMCC_LFLAGS += -O3 EMCC_LFLAGS += -s MODULARIZE -EMCC_LFLAGS += -s EXPORTED_FUNCTIONS='["_main", "_shim_graphics_set_callback"]' +EMCC_LFLAGS += -s EXPORTED_FUNCTIONS='["_main", "_shim_graphics_set_callback", "_mapglyph", "_display_inventory"]' EMCC_LFLAGS += -s EXPORTED_RUNTIME_METHODS='["cwrap", "ccall", "addFunction", \ "removeFunction", "UTF8ToString", "getValue", "setValue"]' EMCC_LFLAGS += -s ERROR_ON_UNDEFINED_SYMBOLS=0 @@ -323,7 +325,7 @@ EMCC_LFLAGS += --embed-file $(WASM_DATA_DIR)@/ EMCC_CFLAGS = EMCC_CFLAGS += -Wall EMCC_CFLAGS += -Werror -EMCC_CFLAGS +EMCC_CFLAGS += -DNO_SIGNAL #EMCC_CFLAGS += -s DISABLE_EXCEPTION_CATCHING=0 #EMCC_DEBUG_CFLAGS += -s ASSERTIONS=1 EMCC_DEBUG_CFLAGS += -s ASSERTIONS=2