Skip to content
This repository has been archived by the owner on Sep 16, 2024. It is now read-only.

Commit

Permalink
Fix web build
Browse files Browse the repository at this point in the history
  • Loading branch information
OFFTKP committed Jan 3, 2024
1 parent 89ef2bf commit 4ba7b52
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 57 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/Web.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Build Web & Deploy to GH
on: [push]
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: 'true'
- name: Setup emsdk
uses: mymindstorm/setup-emsdk@v10
with:
version: 2.0.21
actions-cache-folder: 'emsdk-cache'
- name: Build
run: |
emcmake cmake -B build
cmake --build build --config Release
mkdir website
mv build/hydra.html build/index.html
- name: Deploy
uses: JamesIves/github-pages-deploy-action@v4
with:
branch: gh-pages
folder: build
target-folder: commit/${{ github.sha }}
force: false
clean: false
41 changes: 22 additions & 19 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ project(hydra

add_executable(hydra)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD 20 CACHE STRING "" FORCE)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(WITH_APPS OFF CACHE BOOL "" FORCE)
set(BUILD_SHARED_LIBS OFF)
set(BUILD_STATIC_LIBS ON)
set(DPP_BUILD_TEST OFF)
Expand Down Expand Up @@ -57,7 +57,9 @@ elseif(HYDRA_MACOS)
# "-framework Metal"
)
elseif(HYDRA_LINUX)
set(HYDRA_LIBRARIES ${HYDRA_LIBRARIES} OpenGL::GL)
elseif(HYDRA_FREEBSD)
set(HYDRA_LIBRARIES ${HYDRA_LIBRARIES} OpenGL::GL)
elseif(HYDRA_ANDROID)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(HYDRA_LIBRARIES ${HYDRA_LIBRARIES} GLESv3 EGL OpenSLES log android)
Expand All @@ -77,15 +79,19 @@ elseif(HYDRA_IOS)
# "-framework GameController"
)
elseif(HYDRA_WEB)
set_target_properties(hydra PROPERTIES LINK_FLAGS "-s TOTAL_MEMORY=1024MB -lidbfs.js -s ELIMINATE\_DUPLICATE\_FUNCTIONS=1 -flto -s USE_CLOSURE_COMPILER=0 --closure 0")
set(LONG_INT 8) # for openssl
set(CMAKE_EXECUTABLE_SUFFIX ".html")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -sEXPORTED_RUNTIME_METHODS=ccall")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -sEXPORTED_RUNTIME_METHODS=ccall")
set_target_properties(hydra PROPERTIES LINK_FLAGS "-sALLOW_MEMORY_GROWTH -s TOTAL_MEMORY=192MB -lidbfs.js -s ELIMINATE\_DUPLICATE\_FUNCTIONS=1 -flto -s USE_CLOSURE_COMPILER=0 --closure 0")
else()
message(FATAL_ERROR "Unsupported platform")
endif()

# Thanks SkyEmu for most of these configurations
if (MSVC)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /MT /Ox /Ob2 /Oi /Ot /GT /GF /GS- /fp:fast /fp:except- /MP /sdl- /EHsc /D_SECURE_SCL=0 /D_SCL_SECURE_NO_WARNINGS /D_ITERATOR_DEBUG_LEVEL=0 /D_HAS_ITERATOR_DEBUGGING=0")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MT /Ox /Ob2 /Oi /Ot /GT /GF /GS- /fp:fast /fp:except- /MP /sdl- /EHsc /D_SECURE_SCL=0 /D_SCL_SECURE_NO_WARNINGS /D_ITERATOR_DEBUG_LEVEL=0 /D_HAS_ITERATOR_DEBUGGING=0 ")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MT /Ox /Ob2 /Oi /Ot /GT /GF /GS- /fp:fast /fp:except- /MP /sdl- /EHsc /D_SECURE_SCL=0 /D_SCL_SECURE_NO_WARNINGS /D_ITERATOR_DEBUG_LEVEL=0 /D_HAS_ITERATOR_DEBUGGING=0")
elseif(NOT HYDRA_WEB)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O3")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O3")
Expand All @@ -102,8 +108,16 @@ add_subdirectory(vendored/fmt)
add_subdirectory(vendored/argparse)
add_subdirectory(vendored/SDL)

