user2319066
user2319066

Reputation: 195

Separate arm64 and x86_64 compiled frameworks in Xcode

I have mylib.framework compiled using Kotlin/Native for arm64 and x86_64 architectures and embed arm64 architecture using Embedded Binaries in general tab. In test target I reference x86_64 framework using Link Binary With Libraries. Archive and compilation for devices works well, but compilaton process for test target references to arm64 framework. In general tab for test target I have no Embedded Binaries section.

How can I tell Xcode to use arm64 framework for device and archive build and for test target use x86_64 framework?

Upvotes: 0

Views: 685

Answers (2)

user2319066
user2319066

Reputation: 195

Now I use Xcode's "Run Script" in "Build Phases" tab with following content found in the web:

APP_PATH="${TARGET_BUILD_DIR}/${WRAPPER_NAME}"
# This script loops through the frameworks embedded in the application and
# removes unused architectures.
find "$APP_PATH" -name '*.framework' -type d | while read -r FRAMEWORK
do
    FRAMEWORK_EXECUTABLE_NAME=$(defaults read "$FRAMEWORK/Info.plist" CFBundleExecutable)
    FRAMEWORK_EXECUTABLE_PATH="$FRAMEWORK/$FRAMEWORK_EXECUTABLE_NAME"
    echo "Executable is $FRAMEWORK_EXECUTABLE_PATH"
    EXTRACTED_ARCHS=()
    for ARCH in $ARCHS
    do
        echo "Extracting $ARCH from $FRAMEWORK_EXECUTABLE_NAME"
        lipo -extract "$ARCH" "$FRAMEWORK_EXECUTABLE_PATH" -o "$FRAMEWORK_EXECUTABLE_PATH-$ARCH"
        EXTRACTED_ARCHS+=("$FRAMEWORK_EXECUTABLE_PATH-$ARCH")
    done
    echo "Merging extracted architectures: ${ARCHS}"
    lipo -o "$FRAMEWORK_EXECUTABLE_PATH-merged" -create "${EXTRACTED_ARCHS[@]}"
    rm "${EXTRACTED_ARCHS[@]}"
    echo "Replacing original executable with thinned version"
    rm "$FRAMEWORK_EXECUTABLE_PATH"
    mv "$FRAMEWORK_EXECUTABLE_PATH-merged" "$FRAMEWORK_EXECUTABLE_PATH"
done

Upvotes: 0

Kai Huppmann
Kai Huppmann

Reputation: 10775

First of all (if you do not already), you should use the gradle-plugin for Kotlin-Native / Kotlin-Multiplatform. The major advantage for your goal is that, with the right targets an dependencies it provides you a lot of tasks, each of which addresses exactly one of the your needs:

$ sh -c ". ./gradlew tasks"
...
compileDebugIos_arm64KotlinNative - Compiles Kotlin/Native source set 'main' into a framework
compileDebugIos_x64KotlinNative - Compiles Kotlin/Native source set 'main' into a framework
compileReleaseIos_arm64KotlinNative - Compiles Kotlin/Native source set 'main' into a framework
compileReleaseIos_x64KotlinNative - Compiles Kotlin/Native source set 'main' into a framework


You then declare different values per target for each task in your build settings 'User-defined-section' like that:

enter image description here

Last step is to add a build phase to your Xcode-Project that compiles the Kotlin code for exactly the needs of the given context, by dynamically using a fitting task name:

...
sh -c ". ./gradlew $KONAN_TASK"
...

This tutorial (https://www.raywenderlich.com/7357-ios-app-with-kotlin-native-getting-started) provides more details and sample code.

Upvotes: 1

Related Questions