| 1 |
#!/usr/bin/env bash |
| 2 |
|
| 3 |
set -e |
| 4 |
|
| 5 |
function print_help { |
| 6 |
local SELF_NAME |
| 7 |
SELF_NAME=$(basename "$0") |
| 8 |
echo "$SELF_NAME. Combine multiple single-architecture libraries into universal libraries." |
| 9 |
echo "" |
| 10 |
echo "Usage:" |
| 11 |
echo " $SELF_NAME [options] <path>..." |
| 12 |
echo "" |
| 13 |
echo "Options:" |
| 14 |
echo " -h" |
| 15 |
echo " Print this help message." |
| 16 |
echo " -o" |
| 17 |
echo " Output directory to store the universal libraries." |
| 18 |
echo "" |
| 19 |
echo "Example:" |
| 20 |
echo " Given the follow directories:" |
| 21 |
echo " ├── arm64/" |
| 22 |
echo " │ ├── libfoo.a" |
| 23 |
echo " │ └── libbar.a" |
| 24 |
echo " └── x86_64/" |
| 25 |
echo " ├── libfoo.a" |
| 26 |
echo " └── libbar.a" |
| 27 |
echo "" |
| 28 |
echo " $SELF_NAME -o universal/ arm64/ x86_64/" |
| 29 |
echo "" |
| 30 |
echo " Each library is combined into a corresponding universal library:" |
| 31 |
echo " └── universal/" |
| 32 |
echo " ├── libfoo.a" |
| 33 |
echo " └── libbar.a" |
| 34 |
echo "" |
| 35 |
echo "Each <path> should contain one or more single-architecture static libraries." |
| 36 |
echo "All <path>s should contain the same number of libraries, with the same names." |
| 37 |
} |
| 38 |
|
| 39 |
OUTPUT_DIR="" |
| 40 |
while getopts "ho:" opt; do |
| 41 |
case ${opt} in |
| 42 |
h) |
| 43 |
print_help |
| 44 |
exit 1 |
| 45 |
;; |
| 46 |
o) |
| 47 |
OUTPUT_DIR="${OPTARG}" |
| 48 |
;; |
| 49 |
*) |
| 50 |
print_help |
| 51 |
exit 1 |
| 52 |
;; |
| 53 |
esac |
| 54 |
done |
| 55 |
|
| 56 |
shift $((OPTIND - 1)) |
| 57 |
|
| 58 |
PATHS=("$@") |
| 59 |
|
| 60 |
if [[ ! "${PATHS[*]}" ]]; then |
| 61 |
echo "One or more paths required." |
| 62 |
print_help |
| 63 |
exit 1 |
| 64 |
fi |
| 65 |
|
| 66 |
if [[ ! "${OUTPUT_DIR}" ]]; then |
| 67 |
echo "Output directory required." |
| 68 |
print_help |
| 69 |
exit 1 |
| 70 |
fi |
| 71 |
|
| 72 |
# Create the output directory, if it doesn't exist already. |
| 73 |
mkdir -p "${OUTPUT_DIR}" |
| 74 |
|
| 75 |
# Use the first path as the "leader" path. All paths should contain the same number of files with |
| 76 |
# the same names, so it doesn't matter which we chose. |
| 77 |
LEADER_PATH="${PATHS[0]}" |
| 78 |
|
| 79 |
# Loop through each file in the leader path. For each library we find, we'll collect additional |
| 80 |
# architectures in the other paths and combine them all into a universal library. |
| 81 |
for FILE in "${LEADER_PATH}"/*.a; do |
| 82 |
[ -f "${FILE}" ] || continue |
| 83 |
|
| 84 |
# The static library file name, like "libfilament.a" |
| 85 |
LIBRARY_NAME="${FILE##*/}" |
| 86 |
|
| 87 |
echo "Combining architectures for library: ${LIBRARY_NAME}" |
| 88 |
|
| 89 |
INPUT_FILES=("${LEADER_PATH}/${LIBRARY_NAME}") |
| 90 |
for ARCH_PATH in "${PATHS[@]:1}"; do |
| 91 |
THIS_FILE="${ARCH_PATH}/${LIBRARY_NAME}" |
| 92 |
if [[ -f "${THIS_FILE}" ]]; then |
| 93 |
INPUT_FILES+=("${THIS_FILE}") |
| 94 |
else |
| 95 |
echo "Error: ${THIS_FILE} does not exist." |
| 96 |
exit 1 |
| 97 |
fi |
| 98 |
done |
| 99 |
|
| 100 |
lipo "${INPUT_FILES[@]}" -create -output "${OUTPUT_DIR}/${LIBRARY_NAME}" |
| 101 |
done |