diff --git a/.github/cache/action.yml b/.github/cache/action.yml deleted file mode 100644 index e340d98..0000000 --- a/.github/cache/action.yml +++ /dev/null @@ -1,18 +0,0 @@ -name: cache -descritpion: Set up cache for vcpkg -inputs: - path: - description: "The path to cache" - required: true - triplet: - description: "The vcpkg triplet to cache" - required: true -runs: - using: "composite" - steps: - - name: Cache vcpkg - uses: actions/cache@v4 - with: - path: ${{ inputs.path }} - key: vcpkg-${{ inputs.triplet }}-${{ hashFiles('vcpkg.json') }} - diff --git a/.github/scripts/osx_setup.sh b/.github/scripts/osx_setup.sh index 0a3f6a8..b907d28 100755 --- a/.github/scripts/osx_setup.sh +++ b/.github/scripts/osx_setup.sh @@ -1,2 +1 @@ -brew install autoconf autoconf-archive automake libtool ninja -/Users/runner/work/vcpkg/bootstrap-vcpkg.sh \ No newline at end of file +brew install autoconf autoconf-archive automake libtool \ No newline at end of file diff --git a/.github/scripts/ubuntu_setup.sh b/.github/scripts/ubuntu_setup.sh index cfc2eca..c7fb32d 100755 --- a/.github/scripts/ubuntu_setup.sh +++ b/.github/scripts/ubuntu_setup.sh @@ -27,26 +27,9 @@ # causing unpredictable behavior and build failures. # echo "==============================================================================" -echo "Freeing up disk space on CI system" +echo "Installing Ubuntu dependencies" echo "==============================================================================" -echo "Listing 100 largest packages" -dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -n | tail -n 100 -df -h -echo "Removing large packages" -sudo apt-get remove -y '^ghc-8.*' -sudo apt-get remove -y '^dotnet-.*' -sudo apt-get remove -y '^llvm-.*' -sudo apt-get remove -y 'php.*' -sudo apt-get remove -y azure-cli google-cloud-sdk hhvm google-chrome-stable firefox powershell mono-devel -sudo apt-get autoremove -y -sudo apt-get clean -echo "Removing large directories" -# deleting 15GB -rm -rf /usr/share/dotnet/ -rm -rf /opt/hostedtoolcache -rm -rf /opt/ghc -sudo rm -rf /usr/local/lib/android df -h sudo apt-get update sudo apt-get install -y \ @@ -76,7 +59,9 @@ sudo apt-get install -y \ libxcb-xinerama0-dev \ libxcb-xkb-dev \ libxi-dev \ + libxtst-dev \ libxkbcommon-dev \ libxkbcommon-x11-dev \ - libxrender-dev + libxrender-dev \ + libxrandr-dev df -h \ No newline at end of file diff --git a/.github/vcpkg/action.yml b/.github/vcpkg/action.yml deleted file mode 100644 index 648ac02..0000000 --- a/.github/vcpkg/action.yml +++ /dev/null @@ -1,25 +0,0 @@ -name: vcpkg -descritpion: Bootstrap and install dependencies with vcpkg -inputs: - os: - description: "The operating system to run the job on" - required: true - triplet: - description: "The vcpkg triplet to use" - required: true - shell: - description: "The shell to use for running commands (optional)" - required: true -runs: - using: "composite" - steps: - - uses: actions/setup-python@v5 - with: - python-version: "3.10" - - name: Clone vcpkg - shell: ${{ inputs.shell }} - run: git clone https://github.com/microsoft/vcpkg - - name: Bootstrap vcpkg - shell: ${{ inputs.shell }} - run: python ./.github/vcpkg/setup.py --os ${{ inputs.os }} --triplet ${{ inputs.triplet }} - diff --git a/.github/vcpkg/setup.py b/.github/vcpkg/setup.py deleted file mode 100644 index 2fbb68d..0000000 --- a/.github/vcpkg/setup.py +++ /dev/null @@ -1,12 +0,0 @@ -#!/usr/bin/env python3 - -import argparse -import subprocess -import sys - -if __name__ == "__main__": - parser = argparse.ArgumentParser(description="Setup vcpkg for the project") - parser.add_argument("--vcpkg-root", type=str, default="vcpkg", help="Path to the vcpkg directory") - args = parser.parse_args() - - diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml deleted file mode 100644 index 1486dba..0000000 --- a/.github/workflows/build.yml +++ /dev/null @@ -1,67 +0,0 @@ -name: Build - -on: - push: - branches: [master] - -permissions: - contents: write - packages: write - -jobs: - build: - name: Build (${{ matrix.os }}) - runs-on: ${{ matrix.os }} - strategy: - fail-fast: false - matrix: - include: - - os: windows-latest - triplet: x64-windows - vcpkg_root: D:\a\vcpkg - - os: ubuntu-latest - triplet: x64-linux - vcpkg_root: /home/runner/vcpkg - - os: macos-latest - triplet: x64-osx - vcpkg_root: /Users/runner/work/vcpkg - env: - VCPKG_BINARY_SOURCES: >- - clear;http,http://${{secrets.TS_IPV4}}:12345,readwrite - - steps: - - name: Tailscale connect (MacOS) - if: matrix.os == 'macos-latest' - uses: tailscale/github-action@v3 - with: - oauth-client-id: ${{ secrets.TS_OAUTH_CLIENT_ID }} - oauth-secret: ${{ secrets.TS_OAUTH_SECRET }} - tags: tag:ci - - - name: Tailscale connect (Linux/Windows) - if: matrix.os != 'macos-latest' - uses: tailscale/github-action@v4 - with: - oauth-client-id: ${{ secrets.TS_OAUTH_CLIENT_ID }} - oauth-secret: ${{ secrets.TS_OAUTH_SECRET }} - tags: tag:ci - - - name: Checkout vcpkg - run: | - git clone --branch 2026.01.16 https://github.com/microsoft/vcpkg ${{ matrix.vcpkg_root }} - - - name: Checkout WebFrame - uses: actions/checkout@v4 - - - name: Ubuntu setup - if: matrix.os == 'ubuntu-latest' - run: ./.github/scripts/ubuntu_setup.sh - - - name: MacOS setup - if: matrix.os == 'macos-latest' - run: ./.github/scripts/osx_setup.sh - - - name: CMake build - run: | - cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=${{ matrix.vcpkg_root }}/scripts/buildsystems/vcpkg.cmake - cmake --build build \ No newline at end of file diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..6868c4f --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,102 @@ +name: WebFrame CI/CD Pipeline + +on: + push: + +permissions: + contents: write + packages: write + +jobs: + test: + name: Unit Tests + runs-on: ubuntu-latest + env: + VCPKG_BINARY_SOURCES: >- + clear;files,${{ github.workspace }}/vcpkg-test-cache,readwrite + steps: + - name: Checkout vcpkg + uses: actions/checkout@v6 + with: + repository: microsoft/vcpkg + path: vcpkg + ref: '2026.01.16' + + - name: Checkout code + uses: actions/checkout@v6 + with: + repository: maxtek6/WebFrame + path: WebFrame + ref: ${{ github.ref }} + + - name: "Restore VCPKG cache" + uses: actions/cache/restore@v4 + with: + path: ${{ github.workspace }}/vcpkg-test-cache + key: vcpkg-test-cache-${{ github.run_id }} + restore-keys: vcpkg-test-cache + + - name: CMake Build + run: | + cmake -S WebFrame -B build -DBUILD_TESTING=ON -DBUILD_RUNTIME=OFF -DVCPKG_ROOT=${{ github.workspace }}/vcpkg + cd build + make -j4 + make test + + - name: "Save VCPKG cache" + uses: actions/cache/save@v4 + with: + path: ${{ github.workspace }}/vcpkg-test-cache + key: vcpkg-test-cache-${{ github.run_id }} + + build: + needs: test + name: Build Runtime + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [windows-latest, ubuntu-latest, macos-latest] + env: + VCPKG_BINARY_SOURCES: >- + clear;files,${{ github.workspace }}/vcpkg-${{ matrix.os }},readwrite + steps: + - name: Checkout vcpkg + uses: actions/checkout@v6 + with: + repository: microsoft/vcpkg + path: vcpkg + ref: '2026.01.16' + + - name: Checkout code + uses: actions/checkout@v6 + with: + repository: maxtek6/WebFrame + path: WebFrame + ref: ${{ github.ref }} + + - name: "Restore VCPKG cache" + uses: actions/cache/restore@v5 + with: + path: ${{ github.workspace }}/vcpkg-${{ matrix.os }} + key: vcpkg-${{ matrix.os }}-${{ github.run_id }} + restore-keys: vcpkg-${{ matrix.os }} + + - name: Ubuntu setup + if: matrix.os == 'ubuntu-latest' + run: ./WebFrame/.github/scripts/ubuntu_setup.sh + + - name: MacOS setup + if: matrix.os == 'macos-latest' + run: ./WebFrame/.github/scripts/osx_setup.sh + + - name: CMake Build + run: | + cmake -S WebFrame -B build -DBUILD_TESTING=OFF -DBUILD_RUNTIME=ON -DVCPKG_ROOT=${{ github.workspace }}/vcpkg + cmake --build build + + - name: "Save VCPKG cache" + uses: actions/cache/save@v5 + with: + path: ${{ github.workspace }}/vcpkg-${{ matrix.os }} + key: vcpkg-${{ matrix.os }}-${{ github.run_id }} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 7bddcbb..fd1f6bf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,9 +1,39 @@ +option(BUILD_TESTING "Build testing" OFF) +option(BUILD_RUNTIME "Build runtime" ON) +option(BUILD_EXAMPLE "Build example" OFF) +option(BUILD_DOXYGEN "Build doxygen documentation" OFF) + +include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/webframe.cmake) +include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/configure.cmake) + +if(NOT VCPKG_ROOT) + set(VCPKG_ROOT $ENV{VCPKG_ROOT}) + if(NOT VCPKG_ROOT) + message(FATAL_ERROR "VCPKG_ROOT environment variable is not set. Please set it to the root of your vcpkg installation.") + endif() +endif() + +configure_vcpkg_features() + +set(CMAKE_TOOLCHAIN_FILE "${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" CACHE STRING "Vcpkg toolchain file") + cmake_minimum_required(VERSION 3.15) project(webframe VERSION 0.1.0) -#add_library(webframe_shell STATIC src/shell/shell.cpp) +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) + +configure_packages() + +add_subdirectory(src) -#add_library(webframe_desktop SHARED) +if(BUILD_TESTING) + enable_testing() + add_subdirectory(tests) +endif() -#add_library(webframe_browser SHARED) +if(BUILD_EXAMPLE) + add_subdirectory(example) +endif() \ No newline at end of file diff --git a/cmake/configure.cmake b/cmake/configure.cmake new file mode 100644 index 0000000..22965be --- /dev/null +++ b/cmake/configure.cmake @@ -0,0 +1,34 @@ +function(configure_vcpkg_features) + if(BUILD_TESTING) + list(APPEND VCPKG_MANIFEST_FEATURES "testing") + endif() + + if(BUILD_RUNTIME) + list(APPEND VCPKG_MANIFEST_FEATURES "runtime") + endif() + + if(BUILD_EXAMPLE) + list(APPEND VCPKG_MANIFEST_FEATURES "example") + endif() + + set(VCPKG_MANIFEST_FEATURES "${VCPKG_MANIFEST_FEATURES}" PARENT_SCOPE) +endfunction() + +function(configure_packages) + if(BUILD_TESTING) + find_package(maxtest CONFIG REQUIRED) + endif() + if(BUILD_RUNTIME) + find_package(Libevent CONFIG REQUIRED) + if(NOT WIN32) + find_package(CEF CONFIG REQUIRED) + endif() + find_package(wxWidgets CONFIG REQUIRED) + endif() + if(BUILD_EXAMPLE) + find_package(unofficial-sqlite3 REQUIRED) + find_package(yyjson CONFIG REQUIRED) + find_package(megamimes CONFIG REQUIRED) + find_package(hyperpage CONFIG REQUIRED) + endif() +endfunction() \ No newline at end of file diff --git a/cmake/webframe.cmake b/cmake/webframe.cmake new file mode 100644 index 0000000..0a081d0 --- /dev/null +++ b/cmake/webframe.cmake @@ -0,0 +1,61 @@ +function(webframe_add_runtime) + set(options) + set(oneValueArgs NAME) + set(multiValueArgs + SOURCES + PRIVATE_INCLUDE_DIRS + PUBLIC_INCLUDE_DIRS + PRIVATE_LINK_LIBRARIES + PUBLIC_LINK_LIBRARIES) + cmake_parse_arguments(RUNTIME + "${options}" + "${oneValueArgs}" + "${multiValueArgs}" + ${ARGN} + ) + add_library(webframe_${RUNTIME_NAME} STATIC ${RUNTIME_SOURCES}) + if(RUNTIME_PRIVATE_INCLUDE_DIRS) + target_include_directories(webframe_${RUNTIME_NAME} PRIVATE ${RUNTIME_PRIVATE_INCLUDE_DIRS}) + endif() + if(RUNTIME_PUBLIC_INCLUDE_DIRS) + target_include_directories(webframe_${RUNTIME_NAME} PUBLIC ${RUNTIME_PUBLIC_INCLUDE_DIRS}) + endif() + if(RUNTIME_PRIVATE_LINK_LIBRARIES) + target_link_libraries(webframe_${RUNTIME_NAME} PRIVATE ${RUNTIME_PRIVATE_LINK_LIBRARIES}) + endif() + if(RUNTIME_PUBLIC_LINK_LIBRARIES) + target_link_libraries(webframe_${RUNTIME_NAME} PUBLIC ${RUNTIME_PUBLIC_LINK_LIBRARIES}) + endif() +endfunction() + +function(webframe_add_application) + set(options) + set(oneValueArgs NAME) + set(multiValueArgs + SOURCES + INCLUDE_DIRS + LINK_LIBRARIES + RUNTIME) + cmake_parse_arguments(APPLICATION + "${options}" + "${oneValueArgs}" + "${multiValueArgs}" + ${ARGN}) + foreach(RUNTIME ${APPLICATION_RUNTIME}) + set(TARGET_NAME ${APPLICATION_NAME}_${RUNTIME}) + if(WIN32) + if(RUNTIME STREQUAL "desktop") + add_executable(${TARGET_NAME} WIN32 ${APPLICATION_SOURCES}) + else() + add_executable(${TARGET_NAME} ${APPLICATION_SOURCES}) + endif() + else() + add_executable(${TARGET_NAME} ${APPLICATION_SOURCES}) + endif() + if(APPLICATION_INCLUDE_DIRS) + target_include_directories(${TARGET_NAME} PRIVATE ${APPLICATION_INCLUDE_DIRS}) + endif() + target_link_libraries(${TARGET_NAME} PRIVATE webframe_${RUNTIME} ${APPLICATION_LINK_LIBRARIES}) + add_dependencies(${TARGET_NAME} webframe_${RUNTIME}) + endforeach() +endfunction() \ No newline at end of file diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt new file mode 100644 index 0000000..2174c50 --- /dev/null +++ b/example/CMakeLists.txt @@ -0,0 +1,37 @@ + +set(HYPERPAGE_ARCHIVE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) +set(HYPERPAGE_ARCHIVE_PATH ${CMAKE_CURRENT_BINARY_DIR}/hyperpage.db) + +file(GLOB_RECURSE HYPERPAGE_FRONTEND_SOURCES CONFIGURE_DEPENDS + ${CMAKE_CURRENT_SOURCE_DIR}/frontend/*) + +add_custom_command( + OUTPUT ${HYPERPAGE_ARCHIVE_PATH} + COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR} + COMMAND hyperpage::hyperpack --output ${HYPERPAGE_ARCHIVE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/frontend + DEPENDS ${HYPERPAGE_FRONTEND_SOURCES} + COMMENT "Bundling example frontend with hyperpack" + VERBATIM +) + +add_custom_target(example_frontend_bundle ALL + DEPENDS ${HYPERPAGE_ARCHIVE_PATH}) + +configure_file( + ${CMAKE_CURRENT_SOURCE_DIR}/archive.hpp.in + ${CMAKE_CURRENT_BINARY_DIR}/archive.hpp + @ONLY +) +if(WIN32) + webframe_add_application(NAME example + SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp + INCLUDE_DIRS ${CMAKE_CURRENT_BINARY_DIR} + LINK_LIBRARIES yyjson::yyjson hyperpage::hyperpage + RUNTIME desktop server) +else() + webframe_add_application(NAME example + SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp + INCLUDE_DIRS ${CMAKE_CURRENT_BINARY_DIR} + LINK_LIBRARIES yyjson::yyjson hyperpage::hyperpage + RUNTIME server) +endif() \ No newline at end of file diff --git a/example/archive.hpp.in b/example/archive.hpp.in new file mode 100644 index 0000000..1de3dca --- /dev/null +++ b/example/archive.hpp.in @@ -0,0 +1,9 @@ +#ifndef ARCHIVE_HPP +#define ARCHIVE_HPP + +#include + +constexpr const char* ARCHIVE_DIRECTORY = "@HYPERPAGE_ARCHIVE_DIRECTORY@"; + + +#endif \ No newline at end of file diff --git a/example/frontend/form.js b/example/frontend/form.js new file mode 100644 index 0000000..4bd6274 --- /dev/null +++ b/example/frontend/form.js @@ -0,0 +1,39 @@ +async function handleNameSubmit(event) { + event.preventDefault(); + + const nameInput = document.querySelector("#name"); + if (!nameInput) { + return; + } + + try { + const response = await fetch("/greetingIPC", { + method: "POST", + headers: { + "Content-Type": "application/json" + }, + body: JSON.stringify({ + name: nameInput.value + }) + }); + + if (!response.ok) { + throw new Error(`Request failed with status ${response.status}`); + } + + const responseText = await response.text(); + if (!responseText) { + throw new Error("Endpoint returned an empty response body."); + } + + const responseBody = JSON.parse(responseText); + if (typeof responseBody.greeting !== "string") { + throw new Error("Endpoint response is missing the 'greeting' string."); + } + + alert(responseBody.greeting); + } + catch (error) { + alert(error instanceof Error ? error.message : "Greeting request failed."); + } +} \ No newline at end of file diff --git a/example/frontend/index.html b/example/frontend/index.html new file mode 100644 index 0000000..2785b06 --- /dev/null +++ b/example/frontend/index.html @@ -0,0 +1,24 @@ + + + + + + WebFrame + + + +
+

