jimeh / build-emacs-for-macos

Somewhat hacky script to automate building of Emac.app on macOS.
484 stars 50 forks source link

M1 support #47

Open shaunsingh opened 3 years ago

shaunsingh commented 3 years ago

Hi, as libgccjit is now available for m1 MacBooks, it should be possible to compile emacs master for apple silicon. The emacs-plus@28 --native-comp works fine, but it was a bit buggy,

trying to build using the following command ./build-emacs-for-macos --git-sha 3f8b303ec087ba3d0bd524c7fc8c628ef15a3e4a master results in this output:

build-emacs-for-macos on  master via  v2.6.3
❯ ./build-emacs-for-macos --git-sha 3f8b303ec087ba3d0bd524c7fc8c628ef15a3e4a master

==> INFO: Fetching info for git ref: 3f8b303ec087ba3d0bd524c7fc8c628ef15a3e4a
==> INFO: Downloading tarball from GitHub. This could take a while, please be patient.
==> CMD: curl -L https://github.com/emacs-mirror/emacs/tarball/3f8b303ec087ba3d0bd524c7fc8c628ef15a3e4a -o /Users/shauryasingh/IdeaProjects/build-emacs-for-macos/tarballs/emacs-mirror-emacs-3f8b303.tgz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   167  100   167    0     0    893      0 --:--:-- --:--:-- --:--:--   893
100 40.8M    0 40.8M    0     0  4090k      0 --:--:--  0:00:10 --:--:-- 4492k
==> INFO: Extracting tarball...
==> CMD: tar -xzf /Users/shauryasingh/IdeaProjects/build-emacs-for-macos/tarballs/emacs-mirror-emacs-3f8b303.tgz -C /Users/shauryasingh/IdeaProjects/build-emacs-for-macos/sources
==> CMD: mkdir -p /Users/shauryasingh/IdeaProjects/build-emacs-for-macos/sources/emacs-mirror-emacs-3f8b303/macos_patches
==> INFO: Downloading patch: https://github.com/d12frosted/homebrew-emacs-plus/raw/master/patches/emacs-28/fix-window-role.patch
==> CMD: curl -L# https://github.com/d12frosted/homebrew-emacs-plus/raw/master/patches/emacs-28/fix-window-role.patch -o /Users/shauryasingh/IdeaProjects/build-emacs-for-macos/sources/emacs-mirror-emacs-3f8b303/macos_patches/patch-001.diff
############################################################################################################################################################################################## 100.0%
==> INFO: Applying patch...
==> CMD: patch -f -p1 -i /Users/shauryasingh/IdeaProjects/build-emacs-for-macos/sources/emacs-mirror-emacs-3f8b303/macos_patches/patch-001.diff
patching file src/nsterm.m
Hunk #1 succeeded at 8881 (offset 113 lines).
==> CMD: mkdir -p /Users/shauryasingh/IdeaProjects/build-emacs-for-macos/sources/emacs-mirror-emacs-3f8b303/macos_patches
==> INFO: Downloading patch: https://github.com/d12frosted/homebrew-emacs-plus/raw/master/patches/emacs-28/system-appearance.patch
==> CMD: curl -L# https://github.com/d12frosted/homebrew-emacs-plus/raw/master/patches/emacs-28/system-appearance.patch -o /Users/shauryasingh/IdeaProjects/build-emacs-for-macos/sources/emacs-mirror-emacs-3f8b303/macos_patches/patch-002.diff
############################################################################################################################################################################################## 100.0%
==> INFO: Applying patch...
==> CMD: patch -f -p1 -i /Users/shauryasingh/IdeaProjects/build-emacs-for-macos/sources/emacs-mirror-emacs-3f8b303/macos_patches/patch-002.diff
patching file src/frame.h
patching file src/nsfns.m
Hunk #1 succeeded at 1260 (offset 7 lines).
patching file src/nsterm.m
Hunk #1 succeeded at 2173 (offset -21 lines).
Hunk #2 succeeded at 5669 (offset -82 lines).
Hunk #3 succeeded at 5915 (offset -82 lines).
Hunk #4 succeeded at 5965 (offset -82 lines).
Hunk #5 succeeded at 6191 (offset -82 lines).
Hunk #6 succeeded at 9212 (offset 80 lines).
Hunk #7 succeeded at 10298 (offset 284 lines).
==> CMD: ./autogen.sh
Checking whether you have the necessary tools...
(Read INSTALL.REPO for more details on building Emacs)
Checking for autoconf (need at least version 2.65) ... ok
Your system has the required tools.
Building aclocal.m4 ...
Running 'autoreconf -fi -I m4' ...
You can now run './configure'.
==> INFO: Detecting native-comp support: Supported
==> INFO: Compiling from source. This will take a while...
==> INFO: Compiling with native-comp enabled
==> INFO: Applying patch...
==> CMD: patch -f -p1 -i /Users/shauryasingh/IdeaProjects/build-emacs-for-macos/sources/emacs-mirror-emacs-3f8b303/macos_patches/native-comp-env-setup.diff
patching file lisp/emacs-lisp/comp.el
==> CMD: ./configure --with-ns --with-modules --enable-locallisppath=/Library/Application Support/Emacs/${version}/site-lisp:/Library/Application Support/Emacs/site-lisp --with-xwidgets --with-native-compilation
checking for xcrun... xcrun
checking for make... yes
checking for GNU Make... make
checking build system type... aarch64-apple-darwin20.4.0
checking host system type... aarch64-apple-darwin20.4.0
checking whether the C compiler works... no
configure: error: in `/Users/shauryasingh/IdeaProjects/build-emacs-for-macos/sources/emacs-mirror-emacs-3f8b303':
configure: error: C compiler cannot create executables
See `config.log' for more details
ERROR: Exit code: 77

