Subject: bug#75812: 29.2.50; Support native-compiling package-install from batch Emacs

From: Spencer Baugh <[email-masked]>
Date: Fri, 24 Jan 2025 17:35:50 -0500

Stefan Kangas <[email-masked]> writes:
> Spencer Baugh <[email-masked]> writes:
>
>> Currently, setting package-native-compile to t will make package-install
>> asynchronously native-compile the package.  This works great for
>> interactive usage.
>>
>> However, this is unsuitable for a batch Emacs running package-install.
>> "emacs -batch --eval (package-install something)" will exit once the
>> package is installed, but before the asynchronous native compilation
>> completes; the native compilation will just be abandoned.  So,
>> currently, there's no straightforward way to native compile a package
>> from a batch Emacs.
>>
>> Running package-install from a batch Emacs is useful as a way to update
>> or pre-install packages in scripts, without requiring user interaction.
>> This is widely used by package managers like Nix or dpkg to install
>> Emacs packages as OS packages.  In my specific case, it's used to
>> install and byte-compile common packages into site-lisp.  In all these
>> cases, it would be nice to be able to native-compile the package at
>> installation time and include the .eln files in the package directory.
>>
>> A straightforward solution would be for package-install-file to support
>> synchronously native-compiling the installed package, such that after
>> package-install finishes running, the native compilation is complete.
>> Perhaps with a new value of package-native-compile?
>
> Sounds good to me, thanks.  We'll take patches implementing this.

I'm happy to implement this, I'm just a bit lost about where to start.
I tried the obvious "iterate over *.el files in the package directory,
calling native-compile", but that failed on various files in various
packages.

There's a lot of code in package--compile and byte-recompile-directory,
which does a bunch of stuff like ignoring certain files.  I guess I need
that code.  Ultimately, package--compile calls byte-compile-file, so if
I could get byte-compile-file to also emit native-compiled files, that
would work.  Is there a way to do that?

byte+native-compile looks like a relevant variable, but is that the
right direction to head in?