Skip to content

Commit

Permalink
Support universal macOS binary as universal-apple-darwin (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e authored Jan 10, 2023
1 parent 1b00370 commit ca9055e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 22 deletions.
1 change: 1 addition & 0 deletions .github/.cspell/project-dictionary.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
coreutils
gtar
lipo
mktemp
tmpdir
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Note: In this file, do not use the hard wrap in the middle of a sentence for com

## [Unreleased]

- Support universal macOS binary as `target: universal-apple-darwin`.

## [1.11.1] - 2022-12-28

- Fix installation of cross.
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,9 @@ jobs:
os: ubuntu-latest
- target: x86_64-apple-darwin
os: macos-latest
# Universal macOS binary is supported as universal-apple-darwin.
- target: universal-apple-darwin
os: macos-latest
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
Expand Down
72 changes: 50 additions & 22 deletions main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,13 @@ build_tool="${INPUT_BUILD_TOOL:-}"
if [[ -z "${build_tool}" ]]; then
build_tool="cargo"
if [[ "${host}" != "${target}" ]]; then
x rustup target add "${target}"
case "${target}" in
universal-apple-darwin) x rustup target add aarch64-apple-darwin x86_64-apple-darwin ;;
*) x rustup target add "${target}" ;;
esac
case "${target}" in
# https://github.com/cross-rs/cross#supported-targets
*windows-msvc | *windows-gnu | *darwin | *fuchsia | *redox) ;;
*-windows-msvc* | *-windows-gnu* | *-darwin* | *-fuchsia* | *-redox*) ;;
*)
# If any of these are set, it is obvious that the user has set up a cross-compilation environment on the host.
if [[ -z "$(eval "echo \${CARGO_TARGET_${target_upper}_LINKER:-}")" ]] && [[ -z "$(eval "echo \${CARGO_TARGET_${target_upper}_RUNNER:-}")" ]]; then
Expand Down Expand Up @@ -182,12 +185,6 @@ else
metadata=$(cargo metadata --format-version=1 --no-deps)
target_dir=$(jq <<<"${metadata}" -r '.target_directory')
fi
if [[ -n "${INPUT_TARGET:-}" ]]; then
build_options+=("--target" "${target}")
target_dir="${target_dir}/${target}/release"
else
target_dir="${target_dir}/release"
fi

strip=""
workspace_root=$(jq <<<"${metadata}" -r '.workspace_root')
Expand All @@ -211,23 +208,54 @@ if ! grep -Eq '^strip\s*=' "${workspace_root}/Cargo.toml"; then
fi
fi

case "${build_tool}" in
cargo) x cargo build "${build_options[@]}" ;;
cross)
if ! type -P cross &>/dev/null; then
x cargo install cross --locked
fi
x cross build "${build_options[@]}"
build() {
case "${build_tool}" in
cargo) x cargo build "${build_options[@]}" "$@" ;;
cross)
if ! type -P cross &>/dev/null; then
x cargo install cross --locked
fi
x cross build "${build_options[@]}" "$@"
;;
*) bail "unrecognized build tool '${build_tool}'" ;;
esac
}
do_strip() {
target_dir="$1"
if [[ -n "${strip:-}" ]]; then
for bin_exe in "${bins[@]}"; do
x "${strip}" "${target_dir}/${bin_exe}"
done
fi
}

case "${INPUT_TARGET:-}" in
'')
build
target_dir="${target_dir}/release"
do_strip "${target_dir}"
;;
universal-apple-darwin)
# Refs: https://developer.apple.com/documentation/apple-silicon/building-a-universal-macos-binary
build --target aarch64-apple-darwin
build --target x86_64-apple-darwin
aarch64_target_dir="${target_dir}/aarch64-apple-darwin/release"
x86_64_target_dir="${target_dir}/x86_64-apple-darwin/release"
target_dir="${target_dir}/${target}/release"
mkdir -p "${target_dir}"
do_strip "${aarch64_target_dir}"
do_strip "${x86_64_target_dir}"
for bin_exe in "${bins[@]}"; do
x lipo -create -output "${target_dir}/${bin_exe}" "${aarch64_target_dir}/${bin_exe}" "${x86_64_target_dir}/${bin_exe}"
done
;;
*)
build --target "${target}"
target_dir="${target_dir}/${target}/release"
do_strip "${target_dir}"
;;
*) bail "unrecognized build tool '${build_tool}'" ;;
esac

if [[ -n "${strip:-}" ]]; then
for bin_exe in "${bins[@]}"; do
x "${strip}" "${target_dir}/${bin_exe}"
done
fi

if [[ "${INPUT_TAR/all/${platform}}" == "${platform}" ]] || [[ "${INPUT_ZIP/all/${platform}}" == "${platform}" ]]; then
cwd=$(pwd)
tmpdir=$(mktemp -d)
Expand Down

0 comments on commit ca9055e

Please sign in to comment.