forked from curl/curl-for-win
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_libclean.sh
executable file
·50 lines (43 loc) · 1.46 KB
/
_libclean.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
#!/bin/sh
# Copyright (C) Viktor Szakats. See LICENSE.md
# SPDX-License-Identifier: MIT
# shellcheck disable=SC3040
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:
# - strip `libname_la-` prefix added by autotools
# - 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=''
while [ "${1#--*}" != "${1:-}" ]; do
if [ "$1" = '--ar' ]; then
shift; AR="$1"; shift
elif [ "$1" = '--strip' ]; then
shift; strip="$1"; shift
fi
done
[ -z "${AR:-}" ] && exit 1
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)"
"${AR}" x --output="${tmp}" "${f}" # --output= option requires llvm-ar v15.0.0 or binutils
for o in "${tmp}"/*; do
n="$(printf '%s' "${o}" | sed -E \
-e 's/lib[a-z0-9]+_la-//g' \
-e 's/(\.cc\.obj|\.c\.obj|\.obj)$/.o/g')"
[ "${o}" != "${n}" ] && mv -n "${o}" "${n}"
done
[ -n "${strip}" ] && "${strip}" --enable-deterministic-archives --strip-debug "${tmp}"/*
rm "${f}"
# shellcheck disable=SC2046
"${AR}" crD "${f}" $(find "${tmp}" -type f | sort)
rm -r -f "${tmp}"
fi
done