From d6323120c7a535de16c68e9b063c70e5793927ff Mon Sep 17 00:00:00 2001
From: Maxim Reznik <reznikmm@gmail.com>
Date: Tue, 12 Mar 2024 23:59:54 +0200
Subject: [PATCH] Filter out `-static` option on MacOS X

This is to fix gprbuild 24 on Mac OS. The error is

> ld: library not found for -lcrt0.o

The gprbuild call `gcc -static ...` during linking a main, but
MacOS X was never able to do [static linking][1].

The gprbuild reads `-static` option from `*.bexch` file.
The `*.bexch` file is created by `gprbind` by reading
`b__<main>.adb` generated by `gnatbind`.
The `gnatbind` writes `-static` to `b__<main>.adb` for ages,
but gprbuild 22 filtered this option.
The filtering was removed in [the commit][2].

[1]: https://stackoverflow.com/questions/3801011/ld-library-not-found-for-lcrt0-o-on-osx-10-6-with-gcc-clang-static-flag
[2]: https://github.com/AdaCore/gprbuild/commit/48ea3c19d4f5552109642f339d22c58308970c42
---
 specs/gprbuild.anod                    | 13 +++++++++++++
 specs/patches/gprbuild-no-static.patch | 11 +++++++++++
 2 files changed, 24 insertions(+)
 create mode 100644 specs/patches/gprbuild-no-static.patch

diff --git a/specs/gprbuild.anod b/specs/gprbuild.anod
index 1a1432f..7ef6f9a 100644
--- a/specs/gprbuild.anod
+++ b/specs/gprbuild.anod
@@ -3,9 +3,12 @@ from e3.fs import sync_tree, cp
 from e3.anod.helper import Make, text_replace
 from e3.anod.spec import Anod
 from e3.anod.loader import spec
+from e3.diff import patch
 import os
 from datetime import date
 
+patch_name = "gprbuild-no-static.patch"
+
 class GPRbuild(spec("gh-artifact")):
     @property
     def version(self):
@@ -23,6 +26,7 @@ class GPRbuild(spec("gh-artifact")):
                 url="https://github.com/AdaCore/gprbuild/archive/v%s/%s"
                 % (self.version, self.tarball),
             ),
+            self.LocalSourceBuilder(name=patch_name),
         ]
 
     @property
@@ -38,6 +42,7 @@ class GPRbuild(spec("gh-artifact")):
                 Anod.Source(
                     name=self.deps["xmlada"].tarball, publish=True, dest="xmlada"
                 ),
+                Anod.Source(name=patch_name, publish=True, unpack_cmd=cp, dest="patches"),
             ]
         else:
             return [
@@ -47,6 +52,7 @@ class GPRbuild(spec("gh-artifact")):
                     publish=True,
                     dest="gprconfig_kb",
                 ),
+                Anod.Source(name=patch_name, publish=True, unpack_cmd=cp, dest="patches"),
             ]
 
     @property
@@ -135,6 +141,13 @@ class GPRbuild(spec("gh-artifact")):
 
         self.update_version()
 
+        if self.env.host.os.name == "darwin":
+            # MacOS X can't execute 'gcc -static', patching gprbuild
+            patch(
+                os.path.join(self["SRC_DIR"], "patches", patch_name),
+                self.build_space.src_dir,
+            )
+
         if self.bootstrap:
             return self.do_bootstrap()
 
diff --git a/specs/patches/gprbuild-no-static.patch b/specs/patches/gprbuild-no-static.patch
new file mode 100644
index 0000000..bcad8c6
--- /dev/null
+++ b/specs/patches/gprbuild-no-static.patch
@@ -0,0 +1,11 @@
+diff -Naur src/gprbuild-link.adb src/gprbuild-link.adb
+--- src/gprbuild-link.adb	2023-10-09 21:31:11.000000000 +0300
++++ src/gprbuild-link.adb	2024-03-12 23:46:51.766005336 +0200
+@@ -1880,6 +1880,7 @@
+                               when Resulting_Options =>
+                                  if not Apply_Bindfile_Option_Substitution
+                                           (Line (1 .. Last))
++                                    and then Line (1 .. Last) /= "-static"
+                                  then
+                                     Binding_Options.Append (Line (1 .. Last));
+                                  end if;