From: Denis Ovsienko Date: Tue, 17 Sep 2024 23:22:58 +0000 (+0100) Subject: CMake: Skip snprintf(3) tests when cross-compiling. X-Git-Url: https://git.tcpdump.org/tcpdump/commitdiff_plain/b735a4c3b343bdf54a01e79127d13c921758f4f9 CMake: Skip snprintf(3) tests when cross-compiling. Same as in libpcap. (cherry picked from commit 2ec7e78bacc1d211af8765af4b4675eb6f1532ef) --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 9de318a2..b72b3df4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -500,56 +500,60 @@ else(STDLIBS_HAVE_GETSERVENT) endif(STDLIBS_HAVE_GETSERVENT) cmake_pop_check_state() -# -# Require a proof of suitable snprintf(3), same as in Autoconf. -# -include(CheckCSourceRuns) -check_c_source_runs(" -#include -#include -#include -#include - -int main() -{ - char buf[100]; - uint64_t t = (uint64_t)1 << 32; - - snprintf(buf, sizeof(buf), \"%zu\", sizeof(buf)); - if (strncmp(buf, \"100\", sizeof(buf))) - return 1; - - snprintf(buf, sizeof(buf), \"%zd\", -sizeof(buf)); - if (strncmp(buf, \"-100\", sizeof(buf))) - return 2; - - snprintf(buf, sizeof(buf), \"%\" PRId64, -t); - if (strncmp(buf, \"-4294967296\", sizeof(buf))) - return 3; - - snprintf(buf, sizeof(buf), \"0o%\" PRIo64, t); - if (strncmp(buf, \"0o40000000000\", sizeof(buf))) - return 4; - - snprintf(buf, sizeof(buf), \"0x%\" PRIx64, t); - if (strncmp(buf, \"0x100000000\", sizeof(buf))) - return 5; - - snprintf(buf, sizeof(buf), \"%\" PRIu64, t); - if (strncmp(buf, \"4294967296\", sizeof(buf))) - return 6; - - return 0; -} - -" - SUITABLE_SNPRINTF -) -if(NOT SUITABLE_SNPRINTF) - message(FATAL_ERROR +if (NOT CMAKE_CROSSCOMPILING) + # + # Require a proof of suitable snprintf(3), same as in Autoconf. + # + include(CheckCSourceRuns) + check_c_source_runs(" + #include + #include + #include + #include + + int main() + { + char buf[100]; + uint64_t t = (uint64_t)1 << 32; + + snprintf(buf, sizeof(buf), \"%zu\", sizeof(buf)); + if (strncmp(buf, \"100\", sizeof(buf))) + return 1; + + snprintf(buf, sizeof(buf), \"%zd\", -sizeof(buf)); + if (strncmp(buf, \"-100\", sizeof(buf))) + return 2; + + snprintf(buf, sizeof(buf), \"%\" PRId64, -t); + if (strncmp(buf, \"-4294967296\", sizeof(buf))) + return 3; + + snprintf(buf, sizeof(buf), \"0o%\" PRIo64, t); + if (strncmp(buf, \"0o40000000000\", sizeof(buf))) + return 4; + + snprintf(buf, sizeof(buf), \"0x%\" PRIx64, t); + if (strncmp(buf, \"0x100000000\", sizeof(buf))) + return 5; + + snprintf(buf, sizeof(buf), \"%\" PRIu64, t); + if (strncmp(buf, \"4294967296\", sizeof(buf))) + return 6; + + return 0; + } + + " + SUITABLE_SNPRINTF + ) + if(NOT SUITABLE_SNPRINTF) + message(FATAL_ERROR "The snprintf(3) implementation in this libc is not suitable, tcpdump would not work correctly even if it managed to compile." - ) + ) + endif() +else() + message(STATUS "Skipped SUITABLE_SNPRINTF because cross-compiling.") endif() check_function_exists(getopt_long HAVE_GETOPT_LONG)