1- # add c-ares
2- macro (add_cares )
1+ # build and install c-ares
2+ macro (build_install_cares )
33 if (NOT c-ares_DOWNLOAD_URL)
44 get_github_latest_release_url("c-ares" "c-ares" )
55 endif ()
66 FetchContent_Declare(c-ares URL ${c-ares_DOWNLOAD_URL})
7- FetchContent_MakeAvailable(c-ares)
7+ # not using FetchContent_MakeAvailable to save compile time
8+ FetchContent_GetProperties(c-ares)
9+
10+ if (NOT c-ares_POPULATED)
11+ FetchContent_Populate(c-ares)
12+
13+ # configure build and install, c-ares provided makefiles already, using them to build is faster
14+ if (CMAKE_CXX_COMPILER_ID MATCHES MSVC )
15+ execute_process (COMMAND buildconf.bat WORKING_DIRECTORY ${c-ares_SOURCE_DIR})
16+ execute_process (COMMAND nmake -f Makefile.msvc WORKING_DIRECTORY ${c-ares_SOURCE_DIR})
17+ set (ENV{INSTALL_DIR} ${C-ARES_DIR})
18+ execute_process (COMMAND nmake -f Makefile.msvc install WORKING_DIRECTORY ${c-ares_SOURCE_DIR})
19+ else ()
20+ execute_process (COMMAND ./buildconf WORKING_DIRECTORY ${c-ares_SOURCE_DIR})
21+ execute_process (COMMAND make WORKING_DIRECTORY ${c-ares_SOURCE_DIR})
22+ execute_process (COMMAND make --install WORKING_DIRECTORY ${c-ares_SOURCE_DIR})
23+ endif ()
24+
25+ endif ()
826endmacro ()
927
1028# Set normal resolver
@@ -30,12 +48,28 @@ macro(set_cares_resolver)
3048 list (APPEND TRANTOR_SOURCES trantor/net/inner/AresResolver.cc)
3149 list (APPEND private_headers trantor/net/inner/AresResolver.h)
3250
51+ # copy dll/libs for Windows, to solve the test/unittests running 0x000135 error
52+ copy_files_for_win(${C-ARES_DIR}/bin "dll" )
53+ copy_files_for_win(${C-ARES_DIR}/lib "lib" )
54+ copy_files_for_win(${C-ARES_DIR}/lib "dll" )
3355endmacro ()
3456
3557# ######################################################################################################################
3658message (STATUS "Setting c-ares: ${TRANTOR_USE_C-ARES}" )
3759
3860if (TRANTOR_USE_C-ARES)
61+ # Set C-ARES_DIR, to let find_package could search from it
62+ if (WIN32 )
63+ set (C-ARES_DIR
64+ c:/c-ares
65+ CACHE PATH "Let find_package use the directory to find" FORCE
66+ )
67+ else ()
68+ set (C-ARES_DIR
69+ ${CMAKE_INSTALL_PREFIX} /c-ares
70+ CACHE PATH "Let find_package use the directory to find" FORCE
71+ )
72+ endif ()
3973
4074 find_package (c-ares)
4175
@@ -44,7 +78,8 @@ if(TRANTOR_USE_C-ARES)
4478 message (STATUS "⚠️c-ares not found, Building it with FetchContent..." )
4579 unset (c-ares_FOUND)
4680
47- add_cares()
81+ build_install_cares()
82+ find_package (c-ares REQUIRED)
4883
4984 else ()
5085 message (FATAL_ERROR "⛔c-ares not found, please install it or set FETCH_BUILD_MISSING_DEPS to ON" )
0 commit comments