| 1 | # |
| 2 | # Makefile to use with emscripten |
| 3 | # See https://emscripten.org/docs/getting_started/downloads.html |
| 4 | # for installation instructions. |
| 5 | # |
| 6 | # This Makefile assumes you have loaded emscripten's environment. |
| 7 | # (On Windows, you may need to execute emsdk_env.bat or encmdprompt.bat ahead) |
| 8 | # |
| 9 | # Running `make -f Makefile.emscripten` will produce three files: |
| 10 | # - web/index.html |
| 11 | # - web/index.js |
| 12 | # - web/index.wasm |
| 13 | # |
| 14 | # All three are needed to run the demo. |
| 15 | |
| 16 | CC = emcc |
| 17 | CXX = em++ |
| 18 | WEB_DIR = web |
| 19 | EXE = $(WEB_DIR)/index.html |
| 20 | IMGUI_DIR = ../.. |
| 21 | SOURCES = main.cpp |
| 22 | SOURCES += $(IMGUI_DIR)/backends/imgui_impl_sdl3.cpp $(IMGUI_DIR)/backends/imgui_impl_wgpu.cpp |
| 23 | SOURCES += $(IMGUI_DIR)/imgui.cpp $(IMGUI_DIR)/imgui_demo.cpp $(IMGUI_DIR)/imgui_draw.cpp $(IMGUI_DIR)/imgui_tables.cpp $(IMGUI_DIR)/imgui_widgets.cpp |
| 24 | OBJS = $(addsuffix .o, $(basename $(notdir $(SOURCES)))) |
| 25 | UNAME_S := $(shell uname -s) |
| 26 | CPPFLAGS = |
| 27 | LDFLAGS = |
| 28 | EMS = |
| 29 | |
| 30 | ##--------------------------------------------------------------------- |
| 31 | ## EMSCRIPTEN OPTIONS |
| 32 | ##--------------------------------------------------------------------- |
| 33 | |
| 34 | # ("EMS" options gets added to both CPPFLAGS and LDFLAGS, whereas some options are for linker only) |
| 35 | EMS += -s USE_SDL=3 |
| 36 | EMS += -s DISABLE_EXCEPTION_CATCHING=1 |
| 37 | LDFLAGS += -s WASM=1 |
| 38 | LDFLAGS += -s ALLOW_MEMORY_GROWTH=1 |
| 39 | LDFLAGS += -s ASYNCIFY=1 |
| 40 | LDFLAGS += -s NO_EXIT_RUNTIME=0 |
| 41 | LDFLAGS += -s ASSERTIONS=1 |
| 42 | |
| 43 | # Using Dawn-based WebGPU port (requires Emscripten >= 4.0.10) |
| 44 | EMS += --use-port=emdawnwebgpu |
| 45 | LDFLAGS += --use-port=emdawnwebgpu |
| 46 | |
| 47 | # Build as single file (binary text encoded in .html file) |
| 48 | #LDFLAGS += -sSINGLE_FILE |
| 49 | |
| 50 | # Emscripten allows preloading a file or folder to be accessible at runtime. |
| 51 | # The Makefile for this example project suggests embedding the misc/fonts/ folder into our application, it will then be accessible as "/fonts" |
| 52 | # See documentation for more details: https://emscripten.org/docs/porting/files/packaging_files.html |
| 53 | # (Default value is 0. Set to 1 to enable file-system and include the misc/fonts/ folder as part of the build.) |
| 54 | USE_FILE_SYSTEM ?= 0 |
| 55 | ifeq ($(USE_FILE_SYSTEM), 0) |
| 56 | LDFLAGS += -s NO_FILESYSTEM=1 |
| 57 | CPPFLAGS += -DIMGUI_DISABLE_FILE_FUNCTIONS |
| 58 | endif |
| 59 | ifeq ($(USE_FILE_SYSTEM), 1) |
| 60 | LDFLAGS += --no-heap-copy --preload-file ../../misc/fonts@/fonts |
| 61 | endif |
| 62 | |
| 63 | ##--------------------------------------------------------------------- |
| 64 | ## FINAL BUILD FLAGS |
| 65 | ##--------------------------------------------------------------------- |
| 66 | |
| 67 | CPPFLAGS += -I$(IMGUI_DIR) -I$(IMGUI_DIR)/backends |
| 68 | #CPPFLAGS += -g |
| 69 | CPPFLAGS += -Wall -Wformat -Os $(EMS) |
| 70 | LDFLAGS += --shell-file ../libs/emscripten/shell_minimal.html |
| 71 | LDFLAGS += $(EMS) |
| 72 | |
| 73 | ##--------------------------------------------------------------------- |
| 74 | ## BUILD RULES |
| 75 | ##--------------------------------------------------------------------- |
| 76 | |
| 77 | %.o:%.cpp |
| 78 | $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $< |
| 79 | |
| 80 | %.o:$(IMGUI_DIR)/%.cpp |
| 81 | $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $< |
| 82 | |
| 83 | %.o:$(IMGUI_DIR)/backends/%.cpp |
| 84 | $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $< |
| 85 | |
| 86 | all: $(EXE) |
| 87 | @echo Build complete for $(EXE) |
| 88 | |
| 89 | $(WEB_DIR): |
| 90 | mkdir $@ |
| 91 | |
| 92 | serve: all |
| 93 | python3 -m http.server -d $(WEB_DIR) |
| 94 | |
| 95 | $(EXE): $(OBJS) $(WEB_DIR) |
| 96 | $(CXX) -o $@ $(OBJS) $(LDFLAGS) |
| 97 | |
| 98 | clean: |
| 99 | rm -f $(EXE) $(OBJS) $(WEB_DIR)/*.js $(WEB_DIR)/*.wasm $(WEB_DIR)/*.wasm.pre |