Skip to content

build: build a universal2 binary - #86

Closed
DarthMDev wants to merge 1 commit into
MCMi460:developmentfrom
DarthMDev:build-for-universal2
Closed

DarthMDev wants to merge 1 commit into
MCMi460:developmentfrom
DarthMDev:build-for-universal2

Conversation

@DarthMDev

Copy link
Copy Markdown
Contributor

This improves performance and battery life on apple silicon macs as it doesn't need to emulate x86_64, also it still is compatible with intel macs

@HotaruBlaze

HotaruBlaze commented Jun 24, 2023 •

Copy link
Copy Markdown
Contributor

This was talked about in the discord before and wasn't done due to the build being something like 300mb, is this still the case?

Also your targeting main, and you should be targeting development branch.

@DarthMDev

DarthMDev commented Jun 24, 2023 •

Copy link
Copy Markdown
Contributor Author

This was talked about in the discord before and wasn't dome due to the build being something like 300mb, is this still the case?

Also your targeting main, and you should be targeting development branch.

shoot it is pretty huge in size hmm
what can be done to mitigate this issue?

@DarthMDev

Copy link
Copy Markdown
Contributor Author

looks like most of the size comes from python in the libs folder ? interesting..

@DarthMDev
DarthMDev changed the base branch from main to development June 24, 2023 04:47
@HotaruBlaze

Copy link
Copy Markdown
Contributor

I'm currently trying some stuff out, seeing what we can do about it, I have a couple of theory's to fix it,

@HotaruBlaze

Copy link
Copy Markdown
Contributor

The issue seems to be with Qt being the bulk of the build, been trying to see what i can do but so far running into dead ends

@HotaruBlaze

HotaruBlaze commented Jun 24, 2023 •

Copy link
Copy Markdown
Contributor

TLDR before reading: DO NOT MERGE THIS YET

After pushing this more and trying to see if I can get it to work, I'm not seeing an easy way around this, as this has already broken my MacOS's python pretty heavily attempting multiple things and getting nowhere but ill leave my notes explaining that at least for now, I really don't think this is a good move.

  1. Universal2 with python seems to be very buggy at least for me, as well as I don't see an easy way to build a universal2 image unless you have a venv specifically with only Universal2.

  2. The file size issue, 60MB -> 250-280MB is too much of a jump to justify this, this goes into way move detail at the end.

3) Due to the size issues as well as build quirks, it may be better to build a m1 specific build instead of depending on universal2 due to pip behaviour.
Forgot that Github Actions cant build M1 arm, only Linux arm builds are supported.


Now the main issue here, is the file size, this seems to be an issue with around py2app, python and Qt 5 & 6 causing a massive mess with the finished build.

Their seems to be a lot of junk included by the py2app build, such as below

phoenix@phoenixs-iMac-Pro lib % du -hs * | sort -rh | head -5
208M    python3.10
 43M    python310.zip
2.5M    tcl8.6
1.8M    tk8.6
292K    tcl8
phoenix@phoenixs-iMac-Pro lib % 

python310.zip is included in the build for seemingly no reason, now yes its 43M but 43M that from what i can tell, is just leftover.

However you will also notice python3.10 is 208M

phoenix@phoenixs-iMac-Pro python3.10 % du -hs * | sort -rh | head -5
193M    PyQt6
 15M    lib-dynload
304K    certifi
232K    qtwidgets
104K    config-3.10-darwin
phoenix@phoenixs-iMac-Pro python3.10 % 

As I mentioned before, theirs an issue with Qt5 & 6 being included in the build. and this is one of the big issues

phoenix@phoenixs-iMac-Pro python3.10 % find . -type f -size +5M -exec du -ah {} + | sort -hr | head -10
 19M    ./PyQt6/Qt6/plugins/multimedia/libffmpegmediaplugin.dylib
 10M    ./PyQt6/QtWidgets.abi3.so
8.6M    ./PyQt6/Qt6/lib/QtPdf.framework/Versions/A/QtPdf
8.1M    ./PyQt6/Qt6/lib/QtGui.framework/Versions/A/QtGui
6.1M    ./PyQt6/Qt6/lib/QtWidgets.framework/Versions/A/QtWidgets
5.8M    ./PyQt6/QtCore.abi3.so
5.8M    ./PyQt6/Qt6/lib/QtCore.framework/Versions/A/QtCore
5.5M    ./PyQt6/Qt6/lib/QtQuick.framework/Versions/A/QtQuick
5.2M    ./PyQt6/QtGui.abi3.so
phoenix@phoenixs-iMac-Pro python3.10 % 
phoenix@phoenixs-iMac-Pro python3.10 % find PyQt6 -type f | wc -l 
    2559

Theirs a lot of files included here, that quickly add up over time, What makes matters worse is a lot of these included files aren't actually used.

Trying to exclude all of these via py2app didn't seem to work, and we may have to start cleaning up the build before uploading it, however that seems pretty hacky, as well as it seemed to cause issues for me with breaking signing that py2app seemed to be doing.

@DarthMDev

DarthMDev commented Jun 24, 2023 •

Copy link
Copy Markdown
Contributor Author

Yeah I could see how about building for just arm64 under a build-arm64 script, the question is if it would work under an intel mac environment which is what github actions uses (for now)
I know the other way around works where an m1 can build for x64 only using rosetta2

@HotaruBlaze

HotaruBlaze commented Jun 24, 2023 •

Copy link
Copy Markdown
Contributor

Github Actions does not actually support building on M1 mac at this time, so yeah if we'd want to have a image close it would need to be universal2

@DarthMDev

DarthMDev commented Jun 24, 2023 •

Copy link
Copy Markdown
Contributor Author

if you were willing to switch i know cirrus ci uses an m1 environment , then we could just build both
that also might be painful though

@HotaruBlaze

Copy link
Copy Markdown
Contributor

To be honest, we may just need to figure out a way to fix Universal2, as x64 can build it, just cant test it. But we'd need a debloater or figure out where its breaking, as trying to lock down py2app didnt work, and Trying to use PyInstaller on macOS causes a segmentation fault 11

@spotlightishere

Copy link
Copy Markdown
Collaborator

Just wanted to chime in: Despite PyQt6 itself supporting universal wheels, Qt itself does not (as provided by the dependency PyQt6_Qt6). In this PR's current state the resulting launcher is universal, but it will play mix-and-match with framework architectures, and Qt will remain the native built architecture.


It appears that py2applet and pip both recognize the special universal2 arch (i.e. -arch x86_64 -arch arm64 where applicable), which is likely a better long-term solution so that we retain universal-specific fixes upstream.

One potential approach may be to manually create a universal version of PyQt6_Qt6 via lipo. It's difficult to enforce that pip always installs the universal2 tagged wheels due to upstream woes. I was able to create a universal wheel using the following:

Example wheel build script
# Download and unpack
pip3 download --only-binary=:all: --platform=macosx_13_0_x86_64 PyQt6_Qt6
pip3 download --only-binary=:all: --platform=macosx_13_0_arm64 PyQt6_Qt6
wheel unpack PyQt6_*arm64.whl --dest arm64
wheel unpack PyQt6_*x86_64.whl --dest x86_64

# We'll use x86_64 as our basis.
# As of writing, PyQt6_Qt6 specifies a minimum of 10.14 for x86_64, and 11.0 for arm64.
# We'll reuse this tag; it should be updated if this ever changes in the future.
wheel tags --platform-tag macosx_10_14_universal2 PyQt6_*x86_64.whl
wheel unpack PyQt6_*universal2.whl --dest universal

# https://stackoverflow.com/a/46020381
merge_frameworks() {
  # Remove the leading universal/ from this path.
  binary_path="${1##universal/}"
  lipo -create -arch x86_64 "x86_64/$binary_path" "arm64/$binary_path" -output "universal/$binary_path"
}
export -f merge_frameworks

# Iterate through all frameworks and libraries, and lipo together
find universal -perm +111 -type f -exec sh -c 'merge_frameworks "$1"' _ {} \;
wheel pack universal/PyQt6_*

# Finally, install our universal wheel.
pip3 install PyQt6_*universal2.whl

If this is desired, I would be happy to help out finding a way to integrate this - perhaps vendoring PyQt6 and publishing universal wheels via GitHub Package Registry. The resulting app binary is 341.4 MB via this method (❗) in which ~49 MB are PyQt6 and ~259 MB are from Qt itself. (This could surely be slimmed down, by removing i.e. QtPdf). I was unable to reproduce having a large zip.


If not, it may make sense to do per-architecture builds, given how large Qt unfortunately can be 😅 Via --platform=macosx_11_0_arm64 and so forth, x86_64 pip can be forced to download/install arm64 PyQt6.

@DarthMDev

Copy link
Copy Markdown
Contributor Author

the only problem is with github actions , per architecture builds cant be automated

@HotaruBlaze

Copy link
Copy Markdown
Contributor

I think part of the issue is that the universal version of Qt, does not seem to get trimmed unlike x64, maybe py2app does not understand what to do with the universal build?

My only issue with the universal build is the size, if we can figure out a fix for that, then we can deal with the rest of the issues. We also use a very small collection of qt modules

@HotaruBlaze

Copy link
Copy Markdown
Contributor

Interesting, i dont know why i didnt decide to look into this, Compressing the build like we usually do, Results in a ~110MB zip file.

This is slightly over x2 however its not as big as initially suspected

@HotaruBlaze

HotaruBlaze commented Jun 24, 2023 •

Copy link
Copy Markdown
Contributor

PS: The supplied .sh script provided as this PR does not work and would break CI, your removing name=NSO-RPC, that'd break Github Actions, aswell as
sed -i '' -e "s/archs=['x86_64'],/archs=['arm64'],/" setup.py
Does not seem to add anything or change the setup.py

@HotaruBlaze

HotaruBlaze commented Jun 24, 2023 •

Copy link
Copy Markdown
Contributor

Update: I just discovered a build bug around MacOS and Qt for NSO-RPC, investigating it now to see if i can figure out whats going on,

@DarthMDev

Copy link
Copy Markdown
Contributor Author

PS: The supplied .sh script provided as this PR does not work and would break CI, your removing name=NSO-RPC, that'd break Github Actions, aswell as sed -i '' -e "s/archs=['x86_64'],/archs=['arm64'],/" setup.py Does not seem to add anything or change the setup.py

hmm dang ill need to look into this more

@DarthMDev

Copy link
Copy Markdown
Contributor Author

hopefully this is the better method

@HotaruBlaze

Copy link
Copy Markdown
Contributor

Update: I just discovered a build bug around MacOS and Qt for NSO-RPC, investigating it now to see if i can figure out whats going on,

Odd. I cant get it to replicate, something was overriding the QtPy imports for app.py, replacing it with Qt6 versions, but doing it wrong, causing a build to fail on execution, however I cant get it to happen again.

@HotaruBlaze

Copy link
Copy Markdown
Contributor

hopefully this is the better method

You didnt update codesign, it still refrences app.app. it should be NSO-RPC.app
codesign --deep --force --sign - dist/NSO-RPC.app/Contents/MacOS/*

@DarthMDev

Copy link
Copy Markdown
Contributor Author

oh whoops lol

@HotaruBlaze

HotaruBlaze commented Jun 24, 2023 •

Copy link
Copy Markdown
Contributor

OK, So update,
Changing py2app to the following
python3 setup.py py2app -O2 --arch=universal2
Aswell as using the script provided by #86 (comment)

Gives me a working universal2 app at 402.3 MB (At least working on x64).

Now yes that's quite big, however when zipped up, it becomes 168 MB, So it compresses pretty well considering.

image

Note this also includes all the junk mentioned before about being included in Qt6

Edit: I however I need to get some sleep now, so I wont be able to check up on this more until later on.

@DarthMDev

Copy link
Copy Markdown
Contributor Author

This does sound better though so thats good!

This improves performance and battery life on apple silicon macs as it doesn't need to emulate x86_64, also it still is compatible with intel macs
@DarthMDev
DarthMDev force-pushed the build-for-universal2 branch from 431927e to 668cf19 Compare June 24, 2023 21:25
@DarthMDev

Copy link
Copy Markdown
Contributor Author

squashed my commits

@HotaruBlaze

HotaruBlaze commented Jun 25, 2023 •

Copy link
Copy Markdown
Contributor

Would you be able to compress a build of universal2 for me, and reply with the file size of the folder itself as well as the zip.
I'd like to compare build sizes and see if we can find any discrepancy's
This is the command: zip -yr mac-portable.zip NSO-RPC.app/

@DarthMDev

Copy link
Copy Markdown
Contributor Author

Sure ill do it when i can

@DarthMDev

DarthMDev commented Jun 26, 2023 •

Copy link
Copy Markdown
Contributor Author

for me with just running the script i get a 1.22 GB .app (using python3.9)
zipping it up i get 448.2 MB zip
(pyside6 seems to take up the most, 971.7 MB)

@HotaruBlaze

HotaruBlaze commented Jun 26, 2023 •

Copy link
Copy Markdown
Contributor

Testing out the debloating idea, I've been able to shave off some of the size, however I haven't tried moving these steps over to Github Actions yet. So the size isn't final. Ive also still to test it on a M1 mac. Reported as working by Delta

Original:
image
Debloated:
image

% file NSO-RPC.app/Contents/MacOS/NSO-RPC
image

@DarthMDev

Copy link
Copy Markdown
Contributor Author

Very nice!

@HotaruBlaze

Copy link
Copy Markdown
Contributor

Universal2 builds are done and will have a PR created shortly

Here are the final build sizes:
image

@HotaruBlaze

Copy link
Copy Markdown
Contributor

I've submitted a PR with some of the details provided here and submitted the final PR for it here: #89

I've also Co-Signed the commit, giving both @DarthMDev and @spotlightishere credit for their help! o7

@DarthMDev DarthMDev closed this Jun 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants