From be326b22aa0e940d3b4e88153d01e4e9aceddc24 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Wed, 27 Oct 2021 03:17:17 +0100 Subject: [PATCH] refactor(native-comp): improve native-comp env setup patch Instead of patching emacs sources before building Emacs, we can use site-start.el instead for a much cleaner way of setting LIBRARY_PATH before the user config is loaded. --- build-emacs-for-macos | 56 +++++++++++++++++-------- patches/native-comp-env-setup.diff.erb | 57 -------------------------- 2 files changed, 39 insertions(+), 74 deletions(-) delete mode 100644 patches/native-comp-env-setup.diff.erb diff --git a/build-emacs-for-macos b/build-emacs-for-macos index a12e94e..9b264b6 100755 --- a/build-emacs-for-macos +++ b/build-emacs-for-macos @@ -286,8 +286,6 @@ class Build verify_native_comp gcc_info.verify_libgccjit - apply_native_comp_env_setup_patch(source) - ENV['CFLAGS'] = [ "-I#{File.join(gcc_info.root_dir, 'include')}", "-I#{File.join(gcc_info.libgccjit_root_dir, 'include')}", @@ -607,21 +605,6 @@ class Build system(*args) || err("Exit code: #{$CHILD_STATUS.exitstatus}") end - def apply_native_comp_env_setup_patch(source) - term = 'native-compile-setup-environment-variables' - file = 'lisp/emacs-lisp/comp.el' - return if `grep '#{term}' '#{file}'`.strip.size.positive? - - template = File.read( - File.join(__dir__, 'patches/native-comp-env-setup.diff.erb') - ) - patch = ERB.new(template).result(gcc_info.get_binding) - patch_file = File.join(source, 'macos_patches/native-comp-env-setup.diff') - - File.write(patch_file, patch) - apply_patch({ file: patch_file }, source) - end - def effective_version @effective_version ||= begin case ref @@ -975,10 +958,45 @@ class GccLibEmbedder < AbstractEmbedder FileUtils.rm(Dir[File.join(target_dir, '**', '.DS_Store')], force: true) FileUtils.chmod_R('u+w', target_dir) FileUtils.mv(source_darwin_dir, target_darwin_dir) + + env_setup = ERB.new(NATIVE_COMP_ENV_VAR_TPL).result(gcc_info.get_binding) + return if File.exist?(site_start_el_file) && + File.read(site_start_el_file).include?(env_setup) + + File.open(site_start_el_file, 'a') do |f| + f.puts("\n#{env_setup}") + end end private + NATIVE_COMP_ENV_VAR_TPL = <<~ELISP + ;; Set LIBRARY_PATH to point at bundled GCC and Xcode Command Line Tools to + ;; ensure native-comp works. + (when (and (eq system-type 'darwin) + (string-match-p "\\.app\\/Contents\\/MacOS\\/?$" + invocation-directory)) + (let* ((library-path-env (getenv "LIBRARY_PATH")) + (devtools-dir + "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib") + (gcc-dir (expand-file-name + "<%= app_bundle_relative_lib_dir %>" + invocation-directory)) + (darwin-dir (expand-file-name + "<%= app_bundle_relative_darwin_lib_dir %>" + invocation-directory)) + (lib-paths (list))) + + (if library-path-env + (push library-path-env lib-paths)) + (if (file-directory-p devtools-dir) + (push devtools-dir lib-paths)) + (push darwin-dir lib-paths) + (push gcc-dir lib-paths) + + (setenv "LIBRARY_PATH" (mapconcat 'identity lib-paths ":")))) + ELISP + def embedded? Dir[File.join(target_dir, 'libgcc*')].any? end @@ -1002,6 +1020,10 @@ class GccLibEmbedder < AbstractEmbedder def relative_dir(path, root) Pathname.new(path).relative_path_from(Pathname.new(root)).to_s end + + def site_start_el_file + @site_start_el_file ||= File.join(resources_dir, 'lisp', 'site-start.el') + end end class GccInfo diff --git a/patches/native-comp-env-setup.diff.erb b/patches/native-comp-env-setup.diff.erb deleted file mode 100644 index 1c7147a..0000000 --- a/patches/native-comp-env-setup.diff.erb +++ /dev/null @@ -1,57 +0,0 @@ -diff --git a/lisp/emacs-lisp/comp.el b/lisp/emacs-lisp/comp.el -index 638d4b274c..2599211936 100644 ---- a/lisp/emacs-lisp/comp.el -+++ b/lisp/emacs-lisp/comp.el -@@ -4224,6 +4224,52 @@ native-compile-async - (let ((load (not (not load)))) - (native--compile-async files recursively load selector))) - -+;;;###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")) -+ (devtools-dir -+ "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib") -+ (gcc-dir (expand-file-name -+ "<%= app_bundle_relative_lib_dir %>" -+ invocation-directory)) -+ (darwin-dir (expand-file-name -+ "<%= app_bundle_relative_darwin_lib_dir %>" -+ invocation-directory)) -+ (lib-paths (list))) -+ -+ (if library-path-env -+ (push library-path-env lib-paths)) -+ (if (file-directory-p devtools-dir) -+ (push devtools-dir lib-paths)) -+ (push darwin-dir lib-paths) -+ (push gcc-dir lib-paths) -+ -+ (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 'comp--native-compile -+ 'native-compile-setup-environment-variables) -+ (advice-remove 'native-compile-async -+ '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 'comp--native-compile :before -+ 'native-compile-setup-environment-variables) -+(advice-add 'native-compile-async :before -+ 'native-compile-setup-environment-variables) -+(advice-add 'native--compile-async :before -+ 'native-compile-setup-environment-variables) -+ - (provide 'comp) - - ;; LocalWords: limplified limplified limplification limplify Limple LIMPLE libgccjit elc eln