Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/cgo: does not compile for iOS simulator arm64 #57442

Open
CaptainDario opened this issue Dec 22, 2022 · 10 comments
Open

cmd/cgo: does not compile for iOS simulator arm64 #57442

CaptainDario opened this issue Dec 22, 2022 · 10 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FeatureRequest help wanted NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@CaptainDario
Copy link

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

$ go version
go version go1.19.3 darwin/amd64

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)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/darioklepoch/Library/Caches/go-build"
GOENV="/Users/darioklepoch/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/darioklepoch/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/darioklepoch/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.19.3"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/d8/p77zrhr12g1_xc5y4bl_0rmh0000gn/T/go-build10530953=/tmp/go-build -gno-record-gcc-switches -fno-common"

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.

@cherrymui
Copy link
Member

iOS simulator on arm64 is not a supported platform yet. You're welcome to file a feature request (or make this issue to), or even better, work on it to make it possible. Thanks.

@cherrymui cherrymui added this to the Unplanned milestone Dec 22, 2022
@seankhliao seankhliao changed the title [cgo]: Does not compile for iOS simulator arm64 cmd/cgo: Does not compile for iOS simulator arm64 Dec 23, 2022
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Dec 23, 2022
@CaptainDario
Copy link
Author

Thank you for the quick response. As I do not have any experience implementing such a feature I cannot really help, I will wait for an implementation then.

@user1121114685
Copy link

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

$ go version
go version go1.19.3 darwin/amd64

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)?

go env Output

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.

I also encountered this problem, and now there is no iphone as a debugging device, the project cannot go on.

@seankhliao seankhliao changed the title cmd/cgo: Does not compile for iOS simulator arm64 cmd/cgo: does not compile for iOS simulator arm64 Dec 24, 2022
@dmitshur dmitshur added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Dec 28, 2022
@albertoAround
Copy link

Is there any news about Silicon Simulator support? I was able to create a framework from Go code and I have a weird problem with the Simulator. Sometimes it just hangs on the start and the best that I achieved is that the App runs, but when I get to the point that the Go code is being called, it hangs again, but if I pause from Xcode the code and resume it, it snaps out and it continue.

All works ok if I edit the scheme and I disable "Debug executable", but as a developer I think you can imagine that I can't work without debugging.

@leixjin
Copy link

leixjin commented Mar 21, 2023

Is there any news about Silicon Simulator support? I was able to create a framework from Go code and I have a weird problem with the Simulator. Sometimes it just hangs on the start and the best that I achieved is that the App runs, but when I get to the point that the Go code is being called, it hangs again, but if I pause from Xcode the code and resume it, it snaps out and it continue.

All works ok if I edit the scheme and I disable "Debug executable", but as a developer I think you can imagine that I can't work without debugging.

Do you have a solution to this problem?

@albertoAround
Copy link

Is there any news about Silicon Simulator support? I was able to create a framework from Go code and I have a weird problem with the Simulator. Sometimes it just hangs on the start and the best that I achieved is that the App runs, but when I get to the point that the Go code is being called, it hangs again, but if I pause from Xcode the code and resume it, it snaps out and it continue.
All works ok if I edit the scheme and I disable "Debug executable", but as a developer I think you can imagine that I can't work without debugging.

Do you have a solution to this problem?

Nope...

@aandea
Copy link

aandea commented Oct 14, 2023

I would like to add a vote to bump this feature to be added in the dev queue. Development on Mac computers becomes fast an Apple silicone exclusive.

@palaniraja
Copy link

I found a fix to build with correct (arm64) simulator runtime on Apple Silicon

in your clangwrap.sh add

if [ "$SDK" = "iphoneos" ]; then
  export TARGET="-target $CARCH-apple-ios$MIN_VERSION"
elif [ "$SDK" = "iphonesimulator" ]; then
  export TARGET="-target $CARCH-apple-ios$MIN_VERSION-simulator"
fi

and include the $TARGET part of your xcrun like below

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

you can arrive correct $MIN_VERSION from your simulator sdk by running xcrun --sdk iphonesimulator --show-sdk-path

@maksimlya
Copy link

I found a fix to build with correct (arm64) simulator runtime on Apple Silicon

in your clangwrap.sh add

if [ "$SDK" = "iphoneos" ]; then
  export TARGET="-target $CARCH-apple-ios$MIN_VERSION"
elif [ "$SDK" = "iphonesimulator" ]; then
  export TARGET="-target $CARCH-apple-ios$MIN_VERSION-simulator"
fi

and include the $TARGET part of your xcrun like below

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

you can arrive correct $MIN_VERSION from your simulator sdk by running xcrun --sdk iphonesimulator --show-sdk-path

Hi, this workaround worked for me to be able to create xcframework that does compiles on M1/M2 simulators. But I still have the issue with app handling on app splash screen (probably since my JSI install method happens very early in the app lifecycle and go being called).

Gomobile - this project seems to support m1/m2 simulators for a while now (according to their github discussions), but they bind the files for Java/Swift which I'm not interested in (need the native compilation).

Can somewhat do the workaround with killing xcode/etc, but would be nive if anyone has suggestion how to fix that. Thx!

@albertoAround
Copy link

Still hanging on the splashscreen too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FeatureRequest help wanted NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

10 participants