Makefile.emscripten 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #
  2. # Makefile to use with GLFW+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_opengl3.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' in LDFLAGS) 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 -s ALLOW_MEMORY_GROWTH=1 -s NO_EXIT_RUNTIME=0 -s ASSERTIONS=1
  35. # Build as single file (binary text encoded in .html file)
  36. #LDFLAGS += -sSINGLE_FILE
  37. # Uncomment next line to fix possible rendering bugs with Emscripten version older then 1.39.0 (https://github.com/ocornut/imgui/issues/2877)
  38. #EMS += -s BINARYEN_TRAP_MODE=clamp
  39. #EMS += -s SAFE_HEAP=1 ## Adds overhead
  40. # Emscripten allows preloading a file or folder to be accessible at runtime.
  41. # The Makefile for this example project suggests embedding the misc/fonts/ folder into our application, it will then be accessible as "/fonts"
  42. # See documentation for more details: https://emscripten.org/docs/porting/files/packaging_files.html
  43. # (Default value is 0. Set to 1 to enable file-system and include the misc/fonts/ folder as part of the build.)
  44. USE_FILE_SYSTEM ?= 0
  45. ifeq ($(USE_FILE_SYSTEM), 0)
  46. LDFLAGS += -s NO_FILESYSTEM=1
  47. CPPFLAGS += -DIMGUI_DISABLE_FILE_FUNCTIONS
  48. endif
  49. ifeq ($(USE_FILE_SYSTEM), 1)
  50. LDFLAGS += --no-heap-copy --preload-file ../../misc/fonts@/fonts
  51. endif
  52. ##---------------------------------------------------------------------
  53. ## FINAL BUILD FLAGS
  54. ##---------------------------------------------------------------------
  55. CPPFLAGS += -I$(IMGUI_DIR) -I$(IMGUI_DIR)/backends
  56. #CPPFLAGS += -g
  57. CPPFLAGS += -Wall -Wformat -Os $(EMS)
  58. LDFLAGS += --shell-file ../libs/emscripten/shell_minimal.html
  59. LDFLAGS += $(EMS)
  60. ##---------------------------------------------------------------------
  61. ## BUILD RULES
  62. ##---------------------------------------------------------------------
  63. %.o:%.cpp
  64. $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $<
  65. %.o:$(IMGUI_DIR)/%.cpp
  66. $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $<
  67. %.o:$(IMGUI_DIR)/backends/%.cpp
  68. $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $<
  69. all: $(EXE)
  70. @echo Build complete for $(EXE)
  71. $(WEB_DIR):
  72. mkdir $@
  73. serve: all
  74. python3 -m http.server -d $(WEB_DIR)
  75. $(EXE): $(OBJS) $(WEB_DIR)
  76. $(CXX) -o $@ $(OBJS) $(LDFLAGS)
  77. clean:
  78. rm -rf $(OBJS) $(WEB_DIR)