golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
121.2k stars 17.37k forks source link

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

Open CaptainDario opened 1 year ago

CaptainDario commented 1 year ago

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 commented 1 year ago

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.

CaptainDario commented 1 year ago

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 commented 1 year ago

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.

albertoAround commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 8 months ago

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 commented 8 months ago

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 commented 2 months ago

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 commented 1 month ago

Still hanging on the splashscreen too