golang/go

cmd/cgo: does not compile for iOS simulator arm64

Open

#57,442 opened on Dec 22, 2022

View on GitHub
 (13 comments) (4 reactions) (0 assignees)Go (19,008 forks)batch import
FeatureRequestNeedsInvestigationcompiler/runtimehelp wanted

Repository metrics

Stars
 (133,883 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

What version of Go are you using (go version)?

Does this issue reproduce with the latest release?

I am using the latest release

What operating system and processor architecture are you using (go env)?

What did you do?

I am trying to compile a go project to iOS c libraries and then create a xcframework out of those libraries. This works great for iOS arm64 and simulator x86. However, when I try to compile for simulator arm64 I encounter problems. The library built for arm64 simulator seems to not target the simulator but a real iOS device. This is problematic for using the simulator on new apple silicone devices. I compiled using this command

ios-arm64:
	cd kagome; \
	CGO_ENABLED=1 \
	GOOS=darwin \
	GOARCH=arm64 \
	SDK=iphoneos \
	CC=$(PWD)/clangwrap.sh \
	CGO_CFLAGS="-fembed-bitcode" \
	go build -buildmode=c-archive -o ../$(OUT)/ios/kagome_dart_ios_arm64.a kagome.go

simulator-arm64:
	cd kagome; \
	CGO_ENABLED=1 \
	GOOS=darwin \
	GOARCH=arm64 \
	SDK=iphonesimulator \
	CGO_CFLAGS="-fembed-bitcode" \
	CC=$(PWD)/clangwrap.sh \
	go build -buildmode=c-archive -o ../$(OUT)/ios/kagome_dart_simulator_arm64.a kagome.go

and this is the clangwrap.sh

#!/bin/sh

# go/clangwrap.sh

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

exec $CLANG -arch $CARCH -isysroot $SDK_PATH -mios-version-min=10.0 "$@"

When now creating a xcframework

xcodebuild -create-xcframework \
		-library $(OUT)/ios/kagome_dart_simulator_arm64.a \
		-library $(OUT)/ios/kagome_dart_ios_arm64.a \
		-output  $(OUT)/ios/kagome_dart.xcframework

the error is

A library with the identifier 'ios-arm64' already exists.

What did you expect to see?

I expected the library to be built for an arm64 simulator.

What did you see instead?

The library was built for an arm64 iOS device.

Contributor guide