set(HYDRA_INCLUDES ${HYDRA_INCLUDES}
${CMAKE_CURRENT_BINARY_DIR}/vendored/openssl/include
vendored/openssl/include
)
add_subdirectory(vendored/openssl)
set_property(TARGET ssl PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded")
set_property(TARGET crypto PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded")
set(HYDRA_LIBRARIES ${HYDRA_LIBRARIES} ssl crypto)

if(NOT HYDRA_WEB)
set(WITH_APPS OFF CACHE BOOL "" FORCE) # don't build executable
set(CURL_USE_LIBSSH2 OFF)
set(CURL_USE_LIBPSL OFF)
set(CURL_USE_OPENSSL ON)
Expand All @@ -112,29 +126,19 @@ if(NOT HYDRA_WEB)
set(CURL_ENABLE_EXPORT_TARGET OFF)
set(BUILD_TESTING OFF)
set(BUILD_CURL_EXE OFF)
set(BUILD_SHARED_LIBS OFF)
set(BUILD_STATIC_LIBS ON)
if (WIN32)
set(CURL_STATIC_CRT ON)
endif()
add_definitions(-DCURL_STATICLIB)
add_subdirectory(vendored/openssl)
add_subdirectory(vendored/curl)
set_property(TARGET ssl PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded")
set_property(TARGET crypto PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded")
set_property(TARGET libcurl_static PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded")
set(HYDRA_INCLUDES ${HYDRA_INCLUDES}
${CMAKE_CURRENT_BINARY_DIR}/vendored/openssl/include
vendored/openssl/include
vendored/curl/include
)
set(HYDRA_LIBRARIES ${HYDRA_LIBRARIES} libcurl_static
ssl
crypto
)
set(HYDRA_LIBRARIES ${HYDRA_LIBRARIES} libcurl_static)
endif()

if(NOT HYDRA_IOS)
if(NOT HYDRA_IOS AND NOT HYDRA_WEB)
find_package(OpenGL REQUIRED)
endif()

Expand Down Expand Up @@ -193,7 +197,6 @@ set(HYDRA_INCLUDES

set(HYDRA_LIBRARIES
${HYDRA_LIBRARIES}
OpenGL::GL
SDL3-static
fmt
)
Expand Down Expand Up @@ -264,6 +267,6 @@ target_include_directories(hydra PRIVATE ${HYDRA_INCLUDES})
target_compile_definitions(hydra PRIVATE HYDRA_VERSION="${PROJECT_VERSION}")
target_compile_definitions(hydra PRIVATE ${HYDRA_DEFINITIONS})

if (HYDRA_LINUX OR HYDRA_FREEBSD OR HYDRA_MACOS OR HYDRA_ANDROID)
if (HYDRA_LINUX OR HYDRA_FREEBSD OR HYDRA_MACOS OR HYDRA_ANDROID OR HYDRA_WEB)
target_compile_definitions(hydra PRIVATE HYDRA_LIBDL)
endif()
2 changes: 1 addition & 1 deletion core
Submodule core updated 1 files
+3 −3 include/hydra/core.hxx
10 changes: 3 additions & 7 deletions include/corewrapper.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <cstring>
#include <string>
#include <vector>
#if defined(HYDRA_LINUX) || defined(HYDRA_MACOS)
#if defined(HYDRA_LIBDL)
#include <dlfcn.h>
#elif defined(HYDRA_WINDOWS)
#include <windows.h>
Expand Down Expand Up @@ -39,8 +39,6 @@ namespace hydra
return dlsym(handle, name);
#elif defined(HYDRA_WINDOWS)
return (void*)GetProcAddress((HMODULE)handle, name);
#elif defined(HYDRA_WII)
ELFIO::elfio* reader = (ELFIO::elfio*)handle;
#else
#pragma message("dynlib_get_symbol not implemented for this platform")
#error dynlib_get_symbol not implemented for this platform
Expand All @@ -54,8 +52,6 @@ namespace hydra
dlclose(handle);
#elif defined(HYDRA_WINDOWS)
FreeLibrary((HMODULE)handle);
#elif defined(HYDRA_WII)
delete (ELFIO::elfio*)handle;
#else
#pragma message("dynlib_close not implemented for this platform")
#error dynlib_close not implemented for this platform
Expand All @@ -64,7 +60,7 @@ namespace hydra

inline std::string dynlib_get_extension()
{
#if defined(HYDRA_LINUX) || defined(HYDRA_ANDROID) || defined(HYDRA_FREEBSD)
#if defined(HYDRA_LINUX) || defined(HYDRA_ANDROID) || defined(HYDRA_FREEBSD) || defined(HYDRA_WEB)
return ".so";
#elif defined(HYDRA_MACOS)
return ".dylib";
Expand All @@ -79,7 +75,7 @@ namespace hydra

inline std::string dynlib_get_error()
{
#if defined(HYDRA_LINUX) || defined(HYDRA_MACOS) || defined(HYDRA_ANDROID) || defined(HYDRA_FREEBSD)
#if defined(HYDRA_LIBDL)
return dlerror();
#elif defined(HYDRA_WINDOWS)
// DWORD error = GetLastError();
Expand Down
31 changes: 5 additions & 26 deletions include/hsystem.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,11 @@
#define HYDRA_ARM64
#elif defined(__arm__)
#define HYDRA_ARM
#elif defined(__EMSCRIPTEN__)
#else
#pragma message("Unknown architecture")
#endif

#ifdef HYDRA_WINDOWS
#include <immintrin.h>
#include <intrin.h>
#pragma intrinsic(_mul128)
#else
#ifdef HYDRA_X86_64
#include <x86intrin.h>
#elif defined(HYDRA_ARM)
#include <arm_acle.h>
#endif
#endif

#ifdef __clang__
#ifdef HYDRA_MACOS
#define hydra_inline inline
#else
#define hydra_inline [[clang::always_inline]] inline
#endif
#elif defined(__GNUC__)
#define hydra_inline __always_inline inline
#elif defined(_MSC_VER)
#define hydra_inline __forceinline inline
#else
#define hydra_inline inline
#endif

inline std::string hydra_os()
{
std::string ret;
Expand All @@ -52,6 +27,10 @@ inline std::string hydra_os()
ret = "macOS";
#elif defined(HYDRA_FREEBSD)
ret = "FreeBSD";
#elif defined(HYDRA_WEB)
return "Web WASM";
#elif defined(HYDRA_IOS)
ret = "iOS";
#else
ret = "Unknown";
#endif
Expand Down
45 changes: 45 additions & 0 deletions include/sanity.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#pragma once

// Sanity checks to verify at compile time that definitions passed by CMake are correct

#ifdef HYDRA_WEB
#if !defined(EMSCRIPTEN) && !defined(__EMSCRIPTEN__)
#error "HYDRA_WEB defined but EMSCRIPTEN not defined"
#endif
#endif

#ifdef HYDRA_WINDOWS
#if !defined(_WIN32) && !defined(_WIN64)
#error "HYDRA_WINDOWS defined but _WIN32 or _WIN64 not defined"
#endif
#endif

#ifdef HYDRA_LINUX
#if !defined(__linux__)
#error "HYDRA_LINUX defined but __linux__ not defined"
#endif
#if defined(__ANDROID__)
#error "HYDRA_LINUX defined but __ANDROID__ defined"
#endif
#if defined(__FreeBSD__)
#error "HYDRA_LINUX defined but __FreeBSD__ defined"
#endif
#endif

#ifdef HYDRA_FREEBSD
#if !defined(__FreeBSD__)
#error "HYDRA_FREEBSD defined but __FreeBSD__ not defined"
#endif
#endif

#if defined(HYDRA_MACOS) || defined(HYDRA_IOS)
#if !defined(__APPLE__)
#error "HYDRA_MACOS defined but __APPLE__ not defined"
#endif
#endif

#ifdef HYDRA_ANDROID
#if !defined(__ANDROID__)
#error "HYDRA_ANDROID defined but __ANDROID__ not defined"
#endif
#endif
1 change: 0 additions & 1 deletion qt/downloaderwindow.cxx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "downloaderwindow.hxx"
#include "download.hxx"
#include "hsystem.hxx"
#include "observer.hxx"
#include "update.hxx"
#include <cstdint>
Expand Down
2 changes: 0 additions & 2 deletions server/server.hxx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#include <hsystem.hxx>

#if defined(HYDRA_LINUX) || defined(HYDRA_MACOS)
#include <netinet/in.h>
#include <sys/socket.h>
Expand Down
28 changes: 28 additions & 0 deletions src/app.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,31 @@
#include <imgui/backends/imgui_impl_sdl3.h>
#include <imgui/imgui.h>
#include <memory>
#include <sanity.hxx>
#include <SDL3/SDL.h>
#include <SDL3/SDL_opengl.h>
#include <SDL3/SDL_video.h>
#include <settings.hxx>

#ifdef HYDRA_WEB
#include <emscripten.h>
#include <functional>
static std::function<void()> MainLoopForEmscriptenP;

static void MainLoopForEmscripten()
{
MainLoopForEmscriptenP();
}

#define EMSCRIPTEN_MAINLOOP_BEGIN MainLoopForEmscriptenP = [&]()
#define EMSCRIPTEN_MAINLOOP_END \
; \
emscripten_set_main_loop(MainLoopForEmscripten, 0, true)
#else
#define EMSCRIPTEN_MAINLOOP_BEGIN
#define EMSCRIPTEN_MAINLOOP_END
#endif

int imgui_main(int argc, char** argv)
{
// TODO: Joystick
Expand Down Expand Up @@ -73,7 +93,12 @@ int imgui_main(int argc, char** argv)

bool done = false;
std::unique_ptr<MainWindow> main_window = std::make_unique<MainWindow>();
#ifdef HYDRA_WEB
io.IniFilename = nullptr;
EMSCRIPTEN_MAINLOOP_BEGIN
#else
while (!done)
#endif
{
SDL_Event event;
while (SDL_PollEvent(&event))
Expand Down Expand Up @@ -115,6 +140,9 @@ int imgui_main(int argc, char** argv)
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
SDL_GL_SwapWindow(window);
}
#ifdef HYDRA_WEB
EMSCRIPTEN_MAINLOOP_END;
#endif

ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplSDL3_Shutdown();
Expand Down
2 changes: 1 addition & 1 deletion src/main.cxx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#include <argparse/argparse.h>
#include <bot.hxx>
#include <filesystem>
#include <iostream>
#ifdef HYDRA_FRONTEND_QT
#include "mainwindow.hxx"
#include <QApplication>
#include <QSurfaceFormat>
#endif
#include <settings.hxx>
#include <update.hxx>

// clang-format off

Expand Down

0 comments on commit 4ba7b52

Please sign in to comment.