-
-
Save sergeytimoshin/ad26be9022fc57d61329bb82a791770b to your computer and use it in GitHub Desktop.
Script to compile spine runtime static library for macOS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
SPINE_VERSION="3.7.83" | |
ARCHS=("x86_64") | |
SDK_VERSION="10.14" | |
ROOT_DIR=$(pwd) | |
WORKER=$(getconf _NPROCESSORS_ONLN) | |
BUILD_DIR="build/macos/spine" | |
LOG_FILE="$ROOT_DIR/$BUILD_DIR/build.log" | |
if [ -d "$BUILD_DIR/download" ]; then | |
rm -rf "$BUILD_DIR/download" | |
fi | |
if [ -d "$BUILD_DIR/source" ]; then | |
rm -rf "$BUILD_DIR/source" | |
fi | |
if [ -f "$LOG_FILE" ]; then | |
rm "$LOG_FILE" | |
touch "$LOG_FILE" | |
fi | |
mkdir -p "$BUILD_DIR/download" | |
mkdir -p "$BUILD_DIR/source" | |
error() { | |
echo "$@" 1>&2 | |
} | |
fail() { | |
error "$@" | |
exit 1 | |
} | |
compile_c++() { | |
includes=("${!2}") | |
includeArg="" | |
if [ ${#includes} -ne 0 ]; then | |
for i in "${includes[@]}"; do | |
includeArg+="-I \"$i\" " | |
done | |
fi | |
if [ -f "$1" ]; then | |
eval "$CXX $CXXFLAGS -c \"$1\" $includeArg -o \"${1%.cpp}.o\"" >> "$LOG_FILE" 2>&1 || fail "$1 compilation error" | |
else | |
fail "$1 doesn't exist" | |
fi | |
} | |
ar_c++() { | |
lib_name="$1" | |
srcs=("${!2}") | |
objects="" | |
for f in "${srcs[@]}"; do | |
filename_obj="${f%.cpp}.o" | |
if [ ! -f "$filename_obj" ]; then | |
fail "$filename_obj doesn't exist" | |
else | |
objects+="\"$filename_obj\" " | |
fi | |
done | |
# echo "$AR -rcs \"$ROOT/$lib_name.a\" $objects" | |
eval "$AR -rcs \"$lib_name.a\" $objects" >> "$LOG_FILE" 2>&1 || fail "ar error" | |
} | |
build_static_library() { | |
lib_name="$1" | |
files=("${!2}") | |
includes=("${!3}") | |
N=$WORKER | |
for f in "${files[@]}"; do | |
((i=i%N)); ((i++==0)) && wait | |
compile_c++ "$f" includes[@] & | |
done | |
wait | |
ar_c++ "$lib_name" files[@] | |
} | |
echo "Downloading: spine" | |
curl -Lo "$BUILD_DIR/download/spine-$SPINE_VERSION.tar.gz" "https://github.com/EsotericSoftware/spine-runtimes/archive/$SPINE_VERSION.tar.gz" >> "$LOG_FILE" 2>&1 || fail "Error download spine" | |
echo "Downloaded: spine" | |
echo " " | |
echo "Uncompressing: spine" | |
tar zxf "$BUILD_DIR/download/spine-$SPINE_VERSION.tar.gz" -C "$BUILD_DIR/source" >> "$LOG_FILE" 2>&1 || fail "Error uncompress spine" | |
echo "Uncompressed: spine" | |
for ARCH in "${ARCHS[@]}"; do | |
echo " " | |
cd "$ROOT_DIR" || exit 1 | |
if [ -d "$BUILD_DIR/output/$ARCH" ]; then | |
rm -rf "$BUILD_DIR/output/$ARCH" | |
fi | |
mkdir -p "$BUILD_DIR/output/$ARCH" | |
cp -R "$BUILD_DIR/source/spine-runtimes-$SPINE_VERSION/spine-cpp/spine-cpp" "$BUILD_DIR/output/$ARCH" | |
if [ -d "$BUILD_DIR/install/$ARCH" ]; then | |
rm -rf "$BUILD_DIR/install/$ARCH" | |
fi | |
mkdir -p "$BUILD_DIR/install/$ARCH" | |
PLATFORM="MacOSX" | |
MIN_VERSION="-mmacosx-version-min" | |
BUILD_SDKROOT="/Applications/Xcode.app/Contents/Developer/Platforms/$PLATFORM.platform/Developer/SDKs/${PLATFORM}${SDK_VERSION}.sdk" | |
export CFLAGS="-arch $ARCH -pipe -isysroot $BUILD_SDKROOT $MIN_VERSION=10.12 -Ofast" | |
export CXXFLAGS="$CFLAGS -std=c++14 -stdlib=libc++ -fno-exceptions -fno-rtti" | |
export CXX="clang++" | |
export AR="ar" | |
cd "$ROOT_DIR/$BUILD_DIR/output/$ARCH" || exit 1 | |
if [ -d "$BUILD_DIR/download" ]; then | |
rm -rf "$BUILD_DIR/download" | |
fi | |
echo "Compiling: spine $ARCH" | |
files=() | |
while IFS='' read -r line; do | |
files+=("$line"); | |
done < <(find . -type f -name "*.cpp") | |
includes=( | |
"spine-cpp/include" | |
) | |
build_static_library "libspine" files[@] includes[@] | |
echo "Compiled: spine $ARCH" | |
echo " " | |
echo "Installing: spine $ARCH" | |
mkdir -p "$ROOT_DIR/$BUILD_DIR/install/$ARCH/lib" | |
cp libspine.a "$ROOT_DIR/$BUILD_DIR/install/$ARCH/lib/libspine.a" | |
mkdir -p "$ROOT_DIR/$BUILD_DIR/install/$ARCH/include" | |
cp -R spine-cpp/include/spine "$ROOT_DIR/$BUILD_DIR/install/$ARCH/include" | |
echo "Installed: spine $ARCH" | |
done | |
cd "$ROOT_DIR/$BUILD_DIR" || exit 1 | |
if [ -f "libspine.a" ]; then | |
rm -f "libspine.a" | |
fi | |
echo " " | |
echo "Creating Flat Lib: spine" | |
LIPO_CMD="lipo -create" | |
for ARCH in "${ARCHS[@]}"; do | |
if [ -f "$ROOT_DIR/$BUILD_DIR/install/$ARCH/lib/libspine.a" ]; then | |
LIPO_CMD="$LIPO_CMD $ROOT_DIR/$BUILD_DIR/install/$ARCH/lib/libspine.a" | |
fi | |
done | |
LIPO_CMD="$LIPO_CMD -output libspine.a" | |
eval "$LIPO_CMD" >> "$LOG_FILE" 2>&1 || fail "Error creation flat spine" | |
echo "Created Flat Lib: spine" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment