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

x/mobile/cmd/gomobile: gomobile bind doesn't recognize android build tag #24856

Closed
hajimehoshi opened this issue Apr 14, 2018 · 9 comments
Closed
Labels
FrozenDueToAge mobile Android, iOS, and x/mobile
Milestone

Comments

@hajimehoshi
Copy link
Member

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

go version go1.10 darwin/amd64

Does this issue reproduce with the latest release?

Yes

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

GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/hajimehoshi/Library/Caches/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/hajimehoshi/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/7t/qw3np69559591s1v0mk5_p1m0000gn/T/go-build866316470=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

$ cat $GOPATH/src/github.com/hajimehoshi/gomobilebindtest/test.go 
// +build android

package gomobilebindtest

func Foo() {}

$ gomobile bind github.com/hajimehoshi/gomobilebindtest

What did you expect to see?

Success to build

What did you see instead?

gomobile: /Users/hajimehoshi/go/bin/gobind -lang=go,java -outdir=/var/folders/7t/qw3np69559591s1v0mk5_p1m0000gn/T/gomobile-work-857454256 github.com/hajimehoshi/gomobilebindtest failed: exit status 1
2018/04/14 15:24:03 package "github.com/hajimehoshi/gomobilebindtest": no buildable Go source files in /Users/hajimehoshi/go/src/github.com/hajimehoshi/gomobilebindtest
@gopherbot gopherbot added this to the Unreleased milestone Apr 14, 2018
@gopherbot gopherbot added the mobile Android, iOS, and x/mobile label Apr 14, 2018
@hajimehoshi
Copy link
Member Author

Building for iOS also fails even with ios build tag.

@hajimehoshi
Copy link
Member Author

Interestingly, adding !android build tag also fails:

$ cat $GOPATH/src/github.com/hajimehoshi/gomobilebindtest/test.go 
// +build !android

package gomobilebindtest

func Foo() {}

$ gomobile bind github.com/hajimehoshi/gomobilebindtest
gomobile: package "github.com/hajimehoshi/gomobilebindtest": no buildable Go source files in /Users/hajimehoshi/go/src/github.com/hajimehoshi/gomobilebindtest

@hajimehoshi
Copy link
Member Author

git bisect says golang/mobile@4600df5 is the cause

@hajimehoshi
Copy link
Member Author

CC @eliasnaur

@eliasnaur
Copy link
Contributor

Yes, CL 99316 that you refer to changed gobind to be able to generate standalone bindings and changed gomobile to use that instead of its own complicated binding generator. That main benefit is that you can now avoid gomobile altogether for special needs. From the CL description:

"This greatly simplifies gomobile bind, but also paves the way to skip
gomobile bind entirely. For example:

$ gobind -outdir=$GOPATH golang.org/x/mobile/example/bind/hello
$ GOOS=android GOARCH=arm64 CC=<ndk-toolchain>/bin/clang go build -buildmode=c-shared -o libgobind.so gobind
$ ls libgobind.*
libgobind.h  libgobind.so

"

Another benefit is you can now run gobind -lang=objc,go,java some/pkg to generate a complete set of bindings for both platforms and check the result into version control.

To accomplish that without needing the Android and Xcode SDKs, gobind has to be platform neutral and as such will generate bindings without any particular GOOS or GOARCH set. If you need to vary the API, use the -tags flag to gomobile or gobind.

In summary, go install some/pkg will have to work before you can use gobind or gomobile bind on some/pkg.

The reason your !android example fails is that while bindings themselves are generated without GOOS/GOARCH set, they are set when gomobile builds the libraries for pkg.aar. So gobind will generate bindings for Foo, while gomobile will fail to find any Go files because GOOS=android. In summary, your exported API must be platform independent (or you can use tags to select variants), but your implementation can and will depend on GOOS/GOARCH.

@hajimehoshi
Copy link
Member Author

Thank you for elaborating! I'm still not sure the implementation detail, but what I needed to do is to make the lib buildable both on mobile and non-mobile?

@eliasnaur
Copy link
Contributor

In further thought, your use case might still be possible. Could you try https://golang.org/cl/99777?

@eliasnaur eliasnaur reopened this Apr 14, 2018
@gopherbot
Copy link

Change https://golang.org/cl/99777 mentions this issue: cmd/gomobile,cmd/gobind: allow per-platform bindings again

@hajimehoshi
Copy link
Member Author

Yeah, with that patch, I've succeeded the experiment in the first comment:

$ cat $GOPATH/src/github.com/hajimehoshi/gomobilebindtest/test.go 
// +build android

package gomobilebindtest

func Foo() {}

$ gomobile bind github.com/hajimehoshi/gomobilebindtest

@golang golang locked and limited conversation to collaborators Apr 17, 2019
imWildCat pushed a commit to imWildCat/go-mobile that referenced this issue Apr 10, 2021
CL 99316 changed gobind to be platform independent, so
standalone bindings could be generated without having the
Android and Xcode SDKs installed. However, bindings that does
depend on GOOS for its exported API, in particular go source
files that use Cgo now only works if the exported API is
extracted to platform independent files.

By switching to use the source importer, importer.For("source", nil),
gobind can type check the bound packages even in the presence of
Cgo.

The source importer in Go 1.9 and 1.10 has problems with relative
imports and imports from testdata directories (issues 23092 and 24392),
but works from Go 1.10.1 on.

Fixes golang/go#24856

Change-Id: Icb18dce15325b7d4e58cabc1181051bc6269fc1f
Reviewed-on: https://go-review.googlesource.com/99777
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
imWildCat pushed a commit to imWildCat/go-mobile that referenced this issue Apr 11, 2021
CL 99316 changed gobind to be platform independent, so
standalone bindings could be generated without having the
Android and Xcode SDKs installed. However, bindings that does
depend on GOOS for its exported API, in particular go source
files that use Cgo now only works if the exported API is
extracted to platform independent files.

By switching to use the source importer, importer.For("source", nil),
gobind can type check the bound packages even in the presence of
Cgo.

The source importer in Go 1.9 and 1.10 has problems with relative
imports and imports from testdata directories (issues 23092 and 24392),
but works from Go 1.10.1 on.

Fixes golang/go#24856

Change-Id: Icb18dce15325b7d4e58cabc1181051bc6269fc1f
Reviewed-on: https://go-review.googlesource.com/99777
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge mobile Android, iOS, and x/mobile
Projects
None yet
Development

No branches or pull requests

3 participants