Skip to content

Commit

Permalink
Make user provide standalone Asio library to use
Browse files Browse the repository at this point in the history
Instead of fetching a specific version of Asio to use when building
without boost, expect the user to provide a version to use similar to
how boost is used.

This is more flexible and probably what most users would expect.
  • Loading branch information
laudrup committed Jan 26, 2024
1 parent 9fa03d2 commit c9dc543
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 27 deletions.
11 changes: 10 additions & 1 deletion .github/workflows/unittest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,15 @@ jobs:
uses: mxschmitt/action-tmate@v3

unittest-standalone-asio:
name: "Asio ${{matrix.build_type}} C++${{matrix.standard}}"
name: "${{matrix.asio_version}} ${{matrix.build_type}} C++${{matrix.standard}}"
runs-on: ${{matrix.os}}
defaults:
run:
shell: bash
strategy:
fail-fast: false
matrix:
asio_version: ["asio-1-28-0", "asio-1-29-0"]
os: [windows-2022]
build_type: [Debug, Release]
standard: [14, 17, 20]
Expand All @@ -162,6 +163,14 @@ jobs:
- name: Create build directory
run: mkdir build

- name: Checkout Asio
working-directory: build
uses: actions/checkout@v4
with:
repository: https://github.com/chriskohlhoff/asio.git
ref: ${{matrix.asio_version}}
fetch-depth: 0

- name: Configure
working-directory: build
run: |
Expand Down
36 changes: 10 additions & 26 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ cmake_minimum_required(VERSION 3.15)

set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})

include(FetchContent)
include(VersionFromGit)

version_from_git()
Expand Down Expand Up @@ -35,36 +34,12 @@ option(WARNINGS_AS_ERRORS "Treat compiler warnings as errors" ON)

add_library(${PROJECT_NAME} INTERFACE)

if(ENABLE_WINTLS_STANDALONE_ASIO)
FetchContent_Declare(
asio
GIT_REPOSITORY https://github.com/chriskohlhoff/asio.git
GIT_TAG asio-1-28-0
)

FetchContent_GetProperties(asio)
if(NOT asio_POPULATED)
FetchContent_Populate(asio)
endif()
endif()

target_include_directories(${PROJECT_NAME}
INTERFACE
$<BUILD_INTERFACE:${${PROJECT_NAME}_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)

if(ENABLE_WINTLS_STANDALONE_ASIO)
target_include_directories(${PROJECT_NAME}
INTERFACE
${asio_SOURCE_DIR}/asio/include
)
target_compile_definitions(${PROJECT_NAME}
INTERFACE
WINTLS_USE_STANDALONE_ASIO
)
endif()

if(WIN32)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
Expand Down Expand Up @@ -149,7 +124,16 @@ if(WIN32)
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /DEBUG /OPT:REF /OPT:ICF")
endif()

if(NOT ENABLE_WINTLS_STANDALONE_ASIO)
if(ENABLE_WINTLS_STANDALONE_ASIO)
find_package(Asio REQUIRED)

target_link_libraries(${PROJECT_NAME} INTERFACE
Asio::Asio
)
target_compile_definitions(${PROJECT_NAME} INTERFACE
WINTLS_USE_STANDALONE_ASIO
)
else()
find_package(Boost REQUIRED)

target_link_libraries(${PROJECT_NAME} INTERFACE
Expand Down
44 changes: 44 additions & 0 deletions cmake/FindAsio.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
include(FindPackageHandleStandardArgs)

find_package(Threads QUIET)

if (NOT Threads_FOUND)
message(FATAL_ERROR "Asio requires Threads")
endif()

if(DEFINED ENV{ASIO_ROOT})
set(ASIO_DIR "$ENV{ASIO_ROOT}/asio/include/")
else()
set(ASIO_DIR "${CMAKE_CURRENT_BINARY_DIR}/asio/asio/include/")
endif()

find_path(ASIO_INCLUDE_DIR asio.hpp
HINTS
${ASIO_DIR}
)

if(ASIO_INCLUDE_DIR AND EXISTS "${ASIO_INCLUDE_DIR}/asio/version.hpp")
file(STRINGS "${ASIO_INCLUDE_DIR}/asio/version.hpp" asio_version_str REGEX "^#define[ \t]+ASIO_VERSION[ \t]+.*")
string(REGEX REPLACE "^#define[ \t]+ASIO_VERSION[ \t]+([0-9]+).*$" "\\1" asio_version_number "${asio_version_str}")
math(EXPR asio_sub_minor_version "${asio_version_number} % 100")
math(EXPR asio_minor_version "${asio_version_number} / 100 % 1000")
math(EXPR asio_major_version "${asio_version_number} / 100000")
set(ASIO_VERSION_STRING "${asio_major_version}.${asio_minor_version}.${asio_sub_minor_version}")
endif()

find_package_handle_standard_args(
Asio
REQUIRED_VARS ASIO_INCLUDE_DIR
VERSION_VAR ASIO_VERSION_STRING
)

mark_as_advanced(ASIO_INCLUDE_DIR)

if(ASIO_FOUND AND NOT TARGET Asio::Asio)
add_library(Asio::Asio INTERFACE IMPORTED)
target_include_directories(Asio::Asio
INTERFACE
${ASIO_INCLUDE_DIR}
Threads::Threads
)
endif()

0 comments on commit c9dc543

Please sign in to comment.