Skip to content

Commit

Permalink
Merge pull request CleverRaven#70697 from nornagon/emscripten-new
Browse files Browse the repository at this point in the history
add webassembly build
  • Loading branch information
Maleclypse authored and nornagon committed Jun 7, 2024
1 parent f8cf42f commit 87cf016
Show file tree
Hide file tree
Showing 17 changed files with 585 additions and 9 deletions.
59 changes: 59 additions & 0 deletions .github/workflows/emscripten.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: Emscripten build

on:
push:
branches:
- master
paths-ignore:
- 'android/**'
- 'build-data/osx/**'
- 'doc/**'
- 'doxygen_doc/**'
- 'lang/**'
- 'lgtm/**'
- 'tools/**'
- '!tools/format/**'
- 'utilities/**'
pull_request:
branches:
- master
paths-ignore:
- 'android/**'
- 'build-data/osx/**'
- 'doc/**'
- 'doxygen_doc/**'
- 'lang/**'
- 'lgtm/**'
- 'tools/**'
- '!tools/format/**'
- 'utilities/**'

# We only care about the latest revision of a PR, so cancel previous instances.
concurrency:
group: emscripten-build-${{ github.event.pull_request.number || github.ref_name }}
cancel-in-progress: true

jobs:
build_catatclysm:
name: Build
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- uses: mymindstorm/setup-emsdk@v13

- name: Prepare web data
run: ./build-scripts/prepare-web-data.sh

- name: Build with emcc (emscripten)
run: ./build-scripts/build-emscripten.sh

- name: Assemble web bundle
run: ./build-scripts/prepare-web.sh

