Search code examples
macossdladagnat

Cannot build SDL Ada Bindings on OSX Sonoma because of error: missing binary operator before token "("


I am trying to write a small game using ADA (via Alire and gnat_native toolchain) and SDL2 on my OSX Sonoma 14.4.1.

For Ada I am using Alire and from what I can see I have latest gnat native version (according to https://alire.ada.dev/crates/gnat_native):

> alr --version
alr 2.0.1

> alr toolchain
CRATE VERSION STATUS NOTES
gprbuild 22.0.1 Default
gnat_native 13.2.2 Default

I added the SDL bindings to my project with the following command alr with sdlada but when I try building it with alr build on my Mac I get the following error:

> alr build
ⓘ Running pre-build actions for sdlada=2.5.20...                               
mkdir -p gen/src/
ⓘ Building tictactoe=0.1.0-dev/tictactoe.gpr...
Compile
   [C]            version_ttf.c
   [C]            version_images.c
   [C]            version.c
In file included from /usr/local/include/SDL2/SDL_platform.h:76,
                 from /usr/local/include/SDL2/SDL_config.h:33,
                 from /usr/local/include/SDL2/SDL_stdinc.h:31,
                 from /usr/local/include/SDL2/SDL_main.h:25,
                 from /usr/local/include/SDL2/SDL.h:32,
                 from /Users/stefano/.local/share/alire/builds/sdlada_2.5.20_cd53c280/42d3242441b923cdce0d399fdf8d3b6b1a75d0874241c5467499cc92113894c2/src/version.c:4:
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/TargetConditionals.h:140:50: error: missing binary operator before token "("
  140 | #if !defined(__has_extension) || !__has_extension(define_target_os_macros)
      |                                                  ^
In file included from /usr/local/include/SDL2/SDL_platform.h:76,
                 from /usr/local/include/SDL2/SDL_config.h:33,
                 from /usr/local/include/SDL2/SDL_stdinc.h:31,
                 from /usr/local/include/SDL2/SDL_main.h:25,
                 from /usr/local/include/SDL2/SDL.h:32,
                 from /usr/local/include/SDL2/SDL_image.h:32,
                 from /Users/stefano/.local/share/alire/builds/sdlada_2.5.20_cd53c280/42d3242441b923cdce0d399fdf8d3b6b1a75d0874241c5467499cc92113894c2/src/image/version_images.c:4:
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/TargetConditionals.h:140:50: error: missing binary operator before token "("
  140 | #if !defined(__has_extension) || !__has_extension(define_target_os_macros)
      |                                                  ^
In file included from /usr/local/include/SDL2/SDL_platform.h:76,
                 from /usr/local/include/SDL2/SDL_config.h:33,
                 from /usr/local/include/SDL2/SDL_stdinc.h:31,
                 from /usr/local/include/SDL2/SDL_main.h:25,
                 from /usr/local/include/SDL2/SDL.h:32,
                 from /usr/local/include/SDL2/SDL_ttf.h:39,
                 from /Users/stefano/.local/share/alire/builds/sdlada_2.5.20_cd53c280/42d3242441b923cdce0d399fdf8d3b6b1a75d0874241c5467499cc92113894c2/src/ttf/version_ttf.c:4:
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/TargetConditionals.h:140:50: error: missing binary operator before token "("
  140 | #if !defined(__has_extension) || !__has_extension(define_target_os_macros)
      |                                                  ^

   compilation of version.c failed
   compilation of version_images.c failed
   compilation of version_ttf.c failed

gprbuild: *** compilation phase failed

Which seems to be an error raised in the OSX SDK libraries themselves.

I think this SDL installation works because I have been using it a different project written in C++ (C++20) and compiled via CMake/Clang.

Taking inspiration from this post https://github.com/orgs/Homebrew/discussions/5195 it seems that the issue is because the pre-processor that is being used (which I am not sure how to find out which one is it) does not support __has_extension which is a C++17 feature.

Is there any way that I can instruct Alire/toolchain/gnat_native to use a different C++ compiler supporting this? Or force the compiler used by alr build to use the C++17 standard? Or is there a way to use a different tool-chain that can support this (possibly via Alire)? Or a way to just let the SDL ADA bindings to the static or dynamic compiled I already have installed on my machine (via home-brew)?


Solution

  • That was my question on the Homebrew site! I thought it looked familiar!

    The following file locations are Alire 2.0-specific, earlier Alire versions are organised differently.

    Under ~.local/share/alire, you’ll find 3 directories:

    • toolchains contains .. toolchain releases.
    • releases contains downloaded releases of crates.
    • builds contains cached builds of crates using specific tools.

    Now you have an (unsuccessful) build, the last will have been populated, so to proceed from there,

    • navigate to the latest sdlada; I have sdlada_2.5.20_cd53c280.
    • Inside this there’s at least one long hash, corresponding to the tool used. I only have one; cd into it (if there’s more than one, you could use the latest, I suppose).
    • edit build/gnat/sdlada.gpr like this:
    @@ -36,6 +36,7 @@ library project SDLAda is
        for Library_Kind use "static";
     
        package Compiler is
    +      for Driver ("C") use "clang";
           Common_Switches := ("-ffunction-sections", "-fdata-sections");
           C_Switches      := ();
           Ada_Switches    := ("-gnat2012", "-gnata", "-gnato", "-gnatwa",
    

    This is very much a workround! The proper resolution needs to be done at source. Would you like to post an issue on sdlada’s Github repo, or shall I do it?