MegaManX
MegaManX

Reputation: 8918

Compiling libarchive for iOS?

I have cloned project from https://github.com/libarchive/libarchive, and have build it for mac, both arm64 and x86_64. I want to use libarchive in my iOS application. I have seen few projects here and there, that do exactly this, but unfortunately they are 10+ years old, and I can't make them work.

Since it has been done, I know it is possible, but does anyone know how this can be done?

EDIT: As per comment: Well I will start at first step. I copied all the .c and .h files, including config.h. When I try to compile it says that lzma.h is missing. I tried a lot of stuff, like adding liblzma.tbd in link with binary libraries phase, but nothing. It seems stuck. I don't know where this lzma.h file should be coming from?

Upvotes: 0

Views: 196

Answers (1)

MegaManX
MegaManX

Reputation: 8918

After a lot of work, I have actually made it, libarchive for iOS as XCFramework. It supports arm64 device, arm64 simulator and x86_64 simulator. This is the script that builds libarchive:

#!/bin/bash

LZ4_FILE="lz4-1.9.4.tar.gz"
XZ_FILE="xz-5.6.1.tar.gz"
LIBARCHIVE_FILE="libarchive-3.7.2.tar.gz"
MIN_IOS=15.0

# Compiler settings for x86_64 simulator
CC_X86_64_SIM="$(xcrun --sdk iphonesimulator --find clang) -isysroot $(xcrun --sdk iphonesimulator --show-sdk-path) -arch x86_64 -mios-simulator-version-min=${MIN_IOS}"

# Compiler settings for arm64 simulator
CC_ARM64_SIM="$(xcrun --sdk iphonesimulator --find clang) -isysroot $(xcrun --sdk iphonesimulator --show-sdk-path) -arch arm64 -mios-simulator-version-min=${MIN_IOS}"

# Compiler settings for arm64 device
CC_ARM64_IOS="$(xcrun --sdk iphoneos --find clang) -isysroot $(xcrun --sdk iphoneos --show-sdk-path) -arch arm64 -miphoneos-version-min=${MIN_IOS}"

LIB_PATH="$(pwd)/ios/libs"
INCLUDE_PATH="$(pwd)/ios/include"
XCFRAMEWORK_PATH="$(pwd)/ios/xcframeworks"

cd "$(dirname "$0")"
rm -rf ios
mkdir -p "${LIB_PATH}"
mkdir -p "${INCLUDE_PATH}"
mkdir -p "${XCFRAMEWORK_PATH}"

remove_tar_gz_suffix() {
  local filename="$1"
  echo "${filename%.tar.gz}"
}

check_success()
{
    if [ $? -eq 0 ]; then
    echo "Succeeded"
    else
    echo "Failed"
    exit 1
    fi
}

create_xcframework()
{

    echo "Creating FAT binary for simulator"
    lipo -create "${LIB_PATH}/x86_64_sim_$1.a" "${LIB_PATH}/arm64_sim_$1.a" -o "${LIB_PATH}/sim_fat_$1.a"
    check_success

    echo "Creating XCFramework for $1"
    xcodebuild -create-xcframework \
    -library "${LIB_PATH}/sim_fat_$1.a" -headers "${INCLUDE_PATH}/${LIB}" \
    -library "${LIB_PATH}/arm64_ios_$1.a" -headers "${INCLUDE_PATH}/${LIB}" \
    -output "${XCFRAMEWORK_PATH}/$1.xcframework"
    check_success
    
    echo "Cleaning up individual architecture binaries"
    rm -f "${LIB_PATH}/sim_fat_$1.a" "${LIB_PATH}/arm64_$1.a"
}