WebFrame

+
+ +
+ + + +
+ + + + \ No newline at end of file diff --git a/example/main.cpp b/example/main.cpp new file mode 100644 index 0000000..27516c2 --- /dev/null +++ b/example/main.cpp @@ -0,0 +1,98 @@ +#include + +#include + +#include + +static std::string get_archive_path() +{ + std::string result(ARCHIVE_DIRECTORY); +#ifdef _WIN32 + std::replace(result.begin(), result.end(), '/', '\\'); + result += "\\"; +#else + result += "/"; +#endif + result += "hyperpage.db"; + return result; +} + +class archive_handler : public webframe::handler +{ +public: + void open(const std::string& archive_path) + { + _archive.reset(new hyperpage::reader(archive_path)); + } +protected: + void handle_get(const webframe::request* req, webframe::response* res) override + { + std::string page_path = req->get_path(); + if(page_path.empty() || page_path == "/") { + page_path = "/index.html"; + } + auto page = _archive->load(page_path); + if (page) { + res->set_status(200); + res->set_header("Content-Type", page->get_mime_type()); + res->set_body(page->get_content(), page->get_length()); + } + else { + res->set_status(404); + res->set_header("Content-Type", "text/plain"); + const std::string not_found_msg = "404 Not Found"; + res->set_body(reinterpret_cast(not_found_msg.data()), not_found_msg.size()); + } + } +private: + std::unique_ptr _archive; +}; + +class greeting_ipc_handler : public webframe::handler +{ +protected: + void handle_post(const webframe::request* req, webframe::response* res) override + { + std::unique_ptr request_body(nullptr, &yyjson_doc_free); + std::unique_ptr response_body(nullptr, &yyjson_mut_doc_free); + request_body.reset(yyjson_read(reinterpret_cast(req->get_body().first), req->get_body().second, 0)); + if (!request_body || !yyjson_is_obj(request_body->root)) { + throw webframe::exception::bad_request; + } + yyjson_val* name_val = yyjson_obj_get(request_body->root, "name"); + if (!name_val || !yyjson_is_str(name_val)) { + throw webframe::exception::bad_request; + } + const char* name = yyjson_get_str(name_val); + + response_body.reset(yyjson_mut_doc_new(nullptr)); + yyjson_mut_val* root = yyjson_mut_obj(response_body.get()); + const std::string greeting = "Hello, " + std::string(name) + "!"; + yyjson_mut_obj_add_strcpy(response_body.get(), root, "greeting", greeting.c_str()); + yyjson_mut_doc_set_root(response_body.get(), root); + + size_t response_len; + const char *response_data = yyjson_mut_write(response_body.get(), 0, &response_len); + res->set_status(200); + res->set_header("Content-Type", "application/json"); + res->set_body(reinterpret_cast(response_data), response_len); + free(const_cast(response_data)); + } +}; + +class example_application : public webframe::application +{ +public: + void configure_router(webframe::router *router) override + { + _archive_handler.open(get_archive_path()); + router->set_default(&_archive_handler); + router->add_route("/greetingIPC", &_greeting_ipc_handler); + } +private: + archive_handler _archive_handler; + greeting_ipc_handler _greeting_ipc_handler; +}; + + +WEBFRAME_MAIN(example_application) diff --git a/include/webframe.hpp b/include/webframe.hpp index 0e7151d..70b38e7 100644 --- a/include/webframe.hpp +++ b/include/webframe.hpp @@ -1,5 +1,5 @@ -/* WebFrame - * +/* WebFrame + * * Copyright (C) 2026 Maxtek Consulting * * This program is free software: you can redistribute it and/or modify @@ -19,6 +19,92 @@ #ifndef WEBFRAME_HPP #define WEBFRAME_HPP +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#if defined(_WIN32) && defined(WEBFRAME_DESKTOP_RUNTIME) +#define WEBFRAME_WIN32_APP 1 +#endif + +#if defined(WEBFRAME_WIN32_APP) +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif +#include +#endif + +namespace webframe +{ + enum class method + { + http_get, + http_post, + http_put, + http_delete + }; + + class request + { + public: + virtual method get_method() const = 0; + virtual std::string get_path() const = 0; + virtual bool get_header(const std::string &key, std::string &value) const = 0; + virtual std::pair get_body() const = 0; + virtual void read_body(const std::function &callback) const = 0; + }; + + class response + { + public: + virtual void set_status(int status_code) = 0; + virtual void set_header(const std::string &key, const std::string &value) = 0; + virtual void set_body(const uint8_t *data, size_t size) = 0; + virtual void write_body(const std::function &)> &callback) = 0 ; + }; + + + + class runtime + { + public: +#ifdef WEBFRAME_WIN32_APP + virtual int dispatch(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow, application *a, router *r) = 0; +#else + virtual int dispatch(int argc, const char **argv, application *a, router *r) = 0; +#endif + }; + + runtime *webframe_init(); +} +#if defined(WEBFRAME_WIN32_APP) + #define WEBFRAME_MAIN(AppType) \ + int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) \ + { \ + std::unique_ptr runtime(webframe::webframe_init()); \ + AppType app; \ + webframe::router router; \ + return runtime->dispatch(hInstance, hPrevInstance, lpCmdLine, nCmdShow, &app, &router); \ + } +#else + #define WEBFRAME_MAIN(AppType) \ + int main(int argc, const char **argv) \ + { \ + std::unique_ptr runtime(webframe::webframe_init()); \ + AppType app; \ + webframe::router router; \ + return runtime->dispatch(argc, argv, &app, &router); \ + } +#endif -#endif \ No newline at end of file +#endif diff --git a/include/webframe/application.hpp b/include/webframe/application.hpp new file mode 100644 index 0000000..d0d013d --- /dev/null +++ b/include/webframe/application.hpp @@ -0,0 +1,20 @@ +#ifndef WEBFRAME_APPLICATION_HPP +#define WEBFRAME_APPLICATION_HPP + +namespace webframe +{ + class router; + + class application + { + public: + application() = default; + virtual ~application() = default; + virtual void configure_desktop(); + virtual void configure_server(int argc, const char **argv); + virtual void configure_router(router *ctrl); + virtual void on_dispatch(); + }; +} + +#endif \ No newline at end of file diff --git a/include/webframe/controller.hpp b/include/webframe/controller.hpp new file mode 100644 index 0000000..e69de29 diff --git a/include/webframe/exception.hpp b/include/webframe/exception.hpp new file mode 100644 index 0000000..36361b6 --- /dev/null +++ b/include/webframe/exception.hpp @@ -0,0 +1,29 @@ +#ifndef WEBFRAME_EXCEPTION_HPP +#define WEBFRAME_EXCEPTION_HPP + +#include + +namespace webframe +{ + class exception + { + public: + exception(int status_code, const std::string &message, const std::string &content_type = "text/plain"); + int get_status_code() const; + std::string get_message() const; + std::string get_content_type() const; + + static exception bad_request; + static exception unauthorized; + static exception forbidden; + static exception not_found; + static exception method_not_allowed; + static exception internal_server_error; + private: + int _status_code; + std::string _message; + std::string _content_type; + }; +} + +#endif \ No newline at end of file diff --git a/include/webframe/handler.hpp b/include/webframe/handler.hpp new file mode 100644 index 0000000..99aabb7 --- /dev/null +++ b/include/webframe/handler.hpp @@ -0,0 +1,21 @@ +#ifndef WEBFRAME_HANDLER_HPP +#define WEBFRAME_HANDLER_HPP + +namespace webframe +{ + class request; + class response; + + class handler + { + public: + void handle_request(const request* req, response* res); + protected: + virtual void handle_get(const request* req, response* res); + virtual void handle_post(const request* req, response* res); + virtual void handle_put(const request* req, response* res); + virtual void handle_delete(const request* req, response* res); + }; +} + +#endif \ No newline at end of file diff --git a/include/webframe/router.hpp b/include/webframe/router.hpp new file mode 100644 index 0000000..d10f0d9 --- /dev/null +++ b/include/webframe/router.hpp @@ -0,0 +1,27 @@ +#ifndef WEBFRAME_ROUTER_HPP +#define WEBFRAME_ROUTER_HPP + +#include +#include + +namespace webframe +{ + class request; + class response; + class handler; + + class router + { + public: + router() = default; + ~router() = default; + void add_route(const std::string& path, handler* h); + handler *find_route(const std::string& path); + void set_default(handler* h); + handler* get_default(); + private: + std::unordered_map _handlers; + handler* _default_handler = nullptr; + }; +} +#endif \ No newline at end of file diff --git a/ports/README.md b/ports/README.md new file mode 100644 index 0000000..d28ff10 --- /dev/null +++ b/ports/README.md @@ -0,0 +1,14 @@ +# Ports + +CEF is currently built as a port for MacOS and Linux + +## TODO + +The wxWebView chromium module should be built as a port. This can be +done one of two ways: + +1. Implement wxWebView CEF backend and set wxWidgets as a dependency +2. Build wxWidgets as a port with both Edge and CEF. + +The first option is simpler and cleaner, but this could change in the +future. \ No newline at end of file diff --git a/ports/cef/CMakeLists.txt b/ports/cef/CMakeLists.txt new file mode 100644 index 0000000..2b7a5af --- /dev/null +++ b/ports/cef/CMakeLists.txt @@ -0,0 +1,49 @@ +# This CMakeLists.txt is used by the vcpkg overlay port to build +# libcef_dll_wrapper from the CEF binary distribution source. +cmake_minimum_required(VERSION 3.15) +project(cef_dll_wrapper LANGUAGES CXX) + +# Collect all wrapper source files +file(GLOB_RECURSE WRAPPER_SOURCES + "${CMAKE_CURRENT_SOURCE_DIR}/libcef_dll/*.cc" + "${CMAKE_CURRENT_SOURCE_DIR}/libcef_dll/*.cpp" +) + +# Exclude test sources +list(FILTER WRAPPER_SOURCES EXCLUDE REGEX ".*/test/.*") + +# Exclude platform-specific sources that don't apply +if(NOT CMAKE_SYSTEM_NAME STREQUAL "Darwin") + list(FILTER WRAPPER_SOURCES EXCLUDE REGEX ".*_mac\\.mm$") + list(FILTER WRAPPER_SOURCES EXCLUDE REGEX ".*_mac\\.cc$") +endif() +if(NOT CMAKE_SYSTEM_NAME STREQUAL "Windows") + list(FILTER WRAPPER_SOURCES EXCLUDE REGEX ".*_win\\.cc$") +endif() + +add_library(cef_dll_wrapper STATIC ${WRAPPER_SOURCES}) + +target_include_directories(cef_dll_wrapper + PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}" +) + +# CEF wrapper requires WRAPPING_CEF_SHARED to compile cpptoc/ctocpp sources +target_compile_definitions(cef_dll_wrapper PRIVATE WRAPPING_CEF_SHARED) + +set_target_properties(cef_dll_wrapper PROPERTIES + CXX_STANDARD 17 + CXX_STANDARD_REQUIRED ON + POSITION_INDEPENDENT_CODE ON +) + +if(CMAKE_SYSTEM_NAME STREQUAL "Linux") + target_compile_options(cef_dll_wrapper PRIVATE + -Wno-undefined-var-template + -Wno-missing-field-initializers + ) +endif() + +install(TARGETS cef_dll_wrapper + ARCHIVE DESTINATION lib + LIBRARY DESTINATION lib +) diff --git a/ports/cef/cef-config.cmake b/ports/cef/cef-config.cmake new file mode 100644 index 0000000..5efdce7 --- /dev/null +++ b/ports/cef/cef-config.cmake @@ -0,0 +1,109 @@ +# CMake config file for the CEF vcpkg overlay port. +# +# Provides the following imported targets: +# CEF::wrapper – static libcef_dll_wrapper (compiled from source) +# CEF::lib – prebuilt libcef shared library +# CEF::CEF – convenience interface that links both +# +# Variables: +# CEF_RESOURCE_DIR – path to the CEF Resources directory (PAK/locale files) + +get_filename_component(_CEF_ROOT "${CMAKE_CURRENT_LIST_DIR}/../.." ABSOLUTE) + +# ---------- CEF::wrapper (static) ---------- +if(NOT TARGET CEF::wrapper) + add_library(CEF::wrapper STATIC IMPORTED) + + find_library(_CEF_WRAPPER_RELEASE + NAMES cef_dll_wrapper + PATHS "${_CEF_ROOT}/lib" + NO_DEFAULT_PATH + ) + find_library(_CEF_WRAPPER_DEBUG + NAMES cef_dll_wrapper + PATHS "${_CEF_ROOT}/debug/lib" + NO_DEFAULT_PATH + ) + + if(_CEF_WRAPPER_RELEASE) + set_property(TARGET CEF::wrapper APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE) + set_target_properties(CEF::wrapper PROPERTIES + IMPORTED_LOCATION_RELEASE "${_CEF_WRAPPER_RELEASE}" + ) + endif() + if(_CEF_WRAPPER_DEBUG) + set_property(TARGET CEF::wrapper APPEND PROPERTY IMPORTED_CONFIGURATIONS DEBUG) + set_target_properties(CEF::wrapper PROPERTIES + IMPORTED_LOCATION_DEBUG "${_CEF_WRAPPER_DEBUG}" + ) + endif() + + set_target_properties(CEF::wrapper PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${_CEF_ROOT}/include" + ) +endif() + +# ---------- CEF::lib (prebuilt shared library) ---------- +if(NOT TARGET CEF::lib) + if(APPLE) + # On macOS, CEF is a .framework bundle + set(_CEF_FRAMEWORK_DIR "${_CEF_ROOT}/lib/Chromium Embedded Framework.framework") + if(EXISTS "${_CEF_FRAMEWORK_DIR}") + set(_CEF_SHARED_RELEASE "${_CEF_FRAMEWORK_DIR}/Chromium Embedded Framework") + endif() + + add_library(CEF::lib SHARED IMPORTED) + if(_CEF_SHARED_RELEASE) + set_property(TARGET CEF::lib APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE) + set_target_properties(CEF::lib PROPERTIES + IMPORTED_LOCATION_RELEASE "${_CEF_SHARED_RELEASE}" + IMPORTED_SONAME_RELEASE "@rpath/Chromium Embedded Framework.framework/Chromium Embedded Framework" + ) + endif() + else() + # On Linux, libcef.so is in the lib/ directory + find_library(_CEF_SHARED_RELEASE + NAMES cef + PATHS "${_CEF_ROOT}/lib" + NO_DEFAULT_PATH + ) + + add_library(CEF::lib SHARED IMPORTED) + if(_CEF_SHARED_RELEASE) + set_property(TARGET CEF::lib APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE) + set_target_properties(CEF::lib PROPERTIES + IMPORTED_LOCATION_RELEASE "${_CEF_SHARED_RELEASE}" + IMPORTED_NO_SONAME TRUE + ) + endif() + endif() + + # For debug, fall back to the same release library (CEF does not ship a debug build) + if(_CEF_SHARED_RELEASE AND NOT _CEF_SHARED_DEBUG) + set(_CEF_SHARED_DEBUG "${_CEF_SHARED_RELEASE}") + endif() + if(_CEF_SHARED_DEBUG) + set_property(TARGET CEF::lib APPEND PROPERTY IMPORTED_CONFIGURATIONS DEBUG) + set_target_properties(CEF::lib PROPERTIES + IMPORTED_LOCATION_DEBUG "${_CEF_SHARED_DEBUG}" + ) + endif() +endif() + +# ---------- CEF::CEF (convenience) ---------- +if(NOT TARGET CEF::CEF) + add_library(CEF::CEF INTERFACE IMPORTED) + set_property(TARGET CEF::CEF PROPERTY + INTERFACE_LINK_LIBRARIES CEF::wrapper CEF::lib + ) +endif() + +# ---------- Resources ---------- +set(CEF_RESOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/Resources" CACHE PATH "CEF resource directory") + +# ---------- Validation ---------- +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(CEF DEFAULT_MSG + _CEF_WRAPPER_RELEASE + _CEF_SHARED_RELEASE +) diff --git a/ports/cef/portfile.cmake b/ports/cef/portfile.cmake new file mode 100644 index 0000000..2e09bcb --- /dev/null +++ b/ports/cef/portfile.cmake @@ -0,0 +1,96 @@ +# CEF distributes a prebuilt shared library (libcef) plus wrapper source code +# (libcef_dll_wrapper) that must be compiled. This port downloads the official +# CEF binary distribution, builds the wrapper, and installs everything. + +set(CEF_VERSION "122.1.10+gc902316+chromium-122.0.6261.112") + +# --------------------------------------------------------------------------- +# Platform-specific archive selection (non-Windows only) +# --------------------------------------------------------------------------- +if(VCPKG_TARGET_IS_LINUX) + set(CEF_PLATFORM "linux64") +elseif(VCPKG_TARGET_IS_OSX) + if(VCPKG_TARGET_ARCHITECTURE STREQUAL "arm64") + set(CEF_PLATFORM "macosarm64") + else() + set(CEF_PLATFORM "macosx64") + endif() +else() + message(FATAL_ERROR "CEF: unsupported platform") +endif() + +set(CEF_ARCHIVE "cef_binary_${CEF_VERSION}_${CEF_PLATFORM}_minimal.tar.bz2") + +vcpkg_download_distfile(ARCHIVE + URLS "https://cef-builds.spotifycdn.com/${CEF_ARCHIVE}" + FILENAME "${CEF_ARCHIVE}" + # After the first download attempt vcpkg will print the real SHA512. + # Replace the placeholder "0" hashes above with the correct values. + SKIP_SHA512 +) + +vcpkg_extract_source_archive(SOURCE_PATH ARCHIVE "${ARCHIVE}") + +# --------------------------------------------------------------------------- +# Build libcef_dll_wrapper (static library compiled from source) +# --------------------------------------------------------------------------- +file(COPY "${CMAKE_CURRENT_LIST_DIR}/CMakeLists.txt" DESTINATION "${SOURCE_PATH}") + +vcpkg_cmake_configure(SOURCE_PATH "${SOURCE_PATH}") +vcpkg_cmake_install() + +# --------------------------------------------------------------------------- +# Install prebuilt CEF shared library and helpers +# --------------------------------------------------------------------------- +if(VCPKG_TARGET_IS_LINUX) + file(INSTALL "${SOURCE_PATH}/Release/libcef.so" DESTINATION "${CURRENT_PACKAGES_DIR}/lib") + # Also install EGL/GLES/Vulkan helper libraries if present + file(GLOB _cef_helpers "${SOURCE_PATH}/Release/libEGL.so" "${SOURCE_PATH}/Release/libGLESv2.so" + "${SOURCE_PATH}/Release/libvk_swiftshader.so" "${SOURCE_PATH}/Release/libvulkan.so.1") + if(_cef_helpers) + file(INSTALL ${_cef_helpers} DESTINATION "${CURRENT_PACKAGES_DIR}/lib") + endif() + # Install v8 snapshot and sandbox helper + file(GLOB _cef_data "${SOURCE_PATH}/Release/*.bin" "${SOURCE_PATH}/Release/chrome-sandbox") + if(_cef_data) + file(INSTALL ${_cef_data} DESTINATION "${CURRENT_PACKAGES_DIR}/share/${PORT}/bin") + endif() +elseif(VCPKG_TARGET_IS_OSX) + file(GLOB _cef_framework "${SOURCE_PATH}/Release/Chromium Embedded Framework.framework") + if(_cef_framework) + file(INSTALL ${_cef_framework} DESTINATION "${CURRENT_PACKAGES_DIR}/lib") + endif() +endif() + +# Tell vcpkg that this port intentionally provides shared libraries even if +# the triplet default is static (CEF is always a shared library). +set(VCPKG_POLICY_DLLS_WITHOUT_EXPORTS enabled) + +# --------------------------------------------------------------------------- +# Install CEF headers +# --------------------------------------------------------------------------- +file(INSTALL "${SOURCE_PATH}/include/" DESTINATION "${CURRENT_PACKAGES_DIR}/include") + +# --------------------------------------------------------------------------- +# Install resource files (PAK, locales, ICU data, etc.) +# --------------------------------------------------------------------------- +if(EXISTS "${SOURCE_PATH}/Resources") + file(INSTALL "${SOURCE_PATH}/Resources/" DESTINATION "${CURRENT_PACKAGES_DIR}/share/${PORT}/Resources") +endif() + +# --------------------------------------------------------------------------- +# Install CMake config +# --------------------------------------------------------------------------- +file(INSTALL "${CMAKE_CURRENT_LIST_DIR}/cef-config.cmake" + DESTINATION "${CURRENT_PACKAGES_DIR}/share/${PORT}") + +# --------------------------------------------------------------------------- +# Copyright +# --------------------------------------------------------------------------- +vcpkg_install_copyright(FILE_LIST "${SOURCE_PATH}/LICENSE.txt") + +# --------------------------------------------------------------------------- +# Usage hint +# --------------------------------------------------------------------------- +file(INSTALL "${CMAKE_CURRENT_LIST_DIR}/usage" + DESTINATION "${CURRENT_PACKAGES_DIR}/share/${PORT}") diff --git a/ports/cef/usage b/ports/cef/usage new file mode 100644 index 0000000..44c4455 --- /dev/null +++ b/ports/cef/usage @@ -0,0 +1,13 @@ +cef provides CMake targets: + + find_package(CEF CONFIG REQUIRED) + target_link_libraries(main PRIVATE CEF::CEF) + +Individual targets are also available: + + CEF::wrapper – static libcef_dll_wrapper + CEF::lib – prebuilt libcef_static static library + +The CEF resource directory (PAK files, locales) is available as: + + ${CEF_RESOURCE_DIR} diff --git a/ports/cef/vcpkg.json b/ports/cef/vcpkg.json new file mode 100644 index 0000000..ecc81a0 --- /dev/null +++ b/ports/cef/vcpkg.json @@ -0,0 +1,19 @@ +{ + "name": "cef", + "version": "122.1.10", + "port-version": 0, + "description": "Chromium Embedded Framework (CEF) - A framework for embedding Chromium-based browsers in other applications.", + "homepage": "https://bitbucket.org/chromiumembedded/cef", + "license": "BSD-3-Clause", + "supports": "!windows", + "dependencies": [ + { + "name": "vcpkg-cmake", + "host": true + }, + { + "name": "vcpkg-cmake-config", + "host": true + } + ] +} diff --git a/ports/hyperpage/fix-missing-cstdint.patch b/ports/hyperpage/fix-missing-cstdint.patch new file mode 100644 index 0000000..236d86f --- /dev/null +++ b/ports/hyperpage/fix-missing-cstdint.patch @@ -0,0 +1,10 @@ +--- hyperpage.hpp.orig 2026-02-28 17:38:49.280169518 -0600 ++++ hyperpage.hpp 2026-02-28 17:38:49.283778456 -0600 +@@ -29,6 +29,7 @@ + * @author John R. Patek Sr. + */ + ++#include + #include + #include + #include diff --git a/ports/hyperpage/portfile.cmake b/ports/hyperpage/portfile.cmake new file mode 100644 index 0000000..2ee2781 --- /dev/null +++ b/ports/hyperpage/portfile.cmake @@ -0,0 +1,31 @@ +vcpkg_from_github( + OUT_SOURCE_PATH SOURCE_PATH + REPO maxtek6/hyperpage + REF b679d32305783aff6cc37600387616c48e8f07ff + SHA512 9cc0187583522afe070e9cee2fecb73594ee2348c47b733fc73bb4187ab9dd7cd1ebaeb2ab122ebd1a93b04a739e5c6792bd0f9a99cc0539e46920346b71072e + HEAD_REF master + PATCHES + fix-missing-cstdint.patch +) + +vcpkg_cmake_configure( + SOURCE_PATH "${SOURCE_PATH}" + OPTIONS + -DCMAKE_INSTALL_INCLUDEDIR=include +) + +vcpkg_cmake_install() + +vcpkg_cmake_config_fixup( + PACKAGE_NAME hyperpage + CONFIG_PATH lib/cmake/hyperpage +) + +file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/include") + +vcpkg_copy_tools(TOOL_NAMES hyperpack AUTO_CLEAN) + +vcpkg_install_copyright(FILE_LIST "${SOURCE_PATH}/LICENSE") + +file(INSTALL "${CMAKE_CURRENT_LIST_DIR}/usage" + DESTINATION "${CURRENT_PACKAGES_DIR}/share/${PORT}") diff --git a/ports/hyperpage/usage b/ports/hyperpage/usage new file mode 100644 index 0000000..6835016 --- /dev/null +++ b/ports/hyperpage/usage @@ -0,0 +1,11 @@ +hyperpage provides CMake targets: + + find_package(hyperpage CONFIG REQUIRED) + target_link_libraries(main PRIVATE hyperpage::hyperpage) + +hyperpage also provides the hyperpack command-line utility: + + hyperpack [--output ] [--verbose] ... + + Packs files from the specified directories into a hyperpage SQLite database. + The default output file is "hyperpage.db". diff --git a/ports/hyperpage/vcpkg.json b/ports/hyperpage/vcpkg.json new file mode 100644 index 0000000..daf707b --- /dev/null +++ b/ports/hyperpage/vcpkg.json @@ -0,0 +1,22 @@ +{ + "name": "hyperpage", + "version": "0.1.0", + "port-version": 0, + "description": "Fast and efficient solution for storing and loading web content", + "homepage": "https://github.com/maxtek6/hyperpage", + "license": "MIT", + "dependencies": [ + { + "name": "vcpkg-cmake", + "host": true + }, + { + "name": "vcpkg-cmake-config", + "host": true + }, + "sqlite3", + "megamimes", + "argparse", + "mio" + ] +} diff --git a/ports/maxtest/portfile.cmake b/ports/maxtest/portfile.cmake new file mode 100644 index 0000000..e0997f3 --- /dev/null +++ b/ports/maxtest/portfile.cmake @@ -0,0 +1,42 @@ +vcpkg_from_github( + OUT_SOURCE_PATH SOURCE_PATH + REPO maxtek6/maxtest + REF master + SHA512 ad470249867d8f4d915e2c0b89f1ff4b62fef9d0202e402fa8b7ac2cff457181f5e45cde7fbc33d1282d045778e2485a0d2929563624fbdc67cff96ce943aea2 + HEAD_REF master +) + +# maxtest is header-only, just install the header file +file(INSTALL "${SOURCE_PATH}/maxtest.hpp" + DESTINATION "${CURRENT_PACKAGES_DIR}/include") + +# Create CMake config files for find_package support +file(WRITE "${CURRENT_PACKAGES_DIR}/share/maxtest/maxtest-config.cmake" +" +include_guard(GLOBAL) + +add_library(maxtest INTERFACE) +target_include_directories(maxtest INTERFACE + \$ + \$ +) +add_library(Maxtest::Maxtest ALIAS maxtest) + +function(maxtest_add_executable TARGET_NAME) + add_executable(\${TARGET_NAME} \${ARGN}) + target_link_libraries(\${TARGET_NAME} PRIVATE Maxtest::Maxtest) +endfunction() + +function(maxtest_add_test TARGET_NAME TEST_NAME) + add_test(NAME \${TEST_NAME} COMMAND \${TARGET_NAME} \${TEST_NAME} \${ARGN}) +endfunction() + +include(CTest) +enable_testing() +set(CTEST_OUTPUT_ON_FAILURE ON) +") + +vcpkg_install_copyright(FILE_LIST "${SOURCE_PATH}/LICENSE") + +file(INSTALL "${CMAKE_CURRENT_LIST_DIR}/usage" + DESTINATION "${CURRENT_PACKAGES_DIR}/share/${PORT}") diff --git a/ports/maxtest/usage b/ports/maxtest/usage new file mode 100644 index 0000000..1360474 --- /dev/null +++ b/ports/maxtest/usage @@ -0,0 +1,17 @@ +The package maxtest provides CMake integration for the maxtest unit testing framework. + +## Usage + +After installing maxtest, you can use it in your CMakeLists.txt: + +```cmake +find_package(maxtest REQUIRED CONFIG) + +# Create an executable with maxtest +maxtest_add_executable(my_tests test.cpp) + +# Add individual test cases +maxtest_add_test(my_tests test_name) +``` + +For more information, visit: https://github.com/maxtek6/maxtest diff --git a/ports/maxtest/vcpkg.json b/ports/maxtest/vcpkg.json new file mode 100644 index 0000000..bf41728 --- /dev/null +++ b/ports/maxtest/vcpkg.json @@ -0,0 +1,18 @@ +{ + "name": "maxtest", + "version": "0.0.1", + "port-version": 0, + "description": "Lightweight unit testing framework for C++ and CMake", + "homepage": "https://github.com/maxtek6/maxtest", + "license": "MIT", + "dependencies": [ + { + "name": "vcpkg-cmake", + "host": true + }, + { + "name": "vcpkg-cmake-config", + "host": true + } + ] +} diff --git a/ports/megamimes/CMakeLists.txt b/ports/megamimes/CMakeLists.txt new file mode 100644 index 0000000..f81f8bc --- /dev/null +++ b/ports/megamimes/CMakeLists.txt @@ -0,0 +1,37 @@ +cmake_minimum_required(VERSION 3.15) +project(megamimes VERSION 1.0.0 LANGUAGES C) + +include(GNUInstallDirs) +include(CMakePackageConfigHelpers) + +add_library(megamimes STATIC src/MegaMimes.c) +add_library(megamimes::megamimes ALIAS megamimes) + +target_include_directories(megamimes + PUBLIC + $ + $ +) + +install(TARGETS megamimes + EXPORT megamimes-targets + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} +) + +install(FILES src/MegaMimes.h + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} +) + +install(EXPORT megamimes-targets + FILE megamimes-targets.cmake + NAMESPACE megamimes:: + DESTINATION share/cmake/megamimes +) + +file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/megamimes-config.cmake" + "include(\"\${CMAKE_CURRENT_LIST_DIR}/megamimes-targets.cmake\")\n" +) + +install(FILES "${CMAKE_CURRENT_BINARY_DIR}/megamimes-config.cmake" + DESTINATION share/cmake/megamimes +) diff --git a/ports/megamimes/portfile.cmake b/ports/megamimes/portfile.cmake new file mode 100644 index 0000000..c3a764f --- /dev/null +++ b/ports/megamimes/portfile.cmake @@ -0,0 +1,22 @@ +vcpkg_from_github( + OUT_SOURCE_PATH SOURCE_PATH + REPO kobbyowen/MegaMimes + REF b839068db99cbfcff1af8df1229bd7e41701fe96 + SHA512 1581ddb6e85929ce7ec1e97578ad77fadd3f6ac82a1e4a379772dfa90b048a9c508059cc63525feef73a7a08e8553789830ef48e28ebc0e6a1123f1b0fb69889 + HEAD_REF master +) + +# MegaMimes does not include a CMakeLists.txt, so provide one +file(COPY "${CMAKE_CURRENT_LIST_DIR}/CMakeLists.txt" DESTINATION "${SOURCE_PATH}") + +vcpkg_cmake_configure( + SOURCE_PATH "${SOURCE_PATH}" +) + +vcpkg_cmake_install() + +vcpkg_cmake_config_fixup(CONFIG_PATH share/cmake/megamimes) + +file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/include") + +vcpkg_install_copyright(FILE_LIST "${SOURCE_PATH}/LICENSE") diff --git a/ports/megamimes/vcpkg.json b/ports/megamimes/vcpkg.json new file mode 100644 index 0000000..3ba71d3 --- /dev/null +++ b/ports/megamimes/vcpkg.json @@ -0,0 +1,18 @@ +{ + "name": "megamimes", + "version": "1.0.0", + "port-version": 0, + "description": "Tiny, dependency-free C library for MIME type detection", + "homepage": "https://github.com/kobbyowen/MegaMimes", + "license": "MIT", + "dependencies": [ + { + "name": "vcpkg-cmake", + "host": true + }, + { + "name": "vcpkg-cmake-config", + "host": true + } + ] +} diff --git a/ports/wxwidgets/example/CMakeLists.txt b/ports/wxwidgets/example/CMakeLists.txt new file mode 100644 index 0000000..11d3084 --- /dev/null +++ b/ports/wxwidgets/example/CMakeLists.txt @@ -0,0 +1,33 @@ +cmake_minimum_required(VERSION 3.7) + +project(wxwidgets-example) + +add_executable(main WIN32 popup.cpp) + +find_package(wxWidgets REQUIRED) +target_compile_features(main PRIVATE cxx_std_11) +target_compile_definitions(main PRIVATE ${wxWidgets_DEFINITIONS} "$<$:${wxWidgets_DEFINITIONS_DEBUG}>") +target_include_directories(main PRIVATE ${wxWidgets_INCLUDE_DIRS}) +target_link_libraries(main PRIVATE ${wxWidgets_LIBRARIES}) + +add_executable(main2 WIN32 popup.cpp) + +find_package(wxWidgets CONFIG REQUIRED) +target_link_libraries(main2 PRIVATE wx::core wx::base) +target_compile_features(main2 PRIVATE cxx_std_11) + +option(USE_WXRC "Use the wxrc resource compiler" ON) +if(USE_WXRC) + execute_process( + COMMAND "${wxWidgets_wxrc_EXECUTABLE}" --help + RESULTS_VARIABLE error_result + ) + if(error_result) + message(FATAL_ERROR "Failed to run wxWidgets_wxrc_EXECUTABLE (${wxWidgets_wxrc_EXECUTABLE})") + endif() +endif() + +set(PRINT_VARS "" CACHE STRING "Variables to print at the end of configuration") +foreach(var IN LISTS PRINT_VARS) + message(STATUS "${var}:=${${var}}") +endforeach() diff --git a/ports/wxwidgets/fix-libs-export.patch b/ports/wxwidgets/fix-libs-export.patch new file mode 100644 index 0000000..63bb9d3 --- /dev/null +++ b/ports/wxwidgets/fix-libs-export.patch @@ -0,0 +1,21 @@ +diff --git a/build/cmake/config.cmake b/build/cmake/config.cmake +index b359560..7504458 100644 +--- a/build/cmake/config.cmake ++++ b/build/cmake/config.cmake +@@ -39,8 +39,14 @@ macro(wx_get_dependencies var lib) + else() + # For the value like $<$:LIB_PATH> + # Or $<$>:LIB_PATH> +- string(REGEX REPLACE "^.+>:(.+)>$" "\\1" dep_name ${dep}) +- if (NOT dep_name) ++ if(dep MATCHES "^(.+>):(.+)>$") ++ if(CMAKE_BUILD_TYPE STREQUAL "Debug" AND CMAKE_MATCH_1 STREQUAL [[$<$>]]) ++ continue() ++ elseif(CMAKE_BUILD_TYPE STREQUAL "Release" AND CMAKE_MATCH_1 STREQUAL [[$<$]]) ++ continue() ++ endif() ++ set(dep_name "${CMAKE_MATCH_2}") ++ else() + set(dep_name ${dep}) + endif() + endif() diff --git a/ports/wxwidgets/fix-pcre2.patch b/ports/wxwidgets/fix-pcre2.patch new file mode 100644 index 0000000..20063f4 --- /dev/null +++ b/ports/wxwidgets/fix-pcre2.patch @@ -0,0 +1,23 @@ +diff --git a/build/cmake/modules/FindPCRE2.cmake b/build/cmake/modules/FindPCRE2.cmake +index a27693a..455675a 100644 +--- a/build/cmake/modules/FindPCRE2.cmake ++++ b/build/cmake/modules/FindPCRE2.cmake +@@ -24,7 +24,10 @@ set(PCRE2_CODE_UNIT_WIDTH_USED "${PCRE2_CODE_UNIT_WIDTH}" CACHE INTERNAL "") + + find_package(PkgConfig QUIET) + pkg_check_modules(PC_PCRE2 QUIET libpcre2-${PCRE2_CODE_UNIT_WIDTH}) ++set(PCRE2_LIBRARIES ${PC_PCRE2_LINK_LIBRARIES}) ++set(PCRE2_INCLUDE_DIRS ${PC_PCRE2_INCLUDE_DIRS}) + ++if (0) + find_path(PCRE2_INCLUDE_DIRS + NAMES pcre2.h + HINTS ${PC_PCRE2_INCLUDEDIR} +@@ -36,6 +39,7 @@ find_library(PCRE2_LIBRARIES + HINTS ${PC_PCRE2_LIBDIR} + ${PC_PCRE2_LIBRARY_DIRS} + ) ++endif() + + include(FindPackageHandleStandardArgs) + FIND_PACKAGE_HANDLE_STANDARD_ARGS(PCRE2 REQUIRED_VARS PCRE2_LIBRARIES PCRE2_INCLUDE_DIRS VERSION_VAR PC_PCRE2_VERSION) diff --git a/ports/wxwidgets/gtk3-link-libraries.patch b/ports/wxwidgets/gtk3-link-libraries.patch new file mode 100644 index 0000000..4e4c481 --- /dev/null +++ b/ports/wxwidgets/gtk3-link-libraries.patch @@ -0,0 +1,17 @@ +diff --git a/build/cmake/modules/FindGTK3.cmake b/build/cmake/modules/FindGTK3.cmake +index d2939a1..daf33fe 100644 +--- a/build/cmake/modules/FindGTK3.cmake ++++ b/build/cmake/modules/FindGTK3.cmake +@@ -47,6 +47,12 @@ include(CheckSymbolExists) + set(CMAKE_REQUIRED_INCLUDES ${GTK3_INCLUDE_DIRS}) + check_symbol_exists(GDK_WINDOWING_WAYLAND "gdk/gdk.h" wxHAVE_GDK_WAYLAND) + check_symbol_exists(GDK_WINDOWING_X11 "gdk/gdk.h" wxHAVE_GDK_X11) ++# With Lerc support in TIFF, Gtk3 may carry C++ compiler libs which break FindWxWidgets.cmake. ++# WxWidgets is C++, so we can remove them here using the inverse pattern. ++set(cxx_libs "${CMAKE_CXX_IMPLICIT_LINK_LIBRARIES}") ++list(REMOVE_ITEM cxx_libs ${CMAKE_C_IMPLICIT_LINK_LIBRARIES}) ++list(REMOVE_ITEM GTK3_LINK_LIBRARIES ${cxx_libs}) ++set(GTK3_LIBRARIES "${GTK3_LINK_LIBRARIES}" CACHE INTERNAL "") + include(FindPackageHandleStandardArgs) + FIND_PACKAGE_HANDLE_STANDARD_ARGS(GTK3 DEFAULT_MSG GTK3_INCLUDE_DIRS GTK3_LIBRARIES VERSION_OK) + diff --git a/ports/wxwidgets/install-layout.patch b/ports/wxwidgets/install-layout.patch new file mode 100644 index 0000000..3a8b545 --- /dev/null +++ b/ports/wxwidgets/install-layout.patch @@ -0,0 +1,46 @@ +diff --git a/build/cmake/init.cmake b/build/cmake/init.cmake +index f044d22d4d..a78b9aa1e9 100644 +--- a/build/cmake/init.cmake ++++ b/build/cmake/init.cmake +@@ -200,12 +200,12 @@ endif() + + if(WIN32_MSVC_NAMING) + if(wxBUILD_SHARED) +- set(lib_suffix "_dll") ++ # set(lib_suffix "_dll") + else() +- set(lib_suffix "_lib") ++ # set(lib_suffix "_lib") + endif() + +- set(wxPLATFORM_LIB_DIR "${wxCOMPILER_PREFIX}${wxARCH_SUFFIX}${lib_suffix}") ++ # set(wxPLATFORM_LIB_DIR "${wxCOMPILER_PREFIX}${wxARCH_SUFFIX}${lib_suffix}") + + # Generator expression to not create different Debug and Release directories + set(GEN_EXPR_DIR "$<1:/>") +diff --git a/build/cmake/install.cmake b/build/cmake/install.cmake +index a373983043..2e1ace7bf9 100644 +--- a/build/cmake/install.cmake ++++ b/build/cmake/install.cmake +@@ -63,7 +63,7 @@ else() + + install(DIRECTORY DESTINATION "bin") + install(CODE "execute_process( \ +- COMMAND ${CMAKE_COMMAND} -E create_symlink \ ++ COMMAND ${CMAKE_COMMAND} -E copy \ + \"\$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}/lib/wx/config/${wxBUILD_FILE_ID}\" \ + \"\$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}/bin/wx-config\" \ + )" +diff --git a/build/cmake/utils/CMakeLists.txt b/build/cmake/utils/CMakeLists.txt +index 15f4339ef9..f93849e025 100644 +--- a/build/cmake/utils/CMakeLists.txt ++++ b/build/cmake/utils/CMakeLists.txt +@@ -39,7 +39,7 @@ if(wxUSE_XRC) + + # Don't use wx_install() here to preserve escaping. + install(CODE "execute_process( \ +- COMMAND ${CMAKE_COMMAND} -E create_symlink \ ++ COMMAND ${CMAKE_COMMAND} -E copy \ + \"\$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}/bin/${wxrc_output_name}${EXE_SUFFIX}\" \ + \"\$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}/bin/wxrc${EXE_SUFFIX}\" \ + )" diff --git a/ports/wxwidgets/nanosvg-ext-depend.patch b/ports/wxwidgets/nanosvg-ext-depend.patch new file mode 100644 index 0000000..f76a456 --- /dev/null +++ b/ports/wxwidgets/nanosvg-ext-depend.patch @@ -0,0 +1,13 @@ +diff --git a/build/cmake/wxWidgetsConfig.cmake.in b/build/cmake/wxWidgetsConfig.cmake.in +index b251109..60cf762 100644 +--- a/build/cmake/wxWidgetsConfig.cmake.in ++++ b/build/cmake/wxWidgetsConfig.cmake.in +@@ -1,5 +1,8 @@ + @PACKAGE_INIT@ + ++include(CMakeFindDependencyMacro) ++find_dependency(NanoSVG CONFIG) ++ + cmake_policy(PUSH) + # Set policies to prevent warnings + if(POLICY CMP0057) diff --git a/ports/wxwidgets/portfile.cmake b/ports/wxwidgets/portfile.cmake new file mode 100644 index 0000000..b2217a2 --- /dev/null +++ b/ports/wxwidgets/portfile.cmake @@ -0,0 +1,283 @@ +vcpkg_from_github( + OUT_SOURCE_PATH SOURCE_PATH + REPO wxWidgets/wxWidgets + REF "v${VERSION}" + SHA512 8ad17582c4ba721ffe76ada4bb8bd7bc4b050491220aca335fd0506a51354fb789d5bc3d965f0f459dc81784d6427c88272e2acc2099cddf73730231b5a16f62 + HEAD_REF master + PATCHES + install-layout.patch + relocatable-wx-config.patch + nanosvg-ext-depend.patch + fix-libs-export.patch + fix-pcre2.patch + gtk3-link-libraries.patch + sdl2.patch +) + +# Submodule dependencies +vcpkg_from_github( + OUT_SOURCE_PATH lexilla_SOURCE_PATH + REPO wxWidgets/lexilla + REF "27c20a6ae5eebf418debeac0166052ed6fb653bc" + SHA512 7e5de7f664509473b691af8261fca34c2687772faca7260eeba5f2984516e6f8edf88c27192e056c9dda996e2ad2c20f6d1dff1c4bd2f3c0d74852cb50ca424a + HEAD_REF wx +) +file(COPY "${lexilla_SOURCE_PATH}/" DESTINATION "${SOURCE_PATH}/src/stc/lexilla") +vcpkg_from_github( + OUT_SOURCE_PATH scintilla_SOURCE_PATH + REPO wxWidgets/scintilla + REF "0b90f31ced23241054e8088abb50babe9a44ae67" + SHA512 db1f3007f4bd8860fad0817b6cf87980a4b713777025128cf5caea8d6d17b6fafe23fd22ff6886d7d5a420f241d85b7502b85d7e52b4ddb0774edc4b0a0203e7 + HEAD_REF wx +) +file(COPY "${scintilla_SOURCE_PATH}/" DESTINATION "${SOURCE_PATH}/src/stc/scintilla") + +vcpkg_check_features( + OUT_FEATURE_OPTIONS FEATURE_OPTIONS + FEATURES + fonts wxUSE_PRIVATE_FONTS + media wxUSE_MEDIACTRL + secretstore wxUSE_SECRETSTORE + sound wxUSE_SOUND + webview wxUSE_WEBVIEW +) + +if(NOT VCPKG_TARGET_IS_WINDOWS) + vcpkg_check_features(OUT_FEATURE_OPTIONS CHROMIUM_FEATURE_OPTIONS FEATURES + webview-chromium wxUSE_WEBVIEW_CHROMIUM + ) + list(APPEND FEATURE_OPTIONS ${CHROMIUM_FEATURE_OPTIONS}) +endif() + +# Only use wxUSE_WEBVIEW_EDGE on Windows (webview2) +if(VCPKG_TARGET_IS_WINDOWS AND "webview" IN_LIST FEATURES) + list(APPEND FEATURE_OPTIONS "-DwxUSE_WEBVIEW_EDGE=ON") +endif() + +# --------------------------------------------------------------------------- +# Chromium / CEF backend for wxWebView +# --------------------------------------------------------------------------- +# The upstream webview_chromium/CMakeLists.txt tries to download a CEF binary +# distribution or use a local one via add_subdirectory(). In vcpkg we already +# have a pre-built CEF overlay port, so we replace that file with a minimal +# version that uses find_package(CEF CONFIG) instead. +if("webview-chromium" IN_LIST FEATURES) + file(WRITE "${SOURCE_PATH}/build/cmake/lib/webview_chromium/CMakeLists.txt" +[=[ +############################################################################# +# webview_chromium/CMakeLists.txt (vcpkg overlay – uses pre-built CEF) +############################################################################# + +# Locate the CEF package installed by the vcpkg overlay port. +find_package(CEF CONFIG REQUIRED) + +# wxWidgets source files use: #include "include/cef_client.h" +# CEF::wrapper exposes /include as its INTERFACE_INCLUDE_DIRECTORIES, +# but wxWidgets needs the *parent* of that directory on the search path so +# that "include/cef_client.h" resolves correctly. +get_target_property(_cef_include_dirs CEF::wrapper INTERFACE_INCLUDE_DIRECTORIES) +list(GET _cef_include_dirs 0 _cef_inc) +get_filename_component(_cef_prefix "${_cef_inc}" DIRECTORY) + +set(CEF_ROOT "${_cef_prefix}" CACHE PATH "CEF root directory (vcpkg)" FORCE) +wx_lib_include_directories(wxwebview PRIVATE "${_cef_prefix}") + +# Link the webview library against the complete CEF package. +wx_lib_link_libraries(wxwebview PUBLIC CEF::CEF) +]=]) +endif() + +set(OPTIONS_RELEASE "") +if(NOT "debug-support" IN_LIST FEATURES) + list(APPEND OPTIONS_RELEASE "-DwxBUILD_DEBUG_LEVEL=0") +endif() + +set(OPTIONS "") +if(VCPKG_TARGET_IS_WINDOWS AND (VCPKG_TARGET_ARCHITECTURE STREQUAL "arm64" OR VCPKG_TARGET_ARCHITECTURE STREQUAL "arm")) + list(APPEND OPTIONS + -DwxUSE_STACKWALKER=OFF + ) +endif() + +if(VCPKG_TARGET_IS_WINDOWS OR VCPKG_TARGET_IS_OSX) + list(APPEND OPTIONS -DwxUSE_WEBREQUEST_CURL=OFF) +else() + list(APPEND OPTIONS -DwxUSE_WEBREQUEST_CURL=ON) +endif() + +if(VCPKG_TARGET_IS_WINDOWS) + if(VCPKG_CRT_LINKAGE STREQUAL "dynamic") + list(APPEND OPTIONS -DwxBUILD_USE_STATIC_RUNTIME=OFF) + else() + list(APPEND OPTIONS -DwxBUILD_USE_STATIC_RUNTIME=ON) + endif() +endif() + +if(VCPKG_TARGET_IS_WINDOWS AND "webview" IN_LIST FEATURES AND VCPKG_LIBRARY_LINKAGE STREQUAL "static") + list(APPEND OPTIONS -DwxUSE_WEBVIEW_EDGE_STATIC=ON) +endif() + +vcpkg_find_acquire_program(PKGCONFIG) + +# This may be set to ON by users in a custom triplet. +# The use of 'WXWIDGETS_USE_STD_CONTAINERS' (ON or OFF) is not API compatible +# which is why it must be set in a custom triplet rather than a port feature. +# For backwards compatibility, we also replace 'wxUSE_STL' (which no longer +# exists) with 'wxUSE_STD_STRING_CONV_IN_WXSTRING' which still exists and was +# set by `wxUSE_STL` previously. +if(NOT DEFINED WXWIDGETS_USE_STL) + set(WXWIDGETS_USE_STL OFF) +endif() + +if(NOT DEFINED WXWIDGETS_USE_STD_CONTAINERS) + set(WXWIDGETS_USE_STD_CONTAINERS OFF) +endif() + +vcpkg_cmake_configure( + SOURCE_PATH "${SOURCE_PATH}" + OPTIONS + -DCMAKE_CXX_STANDARD=17 + ${FEATURE_OPTIONS} + -DwxUSE_REGEX=sys + -DwxUSE_ZLIB=sys + -DwxUSE_EXPAT=sys + -DwxUSE_LIBJPEG=sys + -DwxUSE_LIBPNG=sys + -DwxUSE_LIBTIFF=sys + -DwxUSE_NANOSVG=sys + -DwxUSE_LIBWEBP=sys + -DwxUSE_GLCANVAS=ON + -DwxUSE_LIBGNOMEVFS=OFF + -DwxUSE_LIBNOTIFY=OFF + -DwxUSE_STD_STRING_CONV_IN_WXSTRING=${WXWIDGETS_USE_STL} + -DwxUSE_STD_CONTAINERS=${WXWIDGETS_USE_STD_CONTAINERS} + -DwxUSE_UIACTIONSIMULATOR=OFF + -DCMAKE_DISABLE_FIND_PACKAGE_GSPELL=ON + -DCMAKE_DISABLE_FIND_PACKAGE_MSPACK=ON + -DwxBUILD_INSTALL_RUNTIME_DIR:PATH=bin + ${OPTIONS} + "-DPKG_CONFIG_EXECUTABLE=${PKGCONFIG}" + # The minimum cmake version requirement for Cotire is 2.8.12. + # however, we need to declare that the minimum cmake version requirement is at least 3.1 to use CMAKE_PREFIX_PATH as the path to find .pc. + -DPKG_CONFIG_USE_CMAKE_PREFIX_PATH=ON + OPTIONS_RELEASE + ${OPTIONS_RELEASE} + MAYBE_UNUSED_VARIABLES + CMAKE_DISABLE_FIND_PACKAGE_GSPOLL + CMAKE_DISABLE_FIND_PACKAGE_MSPACK +) + +vcpkg_cmake_install() +vcpkg_cmake_config_fixup(CONFIG_PATH lib/cmake/wxWidgets) + +# The CMake export is not ready for use: It lacks a config file. +file(REMOVE_RECURSE + ${CURRENT_PACKAGES_DIR}/lib/cmake + ${CURRENT_PACKAGES_DIR}/debug/lib/cmake +) + +set(tools wxrc) +if(NOT VCPKG_TARGET_IS_WINDOWS) + list(APPEND tools wxrc-3.3) + file(MAKE_DIRECTORY "${CURRENT_PACKAGES_DIR}/tools/${PORT}") + file(RENAME "${CURRENT_PACKAGES_DIR}/bin/wx-config" "${CURRENT_PACKAGES_DIR}/tools/${PORT}/wx-config") + if(NOT VCPKG_BUILD_TYPE) + file(MAKE_DIRECTORY "${CURRENT_PACKAGES_DIR}/tools/${PORT}/debug") + file(RENAME "${CURRENT_PACKAGES_DIR}/debug/bin/wx-config" "${CURRENT_PACKAGES_DIR}/tools/${PORT}/debug/wx-config") + endif() +endif() +vcpkg_copy_tools(TOOL_NAMES ${tools} AUTO_CLEAN) + +# do the copy pdbs now after the dlls got moved to the expected /bin folder above +vcpkg_copy_pdbs() + +file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/include/msvc") +file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/include") +file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/lib/mswu") +if(VCPKG_BUILD_TYPE STREQUAL "release") + file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/lib/mswud") +endif() + +file(GLOB_RECURSE INCLUDES "${CURRENT_PACKAGES_DIR}/include/*.h") +if(EXISTS "${CURRENT_PACKAGES_DIR}/lib/mswu/wx/setup.h") + list(APPEND INCLUDES "${CURRENT_PACKAGES_DIR}/lib/mswu/wx/setup.h") +endif() +if(EXISTS "${CURRENT_PACKAGES_DIR}/debug/lib/mswud/wx/setup.h") + list(APPEND INCLUDES "${CURRENT_PACKAGES_DIR}/debug/lib/mswud/wx/setup.h") +endif() +foreach(INC IN LISTS INCLUDES) + file(READ "${INC}" _contents) + if(VCPKG_LIBRARY_LINKAGE STREQUAL "static") + string(REPLACE "defined(WXUSINGDLL)" "0" _contents "${_contents}") + else() + string(REPLACE "defined(WXUSINGDLL)" "1" _contents "${_contents}") + endif() + # Remove install prefix from setup.h to ensure package is relocatable + string(REGEX REPLACE "\n#define wxINSTALL_PREFIX [^\n]*" "\n#define wxINSTALL_PREFIX \"\"" _contents "${_contents}") + file(WRITE "${INC}" "${_contents}") +endforeach() + +if(NOT EXISTS "${CURRENT_PACKAGES_DIR}/include/wx/setup.h") + file(GLOB_RECURSE WX_SETUP_H_FILES_DBG "${CURRENT_PACKAGES_DIR}/debug/lib/*.h") + file(GLOB_RECURSE WX_SETUP_H_FILES_REL "${CURRENT_PACKAGES_DIR}/lib/*.h") + + if(NOT DEFINED VCPKG_BUILD_TYPE OR VCPKG_BUILD_TYPE STREQUAL "release") + vcpkg_replace_string("${WX_SETUP_H_FILES_REL}" "${CURRENT_PACKAGES_DIR}" "" IGNORE_UNCHANGED) + + string(REPLACE "${CURRENT_PACKAGES_DIR}/lib/" "" WX_SETUP_H_FILES_REL "${WX_SETUP_H_FILES_REL}") + string(REPLACE "/setup.h" "" WX_SETUP_H_REL_RELATIVE "${WX_SETUP_H_FILES_REL}") + endif() + if(NOT DEFINED VCPKG_BUILD_TYPE OR VCPKG_BUILD_TYPE STREQUAL "debug") + vcpkg_replace_string("${WX_SETUP_H_FILES_DBG}" "${CURRENT_PACKAGES_DIR}" "" IGNORE_UNCHANGED) + + string(REPLACE "${CURRENT_PACKAGES_DIR}/debug/lib/" "" WX_SETUP_H_FILES_DBG "${WX_SETUP_H_FILES_DBG}") + string(REPLACE "/setup.h" "" WX_SETUP_H_DBG_RELATIVE "${WX_SETUP_H_FILES_DBG}") + endif() + + configure_file("${CMAKE_CURRENT_LIST_DIR}/setup.h.in" "${CURRENT_PACKAGES_DIR}/include/wx/setup.h" @ONLY) +endif() + +file(GLOB configs LIST_DIRECTORIES false "${CURRENT_PACKAGES_DIR}/lib/wx/config/*" "${CURRENT_PACKAGES_DIR}/tools/${PORT}/wx-config") +foreach(config IN LISTS configs) + vcpkg_replace_string("${config}" "${CURRENT_INSTALLED_DIR}" [[${prefix}]]) +endforeach() +file(GLOB configs LIST_DIRECTORIES false "${CURRENT_PACKAGES_DIR}/debug/lib/wx/config/*" "${CURRENT_PACKAGES_DIR}/tools/${PORT}/debug/wx-config") +foreach(config IN LISTS configs) + vcpkg_replace_string("${config}" "${CURRENT_INSTALLED_DIR}/debug" [[${prefix}]]) +endforeach() + +# For CMake multi-config in connection with wrapper +if(EXISTS "${CURRENT_PACKAGES_DIR}/debug/lib/mswud/wx/setup.h") + file(INSTALL "${CURRENT_PACKAGES_DIR}/debug/lib/mswud/wx/setup.h" + DESTINATION "${CURRENT_PACKAGES_DIR}/lib/mswud/wx" + ) +endif() + +if(NOT "debug-support" IN_LIST FEATURES) + if(VCPKG_TARGET_IS_WINDOWS) + vcpkg_replace_string("${CURRENT_PACKAGES_DIR}/include/wx/debug.h" "#define wxDEBUG_LEVEL 1" "#define wxDEBUG_LEVEL 0") + else() + vcpkg_replace_string("${CURRENT_PACKAGES_DIR}/include/wx-3.3/wx/debug.h" "#define wxDEBUG_LEVEL 1" "#define wxDEBUG_LEVEL 0") + endif() +endif() + +if("example" IN_LIST FEATURES) + file(INSTALL + "${CMAKE_CURRENT_LIST_DIR}/example/CMakeLists.txt" + "${SOURCE_PATH}/samples/popup/popup.cpp" + "${SOURCE_PATH}/samples/sample.xpm" + DESTINATION "${CURRENT_PACKAGES_DIR}/share/${PORT}/example" + ) + vcpkg_replace_string("${CURRENT_PACKAGES_DIR}/share/${PORT}/example/popup.cpp" "../sample.xpm" "sample.xpm") +endif() + +configure_file("${CMAKE_CURRENT_LIST_DIR}/vcpkg-cmake-wrapper.cmake" "${CURRENT_PACKAGES_DIR}/share/${PORT}/vcpkg-cmake-wrapper.cmake" @ONLY) + +file(REMOVE "${CURRENT_PACKAGES_DIR}/wxwidgets.props") +file(REMOVE "${CURRENT_PACKAGES_DIR}/debug/wxwidgets.props") +file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/build") +file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/build") +file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/share") + +file(INSTALL "${CMAKE_CURRENT_LIST_DIR}/usage" DESTINATION "${CURRENT_PACKAGES_DIR}/share/${PORT}") +vcpkg_install_copyright(FILE_LIST "${SOURCE_PATH}/docs/licence.txt") diff --git a/ports/wxwidgets/relocatable-wx-config.patch b/ports/wxwidgets/relocatable-wx-config.patch new file mode 100644 index 0000000..608c9c5 --- /dev/null +++ b/ports/wxwidgets/relocatable-wx-config.patch @@ -0,0 +1,40 @@ +diff --git a/wx-config.in b/wx-config.in +index 4df8571..a90db3d 100644 +--- a/wx-config.in ++++ b/wx-config.in +@@ -398,8 +398,23 @@ is_cross() { [ "x@cross_compiling@" = "xyes" ]; } + + + # Determine the base directories we require. +-prefix=${input_option_prefix-${this_prefix:-@prefix@}} +-exec_prefix=${input_option_exec_prefix-${input_option_prefix-${this_exec_prefix:-@exec_prefix@}}} ++vcpkg_prefix=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd -P) ++case "$vcpkg_prefix" in ++ */lib/wx/config) ++ vcpkg_prefix=${vcpkg_prefix%/*/*/*} ++ ;; ++ */tools/wxwidgets/debug) ++ vcpkg_prefix=${vcpkg_prefix%/*/*/*}/debug ++ ;; ++ */tools/wxwidgets) ++ vcpkg_prefix=${vcpkg_prefix%/*/*} ++ ;; ++esac ++if [ -n "@MINGW@" -a -n "@CMAKE_HOST_WIN32@" ]; then ++ vcpkg_prefix=$(cygpath -m "$vcpkg_prefix") ++fi ++prefix=${input_option_prefix-${this_prefix:-$vcpkg_prefix}} ++exec_prefix=${input_option_exec_prefix-${input_option_prefix-${this_exec_prefix:-$prefix}}} + wxconfdir="@libdir@/wx/config" + + installed_configs=`cd "$wxconfdir" 2> /dev/null && ls | grep -v "^inplace-"` +@@ -936,6 +951,9 @@ prefix=${this_prefix-$prefix} + exec_prefix=${this_exec_prefix-$exec_prefix} + + includedir="@includedir@" ++if [ "@CMAKE_BUILD_TYPE@" = "Debug" ] ; then ++ includedir="${includedir%/debug/include}/include" ++fi + libdir="@libdir@" + bindir="@bindir@" + diff --git a/ports/wxwidgets/sdl2.patch b/ports/wxwidgets/sdl2.patch new file mode 100644 index 0000000..511775c --- /dev/null +++ b/ports/wxwidgets/sdl2.patch @@ -0,0 +1,29 @@ +diff --git a/build/cmake/init.cmake b/build/cmake/init.cmake +index 5447d33..f5440b4 100644 +--- a/build/cmake/init.cmake ++++ b/build/cmake/init.cmake +@@ -530,7 +530,9 @@ if(wxUSE_GUI) + endif() + + if(wxUSE_SOUND AND wxUSE_LIBSDL AND UNIX AND NOT APPLE) +- find_package(SDL2) ++ find_package(SDL2 CONFIG REQUIRED) ++ set(SDL2_INCLUDE_DIR "" CACHE INTERNAL "") ++ set(SDL2_LIBRARY SDL2::SDL2 CACHE INTERNAL "") + if(NOT SDL2_FOUND) + find_package(SDL) + endif() +diff --git a/build/cmake/wxWidgetsConfig.cmake.in b/build/cmake/wxWidgetsConfig.cmake.in +index 60cf762..202a8c3 100644 +--- a/build/cmake/wxWidgetsConfig.cmake.in ++++ b/build/cmake/wxWidgetsConfig.cmake.in +@@ -2,6 +2,9 @@ + + include(CMakeFindDependencyMacro) + find_dependency(NanoSVG CONFIG) ++if("@wxUSE_LIBSDL@") ++ find_dependency(SDL2 CONFIG) ++endif() + + cmake_policy(PUSH) + # Set policies to prevent warnings diff --git a/ports/wxwidgets/setup.h.in b/ports/wxwidgets/setup.h.in new file mode 100644 index 0000000..ad95797 --- /dev/null +++ b/ports/wxwidgets/setup.h.in @@ -0,0 +1,5 @@ +#ifdef _DEBUG +#include "../../debug/lib/@WX_SETUP_H_DBG_RELATIVE@/setup.h" +#else +#include "../../lib/@WX_SETUP_H_REL_RELATIVE@/setup.h" +#endif diff --git a/ports/wxwidgets/usage b/ports/wxwidgets/usage new file mode 100644 index 0000000..209456e --- /dev/null +++ b/ports/wxwidgets/usage @@ -0,0 +1,4 @@ +The package wxwidgets provides CMake targets: + + find_package(wxWidgets CONFIG REQUIRED) + target_link_libraries(main PRIVATE wx::core wx::base) diff --git a/ports/wxwidgets/vcpkg-cmake-wrapper.cmake b/ports/wxwidgets/vcpkg-cmake-wrapper.cmake new file mode 100644 index 0000000..f2a379a --- /dev/null +++ b/ports/wxwidgets/vcpkg-cmake-wrapper.cmake @@ -0,0 +1,87 @@ +cmake_policy(PUSH) +cmake_policy(SET CMP0012 NEW) +cmake_policy(SET CMP0054 NEW) +cmake_policy(SET CMP0057 NEW) + +get_filename_component(_vcpkg_wx_root "${CMAKE_CURRENT_LIST_DIR}/../.." ABSOLUTE) +set(wxWidgets_ROOT_DIR "${_vcpkg_wx_root}" CACHE INTERNAL "") +set(WX_ROOT_DIR "${_vcpkg_wx_root}" CACHE INTERNAL "") +unset(_vcpkg_wx_root) + +if(WIN32) + # Find all libs with "33" infix which is unknown to FindwxWidgets.cmake + function(z_vcpkg_wxwidgets_find_base_library BASENAME) + find_library(WX_${BASENAME}d wx${BASENAME}33ud NAMES wx${BASENAME}d PATHS "${wxWidgets_ROOT_DIR}/debug/lib" NO_DEFAULT_PATH) + find_library(WX_${BASENAME} wx${BASENAME}33u NAMES wx${BASENAME} PATHS "${wxWidgets_ROOT_DIR}/lib" NO_DEFAULT_PATH REQUIRED) + endfunction() + function(z_vcpkg_wxwidgets_find_suffix_library BASENAME) + foreach(lib IN LISTS ARGN) + find_library(WX_${lib}d NAMES wx${BASENAME}33ud_${lib} PATHS "${wxWidgets_ROOT_DIR}/debug/lib" NO_DEFAULT_PATH) + find_library(WX_${lib} NAMES wx${BASENAME}33u_${lib} PATHS "${wxWidgets_ROOT_DIR}/lib" NO_DEFAULT_PATH) + endforeach() + endfunction() + z_vcpkg_wxwidgets_find_base_library(base) + z_vcpkg_wxwidgets_find_suffix_library(base net odbc xml) + z_vcpkg_wxwidgets_find_suffix_library(msw core adv aui html media xrc dbgrid gl qa richtext stc ribbon propgrid webview) + if(WX_stc AND "@VCPKG_LIBRARY_LINKAGE@" STREQUAL "static") + z_vcpkg_wxwidgets_find_base_library(scintilla) + endif() + # Force FindwxWidgets.cmake win32 mode for all windows targets built on windows + set(_vcpkg_wxwidgets_backup_crosscompiling "${CMAKE_CROSSCOMPILING}") + set(CMAKE_CROSSCOMPILING 0) + set(wxWidgets_LIB_DIR "${wxWidgets_ROOT_DIR}/lib" CACHE INTERNAL "") +else() + # FindwxWidgets.cmake unix mode, single-config + set(_vcpkg_wxconfig "") + if(CMAKE_BUILD_TYPE STREQUAL "Debug" OR "Debug" IN_LIST MAP_IMPORTED_CONFIG_${CMAKE_BUILD_TYPE}) + # Debug + set(wxWidgets_LIB_DIR "${wxWidgets_ROOT_DIR}/debug/lib" CACHE INTERNAL "") + file(GLOB _vcpkg_wxconfig LIST_DIRECTORIES false "${wxWidgets_LIB_DIR}/wx/config/*") + endif() + if(NOT _vcpkg_wxconfig) + # Release or fallback + set(wxWidgets_LIB_DIR "${wxWidgets_ROOT_DIR}/lib" CACHE INTERNAL "") + file(GLOB _vcpkg_wxconfig LIST_DIRECTORIES false "${wxWidgets_LIB_DIR}/wx/config/*") + endif() + set(wxWidgets_CONFIG_EXECUTABLE "${_vcpkg_wxconfig}" CACHE INTERNAL "") + unset(_vcpkg_wxconfig) +endif() +set(WX_LIB_DIR "${wxWidgets_LIB_DIR}" CACHE INTERNAL "") + +# https://gitlab.kitware.com/cmake/cmake/-/issues/26718 +# Instead of special-casing the `atomic` library, we skip the checks entirely. +set(_wx_lib_found TRUE) + +_find_package(${ARGS}) + +unset(_wx_lib_found) + +if(DEFINED _vcpkg_wxwidgets_backup_crosscompiling) + set(CMAKE_CROSSCOMPILING "${_vcpkg_wxwidgets_backup_crosscompiling}") + unset(_vcpkg_wxwidgets_backup_crosscompiling) +endif() + +if("@VCPKG_LIBRARY_LINKAGE@" STREQUAL "static" AND NOT "wx::core" IN_LIST wxWidgets_LIBRARIES) + find_package(NanoSVG CONFIG QUIET) + list(APPEND wxWidgets_LIBRARIES + NanoSVG::nanosvg NanoSVG::nanosvgrast + ) +endif() + + +if(WIN32 AND "@VCPKG_LIBRARY_LINKAGE@" STREQUAL "static" AND NOT "wx::core" IN_LIST wxWidgets_LIBRARIES) + find_package(EXPAT QUIET) + find_package(JPEG QUIET) + find_package(PNG QUIET) + find_package(TIFF QUIET) + find_package(ZLIB QUIET) + list(APPEND wxWidgets_LIBRARIES + ${EXPAT_LIBRARIES} + ${JPEG_LIBRARIES} + ${PNG_LIBRARIES} + ${TIFF_LIBRARIES} + ${ZLIB_LIBRARIES} + ) +endif() + +cmake_policy(POP) diff --git a/ports/wxwidgets/vcpkg.json b/ports/wxwidgets/vcpkg.json new file mode 100644 index 0000000..f1cafbe --- /dev/null +++ b/ports/wxwidgets/vcpkg.json @@ -0,0 +1,129 @@ +{ + "name": "wxwidgets", + "version": "3.3.1", + "port-version": 1, + "description": [ + "Widget toolkit and tools library for creating graphical user interfaces (GUIs) for cross-platform applications. ", + "Set WXWIDGETS_USE_STL in a custom triplet to build with the wxUSE_STL build option.", + "Set WXWIDGETS_USE_STD_CONTAINERS in a custom triplet to build with the wxUSE_STD_CONTAINERS build option." + ], + "homepage": "https://github.com/wxWidgets/wxWidgets", + "license": "LGPL-2.0-or-later WITH WxWindows-exception-3.1", + "supports": "!uwp & !xbox", + "dependencies": [ + { + "name": "cairo", + "default-features": false, + "platform": "!windows & !osx & !ios" + }, + { + "name": "curl", + "default-features": false, + "platform": "!windows & !osx" + }, + "expat", + { + "name": "gtk3", + "platform": "!windows & !osx & !ios" + }, + { + "name": "libiconv", + "platform": "!windows" + }, + "libjpeg-turbo", + "libpng", + "libwebp", + "nanosvg", + "opengl", + { + "name": "pcre2", + "default-features": false + }, + { + "name": "tiff", + "default-features": false + }, + { + "name": "vcpkg-cmake", + "host": true + }, + { + "name": "vcpkg-cmake-config", + "host": true + }, + "zlib" + ], + "default-features": [ + "debug-support", + "sound" + ], + "features": { + "debug-support": { + "description": "Enable wxWidgets debugging support hooks even for release builds (wxDEBUG_LEVEL 1)" + }, + "example": { + "description": "Example source code and CMake project" + }, + "fonts": { + "description": "Enable to use the font functionality of wxWidgets", + "dependencies": [ + { + "name": "fontconfig", + "platform": "!windows & !osx" + }, + { + "name": "pango", + "platform": "!windows & !osx" + } + ] + }, + "media": { + "description": "Build wxMediaCtrl support", + "dependencies": [ + { + "name": "gstreamer", + "default-features": false, + "platform": "!windows & !osx & !ios" + } + ] + }, + "secretstore": { + "description": "Use wxSecretStore class" + }, + "sound": { + "description": "Build wxSound support", + "dependencies": [ + { + "name": "sdl2", + "default-features": false, + "platform": "!windows & !osx & !ios" + } + ] + }, + "webview": { + "description": "The Edge backend uses Microsoft's Edge WebView2", + "dependencies": [ + { + "name": "webview2", + "platform": "windows" + } + ] + }, + "webview-chromium": { + "description": "Enable Chromium (CEF) backend for wxWebView", + "dependencies": [ + { + "name": "cef", + "platform": "!windows" + }, + { + "name": "wxwidgets", + "default-features": false, + "features": [ + "webview" + ] + } + ] + } + } +} diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..851ee36 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,28 @@ +file(GLOB WEBFRAME_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) +set(WEBFRAME_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../include) + +if(BUILD_RUNTIME) + if(WIN32) + file(GLOB WEBFRAME_DESKTOP_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/runtimes/desktop/*.cpp) + webframe_add_runtime(NAME desktop + SOURCES ${WEBFRAME_SOURCES} ${WEBFRAME_DESKTOP_SOURCES} + PUBLIC_INCLUDE_DIRS ${WEBFRAME_INCLUDE_DIR} + PRIVATE_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/runtimes/desktop + PUBLIC_LINK_LIBRARIES wx::core wx::webview) + target_compile_definitions(webframe_desktop PUBLIC WEBFRAME_DESKTOP_RUNTIME=1) + endif() + file(GLOB WEBFRAME_SERVER_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/runtimes/server/*.cpp) + webframe_add_runtime(NAME server + SOURCES ${WEBFRAME_SOURCES} ${WEBFRAME_SERVER_SOURCES} + PUBLIC_INCLUDE_DIRS ${WEBFRAME_INCLUDE_DIR} + PRIVATE_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/runtimes/server + PUBLIC_LINK_LIBRARIES libevent::core libevent::extra) +endif() + +if(BUILD_TESTING) + file(GLOB WEBFRAME_MOCK_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/runtimes/mock/*.cpp) + webframe_add_runtime(NAME mock + SOURCES ${WEBFRAME_SOURCES} ${WEBFRAME_MOCK_SOURCES} + PUBLIC_INCLUDE_DIRS ${WEBFRAME_INCLUDE_DIR} + PRIVATE_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/runtimes/mock) +endif() \ No newline at end of file diff --git a/src/application.cpp b/src/application.cpp new file mode 100644 index 0000000..0a6829f --- /dev/null +++ b/src/application.cpp @@ -0,0 +1,24 @@ +#include + +namespace webframe +{ + void application::configure_desktop() + { + + } + + void application::configure_server(int argc, const char **argv) + { + + } + + void application::configure_router(router *ctrl) + { + + } + + void application::on_dispatch() + { + + } +} \ No newline at end of file diff --git a/src/exception.cpp b/src/exception.cpp new file mode 100644 index 0000000..7cd9ab6 --- /dev/null +++ b/src/exception.cpp @@ -0,0 +1,29 @@ +#include + +namespace webframe +{ + exception::exception(int status_code, const std::string &message, const std::string &content_type) + : _status_code(status_code), _message(message), _content_type(content_type) {} + + int exception::get_status_code() const + { + return _status_code; + } + + std::string exception::get_message() const + { + return _message; + } + + std::string exception::get_content_type() const + { + return _content_type; + } + + exception exception::bad_request(400, "Bad Request"); + exception exception::unauthorized(401, "Unauthorized"); + exception exception::forbidden(403, "Forbidden"); + exception exception::not_found(404, "Not Found"); + exception exception::method_not_allowed(405, "Method Not Allowed"); + exception exception::internal_server_error(500, "Internal Server Error"); +} \ No newline at end of file diff --git a/src/handler.cpp b/src/handler.cpp new file mode 100644 index 0000000..14a09f4 --- /dev/null +++ b/src/handler.cpp @@ -0,0 +1,55 @@ +#include + +namespace webframe +{ + void handler::handle_request(const request *req, response *res) + { + try + { + switch (req->get_method()) + { + case method::http_get: + handle_get(req, res); + break; + case method::http_post: + handle_post(req, res); + break; + case method::http_put: + handle_put(req, res); + break; + case method::http_delete: + handle_delete(req, res); + break; + default: + throw exception::bad_request; + } + } + catch (const exception &e) + { + res->set_status(e.get_status_code()); + res->set_header("Content-Type", e.get_content_type()); + const std::string &message = e.get_message(); + res->set_body(reinterpret_cast(message.data()), message.size()); + } + } + + void handler::handle_get(const request *req, response *res) + { + throw exception::method_not_allowed; + } + + void handler::handle_post(const request *req, response *res) + { + throw exception::method_not_allowed; + } + + void handler::handle_put(const request *req, response *res) + { + throw exception::method_not_allowed; + } + + void handler::handle_delete(const request *req, response *res) + { + throw exception::method_not_allowed; + } +} \ No newline at end of file diff --git a/src/router.cpp b/src/router.cpp new file mode 100644 index 0000000..7cc2953 --- /dev/null +++ b/src/router.cpp @@ -0,0 +1,54 @@ +#include + +class global_default_handler : public webframe::handler +{ +protected: + void handle_get(const webframe::request* req, webframe::response* res) override + { + throw webframe::exception::not_found; + } + + void handle_post(const webframe::request* req, webframe::response* res) override + { + throw webframe::exception::not_found; + } + + void handle_put(const webframe::request* req, webframe::response* res) override + { + throw webframe::exception::not_found; + } + + void handle_delete(const webframe::request* req, webframe::response* res) override + { + throw webframe::exception::not_found; + } +}; + +static global_default_handler g_default_handler; + +namespace webframe +{ + void router::add_route(const std::string& path, handler* h) + { + _handlers[path] = h; + } + + void router::set_default(handler* h) + { + _default_handler = h; + } + + handler* router::find_route(const std::string& path) + { + auto it = _handlers.find(path); + if (it != _handlers.end()) { + return it->second; + } + return get_default(); + } + + handler* router::get_default() + { + return _default_handler ? _default_handler : &g_default_handler; + } +} \ No newline at end of file diff --git a/src/runtimes/desktop/desktop.hpp b/src/runtimes/desktop/desktop.hpp new file mode 100644 index 0000000..0a17523 --- /dev/null +++ b/src/runtimes/desktop/desktop.hpp @@ -0,0 +1,66 @@ +#ifndef WEBFRAME_DESKTOP_HPP +#define WEBFRAME_DESKTOP_HPP + +#include + +#include +#include +#include + +class wfApp; + +namespace webframe::desktop +{ + class request : public webframe::request + { + public: + request(const wxWebViewHandlerRequest *request); + ~request() = default; + webframe::method get_method() const override; + std::string get_path() const override; + bool get_header(const std::string &key, std::string &value) const override; + std::pair get_body() const override; + void read_body(const std::function &callback) const override; + private: + const wxWebViewHandlerRequest *_request; + wxString _body; + }; + + class response : public webframe::response + { + public: + response(wxWebViewHandlerResponse *response, bool &sent); + ~response() = default; + void set_status(int status) override; + void set_header(const std::string &key, const std::string &value) override; + void set_body(const uint8_t *data, size_t size) override; + void write_body(const std::function &)> &callback) override; + private: + wxWebViewHandlerResponse *_response; + bool &_sent; + }; + + class webview_handler : public wxWebViewHandler + { + public: + webview_handler(webframe::router *router); + ~webview_handler() = default; + void StartRequest(const wxWebViewHandlerRequest& request, wxSharedPtr response) override; + private: + webframe::router *_router; + }; + + class runtime : public webframe::runtime + { + public: +#ifdef WEBFRAME_WIN32_APP + int dispatch(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow, webframe::application *a, webframe::router *r) override; +#else + int dispatch(int argc, const char **argv, webframe::application *a, webframe::router *r) override; +#endif + private: + int launch_wx_app(wfApp *app, webframe::application *a, webframe::router *r); + }; +} + +#endif \ No newline at end of file diff --git a/src/runtimes/desktop/request.cpp b/src/runtimes/desktop/request.cpp new file mode 100644 index 0000000..7521714 --- /dev/null +++ b/src/runtimes/desktop/request.cpp @@ -0,0 +1,75 @@ +#include + +namespace webframe +{ + namespace desktop + { + request::request(const wxWebViewHandlerRequest *request) : _request(request) + { + _body = _request->GetDataString(); + } + + webframe::method request::get_method() const + { + const wxString method = _request->GetMethod(); + webframe::method result; + if (method == "GET") + { + result = webframe::method::http_get; + } + else if (method == "POST") + { + result = webframe::method::http_post; + } + else if (method == "PUT") + { + result = webframe::method::http_put; + } + else if (method == "DELETE") + { + result = webframe::method::http_delete; + } + else + { + result = static_cast(-1); + } + return result; + } + + std::string request::get_path() const + { + wxURI uri = _request->GetURI(); + wxString uri_path = uri.GetPath(); + if (!uri_path.StartsWith("/")) + { + uri_path.Prepend("/"); + } + return std::string(uri_path.ToStdString()); + } + + bool request::get_header(const std::string &key, std::string &value) const + { + wxString header_value = _request->GetHeader(key); + bool result(false); + if (!header_value.empty()) + { + value = std::string(header_value.ToStdString()); + result = true; + } + return result; + } + + std::pair request::get_body() const + { + const uint8_t *data = reinterpret_cast(_body.GetData().AsCharBuf().data()); + size_t size = _body.size(); + return { data, size }; + } + + void request::read_body(const std::function &callback) const + { + const auto& [data, size] = get_body(); + callback(data, size); + } + } +} \ No newline at end of file diff --git a/src/runtimes/desktop/response.cpp b/src/runtimes/desktop/response.cpp new file mode 100644 index 0000000..a6ac3e9 --- /dev/null +++ b/src/runtimes/desktop/response.cpp @@ -0,0 +1,46 @@ +#include + +namespace webframe +{ + namespace desktop + { + response::response(wxWebViewHandlerResponse *response, bool &sent) : _response(response), _sent(sent) + { + } + + void response::set_status(int status) + { + _response->SetStatus(status); + } + + void response::set_header(const std::string &key, const std::string &value) + { + _response->SetHeader(key, value); + } + + void response::set_body(const uint8_t *data, size_t size) + { + _response->Finish(std::string(reinterpret_cast(data), size)); + _sent = true; + } + + void response::write_body(const std::function &)> &callback) + { + std::vector buffer; + bool has_more_data = true; + while (has_more_data) + { + std::pair data; + if (callback(data)) + { + buffer.insert(buffer.end(), data.first, data.first + data.second); + } + else + { + has_more_data = false; + } + } + set_body(buffer.data(), buffer.size()); + } + } +} \ No newline at end of file diff --git a/src/runtimes/desktop/runtime.cpp b/src/runtimes/desktop/runtime.cpp new file mode 100644 index 0000000..d4f60df --- /dev/null +++ b/src/runtimes/desktop/runtime.cpp @@ -0,0 +1,86 @@ +#include + +class wfApp : public wxApp +{ +public: + void Init(webframe::application *application, webframe::router *router) + { + _application = application; + _router = router; + } + bool OnInit() override + { + _application->configure_desktop(); + _application->configure_router(_router); + _frame = std::make_unique(nullptr, wxID_ANY, "WebFrame"); + _webview = wxWebView::New(_frame.get(), wxID_ANY, "about:blank", wxDefaultPosition, wxDefaultSize, wxWebViewBackendEdge); +#ifdef __WXMSW__ + _webview->Bind(wxEVT_WEBVIEW_NAVIGATING, [](wxWebViewEvent &event) + { + const wxString url = event.GetURL(); + if (!url.StartsWith("https://webframe.ipc")) + { + event.Skip(); + } }); +#endif + _webview->Bind(wxEVT_WEBVIEW_CREATED, [this](wxWebViewEvent &event) + { + wxWebView *webview = reinterpret_cast(event.GetEventObject()); + webview->RegisterHandler(wxSharedPtr(new webframe::desktop::webview_handler(_router))); + webview->LoadURL("https://webframe.ipc/index.html"); + }); + _frame->Show(); + return true; + } + +private: + std::unique_ptr _frame; + wxWebView *_webview; + webframe::application *_application; + webframe::router *_router; +}; + +namespace webframe +{ + namespace desktop + { + int runtime::launch_wx_app(wfApp *app, webframe::application *a, webframe::router *r) + { + int result(0); + app->Init(a, r); + if (app->CallOnInit()) + { + a->on_dispatch(); + result = app->OnRun(); + } + else + { + result = 1; + } + wxEntryCleanup(); + return result; + } + } + + runtime *webframe_init() + { + return new desktop::runtime(); + } +} + +// keep platform specific garbage down here +wxIMPLEMENT_APP_NO_MAIN(wfApp); +#ifdef WEBFRAME_WIN32_APP +#include +int webframe::desktop::runtime::dispatch(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow, webframe::application *a, webframe::router *r) +{ + wxEntryStart(hInstance); + return launch_wx_app(reinterpret_cast(wxTheApp), a, r); +} +#else +int webframe::desktop::runtime::dispatch(int argc, const char **argv, webframe::application *a, webframe::router *r) +{ + wxEntryStart(argc, const_cast(argv)); + return launch_wx_app(reinterpret_cast(wxTheApp), a, r); +} +#endif \ No newline at end of file diff --git a/src/runtimes/desktop/webview_handler.cpp b/src/runtimes/desktop/webview_handler.cpp new file mode 100644 index 0000000..f85667e --- /dev/null +++ b/src/runtimes/desktop/webview_handler.cpp @@ -0,0 +1,25 @@ +#include + +namespace webframe +{ + namespace desktop + { + webview_handler::webview_handler(webframe::router *router) : wxWebViewHandler("https"), _router(router) + { + SetVirtualHost("webframe.ipc"); + } + + void webview_handler::StartRequest(const wxWebViewHandlerRequest& request, wxSharedPtr response) + { + bool sent(false); + webframe::desktop::request req(&request); + webframe::desktop::response res(response.get(), sent); + webframe::handler *handler = _router->find_route(req.get_path()); + handler->handle_request(&req, &res); + if (!sent) + { + response->Finish(""); + } + } + } +} \ No newline at end of file diff --git a/src/runtimes/mock/runtime.cpp b/src/runtimes/mock/runtime.cpp new file mode 100644 index 0000000..28868a6 --- /dev/null +++ b/src/runtimes/mock/runtime.cpp @@ -0,0 +1,36 @@ +#include + +#include + +namespace webframe +{ + namespace mock + { + class runtime : public webframe::runtime + { + public: + int dispatch(int argc, const char ** argv, application *a, router *r) override + { + int result(0); + try + { + a->configure_desktop(); + a->configure_server(argc, argv); + a->configure_router(r); + a->on_dispatch(); + } + catch(const std::exception& e) + { + std::cerr << e.what() << std::endl; + result = 1; + } + return result; + } + }; + } + + runtime *webframe_init() + { + return new mock::runtime(); + } +} diff --git a/src/runtimes/server/request.cpp b/src/runtimes/server/request.cpp new file mode 100644 index 0000000..e8895b3 --- /dev/null +++ b/src/runtimes/server/request.cpp @@ -0,0 +1,68 @@ +#include + +namespace webframe::server +{ + request::request(struct evhttp_request *req) : _req(req) + { + _headers = evhttp_request_get_input_headers(_req); + struct evbuffer *input_buffer = evhttp_request_get_input_buffer(_req); + size_t body_size = evbuffer_get_length(input_buffer); + _body.resize(body_size); + evbuffer_copyout(input_buffer, _body.data(), body_size); + } + + webframe::method request::get_method() const + { + webframe::method method; + switch(evhttp_request_get_command(_req)) + { + case EVHTTP_REQ_GET: + method = webframe::method::http_get; + break; + case EVHTTP_REQ_POST: + method = webframe::method::http_post; + break; + case EVHTTP_REQ_PUT: + method = webframe::method::http_put; + break; + case EVHTTP_REQ_DELETE: + method = webframe::method::http_delete; + break; + default: + method = static_cast(-1); + break; + } + return method; + + } + + std::string request::get_path() const + { + const evhttp_uri *uri = evhttp_request_get_evhttp_uri(_req); + const char *path = evhttp_uri_get_path(uri); + return path ? path : ""; + } + + bool request::get_header(const std::string &key, std::string &value) const + { + const char *header_value = evhttp_find_header(_headers, key.c_str()); + bool result(false); + if (header_value) + { + value = header_value; + result = true; + } + return result; + } + + std::pair request::get_body() const + { + return { _body.data(), _body.size() }; + } + + void request::read_body(const std::function &callback) const + { + callback(_body.data(), _body.size()); + } + +} \ No newline at end of file diff --git a/src/runtimes/server/response.cpp b/src/runtimes/server/response.cpp new file mode 100644 index 0000000..2c507ea --- /dev/null +++ b/src/runtimes/server/response.cpp @@ -0,0 +1,46 @@ +#include + +namespace webframe::server +{ + response::response(struct evhttp_request *req, bool &sent_response, int &status) : _req(req), _sent_response(sent_response), _status(status) + { + _headers = evhttp_request_get_output_headers(_req); + } + + void response::set_status(int status) + { + _status = status; + } + + void response::set_header(const std::string &key, const std::string &value) + { + evhttp_add_header(_headers, key.c_str(), value.c_str()); + } + + void response::set_body(const uint8_t *data, size_t size) + { + std::unique_ptr output_buffer(evbuffer_new(), &evbuffer_free); + evbuffer_add(output_buffer.get(), data, size); + evhttp_send_reply(_req, _status, nullptr, output_buffer.get()); + _sent_response = true; + } + + void response::write_body(const std::function &)> &callback) + { + bool has_more = true; + evhttp_send_reply_start(_req, _status, nullptr); + while (has_more) + { + std::unique_ptr output_buffer(evbuffer_new(), &evbuffer_free); + std::pair chunk; + has_more = callback(chunk); + if(chunk.first && chunk.second > 0) + { + evbuffer_add(output_buffer.get(), chunk.first, chunk.second); + } + evhttp_send_reply_chunk(_req, output_buffer.get()); + } + evhttp_send_reply_end(_req); + _sent_response = true; + } +} \ No newline at end of file diff --git a/src/runtimes/server/runtime.cpp b/src/runtimes/server/runtime.cpp new file mode 100644 index 0000000..bcca62c --- /dev/null +++ b/src/runtimes/server/runtime.cpp @@ -0,0 +1,86 @@ +#ifdef _WIN32 +#include +#endif + +#include + +#include + +#ifdef _WIN32 +namespace +{ + class winsock_session + { + public: + winsock_session() + { + const int result = WSAStartup(MAKEWORD(2, 2), &_data); + if (result != 0) + { + throw std::runtime_error("WSAStartup failed"); + } + } + + ~winsock_session() + { + WSACleanup(); + } + + private: + WSADATA _data{}; + }; +} +#endif + +static void evhttp_callback(struct evhttp_request *req, void *arg) +{ + bool sent_response = false; + int status = 200; + webframe::router *runtime = static_cast(arg); + webframe::server::request request(req); + webframe::server::response response(req, sent_response, status); + webframe::handler *handler = runtime->find_route(request.get_path()); + handler->handle_request(&request, &response); + if (!sent_response) + { + evhttp_send_reply(req, status, nullptr, nullptr); + } +} + +namespace webframe +{ + namespace server + { + int runtime::dispatch(int argc, const char **argv, webframe::application *a, webframe::router *r) + { +#ifdef _WIN32 + winsock_session winsock; +#endif + a->configure_server(argc, argv); + a->configure_router(r); + _event_base.reset(event_base_new()); + _http.reset(evhttp_new(_event_base.get())); + evhttp_set_gencb(_http.get(), evhttp_callback, r); + listen("0.0.0.0", 8080); + a->on_dispatch(); + serve(); + return 0; + } + void runtime::listen(const char *address, int port) + { + evhttp_bind_socket(_http.get(), address, port); + } + void runtime::serve() + { + event_base_dispatch(_event_base.get()); + } + void runtime::stop() + { + event_base_loopbreak(_event_base.get()); + } + } + runtime *webframe_init() + { + return new server::runtime(); + } +} \ No newline at end of file diff --git a/src/runtimes/server/server.hpp b/src/runtimes/server/server.hpp new file mode 100644 index 0000000..fc68852 --- /dev/null +++ b/src/runtimes/server/server.hpp @@ -0,0 +1,59 @@ +#ifndef WEBFRAME_SERVER_HPP +#define WEBFRAME_SERVER_HPP + +#include + +#include +#include +#include + +namespace webframe::server +{ + class request : public webframe::request + { + public: + request(struct evhttp_request *req); + ~request() = default; + webframe::method get_method() const override; + std::string get_path() const override; + bool get_header(const std::string &key, std::string &value) const override; + std::pair get_body() const override; + void read_body(const std::function &callback) const override; + private: + struct evhttp_request *_req; + evkeyvalq *_headers; + std::vector _body; + }; + + class response : public webframe::response + { + public: + response(struct evhttp_request *req, bool &sent_response, int &status); + ~response() = default; + void set_status(int status) override; + void set_header(const std::string &key, const std::string &value) override; + void set_body(const uint8_t *data, size_t size) override; + void write_body(const std::function &)> &callback) override; + private: + struct evhttp_request *_req; + evkeyvalq *_headers; + + bool &_sent_response; + int &_status; + }; + + class runtime : public webframe::runtime + { + public: + int dispatch(int argc, const char **argv, webframe::application *a, webframe::router *r) override; + private: + void listen(const char *address, int port); + void serve(); + void stop(); + + std::unique_ptr _event_base{nullptr, &event_base_free}; + std::unique_ptr _http{nullptr, &evhttp_free}; + }; +} + +#endif \ No newline at end of file diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..1246a40 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,11 @@ +file(GLOB APPTEST_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp) + +webframe_add_application(NAME apptest + SOURCES ${APPTEST_SOURCES} + INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/include + LINK_LIBRARIES Maxtest::Maxtest + RUNTIME mock) + +maxtest_add_test(apptest_mock webframe::noop_test) +maxtest_add_test(apptest_mock webframe::dispatch_error_test) +maxtest_add_test(apptest_mock webframe::empty_router_test) \ No newline at end of file diff --git a/tests/include/test.hpp b/tests/include/test.hpp new file mode 100644 index 0000000..0946006 --- /dev/null +++ b/tests/include/test.hpp @@ -0,0 +1,36 @@ +#ifndef WEBFRAME_TEST_HPP +#define WEBFRAME_TEST_HPP + +#include +#include +#include + +#include + +namespace webframe::test +{ + template + struct test_case + { + static inline void run() + { + std::unique_ptr runtime(webframe::webframe_init()); + ApplicationType app; + webframe::router router; + const int actual_result = runtime->dispatch(0, nullptr, &app, &router); + MAXTEST_ASSERT(actual_result == ExpectedStatusCode); + } + }; + + using noop_test = test_case; + using dispatch_error_test = test_case; + using empty_router_test = test_case; +} + +#define WEBFRAME_TEST_CASE(TestName) \ + MAXTEST_TEST_CASE(webframe::TestName) \ + { \ + webframe::test::TestName::run(); \ + } + +#endif \ No newline at end of file diff --git a/tests/include/test/application.hpp b/tests/include/test/application.hpp new file mode 100644 index 0000000..47d595a --- /dev/null +++ b/tests/include/test/application.hpp @@ -0,0 +1,36 @@ +#ifndef WEBFRAME_TEST_APPLICATION_HPP +#define WEBFRAME_TEST_APPLICATION_HPP + +#include + +namespace webframe::test +{ + class client; + + class application : public webframe::application + { + public: + void configure_router(webframe::router *router) override; + void on_dispatch() override; + + protected: + virtual void setup_router(webframe::router *router); + virtual void run_tests(client *c) = 0; + private: + std::unique_ptr _client; + }; + + class dispatch_error : public application + { + protected: + void run_tests(client *c) override; + }; + + class empty_router : public application + { + protected: + void run_tests(client *c) override; + }; +} + +#endif \ No newline at end of file diff --git a/tests/include/test/client.hpp b/tests/include/test/client.hpp new file mode 100644 index 0000000..12368d4 --- /dev/null +++ b/tests/include/test/client.hpp @@ -0,0 +1,19 @@ +#ifndef WEBFRAME_TEST_CLIENT_HPP +#define WEBFRAME_TEST_CLIENT_HPP + +#include + +namespace webframe::test +{ + class client + { + public: + client(webframe::router *router); + ~client() = default; + void run(const webframe::request *request, webframe::response *response); + private: + webframe::router *_router; + }; +} + +#endif \ No newline at end of file diff --git a/tests/include/test/message.hpp b/tests/include/test/message.hpp new file mode 100644 index 0000000..50621e4 --- /dev/null +++ b/tests/include/test/message.hpp @@ -0,0 +1,47 @@ +#ifndef WEBFRAME_TEST_MESSAGE_HPP +#define WEBFRAME_TEST_MESSAGE_HPP + +#include + +#include +#include + +namespace webframe::test +{ + struct message_data + { + std::map headers; + std::vector body; + }; + + struct request_data : public message_data + { + webframe::method method; + std::string path; + }; + + struct response_data : public message_data + { + int status_code; + }; + + class request : public webframe::request, public request_data + { + public: + webframe::method get_method() const override; + std::string get_path() const override; + bool get_header(const std::string &key, std::string &value) const override; + std::pair get_body() const override; + void read_body(const std::function &callback) const override; + }; + + class response : public webframe::response, public response_data + { + public: + void set_status(int status) override; + void set_header(const std::string &key, const std::string &value) override; + void set_body(const uint8_t *data, size_t size) override; + void write_body(const std::function &)> &callback) override; + }; +} +#endif \ No newline at end of file diff --git a/tests/src/application.cpp b/tests/src/application.cpp new file mode 100644 index 0000000..dd042e5 --- /dev/null +++ b/tests/src/application.cpp @@ -0,0 +1,36 @@ +#include + +namespace webframe::test +{ + void application::configure_router(webframe::router *router) + { + setup_router(router); + _client = std::make_unique(router); + } + + void application::setup_router(webframe::router *router) + { + (void)router; + } + + void application::on_dispatch() + { + run_tests(_client.get()); + } + + void dispatch_error::run_tests(client *c) + { + (void)c; + throw std::runtime_error("dispatch error"); + } + + void empty_router::run_tests(client *c) + { + request req; + req.method = webframe::method::http_get; + req.path = "/somepath"; + response res; + c->run(&req, &res); + MAXTEST_ASSERT(res.status_code == 404); + } +} \ No newline at end of file diff --git a/tests/src/client.cpp b/tests/src/client.cpp new file mode 100644 index 0000000..803996f --- /dev/null +++ b/tests/src/client.cpp @@ -0,0 +1,14 @@ +#include + +namespace webframe::test +{ + client::client(webframe::router *router) : _router(router) + { + } + + void client::run(const webframe::request *request, webframe::response *response) + { + webframe::handler *handler = _router->find_route(request->get_path()); + handler->handle_request(request, response); + } +} \ No newline at end of file diff --git a/tests/src/main.cpp b/tests/src/main.cpp new file mode 100644 index 0000000..f99833c --- /dev/null +++ b/tests/src/main.cpp @@ -0,0 +1,8 @@ +#include + +MAXTEST_MAIN +{ + WEBFRAME_TEST_CASE(noop_test); + WEBFRAME_TEST_CASE(dispatch_error_test); + WEBFRAME_TEST_CASE(empty_router_test); +}; \ No newline at end of file diff --git a/tests/src/message.cpp b/tests/src/message.cpp new file mode 100644 index 0000000..8bbcf6d --- /dev/null +++ b/tests/src/message.cpp @@ -0,0 +1,65 @@ +#include + +namespace webframe::test +{ + webframe::method request::get_method() const + { + return method; + } + + std::string request::get_path() const + { + return path; + } + + bool request::get_header(const std::string &key, std::string &value) const + { + auto it = headers.find(key); + if (it == headers.end()) + { + return false; + } + value = it->second; + return true; + } + + std::pair request::get_body() const + { + return { body.data(), body.size() }; + } + + void request::read_body(const std::function &callback) const + { + callback(body.data(), body.size()); + } + + void response::set_status(int status) + { + status_code = status; + } + + void response::set_header(const std::string &key, const std::string &value) + { + headers[key] = value; + } + + void response::set_body(const uint8_t *data, size_t size) + { + body.assign(data, data + size); + } + + void response::write_body(const std::function &)> &callback) + { + bool has_more = true; + while (has_more) + { + std::pair chunk; + has_more = callback(chunk); + if(chunk.first && chunk.second > 0) + { + body.insert(body.end(), chunk.first, chunk.first + chunk.second); + } + } + + } +} \ No newline at end of file diff --git a/vcpkg-configuration.json b/vcpkg-configuration.json new file mode 100644 index 0000000..7b1ad85 --- /dev/null +++ b/vcpkg-configuration.json @@ -0,0 +1,3 @@ +{ + "overlay-ports": ["ports"] +} diff --git a/vcpkg.json b/vcpkg.json index d5c4105..cb88440 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -2,10 +2,46 @@ "name": "webframe", "version": "0.1.0", "description": "Portable C++ runtime for web applications", - "homepage": "https://github.com/maxtek6/webframe", + "homepage": "https://github.com/maxtek6/WebFrame", "license": "LGPL-3.0-or-later", - "dependencies": [ - "qtwebview", - "libevent" - ] + "features": { + "testing": { + "description": "Testing dependencies for WebFrame", + "dependencies": [ + "maxtest" + ] + }, + "runtime": { + "description": "The runtime component of WebFrame", + "dependencies": [ + { + "name": "cef", + "platform": "!windows" + }, + { + "name": "wxwidgets", + "features": [ + "webview", + "webview-chromium" + ], + "platform": "!windows" + }, + { + "name": "wxwidgets", + "features": [ + "webview" + ], + "platform": "windows" + }, + "libevent" + ] + }, + "example": { + "description": "Example application for WebFrame", + "dependencies": [ + "yyjson", + "hyperpage" + ] + } + } } \ No newline at end of file