joeylib2/make/web.mk

61 lines
2.8 KiB
Makefile

# Part of JoeyLib, Copyright (c) 2026 Scott Duensing, under the MIT License.
# See LICENSE.md for license information.
# SPDX-License-Identifier: MIT
#
# Web build rules: JoeyLib programs as web pages, through the hosted core and
# the same SDL3 backend the native SDL3 port uses (src/hosted,
# src/hosted/sdl3), compiled to WebAssembly with Emscripten. The rules are
# make/hosted.mk's; this file says what is the web's. A project reaches it
# through make/project.mk.
#
# A program is a folder of its own, bin/NAME/: NAME.html (the page, from
# make/support/web/shell.html), NAME.js and NAME.wasm, NAME.data, its DATA
# folder packed in (Emscripten's --preload-file: the program finds it at
# /DATA, its current directory being /), and icon.png, its icon, which the
# page names as its own. Serve the folder over HTTP and open NAME.html;
# joey-run --port web does both.
#
# A JoeyLib program owns its loop and blocks in jlWaitVBL, which a browser
# does not allow, so programs are built with ASYNCIFY: the backend's waits
# hand the browser its turn (emscripten_sleep) and the program carries on
# where it was, unmodified.
PLATFORM := web
include $(dir $(lastword $(MAKEFILE_LIST)))/common.mk
$(call requireTool,$(EMCC),toolchain:web)
SDL3_WEB_LIB := $(SDL3_WEB_DIR)/lib/libSDL3.a
$(call requireTool,$(SDL3_WEB_LIB),toolchain:web)
# Emscripten's own environment (where its config and cache are), for every
# recipe.
export EM_CONFIG
export EM_CACHE
WEB_SHELL := $(REPO_DIR)/make/support/web/shell.html
# The asyncify unwind stack: deep enough for a wait inside a program's
# own call chain several levels down.
WEB_ASYNCIFY_STACK := 65536
HOSTED_CC := $(EMCC)
HOSTED_AR := $(dir $(EMCC))emar
HOSTED_BACKEND := sdl3
HOSTED_DEFINES := -DJOEYLIB_PLATFORM_HOSTED -DJOEYLIB_HOSTED_SDL3
HOSTED_SDK_CFLAGS := -I$(SDL3_WEB_DIR)/include
HOSTED_LINK := $(SDL3_WEB_LIB) -sASYNCIFY -sASYNCIFY_STACK_SIZE=$(WEB_ASYNCIFY_STACK) -sALLOW_MEMORY_GROWTH=1 -lidbfs.js -sEXPORTED_RUNTIME_METHODS=FS,ENV --shell-file $(WEB_SHELL)
PROGRAM_EXT := .html
# A program's page is rebuilt when the shell or SDL changes.
HOSTED_PROGRAM_DEPS := $(WEB_SHELL) $(SDL3_WEB_LIB)
# Every program in a folder of its own, with only its own DATA beside it:
# the DATA folder is packed into the program.
programDir = $(or $(call programVar,$(1),BINDIR),$(BINDIR))/$(call programName,$(1))
HOSTED_LINK_DATA = $(if $(call programVar,$(1),DATA),--preload-file $(call programDataDir,$(1))@/DATA)
# Its icon, the browser's for the page: icon.png beside it (the shell's
# <link rel="icon">), WEB_ICON_SIZE.
WEB_ICON_SIZE := 32x32
HOSTED_ICON = $(call programDir,$(1))/icon.png
HOSTED_ICON_BAKE = $(ICON_BAKE) png $(call programIcon,$(1)) $(call HOSTED_ICON,$(1)) $(WEB_ICON_SIZE)
include $(REPO_DIR)/make/hosted.mk