Makefile.emscripten 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. CC = emcc
  16. CXX = em++
  17. WEB_DIR = web
  18. EXE = $(WEB_DIR)/index.html
  19. IMGUI_DIR = ../..
  20. SOURCES = main.cpp
  21. 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
  22. SOURCES += $(IMGUI_DIR)/backends/imgui_impl_glfw.cpp $(IMGUI_DIR)/backends/imgui_impl_wgpu.cpp
  23. OBJS = $(addsuffix .o, $(basename $(notdir $(SOURCES))))
  24. UNAME_S := $(shell uname -s)
  25. CPPFLAGS =
  26. LDFLAGS =
  27. EMS =
  28. ##---------------------------------------------------------------------
  29. ## EMSCRIPTEN OPTIONS
  30. ##---------------------------------------------------------------------
  31. # ("EMS" options gets added to both CPPFLAGS and LDFLAGS, whereas some options are for linker only)
  32. # Note: For glfw, we use emscripten-glfw port (contrib.glfw3) instead of (-s USE_GLFW=3) to get a better support for High DPI displays.
  33. EMS += -s DISABLE_EXCEPTION_CATCHING=1 --use-port=contrib.glfw3
  34. LDFLAGS += -s WASM=1
  35. LDFLAGS += -s ALLOW_MEMORY_GROWTH=1
  36. LDFLAGS += -s ASYNCIFY=1
  37. LDFLAGS += -s NO_EXIT_RUNTIME=0
  38. LDFLAGS += -s ASSERTIONS=1
  39. # (1) Using legacy WebGPU implementation (Emscripten < 4.0.10)
  40. #EMS += -DIMGUI_IMPL_WEBGPU_BACKEND_WGPU
  41. #LDFLAGS += -s USE_WEBGPU=1
  42. # or (2) Using newer Dawn-based WebGPU port (Emscripten >= 4.0.10)
  43. EMS += --use-port=emdawnwebgpu
  44. LDFLAGS += --use-port=emdawnwebgpu
  45. # Build as single file (binary text encoded in .html file)
  46. #LDFLAGS += -sSINGLE_FILE
  47. # Emscripten allows preloading a file or folder to be accessible at runtime.
  48. # The Makefile for this example project suggests embedding the misc/fonts/ folder into our application, it will then be accessible as "/fonts"
  49. # See documentation for more details: https://emscripten.org/docs/porting/files/packaging_files.html
  50. # (Default value is 0. Set to 1 to enable file-system and include the misc/fonts/ folder as part of the build.)
  51. USE_FILE_SYSTEM ?= 0
  52. ifeq ($(USE_FILE_SYSTEM), 0)
  53. LDFLAGS += -s NO_FILESYSTEM=1
  54. CPPFLAGS += -DIMGUI_DISABLE_FILE_FUNCTIONS
  55. endif
  56. ifeq ($(USE_FILE_SYSTEM), 1)
  57. LDFLAGS += --no-heap-copy --preload-file ../../misc/fonts@/fonts
  58. endif
  59. ##---------------------------------------------------------------------
  60. ## FINAL BUILD FLAGS
  61. ##---------------------------------------------------------------------
  62. CPPFLAGS += -I$(IMGUI_DIR) -I$(IMGUI_DIR)/backends
  63. #CPPFLAGS += -g
  64. CPPFLAGS += -Wall -Wformat -Os $(EMS)
  65. LDFLAGS += --shell-file ../libs/emscripten/shell_minimal.html
  66. LDFLAGS += $(EMS)
  67. ##---------------------------------------------------------------------
  68. ## BUILD RULES
  69. ##---------------------------------------------------------------------
  70. %.o:%.cpp
  71. $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $<
  72. %.o:$(IMGUI_DIR)/%.cpp
  73. $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $<
  74. %.o:$(IMGUI_DIR)/backends/%.cpp
  75. $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $<
  76. all: $(EXE)
  77. @echo Build complete for $(EXE)
  78. $(WEB_DIR):
  79. mkdir $@
  80. serve: all
  81. python3 -m http.server -d $(WEB_DIR)
  82. $(EXE): $(OBJS) $(WEB_DIR)
  83. $(CXX) -o $@ $(OBJS) $(LDFLAGS)
  84. clean:
  85. rm -f $(EXE) $(OBJS) $(WEB_DIR)/*.js $(WEB_DIR)/*.wasm $(WEB_DIR)/*.wasm.pre