Author: brane
Date: Sat Jun 14 13:05:53 2025
New Revision: 1926412

URL: http://svn.apache.org/viewvc?rev=1926412&view=rev
Log:
In the CMake build: add a runtime check to determine if the target platform
uses ELF, instead of guessing based on the name of the platform.

* build/SerfPlatform.cmake
  (SERF_ELF_TARGET): Set to FALSE on Windows and Darwin only.
  (_CheckElfTarget): New configure-time test to determine if
   SERF_ELF_TARGET should be set (to TRUE).

Modified:
    serf/trunk/build/SerfPlatform.cmake

Modified: serf/trunk/build/SerfPlatform.cmake
URL: 
http://svn.apache.org/viewvc/serf/trunk/build/SerfPlatform.cmake?rev=1926412&r1=1926411&r2=1926412&view=diff
==============================================================================
--- serf/trunk/build/SerfPlatform.cmake (original)
+++ serf/trunk/build/SerfPlatform.cmake Sat Jun 14 13:05:53 2025
@@ -17,9 +17,20 @@
 #   under the License.
 # ===================================================================
 
-if(${CMAKE_SYSTEM_NAME} MATCHES  "Darwin")
+if(${CMAKE_SYSTEM_NAME} MATCHES "Windows")
+  set(SERF_WINDOWS TRUE)
+  set(SERF_ELF_TARGET FALSE)
+  if(CMAKE_GENERATOR_PLATFORM MATCHES "(x64|ARM64|IA64)")
+    set(SERF_WIN64 TRUE)
+    set(SERF_PLATFORM "Windows (64-bit)")
+  else()
+    set(SERF_WIN32 TRUE)
+    set(SERF_PLATFORM "Windows (32-bit)")
+  endif()
+elseif(${CMAKE_SYSTEM_NAME} MATCHES  "Darwin")
   set(SERF_UNIX TRUE)
   set(SERF_DARWIN TRUE)
+  set(SERF_ELF_TARGET FALSE)
   if(NOT RELATIVE_RPATH)
     set(CMAKE_MACOSX_RPATH FALSE)
   endif()
@@ -31,32 +42,19 @@ if(${CMAKE_SYSTEM_NAME} MATCHES  "Darwin
 elseif(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
   set(SERF_UNIX TRUE)
   set(SERF_LINUX TRUE)
-  set(SERF_ELF_TARGET TRUE)
   set(SERF_PLATFORM "Linux")
 elseif(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
   set(SERF_UNIX TRUE)
   set(SERF_FREEBSD TRUE)
-  set(SERF_ELF_TARGET TRUE)
   set(SERF_PLATFORM "FreeBSD")
 elseif(${CMAKE_SYSTEM_NAME} MATCHES "OpenBSD")
   set(SERF_UNIX TRUE)
   set(SERF_OPENBSD TRUE)
-  set(SERF_ELF_TARGET TRUE)
   set(SERF_PLATFORM "OpenBSD")
 elseif(${CMAKE_SYSTEM_NAME} MATCHES "NetBSD")
   set(SERF_UNIX TRUE)
   set(SERF_NETBSD TRUE)
-  set(SERF_ELF_TARGET TRUE)
   set(SERF_PLATFORM "NetBSD")
-elseif(${CMAKE_SYSTEM_NAME} MATCHES "Windows")
-  set(SERF_WINDOWS TRUE)
-  if(CMAKE_GENERATOR_PLATFORM MATCHES "(x64|ARM64|IA64)")
-    set(SERF_WIN64 TRUE)
-    set(SERF_PLATFORM "Windows (64-bit)")
-  else()
-    set(SERF_WIN32 TRUE)
-    set(SERF_PLATFORM "Windows (32-bit)")
-  endif()
 else()
   set(SERF_UNIX TRUE)
   set(SERF_PLATFORM "generic Unix")
@@ -71,3 +69,34 @@ string(TOLOWER ${CMAKE_SYSTEM_PROCESSOR}
 string(TOLOWER ${CMAKE_SYSTEM_NAME} _system)
 string(TOLOWER ${CMAKE_SYSTEM_VERSION} _version)
 set(SERF_TARGET "${_arch}-${_system}${_version}")
+
+# Check if the target uses the ELF object format. We've explicitly set this to
+# FALSE on Windows and Darwin; on other platforms, we build an executable file
+# and check if it has the ELF magic number in the header.
+if(NOT DEFINED SERF_ELF_TARGET)
+  # Wrap the test into a function to keep the variables scoped.
+  function(_CheckElfTarget var_)
+    set(msg "Check if the target platform uses ELF")
+    set(src "${CMAKE_CURRENT_BINARY_DIR}/check_elf_target.c")
+    set(bin "${CMAKE_CURRENT_BINARY_DIR}/check_elf_target.out")
+    message(STATUS "${msg}")
+    file(WRITE "${src}" "int main(void) { return 0; }")
+    try_compile(success "${CMAKE_CURRENT_BINARY_DIR}"
+                SOURCES "${src}"
+                OUTPUT_VARIABLE log
+                COPY_FILE "${bin}")
+    if(success)
+      file(READ "${bin}" header LIMIT 4 HEX)
+      if("${header}" STREQUAL "7f454c46") # 0x7f E L F
+        message(STATUS "${msg} - yes")
+        set(${var_} TRUE PARENT_SCOPE)
+      else()
+        message(STATUS "${msg} - no")
+      endif()
+    else(success)
+      message(STATUS "${msg} - unknown")
+      message(STATUS "${log}")
+    endif(success)
+  endfunction(_CheckElfTarget)
+  _CheckElfTarget(SERF_ELF_TARGET)
+endif()


Reply via email to