Subject: bug#57015: 29.0.50; macOS: libgccjit was not found: Homebrew libgccjit 12.1.0

From: Naofumi Yasufuku <[email-masked]>
Date: Sun, 07 Aug 2022 00:13:23 +0900

Hi Eli,

Eli Zaretskii <[email-masked]> writes:
>> From: Naofumi Yasufuku <[email-masked]>
>> Date: Sat, 06 Aug 2022 20:38:58 +0900
>> 
>> './configure --with-native-compilation' fails if Homebrew libgccjit is
>> updated to 12.1.0.  Attached configure.ac patch fixes this issue.
>> I think the same patch should be applied to emacs-28 branch.
>
> I don't mind installing this on master, but I'd rather document that
> Emacs 28 cannot be built on macOS with such a new libgccjit than
> destabilize the release branch, even if it's only for macOS.  E.g.,
> who says that 'readlink' will always be available on macOS?

Thank you for your indication.  readlink '-f' option is not supported
by BSD-based readlink shipped with old macOS.  Revised patch is
attached.  This patch just choose 'current/libgccjit.dylib' from
multiple libgccjit.dylib paths.

--8<---------------cut here---------------start------------->8---
$ brew ls -v libgccjit | grep -E 'libgccjit\.(so|dylib)$'
/usr/local/Cellar/libgccjit/12.1.0/lib/gcc/current/libgccjit.dylib
/usr/local/Cellar/libgccjit/12.1.0/lib/gcc/12/libgccjit.dylib
$
$ ls -lR /usr/local/Cellar/libgccjit/12.1.0/lib/gcc
total 0
drwxr-xr-x  4 naofumi  admin  128 May  6 16:53 12
drwxr-xr-x  4 naofumi  admin  128 May  6 16:53 current

/usr/local/Cellar/libgccjit/12.1.0/lib/gcc/12:
total 0
lrwxr-xr-x  1 naofumi  admin  28 May  6 16:53 libgccjit.0.dylib -> ../current/libgccjit.0.dylib
lrwxr-xr-x  1 naofumi  admin  26 May  6 16:53 libgccjit.dylib -> ../current/libgccjit.dylib

/usr/local/Cellar/libgccjit/12.1.0/lib/gcc/current:
total 82304
-rw-r--r--  1 naofumi  admin  42138352 Aug  6 18:06 libgccjit.0.dylib
lrwxr-xr-x  1 naofumi  admin        17 May  6 16:53 libgccjit.dylib -> libgccjit.0.dylib
$
--8<---------------cut here---------------end--------------->8---

>From 3757f2e69c8aeb392377a83b7b1d06fc401cc8ff Mon Sep 17 00:00:00 2001
From: Naofumi Yasufuku <[email-masked]>
Date: Sat, 6 Aug 2022 22:10:36 +0900
Subject: [PATCH] Fix libgccjit library path lookup for Homebrew libgccjit
 12.1.0 and above

* configure.ac: Choose the first match from multiple libgccjit.dylib
paths in 'brew ls -v libgccjit' output.  Prefer 'current/libgccjit.dylib'
over '${ver}/libgccjit.dylib'  (Bug#57015)
---
 configure.ac | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/configure.ac b/configure.ac
index 1a264275bd..55dcb93f9a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -4240,8 +4240,15 @@ AC_DEFUN
         if test -n "`$BREW --prefix --installed libgccjit 2>/dev/null`"; then
           MAC_CFLAGS="-I$(dirname $($BREW ls -v libgccjit | \
                                                 grep libgccjit.h))"
-          MAC_LIBS="-L$(dirname $($BREW ls -v libgccjit| \
-                                            grep -E 'libgccjit\.(so|dylib)$'))"
+          MAC_LIBS="-L$(dirname $($BREW ls -v libgccjit | \
+                                  grep -E 'current/libgccjit\.(so|dylib)$' | \
+                                  head -1))"
+          if test "${MAC_LIBS}" = "-L"; then
+            # Try for Homebrew libgccjit < 12.1.0.
+            MAC_LIBS="-L$(dirname $($BREW ls -v libgccjit | \
+                                    grep -E 'libgccjit\.(so|dylib)$' | \
+                                    head -1))"
+          fi
         fi
       fi
 
-- 
2.37.1

>
> Is it really a problem to use only libgccjit 12.0 and older for the
> release branch?
>

No.  This libgccjit.dylib path problem is specific for the situation of
Homebrew libgccjit 12.1.0 and above.  I found that this problem is
caused by the following Homebrew change:

https://github.com/Homebrew/homebrew-core/commit/eb60b7d88a5bc4ffdff8859e88c4c11f5f403167#diff-14dead016a29e43c6e17b0e951d2a23e62156cf732e7d6d2cf603934397e8670R52

--8<---------------cut here---------------start------------->8---
eb60b7d88a5bc4ffdff8859e88c4c11f5f403167
Author:     Francois-Xavier Coudert <[email-masked]>
AuthorDate: Fri Apr 29 23:47:04 2022 +0200
Commit:     BrewTestBot <[email-masked]>
CommitDate: Fri Aug 5 14:27:51 2022 +0000

Parent:     39c1b9e gcc 12.1.0
Contained:  master

libgccjit 12.1.0

- align with GCC lib dir
--8<---------------cut here---------------end--------------->8---

--8<---------------cut here---------------start------------->8---
+    # Use `lib/gcc/current` to align with the GCC formula.
     args = %W[
       --prefix=#{prefix}
-      --libdir=#{lib}/gcc/#{version.major}
+      --libdir=#{lib}/gcc/current
--8<---------------cut here---------------end--------------->8---

But, unfortunately, macOS Homebrew users have no way to keep installing
libgccjit 11.x at the moment.  Yes, its just a matter of maintaining 
Homebrew libgccjit package... :-< 

gcc packages are maintaned as gcc@VERSION, but libgccjit is not:

--8<---------------cut here---------------start------------->8---
$ brew search gcc
==> Formulae
aarch64-elf-gcc     gcc@5               i686-elf-gcc        scc
gcc ✔               gcc@6               libgccjit ✔         tcc
gcc@10              gcc@7               x86_64-elf-gcc      ncc
gcc@11              gcc@8               grc
[email-masked]             gcc@9               ghc ✔

==> Casks
gcc-aarch64-embedded                     gcs
gcc-arm-embedded                         icc
$
--8<---------------cut here---------------start------------->8---

--8<---------------cut here---------------start------------->8---
$ ./emacs/28/bin/emacs-28.1.91
dyld[40808]: Library not loaded: '/usr/local/opt/libgccjit/lib/gcc/11/libgccjit.0.dylib'
  Referenced from: '/Users/naofumi/.local/emacs/28/bin/emacs-28.1.91'
  Reason: tried: '/usr/local/opt/libgccjit/lib/gcc/11/libgccjit.0.dylib' (no such file), '/usr/local/lib/libgccjit.0.dylib' (no such file), '/usr/lib/libgccjit.0.dylib' (no such file)
zsh: abort      ./emacs/28/bin/emacs-28.1.91
$
$ otool -L ./emacs/28/bin/emacs-28.1.91
emacs/28/bin/emacs-28.1.91:
        /System/Library/Frameworks/AppKit.framework/Versions/C/AppKit (compatibility version 45.0.0, current version 2113.40.126)
        /System/Library/Frameworks/IOKit.framework/Versions/A/IOKit (compatibility version 1.0.0, current version 275.0.0)
        /System/Library/Frameworks/Carbon.framework/Versions/A/Carbon (compatibility version 2.0.0, current version 165.0.0)
        /System/Library/Frameworks/IOSurface.framework/Versions/A/IOSurface (compatibility version 1.0.0, current version 1.0.0)
        /System/Library/Frameworks/QuartzCore.framework/Versions/A/QuartzCore (compatibility version 1.2.0, current version 1.11.0)
        /usr/local/opt/libtiff/lib/libtiff.5.dylib (compatibility version 14.0.0, current version 14.0.0)
        /usr/local/opt/jpeg/lib/libjpeg.9.dylib (compatibility version 15.0.0, current version 15.0.0)
        /usr/local/opt/libpng/lib/libpng16.16.dylib (compatibility version 54.0.0, current version 54.0.0)
        /usr/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.11)
        /usr/local/opt/giflib/lib/libgif.dylib (compatibility version 0.0.0, current version 7.2.0)
        /usr/local/opt/librsvg/lib/librsvg-2.2.dylib (compatibility version 51.0.0, current version 51.0.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1311.100.3)
        /usr/local/opt/glib/lib/libgio-2.0.0.dylib (compatibility version 7201.0.0, current version 7201.3.0)
        /usr/local/opt/gdk-pixbuf/lib/libgdk_pixbuf-2.0.0.dylib (compatibility version 4201.0.0, current version 4201.8.0)
        /usr/local/opt/glib/lib/libgobject-2.0.0.dylib (compatibility version 7201.0.0, current version 7201.3.0)
        /usr/local/opt/glib/lib/libglib-2.0.0.dylib (compatibility version 7201.0.0, current version 7201.3.0)
        /usr/local/opt/gettext/lib/libintl.8.dylib (compatibility version 11.0.0, current version 11.0.0)
        /usr/local/opt/cairo/lib/libcairo.2.dylib (compatibility version 11603.0.0, current version 11603.0.0)
        /System/Library/Frameworks/WebKit.framework/Versions/A/WebKit (compatibility version 1.0.0, current version 613.1.17)
        /usr/lib/libxml2.2.dylib (compatibility version 10.0.0, current version 10.9.0)
        /usr/lib/libncurses.5.4.dylib (compatibility version 5.4.0, current version 5.4.0)
        /usr/local/opt/gnutls/lib/libgnutls.30.dylib (compatibility version 64.0.0, current version 64.1.0)
        /usr/local/opt/little-cms2/lib/liblcms2.2.dylib (compatibility version 3.0.0, current version 3.13.0)
        /usr/local/opt/jansson/lib/libjansson.4.dylib (compatibility version 19.0.0, current version 19.0.0)
        /usr/local/opt/gmp/lib/libgmp.10.dylib (compatibility version 15.0.0, current version 15.1.0)
        /usr/local/opt/libgccjit/lib/gcc/11/libgccjit.0.dylib (compatibility version 0.0.0, current version 0.0.1)
        /System/Library/Frameworks/CFNetwork.framework/Versions/A/CFNetwork (compatibility version 1.0.0, current version 1331.0.7)
        /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1858.112.0)
        /System/Library/Frameworks/CoreGraphics.framework/Versions/A/CoreGraphics (compatibility version 64.0.0, current version 1557.5.4)
        /System/Library/Frameworks/CoreServices.framework/Versions/A/CoreServices (compatibility version 1.0.0, current version 1141.1.0)
        /System/Library/Frameworks/CoreText.framework/Versions/A/CoreText (compatibility version 1.0.0, current version 1.0.0)
        /System/Library/Frameworks/Foundation.framework/Versions/C/Foundation (compatibility version 300.0.0, current version 1858.112.0)
        /usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)
$
--8<---------------cut here---------------end--------------->8---

Regards,
  Naofumi