Skip to content

Commit 33333f9

Browse files
authored
Used UPX to reduce Windows x64 build size by ~23MB (~23%) (#332)
1 parent 339ef29 commit 33333f9

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ dist/
2222
# before PyInstaller builds the exe, so as to inject date/other infos into it.
2323
*.manifest
2424
*.spec
25+
.upx/
2526

2627
# Dev settings
2728
settings.toml

scripts/build.ps1

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@
22

33
& "$PSScriptRoot/compile_resources.ps1"
44

5+
$ProjectRoot = "$PSScriptRoot/.."
56
$SupportsSplashScreen = [System.Convert]::ToBoolean($(uv run --active python -c "import _tkinter; print(hasattr(_tkinter, '__file__'))"))
67

78
$arguments = @(
8-
"$PSScriptRoot/../src/AutoSplit.py",
9+
"$ProjectRoot/src/AutoSplit.py",
910
'--onefile',
1011
'--windowed',
11-
'--additional-hooks-dir=Pyinstaller/hooks',
1212
'--optimize 2', # Remove asserts and docstrings for smaller build
13-
"--add-data=pyproject.toml$([System.IO.Path]::PathSeparator).",
14-
'--icon=res/icon.ico')
13+
"--additional-hooks-dir=$ProjectRoot/Pyinstaller/hooks",
14+
"--add-data=$ProjectRoot/pyproject.toml$([System.IO.Path]::PathSeparator).",
15+
"--upx-dir=$PSScriptRoot/.upx"
16+
"--icon=$ProjectRoot/res/icon.ico")
1517
if ($SupportsSplashScreen) {
1618
# https://github.com/pyinstaller/pyinstaller/issues/9022
17-
$arguments += @('--splash=res/splash.png')
19+
$arguments += @("--splash=$ProjectRoot/res/splash.png")
1820
}
1921
if ($IsWindows) {
2022
$arguments += @(
@@ -25,10 +27,10 @@ if ($IsWindows) {
2527
Start-Process -Wait -NoNewWindow uv -ArgumentList $(@('run', '--active', 'pyinstaller') + $arguments)
2628

2729
If ($IsLinux) {
28-
Move-Item -Force $PSScriptRoot/../dist/AutoSplit $PSScriptRoot/../dist/AutoSplit.elf
30+
Move-Item -Force $ProjectRoot/dist/AutoSplit $ProjectRoot/dist/AutoSplit.elf
2931
If ($?) {
3032
Write-Host 'Added .elf extension'
3133
}
32-
chmod +x $PSScriptRoot/../dist/AutoSplit.elf
34+
chmod +x $ProjectRoot/dist/AutoSplit.elf
3335
Write-Host 'Added execute permission'
3436
}

scripts/install.ps1

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ If ($IsLinux) {
2929
}
3030
}
3131

32-
# Installing Python dependencies
32+
# Installing system dependencies
3333
If ($IsLinux) {
3434
If (-not $Env:GITHUB_JOB -or $Env:GITHUB_JOB -eq 'Build') {
3535
sudo apt-get update
@@ -38,8 +38,23 @@ If ($IsLinux) {
3838
}
3939
}
4040

41+
# UPX is only used by PyInstaller on Windows
42+
If ($IsWindows -and [System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture -eq 'X64') {
43+
$UPXVersion = '5.0.1'
44+
$UPXFolderName = "upx-$UPXVersion-win64"
45+
Write-Output "Installing $UPXFolderName"
46+
if (Test-Path "$PSScriptRoot/.upx") { Remove-Item $PSScriptRoot/.upx -Recurse }
47+
Invoke-WebRequest `
48+
-Uri https://github.com/upx/upx/releases/download/v$UPXVersion/$UPXFolderName.zip `
49+
-OutFile $Env:TEMP/$UPXFolderName.zip
50+
# Automatically install in a local untracked folder. This makes it easy to version and replicate on CI
51+
Expand-Archive $Env:TEMP/$UPXFolderName.zip $PSScriptRoot/.upx
52+
Move-Item $PSScriptRoot/.upx/$UPXFolderName/* $PSScriptRoot/.upx
53+
Remove-Item $PSScriptRoot/.upx/$UPXFolderName
54+
}
55+
4156
$prod = If ($Env:GITHUB_JOB -eq 'Build') { '--no-dev' } Else { }
42-
$lock = If ($Env:GITHUB_JOB) { '--locked' } Else { '--upgrade' }
57+
$lock = If ($Env:GITHUB_JOB) { '--locked' } Else { }
4358
Write-Output "Installing Python dependencies with: uv sync $prod $lock"
4459
uv sync --active $prod $lock
4560

0 commit comments

Comments
 (0)