Created
April 9, 2019 16:51
-
-
Save Wizermil/ba30d5515f4809d8ae82a28f59563a9c to your computer and use it in GitHub Desktop.
Script to compile spine runtime static library for emscripten
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" | |
ROOT_DIR=$(pwd) | |
WORKER=$(getconf _NPROCESSORS_ONLN) | |
BUILD_DIR="build/emscripten/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" | |
echo " " | |
cd "$ROOT_DIR" || exit 1 | |
if [ -d "$BUILD_DIR/output" ]; then | |
rm -rf "$BUILD_DIR/output" | |
fi | |
mkdir -p "$BUILD_DIR/output" | |
cp -R "$BUILD_DIR/source/spine-runtimes-$SPINE_VERSION/spine-cpp/spine-cpp" "$BUILD_DIR/output" | |
if [ -d "$BUILD_DIR/install" ]; then | |
rm -rf "$BUILD_DIR/install" | |
fi | |
mkdir -p "$BUILD_DIR/install" | |
export CFLAGS="-m32 -Os" | |
export CXXFLAGS="$CFLAGS -std=c++14 -stdlib=libc++ -fno-exceptions -fno-rtti" | |
export CXX="em++" | |
export AR="emar" | |
cd "$ROOT_DIR/$BUILD_DIR/output" || exit 1 | |
if [ -d "$BUILD_DIR/download" ]; then | |
rm -rf "$BUILD_DIR/download" | |
fi | |
echo "Compiling: spine emscripten" | |
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 emscripten" | |
echo " " | |
echo "Installing: spine emscripten" | |
mkdir -p "$ROOT_DIR/$BUILD_DIR/install/lib" | |
cp libspine.a "$ROOT_DIR/$BUILD_DIR/install/lib/libspine.a" | |
mkdir -p "$ROOT_DIR/$BUILD_DIR/install/include" | |
cp -R spine-cpp/include/spine "$ROOT_DIR/$BUILD_DIR/install/include" | |
echo "Installed: spine emscripten" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment