Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/native comp #79

Merged
merged 41 commits into from
Dec 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
ad78416
First native-comp release
daviderestivo Sep 19, 2020
9849ec3
Cleanup native-comp support
daviderestivo Oct 18, 2020
571a229
Minor fixes to README file
daviderestivo Oct 18, 2020
0c7654c
Merge branch 'dev' into feature/native-comp
daviderestivo Nov 14, 2020
0a5f8fe
Add native comp support into emacs-28 workflow
daviderestivo Nov 14, 2020
1a2f918
Fix system appearance patch shasum
daviderestivo Nov 14, 2020
54dd035
Add feature/native-comp branch into emacs-28 workflow
daviderestivo Nov 14, 2020
1afb762
Fix emacs-28 workflow
daviderestivo Nov 14, 2020
cf3ed7d
Add verbose flag to brew install
daviderestivo Nov 15, 2020
290fda7
Merge branch 'dev' into feature/native-comp
daviderestivo Nov 15, 2020
958132d
Move branch selection
daviderestivo Nov 15, 2020
3292976
Include gcc lib path into LIBRARY_PATH
daviderestivo Nov 15, 2020
6706dda
Add more native-comp reference links
daviderestivo Nov 15, 2020
7c46f56
Merge branch 'dev' into feature/native-comp
daviderestivo Nov 15, 2020
43fc1c0
Add dependencies
daviderestivo Nov 21, 2020
940b1e4
Remove custom gcc formula and re-arrange dependencies
daviderestivo Nov 23, 2020
0436039
Some cleanups
daviderestivo Nov 23, 2020
0a0fb6e
Add Ahead-of-Time native compilation installation option
daviderestivo Nov 25, 2020
4d1df80
Disable AoT to speedup build time, add native-comp env setup patch
daviderestivo Nov 25, 2020
6f72dcf
Add native-comp make_flags support
daviderestivo Nov 25, 2020
c1ec4e2
Fix `get_resource_url' to work with GitHub Actions CI
daviderestivo Nov 25, 2020
c8c6ccc
Fix typo
daviderestivo Nov 25, 2020
c8373e4
Use gcc-10 instead of Apple compiler
daviderestivo Nov 25, 2020
05c4c07
Roll back to clang
daviderestivo Nov 25, 2020
d6b2f1d
Add locallisppath to configure
daviderestivo Nov 25, 2020
0576443
Add /usr/local/lib/gcc/*/gcc/*-apple-darwin*/*/ to LIBRARY_PATH
daviderestivo Nov 26, 2020
ded8765
Merge branch 'dev' into feature/native-comp
daviderestivo Nov 26, 2020
c1502bf
Cleanup CFLAGS
daviderestivo Nov 26, 2020
b485930
Use always --verbose in brew install
daviderestivo Nov 28, 2020
0a65980
Remove harfbuzz and --disable-ns-self-contained when native-comp is on
daviderestivo Nov 28, 2020
9f5ac5e
Cleanup formula and add back --disable-ns-self-contained by default
daviderestivo Nov 28, 2020
618833b
Improve full Ahead-of-Time compilation support
daviderestivo Dec 6, 2020
b4bf3a7
Merge branch 'dev' into feature/native-comp
daviderestivo Dec 18, 2020
ceeb2d9
Change .eln files dylib ID before post-install phase
daviderestivo Dec 22, 2020
2cb87ad
Cosmetic changes
daviderestivo Dec 22, 2020
b87da82
Make native-comp feature less verbose
daviderestivo Dec 22, 2020
a049be5
Remove native-comp branch from GitHub Actions workflow
daviderestivo Dec 22, 2020
de7b8c5
Fix README file
daviderestivo Dec 22, 2020
bcc6e21
Improve native-comp option description
daviderestivo Dec 22, 2020
a384587
Cosmetic fix
daviderestivo Dec 22, 2020
71612c1
Cosmetic changes to README file
daviderestivo Dec 22, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/emacs-28.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ jobs:
- ''
# All options (imagemagick@7)
- '--with-cocoa --with-no-frame-refocus --with-imagemagick --with-pdumper --with-xwidgets'
# All options (imagemagick@7) + native-comp support
- '--with-cocoa --with-no-frame-refocus --with-imagemagick --with-pdumper --with-xwidgets --with-native-comp'

env:
HOMEBREW_GITHUB_REF: ${{ github.head_ref || github.ref }}
Expand Down
82 changes: 79 additions & 3 deletions Formula/emacs-head@28.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
class EmacsHeadAT28 < Formula
desc "GNU Emacs text editor"
homepage "https://www.gnu.org/software/emacs/"
url "https://github.com/emacs-mirror/emacs.git"
version "28.0.50"
revision 1

depends_on "autoconf" => :build
depends_on "coreutils" => :build
depends_on "gnu-sed" => :build
depends_on "texinfo" => :build
depends_on "automake" => :build
depends_on "cmake" => :build
depends_on "pkg-config" => :build
depends_on "gcc" => :build
depends_on "giflib"
depends_on "gnutls"
depends_on "librsvg"
Expand Down Expand Up @@ -48,6 +49,10 @@ class EmacsHeadAT28 < Formula
"Enable pdumper support"
option "with-xwidgets",
"Enable xwidgets support"
option "with-native-comp",
"Enable Elisp native compilation support"
option "with-native-full-aot",
"Enable Elisp Ahead-of-Time native compilation support"
option "with-modern-icon-sjrmanning",
"Use a modern style icon by @Sjrmanning"
option "with-modern-icon-asingh4242",
Expand Down Expand Up @@ -141,6 +146,15 @@ class EmacsHeadAT28 < Formula
option "with-retro-icon-sink",
"Use a retro style icon by Erik Mugele"

if build.with? "native-comp"
url "https://github.com/emacs-mirror/emacs.git", :branch => "feature/native-comp"
depends_on "gmp" => :build
depends_on "libjpeg" => :build
depends_on "libgccjit" => :reccomended
else
url "https://github.com/emacs-mirror/emacs.git"
end

def self.get_resource_url(resource)
if ENV['HOMEBREW_GITHUB_REF']
branch = ENV['HOMEBREW_GITHUB_REF'].sub("refs/heads/", "")
Expand All @@ -155,6 +169,13 @@ def self.get_resource_url(resource)
# overcome the reinstall issue mentioned here:
# https://github.com/daviderestivo/homebrew-emacs-head/issues/28

if build.with? "native-comp"
patch do
url EmacsHeadAT28.get_resource_url("patches/0009-Native-comp-env-setup.patch")
sha256 "418939d1935a1b0cdbd09c500ab964ee38e05accdd76a73564647c4597bf1230"
end
end

# Patches
resource "0001-No-frame-refocus-cocoa" do
url EmacsHeadAT28.get_resource_url("patches/0001-No-frame-refocus-cocoa.patch")
Expand All @@ -176,6 +197,11 @@ def self.get_resource_url(resource)
sha256 "1f8423ea7e6e66c9ac6dd8e37b119972daa1264de00172a24a79a710efcb8130"
end

resource "0009-Native-comp-env-setup.patch" do
url EmacsHeadAT28.get_resource_url("patches/0009-Native-comp-env-setup.patch")
sha256 "418939d1935a1b0cdbd09c500ab964ee38e05accdd76a73564647c4597bf1230"
end

# Icons
resource "modern-icon-sjrmanning" do
url EmacsHeadAT28.get_resource_url("icons/modern-icon-sjrmanning.icns")
Expand Down Expand Up @@ -451,12 +477,42 @@ def install
--without-x
]

make_flags = []

if build.with? "dbus"
args << "--with-dbus"
else
args << "--without-dbus"
end

if build.with? "native-comp"

if build.with? "native-full-aot"
ohai "Force full Ahead-of-Time compilation"
make_flags << "NATIVE_FULL_AOT=1"
end

gcc_version = Formula["gcc"].any_installed_version
gcc_version_major = gcc_version.major
gcc_lib="#{HOMEBREW_PREFIX}/lib/gcc/#{gcc_version_major}"

ENV['CFLAGS'] = [
'-O2',
'-march=native'
].compact.join(' ')

ENV.append "CFLAGS", "-I#{Formula["gcc"].include}"

ENV.append "LDFLAGS", "-L#{gcc_lib}"
ENV.append "LDFLAGS", "-I#{Formula["gcc"].include}"
ENV.append "LDFLAGS", "-I#{Formula["libgccjit"].include}"
ENV.append "LDFLAGS", "-I#{Formula["gmp"].include}"
ENV.append "LDFLAGS", "-I#{Formula["libjpeg"].include}"

args << "--with-nativecomp"
make_flags << "BYTE_COMPILE_EXTRA_FLAGS=--eval '(setq comp-speed 2)'"
end

# Note that if ./configure is passed --with-imagemagick but can't find the
# library it does not fail but imagemagick support will not be available.
# See: https://debbugs.gnu.org/cgi/bugreport.cgi?bug=24455
Expand Down Expand Up @@ -484,6 +540,9 @@ def install
ENV.append_to_cflags "-g3"
end

# Use GNU install
ENV.prepend_path "PATH", Formula["coreutils"].opt_libexec/"gnubin"
# Use GNU sed
ENV.prepend_path "PATH", Formula["gnu-sed"].opt_libexec/"gnubin"
system "./autogen.sh"

Expand All @@ -505,7 +564,7 @@ def install
end
end

system "make"
system "make", *make_flags
system "make", "install"

icons_dir = buildpath/"nextstep/Emacs.app/Contents/Resources"
Expand Down Expand Up @@ -541,6 +600,23 @@ def install
end
end

if build.with? "native-comp"
contents_dir = buildpath/"nextstep/Emacs.app/Contents"
contents_dir.install "native-lisp"
contents_dir.install "lisp"

# Change .eln files dylib ID to avoid that after the
# post-install phase all of the *.eln files end up with the
# same ID. See: https://github.com/Homebrew/brew/issues/9526
# and https://github.com/Homebrew/brew/pull/10075
Dir.glob(contents_dir/"native-lisp/*/*.eln").each do |f|
fo = MachO::MachOFile.new(f)
ohai "Change dylib_id of ELN files before post_install phase"
fo.dylib_id = "#{contents_dir}/" + f
fo.write!
end
end

# Install the (separate) debug symbol data that is generated
# for the application
if build.with? "crash-debug"
Expand Down Expand Up @@ -573,7 +649,7 @@ def install
end
end

system "make"
system "make", *make_flags
system "make", "install"
end

Expand Down
67 changes: 49 additions & 18 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ GNU Emacs 26.3 bottles have been built with the following options:
#+begin_src bash
--with-cocoa
--with-no-frame-refocus
--with-imagemagick (6)
--with-imagemagick (6.x)
--with-multicolor-fonts
#+end_src

Expand All @@ -50,7 +50,7 @@ GNU Emacs 27.1 bottles have been built with the following options:
#+begin_src bash
--with-cocoa
--with-no-frame-refocus
--with-imagemagick (7)
--with-imagemagick (7.x)
--with-pdumper
--with-xwidgets
#+end_src
Expand Down Expand Up @@ -99,22 +99,24 @@ Modeline Theme: [[https://github.com/daviderestivo/smart-mode-line-atom-one-dark
** Compilation options
The following compilation options are available:

| Option | Description |
|-------------------------+------------------------------------------------------------------------------|
| ~--with-crash-debug~ | Append `-g3` to CFLAGS to enable crash debugging |
| ~--with-cocoa~ | Build a Cocoa version of GNU Emacs |
| ~--with-ctags~ | Don't remove the ctags executable that GNU Emacs provides |
| ~--with-dbus~ | Build with dbus support |
| ~--without-gnutls~ | Disable gnutls support |
| ~--with-imagemagick~ | Build with imagemagick support |
| ~--without-librsvg~ | Disable librsvg support |
| ~--with-mailutils~ | Build with mailutils support |
| ~--with-multicolor-fonts~ | Enable multicolor fonts support on macOS (only for emacs-head) |
| ~--without-modules~ | Disable dynamic modules support |
| ~--with-no-frame-refocus~ | Disables frame re-focus (ie. closing one frame does not refocus another one) |
| ~--without-libxml2~ | Disable libxml2 support |
| ~--with-pdumper~ | Enable pdumper support (only for emacs-head@27 and emacs-head@28) |
| ~--with-xwidgets~ | Enable xwidgets support (only for emacs-head@27 and emacs-head@28) |
| Option | Description |
|---------------------------+--------------------------------------------------------------------------------|
| ~--with-crash-debug~ | Append `-g3` to CFLAGS to enable crash debugging |
| ~--with-cocoa~ | Build a Cocoa version of GNU Emacs |
| ~--with-ctags~ | Don't remove the ctags executable that GNU Emacs provides |
| ~--with-dbus~ | Build with dbus support |
| ~--without-gnutls~ | Disable gnutls support |
| ~--with-imagemagick~ | Build with imagemagick support |
| ~--without-librsvg~ | Disable librsvg support |
| ~--with-mailutils~ | Build with mailutils support |
| ~--with-multicolor-fonts~ | Enable multicolor fonts support on macOS (only for emacs-head) |
| ~--without-modules~ | Disable dynamic modules support |
| ~--with-no-frame-refocus~ | Disables frame re-focus (ie. closing one frame does not refocus another one) |
| ~--without-libxml2~ | Disable libxml2 support |
| ~--with-pdumper~ | Enable pdumper support (only for emacs-head@27 and emacs-head@28) |
| ~--with-xwidgets~ | Enable xwidgets support (only for emacs-head@27 and emacs-head@28) |
| ~--with-native-comp~ | Enable Elisp native compilation support (only for emacs-head@28) |
| ~--with-native-full-aot~ | Enable Elisp Ahead-of-Time native compilation support (only for emacs-head@28) |

For the terminal version only of GNU Emacs please omit ~--with-cocoa~.

Expand Down Expand Up @@ -165,6 +167,35 @@ This formula support via a (patch) native macOS X Cocoa Xwidgets
support via embedding a native webkit window. To enable this feature
please use ~--HEAD --with-xwidgets~.

** Native-comp support
This formula support native Elisp code compilation. In order to enable
it please follow the below instructions:

#+begin_src bash
brew install emacs-head@28 --with-cocoa --with-native-comp
#+end_src

NATIVE_FULL_AOT is disabled by default to improve build time, Only few
lisp source files will be compiled during the build. All of the
remaining ones will be dynamically compiled in the background once the
Emacs application is launched for the first time. If you with to
enable native full AoT, please use ~--with-native-full-aot~ option
together with ~--with-native-comp~.

More details about native Elisp code compilation can be found in the
below links:

- [[https://european-lisp-symposium.org/static/2020/corallo-nassi-manca-slides.pdf][Bringing GNU Emacs to Native Code (Slides)]]
- [[https://www.youtube.com/watch?v=zKHYZOAc_bQ&app=desktop][Bringing GNU Emacs to Native Code (YouTube Video)]]
- [[http://akrl.sdf.org/gccemacs.html][gccemacs]]

Additional useful links:

- [[https://gist.github.com/AllenDang/f019593e65572a8e0aefc96058a2d23e][Use pre-built gccemacs]]
- [[https://github.com/jimeh/build-emacs-for-macos][build-emacs-for-macos]]
- [[https://github.com/shshkn/emacs.d/blob/master/docs/nativecomp.md][gccemacs]]
- [[https://gist.github.com/mikroskeem/0a5c909c1880408adf732ceba6d3f9ab][gccemacs on OS X]]

** Available icons
| Option | Description | Image | URL |
|----------------------------------------------------+-----------------------------------------------------+--------------------------------------------------------+------|
Expand Down
50 changes: 50 additions & 0 deletions patches/0009-Native-comp-env-setup.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
diff --git a/lisp/emacs-lisp/comp.el b/lisp/emacs-lisp/comp.el
index 25e2de9..bcedd31 100644
--- a/lisp/emacs-lisp/comp.el
+++ b/lisp/emacs-lisp/comp.el
@@ -2801,6 +2801,45 @@ queued with LOAD %"
(comp-run-async-workers)
(message "Compilation started."))))

+;;;###autoload
+(defun native-compile-setup-environment-variables (&rest _args)
+ "Ensure LIBRARY_PATH is set correctly when libgccjit is bundled."
+ (when (and (eq system-type 'darwin)
+ (string-match-p "\.app\/Contents\/MacOS\/?$"
+ invocation-directory))
+ (let* ((library-path-env (getenv "LIBRARY_PATH"))
+ (gcc-base-dir (concat invocation-directory "lib/gcc"))
+ (gcc-version (car (seq-filter
+ (lambda (dir) (string-match-p "^[0-9]+$" dir))
+ (directory-files gcc-base-dir))))
+ (gcc-dir (concat gcc-base-dir "/" gcc-version))
+ (darwin-base-dir (car (file-expand-wildcards
+ (concat gcc-dir "/gcc/*apple-darwin*"))))
+ (darwin-version (car (seq-filter
+ (lambda (dir)
+ (string-match-p
+ "^[0-9]+\\(\.[0-9]+\\(\.[0-9]+\\)?\\)?$" dir))
+ (directory-files darwin-base-dir))))
+ (darwin-dir (concat darwin-base-dir "/" darwin-version))
+ (lib-paths (append
+ (list gcc-dir darwin-dir)
+ (if library-path-env (list library-path-env) (list)))))
+
+ (when (and gcc-dir darwin-dir)
+ (setenv "LIBRARY_PATH" (mapconcat 'identity lib-paths ":")))))
+
+ ;; Remove advice, as it only needs to run once.
+ (advice-remove 'native-compile
+ 'native-compile-setup-environment-variables)
+ (advice-remove 'native-compile-async
+ 'native-compile-setup-environment-variables))
+
+;; Ensure environment setup runs before any native compilation.
+(advice-add 'native-compile :before
+ 'native-compile-setup-environment-variables)
+(advice-add 'native-compile-async :before
+ 'native-compile-setup-environment-variables)
+
(provide 'comp)

;;; comp.el ends here