SDL icon indicating copy to clipboard operation
SDL copied to clipboard

Latest macOS system header causes compilation failures

Open Traace opened this issue 1 year ago • 2 comments

Issue Description: Im unable to compile projects that uses SDL2 on latest MacOS. The current system header TargetConditionals.h seems to be incompatible with SDL

How to reproduce:

  1. Install MacOS 14.4.1
  2. Install SDL2 2.30.2 (for example via Homebrew)
  3. Create a project that uses SDL2
  4. Try to compile the project
  5. Get error:
In file included from /opt/homebrew/include/SDL2/SDL_platform.h:76,
                 from /opt/homebrew/include/SDL2/SDL_config.h:33,
                 from /opt/homebrew/include/SDL2/SDL_stdinc.h:31,
                 from /opt/homebrew/include/SDL2/SDL_main.h:25,
                 from /opt/homebrew/include/SDL2/SDL.h:32,
                 from src/pc/audio/audio_sdl2.c:3:
/Library/Developer/CommandLineTools/SDKs/MacOSX14.sdk/usr/include/TargetConditionals.h:140:50: error: missing binary operator before token "("
  140 | #if !defined(__has_extension) || !__has_extension(define_target_os_macros)
      |                                                  ^

What I expect: Use SDL in projects without modifying header files first.

Workaround: Change the following code in SDL_platform.h Before (with failure):

#if defined(__APPLE__)
/* lets us know what version of Mac OS X we're compiling on */
#include <AvailabilityMacros.h>
#include <TargetConditionals.h>

After (project builds fine):

#if defined(__APPLE__)
/* lets us know what version of Mac OS X we're compiling on */
#include <AvailabilityMacros.h>
#  ifndef __has_extension
#    define __has_extension(x) 0
#  endif
#include <TargetConditionals.h>

Traace avatar Apr 26 '24 08:04 Traace

Which compiler are you using that doesn't define __has_extension on macOS? I guess it's GCC?

slime73 avatar Apr 26 '24 11:04 slime73

Which compiler are you using that doesn't define __has_extension on macOS? I guess it's GCC?

It may be a clang version e.g. on linux being used as a cross-compiler.

#  ifndef __has_extension
#    define __has_extension(x) 0
#  endif

https://clang.llvm.org/docs/LanguageExtensions.html suggests __has_feature as its replacement, so something like this maybe ??

diff --git a/include/SDL3/SDL_platform_defines.h b/include/SDL3/SDL_platform_defines.h
index de0908f..e582efd 100644
--- a/include/SDL3/SDL_platform_defines.h
+++ b/include/SDL3/SDL_platform_defines.h
@@ -65,6 +65,14 @@
 #define SDL_PLATFORM_APPLE  1
 /* lets us know what version of macOS we're compiling on */
 #include <AvailabilityMacros.h>
+/* macOS 14 SDK has a problematic use of __has_extension in TargetConditionals.h: */
+#ifndef __has_extension
+# ifdef __has_feature
+#  define __has_extension __has_feature
+# else
+#  define __has_extension(x) 0
+# endif
+#endif
 #include <TargetConditionals.h>
 
 /* Fix building with older SDKs that don't define these

sezero avatar Apr 26 '24 11:04 sezero

Let's make sure we undef that after the include, if we defined it, but otherwise I say we try this approach.

icculus avatar May 18 '24 17:05 icculus

Let's make sure we undef that after the include, if we defined it, but otherwise I say we try this approach.

https://github.com/libsdl-org/SDL/pull/9832

sezero avatar May 18 '24 17:05 sezero