Skip to content

Commit

Permalink
Update to macos-15 on CI
Browse files Browse the repository at this point in the history
Add wrapper for CXX_MODULES

The example module test needs oppenssl
  • Loading branch information
ClausKlein committed Nov 30, 2024
1 parent 7fcf4a4 commit 96a0e72
Show file tree
Hide file tree
Showing 11 changed files with 282 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ jobs:

strategy:
matrix:
os: [macos-12, ubuntu-24.04, windows-2022]
os: [macos-15, ubuntu-24.04, windows-2022]

runs-on: ${{ matrix.os }}

Expand Down
1 change: 1 addition & 0 deletions asio/.cmakefiles.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ cmake/spell-targets.cmake
cmake/spell.cmake
cmake/variables.cmake
cmake/windows-set-path.cmake
module/asio.cppm
src/examples/CMakeLists.txt
src/tests/CMakeLists.txt
'
Expand Down
12 changes: 6 additions & 6 deletions asio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,23 @@ elseif(ASIO_HAS_SNPRINTF)
endif()

option(ASIO_SEPARATE_COMPILATION "build asio lib too" ${PROJECT_IS_TOP_LEVEL})

set(CMAKE_DEBUG_POSTFIX D)

# ---- add dependency libraries ----

find_package(Threads REQUIRED)
find_package(OpenSSL REQUIRED)

# ---- Declare library ----

file(GLOB_RECURSE _asio_implementation "include/asio/*/*.hpp" "include/asio/*.ipp")
list(FILTER _asio_implementation EXCLUDE REGEX [=[.*/experimental/.*]=])

# asio interface library
add_library(asio_header INTERFACE)
add_library(asio::asio_header ALIAS asio_header)
target_sources(asio_header INTERFACE FILE_SET HEADERS BASE_DIRS include FILES ${_asio_implementation})
target_link_libraries(asio_header INTERFACE OpenSSL::SSL OpenSSL::Crypto)