Perhaps a patch is needed?

jimeh commented 3 years ago

This error actually looks like you're missing Xcode or the Xcode command line tools.

Try this:

xcode-select --install

As for building Emacs on M1-based machines, it should in theory work fine as people have reported the emacs-plus homebrew formula working with native-comp and all.

That said I've not seen any reports of successful builds with my build script here, and I don't have a M1 Mac myself yet. So I'd be curious to hear if you can get it working 🙂

shaunsingh commented 3 years ago

Ah, I have the Xcode command line tools installed but didn't realize I need the full Xcode installation as well. Ill get it installed and get back to you.

I've been using the emacs-plus formula so far, but I just wanted to try the official version for fun

shaunsingh commented 3 years ago

Sadly got the same error after installing full Xcode as well:

build-emacs-for-macos on  master [?] via  v2.6.3 took 12s
❯ ./build-emacs-for-macos -j8 --native-comp --no-titlebar
==> INFO: Fetching info for git ref: master
==> INFO: emacs-mirror-emacs-1855e3d.tgz already exists locally, attempting to use.
==> INFO: emacs-mirror-emacs-1855e3d source tree exists, attempting to use.
==> CMD: ./autogen.sh
Checking whether you have the necessary tools...
(Read INSTALL.REPO for more details on building Emacs)
Checking for autoconf (need at least version 2.65) ... ok
Your system has the required tools.
Running 'autoreconf -fi -I m4' ...
You can now run './configure'.
==> INFO: Compiling from source. This will take a while...
==> INFO: Compiling with native-comp enabled
==> CMD: ./configure --with-ns --with-modules --enable-locallisppath=/Library/Application Support/Emacs/${version}/site-lisp:/Library/Application Support/Emacs/site-lisp --with-xwidgets --with-native-compilation
checking for xcrun... xcrun
checking for make... yes
checking for GNU Make... make
checking build system type... aarch64-apple-darwin20.4.0
checking host system type... aarch64-apple-darwin20.4.0
checking whether the C compiler works... no
configure: error: in `/Users/shauryasingh/IdeaProjects/build-emacs-for-macos/sources/emacs-mirror-emacs-1855e3d':
configure: error: C compiler cannot create executables
See `config.log' for more details
ERROR: Exit code: 77
jimeh commented 3 years ago