- name: Create artifact
uses: actions/upload-artifact@v4
with:
name: play-cdda
path: build/*
60 changes: 58 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
# Run: make NATIVE=win32
# OS X
# Run: make NATIVE=osx
# Emscripten
# Run: make NATIVE=emscripten

# Build types:
# Debug (no optimizations)
Expand Down Expand Up @@ -112,6 +114,10 @@ WARNINGS = \
-Wno-unknown-warning-option \
-Wno-dangling-reference \
-Wno-c++20-compat
ifeq ($(NATIVE), emscripten)
# The EM_ASM macro triggers this warning.
WARNINGS += -Wno-gnu-zero-variadic-macro-arguments
endif
# Uncomment below to disable warnings
#WARNINGS = -w
DEBUGSYMS = -g
Expand Down Expand Up @@ -144,6 +150,9 @@ VERSION = 0.H

TARGET_NAME = cataclysm
TILES_TARGET_NAME = $(TARGET_NAME)-tiles
ifeq ($(NATIVE), emscripten)
TILES_TARGET_NAME = $(TARGET_NAME)-tiles.js
endif

TARGET = $(BUILD_PREFIX)$(TARGET_NAME)
TILESTARGET = $(BUILD_PREFIX)$(TILES_TARGET_NAME)
Expand Down Expand Up @@ -389,6 +398,8 @@ ifeq ($(RELEASE), 1)
else
OPTLEVEL = -O3
endif
else ifeq ($(NATIVE), emscripten)
OPTLEVEL = -Os
else
# MXE ICE Workaround
# known bad on 4.9.3 and 4.9.4, if it gets fixed this could include a version test too
Expand Down Expand Up @@ -457,7 +468,9 @@ else
# way to turn off optimization (make NOOPT=1) entirely.
OPTLEVEL = -O0
else
ifeq ($(shell $(CXX) -E -Og - < /dev/null > /dev/null 2>&1 && echo fog),fog)
ifeq ($(NATIVE),emscripten)
OPTLEVEL = -O3
else ifeq ($(shell $(CXX) -E -Og - < /dev/null > /dev/null 2>&1 && echo fog),fog)
OPTLEVEL = -Og
else
OPTLEVEL = -O0
Expand Down Expand Up @@ -618,6 +631,47 @@ ifeq ($(NATIVE), cygwin)
TARGETSYSTEM=CYGWIN
endif

# Emscripten
ifeq ($(NATIVE), emscripten)
CXX=emcc
LD=emcc

# Flags that are common across compile and link phases.
EMCC_COMMON_FLAGS = -sUSE_SDL=2 -sUSE_SDL_IMAGE=2 -sUSE_SDL_TTF=2 -sSDL2_IMAGE_FORMATS=['png']

ifneq ($(RELEASE), 1)
EMCC_COMMON_FLAGS += -g
endif

CXXFLAGS += $(EMCC_COMMON_FLAGS)
LDFLAGS += $(EMCC_COMMON_FLAGS)

LDFLAGS += --preload-file web_bundle@/
LDFLAGS += -sEXPORTED_RUNTIME_METHODS=['FS','stackTrace','jsStackTrace']
LDFLAGS += -sINITIAL_MEMORY=512MB
LDFLAGS += -sMAXIMUM_MEMORY=4GB
LDFLAGS += -sALLOW_MEMORY_GROWTH
LDFLAGS += -sSTACK_SIZE=262144
LDFLAGS += -sASYNCIFY
LDFLAGS += -sASYNCIFY_STACK_SIZE=16384
LDFLAGS += -sENVIRONMENT=web
LDFLAGS += -lidbfs.js
LDFLAGS += -lembind
LDFLAGS += -sWASM_BIGINT # Browser will require BigInt support.
LDFLAGS += -sMAX_WEBGL_VERSION=2

ifeq ($(RELEASE), 1)
# Release-mode Linker flags.
LDFLAGS += -Os
LDFLAGS += -sLZ4
else
# Debug mode linker flags.
LDFLAGS += -O1 # Emscripten link time is slow, so use low optimization level.
LDFLAGS += -sFS_DEBUG
LDFLAGS += -gseparate-dwarf
endif
endif

# MXE cross-compile to win32
ifneq (,$(findstring mingw32,$(CROSS)))
DEFINES += -DCROSS_LINUX
Expand Down Expand Up @@ -716,7 +770,7 @@ ifeq ($(TILES), 1)
LDFLAGS += -lSDL2_mixer
endif
endif
else # not osx
else ifneq ($(NATIVE),emscripten)
CXXFLAGS += $(shell $(PKG_CONFIG) --cflags sdl2 SDL2_image SDL2_ttf)

ifeq ($(STATIC), 1)
Expand Down Expand Up @@ -961,7 +1015,9 @@ $(TARGET): $(OBJS)
ifeq ($(RELEASE), 1)
ifndef DEBUG_SYMBOLS
ifneq ($(BACKTRACE),1)
ifneq ($(NATIVE), emscripten)
$(STRIP) $(TARGET)
endif
endif
endif
endif
Expand Down
7 changes: 7 additions & 0 deletions build-scripts/build-emscripten.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash
set -exo pipefail

emsdk install 3.1.51
emsdk activate 3.1.51

make -j`nproc` NATIVE=emscripten BACKTRACE=0 TILES=1 TESTS=0 RUNTESTS=0 RELEASE=1 cataclysm-tiles.js
22 changes: 22 additions & 0 deletions build-scripts/prepare-web-data.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash
set -exo pipefail

rm -rf web_bundle

BUNDLE_DIR=web_bundle
DATA_DIR=$BUNDLE_DIR/data
mkdir -p $DATA_DIR
cp -R data/{core,font,fontdata.json,json,mods,names,raw,motd,credits,title,help} $DATA_DIR/
cp -R gfx $BUNDLE_DIR/

# Remove .DS_Store files.
find web_bundle -name ".DS_Store" -type f -exec rm {} \;

# Remove obsolete mods.
echo "Removing obsolete mods..."
for MOD_DIR in $DATA_DIR/mods/*/ ; do
if jq -e '.[] | select(.type == "MOD_INFO") | .obsolete' "$MOD_DIR/modinfo.json" >/dev/null; then
echo "$MOD_DIR is obsolete, excluding from web_bundle..."
rm -rf $MOD_DIR
fi
done
10 changes: 10 additions & 0 deletions build-scripts/prepare-web.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash
set -exo pipefail

mkdir -p build/
cp \
index.html \
cataclysm-tiles.{data,js,wasm} \
data/font/Terminus.ttf \
build
cp data/cataicon.ico build/favicon.ico
Loading

0 comments on commit 87cf016

Please sign in to comment.