From 1fa6361401a3d1437f2cb5443c0f57c98f72f650 Mon Sep 17 00:00:00 2001 From: DaniTheSkunk <> Date: Wed, 21 Dec 2022 14:01:04 +0000 Subject: [PATCH] support emscripten compile --- em.ini | 9 +++++++++ meson.build | 27 ++++++++++++++++++++++---- prebuildem.bat | 2 ++ src/gl.h | 6 ++++++ src/test.c | 19 ++++++++++++++---- subprojects/glew-2.1.0/meson.build | 6 +++++- subprojects/zlib-1.2.13/CMakeLists.txt | 10 +++++----- 7 files changed, 65 insertions(+), 14 deletions(-) create mode 100644 em.ini create mode 100644 prebuildem.bat diff --git a/em.ini b/em.ini new file mode 100644 index 0000000..12157f5 --- /dev/null +++ b/em.ini @@ -0,0 +1,9 @@ +[binaries] +c = ['c:/dev/_tools/emsdk/upstream/emscripten/emcc.bat'] +ar =['c:/dev/_tools/emsdk/upstream/emscripten/emar.bat'] + +[host_machine] +system = 'emscripten' +cpu_family = 'wasm' +cpu = 'wasm' +endian = 'little' \ No newline at end of file diff --git a/meson.build b/meson.build index b0c7f48..7eed277 100644 --- a/meson.build +++ b/meson.build @@ -1,4 +1,10 @@ -project('skunkworks', 'c', default_options: ['buildtype=release']) +project('skunkworks', 'c', + default_options: [ + 'buildtype=release', + 'strip=true', + 'debug=false' + ] +) cmake = import('cmake') @@ -20,9 +26,18 @@ opt.add_cmake_defines({ opt.append_compile_args('c', '-I../subprojects/zlib-1.2.13', '-Isubprojects/zlib-1.2.13/__CMake_build') libpng = cmake.subproject('lpng1639', options: opt).dependency('png_static') -libglfw = cmake.subproject('glfw-3.3.8').dependency('glfw') +opt = cmake.subproject_options() +opt.add_cmake_defines({'GLFW_BUILD_DOCS': false}) -libglew = subproject('glew-2.1.0').get_variable('libglew_dep') + +if host_machine.system() == 'emscripten' + #libglfw = meson.get_compiler('c').find_library('glfw') + deps = [libz, libpng] +else + libglfw = cmake.subproject('glfw-3.3.8', options: opt).dependency('glfw') + libglew = subproject('glew-2.1.0').get_variable('libglew_dep') + deps = [libz, libpng, libglew, libglfw] +endif skunk_sources = [ 'src/skunkworks.c', @@ -31,4 +46,8 @@ skunk_sources = [ 'src/window.c' ] -executable('skunktest', skunk_sources, include_directories: 'include', dependencies: [libz, libpng, libglew, libglfw]) \ No newline at end of file +if host_machine.system() == 'emscripten' + executable('skunktest', skunk_sources, include_directories: 'include', dependencies: deps, link_args: ['-lglfw', '-lGL', '-s', 'USE_GLFW=3'], name_suffix: 'html') +else + executable('skunktest', skunk_sources, include_directories: 'include', dependencies: deps) +endif \ No newline at end of file diff --git a/prebuildem.bat b/prebuildem.bat new file mode 100644 index 0000000..05df587 --- /dev/null +++ b/prebuildem.bat @@ -0,0 +1,2 @@ +@echo off +meson setup --cross-file=em.ini --prefer-static buildem diff --git a/src/gl.h b/src/gl.h index 9cb7825..11f8168 100644 --- a/src/gl.h +++ b/src/gl.h @@ -3,3 +3,9 @@ #include #pragma clang diagnostic pop #include + +#ifdef __EMSCRIPTEN__ +#include +#define GL_GLEXT_PROTOTYPES +#define EGL_EGLEXT_PROTOTYPES +#endif diff --git a/src/test.c b/src/test.c index 174fd05..0191231 100644 --- a/src/test.c +++ b/src/test.c @@ -5,19 +5,30 @@ #include "gl.h" +struct window *win; + +void tick(); + int main(int argc, char *argv[]) { - struct window *win; printf("hello world\n"); win = sw_window_create(sw_vec2i(640, 480), "Skunkworks"); png_sig_cmp(0, 0, 0); glClearColor(0.1f, 0.2f, 0.3f, 1.f); +#ifdef __EMSCRIPTEN__ + emscripten_set_main_loop(tick, 0, 1); +#else while (!glfwWindowShouldClose(win->_window)) { - glClear(GL_COLOR_BUFFER_BIT); - glfwSwapBuffers(win->_window); - glfwPollEvents(); + tick(); } +#endif return 0; } + +void tick() { + glClear(GL_COLOR_BUFFER_BIT); + glfwSwapBuffers(win->_window); + glfwPollEvents(); +} diff --git a/subprojects/glew-2.1.0/meson.build b/subprojects/glew-2.1.0/meson.build index 5eb3afd..2285602 100644 --- a/subprojects/glew-2.1.0/meson.build +++ b/subprojects/glew-2.1.0/meson.build @@ -3,6 +3,10 @@ project('glew', 'c') gl = dependency('gl') inc = include_directories('include') c_args = ['-DGLEW_STATIC'] -lib = static_library('glew', 'src/glew.c', include_directories: inc, c_args: c_args, dependencies: gl) +if host_machine.system() == 'emscripten' + lib = static_library('glew', 'src/glew.c', include_directories: inc, c_args: c_args) +else + lib = static_library('glew', 'src/glew.c', include_directories: inc, c_args: c_args, dependencies: gl) +endif libglew_dep = declare_dependency(include_directories: inc, link_with: lib, compile_args: c_args) \ No newline at end of file diff --git a/subprojects/zlib-1.2.13/CMakeLists.txt b/subprojects/zlib-1.2.13/CMakeLists.txt index b412dc7..a08d1a3 100644 --- a/subprojects/zlib-1.2.13/CMakeLists.txt +++ b/subprojects/zlib-1.2.13/CMakeLists.txt @@ -147,10 +147,10 @@ if(MINGW) set(ZLIB_DLL_SRCS ${CMAKE_CURRENT_BINARY_DIR}/zlib1rc.obj) endif(MINGW) -add_library(zlib SHARED ${ZLIB_SRCS} ${ZLIB_DLL_SRCS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS}) +#add_library(zlib SHARED ${ZLIB_SRCS} ${ZLIB_DLL_SRCS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS}) add_library(zlibstatic STATIC ${ZLIB_SRCS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS}) -set_target_properties(zlib PROPERTIES DEFINE_SYMBOL ZLIB_DLL) -set_target_properties(zlib PROPERTIES SOVERSION 1) +#set_target_properties(zlib PROPERTIES DEFINE_SYMBOL ZLIB_DLL) +#set_target_properties(zlib PROPERTIES SOVERSION 1) if(NOT CYGWIN) # This property causes shared libraries on Linux to have the full version @@ -160,7 +160,7 @@ if(NOT CYGWIN) # # This has no effect with MSVC, on that platform the version info for # the DLL comes from the resource file win32/zlib1.rc - set_target_properties(zlib PROPERTIES VERSION ${ZLIB_FULL_VERSION}) + #set_target_properties(zlib PROPERTIES VERSION ${ZLIB_FULL_VERSION}) endif() if(UNIX) @@ -175,7 +175,7 @@ elseif(BUILD_SHARED_LIBS AND WIN32) endif() if(NOT SKIP_INSTALL_LIBRARIES AND NOT SKIP_INSTALL_ALL ) - install(TARGETS zlib zlibstatic + install(TARGETS zlibstatic RUNTIME DESTINATION "${INSTALL_BIN_DIR}" ARCHIVE DESTINATION "${INSTALL_LIB_DIR}" LIBRARY DESTINATION "${INSTALL_LIB_DIR}" )