jimeh / build-emacs-for-macos

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

Add instructions for linking to a native-comp build from the command line. #16

Closed shawnohare closed 4 years ago

shawnohare commented 4 years ago

If possible, it would be useful to provide some basic instructions in the README about how to execute the built Emacs.app from the command line. My naive attempts to do this failed.

I successfully built Emacs.app against feature/native-comp yesterday. I symlinked the built Emacs.app into /Applications/Emacs.app and then tried to link to underlying executable as follows, but have issues with dynamic linkage:

# symlink attempt
❯ ln -sf /Applications/Emacs.d/Content/MacOS/Emacs ~/.local/bin/emacs
❯ emacs --version
emacs: dlopen(/usr/local/bin/../native-lisp/28.0.50-x86_64-apple-darwin19.6.0-204614f4bb50514fa29682e34b0741bf/lisp-mode-0189ba85598c041b7504f0a916c04219-1738806322de892570d69dfc55b437c2.eln, 1): image not found

# hardlink attempt
❯ ln -f /Applications/Emacs.d/Content/MacOS/Emacs ~/.local/bin/emacs
❯ emacs --version
dyld: Library not loaded: @executable_path/lib-x86_64-10_15/libgccjit.so.0
  Referenced from: /Users/shawn.ohare/bin/emacs
  Reason: image not found
zsh: abort      emacs --version

# directly calling the symlinked Emacs.app executable:
❯ /Applications/Emacs.app/Contents/MacOS/Emacs --version
GNU Emacs 28.0.50
Copyright (C) 2020 Free Software Foundation, Inc.
GNU Emacs comes with ABSOLUTELY NO WARRANTY.
You may redistribute copies of GNU Emacs
under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING.

I appear to be able to open emacs via Spotlight just fine, but can't successfully run a doom-emacs install since the linked emacs executable fails.

One obvious solution, which I dislike, is to just modify PATH to include the built emacs executable. Is there another way? In either case I'm sure a few sentences in the README would help.

jimeh commented 4 years ago

Not a bad shout, I'll put something together for the README.

In the meantime though, this gets a little tricky, as you can't just symlink the main executable to somewhere else, because it depends on all the files within the application bundle, and executing it outside of the bundle via a symlink or hardlink, means the executable can't find any other parts of Emacs to run.

Normally with CLI-only installs of Emacs, it will have all of it's internal guts (lisp files, libraries, etc.) placed in absolute paths on disk, so the physical location on disk of the executable or symlinks to it don't matter that much.

Personally I use shell aliases to create a emacs command for use in the terminal, from my emacs.zsh shell setup file:

if [[ "$OSTYPE" == "darwin"* ]]; then
  if [ -f "/Applications/Emacs.app/Contents/MacOS/Emacs" ]; then
    alias emacsgui="env TERM=screen-24bit /Applications/Emacs.app/Contents/MacOS/Emacs"
    alias emacs="env TERM=screen-24bit /Applications/Emacs.app/Contents/MacOS/Emacs -nw"
  fi

  if [ -f "/Applications/Emacs.app/Contents/MacOS/bin/emacsclient" ]; then
    alias emacsclient="env TERM=screen-24bit /Applications/Emacs.app/Contents/MacOS/bin/emacsclient"
  fi
fi

As for doom-emacs, I've just had a look at how its doom command initializes itself, and it either uses the value of a EMACS environment variable as the executable, or if that's not set, just uses emacs.

So for doom-emacs' doom command to work, setting EMACS seems to work for me. Something like this in your shell setup should do the trick:

export EMACS="/Applications/Emacs.app/Contents/MacOS/Emacs"
shawnohare commented 4 years ago

Thanks for the quick response. I think adding the alias snippet to the README would be very helpful, and works well enough until the native-comp feature is readily packaged.