From b6783d40bea876601e163fbdc1cabc9823f477f3 Mon Sep 17 00:00:00 2001 From: Hermann von Kleist Date: Fri, 31 Mar 2023 17:18:48 +0200 Subject: [PATCH] CMake: find_package(asio) if available --- CMakeLists.txt | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fb8ca62..65a06e9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,10 +24,20 @@ target_link_libraries(simple-web-server INTERFACE Threads::Threads) if(USE_STANDALONE_ASIO) target_compile_definitions(simple-web-server INTERFACE ASIO_STANDALONE) - find_path(ASIO_PATH asio.hpp) - if(NOT ASIO_PATH) - message(FATAL_ERROR "Standalone Asio not found") + # There is no canonical way to use Asio from CMake. + # In particular, Asio does not support CMake natively. + # However, Conan and Vcpkg do provide CMake support on their own. + # Prefer the CMake target and fall back to finding asio.hpp. + if(NOT TARGET asio::asio) + find_package(asio) + endif() + if(TARGET asio::asio) + target_link_libraries(simple-web-server INTERFACE asio::asio) else() + find_path(ASIO_PATH asio.hpp) + if(NOT ASIO_PATH) + message(FATAL_ERROR "Standalone Asio not found") + endif() target_include_directories(simple-web-server SYSTEM INTERFACE ${ASIO_PATH}) endif() else()