Hmm, no full Xcode shouldn't be needed. My original suspicion was that it couldn't locate clang for some reason.

After a bit more digging, I have a feeling the issue might be the -march=native CFLAG. I have been meaning to make that flag optional for a while, but haven't had a chance to test it yet. And some quick digging just now seems to indicate using that flag on M1 causes issues.

Could you try commenting out the -march=native flag from within build-emacs-for-macos and seeing if that fixes the issue?

shaunsingh commented 3 years ago

just started the build, looks like its working so far!

shaunsingh commented 3 years ago

looks like it builds fine but I get an error while opening:

Process:               Emacs [70494]
Path:                  /Users/USER/*/Emacs.app/Contents/MacOS/Emacs
Identifier:            org.gnu.Emacs
Version:               ???
Code Type:             ARM-64 (Native)
Parent Process:        ??? [1]
Responsible:           Emacs [70494]
User ID:               501

Date/Time:             2021-05-30 12:32:20.016 -0400
OS Version:            macOS 11.3.1 (20E241)
Report Version:        12
Anonymous UUID:        2F65C0AB-23F0-A096-AB97-66F55868F8F5

Sleep/Wake UUID:       EDA3124E-5E50-4549-9647-C23E3E5CF125

Time Awake Since Boot: 660000 seconds
Time Since Wake:       260 seconds

System Integrity Protection: enabled

Crashed Thread:        Unknown

Exception Type:        EXC_BAD_ACCESS (Code Signature Invalid)
Exception Codes:       0x0000000000000032, 0x0000000104cd8000
Exception Note:        EXC_CORPSE_NOTIFY

Termination Reason:    Namespace CODESIGNING, Code 0x2

kernel messages:

VM Regions Near 0x104cd8000:
--> mapped file                 104cd8000-104f2c000    [ 2384K] r-x/r-x SM=COW  Object_id=97d15133
    mapped file                 104f2c000-1052dc000    [ 3776K] rw-/rw- SM=COW  Object_id=97d15133

Backtrace not available

Unknown thread crashed with ARM Thread State (64-bit):
    x0: 0x0000000104cd8000   x1: 0x000000016b1279b8   x2: 0x000000016b127968   x3: 0x0000000105565f02
    x4: 0x000000000000003d   x5: 0x000000000000003d   x6: 0x000000016b127710   x7: 0x00000000000005f0
    x8: 0x000000016b127998   x9: 0x000000016b127978  x10: 0x000000010558aa78  x11: 0x0000000000000003
   x12: 0x0000000000000003  x13: 0x00000000fffffffc  x14: 0x0000000000000000  x15: 0x0000000000000000
   x16: 0x000000010553a088  x17: 0x6ae100016b127968  x18: 0x0000000000000000  x19: 0x000000016b1279b8
   x20: 0x0000000104cd8000  x21: 0x000000016b127968  x22: 0x0000000104cd8000  x23: 0x000000016b127b18
   x24: 0x000000016b127b80  x25: 0x000000016b127a50  x26: 0x0000000000000000  x27: 0x0000000000000000
   x28: 0x000000016b127af8   fp: 0x000000016b127950   lr: 0x710f80010553a050
    sp: 0x000000016b1278d0   pc: 0x000000010553c474 cpsr: 0x80000000
   far: 0x0000000104cd8000  esr: 0x92000007

Binary images description not available

External Modification Summary:
  Calls made by other processes targeting this process:
    task_for_pid: 0
    thread_create: 0
    thread_set_state: 0
  Calls made by this process:
    task_for_pid: 0
    thread_create: 0
    thread_set_state: 0
  Calls made by all processes on this machine:
    task_for_pid: 0
    thread_create: 0
    thread_set_state: 0

VM Region Summary:
Writable regions: Total=8976K written=0K(0%) resident=0K(0%) swapped_out=0K(0%) unallocated=8976K(100%)

                                VIRTUAL   REGION 
REGION TYPE                        SIZE    COUNT (non-coalesced) 
===========                     =======  ======= 
STACK GUARD                       56.0M        1 
Stack                             8176K        1 
VM_ALLOCATE                        1.0G        2 
VM_ALLOCATE (reserved)             608K        2         reserved VM address space (unallocated)
mapped file                       7600K        7 
===========                     =======  ======= 
TOTAL                              1.1G       13 
TOTAL, minus reserved VM space     1.1G       13 

Model: MacBookPro17,1, BootROM 6723.101.4, proc 8:4:4 processors, 16 GB, SMC 
Graphics: kHW_AppleM1Item, Apple M1, spdisplays_builtin
Memory Module: LPDDR4
AirPort: spairport_wireless_card_type_airport_extreme, wl0: Apr  2 2021 00:48:12 version 18.52.1.2.7.8.116 FWID 01-9f7b4413
Bluetooth: Version 8.0.4d18, 3 services, 27 devices, 1 incoming serial ports
Network Service: Wi-Fi, AirPort, en0
USB Device: USB 3.1 Bus
USB Device: USB 3.1 Bus
Thunderbolt Bus: MacBook Pro, Apple Inc.
Thunderbolt Bus: MacBook Pro, Apple Inc.
jimeh commented 3 years ago

That's interesting and weird, some googling reveals issues around EXC_BAD_ACCESS reported and fixed in Emacs in 2019. Though some people had issues with some packages causing it.

Have you tried launching emacs with -Q to ignore your config?

shaunsingh commented 3 years ago

Nothing seems to be working (with or without my config), my config works under emacs-plus fine so I think its safe to say its an issue with the application itself

image

I codesigned it with my personal certificate just in case, but no luck

jimeh commented 3 years ago

Hmm, if you're able to codesign the app yourself, could you try first signing all *.eln files located under Contents/Resources/native-lisp? Cause codesign's --deep option does not detect those files, but they need to be signed for the app to be valid. I'm actually working on this right now in #44.

Simplest is to do something like find Emacs.app/Contents/Resources/native-ilsp -name '*.eln', and feed all found files into codesign, and then the path to Emacs.app itself as the very last file to be signed.

jimeh commented 3 years ago

To update on this, with #44 merged, there's a new tool which will correctly sign the Emacs.app bundle. It might help getting closer to the bottom of this issue..

To use the tool, you will need to install Go (brew install go), and then run make build to compile bin/emacs-builder.

Then to print the help info for the sign command:

$ bin/emacs-builder sign --help
NAME:
   emacs-builder sign - sign a Emacs.app bundle with codesign

USAGE:
   emacs-builder sign [command options] <emacs-app>

OPTIONS:
   --sign value, -s value          signing identity passed to codesign [$AC_SIGN_IDENTITY]
   --entitlements value, -e value  comma-separated list of entitlements to enable (default: "com.apple.security.cs.allow-jit", "com.apple.security.network.client", "com.apple.security.cs.disable-library-validation", "com.apple.security.automation.apple-events")
   --deep, -d                      pass --deep to codesign (default: true)
   --timestamp, -t                 pass --timestamp to codesign (default: true)
   --force, -f                     pass --force to codesign (default: true)
   --verbose, -v                   pass --verbose to codesign (default: false)
   --options value, -o value       options passed to codesign (default: "runtime")
   --codesign value                specify custom path to codesign executable
   --plan value, -p value          path to build plan YAML file produced by emacs-builder plan [$EMACS_BUILDER_PLAN]
   --help, -h                      show help (default: false)

The only thing you should need to pass in is the --sign flag to specify which signing identity to use.

LambertGreen commented 3 years ago

@jimeh Do I need to have a signing identity created to use as input in the emacs-builder sign command? I have an M1 and am running into the issue per this thread. Also wondering if the signing requirement is specific to machines with the M1 chip.

Thanks.

jimeh commented 3 years ago

@LambertGreen Yes, the sign command does require a signing identity on your machine, which required a paid for Apple Developer Account.

This article's "Acquiring a Developer ID Certificate" section covers it, personally I did the Xcode method which then didn't require a CSR.

As for M1 machines, from what I've understood, for Emacs builds to run they must be signed. macOS seems to have extra security requirements when running on M1 machines, to the point even code compiled on your own machine cannot run unless it's signed, unlike on Intel-based macs.

sethm commented 3 years ago

I believe you don't actually need a paid developer account to self-sign code, only to distribute it to other people. I used a self-signed certificate to sign Emacs, and it seems to work well. Instructions for creating a self-signed certificate are here: https://support.apple.com/guide/keychain-access/create-self-signed-certificates-kyca8916/mac

dignifiedquire commented 3 years ago

hey, trying to build on m1 as well, running into the following issue

==> INFO: Embedding libgccjit into Emacs.app
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/fileutils.rb:529:in `rename': Invalid argument @ rb_file_s_rename - (/Users/dignifiedquire/opensource/build-emacs-for-macos/builds/Emacs.2021-11-20.64fc94b.master.macOS-12.arm64/Emacs.app/Contents/MacOS/lib/lib/gcc/11/gcc/aarch64-apple-darwin20/11, /Users/dignifiedquire/opensource/build-emacs-for-macos/builds/Emacs.2021-11-20.64fc94b.master.macOS-12.arm64/Emacs.app/Contents/MacOS/lib/lib/gcc/11/gcc/aarch64-apple-darwin20/11/11) (Errno::EINVAL)
        from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/fileutils.rb:529:in `block in mv'
        from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/fileutils.rb:1558:in `block in fu_each_src_dest'
        from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/fileutils.rb:1572:in `fu_each_src_dest0'
        from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/fileutils.rb:1556:in `fu_each_src_dest'
        from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/fileutils.rb:520:in `mv'
        from ./build-emacs-for-macos:969:in `embed'
        from ./build-emacs-for-macos:112:in `build'
        from ./build-emacs-for-macos:1288:in `<main>'

any ideas?

jimeh commented 3 years ago

@dignifiedquire I believe this might actually be due to some directory structure changes within the gcc formula itself, as things look different from what I remember. Strangely enough though, I've not yet seen this issue in my nightly builds.

I should have time to investigate what's going on properly within the next day or two.

ianic commented 3 years ago

I have same issue on non M1, Intel mac:

==> INFO: Embedding libgccjit into Emacs.app
Traceback (most recent call last):
    8: from ./build-emacs-for-macos:1288:in `<main>'
    7: from ./build-emacs-for-macos:112:in `build'
    6: from ./build-emacs-for-macos:969:in `embed'
    5: from /Users/ianic/.rbenv/versions/2.7.2/lib/ruby/2.7.0/fileutils.rb:539:in `mv'
    4: from /Users/ianic/.rbenv/versions/2.7.2/lib/ruby/2.7.0/fileutils.rb:1587:in `fu_each_src_dest'
    3: from /Users/ianic/.rbenv/versions/2.7.2/lib/ruby/2.7.0/fileutils.rb:1603:in `fu_each_src_dest0'
    2: from /Users/ianic/.rbenv/versions/2.7.2/lib/ruby/2.7.0/fileutils.rb:1589:in `block in fu_each_src_dest'
    1: from /Users/ianic/.rbenv/versions/2.7.2/lib/ruby/2.7.0/fileutils.rb:548:in `block in mv'
/Users/ianic/.rbenv/versions/2.7.2/lib/ruby/2.7.0/fileutils.rb:548:in `rename': Invalid argument @ rb_file_s_rename - (/Users/ianic/work/dot_files/native/build-emacs-for-macos/builds/Emacs.2021-11-22.9ceb307.master.macOS-12.x86_64/Emacs.app/Contents/MacOS/lib/lib/gcc/11/gcc/x86_64-apple-darwin21/11, /Users/ianic/work/dot_files/native/build-emacs-for-macos/builds/Emacs.2021-11-22.9ceb307.master.macOS-12.x86_64/Emacs.app/Contents/MacOS/lib/lib/gcc/11/gcc/x86_64-apple-darwin21/11/11) (Errno::EINVAL)

I got the build passing after commenting line 969.

jimeh commented 3 years ago

As the nightly builds done via GitHub Actions still haven't encountered this issue, I was worried it might be difficult to reproduce. But the local build I fired off early this morning has the same issue, so I should be able to get a fix sorted tonight :)

jimeh commented 3 years ago

@dignifiedquire @ianic I've just pushed v0.6.24 with a fix for the rename issue. It now only performs the rename operation if the target name is different. This is still needed for macOS 10.15, as the nightly builds which uses 10.15 have not been having this issue.

So it seems the directory structure for the Monterey gcc homebrew bottle at least has changed, but not the Catalina one, yet at least.

Also @dignifiedquire, if you get it all working on your M1 machine, would you mind confirming so, and mentioning what you had to do if you had to do anything special? Cause if it's working, we can finally close this issue :)

dignifiedquire commented 3 years ago

Unfortunately things are not quite working, not sure exactly what the issue is.

I have built using

./build-emacs-for-macos master --native-full-aot

now when I extract the .tbz in the builds directory and try to run it I get

build-emacs-for-macos on master [?] via 🐹 v1.17.2 via 💎 v2.6.8
❯ ./builds/Emacs.2021-11-25.b469a01.master.macOS-12.arm64/Emacs.app/Contents/MacOS/bin/emacs
[1]    87606 killed

when launching the app via mouse click nothing happens.

This is all on current master and on a M1 Max on macOS 12.0.1

jimeh commented 3 years ago

@dignifiedquire I suspect this is due to the app not being signed. Try launching Emacs from a terminal with -nw flags to run it in a terminal, that should print the actual error instead of just a "killed" message. Or alternatively check Console.app for any error output from Emacs.

if it is a signing issue, you should see EXC_BAD_ACCESS (Code Signature Invalid) somewhere.

On Intel-based macs, things built on the machine did not require code signatures, but it seems that's not the case on Apple Silicon macs. One thing I'd suggest you try is right/ctrl click on Emacs.app itself, and choose "Open". Potentially that might provide a dialog that lets you open it despite being unsigned. At least it's how you by-pass the signature check on Intel-based macs if the app was not built on your machine.

dignifiedquire commented 3 years ago

I am not getting bad access errors, the exit code when launching with --nw is 137, indicating killed for OOM, which I find very odd.

The only error in the system logs for emacs is this entry

RBSStateCapture remove item called for untracked item 412-358-48397 (target:[app<application.org.gnu.Emacs.6966867.6972849(501)>:84374])
dignifiedquire commented 3 years ago

I removed my .emacs.d folder to be sure there are no strange interactions, but it did not change anything.

dignifiedquire commented 3 years ago

I managed to get it working using https://github.com/d12frosted/homebrew-emacs-plus, not sure what the differences are, but it does work on my machine

jimeh commented 3 years ago

@dignifiedquire Thanks for your help, apologies it didn't work out.

I believe I'm just gonna have to get my hands on an M1 machine myself to try and figure what's going on.

licht1stein commented 3 years ago

I've built both 28 and 29 without any issues on M1 today.

jimeh commented 3 years ago

@licht1stein Oh, that's awesome new. I haven't gotten around to trying to access a M1 machine yet, but looks like Scaleway are offering M1 Mac Minis for €0.10/hour with a minimum of 24h period, so I'll probably give that a try over the weekend :)

licht1stein commented 2 years ago

I literally had zero difficulties. It compiled on first try and loaded all my configs without any problems. There were a bunch of warnings on compilation, but who cares :)

licht1stein commented 2 years ago

Here's the command I used to build Emacs 28.0.60:

./build-emacs-for-macos --no-frame-refocus --no-titlebar --git-sha 70c229b1fc4f2eb66b5a173cfc8473e4b7ce48f0
vedang commented 2 years ago

I successfully compiled Emacs from source on the M1 Macbook Air right now. Noting down the steps in the hopes it will help others.

Initially, I had a linker failure ld: library not found for -lwebp. I added the line brew 'webp' to the Brewfile to install this dependency (via brew bundle on the CLI). However, later I realised that the problem was because the correct LIBRARY_PATH was not being set. So I'm not sure if this change is needed.

The actual commands I executed were:

brew bundle
export LIBRARY_PATH=$LIBRARY_PATH:/opt/homebrew/lib/
export CPATH=$CPATH:/opt/homebrew/include/
./build-emacs-for-macos --git-sha c00ffe263d9ffeb4d2a5e13cc124c786152137db

Once compilation was successful, I extracted the .tbz file created under builds and signed it as explained in https://github.com/jimeh/build-emacs-for-macos/pull/65

codesign --force --deep -s - Emacs.app
Ivanknmk commented 2 years ago

Thanks @vedang! I was struggling with the same issue of the webp library not found, and most of your solution did work for me. The only thing that didn't work was that doing export CPATH=$CPATH:/opt/homebrew/include/ messed things up as the compilation didn't find the necessary files to compile for arm64.

Anyway, thank you very much for sharing!

stevemolitor commented 2 years ago

@vedang's instructions here worked perfectly for me 2 1/2 weeks ago, but now I'm getting the dreaded error about libgccjit:

configure: error: ELisp native compiler was requested, but libgccjit was not found.
Please try installing libgccjit or a similar package.
If you are sure you want Emacs be compiled without ELisp native compiler,
pass the --without-native-compilation option to configure.
ERROR: Exit code: 1

MacOS upgraded Xcode Command Line Tools for me, which I think introduced the problem.

I uninstalled Xcode Command Line Tools and homebrew, and then reinstalled homebrew (which reinstalled Xcode Command Line Tools), to no avail. Same error.

I noticed that there are different gcc versions between homebrew and Xcode CL tools.

❯ /opt/homebrew/bin/gcc-12 --version
gcc-12 (Homebrew GCC 12.1.0) 12.1.0
~ ❯ which gcc
/usr/bin/gcc
~ ❯ gcc --version
Apple clang version 13.1.6 (clang-1316.0.21.2.5)
Target: arm64-apple-darwin21.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

So, gcc version 12.1.0 for homebrew, Apple clang 13.1.6 for Xcode CL Tools.

I did try aliasing to homebrew gcc via alias gcc=/opt/homebrew/bin/gcc-12 an re-running ./build-emacs-for-macos, but got the same error.

sfraize commented 2 years ago

Hi Steve. Seeing same issue. FYI gcc/libgccjit were rev'd up to 12.1 in homebrew just this past week which has caused some mayhem. When I did a brew upgrade (on M1 Air) it immediately broke two existing Emacs 29 executables (one from build-emacs-for-macos, the other emacs-plus@29) as it deleted the old libraries, and rebuilds were also broken. emacs-plus@29 just pushed a patch for it so that is building again (the directory structure in the Cellar is slightly different with 12.1). I don't know if there's a LIBRARY_PATH that'll work right now.

(Btw originally I had to delete and re-install CommandLineTools as well.)

stevemolitor commented 2 years ago

@sfraize Thanks - I also just saw the emacs-plus issue and patches. Looks like they patched the emacs configure script:

They have a similar patch for 28.

It looks like they added a tail -1 to the end of this:

brew ls -v libgccjit | grep -E 'libgccjit\.(so|dylib)' | tail -1

It looks like without the tail grep matches two lines now. Haven't investigated much further than that yet.

jimeh commented 2 years ago

@stevemolitor @sfraize A patch for the libgccjit 12.x homebrew formula was added in #75, which has been merged into master. This should resolve your libgccjit related issues.

stevemolitor commented 2 years ago

@stevemolitor @sfraize A patch for the libgccjit 12.x homebrew formula was added in #75, which has been merged into master. This should resolve your libgccjit related issues.

Thanks @jimeh #75 worked perfectly for me!