Skip to content

Commit

Permalink
add initial support universal binaries for mac target
Browse files Browse the repository at this point in the history
  • Loading branch information
vszakats committed Aug 29, 2023
1 parent b0ffc98 commit 438ba4f
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
- uses: actions/checkout@v3
- name: 'build'
run: |
export CW_CONFIG='${{ github.ref_name }}-mac'
export CW_CONFIG='${{ github.ref_name }}-mac-macuni'
export CW_REVISION='${{ github.sha }}'
sh -c ./_ci-mac-homebrew.sh
Expand Down
9 changes: 8 additions & 1 deletion _build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ set -o xtrace -o errexit -o nounset; [ -n "${BASH:-}${ZSH_NAME:-}" ] && set -o p
# mac build macOS target (requires macOS host)
# linux build Linux target (requires Linux host)
# musl build Linux target with musl CRT (for linux target) (default: gnu)
# macuni build a macOS universal package (for mac target)
#
# CW_JOBS
# Number of parallel make jobs. Default: 2
Expand Down Expand Up @@ -1045,7 +1046,7 @@ EOF
fi
touch -c -r "${_ref}" "${_fn}"

./_pkg.sh "${_ref}"
./_pkg.sh "${_ref}" 'unified'
fi
}

Expand All @@ -1064,12 +1065,18 @@ if [ "${_OS}" = 'win' ]; then
build_single_target x86
fi
elif [ "${_OS}" = 'mac' ]; then
# TODO: This method is suboptimal. We might want to build pure C
# projects in dual mode and only manual-merge libs that have
# ASM components.
if [ "${_BRANCH#*x64*}" = "${_BRANCH}" ]; then
build_single_target a64
fi
if [ "${_BRANCH#*a64*}" = "${_BRANCH}" ]; then
build_single_target x64
fi
if [ "${_BRANCH#*macuni*}" != "${_BRANCH}" ]; then
./_macuni.sh
fi
else
[ "${unamem}" = 'i686' ] && cpu='x86'
[ "${unamem}" = 'x86_64' ] && cpu='x64'
Expand Down
54 changes: 54 additions & 0 deletions _macuni.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env bash

# Copyright (C) Viktor Szakats. See LICENSE.md
# SPDX-License-Identifier: MIT

# shellcheck disable=SC3040,SC2039
set -o xtrace -o errexit -o nounset; [ -n "${BASH:-}${ZSH_NAME:-}" ] && set -o pipefail

# Merge unified packages made for different CPUs into a single
# one with universal macOS binaries supporting all these CPUs.

# Universal, per-OS package: Initialize
export _PKGSUFFIX_MACUNI="-universal-${_PKGOS}"
export _UNIPKG="curl-${CURL_VER_}${_REVSUFFIX}${_PKGSUFFIX_MACUNI}${_FLAV}"

# Universal, per-OS package: Build
export _NAM="${_UNIPKG}"
export _VER="${CURL_VER_}"
export _OUT="${_UNIPKG}"
export _BAS="${_UNIPKG}"
export _DST="${_UNIPKG}"

_ref='curl/CHANGES'

if [ ! -f "${_ref}" ]; then
# This can happen with CW_BLD partial builds.
echo '! WARNING: curl build missing. Skip packaging.'
else
rm -r -f "${_UNIPKG:?}"
mkdir -p "${_UNIPKG}"
unipkg="${_UNIPKG}"
rm -rf __dirs__.txt
find . -name '__macuni__.txt' | sort -u | while read -r f; do
d="$(dirname "${f}")" # get main directory, e.g. 'curl-8.0.0_1-aarch64-macos'
echo "${d}" >> __dirs__.txt
rsync --archive --update "${d}/" "${unipkg}"
done
rm -rf "${unipkg}/__macuni__.txt"
# Is it possible to merge .map files? Exclude them from universal packages for now.
find "${unipkg}" -name '*.map' -delete
# Walk through all executables and libraries we want to merge into universal.
find "${unipkg}" -mindepth 2 -type f \( -name '*.a' -o -perm +111 \) | while read -r f; do
sub="$(printf '%s' "${f}" | cut -c 2- | sed -E 's|^[^/]+||g')" # get subdir part, e.g. '/lib/libname.a'
in=()
while read -r d; do
in+=("${d}${sub}")
done <<< "$(cat __dirs__.txt)"
lipo -create -output "${f}" "${in[@]}"
# TODO: code sign
touch -r "${_ref}" "${f}"
done
rm -rf __dirs__.txt
./_pkg.sh "${_ref}" 'macuni'
fi
10 changes: 8 additions & 2 deletions _pkg.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ set -o xtrace -o errexit -o nounset; [ -n "${BASH:-}${ZSH_NAME:-}" ] && set -o p

cd "$(dirname "$0")"

mode="${2:-}"

if [ "${_HOSTOS}" = 'mac' ]; then
tar() { gtar "$@"; }
fi
Expand Down Expand Up @@ -113,11 +115,15 @@ if [ "${_NAM}" != "${_UNIPKG}" ]; then
if ! grep -q -a -F "${namver}" -- "${_BLD}"; then
echo "${namver}" >> "${_BLD}"
fi
else
elif [ "${mode}" = 'macuni' ] || [ "${_BRANCH#*macuni*}" = "${_BRANCH}" ]; then
create_pkg "$1" '.tar.xz'
if [ "${_OS}" = 'win' ]; then
create_pkg "$1" '.zip'
fi
fi

rm -r -f "${_DST:?}"
if [ "${mode}" = 'unified' ] && [ "${_BRANCH#*macuni*}" != "${_BRANCH}" ]; then
touch "${_DST}/__macuni__.txt"
else
rm -r -f "${_DST:?}"
fi
5 changes: 0 additions & 5 deletions _ul.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
# shellcheck disable=SC3040,SC2039
set -o errexit -o nounset; [ -n "${BASH:-}${ZSH_NAME:-}" ] && set -o pipefail

if [ "${_OS}" = 'mac' ]; then
# TODO: merge a64 and x64 with lipo
echo 'TODO: merge a64 and x64 with lipo'
fi

if [ "${_OS}" != 'win' ]; then
exit 0
fi
Expand Down

0 comments on commit 438ba4f

Please sign in to comment.