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: gomobile init fails with new NDK 16.0rc #21802

Closed
zllak opened this issue Sep 8, 2017 · 14 comments
Closed

x/mobile: gomobile init fails with new NDK 16.0rc #21802

zllak opened this issue Sep 8, 2017 · 14 comments
Labels
FrozenDueToAge mobile Android, iOS, and x/mobile
Milestone

Comments

@zllak
Copy link

zllak commented Sep 8, 2017

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

go version go1.9 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=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/zllak/work/go"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.9/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.9/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/v3/g9vzz14155s1ffjhjng6zsj00000gn/T/go-build198615708=/tmp/go-build -gno-record-gcc-switches -fno-common"
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"

What did you do?

With the latest NDK 16.0rc, gomobile init fails:

gomobile: go install -gcflags=-shared -ldflags=-shared -pkgdir=/Users/zllak/work/go/pkg/gomobile/pkg_android_arm std failed: exit status 2
# runtime/cgo
_cgo_export.c:2:10: fatal error: 'stdlib.h' file not found

This is very likely due to the Unified Headers migration (https://android.googlesource.com/platform/ndk/+/ndk-release-r16/docs/UnifiedHeadersMigration.md).
This migration removes the headers from $ANDROID_HOME/ndk-bundle/platforms/android-XX/arch-XXX/usr/include

What did you expect to see?

A successful run of gomobile init

What did you see instead?

gomobile: go install -gcflags=-shared -ldflags=-shared -pkgdir=/Users/zllak/work/go/pkg/gomobile/pkg_android_arm std failed: exit status 2
# runtime/cgo
_cgo_export.c:2:10: fatal error: 'stdlib.h' file not found
@gopherbot gopherbot added this to the Unreleased milestone Sep 8, 2017
@gopherbot gopherbot added the mobile Android, iOS, and x/mobile label Sep 8, 2017
@steeve
Copy link
Contributor

steeve commented Sep 17, 2017

After reading the documentation for unified headers, I got it working with the following diff. You'll need to make sure to rm -rf $GOPATH/pkg/gomobile to clear out the build cache, too.

diff --git a/cmd/gomobile/env.go b/cmd/gomobile/env.go
index bda72ec..9445f95 100644
--- a/cmd/gomobile/env.go
+++ b/cmd/gomobile/env.go
@@ -124,11 +124,14 @@ func envInit() (err error) {
                                a = "armv7a"
                        }
                        target := strings.Join([]string{a, "none", os, env}, "-")
-                       sysroot := filepath.Join(ndkRoot, "platforms", toolchain.platform, "arch-"+toolchain.arch)
+                       androidApi := strings.TrimPrefix(toolchain.platform, "android-")
+                       sysroot := filepath.Join(ndkRoot, "sysroot")
+                       isystem := filepath.Join(sysroot, "usr", "include", toolchain.toolPrefix)
+                       ldsysroot := filepath.Join(ndkRoot, "platforms", toolchain.platform, "arch-"+toolchain.arch)
                        gcctoolchain := filepath.Join(ndkRoot, "toolchains", toolchain.gcc, "prebuilt", archNDK())
-                       flags := fmt.Sprintf("-target %s --sysroot %s -gcc-toolchain %s", target, sysroot, gcctoolchain)
-                       cflags := fmt.Sprintf("%s -I%s/include", flags, gomobilepath)
-                       ldflags := fmt.Sprintf("%s -L%s/usr/lib -L%s/lib/%s", flags, sysroot, gomobilepath, arch)
+                       flags := fmt.Sprintf("-target %s -gcc-toolchain %s", target, gcctoolchain)
+                       cflags := fmt.Sprintf("%s --sysroot %s -isystem %s -D__ANDROID_API__=%s -I%s/include", flags, sysroot, isystem, androidApi, gomobilepath)
+                       ldflags := fmt.Sprintf("%s --sysroot %s -L%s/lib/%s", flags, ldsysroot, gomobilepath, arch)
                        androidEnv[arch] = []string{
                                "GOOS=android",
                                "GOARCH=" + arch,
diff --git a/internal/mobileinit/mobileinit_android.go b/internal/mobileinit/mobileinit_android.go
index 049b18e..cd73f43 100644
--- a/internal/mobileinit/mobileinit_android.go
+++ b/internal/mobileinit/mobileinit_android.go
@@ -19,6 +19,7 @@ adb logcat GoLog:I *:S
 #cgo LDFLAGS: -landroid -llog

 #include <android/log.h>
+#include <stdlib.h>
 #include <string.h>
 */
 import "C"

@TMSConsulting
Copy link

I was not able to get a successful run. Same error as @zllak. Did anyone else edit these two files and then get gomobile init to work?

@steeve
Copy link
Contributor

steeve commented Sep 25, 2017

@TMSConsulting I work with @zllak, you'll need to clear your GOPATH/pkg/gomobile path too

@TMSConsulting
Copy link

@steeve, thank you for your note. I got it work. Long live github!

@steeve
Copy link
Contributor

steeve commented Oct 9, 2017

@eliasnaur how can we go about merging this one? not sure how to contribute

@eliasnaur
Copy link
Contributor

@steeve, have you signed the CLA (https://golang.org/doc/contribute.html#cla)? If so, I'll take care of merging the patch for you.

@steeve
Copy link
Contributor

steeve commented Oct 11, 2017

Signed!

@gopherbot
Copy link

Change https://golang.org/cl/69950 mentions this issue: cmd/gomobile, internal/mobileinit: update to the NDK unified headers

@txthinking
Copy link

Is this released?
For now, the bug still here after go get -u golang.org/x/mobile/cmd/gomobile

@steeve
Copy link
Contributor

steeve commented Oct 24, 2017

@txthinking it is released, yes

@txthinking
Copy link

txthinking commented Oct 24, 2017

Thanks. fixed.

@cxy200927099
Copy link

@steeve i have still get this error! could you tell me how to solve this?
I first install gomobile:

go get golang.org/x/mobile/cmd/gomobile

It was always failed, This is because i live in china,The website golang.org can not be opened;
Then i use following method:

//clone it to $GOPATH/srcgolang.org/x/mobile
git clone https://github.com/golang/mobile.git $GOPATH/src/golang.org/x/mobile
//build it byself and install
cd $GOPATH
go build golang.org/x/mobile/cmd/gomobile
go install golang.org/x/mobile/cmd/gomobile

After this i install gomobile sucessfull

But when i exute

gomobile init 

I get the error:

➜  go gomobile init              
gomobile: go install -gcflags=-shared -ldflags=-shared -pkgdir=/Users/chenxingyi/work/go/pkg/gomobile/pkg_android_arm std failed: exit status 2
# runtime/cgo
_cgo_export.c:2:10: fatal error: 'stdlib.h' file not found

And you said the problem was sloved in the release version, but i still get the error! my go env as bellow:

➜  go go env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/chenxingyi/work/go"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.9.2/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.9.2/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/nk/xlblxlxx7g13b29sql7c7_hc0000gn/T/go-build420391291=/tmp/go-build -gno-record-gcc-switches -fno-common"
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"

@cxy200927099
Copy link

cxy200927099 commented Nov 14, 2017

@steeve I have solve the problem by using android studio update ndk version(15.2.4203891)

image

Then clear build cache and excute gomobile init again,It works

rm -rf $GOPATH/pkg/gomobile
gomobile init

camlistorebot pushed a commit to perkeep/perkeep that referenced this issue Nov 20, 2017
Due to a change in the Android NDK (starting from r16), building the
docker container fails with an error during the installation of
gomobile:
    fatal error: 'stdlib.h' file not found

This issue was addressed in more recent versions of gomobile, which
are compatible with the new NDK.
See golang/go#21802

This CL simply updates gomobile to the most recent commit.

Change-Id: I87c468c34de441bc6acd40b717d1d950014e5a99
@royfalk
Copy link

royfalk commented Nov 21, 2017

rm -rf $GOPATH/pkg/gomobile did not work for me with NDK 16.0.4442984

Updating did the trick.

mpl added a commit to mpl/go4droid that referenced this issue Dec 21, 2017
Due to a change in the Android NDK (starting from r16), building the
docker container fails with an error during the installation of
gomobile:
    fatal error: 'stdlib.h' file not found

This issue was addressed in more recent versions of gomobile, which
are compatible with the new NDK.
See golang/go#21802
@golang golang locked and limited conversation to collaborators Nov 21, 2018
imWildCat pushed a commit to imWildCat/go-mobile that referenced this issue Apr 10, 2021
From r16, the legacy headers no longer ship with the NDK. Update
the gomobile build system to use the unified headers instead.
They're available from r14.

Based on a patch by Steeve Morin.

Fixes golang/go#21802

Change-Id: I098330962c737edb833e968fe82558e15837f23f
Reviewed-on: https://go-review.googlesource.com/69950
Reviewed-by: David Crawshaw <crawshaw@golang.org>
imWildCat pushed a commit to imWildCat/go-mobile that referenced this issue Apr 11, 2021
From r16, the legacy headers no longer ship with the NDK. Update
the gomobile build system to use the unified headers instead.
They're available from r14.

Based on a patch by Steeve Morin.

Fixes golang/go#21802

Change-Id: I098330962c737edb833e968fe82558e15837f23f
Reviewed-on: https://go-review.googlesource.com/69950
Reviewed-by: David Crawshaw <crawshaw@golang.org>
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

7 participants