compile_library()
{
    ARCH=$1
    CC=$2
    LIB_NAME=$3
    HOST=$4
    
    OUTPUT_PATH="$(pwd)/output/$ARCH"
    mkdir -p "$OUTPUT_PATH"
    
    if [ "$LIB_NAME" = "lz4" ]; then
    
        LZ4_FOLDER=$(remove_tar_gz_suffix ${LZ4_FILE})
        
        tar -xzf ${LZ4_FILE}
        check_success
        
        cd $LZ4_FOLDER
        
        export CC=$CC
        export CXX="${CC%clang}clang++"
        
        make -j4 install DESTDIR=${OUTPUT_PATH}
        check_success
        
        mkdir -p "${INCLUDE_PATH}/lz4"
        
        cd ..
        
        cp -r $(pwd)/output/$ARCH/usr/local/include/*.h "${INCLUDE_PATH}/lz4/"
        cp $(pwd)/output/$ARCH/usr/local/lib/*.a "${LIB_PATH}/${ARCH}_lz4.a"
        rm -rf $LZ4_FOLDER
        
    elif [ "$LIB_NAME" = "xz" ]; then
    
        XZ_FOLDER=$(remove_tar_gz_suffix ${XZ_FILE})
        tar -xzf ${XZ_FILE}
        
        cd $XZ_FOLDER
        
        export CC=$CC
        export CXX="${CC%clang}clang++"
        
        ./configure --disable-debug \
        --disable-dependency-tracking \
        --disable-silent-rules \
        --host=$HOST \
        --prefix=${OUTPUT_PATH}
        
        make -j4
        make install
        check_success
        
        mkdir -p "${INCLUDE_PATH}/xz"
        cp -r ${OUTPUT_PATH}/include/* "${INCLUDE_PATH}/xz/"
        cp ${OUTPUT_PATH}/lib/liblzma.a "${LIB_PATH}/${ARCH}_xz.a"
        cd ..
        rm -rf $XZ_FOLDER
        
    elif [ "$LIB_NAME" = "libarchive" ]; then
    
        LIBARCHIVE_FOLDER=$(remove_tar_gz_suffix ${LIBARCHIVE_FILE})
        tar -xzf ${LIBARCHIVE_FILE}
        
        cd $LIBARCHIVE_FOLDER
        
        export CC=$CC
        export CXX="${CC%clang}clang++"
        
        autoreconf -f -i
        CFLAGS="$CFLAGS -I${INCLUDE_PATH}/xz -I${INCLUDE_PATH}/lz4" LDFLAGS="$LDFLAGS -L${LIB_PATH}" ./configure --without-lzo2 \
        --without-nettle \
        --without-xml2 \
        --without-openssl \
        --without-expat \
        --host=$HOST \
        --prefix=${OUTPUT_PATH}
        
        make -j4
        make install
        check_success
        
        mkdir -p "${INCLUDE_PATH}/libarchive"
        cp -r ${OUTPUT_PATH}/include/* "${INCLUDE_PATH}/libarchive/"
        cp ${OUTPUT_PATH}/lib/libarchive.a "${LIB_PATH}/${ARCH}_libarchive.a"
        cd ..
        rm -rf $LIBARCHIVE_FOLDER
        
    else
        echo "Unsupported library: $LIB_NAME"
        exit 1
    fi
    
    rm -rf output
}

# Compile and create XCFrameworks for each library
for LIB in lz4 xz libarchive; do
    echo "Building $LIB for x86_64 simulator"
    compile_library x86_64_sim "$CC_X86_64_SIM" $LIB x86_64-apple-darwin
    
    echo "Building $LIB for arm64 simulator"
    compile_library arm64_sim "$CC_ARM64_SIM" $LIB arm64-apple-darwin
    
    echo "Building $LIB for arm64 device"
    compile_library arm64_ios "$CC_ARM64_IOS" $LIB arm64-apple-ios
    
    echo "Building XCFramework for $LIB"
    create_xcframework $LIB
done

# Clean up
rm -rf ios

For this to work we need to download latest versions of libarchive, xz tools and lz4 library. This script will cross compile it. Also set minimum supported iOS as needed.

If it fails, check if you are missing some tools, automake and pkgconfig are both needed and easily installed via homebrew.

Cheers!

Upvotes: 0

Related Questions