-
Notifications
You must be signed in to change notification settings - Fork 207
/
_clean-lib.sh
executable file
·66 lines (58 loc) · 1.78 KB
/
_clean-lib.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/bin/sh
# Copyright (C) Viktor Szakats. See LICENSE.md
# SPDX-License-Identifier: MIT
# shellcheck disable=SC3040,SC2039
set -o errexit -o nounset; [ -n "${BASH:-}${ZSH_NAME:-}" ] && set -o pipefail
# Normalize object names inside an .a library, to create reproducible
# output across build systems:
# - change suffix to .o
# - alpha-sort
# - optionally strip objects when called with `--strip <strip-tool>`
#
# NOTE: This script does not support spaces in filenames.
strip=''
binutils=''
while [ "${1#--*}" != "${1:-}" ]; do
if [ "$1" = '--ar' ]; then
shift; AR="$1"; shift
elif [ "$1" = '--strip' ]; then
shift; strip="$1"; shift
elif [ "$1" = '--binutils' ]; then
shift; binutils="$1"; shift # accepted value: apple
fi
done
[ -z "${AR:-}" ] && exit 1
if [ "${binutils}" = 'apple' ]; then
_ar_opt='cr'
else
_ar_opt='crD'
fi
while [ -n "${1:-}" ]; do
f="$1"; shift
# Process .a files only, except .dll.a ones.
if [ "${f#*.a}" != "${f}" ] && \
[ "${f#*.dll.a}" = "${f}" ]; then
echo "! Normalizing library: '${f}'"
tmp="$(mktemp -d)"
if [ "${binutils}" = 'apple' ] || \
[ "${binutils}" = 'old' ]; then
ff="$(readlink -f "${f}")" # requires macOS Monterey
(
cd "${tmp}"
"${AR}" x "${ff}"
)
else
"${AR}" x --output="${tmp}" "${f}" # --output= option requires llvm-ar v15.0.0 or binutils
fi
for o in "${tmp}"/*; do
n="$(printf '%s' "${o}" | sed -E \
-e 's/(\.cc\.obj|\.c\.obj|\.obj)$/.o/g')"
[ "${o}" != "${n}" ] && mv -n "${o}" "${n}"
done
# shellcheck disable=SC2086
[ -n "${strip}" ] && "${strip}" ${_STRIPFLAGS_LIB:-} "${tmp}"/*
rm "${f}"
find "${tmp}" -type f | sort | tr '\n' '\0' | xargs -0 "${AR}" "${_ar_opt}" "${f}"
rm -r -f "${tmp:?}"
fi
done