Skip to content

Commit

Permalink
build fat libgit2 library for mac
Browse files Browse the repository at this point in the history
  • Loading branch information
DivineDominion committed Jun 1, 2021
1 parent bcccf31 commit 8c46173
Showing 1 changed file with 153 additions and 24 deletions.
177 changes: 153 additions & 24 deletions script/update_libgit2
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,161 @@ set -e
# ~/.MacOSX/environment.plist
PATH="/usr/local/bin:$PATH"

if [ "External/libgit2.a" -nt "External/libgit2" ]
then
echo "No update needed."
exit 0
fi
SCRIPT_DIR=$(dirname "$0")
source "${SCRIPT_DIR}/xcode_functions.sh"

cd "External/libgit2"
function setup_build_environment ()
{
# augment path to help it find cmake installed in /usr/local/bin,
# e.g. via brew. Xcode's Run Script phase doesn't seem to honor
# ~/.MacOSX/environment.plist
PATH="/usr/local/bin:$PATH"

if [ -d "build" ]; then
pushd "$SCRIPT_DIR/.." > /dev/null
ROOT_PATH="$PWD"
popd > /dev/null

CLANG="/usr/bin/xcrun clang"
CC="${CLANG}"
CPP="${CLANG} -E"

# We need to clear this so that cmake doesn't have a conniption
MACOSX_DEPLOYMENT_TARGET=""

XCODE_MAJOR_VERSION=$(xcode_major_version)
XCODE_MINOR_VERSION=$(xcode_minor_version)

CAN_BUILD_ARM="0"

# Determine if we can be building for ARM Macs
if [ "${XCODE_MAJOR_VERSION}" -ge "12" ] && [ "${XCODE_MINOR_VERSION}" -ge "2" ]
then
CAN_BUILD_ARM="1"
fi

ARCHS="x86_64"
if [ "${CAN_BUILD_ARM}" -eq "1" ]
then
ARCHS="${ARCHS} arm64 arm64e"
fi
}

function build_all_archs ()
{
setup_build_environment

local setup=$1
local build_arch=$2
local finish_build=$3

# run the prepare function
eval $setup

echo "Building for ${ARCHS}"

for ARCH in ${ARCHS}
do
PLATFORM="macos"
SDKVERSION=$(ios_sdk_version)

# if [ "${ARCH}" == "arm64" ]
# then
# HOST="aarch64-apple-darwin"
# else
# HOST="${ARCH}-apple-darwin"
# fi

SDKNAME="${PLATFORM}${SDKVERSION}"
SDKROOT="$(ios_sdk_path ${SDKNAME})"

echo "Building ${LIBRARY_NAME} for ${SDKNAME} ${ARCH}"
echo "Please stand by..."

# run the per arch build command
eval $build_arch
done

# finish the build (usually lipo)
eval $finish_build
}

function setup ()
{
if [ "${ROOT_PATH}/External/libgit2-mac/libgit2-mac.a" -nt "${ROOT_PATH}/External/libgit2" ]
then
echo "No update needed."
exit 0
fi

LIBRARY_NAME="libgit2"
LIB_PATH="${ROOT_PATH}/External/libgit2-mac"
rm -rf "${LIB_PATH}"

pushd "${ROOT_PATH}/External/libgit2" > /dev/null
}

function build_libgit2 ()
{
rm -rf "build"
fi

mkdir build
cd build

cmake -DBUILD_SHARED_LIBS:BOOL=OFF \
-DLIBSSH2_INCLUDE_DIRS:PATH=/usr/local/include/ \
-DBUILD_CLAR:BOOL=OFF \
-DTHREADSAFE:BOOL=ON \
..
cmake --build .

product="libgit2.a"
install_path="../../${product}"
if [ "${product}" -nt "${install_path}" ]; then
cp -v "${product}" "${install_path}"
fi
mkdir "build"

pushd "build" > /dev/null

# LOL Cmake
#if [ "${ARCH}" != "i386" ] && [ "${ARCH}" != "x86_64" ]
#then
#SYS_ROOT="-DCMAKE_OSX_SYSROOT=${SDKROOT}"
#fi

# install the each built arch somewhere sane
INSTALL_PREFIX="${LIB_PATH}/${SDKNAME}-${ARCH}.sdk"

mkdir -p "${INSTALL_PREFIX}"

LOG="${INSTALL_PREFIX}/build-libgit2.log"
echo "$LOG"


# cmake -DBUILD_SHARED_LIBS:BOOL=OFF \
# -DLIBSSH2_INCLUDE_DIRS:PATH=/usr/local/include/ \
# -DBUILD_CLAR:BOOL=OFF \
# -DTHREADSAFE:BOOL=ON \
# ..
# cmake --build .

cmake \
-DCMAKE_C_COMPILER_WORKS:BOOL=ON \
-DBUILD_SHARED_LIBS:BOOL=OFF \
-DCMAKE_PREFIX_PATH:PATH="${ROOT_PATH}/External/libssh2-mac/bin/${SDKNAME}-${ARCH}.sdk" \
-DPKG_CONFIG_USE_CMAKE_PREFIX_PATH:BOOL=ON \
-DCMAKE_INSTALL_PREFIX:PATH="${INSTALL_PREFIX}/" \
-DBUILD_CLAR:BOOL=OFF \
-DTHREADSAFE:BOOL=ON \
-DCURL:BOOL=OFF \
-DCMAKE_C_FLAGS:STRING="-fembed-bitcode" \
"${SYS_ROOT}" \
-DCMAKE_OSX_ARCHITECTURES:STRING="${ARCH}" \
.. >> "${LOG}" 2>&1
cmake --build . --target install >> "${LOG}" 2>&1

# push the built library into the list
BUILT_LIB_PATHS+=("${INSTALL_PREFIX}/lib/libgit2.a")
popd > /dev/null
}

function fat_binary ()
{
echo "Building fat binary..."

lipo -create "${BUILT_LIB_PATHS[@]}" -output "${ROOT_PATH}/External/libgit2-mac/libgit2-mac.a"

echo "Building done."

popd > /dev/null
}


build_all_archs setup build_libgit2 fat_binary


echo "libgit2 has been updated."

0 comments on commit 8c46173

Please sign in to comment.