diff --git a/lisp/emacs-lisp/comp.el b/lisp/emacs-lisp/comp.el index 8c638312b0..87af889ef4 100644 --- a/lisp/emacs-lisp/comp.el +++ b/lisp/emacs-lisp/comp.el @@ -4215,6 +4215,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 + "<%= relative_lib_dir %>" + invocation-directory)) + (darwin-dir (expand-file-name + "<%= sanitized_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