diff --git a/.github/workflows/gcc.yml b/.github/workflows/gcc.yml index ac4cce317e..99bc2d591d 100644 --- a/.github/workflows/gcc.yml +++ b/.github/workflows/gcc.yml @@ -20,7 +20,7 @@ jobs: strategy: fail-fast: false matrix: - gcc: [12, 13, 14] + gcc: [13, 14] build_type: [Debug] std: [23] diff --git a/CMakeLists.txt b/CMakeLists.txt index f9808ed863..58f75b6d1d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,9 @@ project( include(cmake/project-is-top-level.cmake) include(cmake/variables.cmake) +include(cmake/stdexec.cmake) +fetch_stdexec() add_library(glaze_glaze INTERFACE) add_library(glaze::glaze ALIAS glaze_glaze) @@ -19,7 +21,7 @@ if (MSVC) string(REGEX MATCH "\/cl(.exe)?$" matched_cl ${CMAKE_CXX_COMPILER}) if (matched_cl) # for a C++ standards compliant preprocessor, not needed for clang-cl - target_compile_options(glaze_glaze INTERFACE "/Zc:preprocessor" /permissive- /Zc:lambda) + target_compile_options(glaze_glaze INTERFACE "/Zc:preprocessor" /permissive- /Zc:lambda /Zc:__cplusplus) if(PROJECT_IS_TOP_LEVEL) target_compile_options(glaze_glaze INTERFACE @@ -40,6 +42,7 @@ target_compile_features(glaze_glaze INTERFACE cxx_std_23) target_include_directories( glaze_glaze ${warning_guard} INTERFACE "$" + "$" ) if(NOT CMAKE_SKIP_INSTALL_RULES) diff --git a/README.md b/README.md index 74e03be0e2..70aeed4d4f 100644 --- a/README.md +++ b/README.md @@ -174,11 +174,11 @@ auto ec = glz::write_file_json(obj, "./obj.json", std::string{}); - Only tested on 64bit systems, but should run on 32bit systems - Only supports little-endian systems -[Actions](https://github.com/stephenberry/glaze/actions) build and test with [Clang](https://clang.llvm.org) (15+), [MSVC](https://visualstudio.microsoft.com/vs/features/cplusplus/) (2022), and [GCC](https://gcc.gnu.org) (12+) on apple, windows, and linux. +[Actions](https://github.com/stephenberry/glaze/actions) build and test with [Clang](https://clang.llvm.org) (15+), [MSVC](https://visualstudio.microsoft.com/vs/features/cplusplus/) (2022), and [GCC](https://gcc.gnu.org) (13+) on apple, windows, and linux. ![clang build](https://github.com/stephenberry/glaze/actions/workflows/clang.yml/badge.svg) ![gcc build](https://github.com/stephenberry/glaze/actions/workflows/gcc.yml/badge.svg) ![msvc build](https://github.com/stephenberry/glaze/actions/workflows/msvc.yml/badge.svg) -> Glaze seeks to maintain compatibility with the latest three versions of GCC and Clang, as well as the latest version of MSVC and Apple Clang. +> Glaze seeks to maintain compatibility with the latest three versions of GCC and Clang, as well as the latest version of MSVC and Apple Clang. As an exception, GCC 12 is not supported due to lack of `std::format`. ## How To Use Glaze diff --git a/cmake/CPM.cmake b/cmake/CPM.cmake new file mode 100644 index 0000000000..b273c3bba4 --- /dev/null +++ b/cmake/CPM.cmake @@ -0,0 +1,1225 @@ +# CPM.cmake - CMake's missing package manager +# =========================================== +# See https://github.com/cpm-cmake/CPM.cmake for usage and update instructions. +# +# MIT License +# ----------- +#[[ + Copyright (c) 2019-2023 Lars Melchior and contributors + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +]] + +cmake_minimum_required(VERSION 3.14 FATAL_ERROR) + +# Initialize logging prefix +if(NOT CPM_INDENT) + set(CPM_INDENT + "CPM:" + CACHE INTERNAL "" + ) +endif() + +if(NOT COMMAND cpm_message) + function(cpm_message) + message(${ARGV}) + endfunction() +endif() + +set(CURRENT_CPM_VERSION 1.0.0-development-version) + +get_filename_component(CPM_CURRENT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}" REALPATH) +if(CPM_DIRECTORY) + if(NOT CPM_DIRECTORY STREQUAL CPM_CURRENT_DIRECTORY) + if(CPM_VERSION VERSION_LESS CURRENT_CPM_VERSION) + message( + AUTHOR_WARNING + "${CPM_INDENT} \ +A dependency is using a more recent CPM version (${CURRENT_CPM_VERSION}) than the current project (${CPM_VERSION}). \ +It is recommended to upgrade CPM to the most recent version. \ +See https://github.com/cpm-cmake/CPM.cmake for more information." + ) + endif() + if(${CMAKE_VERSION} VERSION_LESS "3.17.0") + include(FetchContent) + endif() + return() + endif() + + get_property( + CPM_INITIALIZED GLOBAL "" + PROPERTY CPM_INITIALIZED + SET + ) + if(CPM_INITIALIZED) + return() + endif() +endif() + +if(CURRENT_CPM_VERSION MATCHES "development-version") + message( + WARNING "${CPM_INDENT} Your project is using an unstable development version of CPM.cmake. \ +Please update to a recent release if possible. \ +See https://github.com/cpm-cmake/CPM.cmake for details." + ) +endif() + +set_property(GLOBAL PROPERTY CPM_INITIALIZED true) + +macro(cpm_set_policies) + # the policy allows us to change options without caching + cmake_policy(SET CMP0077 NEW) + set(CMAKE_POLICY_DEFAULT_CMP0077 NEW) + + # the policy allows us to change set(CACHE) without caching + if(POLICY CMP0126) + cmake_policy(SET CMP0126 NEW) + set(CMAKE_POLICY_DEFAULT_CMP0126 NEW) + endif() + + # The policy uses the download time for timestamp, instead of the timestamp in the archive. This + # allows for proper rebuilds when a projects url changes + if(POLICY CMP0135) + cmake_policy(SET CMP0135 NEW) + set(CMAKE_POLICY_DEFAULT_CMP0135 NEW) + endif() + + # treat relative git repository paths as being relative to the parent project's remote + if(POLICY CMP0150) + cmake_policy(SET CMP0150 NEW) + set(CMAKE_POLICY_DEFAULT_CMP0150 NEW) + endif() +endmacro() +cpm_set_policies() + +option(CPM_USE_LOCAL_PACKAGES "Always try to use `find_package` to get dependencies" + $ENV{CPM_USE_LOCAL_PACKAGES} +) +option(CPM_LOCAL_PACKAGES_ONLY "Only use `find_package` to get dependencies" + $ENV{CPM_LOCAL_PACKAGES_ONLY} +) +option(CPM_DOWNLOAD_ALL "Always download dependencies from source" $ENV{CPM_DOWNLOAD_ALL}) +option(CPM_DONT_UPDATE_MODULE_PATH "Don't update the module path to allow using find_package" + $ENV{CPM_DONT_UPDATE_MODULE_PATH} +) +option(CPM_DONT_CREATE_PACKAGE_LOCK "Don't create a package lock file in the binary path" + $ENV{CPM_DONT_CREATE_PACKAGE_LOCK} +) +option(CPM_INCLUDE_ALL_IN_PACKAGE_LOCK + "Add all packages added through CPM.cmake to the package lock" + $ENV{CPM_INCLUDE_ALL_IN_PACKAGE_LOCK} +) +option(CPM_USE_NAMED_CACHE_DIRECTORIES + "Use additional directory of package name in cache on the most nested level." + $ENV{CPM_USE_NAMED_CACHE_DIRECTORIES} +) + +set(CPM_VERSION + ${CURRENT_CPM_VERSION} + CACHE INTERNAL "" +) +set(CPM_DIRECTORY + ${CPM_CURRENT_DIRECTORY} + CACHE INTERNAL "" +) +set(CPM_FILE + ${CMAKE_CURRENT_LIST_FILE} + CACHE INTERNAL "" +) +set(CPM_PACKAGES + "" + CACHE INTERNAL "" +) +set(CPM_DRY_RUN + OFF + CACHE INTERNAL "Don't download or configure dependencies (for testing)" +) + +if(DEFINED ENV{CPM_SOURCE_CACHE}) + set(CPM_SOURCE_CACHE_DEFAULT $ENV{CPM_SOURCE_CACHE}) +else() + set(CPM_SOURCE_CACHE_DEFAULT OFF) +endif() + +set(CPM_SOURCE_CACHE + ${CPM_SOURCE_CACHE_DEFAULT} + CACHE PATH "Directory to download CPM dependencies" +) + +if(NOT CPM_DONT_UPDATE_MODULE_PATH) + set(CPM_MODULE_PATH + "${CMAKE_BINARY_DIR}/CPM_modules" + CACHE INTERNAL "" + ) + # remove old modules + file(REMOVE_RECURSE ${CPM_MODULE_PATH}) + file(MAKE_DIRECTORY ${CPM_MODULE_PATH}) + # locally added CPM modules should override global packages + set(CMAKE_MODULE_PATH "${CPM_MODULE_PATH};${CMAKE_MODULE_PATH}") +endif() + +if(NOT CPM_DONT_CREATE_PACKAGE_LOCK) + set(CPM_PACKAGE_LOCK_FILE + "${CMAKE_BINARY_DIR}/cpm-package-lock.cmake" + CACHE INTERNAL "" + ) + file(WRITE ${CPM_PACKAGE_LOCK_FILE} + "# CPM Package Lock\n# This file should be committed to version control\n\n" + ) +endif() + +include(FetchContent) + +# Try to infer package name from git repository uri (path or url) +function(cpm_package_name_from_git_uri URI RESULT) + if("${URI}" MATCHES "([^/:]+)/?.git/?$") + set(${RESULT} + ${CMAKE_MATCH_1} + PARENT_SCOPE + ) + else() + unset(${RESULT} PARENT_SCOPE) + endif() +endfunction() + +# Try to infer package name and version from a url +function(cpm_package_name_and_ver_from_url url outName outVer) + if(url MATCHES "[/\\?]([a-zA-Z0-9_\\.-]+)\\.(tar|tar\\.gz|tar\\.bz2|zip|ZIP)(\\?|/|$)") + # We matched an archive + set(filename "${CMAKE_MATCH_1}") + + if(filename MATCHES "([a-zA-Z0-9_\\.-]+)[_-]v?(([0-9]+\\.)*[0-9]+[a-zA-Z0-9]*)") + # We matched - (ie foo-1.2.3) + set(${outName} + "${CMAKE_MATCH_1}" + PARENT_SCOPE + ) + set(${outVer} + "${CMAKE_MATCH_2}" + PARENT_SCOPE + ) + elseif(filename MATCHES "(([0-9]+\\.)+[0-9]+[a-zA-Z0-9]*)") + # We couldn't find a name, but we found a version + # + # In many cases (which we don't handle here) the url would look something like + # `irrelevant/ACTUAL_PACKAGE_NAME/irrelevant/1.2.3.zip`. In such a case we can't possibly + # distinguish the package name from the irrelevant bits. Moreover if we try to match the + # package name from the filename, we'd get bogus at best. + unset(${outName} PARENT_SCOPE) + set(${outVer} + "${CMAKE_MATCH_1}" + PARENT_SCOPE + ) + else() + # Boldly assume that the file name is the package name. + # + # Yes, something like `irrelevant/ACTUAL_NAME/irrelevant/download.zip` will ruin our day, but + # such cases should be quite rare. No popular service does this... we think. + set(${outName} + "${filename}" + PARENT_SCOPE + ) + unset(${outVer} PARENT_SCOPE) + endif() + else() + # No ideas yet what to do with non-archives + unset(${outName} PARENT_SCOPE) + unset(${outVer} PARENT_SCOPE) + endif() +endfunction() + +function(cpm_find_package NAME VERSION) + string(REPLACE " " ";" EXTRA_ARGS "${ARGN}") + find_package(${NAME} ${VERSION} ${EXTRA_ARGS} QUIET) + if(${CPM_ARGS_NAME}_FOUND) + if(DEFINED ${CPM_ARGS_NAME}_VERSION) + set(VERSION ${${CPM_ARGS_NAME}_VERSION}) + endif() + cpm_message(STATUS "${CPM_INDENT} Using local package ${CPM_ARGS_NAME}@${VERSION}") + CPMRegisterPackage(${CPM_ARGS_NAME} "${VERSION}") + set(CPM_PACKAGE_FOUND + YES + PARENT_SCOPE + ) + else() + set(CPM_PACKAGE_FOUND + NO + PARENT_SCOPE + ) + endif() +endfunction() + +# Create a custom FindXXX.cmake module for a CPM package This prevents `find_package(NAME)` from +# finding the system library +function(cpm_create_module_file Name) + if(NOT CPM_DONT_UPDATE_MODULE_PATH) + # erase any previous modules + file(WRITE ${CPM_MODULE_PATH}/Find${Name}.cmake + "include(\"${CPM_FILE}\")\n${ARGN}\nset(${Name}_FOUND TRUE)" + ) + endif() +endfunction() + +# Find a package locally or fallback to CPMAddPackage +function(CPMFindPackage) + set(oneValueArgs NAME VERSION GIT_TAG FIND_PACKAGE_ARGUMENTS) + + cmake_parse_arguments(CPM_ARGS "" "${oneValueArgs}" "" ${ARGN}) + + if(NOT DEFINED CPM_ARGS_VERSION) + if(DEFINED CPM_ARGS_GIT_TAG) + cpm_get_version_from_git_tag("${CPM_ARGS_GIT_TAG}" CPM_ARGS_VERSION) + endif() + endif() + + set(downloadPackage ${CPM_DOWNLOAD_ALL}) + if(DEFINED CPM_DOWNLOAD_${CPM_ARGS_NAME}) + set(downloadPackage ${CPM_DOWNLOAD_${CPM_ARGS_NAME}}) + elseif(DEFINED ENV{CPM_DOWNLOAD_${CPM_ARGS_NAME}}) + set(downloadPackage $ENV{CPM_DOWNLOAD_${CPM_ARGS_NAME}}) + endif() + if(downloadPackage) + CPMAddPackage(${ARGN}) + cpm_export_variables(${CPM_ARGS_NAME}) + return() + endif() + + cpm_find_package(${CPM_ARGS_NAME} "${CPM_ARGS_VERSION}" ${CPM_ARGS_FIND_PACKAGE_ARGUMENTS}) + + if(NOT CPM_PACKAGE_FOUND) + CPMAddPackage(${ARGN}) + cpm_export_variables(${CPM_ARGS_NAME}) + endif() + +endfunction() + +# checks if a package has been added before +function(cpm_check_if_package_already_added CPM_ARGS_NAME CPM_ARGS_VERSION) + if("${CPM_ARGS_NAME}" IN_LIST CPM_PACKAGES) + CPMGetPackageVersion(${CPM_ARGS_NAME} CPM_PACKAGE_VERSION) + if("${CPM_PACKAGE_VERSION}" VERSION_LESS "${CPM_ARGS_VERSION}") + message( + WARNING + "${CPM_INDENT} Requires a newer version of ${CPM_ARGS_NAME} (${CPM_ARGS_VERSION}) than currently included (${CPM_PACKAGE_VERSION})." + ) + endif() + cpm_get_fetch_properties(${CPM_ARGS_NAME}) + set(${CPM_ARGS_NAME}_ADDED NO) + set(CPM_PACKAGE_ALREADY_ADDED + YES + PARENT_SCOPE + ) + cpm_export_variables(${CPM_ARGS_NAME}) + else() + set(CPM_PACKAGE_ALREADY_ADDED + NO + PARENT_SCOPE + ) + endif() +endfunction() + +# Parse the argument of CPMAddPackage in case a single one was provided and convert it to a list of +# arguments which can then be parsed idiomatically. For example gh:foo/bar@1.2.3 will be converted +# to: GITHUB_REPOSITORY;foo/bar;VERSION;1.2.3 +function(cpm_parse_add_package_single_arg arg outArgs) + # Look for a scheme + if("${arg}" MATCHES "^([a-zA-Z]+):(.+)$") + string(TOLOWER "${CMAKE_MATCH_1}" scheme) + set(uri "${CMAKE_MATCH_2}") + + # Check for CPM-specific schemes + if(scheme STREQUAL "gh") + set(out "GITHUB_REPOSITORY;${uri}") + set(packageType "git") + elseif(scheme STREQUAL "gl") + set(out "GITLAB_REPOSITORY;${uri}") + set(packageType "git") + elseif(scheme STREQUAL "bb") + set(out "BITBUCKET_REPOSITORY;${uri}") + set(packageType "git") + # A CPM-specific scheme was not found. Looks like this is a generic URL so try to determine + # type + elseif(arg MATCHES ".git/?(@|#|$)") + set(out "GIT_REPOSITORY;${arg}") + set(packageType "git") + else() + # Fall back to a URL + set(out "URL;${arg}") + set(packageType "archive") + + # We could also check for SVN since FetchContent supports it, but SVN is so rare these days. + # We just won't bother with the additional complexity it will induce in this function. SVN is + # done by multi-arg + endif() + else() + if(arg MATCHES ".git/?(@|#|$)") + set(out "GIT_REPOSITORY;${arg}") + set(packageType "git") + else() + # Give up + message(FATAL_ERROR "${CPM_INDENT} Can't determine package type of '${arg}'") + endif() + endif() + + # For all packages we interpret @... as version. Only replace the last occurrence. Thus URIs + # containing '@' can be used + string(REGEX REPLACE "@([^@]+)$" ";VERSION;\\1" out "${out}") + + # Parse the rest according to package type + if(packageType STREQUAL "git") + # For git repos we interpret #... as a tag or branch or commit hash + string(REGEX REPLACE "#([^#]+)$" ";GIT_TAG;\\1" out "${out}") + elseif(packageType STREQUAL "archive") + # For archives we interpret #... as a URL hash. + string(REGEX REPLACE "#([^#]+)$" ";URL_HASH;\\1" out "${out}") + # We don't try to parse the version if it's not provided explicitly. cpm_get_version_from_url + # should do this at a later point + else() + # We should never get here. This is an assertion and hitting it means there's a problem with the + # code above. A packageType was set, but not handled by this if-else. + message(FATAL_ERROR "${CPM_INDENT} Unsupported package type '${packageType}' of '${arg}'") + endif() + + set(${outArgs} + ${out} + PARENT_SCOPE + ) +endfunction() + +# Check that the working directory for a git repo is clean +function(cpm_check_git_working_dir_is_clean repoPath gitTag isClean) + + find_package(Git REQUIRED) + + if(NOT GIT_EXECUTABLE) + # No git executable, assume directory is clean + set(${isClean} + TRUE + PARENT_SCOPE + ) + return() + endif() + + # check for uncommitted changes + execute_process( + COMMAND ${GIT_EXECUTABLE} status --porcelain + RESULT_VARIABLE resultGitStatus + OUTPUT_VARIABLE repoStatus + OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET + WORKING_DIRECTORY ${repoPath} + ) + if(resultGitStatus) + # not supposed to happen, assume clean anyway + message(WARNING "${CPM_INDENT} Calling git status on folder ${repoPath} failed") + set(${isClean} + TRUE + PARENT_SCOPE + ) + return() + endif() + + if(NOT "${repoStatus}" STREQUAL "") + set(${isClean} + FALSE + PARENT_SCOPE + ) + return() + endif() + + # check for committed changes + execute_process( + COMMAND ${GIT_EXECUTABLE} diff -s --exit-code ${gitTag} + RESULT_VARIABLE resultGitDiff + OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_QUIET + WORKING_DIRECTORY ${repoPath} + ) + + if(${resultGitDiff} EQUAL 0) + set(${isClean} + TRUE + PARENT_SCOPE + ) + else() + set(${isClean} + FALSE + PARENT_SCOPE + ) + endif() + +endfunction() + +# Add PATCH_COMMAND to CPM_ARGS_UNPARSED_ARGUMENTS. This method consumes a list of files in ARGN +# then generates a `PATCH_COMMAND` appropriate for `ExternalProject_Add()`. This command is appended +# to the parent scope's `CPM_ARGS_UNPARSED_ARGUMENTS`. +function(cpm_add_patches) + # Return if no patch files are supplied. + if(NOT ARGN) + return() + endif() + + # Find the patch program. + find_program(PATCH_EXECUTABLE patch) + if(WIN32 AND NOT PATCH_EXECUTABLE) + # The Windows git executable is distributed with patch.exe. Find the path to the executable, if + # it exists, then search `../../usr/bin` for patch.exe. + find_package(Git QUIET) + if(GIT_EXECUTABLE) + get_filename_component(extra_search_path ${GIT_EXECUTABLE} DIRECTORY) + get_filename_component(extra_search_path ${extra_search_path} DIRECTORY) + get_filename_component(extra_search_path ${extra_search_path} DIRECTORY) + find_program(PATCH_EXECUTABLE patch HINTS "${extra_search_path}/usr/bin") + endif() + endif() + if(NOT PATCH_EXECUTABLE) + message(FATAL_ERROR "Couldn't find `patch` executable to use with PATCHES keyword.") + endif() + + # Create a temporary + set(temp_list ${CPM_ARGS_UNPARSED_ARGUMENTS}) + + # Ensure each file exists (or error out) and add it to the list. + set(first_item True) + foreach(PATCH_FILE ${ARGN}) + # Make sure the patch file exists, if we can't find it, try again in the current directory. + if(NOT EXISTS "${PATCH_FILE}") + if(NOT EXISTS "${CMAKE_CURRENT_LIST_DIR}/${PATCH_FILE}") + message(FATAL_ERROR "Couldn't find patch file: '${PATCH_FILE}'") + endif() + set(PATCH_FILE "${CMAKE_CURRENT_LIST_DIR}/${PATCH_FILE}") + endif() + + # Convert to absolute path for use with patch file command. + get_filename_component(PATCH_FILE "${PATCH_FILE}" ABSOLUTE) + + # The first patch entry must be preceded by "PATCH_COMMAND" while the following items are + # preceded by "&&". + if(first_item) + set(first_item False) + list(APPEND temp_list "PATCH_COMMAND") + else() + list(APPEND temp_list "&&") + endif() + # Add the patch command to the list + list(APPEND temp_list "${PATCH_EXECUTABLE}" "-p1" "<" "${PATCH_FILE}") + endforeach() + + # Move temp out into parent scope. + set(CPM_ARGS_UNPARSED_ARGUMENTS + ${temp_list} + PARENT_SCOPE + ) + +endfunction() + +# method to overwrite internal FetchContent properties, to allow using CPM.cmake to overload +# FetchContent calls. As these are internal cmake properties, this method should be used carefully +# and may need modification in future CMake versions. Source: +# https://github.com/Kitware/CMake/blob/dc3d0b5a0a7d26d43d6cfeb511e224533b5d188f/Modules/FetchContent.cmake#L1152 +function(cpm_override_fetchcontent contentName) + cmake_parse_arguments(PARSE_ARGV 1 arg "" "SOURCE_DIR;BINARY_DIR" "") + if(NOT "${arg_UNPARSED_ARGUMENTS}" STREQUAL "") + message(FATAL_ERROR "${CPM_INDENT} Unsupported arguments: ${arg_UNPARSED_ARGUMENTS}") + endif() + + string(TOLOWER ${contentName} contentNameLower) + set(prefix "_FetchContent_${contentNameLower}") + + set(propertyName "${prefix}_sourceDir") + define_property( + GLOBAL + PROPERTY ${propertyName} + BRIEF_DOCS "Internal implementation detail of FetchContent_Populate()" + FULL_DOCS "Details used by FetchContent_Populate() for ${contentName}" + ) + set_property(GLOBAL PROPERTY ${propertyName} "${arg_SOURCE_DIR}") + + set(propertyName "${prefix}_binaryDir") + define_property( + GLOBAL + PROPERTY ${propertyName} + BRIEF_DOCS "Internal implementation detail of FetchContent_Populate()" + FULL_DOCS "Details used by FetchContent_Populate() for ${contentName}" + ) + set_property(GLOBAL PROPERTY ${propertyName} "${arg_BINARY_DIR}") + + set(propertyName "${prefix}_populated") + define_property( + GLOBAL + PROPERTY ${propertyName} + BRIEF_DOCS "Internal implementation detail of FetchContent_Populate()" + FULL_DOCS "Details used by FetchContent_Populate() for ${contentName}" + ) + set_property(GLOBAL PROPERTY ${propertyName} TRUE) +endfunction() + +# Download and add a package from source +function(CPMAddPackage) + cpm_set_policies() + + list(LENGTH ARGN argnLength) + if(argnLength EQUAL 1) + cpm_parse_add_package_single_arg("${ARGN}" ARGN) + + # The shorthand syntax implies EXCLUDE_FROM_ALL and SYSTEM + set(ARGN "${ARGN};EXCLUDE_FROM_ALL;YES;SYSTEM;YES;") + endif() + + set(oneValueArgs + NAME + FORCE + VERSION + GIT_TAG + DOWNLOAD_ONLY + GITHUB_REPOSITORY + GITLAB_REPOSITORY + BITBUCKET_REPOSITORY + GIT_REPOSITORY + SOURCE_DIR + FIND_PACKAGE_ARGUMENTS + NO_CACHE + SYSTEM + GIT_SHALLOW + EXCLUDE_FROM_ALL + SOURCE_SUBDIR + CUSTOM_CACHE_KEY + ) + + set(multiValueArgs URL OPTIONS DOWNLOAD_COMMAND PATCHES) + + cmake_parse_arguments(CPM_ARGS "" "${oneValueArgs}" "${multiValueArgs}" "${ARGN}") + + # Set default values for arguments + + if(NOT DEFINED CPM_ARGS_VERSION) + if(DEFINED CPM_ARGS_GIT_TAG) + cpm_get_version_from_git_tag("${CPM_ARGS_GIT_TAG}" CPM_ARGS_VERSION) + endif() + endif() + + if(CPM_ARGS_DOWNLOAD_ONLY) + set(DOWNLOAD_ONLY ${CPM_ARGS_DOWNLOAD_ONLY}) + else() + set(DOWNLOAD_ONLY NO) + endif() + + if(DEFINED CPM_ARGS_GITHUB_REPOSITORY) + set(CPM_ARGS_GIT_REPOSITORY "https://github.com/${CPM_ARGS_GITHUB_REPOSITORY}.git") + elseif(DEFINED CPM_ARGS_GITLAB_REPOSITORY) + set(CPM_ARGS_GIT_REPOSITORY "https://gitlab.com/${CPM_ARGS_GITLAB_REPOSITORY}.git") + elseif(DEFINED CPM_ARGS_BITBUCKET_REPOSITORY) + set(CPM_ARGS_GIT_REPOSITORY "https://bitbucket.org/${CPM_ARGS_BITBUCKET_REPOSITORY}.git") + endif() + + if(DEFINED CPM_ARGS_GIT_REPOSITORY) + list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_REPOSITORY ${CPM_ARGS_GIT_REPOSITORY}) + if(NOT DEFINED CPM_ARGS_GIT_TAG) + set(CPM_ARGS_GIT_TAG v${CPM_ARGS_VERSION}) + endif() + + # If a name wasn't provided, try to infer it from the git repo + if(NOT DEFINED CPM_ARGS_NAME) + cpm_package_name_from_git_uri(${CPM_ARGS_GIT_REPOSITORY} CPM_ARGS_NAME) + endif() + endif() + + set(CPM_SKIP_FETCH FALSE) + + if(DEFINED CPM_ARGS_GIT_TAG) + list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_TAG ${CPM_ARGS_GIT_TAG}) + # If GIT_SHALLOW is explicitly specified, honor the value. + if(DEFINED CPM_ARGS_GIT_SHALLOW) + list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_SHALLOW ${CPM_ARGS_GIT_SHALLOW}) + endif() + endif() + + if(DEFINED CPM_ARGS_URL) + # If a name or version aren't provided, try to infer them from the URL + list(GET CPM_ARGS_URL 0 firstUrl) + cpm_package_name_and_ver_from_url(${firstUrl} nameFromUrl verFromUrl) + # If we fail to obtain name and version from the first URL, we could try other URLs if any. + # However multiple URLs are expected to be quite rare, so for now we won't bother. + + # If the caller provided their own name and version, they trump the inferred ones. + if(NOT DEFINED CPM_ARGS_NAME) + set(CPM_ARGS_NAME ${nameFromUrl}) + endif() + if(NOT DEFINED CPM_ARGS_VERSION) + set(CPM_ARGS_VERSION ${verFromUrl}) + endif() + + list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS URL "${CPM_ARGS_URL}") + endif() + + # Check for required arguments + + if(NOT DEFINED CPM_ARGS_NAME) + message( + FATAL_ERROR + "${CPM_INDENT} 'NAME' was not provided and couldn't be automatically inferred for package added with arguments: '${ARGN}'" + ) + endif() + + # Check if package has been added before + cpm_check_if_package_already_added(${CPM_ARGS_NAME} "${CPM_ARGS_VERSION}") + if(CPM_PACKAGE_ALREADY_ADDED) + cpm_export_variables(${CPM_ARGS_NAME}) + return() + endif() + + # Check for manual overrides + if(NOT CPM_ARGS_FORCE AND NOT "${CPM_${CPM_ARGS_NAME}_SOURCE}" STREQUAL "") + set(PACKAGE_SOURCE ${CPM_${CPM_ARGS_NAME}_SOURCE}) + set(CPM_${CPM_ARGS_NAME}_SOURCE "") + CPMAddPackage( + NAME "${CPM_ARGS_NAME}" + SOURCE_DIR "${PACKAGE_SOURCE}" + EXCLUDE_FROM_ALL "${CPM_ARGS_EXCLUDE_FROM_ALL}" + SYSTEM "${CPM_ARGS_SYSTEM}" + PATCHES "${CPM_ARGS_PATCHES}" + OPTIONS "${CPM_ARGS_OPTIONS}" + SOURCE_SUBDIR "${CPM_ARGS_SOURCE_SUBDIR}" + DOWNLOAD_ONLY "${DOWNLOAD_ONLY}" + FORCE True + ) + cpm_export_variables(${CPM_ARGS_NAME}) + return() + endif() + + # Check for available declaration + if(NOT CPM_ARGS_FORCE AND NOT "${CPM_DECLARATION_${CPM_ARGS_NAME}}" STREQUAL "") + set(declaration ${CPM_DECLARATION_${CPM_ARGS_NAME}}) + set(CPM_DECLARATION_${CPM_ARGS_NAME} "") + CPMAddPackage(${declaration}) + cpm_export_variables(${CPM_ARGS_NAME}) + # checking again to ensure version and option compatibility + cpm_check_if_package_already_added(${CPM_ARGS_NAME} "${CPM_ARGS_VERSION}") + return() + endif() + + if(NOT CPM_ARGS_FORCE) + if(CPM_USE_LOCAL_PACKAGES OR CPM_LOCAL_PACKAGES_ONLY) + cpm_find_package(${CPM_ARGS_NAME} "${CPM_ARGS_VERSION}" ${CPM_ARGS_FIND_PACKAGE_ARGUMENTS}) + + if(CPM_PACKAGE_FOUND) + cpm_export_variables(${CPM_ARGS_NAME}) + return() + endif() + + if(CPM_LOCAL_PACKAGES_ONLY) + message( + SEND_ERROR + "${CPM_INDENT} ${CPM_ARGS_NAME} not found via find_package(${CPM_ARGS_NAME} ${CPM_ARGS_VERSION})" + ) + endif() + endif() + endif() + + CPMRegisterPackage("${CPM_ARGS_NAME}" "${CPM_ARGS_VERSION}") + + if(DEFINED CPM_ARGS_GIT_TAG) + set(PACKAGE_INFO "${CPM_ARGS_GIT_TAG}") + elseif(DEFINED CPM_ARGS_SOURCE_DIR) + set(PACKAGE_INFO "${CPM_ARGS_SOURCE_DIR}") + else() + set(PACKAGE_INFO "${CPM_ARGS_VERSION}") + endif() + + if(DEFINED FETCHCONTENT_BASE_DIR) + # respect user's FETCHCONTENT_BASE_DIR if set + set(CPM_FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR}) + else() + set(CPM_FETCHCONTENT_BASE_DIR ${CMAKE_BINARY_DIR}/_deps) + endif() + + cpm_add_patches(${CPM_ARGS_PATCHES}) + + if(DEFINED CPM_ARGS_DOWNLOAD_COMMAND) + list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS DOWNLOAD_COMMAND ${CPM_ARGS_DOWNLOAD_COMMAND}) + elseif(DEFINED CPM_ARGS_SOURCE_DIR) + list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS SOURCE_DIR ${CPM_ARGS_SOURCE_DIR}) + if(NOT IS_ABSOLUTE ${CPM_ARGS_SOURCE_DIR}) + # Expand `CPM_ARGS_SOURCE_DIR` relative path. This is important because EXISTS doesn't work + # for relative paths. + get_filename_component( + source_directory ${CPM_ARGS_SOURCE_DIR} REALPATH BASE_DIR ${CMAKE_CURRENT_BINARY_DIR} + ) + else() + set(source_directory ${CPM_ARGS_SOURCE_DIR}) + endif() + if(NOT EXISTS ${source_directory}) + string(TOLOWER ${CPM_ARGS_NAME} lower_case_name) + # remove timestamps so CMake will re-download the dependency + file(REMOVE_RECURSE "${CPM_FETCHCONTENT_BASE_DIR}/${lower_case_name}-subbuild") + endif() + elseif(CPM_SOURCE_CACHE AND NOT CPM_ARGS_NO_CACHE) + string(TOLOWER ${CPM_ARGS_NAME} lower_case_name) + set(origin_parameters ${CPM_ARGS_UNPARSED_ARGUMENTS}) + list(SORT origin_parameters) + if(CPM_ARGS_CUSTOM_CACHE_KEY) + # Application set a custom unique directory name + set(download_directory ${CPM_SOURCE_CACHE}/${lower_case_name}/${CPM_ARGS_CUSTOM_CACHE_KEY}) + elseif(CPM_USE_NAMED_CACHE_DIRECTORIES) + string(SHA1 origin_hash "${origin_parameters};NEW_CACHE_STRUCTURE_TAG") + set(download_directory ${CPM_SOURCE_CACHE}/${lower_case_name}/${origin_hash}/${CPM_ARGS_NAME}) + else() + string(SHA1 origin_hash "${origin_parameters}") + set(download_directory ${CPM_SOURCE_CACHE}/${lower_case_name}/${origin_hash}) + endif() + # Expand `download_directory` relative path. This is important because EXISTS doesn't work for + # relative paths. + get_filename_component(download_directory ${download_directory} ABSOLUTE) + list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS SOURCE_DIR ${download_directory}) + + if(CPM_SOURCE_CACHE) + file(LOCK ${download_directory}/../cmake.lock) + endif() + + if(EXISTS ${download_directory}) + if(CPM_SOURCE_CACHE) + file(LOCK ${download_directory}/../cmake.lock RELEASE) + endif() + + cpm_store_fetch_properties( + ${CPM_ARGS_NAME} "${download_directory}" + "${CPM_FETCHCONTENT_BASE_DIR}/${lower_case_name}-build" + ) + cpm_get_fetch_properties("${CPM_ARGS_NAME}") + + if(DEFINED CPM_ARGS_GIT_TAG AND NOT (PATCH_COMMAND IN_LIST CPM_ARGS_UNPARSED_ARGUMENTS)) + # warn if cache has been changed since checkout + cpm_check_git_working_dir_is_clean(${download_directory} ${CPM_ARGS_GIT_TAG} IS_CLEAN) + if(NOT ${IS_CLEAN}) + message( + WARNING "${CPM_INDENT} Cache for ${CPM_ARGS_NAME} (${download_directory}) is dirty" + ) + endif() + endif() + + cpm_add_subdirectory( + "${CPM_ARGS_NAME}" + "${DOWNLOAD_ONLY}" + "${${CPM_ARGS_NAME}_SOURCE_DIR}/${CPM_ARGS_SOURCE_SUBDIR}" + "${${CPM_ARGS_NAME}_BINARY_DIR}" + "${CPM_ARGS_EXCLUDE_FROM_ALL}" + "${CPM_ARGS_SYSTEM}" + "${CPM_ARGS_OPTIONS}" + ) + set(PACKAGE_INFO "${PACKAGE_INFO} at ${download_directory}") + + # As the source dir is already cached/populated, we override the call to FetchContent. + set(CPM_SKIP_FETCH TRUE) + cpm_override_fetchcontent( + "${lower_case_name}" SOURCE_DIR "${${CPM_ARGS_NAME}_SOURCE_DIR}/${CPM_ARGS_SOURCE_SUBDIR}" + BINARY_DIR "${${CPM_ARGS_NAME}_BINARY_DIR}" + ) + + else() + # Enable shallow clone when GIT_TAG is not a commit hash. Our guess may not be accurate, but + # it should guarantee no commit hash get mis-detected. + if(NOT DEFINED CPM_ARGS_GIT_SHALLOW) + cpm_is_git_tag_commit_hash("${CPM_ARGS_GIT_TAG}" IS_HASH) + if(NOT ${IS_HASH}) + list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_SHALLOW TRUE) + endif() + endif() + + # remove timestamps so CMake will re-download the dependency + file(REMOVE_RECURSE ${CPM_FETCHCONTENT_BASE_DIR}/${lower_case_name}-subbuild) + set(PACKAGE_INFO "${PACKAGE_INFO} to ${download_directory}") + endif() + endif() + + cpm_create_module_file(${CPM_ARGS_NAME} "CPMAddPackage(\"${ARGN}\")") + + if(CPM_PACKAGE_LOCK_ENABLED) + if((CPM_ARGS_VERSION AND NOT CPM_ARGS_SOURCE_DIR) OR CPM_INCLUDE_ALL_IN_PACKAGE_LOCK) + cpm_add_to_package_lock(${CPM_ARGS_NAME} "${ARGN}") + elseif(CPM_ARGS_SOURCE_DIR) + cpm_add_comment_to_package_lock(${CPM_ARGS_NAME} "local directory") + else() + cpm_add_comment_to_package_lock(${CPM_ARGS_NAME} "${ARGN}") + endif() + endif() + + cpm_message( + STATUS "${CPM_INDENT} Adding package ${CPM_ARGS_NAME}@${CPM_ARGS_VERSION} (${PACKAGE_INFO})" + ) + + if(NOT CPM_SKIP_FETCH) + cpm_declare_fetch( + "${CPM_ARGS_NAME}" "${CPM_ARGS_VERSION}" "${PACKAGE_INFO}" "${CPM_ARGS_UNPARSED_ARGUMENTS}" + ) + cpm_fetch_package("${CPM_ARGS_NAME}" populated) + if(CPM_SOURCE_CACHE AND download_directory) + file(LOCK ${download_directory}/../cmake.lock RELEASE) + endif() + if(${populated}) + cpm_add_subdirectory( + "${CPM_ARGS_NAME}" + "${DOWNLOAD_ONLY}" + "${${CPM_ARGS_NAME}_SOURCE_DIR}/${CPM_ARGS_SOURCE_SUBDIR}" + "${${CPM_ARGS_NAME}_BINARY_DIR}" + "${CPM_ARGS_EXCLUDE_FROM_ALL}" + "${CPM_ARGS_SYSTEM}" + "${CPM_ARGS_OPTIONS}" + ) + endif() + cpm_get_fetch_properties("${CPM_ARGS_NAME}") + endif() + + set(${CPM_ARGS_NAME}_ADDED YES) + cpm_export_variables("${CPM_ARGS_NAME}") +endfunction() + +# Fetch a previously declared package +macro(CPMGetPackage Name) + if(DEFINED "CPM_DECLARATION_${Name}") + CPMAddPackage(NAME ${Name}) + else() + message(SEND_ERROR "${CPM_INDENT} Cannot retrieve package ${Name}: no declaration available") + endif() +endmacro() + +# export variables available to the caller to the parent scope expects ${CPM_ARGS_NAME} to be set +macro(cpm_export_variables name) + set(${name}_SOURCE_DIR + "${${name}_SOURCE_DIR}" + PARENT_SCOPE + ) + set(${name}_BINARY_DIR + "${${name}_BINARY_DIR}" + PARENT_SCOPE + ) + set(${name}_ADDED + "${${name}_ADDED}" + PARENT_SCOPE + ) + set(CPM_LAST_PACKAGE_NAME + "${name}" + PARENT_SCOPE + ) +endmacro() + +# declares a package, so that any call to CPMAddPackage for the package name will use these +# arguments instead. Previous declarations will not be overridden. +macro(CPMDeclarePackage Name) + if(NOT DEFINED "CPM_DECLARATION_${Name}") + set("CPM_DECLARATION_${Name}" "${ARGN}") + endif() +endmacro() + +function(cpm_add_to_package_lock Name) + if(NOT CPM_DONT_CREATE_PACKAGE_LOCK) + cpm_prettify_package_arguments(PRETTY_ARGN false ${ARGN}) + file(APPEND ${CPM_PACKAGE_LOCK_FILE} "# ${Name}\nCPMDeclarePackage(${Name}\n${PRETTY_ARGN})\n") + endif() +endfunction() + +function(cpm_add_comment_to_package_lock Name) + if(NOT CPM_DONT_CREATE_PACKAGE_LOCK) + cpm_prettify_package_arguments(PRETTY_ARGN true ${ARGN}) + file(APPEND ${CPM_PACKAGE_LOCK_FILE} + "# ${Name} (unversioned)\n# CPMDeclarePackage(${Name}\n${PRETTY_ARGN}#)\n" + ) + endif() +endfunction() + +# includes the package lock file if it exists and creates a target `cpm-update-package-lock` to +# update it +macro(CPMUsePackageLock file) + if(NOT CPM_DONT_CREATE_PACKAGE_LOCK) + get_filename_component(CPM_ABSOLUTE_PACKAGE_LOCK_PATH ${file} ABSOLUTE) + if(EXISTS ${CPM_ABSOLUTE_PACKAGE_LOCK_PATH}) + include(${CPM_ABSOLUTE_PACKAGE_LOCK_PATH}) + endif() + if(NOT TARGET cpm-update-package-lock) + add_custom_target( + cpm-update-package-lock COMMAND ${CMAKE_COMMAND} -E copy ${CPM_PACKAGE_LOCK_FILE} + ${CPM_ABSOLUTE_PACKAGE_LOCK_PATH} + ) + endif() + set(CPM_PACKAGE_LOCK_ENABLED true) + endif() +endmacro() + +# registers a package that has been added to CPM +function(CPMRegisterPackage PACKAGE VERSION) + list(APPEND CPM_PACKAGES ${PACKAGE}) + set(CPM_PACKAGES + ${CPM_PACKAGES} + CACHE INTERNAL "" + ) + set("CPM_PACKAGE_${PACKAGE}_VERSION" + ${VERSION} + CACHE INTERNAL "" + ) +endfunction() + +# retrieve the current version of the package to ${OUTPUT} +function(CPMGetPackageVersion PACKAGE OUTPUT) + set(${OUTPUT} + "${CPM_PACKAGE_${PACKAGE}_VERSION}" + PARENT_SCOPE + ) +endfunction() + +# declares a package in FetchContent_Declare +function(cpm_declare_fetch PACKAGE VERSION INFO) + if(${CPM_DRY_RUN}) + cpm_message(STATUS "${CPM_INDENT} Package not declared (dry run)") + return() + endif() + + FetchContent_Declare(${PACKAGE} ${ARGN}) +endfunction() + +# returns properties for a package previously defined by cpm_declare_fetch +function(cpm_get_fetch_properties PACKAGE) + if(${CPM_DRY_RUN}) + return() + endif() + + set(${PACKAGE}_SOURCE_DIR + "${CPM_PACKAGE_${PACKAGE}_SOURCE_DIR}" + PARENT_SCOPE + ) + set(${PACKAGE}_BINARY_DIR + "${CPM_PACKAGE_${PACKAGE}_BINARY_DIR}" + PARENT_SCOPE + ) +endfunction() + +function(cpm_store_fetch_properties PACKAGE source_dir binary_dir) + if(${CPM_DRY_RUN}) + return() + endif() + + set(CPM_PACKAGE_${PACKAGE}_SOURCE_DIR + "${source_dir}" + CACHE INTERNAL "" + ) + set(CPM_PACKAGE_${PACKAGE}_BINARY_DIR + "${binary_dir}" + CACHE INTERNAL "" + ) +endfunction() + +# adds a package as a subdirectory if viable, according to provided options +function( + cpm_add_subdirectory + PACKAGE + DOWNLOAD_ONLY + SOURCE_DIR + BINARY_DIR + EXCLUDE + SYSTEM + OPTIONS +) + + if(NOT DOWNLOAD_ONLY AND EXISTS ${SOURCE_DIR}/CMakeLists.txt) + set(addSubdirectoryExtraArgs "") + if(EXCLUDE) + list(APPEND addSubdirectoryExtraArgs EXCLUDE_FROM_ALL) + endif() + if("${SYSTEM}" AND "${CMAKE_VERSION}" VERSION_GREATER_EQUAL "3.25") + # https://cmake.org/cmake/help/latest/prop_dir/SYSTEM.html#prop_dir:SYSTEM + list(APPEND addSubdirectoryExtraArgs SYSTEM) + endif() + if(OPTIONS) + foreach(OPTION ${OPTIONS}) + cpm_parse_option("${OPTION}") + set(${OPTION_KEY} "${OPTION_VALUE}") + endforeach() + endif() + set(CPM_OLD_INDENT "${CPM_INDENT}") + set(CPM_INDENT "${CPM_INDENT} ${PACKAGE}:") + add_subdirectory(${SOURCE_DIR} ${BINARY_DIR} ${addSubdirectoryExtraArgs}) + set(CPM_INDENT "${CPM_OLD_INDENT}") + endif() +endfunction() + +# downloads a previously declared package via FetchContent and exports the variables +# `${PACKAGE}_SOURCE_DIR` and `${PACKAGE}_BINARY_DIR` to the parent scope +function(cpm_fetch_package PACKAGE populated) + set(${populated} + FALSE + PARENT_SCOPE + ) + if(${CPM_DRY_RUN}) + cpm_message(STATUS "${CPM_INDENT} Package ${PACKAGE} not fetched (dry run)") + return() + endif() + + FetchContent_GetProperties(${PACKAGE}) + + string(TOLOWER "${PACKAGE}" lower_case_name) + + if(NOT ${lower_case_name}_POPULATED) + FetchContent_Populate(${PACKAGE}) + set(${populated} + TRUE + PARENT_SCOPE + ) + endif() + + cpm_store_fetch_properties( + ${CPM_ARGS_NAME} ${${lower_case_name}_SOURCE_DIR} ${${lower_case_name}_BINARY_DIR} + ) + + set(${PACKAGE}_SOURCE_DIR + ${${lower_case_name}_SOURCE_DIR} + PARENT_SCOPE + ) + set(${PACKAGE}_BINARY_DIR + ${${lower_case_name}_BINARY_DIR} + PARENT_SCOPE + ) +endfunction() + +# splits a package option +function(cpm_parse_option OPTION) + string(REGEX MATCH "^[^ ]+" OPTION_KEY "${OPTION}") + string(LENGTH "${OPTION}" OPTION_LENGTH) + string(LENGTH "${OPTION_KEY}" OPTION_KEY_LENGTH) + if(OPTION_KEY_LENGTH STREQUAL OPTION_LENGTH) + # no value for key provided, assume user wants to set option to "ON" + set(OPTION_VALUE "ON") + else() + math(EXPR OPTION_KEY_LENGTH "${OPTION_KEY_LENGTH}+1") + string(SUBSTRING "${OPTION}" "${OPTION_KEY_LENGTH}" "-1" OPTION_VALUE) + endif() + set(OPTION_KEY + "${OPTION_KEY}" + PARENT_SCOPE + ) + set(OPTION_VALUE + "${OPTION_VALUE}" + PARENT_SCOPE + ) +endfunction() + +# guesses the package version from a git tag +function(cpm_get_version_from_git_tag GIT_TAG RESULT) + string(LENGTH ${GIT_TAG} length) + if(length EQUAL 40) + # GIT_TAG is probably a git hash + set(${RESULT} + 0 + PARENT_SCOPE + ) + else() + string(REGEX MATCH "v?([0123456789.]*).*" _ ${GIT_TAG}) + set(${RESULT} + ${CMAKE_MATCH_1} + PARENT_SCOPE + ) + endif() +endfunction() + +# guesses if the git tag is a commit hash or an actual tag or a branch name. +function(cpm_is_git_tag_commit_hash GIT_TAG RESULT) + string(LENGTH "${GIT_TAG}" length) + # full hash has 40 characters, and short hash has at least 7 characters. + if(length LESS 7 OR length GREATER 40) + set(${RESULT} + 0 + PARENT_SCOPE + ) + else() + if(${GIT_TAG} MATCHES "^[a-fA-F0-9]+$") + set(${RESULT} + 1 + PARENT_SCOPE + ) + else() + set(${RESULT} + 0 + PARENT_SCOPE + ) + endif() + endif() +endfunction() + +function(cpm_prettify_package_arguments OUT_VAR IS_IN_COMMENT) + set(oneValueArgs + NAME + FORCE + VERSION + GIT_TAG + DOWNLOAD_ONLY + GITHUB_REPOSITORY + GITLAB_REPOSITORY + BITBUCKET_REPOSITORY + GIT_REPOSITORY + SOURCE_DIR + FIND_PACKAGE_ARGUMENTS + NO_CACHE + SYSTEM + GIT_SHALLOW + EXCLUDE_FROM_ALL + SOURCE_SUBDIR + ) + set(multiValueArgs URL OPTIONS DOWNLOAD_COMMAND) + cmake_parse_arguments(CPM_ARGS "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + + foreach(oneArgName ${oneValueArgs}) + if(DEFINED CPM_ARGS_${oneArgName}) + if(${IS_IN_COMMENT}) + string(APPEND PRETTY_OUT_VAR "#") + endif() + if(${oneArgName} STREQUAL "SOURCE_DIR") + string(REPLACE ${CMAKE_SOURCE_DIR} "\${CMAKE_SOURCE_DIR}" CPM_ARGS_${oneArgName} + ${CPM_ARGS_${oneArgName}} + ) + endif() + string(APPEND PRETTY_OUT_VAR " ${oneArgName} ${CPM_ARGS_${oneArgName}}\n") + endif() + endforeach() + foreach(multiArgName ${multiValueArgs}) + if(DEFINED CPM_ARGS_${multiArgName}) + if(${IS_IN_COMMENT}) + string(APPEND PRETTY_OUT_VAR "#") + endif() + string(APPEND PRETTY_OUT_VAR " ${multiArgName}\n") + foreach(singleOption ${CPM_ARGS_${multiArgName}}) + if(${IS_IN_COMMENT}) + string(APPEND PRETTY_OUT_VAR "#") + endif() + string(APPEND PRETTY_OUT_VAR " \"${singleOption}\"\n") + endforeach() + endif() + endforeach() + + if(NOT "${CPM_ARGS_UNPARSED_ARGUMENTS}" STREQUAL "") + if(${IS_IN_COMMENT}) + string(APPEND PRETTY_OUT_VAR "#") + endif() + string(APPEND PRETTY_OUT_VAR " ") + foreach(CPM_ARGS_UNPARSED_ARGUMENT ${CPM_ARGS_UNPARSED_ARGUMENTS}) + string(APPEND PRETTY_OUT_VAR " ${CPM_ARGS_UNPARSED_ARGUMENT}") + endforeach() + string(APPEND PRETTY_OUT_VAR "\n") + endif() + + set(${OUT_VAR} + ${PRETTY_OUT_VAR} + PARENT_SCOPE + ) + +endfunction() diff --git a/cmake/stdexec.cmake b/cmake/stdexec.cmake new file mode 100644 index 0000000000..6e7c8fd5aa --- /dev/null +++ b/cmake/stdexec.cmake @@ -0,0 +1,38 @@ +function (fetch_stdexec) + set(branch_or_tag "main") + set(url "https://github.com/NVIDIA/stdexec.git") + set(target_folder "${CMAKE_BINARY_DIR}/_deps/stdexec-src") + + if (NOT EXISTS ${target_folder}) + execute_process( + COMMAND git clone --depth 1 --branch "${branch_or_tag}" --recursive "${url}" "${target_folder}" + WORKING_DIRECTORY ${CMAKE_BINARY_DIR} + RESULT_VARIABLE exec_process_result + OUTPUT_VARIABLE exec_process_output + ) + if(NOT exec_process_result EQUAL "0") + message(FATAL_ERROR "Git clone failed: ${exec_process_output}") + else() + message(STATUS "Git clone succeeded: ${exec_process_output}") + endif() + endif() + + set(stdexec_SOURCE_DIR ${target_folder} CACHE INTERNAL "stdexec source folder" FORCE) + set(stdexec_INCLUDE_DIR ${target_folder}/include CACHE INTERNAL "stdexec include folder" FORCE) + + #[[ + include(FetchContent) + + FetchContent_Declare( + stdexec + GIT_REPOSITORY https://github.com/NVIDIA/stdexec.git + GIT_TAG main + GIT_SHALLOW TRUE + ) + + set(STDEXEC_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) + + FetchContent_MakeAvailable(stdexec) + #]] + +endfunction() \ No newline at end of file diff --git a/include/glaze/core/error.hpp b/include/glaze/core/error.hpp new file mode 100644 index 0000000000..dae1540f59 --- /dev/null +++ b/include/glaze/core/error.hpp @@ -0,0 +1,27 @@ +// Glaze Library +// For the license information refer to glaze.hpp + +#pragma once + +#include "glaze/core/context.hpp" +#include "glaze/core/common.hpp" + +#include + +namespace glz +{ + struct error_category : public std::error_category + { + static const error_category& instance() { + static error_category instance{}; + return instance; + } + + const char* name() const noexcept override { return "glz::error_category"; } + + std::string message(int ec) const override + { + return std::string{nameof(error_code(ec))}; + } + }; +} diff --git a/include/glaze/ext/glaze_asio.hpp b/include/glaze/ext/glaze_asio.hpp deleted file mode 100644 index 887e3ea32f..0000000000 --- a/include/glaze/ext/glaze_asio.hpp +++ /dev/null @@ -1,368 +0,0 @@ -// Glaze Library -// For the license information refer to glaze.hpp - -#pragma once - -#if __has_include() && !defined(GLZ_USE_BOOST_ASIO) -#include -#elif __has_include() -#ifndef GLZ_USING_BOOST_ASIO -#define GLZ_USING_BOOST_ASIO -#endif -#include -#else -static_assert(false, "standalone asio must be included to use glaze/ext/glaze_asio.hpp"); -#endif - -#include -#include -#include - -#include "glaze/rpc/repe.hpp" - -namespace glz -{ -#if defined(GLZ_USING_BOOST_ASIO) - namespace asio = boost::asio; -#endif - inline void send_buffer(asio::ip::tcp::socket& socket, const std::string_view str) - { - const uint64_t size = str.size(); - std::array buffers{asio::buffer(&size, sizeof(uint64_t)), asio::buffer(str)}; - - asio::write(socket, buffers, asio::transfer_exactly(sizeof(uint64_t) + size)); - } - - inline void receive_buffer(asio::ip::tcp::socket& socket, std::string& str) - { - uint64_t size; - asio::read(socket, asio::buffer(&size, sizeof(size)), asio::transfer_exactly(sizeof(uint64_t))); - str.resize(size); - asio::read(socket, asio::buffer(str), asio::transfer_exactly(size)); - } - - inline asio::awaitable co_send_buffer(asio::ip::tcp::socket& socket, const std::string_view str) - { - const uint64_t size = str.size(); - std::array buffers{asio::buffer(&size, sizeof(uint64_t)), asio::buffer(str)}; - - co_await asio::async_write(socket, buffers, asio::transfer_exactly(sizeof(uint64_t) + size), asio::use_awaitable); - } - - inline asio::awaitable co_receive_buffer(asio::ip::tcp::socket& socket, std::string& str) - { - uint64_t size; - co_await asio::async_read(socket, asio::buffer(&size, sizeof(size)), asio::transfer_exactly(sizeof(uint64_t)), - asio::use_awaitable); - str.resize(size); - co_await asio::async_read(socket, asio::buffer(str), asio::transfer_exactly(size), asio::use_awaitable); - } - - inline asio::awaitable call_rpc(asio::ip::tcp::socket& socket, std::string& buffer) - { - co_await co_send_buffer(socket, buffer); - co_await co_receive_buffer(socket, buffer); - } - - template - struct func_traits; - - template - struct func_traits - { - using result_type = Result; - using params_type = void; - using std_func_sig = std::function; - }; - - template - struct func_traits - { - using result_type = Result; - using params_type = Params; - using std_func_sig = std::function; - }; - - template - using func_result_t = typename func_traits::result_type; - - template - using func_params_t = typename func_traits::params_type; - - template - using std_func_sig_t = typename func_traits::std_func_sig; - - template - struct asio_client - { - std::string host{"localhost"}; // host name - std::string service{""}; // often the port - uint32_t concurrency{1}; // how many threads to use - - struct glaze - { - using T = asio_client; - static constexpr auto value = glz::object(&T::host, &T::service, &T::concurrency); - }; - - std::shared_ptr ctx{}; - std::shared_ptr socket{}; - - std::shared_ptr buffer_pool = std::make_shared(); - - [[nodiscard]] std::error_code init() - { - ctx = std::make_shared(concurrency); - socket = std::make_shared(*ctx); - asio::ip::tcp::resolver resolver{*ctx}; - const auto endpoint = resolver.resolve(host, service); -#if !defined(GLZ_USING_BOOST_ASIO) - std::error_code ec{}; -#else - boost::system::error_code ec{}; -#endif - asio::connect(*socket, endpoint, ec); - if (ec) { - return ec; - } - return socket->set_option(asio::ip::tcp::no_delay(true), ec); - } - - template - [[nodiscard]] repe::error_t notify(repe::header&& header, Params&& params) - { - repe::unique_buffer ubuffer{buffer_pool.get()}; - auto& buffer = ubuffer.value(); - - header.notify = true; - const auto ec = repe::request(std::move(header), std::forward(params), buffer); - if (bool(ec)) [[unlikely]] { - return {repe::error_e::invalid_params, glz::format_error(ec, buffer)}; - } - - send_buffer(*socket, buffer); - return {}; - } - - template - [[nodiscard]] repe::error_t get(repe::header&& header, Result&& result) - { - repe::unique_buffer ubuffer{buffer_pool.get()}; - auto& buffer = ubuffer.value(); - - header.notify = false; - header.empty = true; // no params - const auto ec = repe::request(std::move(header), nullptr, buffer); - if (bool(ec)) [[unlikely]] { - return {repe::error_e::invalid_params, glz::format_error(ec, buffer)}; - } - - send_buffer(*socket, buffer); - receive_buffer(*socket, buffer); - - return repe::decode_response(std::forward(result), buffer); - } - - template - [[nodiscard]] glz::expected get(repe::header&& header) - { - std::decay_t result{}; - const auto error = get(std::move(header), result); - if (error) { - return glz::unexpected(error); - } - else { - return {result}; - } - } - - template - [[nodiscard]] repe::error_t set(repe::header&& header, Params&& params) - { - repe::unique_buffer ubuffer{buffer_pool.get()}; - auto& buffer = ubuffer.value(); - - header.notify = false; - const auto ec = repe::request(std::move(header), std::forward(params), buffer); - if (bool(ec)) [[unlikely]] { - return {repe::error_e::invalid_params, glz::format_error(ec, buffer)}; - } - - send_buffer(*socket, buffer); - receive_buffer(*socket, buffer); - - return repe::decode_response(buffer); - } - - template - [[nodiscard]] repe::error_t call(repe::header&& header, Params&& params, Result&& result) - { - repe::unique_buffer ubuffer{buffer_pool.get()}; - auto& buffer = ubuffer.value(); - - header.notify = false; - const auto ec = repe::request(std::move(header), std::forward(params), buffer); - if (bool(ec)) [[unlikely]] { - return {repe::error_e::invalid_params, glz::format_error(ec, buffer)}; - } - - send_buffer(*socket, buffer); - receive_buffer(*socket, buffer); - - return repe::decode_response(std::forward(result), buffer); - } - - [[nodiscard]] repe::error_t call(repe::header&& header) - { - repe::unique_buffer ubuffer{buffer_pool.get()}; - auto& buffer = ubuffer.value(); - - header.notify = false; - header.empty = true; // because no value provided - const auto ec = glz::write_json(std::forward_as_tuple(std::move(header), nullptr), buffer); - if (bool(ec)) [[unlikely]] { - return {repe::error_e::invalid_params, glz::format_error(ec, buffer)}; - } - - send_buffer(*socket, buffer); - receive_buffer(*socket, buffer); - - return repe::decode_response(buffer); - } - - template - [[nodiscard]] std_func_sig_t callable(repe::header&& header) - { - using Params = func_params_t; - using Result = func_result_t; - if constexpr (std::same_as) { - header.empty = true; - return [this, h = std::move(header)]() mutable -> Result { - std::decay_t result{}; - const auto e = call(repe::header{h}, result); - if (e) { - throw std::runtime_error(glz::write_json(e)); - } - return result; - }; - } - else { - header.empty = false; - return [this, h = std::move(header)](Params params) mutable -> Result { - std::decay_t result{}; - const auto e = call(repe::header{h}, params, result); - if (e) { - throw std::runtime_error(e.message); - } - return result; - }; - } - } - - template - [[deprecated("We use a buffer pool now, so this would cause allocations")]] [[nodiscard]] std::string call_raw( - repe::header&& header, Params&& params, repe::error_t& error) - { - std::string buffer{}; - - header.notify = false; - const auto ec = repe::request(std::move(header), std::forward(params), buffer); - if (bool(ec)) [[unlikely]] { - error = {repe::error_e::invalid_params, glz::format_error(ec, buffer)}; - return buffer; - } - - send_buffer(*socket, buffer); - receive_buffer(*socket, buffer); - return buffer; - } - }; - - template - struct asio_server - { - uint16_t port{}; - uint32_t concurrency{1}; // how many threads to use - - struct glaze - { - using T = asio_server; - static constexpr auto value = glz::object(&T::port, &T::concurrency); - }; - - std::shared_ptr ctx{}; - std::shared_ptr signals{}; - - repe::registry registry{}; - - void clear_registry() { registry.clear(); } - - template - requires(glz::detail::glaze_object_t || glz::detail::reflectable) - void on(T& value) - { - registry.template on(value); - } - - bool initialized = false; - - void init() - { - if (!initialized) { - ctx = std::make_shared(concurrency); - signals = std::make_shared(*ctx, SIGINT, SIGTERM); - } - initialized = true; - } - - void run() - { - if (!initialized) { - init(); - } - - signals->async_wait([&](auto, auto) { ctx->stop(); }); - - asio::co_spawn(*ctx, listener(), asio::detached); - - ctx->run(); - } - - // stop the server - void stop() - { - if (ctx) { - ctx->stop(); - } - } - - asio::awaitable run_instance(asio::ip::tcp::socket socket) - { - socket.set_option(asio::ip::tcp::no_delay(true)); - std::string buffer{}; - - try { - while (true) { - co_await co_receive_buffer(socket, buffer); - auto response = registry.call(buffer); - if (response) { - co_await co_send_buffer(socket, response->value()); - } - } - } - catch (const std::exception& e) { - std::fprintf(stderr, "%s\n", e.what()); - } - } - - asio::awaitable listener() - { - auto executor = co_await asio::this_coro::executor; - asio::ip::tcp::acceptor acceptor(executor, {asio::ip::tcp::v6(), port}); - while (true) { - auto socket = co_await acceptor.async_accept(asio::use_awaitable); - asio::co_spawn(executor, run_instance(std::move(socket)), asio::detached); - } - } - }; -} diff --git a/include/glaze/network/repe_client.hpp b/include/glaze/network/repe_client.hpp new file mode 100644 index 0000000000..9c1cf42875 --- /dev/null +++ b/include/glaze/network/repe_client.hpp @@ -0,0 +1,160 @@ +// Glaze Library +// For the license information refer to glaze.hpp + +#pragma once + +#include "glaze/network/socket.hpp" +#include "glaze/network/socket_io.hpp" +#include "glaze/rpc/repe.hpp" + +namespace glz +{ + template + struct repe_client + { + std::string hostname{"127.0.0.1"}; + uint16_t port{}; + glz::socket socket{}; + + struct glaze + { + using T = repe_client; + static constexpr auto value = glz::object(&T::hostname, &T::port); + }; + + std::shared_ptr buffer_pool = std::make_shared(); + + [[nodiscard]] std::error_code init() + { + if (auto ec = socket.connect(hostname, port)) { + return ec; + } + + if (not socket.no_delay()) { + return {ip_error::socket_bind_failed, ip_error_category::instance()}; + } + + return {}; + } + + template + [[nodiscard]] repe::error_t notify(repe::header&& header, Params&& params) + { + repe::unique_buffer ubuffer{buffer_pool.get()}; + auto& buffer = ubuffer.value(); + + header.notify = true; + const auto ec = repe::request(std::move(header), std::forward(params), buffer); + if (bool(ec)) [[unlikely]] { + return {repe::error_e::invalid_params, glz::format_error(ec, buffer)}; + } + + if (auto ec = send(socket, buffer)) { + return {ec.value(), ec.message()}; + } + return {}; + } + + template + [[nodiscard]] repe::error_t get(repe::header&& header, Result&& result) + { + repe::unique_buffer ubuffer{buffer_pool.get()}; + auto& buffer = ubuffer.value(); + + header.notify = false; + header.empty = true; // no params + const auto ec = repe::request(std::move(header), nullptr, buffer); + if (bool(ec)) [[unlikely]] { + return {repe::error_e::invalid_params, glz::format_error(ec, buffer)}; + } + + if (auto ec = send(socket, buffer)) { + return {ec.value(), ec.message()}; + } + if (auto ec = receive(socket, buffer)) { + return {ec.value(), ec.message()}; + } + + return repe::decode_response(std::forward(result), buffer); + } + + template + [[nodiscard]] glz::expected get(repe::header&& header) + { + std::decay_t result{}; + const auto error = get(std::move(header), result); + if (error) { + return glz::unexpected(error); + } + else { + return {result}; + } + } + + template + [[nodiscard]] repe::error_t set(repe::header&& header, Params&& params) + { + repe::unique_buffer ubuffer{buffer_pool.get()}; + auto& buffer = ubuffer.value(); + + header.notify = false; + const auto ec = repe::request(std::move(header), std::forward(params), buffer); + if (bool(ec)) [[unlikely]] { + return {repe::error_e::invalid_params, glz::format_error(ec, buffer)}; + } + + if (auto ec = send(socket, buffer)) { + return {ec.value(), ec.message()}; + } + if (auto ec = receive(socket, buffer)) { + return {ec.value(), ec.message()}; + } + + return repe::decode_response(buffer); + } + + template + [[nodiscard]] repe::error_t call(repe::header&& header, Params&& params, Result&& result) + { + repe::unique_buffer ubuffer{buffer_pool.get()}; + auto& buffer = ubuffer.value(); + + header.notify = false; + const auto ec = repe::request(std::move(header), std::forward(params), buffer); + if (bool(ec)) [[unlikely]] { + return {repe::error_e::invalid_params, glz::format_error(ec, buffer)}; + } + + if (auto ec = send(socket, buffer)) { + return {ec.value(), ec.message()}; + } + if (auto ec = receive(socket, buffer)) { + return {ec.value(), ec.message()}; + } + + return repe::decode_response(std::forward(result), buffer); + } + + [[nodiscard]] repe::error_t call(repe::header&& header) + { + repe::unique_buffer ubuffer{buffer_pool.get()}; + auto& buffer = ubuffer.value(); + + header.notify = false; + header.empty = true; // because no value provided + const auto ec = glz::write_json(std::forward_as_tuple(std::move(header), nullptr), buffer); + if (bool(ec)) [[unlikely]] { + return {repe::error_e::invalid_params, glz::format_error(ec, buffer)}; + } + + if (auto ec = send(socket, buffer)) { + return {ec.value(), ec.message()}; + } + if (auto ec = receive(socket, buffer)) { + return {ec.value(), ec.message()}; + } + + return repe::decode_response(buffer); + } + }; +} diff --git a/include/glaze/network/repe_server.hpp b/include/glaze/network/repe_server.hpp new file mode 100644 index 0000000000..49b7c95554 --- /dev/null +++ b/include/glaze/network/repe_server.hpp @@ -0,0 +1,86 @@ +// Glaze Library +// For the license information refer to glaze.hpp + +#pragma once + +#include "glaze/network/server.hpp" +#include "glaze/network/socket_io.hpp" +#include "glaze/rpc/repe.hpp" + +namespace glz +{ + template + struct repe_server + { + uint16_t port{}; + bool print_errors = false; + glz::server server{}; + + struct glaze + { + using T = repe_server; + static constexpr auto value = glz::object(&T::port); + }; + + repe::registry registry{}; + + void clear_registry() { registry.clear(); } + + template + requires(glz::detail::glaze_object_t || glz::detail::reflectable) + void on(T& value) + { + registry.template on(value); + } + + void run() + { + server.port = port; + + auto ec = server.accept([this](socket&& socket, auto& active) { + if (not socket.no_delay()) { + std::printf("%s", "no_delay failed"); + return; + } + + std::string buffer{}; + + try { + while (active) { + if (auto ec = receive(socket, buffer)) { + if (print_errors) { + std::fprintf(stderr, "%s\n", ec.message().c_str()); + } + if (ec.value() == ip_error::client_disconnected) { + return; + } + } + else { + auto response = registry.call(buffer); + if (response) { + if (auto ec = send(socket, response->value())) { + if (print_errors) { + std::fprintf(stderr, "%s\n", ec.message().c_str()); + } + } + } + } + } + } + catch (const std::exception& e) { + std::fprintf(stderr, "%s\n", e.what()); + } + }); + + if (ec) { + std::fprintf(stderr, "%s\n", ec.message().c_str()); + } + } + + // stop the server + void stop() + { + server.active = false; + } + }; +} diff --git a/include/glaze/network/server.hpp b/include/glaze/network/server.hpp new file mode 100644 index 0000000000..ef1e6c7450 --- /dev/null +++ b/include/glaze/network/server.hpp @@ -0,0 +1,215 @@ +// Glaze Library +// For the license information refer to glaze.hpp + +#pragma once + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "glaze/network/socket.hpp" + +#ifdef _WIN32 +#define GLZ_CLOSE_SOCKET closesocket +#define GLZ_EVENT_CLOSE WSACloseEvent +#define GLZ_EWOULDBLOCK WSAEWOULDBLOCK +#define GLZ_INVALID_EVENT WSA_INVALID_EVENT +#define GLZ_INVALID_SOCKET INVALID_SOCKET +#define GLZ_SOCKET SOCKET +#define GLZ_SOCKET_ERROR SOCKET_ERROR +#define GLZ_SOCKET_ERROR_CODE WSAGetLastError() +#define GLZ_WAIT_FAILED WSA_WAIT_FAILED +#define GLZ_WAIT_RESULT_TYPE DWORD +#else +#include +#include +#if __has_include() +#include +#endif +#include +#include +#include +#include + +#include +#define GLZ_CLOSE_SOCKET ::close +#define GLZ_EVENT_CLOSE ::close +#define GLZ_EWOULDBLOCK EWOULDBLOCK +#define GLZ_INVALID_EVENT (-1) +#define GLZ_INVALID_SOCKET (-1) +#define GLZ_SOCKET int +#define GLZ_SOCKET_ERROR (-1) +#define GLZ_SOCKET_ERROR_CODE errno +#define GLZ_WAIT_FAILED (-1) +#define GLZ_WAIT_RESULT_TYPE int +#endif + +#if defined(__APPLE__) +#include // for kqueue on macOS +#elif defined(__linux__) +#include // for epoll on Linux +#endif + +namespace glz +{ + struct server final + { + int port{}; + std::atomic active = true; + exec::static_thread_pool thread_pool{std::thread::hardware_concurrency() / 2}; + exec::async_scope scope{}; + + ~server() + { + active.store(false); + scope.request_stop(); + thread_pool.request_stop(); + stdexec::sync_wait(scope.on_empty()); + } + + template + auto async_accept(AcceptCallback&& callback) + { + return stdexec::schedule(thread_pool.get_scheduler()) | + stdexec::then([this, callback = std::forward(callback)]() mutable { + return accept(std::move(callback)); + }); + } + + template + std::error_code accept(AcceptCallback&& callback) + { + glz::socket accept_socket{}; + + const auto ec = accept_socket.bind_and_listen(port); + if (ec) { + return {ip_error::socket_bind_failed, ip_error_category::instance()}; + } + +#if defined(__APPLE__) + int event_fd = ::kqueue(); +#elif defined(__linux__) + int event_fd = ::epoll_create1(0); +#elif defined(_WIN32) + HANDLE event_fd = WSACreateEvent(); +#endif + + if (event_fd == GLZ_INVALID_EVENT) { + return {ip_error::queue_create_failed, ip_error_category::instance()}; + } + + bool event_setup_failed = false; +#if defined(__APPLE__) + struct kevent change; + EV_SET(&change, accept_socket.socket_fd, EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, nullptr); + event_setup_failed = ::kevent(event_fd, &change, 1, nullptr, 0, nullptr) == -1; +#elif defined(__linux__) + struct epoll_event ev; + ev.events = EPOLLIN; + ev.data.fd = accept_socket.socket_fd; + event_setup_failed = epoll_ctl(event_fd, EPOLL_CTL_ADD, accept_socket.socket_fd, &ev) == -1; +#elif defined(_WIN32) + event_setup_failed = WSAEventSelect(accept_socket.socket_fd, event_fd, FD_ACCEPT) == GLZ_SOCKET_ERROR; +#endif + + if (event_setup_failed) { + GLZ_EVENT_CLOSE(event_fd); + return {ip_error::event_ctl_failed, ip_error_category::instance()}; + } + +#if defined(__APPLE__) + std::vector events(16); +#elif defined(__linux__) + std::vector epoll_events(16); +#endif + + while (active) { + GLZ_WAIT_RESULT_TYPE n{}; + +#if defined(__APPLE__) + struct timespec timeout + { + 0, 10000000 + }; // 10ms + n = ::kevent(event_fd, nullptr, 0, events.data(), static_cast(events.size()), &timeout); +#elif defined(__linux__) + n = ::epoll_wait(event_fd, epoll_events.data(), static_cast(epoll_events.size()), 10); +#elif defined(_WIN32) + n = WSAWaitForMultipleEvents(1, &event_fd, FALSE, 10, FALSE); +#endif + + if (n == GLZ_WAIT_FAILED) { +#if defined(__APPLE__) || defined(__linux__) + if (errno == EINTR) continue; +#else + if (n == WSA_WAIT_TIMEOUT) continue; +#endif + GLZ_EVENT_CLOSE(event_fd); + return {ip_error::event_wait_failed, ip_error_category::instance()}; + } + + auto spawn_socket = [&] { + sockaddr_in client_addr; + socklen_t client_len = sizeof(client_addr); + auto client_fd = ::accept(accept_socket.socket_fd, (sockaddr*)&client_addr, &client_len); + if (client_fd != GLZ_INVALID_SOCKET) { + scope.spawn( + stdexec::on(thread_pool.get_scheduler(), + stdexec::just() | + stdexec::then([callback, client_fd, this] { + callback(thread_pool.get_scheduler(), socket{client_fd}, active); + }) + ) + ); + } + }; + +#if defined(__APPLE__) || defined(__linux__) + for (int i = 0; i < n; ++i) { +#if defined(__APPLE__) + if (events[i].ident == uintptr_t(accept_socket.socket_fd) && events[i].filter == EVFILT_READ) { +#elif defined(__linux__) + if (epoll_events[i].data.fd == accept_socket.socket_fd && epoll_events[i].events & EPOLLIN) { +#endif + spawn_socket(); + } + } + +#else // Windows + WSANETWORKEVENTS events; + if (WSAEnumNetworkEvents(accept_socket.socket_fd, event_fd, &events) == GLZ_SOCKET_ERROR) { + WSACloseEvent(event_fd); + return {ip_error::event_enum_failed, ip_error_category::instance()}; + } + + if (events.lNetworkEvents & FD_ACCEPT) { + if (events.iErrorCode[FD_ACCEPT_BIT] == 0) { + spawn_socket(); + } + } +#endif + } + + GLZ_EVENT_CLOSE(event_fd); + return {}; + } + }; +} + +#undef GLZ_CLOSE_SOCKET +#undef GLZ_EVENT_CLOSE +#undef GLZ_EWOULDBLOCK +#undef GLZ_INVALID_EVENT +#undef GLZ_INVALID_SOCKET +#undef GLZ_SOCKET +#undef GLZ_SOCKET_ERROR +#undef GLZ_SOCKET_ERROR_CODE +#undef GLZ_WAIT_FAILED +#undef GLZ_WAIT_RESULT_TYPE diff --git a/include/glaze/network/socket.hpp b/include/glaze/network/socket.hpp new file mode 100644 index 0000000000..5626b1d14f --- /dev/null +++ b/include/glaze/network/socket.hpp @@ -0,0 +1,512 @@ +// Glaze Library +// For the license information refer to glaze.hpp + +#pragma once + +#include "glaze/glaze.hpp" + +#ifdef _WIN32 +#define GLZ_CLOSE_SOCKET closesocket +#define GLZ_EWOULDBLOCK WSAEWOULDBLOCK +#define GLZ_INVALID_SOCKET INVALID_SOCKET +#define GLZ_SOCKET SOCKET +#define GLZ_SOCKET_ERROR SOCKET_ERROR +#define GLZ_SOCKET_ERROR_CODE WSAGetLastError() +#include +#include + +#include +#pragma comment(lib, "Ws2_32.lib") +#else +#include +#include +#if __has_include() +#include +#endif +#include +#include +#include +#include + +#include +#define GLZ_CLOSE_SOCKET ::close +#define GLZ_EWOULDBLOCK EWOULDBLOCK +#define GLZ_INVALID_SOCKET (-1) +#define GLZ_SOCKET int +#define GLZ_SOCKET_ERROR (-1) +#define GLZ_SOCKET_ERROR_CODE errno +#endif + +#if defined(__APPLE__) +#include // for kqueue on macOS +#elif defined(__linux__) +#include // for epoll on Linux +#endif + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace glz +{ + namespace ip + { + struct opts + {}; + } + + inline std::string get_ip_port(const sockaddr_in& server_addr) + { + char ip_str[INET_ADDRSTRLEN]{}; + +#ifdef _WIN32 + inet_ntop(AF_INET, &(server_addr.sin_addr), ip_str, INET_ADDRSTRLEN); +#else + inet_ntop(AF_INET, &(server_addr.sin_addr), ip_str, sizeof(ip_str)); +#endif + + return {std::format("{}:{}", ip_str, ntohs(server_addr.sin_port))}; + } + + inline std::string get_socket_error_message(int err) + { +#ifdef _WIN32 + + char* msg = nullptr; + FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, + err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&msg, 0, NULL); + std::string message(msg); + LocalFree(msg); + return {message}; + +#else + return strerror(err); +#endif + } + + struct socket_api_error_category_t final : public std::error_category + { + std::string what{}; + const char* name() const noexcept override { return "socket error"; } + std::string message(int ev) const override + { + if (what.empty()) { + return {get_socket_error_message(ev)}; + } + else { + return {std::format("{}\nDetails: {}", what, get_socket_error_message(ev))}; + } + } + void operator()(int ev, const std::string_view w) + { + what = w; + this->message(ev); + } + }; + + inline const socket_api_error_category_t& socket_api_error_category(const std::string_view what) + { + static socket_api_error_category_t singleton; + singleton.what = what; + return singleton; + } + + inline std::error_code get_socket_error(const std::string_view what = "") + { +#ifdef _WIN32 + int err = WSAGetLastError(); +#else + int err = errno; +#endif + + return {std::error_code(err, socket_api_error_category(what))}; + } + + inline std::error_code check_status(int ec, const std::string_view what = "") + { + if (ec >= 0) { + return {}; + } + + return {get_socket_error(what)}; + } + + // Example: + // + // std::error_code ec = check_status(result, "connect failed"); + // + // if (ec) { + // std::cerr << get_socket_error(std::format("Failed to connect to socket at address: {}.\nIs the server + // running?", ip_port)).message(); + // } + // else { + // std::cout << "Connected successfully!"; + // } + + // For Windows WSASocket Compatability + + inline constexpr uint16_t make_version(uint8_t low_byte, uint8_t high_byte) noexcept + { + return uint16_t(low_byte) | (uint16_t(high_byte) << 8); + } + + inline constexpr uint8_t major_version(uint16_t version) noexcept + { + return uint8_t(version & 0xFF); // Extract the low byte + } + + inline constexpr uint8_t minor_version(uint16_t version) noexcept + { + return uint8_t((version >> 8) & 0xFF); // Shift right by 8 bits and extract the low byte + } + + // Function to get Winsock version string on Windows, return "na" otherwise + inline std::string get_winsock_version_string(uint32_t version = make_version(2, 2)) + { +#if _WIN32 + BYTE major = major_version(uint16_t(version)); + BYTE minor = minor_version(uint16_t(version)); + return std::format("{}.{}", static_cast(major), static_cast(minor)); +#else + (void)version; + return ""; // Default behavior for non-Windows platforms +#endif + } + + // The 'wsa_startup_t' calls the windows WSAStartup function. This must be the first Windows + // Sockets function called by an application or DLL. It allows an application or DLL to + // specify the version of Windows Sockets required and retrieve details of the specific + // Windows Sockets implementation.The application or DLL can only issue further Windows Sockets + // functions after successfully calling WSAStartup. + // + // Important: WSAStartup and its corresponding WSACleanup must be called on the same thread. + // + template + struct windows_socket_startup_t final + { +#ifdef _WIN64 + WSADATA wsa_data{}; + + std::error_code error_code{}; + + std::error_code start(const WORD win_sock_version = make_version(2, 2)) // Request latest Winsock version 2.2 + { + static std::once_flag flag{}; + std::error_code startup_error{}; + std::call_once(flag, [this, win_sock_version, &startup_error]() { + int result = WSAStartup(win_sock_version, &wsa_data); + if (result != 0) { + error_code = get_socket_error( + std::format("Unable to initialize Winsock library version {}.", get_winsock_version_string())); + } + }); + return {error_code}; + } + + windows_socket_startup_t() + { + if constexpr (run_wsa_startup) { + error_code = start(); + } + } + + ~windows_socket_startup_t() { WSACleanup(); } + +#else + std::error_code start() { return std::error_code{}; } +#endif + }; + + enum ip_error { + none = 0, + queue_create_failed, + event_ctl_failed, + event_wait_failed, + event_enum_failed, + socket_connect_failed, + socket_bind_failed, + send_failed, + receive_failed, + client_disconnected + }; + + template + concept ip_header = std::same_as || requires { T::body_size; }; + + struct ip_error_category : public std::error_category + { + static const ip_error_category& instance() + { + static ip_error_category instance{}; + return instance; + } + + const char* name() const noexcept override { return "ip_error_category"; } + + std::string message(int ec) const override + { + using enum ip_error; + switch (static_cast(ec)) { + case none: + return "none"; + case queue_create_failed: + return "queue_create_failed"; + case event_ctl_failed: + return "event_ctl_failed"; + case event_wait_failed: + return "event_wait_failed"; + case event_enum_failed: + return "event_enum_failed"; + case socket_connect_failed: + return "socket_connect_failed"; + case socket_bind_failed: + return "socket_bind_failed"; + case send_failed: + return "send_failed"; + case receive_failed: + return "receive_failed"; + case client_disconnected: + return "client_disconnected"; + default: + return "unknown_error"; + } + } + }; + + struct socket + { + GLZ_SOCKET socket_fd{GLZ_INVALID_SOCKET}; + + void set_non_blocking() + { +#ifdef _WIN32 + u_long mode = 1; + ioctlsocket(socket_fd, FIONBIO, &mode); +#else + int flags = fcntl(socket_fd, F_GETFL, 0); + fcntl(socket_fd, F_SETFL, flags | O_NONBLOCK); +#endif + } + + socket() = default; + + socket(GLZ_SOCKET fd) : socket_fd(fd) { set_non_blocking(); } + + void close() + { + if (socket_fd != GLZ_INVALID_SOCKET) { + GLZ_CLOSE_SOCKET(socket_fd); + } + } + + ~socket() { close(); } + + [[nodiscard]] std::error_code connect(const std::string& address, const int port) + { + socket_fd = ::socket(AF_INET, SOCK_STREAM, 0); + if (socket_fd == -1) { + return {ip_error::socket_connect_failed, ip_error_category::instance()}; + } + + sockaddr_in server_addr; + server_addr.sin_family = AF_INET; + server_addr.sin_port = htons(uint16_t(port)); + inet_pton(AF_INET, address.c_str(), &server_addr.sin_addr); + + if (::connect(socket_fd, (sockaddr*)&server_addr, sizeof(server_addr)) == -1) { + return {ip_error::socket_connect_failed, ip_error_category::instance()}; + } + + set_non_blocking(); + + return {}; + } + + [[nodiscard]] bool no_delay() + { + int flag = 1; + int result = setsockopt(socket_fd, IPPROTO_TCP, TCP_NODELAY, (char*)&flag, sizeof(int)); + return result == 0; + } + + [[nodiscard]] std::error_code bind_and_listen(int port) + { + socket_fd = ::socket(AF_INET, SOCK_STREAM, 0); + if (socket_fd == GLZ_INVALID_SOCKET) { + return {ip_error::socket_bind_failed, ip_error_category::instance()}; + } + + sockaddr_in server_addr; + server_addr.sin_family = AF_INET; + server_addr.sin_addr.s_addr = INADDR_ANY; + server_addr.sin_port = htons(uint16_t(port)); + + if (::bind(socket_fd, (sockaddr*)&server_addr, sizeof(server_addr)) == -1) { + return {ip_error::socket_bind_failed, ip_error_category::instance()}; + } + + if (::listen(socket_fd, SOMAXCONN) == -1) { + return {ip_error::socket_bind_failed, ip_error_category::instance()}; + } + + set_non_blocking(); + if (not no_delay()) { + return {ip_error::socket_bind_failed, ip_error_category::instance()}; + } + + return {}; + } + + template + [[nodiscard]] std::error_code receive(Header& header, std::string& buffer) + { + // first receive the header + size_t total_bytes{}; + while (total_bytes < sizeof(Header)) { + auto bytes = ::recv(socket_fd, reinterpret_cast(&header) + total_bytes, + size_t(sizeof(Header) - total_bytes), 0); + if (bytes == -1) { + if (GLZ_SOCKET_ERROR_CODE == GLZ_EWOULDBLOCK || GLZ_SOCKET_ERROR_CODE == EAGAIN) { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + continue; + } + else { + // error + buffer.clear(); + return {ip_error::receive_failed, ip_error_category::instance()}; + } + } + else if (bytes == 0) { + return {ip_error::client_disconnected, ip_error_category::instance()}; + } + + total_bytes += bytes; + } + + size_t size{}; + if constexpr (std::same_as) { + size = header; + } + else { + size = size_t(header.body_size); + } + + buffer.resize(size); + + total_bytes = 0; + while (total_bytes < size) { + auto bytes = ::recv(socket_fd, buffer.data() + total_bytes, size_t(buffer.size() - total_bytes), 0); + if (bytes == -1) { + if (GLZ_SOCKET_ERROR_CODE == GLZ_EWOULDBLOCK || GLZ_SOCKET_ERROR_CODE == EAGAIN) { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + continue; + } + else { + buffer.clear(); + return {ip_error::receive_failed, ip_error_category::instance()}; + } + } + else if (bytes == 0) { + return {ip_error::client_disconnected, ip_error_category::instance()}; + } + + total_bytes += bytes; + } + return {}; + } + + [[nodiscard]] std::error_code send(const std::string_view buffer) + { + const size_t size = buffer.size(); + size_t total_bytes{}; + while (total_bytes < size) { + auto bytes = ::send(socket_fd, buffer.data() + total_bytes, size_t(buffer.size() - total_bytes), 0); + if (bytes == -1) { + if (GLZ_SOCKET_ERROR_CODE == GLZ_EWOULDBLOCK || GLZ_SOCKET_ERROR_CODE == EAGAIN) { + std::this_thread::yield(); + continue; + } + else { + return {ip_error::send_failed, ip_error_category::instance()}; + } + } + + total_bytes += bytes; + } + return {}; + } + + enum class io_result { completed, would_block, error }; + + io_result send(const std::string_view& buffer, size_t& bytes_sent) + { + auto remaining = buffer.size() - bytes_sent; + auto result = ::send(socket_fd, buffer.data() + bytes_sent, remaining, 0); + if (result > 0) { + bytes_sent += result; + return bytes_sent == buffer.size() ? io_result::completed : io_result::would_block; + } + else if (result == 0 || + (result == -1 && GLZ_SOCKET_ERROR_CODE != GLZ_EWOULDBLOCK && GLZ_SOCKET_ERROR_CODE != EAGAIN)) { + return io_result::error; + } + return io_result::would_block; + } + + template + io_result receive(Header& header, std::string& buffer, size_t& bytes_received) + { + if (bytes_received < sizeof(Header)) { + auto result = + ::recv(socket_fd, reinterpret_cast(&header) + bytes_received, sizeof(Header) - bytes_received, 0); + if (result > 0) { + bytes_received += result; + if (bytes_received < sizeof(Header)) { + return io_result::would_block; + } + } + else if (result == 0 || + (result == -1 && GLZ_SOCKET_ERROR_CODE != GLZ_EWOULDBLOCK && GLZ_SOCKET_ERROR_CODE != EAGAIN)) { + return io_result::error; + } + else { + return io_result::would_block; + } + } + + size_t size{}; + if constexpr (std::same_as) { + size = header; + } + else { + size = size_t(header.body_size); + } + + buffer.resize(size); + + auto result = ::recv(socket_fd, buffer.data() + (bytes_received - sizeof(Header)), + buffer.size() - (bytes_received - sizeof(Header)), 0); + if (result > 0) { + bytes_received += result; + return bytes_received == (sizeof(Header) + buffer.size()) ? io_result::completed : io_result::would_block; + } + else if (result == 0 || + (result == -1 && GLZ_SOCKET_ERROR_CODE != GLZ_EWOULDBLOCK && GLZ_SOCKET_ERROR_CODE != EAGAIN)) { + return io_result::error; + } + return io_result::would_block; + } + }; +} + +#undef GLZ_CLOSE_SOCKET +#undef GLZ_EWOULDBLOCK +#undef GLZ_INVALID_SOCKET +#undef GLZ_SOCKET +#undef GLZ_SOCKET_ERROR +#undef GLZ_SOCKET_ERROR_CODE diff --git a/include/glaze/network/socket_io.hpp b/include/glaze/network/socket_io.hpp new file mode 100644 index 0000000000..d4214f1015 --- /dev/null +++ b/include/glaze/network/socket_io.hpp @@ -0,0 +1,182 @@ +// Glaze Library +// For the license information refer to glaze.hpp + +#pragma once + +#include +#include +#include +#include +#include +#include + +#include "glaze/core/error.hpp" +#include "glaze/glaze.hpp" +#include "glaze/network/socket.hpp" + +namespace glz +{ + template + auto async_connect(Scheduler&& sched, socket& sock, const std::string& address, const int port) + { + return stdexec::just() | stdexec::then([&sock, address, port]() { return sock.connect(address, port); }) | + stdexec::on(std::forward(sched)); + } + + template + auto async_bind_and_listen(Scheduler&& sched, socket& sock, int port) + { + return stdexec::just() | stdexec::then([&sock, port]() { return sock.bind_and_listen(port); }) | + stdexec::on(std::forward(sched)); + } + + template + auto async_send(Scheduler&& sched, socket& sock, const std::string_view buffer) + { + return stdexec::just() | stdexec::let_value([&sock, buffer = std::string(buffer)]() mutable { + size_t bytes_sent = 0; + return exec::repeat_effect_until( + stdexec::then([&]() { + auto result = sock.send(buffer, bytes_sent); + if (result == socket::io_result::error) { + throw std::system_error(errno, std::system_category(), "Send failed"); + } + return result == socket::io_result::completed; + }) | + stdexec::upon_error([](auto&&) { return true; })); + }) | + stdexec::on(std::forward(sched)); + } + + template + auto async_receive(Scheduler&& sched, socket& sock) + { + return stdexec::just() | stdexec::let_value([&sock]() { + Header header{}; + std::string buffer; + size_t bytes_received = 0; + return exec::repeat_effect_until(stdexec::then([&]() { + auto result = sock.receive(header, buffer, bytes_received); + if (result == socket::io_result::error) { + throw std::system_error(errno, + std::system_category(), + "Receive failed"); + } + return result == socket::io_result::completed; + }) | + stdexec::upon_error([](auto&&) { return true; })) | + stdexec::then([header = std::move(header), buffer = std::move(buffer)]() mutable { + return std::make_pair(std::move(header), std::move(buffer)); + }); + }) | + stdexec::on(std::forward(sched)); + } +} + +namespace glz +{ + template + auto async_receive_value(Scheduler&& sched, socket& sckt, T& value) + { + return stdexec::just() | + stdexec::let_value([&sckt, &value, &sched]() { + return async_receive(sched, sckt) + | stdexec::then([&value](auto&& result) { + auto [header, buffer] = std::move(result); + auto ec = glz::read(value, buffer); + if (ec) { + throw std::system_error(int(ec.ec), error_category::instance()); + } + }); + }) | + stdexec::on(std::forward(sched)); + } + + template + auto async_send_value(Scheduler&& sched, socket& sckt, const T& value) + { + return stdexec::just() | + stdexec::let_value([&sckt, &value, &sched]() { + std::string buffer; + auto ec = glz::write(value, buffer); + if (ec) { + throw std::system_error(int(ec.ec), error_category::instance()); + } + + uint64_t header = uint64_t(buffer.size()); + return async_send(sched, sckt, std::string_view{reinterpret_cast(&header), sizeof(header)}) + | stdexec::then([&sckt, buffer = std::move(buffer), &sched]() mutable { + return async_send(sched, sckt, std::move(buffer)); + }); + }) | + stdexec::on(std::forward(sched)); + } +} + +namespace glz +{ + /*template + [[nodiscard]] std::error_code receive(socket& sckt, Buffer& buffer) + { + uint64_t header{}; + if (auto ec = sckt.receive(header, buffer)) { + return ec; + } + return {}; + } + + template + [[nodiscard]] std::error_code send(socket& sckt, Buffer& buffer) + { + uint64_t header = uint64_t(buffer.size()); + + if (auto ec = sckt.send(sv{reinterpret_cast(&header), sizeof(header)})) { + return ec; + } + + if (auto ec = sckt.send(buffer)) { + return ec; + } + + return {}; + }*/ + + template + [[nodiscard]] std::error_code receive_value(socket& sckt, T&& value) + { + static thread_local std::string buffer{}; + + uint64_t header{}; + if (auto ec = sckt.receive(header, buffer)) { + return ec; + } + + if (auto ec = glz::read(std::forward(value), buffer)) { + return {int(ec.ec), error_category::instance()}; + } + + return {}; + } + + template + [[nodiscard]] std::error_code send_value(socket& sckt, T&& value) + { + static thread_local std::string buffer{}; + + if (auto ec = glz::write(std::forward(value), buffer)) { + return {int(ec.ec), error_category::instance()}; + } + + uint64_t header = uint64_t(buffer.size()); + + if (auto ec = sckt.send(sv{reinterpret_cast(&header), sizeof(header)})) { + return ec; + } + + if (auto ec = sckt.send(buffer)) { + return ec; + } + + return {}; + } +} diff --git a/include/glaze/thread/threadpool.hpp b/include/glaze/thread/threadpool.hpp index e4b70e25e1..2b0e93e408 100644 --- a/include/glaze/thread/threadpool.hpp +++ b/include/glaze/thread/threadpool.hpp @@ -5,6 +5,7 @@ #include #include +#include #include #include #include diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 1782c635ac..f3a27c2eac 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -52,7 +52,7 @@ elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") target_compile_options(glz_test_exceptions INTERFACE /W4 /wd4459 /wd4805) endif() -add_subdirectory(asio_repe) +add_subdirectory(repe_server_client) add_subdirectory(api_test) add_subdirectory(binary_test) add_subdirectory(cli_menu_test) @@ -69,6 +69,7 @@ add_subdirectory(mock_json_test) add_subdirectory(stencil_test) add_subdirectory(reflection_test) add_subdirectory(repe_test) +add_subdirectory(socket_test) # We don't run find_package_test or glaze-install_test with MSVC/Windows, because the Github action runner often chokes # Don't run find_package on Clang, because Linux runs with Clang try to use GCC standard library and have errors before Clang 18 diff --git a/tests/asio_repe/CMakeLists.txt b/tests/asio_repe/CMakeLists.txt deleted file mode 100644 index 70417b38f8..0000000000 --- a/tests/asio_repe/CMakeLists.txt +++ /dev/null @@ -1,15 +0,0 @@ -project(asio_repe) - -FetchContent_Declare( - asio - GIT_REPOSITORY https://github.com/chriskohlhoff/asio.git - GIT_TAG asio-1-30-1 - GIT_SHALLOW TRUE -) -FetchContent_GetProperties(asio) -if(NOT asio_POPULATED) - FetchContent_Populate(asio) -endif() - -add_subdirectory(server) -add_subdirectory(client) diff --git a/tests/repe_server_client/CMakeLists.txt b/tests/repe_server_client/CMakeLists.txt new file mode 100644 index 0000000000..ff08e8207a --- /dev/null +++ b/tests/repe_server_client/CMakeLists.txt @@ -0,0 +1,4 @@ +project(repe_server_client) + +add_subdirectory(server) +add_subdirectory(client) diff --git a/tests/asio_repe/client/CMakeLists.txt b/tests/repe_server_client/client/CMakeLists.txt similarity index 67% rename from tests/asio_repe/client/CMakeLists.txt rename to tests/repe_server_client/client/CMakeLists.txt index 67ac43d3d4..19f9c0b70b 100644 --- a/tests/asio_repe/client/CMakeLists.txt +++ b/tests/repe_server_client/client/CMakeLists.txt @@ -2,7 +2,7 @@ project(repe_client) add_executable(${PROJECT_NAME} ${PROJECT_NAME}.cpp) -target_include_directories(${PROJECT_NAME} PRIVATE include ${asio_SOURCE_DIR}/asio/include) +target_include_directories(${PROJECT_NAME} PRIVATE include) target_link_libraries(${PROJECT_NAME} PRIVATE glz_test_exceptions) target_code_coverage(${PROJECT_NAME} AUTO ALL) \ No newline at end of file diff --git a/tests/asio_repe/client/repe_client.cpp b/tests/repe_server_client/client/repe_client.cpp similarity index 72% rename from tests/asio_repe/client/repe_client.cpp rename to tests/repe_server_client/client/repe_client.cpp index 6295935451..40e1907d15 100644 --- a/tests/asio_repe/client/repe_client.cpp +++ b/tests/repe_server_client/client/repe_client.cpp @@ -3,23 +3,24 @@ #include -#include "glaze/ext/glaze_asio.hpp" -#include "glaze/glaze.hpp" -#include "glaze/rpc/repe.hpp" +#include "glaze/network/repe_client.hpp" void asio_client_test() { try { constexpr auto N = 100; - std::vector> clients; + std::vector> clients; clients.reserve(N); std::vector> threads; threads.reserve(N); for (size_t i = 0; i < N; ++i) { - clients.emplace_back(glz::asio_client<>{"localhost", "8080"}); + clients.emplace_back(glz::repe_client<>{"127.0.0.1", 8080}); } + + std::mutex mtx{}; + std::vector results{}; for (size_t i = 0; i < N; ++i) { threads.emplace_back(std::async([&, i] { @@ -38,11 +39,13 @@ void asio_client_test() } int sum{}; - if (auto e_call = client.call({"/sum"}, data, sum); e_call) { + if (auto e_call = client.call({"/sum"}, data, sum)) { std::cerr << glz::write_json(e_call).value_or("error") << '\n'; } else { + std::unique_lock lock{mtx}; std::cout << "i: " << i << ", " << sum << '\n'; + results.emplace_back(sum); } })); } @@ -50,6 +53,12 @@ void asio_client_test() for (auto& t : threads) { t.get(); } + + for (auto v : results) { + if (v != 4950) { + std::abort(); + } + } } catch (const std::exception& e) { std::cerr << e.what() << '\n'; diff --git a/tests/asio_repe/server/CMakeLists.txt b/tests/repe_server_client/server/CMakeLists.txt similarity index 67% rename from tests/asio_repe/server/CMakeLists.txt rename to tests/repe_server_client/server/CMakeLists.txt index 0ee53d4889..50dfb91273 100644 --- a/tests/asio_repe/server/CMakeLists.txt +++ b/tests/repe_server_client/server/CMakeLists.txt @@ -2,7 +2,7 @@ project(repe_server) add_executable(${PROJECT_NAME} ${PROJECT_NAME}.cpp) -target_include_directories(${PROJECT_NAME} PRIVATE include ${asio_SOURCE_DIR}/asio/include) +target_include_directories(${PROJECT_NAME} PRIVATE include) target_link_libraries(${PROJECT_NAME} PRIVATE glz_test_exceptions) target_code_coverage(${PROJECT_NAME} AUTO ALL) \ No newline at end of file diff --git a/tests/asio_repe/server/repe_server.cpp b/tests/repe_server_client/server/repe_server.cpp similarity index 63% rename from tests/asio_repe/server/repe_server.cpp rename to tests/repe_server_client/server/repe_server.cpp index 89056171c0..12a731d55c 100644 --- a/tests/asio_repe/server/repe_server.cpp +++ b/tests/repe_server_client/server/repe_server.cpp @@ -1,26 +1,24 @@ // Glaze Library // For the license information refer to glaze.hpp -#include "glaze/ext/glaze_asio.hpp" -#include "glaze/glaze.hpp" -#include "glaze/rpc/repe.hpp" +#include "glaze/network/repe_server.hpp" struct api { std::function& vec)> sum = [](std::vector& vec) { return std::reduce(vec.begin(), vec.end()); }; - std::function& vec)> max = [](std::vector& vec) { return std::ranges::max(vec); }; + std::function& vec)> maximum = [](std::vector& vec) { return (std::ranges::max)(vec); }; }; #include -void run_server() +int main() { std::cout << "Server active...\n"; try { - glz::asio_server<> server{.port = 8080}; + glz::repe_server<> server{.port = 8080, .print_errors = true}; api methods{}; server.on(methods); server.run(); @@ -30,11 +28,6 @@ void run_server() } std::cout << "Server closed...\n"; -} - -int main() -{ - run_server(); return 0; } diff --git a/tests/socket_test/CMakeLists.txt b/tests/socket_test/CMakeLists.txt new file mode 100644 index 0000000000..2314d21da1 --- /dev/null +++ b/tests/socket_test/CMakeLists.txt @@ -0,0 +1,9 @@ +project(socket_test) + +add_executable(${PROJECT_NAME} ${PROJECT_NAME}.cpp) + +target_link_libraries(${PROJECT_NAME} PRIVATE glz_test_exceptions) + +add_test(NAME ${PROJECT_NAME} COMMAND ${PROJECT_NAME}) + +target_code_coverage(${PROJECT_NAME} AUTO ALL) diff --git a/tests/socket_test/socket_test.cpp b/tests/socket_test/socket_test.cpp new file mode 100644 index 0000000000..c934bcb8e4 --- /dev/null +++ b/tests/socket_test/socket_test.cpp @@ -0,0 +1,149 @@ +// Glaze Library +// For the license information refer to glaze.hpp + +#define UT_RUN_TIME_ONLY + +#include +#include +#include +#include + +#include "glaze/network/server.hpp" +#include "glaze/network/socket_io.hpp" +#include "ut/ut.hpp" + +using namespace ut; + +constexpr bool user_input = false; + +constexpr auto n_clients = 10; +constexpr auto service_0_port{8080}; +constexpr auto service_0_ip{"127.0.0.1"}; + +// std::latch is broken on MSVC: +// std::latch working_clients{n_clients}; +static std::atomic_int working_clients{n_clients}; + +glz::windows_socket_startup_t<> wsa; // wsa_startup (ignored on macOS and Linux) + +glz::server server{service_0_port}; +std::future server_thread{}; + +void handle_client(auto sched, glz::socket client, std::atomic& active) +{ + std::cout << "New client connected!\n"; + + /*auto send_sender = glz::async_send_value(sched, client, "Welcome!"); + stdexec::sync_wait(std::move(send_sender)); + + // Receiving a value + std::string received{}; + auto receive_sender = glz::async_receive_value(sched, client, received); + stdexec::sync_wait(std::move(receive_sender));*/ + + if (auto ec = glz::send_value(client, "Welcome!")) { + std::cerr << ec.message() << '\n'; + return; + } + + while (active) { + std::string received{}; + if (auto ec = glz::receive_value(client, received)) { + std::cerr << ec.message() << '\n'; + return; + } + + std::cout << std::format("Server: {}\n", received); + } +} + +suite make_server = [] { + std::abort(); // IMPORTANT: comment this out when testing, this was added to not make github actions spin forever + std::cout << std::format("Server started on port: {}\n", server.port); + + server_thread = std::async([]{ + auto accept_sender = server.async_accept([](auto sched, glz::socket client_socket, std::atomic& active) { + std::cout << "New client connected!" << std::endl; + handle_client(sched, std::move(client_socket), active); + }); + + // Start the server + std::cout << "Server starting on port 8080..." << std::endl; + auto [result] = stdexec::sync_wait(std::move(accept_sender)).value(); + + if (result) { + std::error_code ec = result; + if (ec) { + std::cerr << "Server error: " << ec.message() << std::endl; + } + } + else { + std::cerr << "Server stopped unexpectedly" << std::endl; + } + }); + + std::this_thread::sleep_for(std::chrono::seconds(1)); +}; + +suite socket_test = [] { + std::vector sockets(n_clients); + std::vector> threads(n_clients); + for (size_t id{}; id < n_clients; ++id) { + threads.emplace_back(std::async([id, &sockets] { + glz::socket& socket = sockets[id]; + + if (socket.connect(service_0_ip, service_0_port)) { + std::cerr << std::format("Failed to connect to server.\nDetails: {}\n", glz::get_socket_error().message()); + } + else { + std::string received{}; + if (auto ec = glz::receive_value(socket, received)) { + std::cerr << ec.message() << '\n'; + return; + } + std::cout << std::format("Received from server: {}\n", received); + + size_t tick{}; + while (tick < 3) { + if (auto ec = glz::send_value(socket, std::format("Client {}, {}", id, tick))) { + std::cerr << ec.message() << '\n'; + return; + } + std::this_thread::sleep_for(std::chrono::seconds(2)); + ++tick; + } + // working_clients.count_down(); + --working_clients; + + while (working_clients) std::this_thread::sleep_for(std::chrono::milliseconds(1)); + if (auto ec = glz::send_value(socket, "close")) { + std::cerr << ec.message() << '\n'; + return; + } + } + })); + } + + // working_clients.arrive_and_wait(); + while (working_clients) std::this_thread::sleep_for(std::chrono::milliseconds(1)); + + if constexpr (user_input) { + std::cout << "\nFinished! Press any key to exit."; + std::cin.get(); + } + + server.active = false; +}; + +int main() +{ + std::signal(SIGINT, [](int) { + server.active = false; + std::exit(0); + }); + + // GCC needs this sleep + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + + return 0; +}