Skip to content

Commit

Permalink
Add wrapper for CXX_MODULES
Browse files Browse the repository at this point in the history
  • Loading branch information
ClausKlein committed Nov 30, 2024
1 parent 9c62c54 commit 36bcecf
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 12 deletions.
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
11 changes: 5 additions & 6 deletions asio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ 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 ----
Expand All @@ -37,6 +36,7 @@ find_package(Threads REQUIRED)
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})
Expand All @@ -51,17 +51,15 @@ target_link_libraries(asio_header INTERFACE Threads::Threads)
if(ASIO_SEPARATE_COMPILATION)
set(_libasio_SOURCES src/asio.cpp)

# TODO(ssl): find_package(OpenSSL)
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 +87,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 +97,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
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;
}
}

}
35 changes: 33 additions & 2 deletions asio/src/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,34 @@ 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 20.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)
Expand All @@ -14,6 +40,11 @@ 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)
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()
21 changes: 19 additions & 2 deletions asio/src/examples/cpp20/operations/callback_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,25 @@
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#include <asio.hpp>
#include <iostream>
#ifdef USE_MODULES
# ifdef HAS_STDLIB_MODULES
import std;
import std.compat;
# else
# include <cstdlib>
# include <exception>
# include <future>
# include <iostream>
# include <string>
# include <system_error>
# include <thread>
# include <utility>
# endif
import asio;
# else
# include <asio.hpp>
# include <iostream>
# endif

//------------------------------------------------------------------------------

Expand Down

0 comments on commit 36bcecf

Please sign in to comment.