CMake INACTIVITY_TIMEOUT not foolproof

CMake implements a file(DOWNLOAD INACTIVITY_TIMEOUT) option that uses curl CURLOPT_LOW_SPEED_TIME to fail much earlier than a TIMEOUT option would when the connection speed is extremely slow. However, some systems intentionally block downloads, but do so in a way that fools the underlying curl library into not tripping INACTIVITY_TIMEOUT. Rather than try to figure out a direct fix in curl for this apparently longstanding problem, we instead decide to implement a CMake configure-time internet connectivity detection.

cmake_minimum_required(VERSION 3.19)
project(Connect LANGUAGES NONE)
enable_testing()
add_test(NAME InternetConnection COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_LIST_DIR}/connection.cmake)
set_property(TEST InternetConnection PROPERTY FIXTURES_SETUP InternetOK)
add_test(NAME NeedsInternet COMMAND ...)
set_property(TEST NeedsInternet PROPERTY FIXTURES_REQUIRED InternetOK)
view raw CMakeLists.txt hosted with ❤ by GitHub
file(DOWNLOAD https://connectivitycheck.gstatic.com/generate_204
STATUS ret
)
list(GET ret 0 status)
if(NOT status EQUAL 0)
list(GET ret 1 msg)
message(FATAL_ERROR "Internet connectivity test failed.
Return code: ${status}
Error: ${msg}"
)
endif()

When a CTest test is comprised of a CMake script, this test can be setup as a FIXTURES_SETUP to avoid calling it repeatedly for each test, instead just once for a set of tests.

The key technique here is the URL connectivitycheck.gstatic.com/generate_204 that is used by Android devices to check connectivity. Any similar URL with zero or tiny download size would also be suitable.