Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build a Fat Framework that includes Mac Catalyst?

How does one build a fat framework that includes the architectures necessary to build to Mac Catalyst apps?

like image 649
Rodrigo Gomez-Palacio Avatar asked Nov 05 '19 02:11

Rodrigo Gomez-Palacio


People also ask

What is catalyst for Mac?

With Mac Catalyst, you can make a Mac version of your iPad app. Click the Mac checkbox in your iPad app's project settings to configure the project to build both Mac and iPad versions of your app. The two apps share the same project and source code, making it easy to change your code in one place.

Is iOS simulator arm64?

The iOS simulator's architecture is x86_64 (the latest Xcode doesn't support an i386-based simulator that the answer refers to), whereas a real iOS device (such as an iPhone or iPad) can be arm64 or arm64e. So when building an app for the simulator you need to specify x86_64, not an ARM architecture.

How do I compile framework in Xcode?

In Xcode, select File ▸ New ▸ Project…. Then choose iOS ▸ Framework & Library ▸ Framework. Click Next.


1 Answers

Apple has introduced a (undocumented?) new target: x86_64-apple-ios13.0-macabi

How to build for this target depends on your frameworks build environment.

1) XCFramework

In case your framework is an Xcode project:

  • Select the target in Xcode
  • Select the "General" tab
  • Under "Deployment Info", tick the "Mac" checkbox: enter image description here
  • build

2) External Build

In case you are building your framework outside Xcode, e.g. a C lib, instead of building for x86_64 & iphonesimulator, build for the new target x86_64-apple-ios13.0-macabi & macosx.

Example for C Lib using make:

MIN_IOS_VERSION="10.0"
LIB_NAME= "theNameOfYourLib"

# The build function
build()
{
ARCH=$1
TARGET=$2
HOST=$3
SDK=$4
SDK_PATH=`xcrun -sdk ${SDK} --show-sdk-path`

export PREFIX=build/${ARCH}
export CFLAGS="-arch ${ARCH} -isysroot ${SDK_PATH} -miphoneos-version-min=${MIN_IOS_VERSION} -std=c99 -target ${TARGET}"
export LDFLAGS="-arch ${ARCH}"
export CC="$(xcrun --sdk ${SDK} -f clang) -arch ${ARCH} -isysroot ${SDK_PATH}"

PKG_CONFIG_ALLOW_CROSS=1 PKG_CONFIG_PATH=$PREFIX/lib/pkgconfig ./configure --host=${HOST} --prefix=$PREFIX

make
make install
}

# Build for all required architectures

build "armv7" "armv7-apple-ios" "arm-apple-darwin" "iphoneos" # MIN_IOS_VERSION must be one of arm7 supported ones to. Else remove this line.
build "arm64" "aarch64-apple-ios" "arm-apple-darwin" "iphoneos"
# build "x86_64" "x86_64-apple-ios" "x86_64-apple-darwin" "iphonesimulator" #obsolete due to x86_64-apple-ios13.0-macabi
build "x86_64" "x86_64-apple-ios13.0-macabi" "x86_64-apple-darwin" "macosx"
build "i386" "i386-apple-ios" "i386-apple-darwin" "iphonesimulator" # same as arm7:  MIN_IOS_VERSION must be one of arm7 supported ones.

# Now find all the artefacts created above (e.g. build/arm64/lib/${LIB_NAME}.a,  build/x86_64/lib/${LIB_NAME}.a ...) and merge them together to a fat lib using lipo

OUTPUT_DIR="fatLib"
lipo -create -output $OUTPUT_DIR/lib/${LIB_NAME}.a build/x86_64/lib/${LIB_NAME}.a build/arm64/lib/${LIB_NAME}.a build/armv7/lib/${LIB_NAME}.a build/i386/lib/${LIB_NAME}.a

# You may also need the header files
cp -R build/armv7/include/* $OUTPUT_DIR/include/

Note: You must/can not add slices for x86_64-apple-ios and x86_64-apple-ios13.0-macabito the fat lib. Both are x86_64. Use only the one for x86_64-apple-ios13.0-macabi.

like image 187
shallowThought Avatar answered Oct 04 '22 13:10

shallowThought