target_compile_definitions(asio_header INTERFACE ${CPPdefinitions})
target_compile_features(
Expand All @@ -51,17 +53,14 @@ target_link_libraries(asio_header INTERFACE Threads::Threads)
if(ASIO_SEPARATE_COMPILATION)
set(_libasio_SOURCES src/asio.cpp)

# TODO(ssl): find_package(OpenSSL)
if(OpenSSL_FOUND)
# TODO(ssl): list(APPEND _libasio_SOURCES src/asio_ssl.cpp)
list(APPEND _libasio_SOURCES src/asio_ssl.cpp)
endif()

set(CMAKE_VERIFY_INTERFACE_HEADER_SETS ${PROJECT_IS_TOP_LEVEL})

file(GLOB_RECURSE _asio_headers "include/asio/*.hpp")
list(FILTER _asio_headers EXCLUDE REGEX [=[.*/experimental/.*\.hpp]=])
list(FILTER _asio_headers EXCLUDE REGEX [=[.*/ssl\.hpp]=])
list(FILTER _asio_headers EXCLUDE REGEX [=[.*/ssl/\.*]=])
list(FILTER _asio_headers EXCLUDE REGEX [=[.*/spawn.*\.hpp]=])

# FIXME: some header in include/asio/detail fails CMAKE_VERIFY_INTERFACE_HEADER_SETS!
Expand Down Expand Up @@ -89,7 +88,7 @@ if(ASIO_SEPARATE_COMPILATION)
endif()

if(OpenSSL_FOUND)
target_link_libraries(asio PUBLIC OpenSSL::SSL)
target_link_libraries(asio PUBLIC OpenSSL::SSL OpenSSL::Crypto)
endif()
endif()

Expand All @@ -99,6 +98,7 @@ if(NOT CMAKE_SKIP_INSTALL_RULES)
include(cmake/install-rules.cmake)
configure_file(asio.pc.cmake asio.pc)
install(FILES ${PROJECT_BINARY_DIR}/asio.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
install(DIRECTORY module DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/asio)
endif()

# ---- Developer mode ----
Expand Down
2 changes: 1 addition & 1 deletion asio/CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
"hidden": true,
"cacheVariables": {
"CMAKE_CXX_FLAGS":
"-fstack-protector-strong -fcf-protection=full -Wall -Wextra -Wpedantic -Wno-conversion -Wno-sign-conversion -Wcast-qual -Wformat=2 -Wno-undef -Werror=float-equal -Wshadow -Wcast-align -Wunused -Wnull-dereference -Wdouble-promotion -Wno-implicit-fallthrough -Wextra-semi -Woverloaded-virtual -Wnon-virtual-dtor -Wno-old-style-cast"
"-fstack-protector-strong -Wall -Wextra -Wpedantic -Wno-conversion -Wno-sign-conversion -Wcast-qual -Wformat=2 -Wundef -Werror=float-equal -Wshadow -Wcast-align -Wunused -Wnull-dereference -Wdouble-promotion -Wno-implicit-fallthrough -Wextra-semi -Woverloaded-virtual -Wnon-virtual-dtor -Wno-old-style-cast"
},
"condition": {
"type": "equals",
Expand Down
25 changes: 25 additions & 0 deletions asio/cmake/install-config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,29 @@ include(CMakeFindDependencyMacro)
find_dependency(Threads)
find_dependency(OpenSSL)

function(add_asio_module NAME)
set(ASIO_ROOT @CMAKE_INSTALL_PREFIX@)
message(STATUS "ASIO_ROOT is: ${ASIO_ROOT}")

add_library(${NAME})
target_include_directories(${NAME} PRIVATE ${ASIO_ROOT}/include)
target_compile_features(${NAME} PUBLIC cxx_std_23)

set(CPPdefinitions "@CPPdefinitions@")
if(CPPdefinitions)
target_compile_definitions(${NAME} PUBLIC ${CPPdefinitions})
endif()

# cmake-format: off
target_sources(${NAME} PUBLIC
FILE_SET modules_public
TYPE CXX_MODULES
BASE_DIRS ${ASIO_ROOT}
FILES
${ASIO_ROOT}/lib/cmake/asio/module/asio.cppm
)
# cmake-format: on
target_link_libraries(${NAME} PUBLIC OpenSSL::SSL OpenSSL::Crypto)
endfunction()

include("${CMAKE_CURRENT_LIST_DIR}/asioTargets.cmake")
5 changes: 3 additions & 2 deletions asio/cmake/install-rules.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ set(ASIO_INSTALL_CMAKEDIR "${CMAKE_INSTALL_LIBDIR}/cmake/${_package}"
)
mark_as_advanced(ASIO_INSTALL_CMAKEDIR)

install(FILES cmake/install-config.cmake DESTINATION "${ASIO_INSTALL_CMAKEDIR}" RENAME "${_package}Config.cmake"
COMPONENT asio_Development
configure_file(cmake/install-config.cmake install-config.cmake @ONLY)
install(FILES ${PROJECT_BINARY_DIR}/install-config.cmake DESTINATION "${ASIO_INSTALL_CMAKEDIR}"
RENAME "${_package}Config.cmake" COMPONENT asio_Development
)

install(FILES "${PROJECT_BINARY_DIR}/${_package}ConfigVersion.cmake" DESTINATION "${ASIO_INSTALL_CMAKEDIR}"
Expand Down
67 changes: 67 additions & 0 deletions asio/module/asio.cppm
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
module;

#include <asio.hpp>
#include <asio/ssl.hpp>

export module asio;

namespace asio {
export using asio::io_context;
export using asio::post;
export using asio::any_completion_executor;
export using asio::any_io_executor;
export using asio::bad_executor;
export using asio::cancellation_signal;
export using asio::cancellation_slot;
export using asio::cancellation_state;
export using asio::cancellation_type;
export using asio::coroutine;
export using asio::detached_t;
export using asio::execution_context;
export using asio::executor;
export using asio::executor_arg_t;
export using asio::invalid_service_owner;
export using asio::io_context;
export using asio::multiple_exceptions;
export using asio::service_already_exists;
export using asio::static_thread_pool;
export using asio::system_context;
export using asio::system_executor;
export using asio::thread_pool;
export using asio::awaitable;
export using asio::deferred;
export using asio::detached;
export using asio::connect;
export using asio::async_connect;
export using asio::buffer;
export using asio::socket_base;
export using asio::co_spawn;

namespace error {
export using asio::error::make_error_code;
}

namespace this_coro {
export using asio::this_coro::executor;
}

namespace ip {
export using asio::ip::tcp;
export using asio::ip::address;
export using asio::ip::address_v4;
}

namespace ssl {
export using asio::ssl::context;
export using asio::ssl::context_base;
export using asio::ssl::host_name_verification;
export using asio::ssl::stream_base;
export using asio::ssl::verify_context;
export using asio::ssl::stream;
namespace error {
export using asio::ssl::error::stream_errors;
export using asio::ssl::error::make_error_code;
}
}

}
3 changes: 3 additions & 0 deletions asio/module/tests/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "main.hpp"

int main() { return bench::main_impl(); }
119 changes: 119 additions & 0 deletions asio/module/tests/main.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#ifdef USE_MODULES
# ifdef HAS_STDLIB_MODULES
import std;
import std.compat;
# else
# include <coroutine>
# include <cstdlib>
# include <exception>
# include <system_error>
# endif
import asio;
#else
# include <asio/awaitable.hpp>
# include <asio/buffer.hpp>
# include <asio/co_spawn.hpp>
# include <asio/connect.hpp>
# include <asio/deferred.hpp>
# include <asio/detached.hpp>
# include <asio/io_context.hpp>
# include <asio/ip/address_v4.hpp>
# include <asio/ip/tcp.hpp>
# include <asio/ssl/error.hpp>
# include <asio/ssl/stream.hpp>
# include <asio/this_coro.hpp>
#endif

namespace bench {

namespace net = asio;
namespace ssl = asio::ssl;
using tcp = asio::ip::tcp;
using std::error_code;

inline void fail(error_code ec, char const* what)
{
if(ec == net::ssl::error::stream_truncated)
return;
exit(1);
}

inline net::awaitable<void> do_session(tcp::socket client_sock)
{
char buff [4096] {};
auto ex = co_await net::this_coro::executor;

ssl::context ctx {ssl::context::tls_client};
ssl::stream<tcp::socket> stream {std::move(client_sock), ctx};

co_await stream.async_handshake(ssl::stream_base::server, net::deferred);
co_await stream.async_read_some(net::buffer(buff), net::deferred);
co_await stream.async_write_some(net::buffer(buff), net::deferred);
co_await stream.async_shutdown(net::deferred);
stream.next_layer().close();
}

inline net::awaitable<void> do_listen()
{
auto ex = co_await net::this_coro::executor;
error_code ec;
tcp::endpoint endpoint {net::ip::address_v4::loopback(), 3000};

// Open the acceptor
tcp::acceptor acceptor{ex};
acceptor.open(endpoint.protocol(), ec);
if(ec)
fail(ec, "open");

// Allow address reuse
acceptor.set_option(net::socket_base::reuse_address(true), ec);
if(ec)
fail(ec, "set_option");

// Bind to the server address
acceptor.bind(endpoint, ec);
if(ec)
fail(ec, "bind");

// Start listening for connections
acceptor.listen(net::socket_base::max_listen_connections, ec);
if(ec)
fail(ec, "listen");

for(;;)
{
tcp::socket socket{ex};
co_await acceptor.async_accept(socket, net::deferred);
net::co_spawn(ex, [s = std::move(socket)] mutable {
return do_session(std::move(s));
}, net::detached);
}
}

inline int main_impl()
{
// The io_context is required for all I/O
net::io_context ioc;

// Spawn a listening port
net::co_spawn(
ioc,
do_listen,
// on completion, spawn will call this function
[](std::exception_ptr ex)
{
// if an exception occurred in the coroutine,
// it's something critical, e.g. out of memory
// we capture normal errors in the ec
// so we just rethrow the exception here,
// which will cause `ioc.run()` to throw
if (ex)
std::rethrow_exception(ex);
});

ioc.run();

return 0;
}

}
41 changes: 37 additions & 4 deletions asio/src/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,50 @@ cmake_minimum_required(VERSION 3.25...3.31)
project(asio_examples LANGUAGES CXX)

if(PROJECT_IS_TOP_LEVEL)
find_package(asio 1.32.0.1 EXACT REQUIRED)
# Find the Asio package containing the function to build the module
find_package(asio 1.32.0.1 EXACT REQUIRED HINT ../stagedir/lib)
enable_testing()

# Use modules?
unset(USE_MODULES)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.28.4 AND CMAKE_GENERATOR STREQUAL "Ninja")
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 17.0)
set(USE_MODULES TRUE)
# see https://releases.llvm.org/15.0.0/projects/libcxx/docs/ReleaseNotes.html
# Always use libc++
add_compile_options(-fexperimental-library)
add_link_options(-lc++experimental)
add_compile_options(-stdlib=libc++)
add_link_options(-stdlib=libc++)
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 15.0)
set(USE_MODULES TRUE)
endif()
message(STATUS "CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES=${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES}")
endif()
if(USE_MODULES)
# Build the asio module
add_asio_module(asio_module)
endif()

endif()

set(ALL_EXAMPLES callback_wrapper)
# set(ALL_EXAMPLES cpp20/operations/callback_wrapper.cpp)
# set(ALL_EXAMPLES module/tests/main.cpp)
set(ALL_EXAMPLES main)

message(STATUS "Examples to be built: ${ALL_EXAMPLES}")

foreach(example ${ALL_EXAMPLES})
add_executable(${example})
target_sources(${example} PRIVATE cpp20/operations/${example}.cpp)
target_link_libraries(${example} asio::asio_header)
target_sources(${example} PRIVATE ../../module/tests/${example}.cpp)
if(USE_MODULES)
target_link_libraries(${example} asio_module)
target_compile_definitions(${example} PUBLIC USE_MODULES)
else()
target_link_libraries(${example} asio::asio_header)
endif()
add_test(NAME ${example} COMMAND echo Test | ${example})
endforeach()
Loading

0 comments on commit 96a0e72

Please sign in to comment.