Skip to content

Instantly share code, notes, and snippets.

@obrhoff
Created July 6, 2024 17:15
Show Gist options
  • Save obrhoff/fd619d5526dbb3ef5211aebd2a5ce23f to your computer and use it in GitHub Desktop.
Save obrhoff/fd619d5526dbb3ef5211aebd2a5ce23f to your computer and use it in GitHub Desktop.

Revisions

  1. obrhoff created this gist Jul 6, 2024.
    155 changes: 155 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,155 @@
    #!/bin/bash

    VERSION="1.5.2"
    SDKVERSION="17.0"
    MINIOSVERSION="10.0"

    ###########################################################################
    #
    # Don't change anything under this line!
    #
    ###########################################################################

    # by default, we won't build for debugging purposes
    if [ "${DEBUG}" == "true" ]; then
    echo "Compiling for debugging ..."
    OPT_CFLAGS="-O0 -fno-inline -g"
    OPT_LDFLAGS=""
    OPT_CONFIG_ARGS="--enable-assertions --disable-asm"
    else
    OPT_CFLAGS="-Ofast -flto"
    OPT_LDFLAGS="-flto"
    OPT_CONFIG_ARGS=""
    fi

    ARCHS="arm64 x86_64"

    DEVELOPER=`xcode-select -print-path`
    DEVELOPER_SDK=`xcrun --sdk iphoneos --show-sdk-path`
    SIMULATOR_SDK=`xcrun --sdk iphonesimulator --show-sdk-path`

    cd "`dirname \"$0\"`"
    REPOROOT=$(pwd)

    # Where we'll end up storing things in the end
    OUTPUTDIR="${REPOROOT}/opus"
    mkdir -p ${OUTPUTDIR}/include
    mkdir -p ${OUTPUTDIR}/lib

    BUILDDIR="${REPOROOT}/build"

    # where we will keep our sources and build from.
    SRCDIR="${BUILDDIR}/src"
    mkdir -p $SRCDIR
    # where we will store intermediary builds
    INTERDIR="${BUILDDIR}/built"
    mkdir -p $INTERDIR

    ########################################

    cd $SRCDIR

    # Exit the script if an error happens
    set -e

    if [ ! -e "${SRCDIR}/opus-${VERSION}.tar.gz" ]; then
    echo "Downloading opus-${VERSION}.tar.gz"
    curl -LO http://downloads.xiph.org/releases/opus/opus-${VERSION}.tar.gz
    fi
    echo "Using opus-${VERSION}.tar.gz"

    tar zxf opus-${VERSION}.tar.gz -C $SRCDIR
    cd "${SRCDIR}/opus-${VERSION}"

    set +e # don't bail out of bash script if ccache doesn't exist
    CCACHE=`which ccache`
    if [ $? == "0" ]; then
    echo "Building with ccache: $CCACHE"
    CCACHE="${CCACHE} "
    else
    echo "Building without ccache"
    CCACHE=""
    fi
    set -e # back to regular "bail out on error" mode

    export ORIGINALPATH=$PATH

    for ARCH in ${ARCHS}
    do
    if [ "${ARCH}" == "x86_64" ]; then
    PLATFORM="iPhoneSimulator"
    SDK_PATH=$SIMULATOR_SDK
    EXTRA_CONFIG="--host=x86_64-apple-darwin"

    else
    PLATFORM="iPhoneOS"
    SDK_PATH=$DEVELOPER_SDK
    EXTRA_CONFIG="--host=aarch64-apple-darwin"
    fi

    EXTRA_CFLAGS="-arch ${ARCH} -miphoneos-version-min=${MINIOSVERSION}"

    mkdir -p "${INTERDIR}/${PLATFORM}${SDKVERSION}-${ARCH}.sdk"

    ./configure --disable-shared --enable-static --with-pic --disable-extra-programs --disable-doc --enable-float-approx ${EXTRA_CONFIG} \
    --prefix="${INTERDIR}/${PLATFORM}${SDKVERSION}-${ARCH}.sdk" \
    LDFLAGS="$LDFLAGS ${OPT_LDFLAGS} -fPIE -L${OUTPUTDIR}/lib -lSystem" \
    CFLAGS="$CFLAGS ${EXTRA_CFLAGS} ${OPT_CFLAGS} -fPIE -I${OUTPUTDIR}/include -isysroot ${SDK_PATH}"

    # Build the application and install it to the fake SDK intermediary dir
    # we have set up. Make sure to clean up afterward because we will re-use
    # this source tree to cross-compile other targets.
    make -j4
    make install
    make clean
    done

    ########################################

    echo "Build library..."

    # These are the libs that comprise libopus.
    OUTPUT_LIBS="libopus.a"
    for OUTPUT_LIB in ${OUTPUT_LIBS}; do
    INPUT_LIBS=""
    for ARCH in ${ARCHS}; do
    if [ "${ARCH}" == "x86_64" ]; then
    PLATFORM="iPhoneSimulator"
    else
    PLATFORM="iPhoneOS"
    fi
    INPUT_ARCH_LIB="${INTERDIR}/${PLATFORM}${SDKVERSION}-${ARCH}.sdk/lib/${OUTPUT_LIB}"
    if [ -e $INPUT_ARCH_LIB ]; then
    INPUT_LIBS="${INPUT_LIBS} ${INPUT_ARCH_LIB}"
    fi
    done
    # Combine the three architectures into a universal library.
    if [ -n "$INPUT_LIBS" ]; then
    lipo -create $INPUT_LIBS \
    -output "${OUTPUTDIR}/lib/${OUTPUT_LIB}"
    else
    echo "$OUTPUT_LIB does not exist, skipping (are the dependencies installed?)"
    fi
    done

    for ARCH in ${ARCHS}; do
    if [ "${ARCH}" == "x86_64" ]; then
    PLATFORM="iPhoneSimulator"
    else
    PLATFORM="iPhoneOS"
    fi
    cp -R ${INTERDIR}/${PLATFORM}${SDKVERSION}-${ARCH}.sdk/include/* ${OUTPUTDIR}/include/
    if [ $? == "0" ]; then
    # We only need to copy the headers over once. (So break out of forloop
    # once we get first success.)
    break
    fi
    done

    ####################
    echo "Building done."
    echo "Cleaning up..."
    rm -fr ${INTERDIR}
    rm -fr "${SRCDIR}/opus-${VERSION}"
    rm -rf "${BUILDDIR}"
    echo "Done."