Commit 5a0ea7fb17b46df6e6fc1074c0f96bfa18851a7a

Ozkan Sezer 2021-11-27T19:10:10

Check for O_CLOEXEC definition in fcntl.h at configuration time, If not available, define it internally as 0 (in SDL_internal.h). Define it as available for macOS >= 10.7 in SDL_config_macosx.h.

diff --git a/CMakeLists.txt b/CMakeLists.txt
index d0bc157..9411c76 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -993,6 +993,10 @@ if(UNIX OR APPLE)
   endif()
 endif()
 
+if(UNIX OR APPLE OR HAIKU OR RISCOS)
+  CheckO_CLOEXEC()
+endif()
+
 if(SDL_JOYSTICK)
   if(SDL_VIRTUAL_JOYSTICK)
     set(HAVE_VIRTUAL_JOYSTICK TRUE)
diff --git a/cmake/sdlchecks.cmake b/cmake/sdlchecks.cmake
index 4b1a6e0..32be19c 100644
--- a/cmake/sdlchecks.cmake
+++ b/cmake/sdlchecks.cmake
@@ -53,6 +53,14 @@ macro(CheckDLOPEN)
   endif()
 endmacro()
 
+macro(CheckO_CLOEXEC)
+  check_c_source_compiles("
+    #include <fcntl.h>
+    int flag = O_CLOEXEC;
+    int main(void) {
+   }" HAVE_O_CLOEXEC)
+endmacro()
+
 # Requires:
 # - n/a
 macro(CheckOSS)
diff --git a/configure b/configure
index 4f61328..cbd5782 100755
--- a/configure
+++ b/configure
@@ -20233,8 +20233,7 @@ int
 main ()
 {
 
-	  EGL_DISPMANX_WINDOW_T window;
-
+          EGL_DISPMANX_WINDOW_T window;
           bcm_host_init();
 
   ;
@@ -24362,6 +24361,39 @@ $as_echo "#define SDL_LOADSO_DLOPEN 1" >>confdefs.h
     fi
 }
 
+CheckO_CLOEXEC()
+{
+    { $as_echo "$as_me:${as_lineno-$LINENO}: checking for O_CLOEXEC" >&5
+$as_echo_n "checking for O_CLOEXEC... " >&6; }
+    cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+
+#include <fcntl.h>
+int flag = O_CLOEXEC;
+
+int
+main ()
+{
+
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"; then :
+  have_o_cloexec=yes
+else
+  have_o_cloexec=no
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+    { $as_echo "$as_me:${as_lineno-$LINENO}: result: $have_o_cloexec" >&5
+$as_echo "$have_o_cloexec" >&6; }
+    if test $have_o_cloexec = yes; then
+
+$as_echo "#define HAVE_O_CLOEXEC 1" >>confdefs.h
+
+    fi
+}
+
 CheckUSBHID()
 {
     case "$host" in
@@ -25055,6 +25087,7 @@ $as_echo "#define SDL_VIDEO_DRIVER_ANDROID 1" >>confdefs.h
         CheckDLOPEN
         CheckARM
         CheckNEON
+        CheckO_CLOEXEC
         CheckOSS
         CheckALSA
         CheckPipewire
@@ -25321,10 +25354,8 @@ $as_echo "#define SDL_TIMER_UNIX 1" >>confdefs.h
 
         # Set up the core platform files
         SOURCES="$SOURCES $srcdir/src/core/windows/*.c"
-
         SOURCES="$SOURCES $srcdir/src/misc/windows/*.c"
         have_misc=yes
-
         # Use the Windows locale APIs.
         SOURCES="$SOURCES $srcdir/src/locale/windows/*.c"
         have_locale=yes
@@ -25556,6 +25587,7 @@ fi
         CheckDiskAudio
         CheckDummyAudio
         CheckDLOPEN
+        CheckO_CLOEXEC
         CheckHaikuVideo
         CheckHaikuGL
         CheckPTHREAD
@@ -25623,6 +25655,7 @@ $as_echo "#define SDL_FILESYSTEM_HAIKU 1" >>confdefs.h
         CheckDiskAudio
         CheckDummyAudio
         CheckDLOPEN
+        CheckO_CLOEXEC
         CheckMETAL
         CheckVulkan
         CheckPTHREAD
@@ -25749,6 +25782,7 @@ $as_echo "#define SDL_VIDEO_RENDER_OGL_ES2 1" >>confdefs.h
         CheckDiskAudio
         CheckDummyAudio
         CheckDLOPEN
+        CheckO_CLOEXEC
         CheckCOCOA
         CheckMETAL
         CheckX11
@@ -25845,6 +25879,7 @@ $as_echo "#define SDL_TIMER_UNIX 1" >>confdefs.h
         CheckOffscreenVideo
         CheckInputEvents
         CheckPTHREAD
+        CheckO_CLOEXEC
 
         # Set up files for the timer library
         if test x$enable_timers = xyes; then
@@ -25940,6 +25975,7 @@ $as_echo "#define SDL_TIMER_UNIX 1" >>confdefs.h
         CheckDiskAudio
         CheckDummyAudio
         CheckDLOPEN
+        CheckO_CLOEXEC
         CheckOSS
         CheckPTHREAD
         CheckClockGettime
diff --git a/configure.ac b/configure.ac
index 2cc063a..625681a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1701,8 +1701,7 @@ CheckRPI()
           #include <bcm_host.h>
           #include <EGL/eglplatform.h>
         ]], [[
-	  EGL_DISPMANX_WINDOW_T window;
-
+          EGL_DISPMANX_WINDOW_T window;
           bcm_host_init();
         ]])], [have_video_rpi=yes],[])
         AC_MSG_RESULT($have_video_rpi)
@@ -3296,6 +3295,19 @@ CheckDLOPEN()
     fi
 }
 
+CheckO_CLOEXEC()
+{
+    AC_MSG_CHECKING(for O_CLOEXEC)
+    AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
+#include <fcntl.h>
+int flag = O_CLOEXEC;
+        ]],[])], [have_o_cloexec=yes],[have_o_cloexec=no])
+    AC_MSG_RESULT($have_o_cloexec)
+    if test $have_o_cloexec = yes; then
+       AC_DEFINE(HAVE_O_CLOEXEC, 1, [ ])
+    fi
+}
+
 dnl Check for the usbhid(3) library on *BSD
 CheckUSBHID()
 {
@@ -3630,6 +3642,7 @@ case "$host" in
         CheckDLOPEN
         CheckARM
         CheckNEON
+        CheckO_CLOEXEC
         CheckOSS
         CheckALSA
         CheckPipewire
@@ -3862,10 +3875,8 @@ case "$host" in
 
         # Set up the core platform files
         SOURCES="$SOURCES $srcdir/src/core/windows/*.c"
-
         SOURCES="$SOURCES $srcdir/src/misc/windows/*.c"
         have_misc=yes
-
         # Use the Windows locale APIs.
         SOURCES="$SOURCES $srcdir/src/locale/windows/*.c"
         have_locale=yes
@@ -4014,6 +4025,7 @@ case "$host" in
         CheckDiskAudio
         CheckDummyAudio
         CheckDLOPEN
+        CheckO_CLOEXEC
         CheckHaikuVideo
         CheckHaikuGL
         CheckPTHREAD
@@ -4071,6 +4083,7 @@ case "$host" in
         CheckDiskAudio
         CheckDummyAudio
         CheckDLOPEN
+        CheckO_CLOEXEC
         CheckMETAL
         CheckVulkan
         CheckPTHREAD
@@ -4175,6 +4188,7 @@ case "$host" in
         CheckDiskAudio
         CheckDummyAudio
         CheckDLOPEN
+        CheckO_CLOEXEC
         CheckCOCOA
         CheckMETAL
         CheckX11
@@ -4259,6 +4273,7 @@ case "$host" in
         CheckOffscreenVideo
         CheckInputEvents
         CheckPTHREAD
+        CheckO_CLOEXEC
 
         # Set up files for the timer library
         if test x$enable_timers = xyes; then
@@ -4338,6 +4353,7 @@ case "$host" in
         CheckDiskAudio
         CheckDummyAudio
         CheckDLOPEN
+        CheckO_CLOEXEC
         CheckOSS
         CheckPTHREAD
         CheckClockGettime
diff --git a/include/SDL_config.h.cmake b/include/SDL_config.h.cmake
index 46b404e..3865581 100644
--- a/include/SDL_config.h.cmake
+++ b/include/SDL_config.h.cmake
@@ -222,6 +222,7 @@
 #cmakedefine HAVE_INOTIFY_INIT 1
 #cmakedefine HAVE_INOTIFY_INIT1 1
 #cmakedefine HAVE_INOTIFY 1
+#cmakedefine HAVE_O_CLOEXEC 1
 
 /* Apple platforms might be building universal binaries, where Intel builds
    can use immintrin.h but other architectures can't. */
diff --git a/include/SDL_config.h.in b/include/SDL_config.h.in
index ddab098..9073cea 100644
--- a/include/SDL_config.h.in
+++ b/include/SDL_config.h.in
@@ -215,6 +215,7 @@
 #define HAVE_STDINT_H 1
 #endif /* HAVE_LIBC */
 
+#undef HAVE_O_CLOEXEC
 #undef HAVE_ALTIVEC_H
 #undef HAVE_DBUS_DBUS_H
 #undef HAVE_FCITX
@@ -234,14 +235,13 @@
 #undef HAVE_DXGI_H
 #undef HAVE_WINDOWS_GAMING_INPUT_H
 #undef HAVE_XINPUT_H
+#undef HAVE_XINPUT_GAMEPAD_EX
+#undef HAVE_XINPUT_STATE_EX
 
 #undef HAVE_MMDEVICEAPI_H
 #undef HAVE_AUDIOCLIENT_H
 #undef HAVE_SENSORSAPI_H
 
-#undef HAVE_XINPUT_GAMEPAD_EX
-#undef HAVE_XINPUT_STATE_EX
-
 /* SDL internal assertion support */
 #undef SDL_DEFAULT_ASSERT_LEVEL
 
diff --git a/include/SDL_config_macosx.h b/include/SDL_config_macosx.h
index 40c7206..2a59959 100644
--- a/include/SDL_config_macosx.h
+++ b/include/SDL_config_macosx.h
@@ -144,6 +144,10 @@
 # endif
 #endif
 
+#if (MAC_OS_X_VERSION_MAX_ALLOWED >= 1070)
+#define HAVE_O_CLOEXEC 1
+#endif
+
 #define HAVE_GCC_ATOMICS 1
 
 /* Enable various audio drivers */
diff --git a/src/SDL_internal.h b/src/SDL_internal.h
index 660c247..c06a477 100644
--- a/src/SDL_internal.h
+++ b/src/SDL_internal.h
@@ -51,6 +51,10 @@
 
 #include "SDL_config.h"
 
+#ifndef HAVE_O_CLOEXEC
+#define O_CLOEXEC                       0
+#endif
+
 /* A few #defines to reduce SDL2 footprint.
    Only effective when library is statically linked.
    You have to manually edit this file. */