xcodebuild 14.2 create xcframework doesn't recognize mac catalyst static library build in rust - xcode

So I've build the following fat binary
➜ wallet-core git:(m/rust_fix) ✗ lipo -detailed_info build/local/catalyst/libwallet_core_rs.a
Fat header in: build/local/catalyst/libwallet_core_rs.a
fat_magic 0xcafebabe
nfat_arch 2
architecture x86_64
cputype CPU_TYPE_X86_64
cpusubtype CPU_SUBTYPE_X86_64_ALL
capabilities 0x0
offset 48
size 1038808
align 2^3 (8)
architecture arm64
cputype CPU_TYPE_ARM64
cpusubtype CPU_SUBTYPE_ARM64_ALL
capabilities 0x0
offset 1038856
size 2649760
align 2^3 (8)
Using the tutorial https://nadim.computer/posts/2022-02-11-maccatalyst.html
and use xcodebuild to build a xcframework from different fat libs in order to create a universal framework:
#!/bin/bash
set -e
TARGET_NAME="libwallet_core_rs.a"
TARGET_XCFRAMEWORK_NAME=../swift/WalletCoreRs.xcframework
BUILD_FOLDER=../build/local
CRATE="wallet-core-rs"
HEADER_NAME="WalletCoreRSBindgen.h"
create_xc_framework() {
rm -rf $TARGET_XCFRAMEWORK_NAME
xcodebuild -create-xcframework -library $BUILD_FOLDER/$TARGET_NAME -library $BUILD_FOLDER/darwin_universal/$TARGET_NAME -library $BUILD_FOLDER/aarch64-apple-ios/release/$TARGET_NAME -library $BUILD_FOLDER/catalyst/$TARGET_NAME -output $TARGET_XCFRAMEWORK_NAME
}
cd rust
echo "Generating Native targets"
CARGO_TARGET_DIR=$BUILD_FOLDER/ cargo build --release
CARGO_TARGET_DIR=$BUILD_FOLDER cargo build --target wasm32-unknown-emscripten --release
if [[ `uname` == "Darwin" ]]; then
echo "Generating Android targets"
CARGO_TARGET_DIR=$BUILD_FOLDER/ cargo build --target aarch64-linux-android --release
CARGO_TARGET_DIR=$BUILD_FOLDER/ cargo build --target armv7-linux-androideabi --release
CARGO_TARGET_DIR=$BUILD_FOLDER/ cargo build --target x86_64-linux-android --release
CARGO_TARGET_DIR=$BUILD_FOLDER/ cargo build --target i686-linux-android --release
echo "Generating iOS targets"
CARGO_TARGET_DIR=$BUILD_FOLDER cargo build --target aarch64-apple-ios --release
CARGO_TARGET_DIR=$BUILD_FOLDER cargo build --target aarch64-apple-ios-sim --release
CARGO_TARGET_DIR=$BUILD_FOLDER cargo build --target x86_64-apple-ios --release
CARGO_TARGET_DIR=$BUILD_FOLDER cargo build --target aarch64-apple-darwin --release
CARGO_TARGET_DIR=$BUILD_FOLDER cargo build --target x86_64-apple-darwin --release
CARGO_TARGET_DIR=$BUILD_FOLDER cargo +nightly build -Z build-std --target aarch64-apple-ios-macabi --release --lib
CARGO_TARGET_DIR=$BUILD_FOLDER cargo +nightly build -Z build-std --target x86_64-apple-ios-macabi --release --lib
lipo $BUILD_FOLDER/x86_64-apple-ios/release/$TARGET_NAME $BUILD_FOLDER/aarch64-apple-ios-sim/release/$TARGET_NAME -create -output $BUILD_FOLDER/$TARGET_NAME
mkdir -p $BUILD_FOLDER/darwin_universal
lipo $BUILD_FOLDER/x86_64-apple-darwin/release/$TARGET_NAME $BUILD_FOLDER/aarch64-apple-darwin/release/$TARGET_NAME -create -output $BUILD_FOLDER/darwin_universal/$TARGET_NAME
mkdir -p $BUILD_FOLDER/catalyst
lipo $BUILD_FOLDER/aarch64-apple-ios-macabi/release/$TARGET_NAME $BUILD_FOLDER/x86_64-apple-ios-macabi/release/$TARGET_NAME -create -output $BUILD_FOLDER/catalyst/$TARGET_NAME
create_xc_framework
fi
cbindgen --crate $CRATE --output ../src/rust/bindgen/$HEADER_NAME
cd -
cp build/local/release/${TARGET_NAME} build/local/lib/
Everything was working fine until XCode 14.2 start to spit out this spurious error:
error: unable to determine the platform for the given binary '$HOME/Documents/C++/wallet-core/build/local/catalyst/libwallet_core_rs.a'; check your deployment version settings
lipo tells me that the build/local/catalyst/libwallet_core_rs.ais perfectly valid fat binary.
That's what gives rustc spec info about the aarch64 target:
➜ wallet-core git:(m/rust_fix) ✗ rustc -Z unstable-options --target=aarch64-apple-ios-macabi --print target-spec-json | jq
{
"abi": "macabi",
"abi-return-struct-as-int": true,
"arch": "aarch64",
"archive-format": "darwin",
"bitcode-llvm-cmdline": "-triple\u0000arm64-apple-ios-macabi\u0000-emit-obj\u0000-disable-llvm-passes\u0000-Os\u0000",
"cpu": "apple-a12",
"data-layout": "e-m:o-i64:64-i128:128-n32:64-S128",
"debuginfo-kind": "dwarf-dsym",
"default-dwarf-version": 2,
"dll-suffix": ".dylib",
"dynamic-linking": true,
"eh-frame-header": false,
"emit-debug-gdb-scripts": false,
"features": "+neon,+fp-armv8,+apple-a12",
"forces-embed-bitcode": true,
"frame-pointer": "non-leaf",
"function-sections": false,
"has-rpath": true,
"is-builtin": true,
"is-like-osx": true,
"link-env": [
"ZERO_AR_DATE=1"
],
"link-env-remove": [
"IPHONEOS_DEPLOYMENT_TARGET"
],
"linker-is-gnu": false,
"lld-flavor": "darwin",
"llvm-target": "arm64-apple-ios-macabi",
"max-atomic-width": 128,
"os": "ios",
"pre-link-args": {
"gcc": [
"-target",
"arm64-apple-ios-macabi"
],
"ld": [
"-arch",
"arm64",
"-platform_version",
"mac-catalyst",
"7.0",
"7.0"
],
"ld64.lld": [
"-arch",
"arm64",
"-platform_version",
"mac-catalyst",
"7.0",
"7.0"
]
},
"split-debuginfo": "packed",
"supported-split-debuginfo": [
"packed",
"unpacked",
"off"
],
"target-family": [
"unix"
],
"target-pointer-width": "64",
"vendor": "apple"
}
Everything outside of catalyst works well, but I wonder how to fix this error if anyone encountered it.

Related

Static library compiled for x86 macos looks like an iPhone simulator library

I have a Cgo library that I'm trying to build for Darwin. My ultimate goal is to have an xcframework that contains versions of the library for macos and iOS, both in x86 and arm64 versions.
My invocation to build the Cgo library looks like this:
CGO_ENABLED=1 \
GOOS=darwin \
GOARCH=amd64 \
SDK=macosx \
CC=$(ROOT_DIR)clangwrap.sh \
go build -buildmode=c-archive -tags ios -o $(MACOS_OUT)/x86_64/mylib.a .
clangwrap.sh is a simple script to invoke clang based on the SDK:
SDK_PATH=`xcrun --sdk $SDK --show-sdk-path`
CLANG=`xcrun --sdk $SDK --find clang`
if [ "$GOARCH" == "amd64" ]; then
CARCH="x86_64"
elif [ "$GOARCH" == "arm64" ]; then
CARCH="arm64"
fi
MIN_VERSION=""
if [ "$SDK" == "macosx" ]; then
MIN_VERSION="-mmacosx-version-min=10.15"
elif [ "$SDK" == "iphoneos" ] || [ "$SDK" == "iphonesimulator" ]; then
MIN_VERSION="-mios-version-min=10.0"
fi
exec $CLANG -arch $CARCH -isysroot $SDK_PATH $MIN_VERSION "$#"
The issue I'm having is that xcodebuild -create-xcframework complains that there is a duplicate library for x86-ios-simulator. If I look at the macos binary with otool, I see that it seems to be an ios binary:
> otool -l lib/darwin/macos/x86_64/mylib.a
Load command 1
cmd LC_VERSION_MIN_IPHONEOS
cmdsize 16
version 10.0
sdk 15.2
Load command 2
cmd LC_SYMTAB
cmdsize 24
symoff 6312
nsyms 9
stroff 6456
strsize 184
Any idea on how to produce a proper macos binary?

Unable to build Docker image using new Mac M1

I am attempting to build a Docker image for my application to use within Integration tests.
The image can be built fine on my old 2017 Macbook but fails when trying on my new Macbook with the M1 chip.
The error I receive is:
unable to build image:
The command '/bin/sh -c make build' returned a non-zero code: 2
{"version": "TEST", "output": "Step 1/9 : FROM golang:1.15.3-alpine3.12 AS builder---> 9701aa6ab80a
Step 2/9 : RUN apk update && apk add gcc make git libc-dev ---> Using cache ---> 87ff8d250e2d
Step 3/9 : ADD ./ /src/ ---> Using cache ---> ef95bb030ff7
Step 4/9 : WORKDIR /src/ ---> Using cache\n ---> 3b982c9ab004
Step 5/9 : RUN make build ---> Running in f7596e65a80b\u001b[91m# github.com/qadre/huski.go\n/usr/local/go/pkg/tool/linux_arm64/link: running gcc failed: exit status 1
collect2: fatal error: cannot find 'ld'\ncompilation terminated.\n\n\u001b[0m\u001b[91
mmake: *** [Makefile:8: build] Error 2\n\u001b[0mRemoving intermediate container f7596e65a80b\n"}
My make build is
build:
#go build -race -o huski-go -ldflags="-X 'main.Version=${VERSION}'"
When I run ld -v I get:
#(#)PROGRAM:ld PROJECT:ld64-609.8 BUILD 15:07:46 Dec 18 2020
configured to support archs: armv6 armv7 armv7s arm64 arm64e arm64_32
i386 x86_64 x86_64h armv6m armv7k armv7m armv7em LTO support using:
LLVM version 12.0.0, (clang-1200.0.32.29) (static support for 27,
runtime is 27) TAPI support using: Apple TAPI version 12.0.0
(tapi-1200.0.23.5)
Has anyone encountered this with the new Macbooks?
After some investigation, I found out this seemed to be an issue when using the Alpine container for my Go application. To fix this I had to add the binutils-gold dependency within my Dockerfile.
My Dockerfile now looks like the below and has fixed the issue:
FROM golang:1.15.3-alpine3.12 AS builder
RUN apk update && apk add gcc make git libc-dev binutils-gold
ADD ./ /src/
WORKDIR /src/
RUN make build
FROM alpine:3.12
COPY --from=builder /src/static /app/static
COPY --from=builder /src/huski-go /app/
ENTRYPOINT ["/app/huski-go"]
You can read more about this where I found the answer here: https://github.com/nodejs/node/issues/4212
You should build in this way:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build ...
this command allows u to build a x86 image in arm

How to create a fat binary on xcode 10

I am trying to make a fat binary of my library using XCode 10 (10A255) Using Fastlane:
xcodebuild(
workspace: "Mylibrary.xcworkspace",
scheme: "Mylibrary",
configuration: "Release",
clean: true,
archive: true
)
But running lipo -i ~/Mylibrary.framework/Mylibrary I only get armv7 arm64 that does not run on simulators.
Does anyone know the reason why this happens?
PS: The same command is working on Xcode 9.4 and lipo -i prints i386 x86_64 armv7 arm64 which works fine.

Error when I try to export adhoc with bitcode

I get this error when I do adhoc export from xcode after I have archived the project:
An error occurred during export
Failed to verify bitcode in Someframework.framework/Someframework
Cannot extract bundle from /var/folders/rg/8ydk5h297ng50z_3dpkr5jx00000gn/T/XcodeDistPipeline.5rb/Root/Payload/...
But when I export for the appstore it completes as expected
I was able to solve it by adding the magic compilation flags:
BITCODE_GENERATION_MODE bitcode
Make sure all of your framework build only for device. Please stripe out simulator architecture before exporting framework build.
If you are creating build using Jekins following script might work for you
if [ -d ./${BUILD_DIR}/YOURFramework.framework ] && lipo ./${BUILD_DIR}/YOURFramework.framework/YOURFramework.framework -verify_arch x86_64; then
lipo -remove i386 ./${BUILD_DIR}/YOURFramework.framework/YOURFramework.framework -output ./${BUILD_DIR}/YOURFramework.framework/YOURFramework.framework
lipo -remove x86_64 ./${BUILD_DIR}/YOURFramework.framework/YOURFramework.framework -output ./${BUILD_DIR}/YOURFramework.framework/YOURFramework.framework

How to make Xcode Run Script x86_64 compatible

Hello hello community!
So here is my issue, welll not really an issue but the following Run Script compiles my static library and it works great! my only issue is that it doesnt compile it for the simulator and i get a x86_64 error. I know that i could just edit this code to make it compatible can someone tell me what i need to do??
# define output folder environment variable
UNIVERSAL_OUTPUTFOLDER=${BUILD_DIR}/${CONFIGURATION}-universal
# Step 1. Build Device and Simulator versions
xcodebuild -target ${PROJECT_NAME} ONLY_ACTIVE_ARCH=NO -configuration ${CONFIGURATION} -sdk iphoneos BUILD_DIR="${BUILD_DIR}"
BUILD_ROOT="${BUILD_ROOT}" xcodebuild -target ${PROJECT_NAME} -configuration ${CONFIGURATION} -sdk iphonesimulator -arch i386 BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}"
# make sure the output directory exists
mkdir -p "${UNIVERSAL_OUTPUTFOLDER}"
# Step 2. Create universal binary file using lipo
lipo -create -output "${UNIVERSAL_OUTPUTFOLDER}/lib${PROJECT_NAME}.a" "${BUILD_DIR}/${CONFIGURATION}-iphoneos/lib${PROJECT_NAME}.a" "${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/lib${PROJECT_NAME}.a"
# Last touch. copy the header files. Just for convenience
cp -R "${BUILD_DIR}/${CONFIGURATION}-iphoneos/include" "${UNIVERSAL_OUTPUTFOLDER}"
In referencing to a "build a framework" project done here. I was able to find that i can add additional builds just by adding an additional "-arch" parameter. and just make the xcodebuild command
xcodebuild -target ${PROJECT_NAME} -configuration ${CONFIGURATION} -sdk iphonesimulator -arch i386 -arch x86_86 BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}"
and it solved my problem! just in case anyone had a slow moment like i did. lol

Resources