zadam / trilium

Build your personal knowledge base with Trilium Notes
GNU Affero General Public License v3.0
26.09k stars 1.78k forks source link

Support compiling native MacOS arm64 (Apple Silicon) builds #4772

Closed gigamonster256 closed 1 month ago

gigamonster256 commented 1 month ago

Following along with the original request in #3125 and the followup plus community troubleshooting in #3302, I was able to successfully build a working native arm64 build of trilium for MacOS on my M1 mac as well using my Windows 11 wsl environment.

Using the native package rather than relying on the Rosetta translation layer leads to a much better startup time (a couple seconds at most vs 10-20 seconds before) and a much snappier experience. (as originally reported in #3302)

A user in #3302 voiced concerns about an arm64 native build being bigger than the x86-64 build, but their observations seem to come from using a different packaging system than trilium uses for its other builds. I replicated the current build flow for arm64 and the resulting binaries are comparable to the released build sizes. (See below)

Screenshot 2024-05-18 at 3 38 18 AM I built both the latest and second latest versions of trilium to test. The top row of applications was compiled by me (both intel and arm builds) and the bottom are from the trilium releases.

Concerning future maintainability, better-sqlite3 does not have prebuilt binaries for version 8.4.0 with electron 25 so I compiled that combination myself for macos arm64 and added it to the precompiled binaries used for trilium releases. If in future releases of trilium the version of electron and/or better-sqlite3 is changed, precompiled binaries will most likely be available through better-sqlite3 releases, removing the need for an Apple Silicon computer to keep arm64 builds up to date.

meichthys commented 1 month ago

Get work! Do you have a sense of how difficult it would be to also build a Linux arm 64 build? I attempted it a few times but haven't been successful.

gigamonster256 commented 1 month ago

Get work! Do you have a sense of how difficult it would be to also build a Linux arm 64 build? I attempted it a few times but haven't been successful.

Based on my very limited understanding of electron apps (literally all learned trying to get this working) the only prebuilt architecture dependent component that changes between trilium builds is the better-sqlite3 database bindings. It seems that the main workhorse of the packaging, electron-packager, is able to "cross-compile" everything else (as much as you can compile an electron app.) Unfortunately the bindings of better-sqlite3 8.4.0 with electron 25 (NODE_MODULE_VERSION = 116 and what trilium currently uses) are not available as binaries from the developers so I had to build it on a native platform to get maxos arm64 working. It looks like all that is needed to get linux_arm64 working is to make/find a copy of the correctly versioned better_sqlite3.node file for linux_arm64 and add/modify the build scripts like I did in this pull request to add it.

Interestingly enough, it seems that electron-packager does not support building on linux_arm64 but does support it as a target so you'd need to build on a supported system then bring the zip over to the arm linux device.

zadam commented 1 month ago

That's great, thanks for the contribution. I was under impression that building an M1 electron package required building on M1.