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: Unable to use godefs with Objective-C #23615

Closed
unsacrificed opened this issue Jan 30, 2018 · 8 comments
Closed

cmd/cgo: Unable to use godefs with Objective-C #23615

unsacrificed opened this issue Jan 30, 2018 · 8 comments

Comments

@unsacrificed
Copy link

Please answer these questions before submitting your issue. Thanks!

go version go1.10rc1 darwin/amd64 (the same with 1.9.3)

go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/unsacrificed/Library/Caches/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/unsacrificed/go"
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/qr/tv0txk2s0zv4579x79sp67m00000gn/T/go-build552297888=/tmp/go-build -gno-record-gcc-switches -fno-common"

I try to go tool cgo -godefs cgodefs.go where cgodefs.go contains

// +build ignore

package tmp

//go:generate go tool cgo -godefs cgodefs.go

/*
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework Cocoa
#import <Cocoa/Cocoa.h>
*/
import "C"

type CGPoint C.CGPoint

and I expect to see source with compatible CGPoint structure. Instead of it I receive a lot of compilation errors:

In file included from /Users/unsacrificed/go/src/gui/cgodefs.go:10:
In file included from /System/Library/Frameworks/Cocoa.framework/Headers/Cocoa.h:12:
In file included from /System/Library/Frameworks/Foundation.framework/Headers/Foundation.h:8:
/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:503:1: error: expected identifier or '('
@class NSString, Protocol;
^
/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:505:9: error: unknown type name 'NSString'
typedef NSString * NSExceptionName NS_EXTENSIBLE_STRING_ENUM;
        ^
/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:506:9: error: unknown type name 'NSString'
typedef NSString * NSRunLoopMode NS_EXTENSIBLE_STRING_ENUM;
... (100000+ lines of errors)

On the other hand I can successfully build and run GoLang app with Objective-C code.

@ianlancetaylor
Copy link
Contributor

The cgo tool works with C, not Objective C. Sorry.

@unsacrificed
Copy link
Author

As far as I understand Objective-C translates to C code and this is why GoLang C package can work with Objective-C. And it looks inconsistent that godefs tools can not do this. Looks like (I'm not sure) that godefs just use wrong/ignore some compile options.

@ianlancetaylor
Copy link
Contributor

ianlancetaylor commented Jan 30, 2018

It may well be that it sometimes works. Let me put it this way: we only support using the cgo tool with C, not Objective C. And I will add that the -godefs option is mainly used for internal purposes only, and is not intended to be a general purpose tool.

What are you really trying to do?

@unsacrificed
Copy link
Author

I need to pass variable of type T from Go code to Objective-C code. T is structure. So I need to create compatible (in terms of memory layout) type/variable in GoLang and pass it to Objective-C via unsafe.Pointer casting. For generic C code I use godefs in such cases.

@ianlancetaylor
Copy link
Contributor

I see. I don't think there is a way to do that if the type is only defined in Objective C.

@unsacrificed
Copy link
Author

I see that godefs compile source to extract information about type (I would like to say that godefs does not parse source directly). If so then whats the diffences C code or Objective-C code if last converted to C code?
I just want to understand and may be report future request.

@ianlancetaylor
Copy link
Contributor

I'm sorry, I don't know what the difference is. I see that you tried it, and that it doesn't work. I know for sure that we do not want to take on the burden of supporting Objective C going forward. I understand that it would be useful for you.

@mikioh mikioh changed the title Unable to use godefs with Objective-C cmd/cgo: Unable to use godefs with Objective-C Feb 1, 2018
@unsacrificed
Copy link
Author

Finally I found a solution. It is possible to do what I want. The only thing I missed: godefs ignores #cgo CFLAGS: -x objective-c line. Solution - pass required CFLAGS to go binary as arguments (-- -x objective-c).
Working example:

// +build ignore

package tmp

//go:generate go tool cgo -godefs -- -x objective-c cgodefs.go

//#import <Cocoa/Cocoa.h>
import "C"

type CGPoint C.CGPoint

output:

// Created by cgo -godefs - DO NOT EDIT
// cgo -godefs -- -x objective-c cgodefs.go

package tmp

type CGPoint struct {
	X	float64
	Y	float64
}

@golang golang locked and limited conversation to collaborators Mar 6, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants