Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to compress binaries with UPX #90

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
8 changes: 8 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ jobs:
codesign: '-'
codesign-prefix: 'com.example.'
codesign-options: 'runtime'
upx: ${{ startsWith(matrix.os, 'ubuntu-') && matrix.target != 'x86_64-pc-windows-gnu' && matrix.target != 'aarch64-unknown-linux-gnu' }}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See https://github.com/taiki-e/upload-rust-binary-action/actions/runs/12377523218/

Is the reason it doesn't work with aarch64 and windows-gnu because UPX doesn't support cross-compilation?

See https://github.com/taiki-e/upload-rust-binary-action/actions/runs/12377523218/job/34547228299?pr=90#step:6:296

Is it intentional that this is only tested in linux? Have there been any problems when tested otherwise?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the reason it doesn't work with aarch64 and windows-gnu because UPX doesn't support cross-compilation?

See https://github.com/taiki-e/upload-rust-binary-action/actions/runs/12377523218/job/34547228299?pr=90#step:6:296

It seems to work with aarch64 linux-gnu + build-tool=cargo.
https://github.com/taiki-e/upload-rust-binary-action/actions/runs/12377523218/job/34547228655?pr=90

Is it intentional that this is only tested in linux? Have there been any problems when tested otherwise?

This doesn't seem to describe why x86_64 windows is skipped.
https://github.com/taiki-e/upload-rust-binary-action/actions/runs/12377523218/job/34547233333?pr=90

Copy link
Author

@mondeja mondeja Dec 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to work with aarch64 linux-gnu + build-tool=cargo.

Added.

This doesn't seem to describe why x86_64 windows is skipped.

The output of the file command is the same when the exe is compressed with UPX or not:

./test-crate/target/release/test-crate.exe: PE32+ executable (console) x86-64 (stripped to external PDB), for MS Windows

See this run (compressing) and this run (not compressing). I don't know how to test it.

- name: Check action outputs
run: |
printf 'outputs.archive should not be empty\n'
Expand All @@ -121,6 +122,13 @@ jobs:

printf 'outputs.md5 should be a file\n'
test -f "${{ steps.upload-rust-binary-action.outputs.md5 }}"
- name: Check UPX
if: startsWith(matrix.os, 'ubuntu-') && matrix.target != 'x86_64-pc-windows-gnu' && matrix.target != 'aarch64-unknown-linux-gnu'
run: |
printf 'binary should be compressed with UPX\n'
target_dir="./test-crate/target/release"
tar -C "$target_dir" -xf "${{ steps.upload-rust-binary-action.outputs.tar }}"
test -n "$(file "$target_dir/test-crate" | grep 'no section header')"
- name: Check b2 output
if: ${{ contains(matrix.checksums || 'b2,sha256,sha512,sha1,md5', 'b2') }}
run: |
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Currently, this action is basically intended to be used in combination with an a
| codesign | false | Sign build products using `codesign` on macOS | String | |
| codesign-prefix | false | Prefix for the `codesign` identifier on macOS | String | |
| codesign-options | false | Specifies a set of option flags to be embedded in the code signature on macOS. See the `codesign` manpage for details. | String | |
| upx | false | Compress binaries using [UPX](https://upx.github.io) on some platforms | Boolean | `false` |

\[1] Required one of `token` input option or `GITHUB_TOKEN` environment variable. Not required when `dry-run` input option is set to `true`.<br>
\[2] This is optional but it is recommended that this always be set to clarify which target you are building for if macOS is included in the matrix because GitHub Actions changed the default architecture of macos-latest since macos-14.<br>
Expand Down
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ inputs:
codesign_options:
description: Alias for 'codesign-options'
required: false
upx:
description: Compress binaries using UPX on some platforms
required: false
default: 'false'

outputs:
archive:
Expand Down Expand Up @@ -171,3 +175,4 @@ runs:
INPUT_CODESIGN: ${{ inputs.codesign }}
INPUT_CODESIGN_PREFIX: ${{ inputs.codesign-prefix || inputs.codesign_prefix }}
INPUT_CODESIGN_OPTIONS: ${{ inputs.codesign-options || inputs.codesign_options }}
INPUT_UPX: ${{ inputs.upx }}
29 changes: 29 additions & 0 deletions main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ build() {
*) bail "unrecognized build tool '${build_tool}'" ;;
esac
}

do_codesign() {
if [[ -n "${INPUT_CODESIGN:-}" ]]; then
local codesign_options=(--sign "${INPUT_CODESIGN}")
Expand Down Expand Up @@ -384,6 +385,34 @@ case "${INPUT_TARGET:-}" in
;;
esac

# Compress binaries with UPX
if [[ "${INPUT_UPX:-}" = "true" ]]; then
compress_binaries() {
for bin_exe in "${bins[@]}"; do
x upx --best "${target_dir}/${bin_exe}"
done
}

case "${host_os}" in
windows)
choco install upx -y
compress_binaries
;;
linux)
if ! type -P upx >/dev/null; then
sudo apt-get install -y upx-ucl
fi
compress_binaries
;;
macos)
# MacOS is not currently supported by UPX
;;
*)
warn "UPX is not available on ${host_os}"
;;
esac
fi

case "${host_os}" in
macos)
if type -P codesign >/dev/null; then
Expand Down
Loading