CMake: find_package(asio) if available

This commit is contained in:
Hermann von Kleist 2023-03-31 17:18:48 +02:00
commit b6783d40be

View file

@ -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)
# 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")
else()
endif()
target_include_directories(simple-web-server SYSTEM INTERFACE ${ASIO_PATH})
endif()
else()