Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add webassembly build #70697

Merged
merged 43 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
75d3dc9
emscripten
rameshvarun Nov 26, 2022
c9b80c4
fix build
nornagon Jan 5, 2024
41741ab
restore version
nornagon Jan 5, 2024
2d9d48b
html updates
nornagon Jan 5, 2024
bcaf6ce
permit webgl 2
nornagon Jan 5, 2024
2cedbbf
-gseparate-dwarf
nornagon Jan 5, 2024
fe2deb1
github action
nornagon Jan 5, 2024
e0ac135
fix action branches
nornagon Jan 5, 2024
fcdf2ee
fix action more
nornagon Jan 5, 2024
a14e565
automatically detect parallelism
nornagon Jan 5, 2024
010d887
fix separate-dwarf to only be on ldflags
nornagon Jan 5, 2024
273fa84
fix artifact name
nornagon Jan 5, 2024
eae5933
un-disable warnings?
nornagon Jan 6, 2024
0eba251
move ASYNCIFY to ldflags
nornagon Jan 6, 2024
0fc790d
fix scripts
nornagon Jan 6, 2024
dca8093
fix scripts
nornagon Jan 6, 2024
6f928a2
allow all tilesets, simplify obsolete mod removal
nornagon Jan 6, 2024
d9b1a39
build with std=gnu++17
nornagon Jan 6, 2024
06bed36
remove pre.js
nornagon Jan 6, 2024
18cbdd7
remove emscripten_exception.cpp
nornagon Jan 6, 2024
4778a2c
instead of gnu++17, turn off the warning
nornagon Jan 6, 2024
33dff59
wrap more things in #if defined(EMSCRIPTEN)
nornagon Jan 6, 2024
884d527
cleanups
nornagon Jan 6, 2024
0c066ad
put filesystem.cpp changes behind #define
nornagon Jan 6, 2024
4ce8b73
browse link points to main CDDA repo
nornagon Jan 6, 2024
c6199b9
reformat index.html with prettier
nornagon Jan 6, 2024
f01a309
astyle
nornagon Jan 6, 2024
942c101
update workflow
nornagon Jan 6, 2024
ae13f67
remove unused env in workflow
nornagon Jan 6, 2024
bf9d67f
remove nonsense
nornagon Jan 6, 2024
b761397
ignore ESC in main menu
nornagon Jan 6, 2024
d3bafce
speedbump when closing page if game is unsaved
nornagon Jan 6, 2024
b61cff3
fs sync polling => precise
nornagon Jan 6, 2024
e64dd60
forgot to include emscripten.h
nornagon Jan 6, 2024
0e8d6fa
don't strip emscripten
nornagon Jan 6, 2024
0a9a09a
astyle
nornagon Jan 6, 2024
c6ff81d
expose setFsNeedsSync on window
nornagon Jan 6, 2024
b484fdf
acorn bug...?
nornagon Jan 7, 2024
60ef5cf
looks like -gseparate-dwarf prevents some binaryen opts
nornagon Jan 7, 2024
99eb8b6
we don't generate the debug.wasm any more
nornagon Jan 7, 2024
7ecee4e
Merge branch 'master' into emscripten-new
nornagon Jan 7, 2024
45f3573
move game_unsaved = false to before return in game::save
nornagon Jan 7, 2024
a225a5c
make setFsNeedsSync static to suppress -Wmissing-declarations error
nornagon Jan 8, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since #70870 is now uploading WebAssembly builds, should we remove this step here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's still handy for development—the emscripten SDK doesn't work well on macOS or Windows, so it's handy to be able to download the built artifact from CI for testing. I'm not aware of any major downsides to uploading it.

Copy link
Member

@BrettDong BrettDong Jan 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought upload-artifact shares the total GitHub Actions storage limit of the repository, so too many build artifact uploads could evict other data like ccache.

But I just re-checked GitHub Actions doc and it only says GitHub Actions storage usage is free for public repositories without mentioning storage limits or data eviction policy, so I'm not too sure now.

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.F

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious if ccache works with emcc so we can reduce build times.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this is definitely worth looking into.

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
Loading