Skip to content

Use CURL library for the network test instead of external tools. #4541

@SunBlack

Description

@SunBlack

We are currently still using a relatively old version of PROJ and are about to upgrade. We are using a Dockerfile for our project that is still based on Ubuntu 22.04, so CURL is not preinstalled, which is why I installed libcurl4-openssl-dev. However, when building I get the message:

Cannot establish if network is available - 'curl' or 'ping' not found

This is technically correct, as libcurl4-openssl-dev does not install curl itself. However, I think it would make more sense to test the ping with libcurl4-openssl-dev itself. You could do it via try_compile + execute_progress:

Since I have thus far not worked with the CURL API, I used AI to generate an example that shows the direction in which I was thinking.

# Make sure we have the helper module for checking C++ source compilation
include(CheckCXXSourceCompiles)

# 1. Write a temporary C++ source file that attempts to ping Google using libcurl
set(TEST_SOURCE "${CMAKE_BINARY_DIR}/curl_test.cpp")
file(WRITE ${TEST_SOURCE} "
#include <curl/curl.h>
#include <iostream>
int main() {
    CURL* curl = curl_easy_init();
    if(curl) {
        curl_easy_setopt(curl, CURLOPT_URL, \"https://www.google.com\");
        curl_easy_setopt(curl, CURLOPT_NOBODY, 1L); // Request headers only
        CURLcode res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);
        return res == CURLE_OK ? 0 : 1;
    }
    return 1;
}
")

# 2. Compile the test program with libcurl
try_compile(CURL_TEST_OK
    ${CMAKE_BINARY_DIR}/curl_test_build
    ${TEST_SOURCE}
    LINK_LIBRARIES curl
)

if(CURL_TEST_OK)
    message(STATUS "Compilation successful.")

    # 3. Attempt to execute the compiled test binary
    execute_process(
        COMMAND ${CMAKE_BINARY_DIR}/curl_test_build/cmTC_*/cmTC_*
        RESULT_VARIABLE EXITCODE
        OUTPUT_VARIABLE STDOUT
        ERROR_VARIABLE STDERR
        OUTPUT_STRIP_TRAILING_WHITESPACE
        ERROR_STRIP_TRAILING_WHITESPACE
    )

    if(EXITCODE EQUAL 0)
        message(STATUS "Google is reachable (libcurl appears to work).")
    else()
        message(WARNING "Ping failed. libcurl compiled, but could not reach Google.\n${STDERR}")
    endif()
else()
    message(WARNING "Compilation failed. libcurl may be missing or misconfigured.")
endif

I think a change to this would be good, as it would make it easier to recognize whether there are really network issues (for proj) or not when building.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions