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

all: support native build on Android #47727

Open
NatoBoram opened this issue Aug 16, 2021 · 7 comments
Open

all: support native build on Android #47727

NatoBoram opened this issue Aug 16, 2021 · 7 comments
Labels
FeatureRequest mobile Android, iOS, and x/mobile NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@NatoBoram
Copy link

NatoBoram commented Aug 16, 2021

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

$ go version
go version go1.16.7 android/arm64

Does this issue reproduce with the latest release?

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

go env Output
$ go env
GO111MODULE=""
GOARCH="arm64"
GOBIN=""
GOCACHE="/data/data/com.termux/files/home/.cache/go-build"
GOENV="/data/data/com.termux/files/home/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="android"
GOINSECURE=""
GOMODCACHE="/data/data/com.termux/files/home/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="android"
GOPATH="/data/data/com.termux/files/home/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/data/data/com.termux/files/usr/lib/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/data/data/com.termux/files/usr/lib/go/pkg/tool/android_arm64"
GOVCS=""
GOVERSION="go1.16.7"
GCCGO="gccgo"
AR="ar"
CC="aarch64-linux-android-clang"
CXX="aarch64-linux-android-clang++"
CGO_ENABLED="1"
GOMOD="/data/data/com.termux/files/home/Applications/go/src/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/data/data/com.termux/files/usr/tmp/go-build2204854682=/tmp/go-build -gno-record-gcc-switches"

What did you do?

./make.bash

What did you expect to see?

Installed Go for android/arm64 in /data/data/com.termux/files/home/Applications/go
Installed commands in /data/data/com.termux/files/home/Applications/go/bin

What did you see instead?

Building Go cmd/dist using /data/data/com.termux/files/usr/lib/go. (go1.16.7 android/arm64)
Building Go toolchain1 using /data/data/com.termux/files/usr/lib/go.
Building Go bootstrap cmd/go (go_bootstrap) using Go toolchain1.
Building Go toolchain2 using go_bootstrap and Go toolchain1.
Building Go toolchain3 using go_bootstrap and Go toolchain2.
Building packages and commands for android/arm64.
# runtime/cgo
/data/data/com.termux/files/usr/bin/ld: /system/lib64/liblog.so: unknown type [0x13] section `.relr.dyn'
/data/data/com.termux/files/usr/bin/ld: skipping incompatible /system/lib64/liblog.so when searching for -llog
/data/data/com.termux/files/usr/bin/ld: /system/lib64/liblog.so: unknown type [0x13] section `.relr.dyn'
/data/data/com.termux/files/usr/bin/ld: skipping incompatible /system/lib64/liblog.so when searching for -llog
/data/data/com.termux/files/usr/bin/ld: cannot find -llog
/data/data/com.termux/files/usr/bin/ld: /system/lib64/liblog.so: unknown type [0x13] section `.relr.dyn'
/data/data/com.termux/files/usr/bin/ld: skipping incompatible /system/lib64/liblog.so when searching for -llog
/data/data/com.termux/files/usr/bin/ld: /system/lib64/liblog.so: unknown type [0x13] section `.relr.dyn'
/data/data/com.termux/files/usr/bin/ld: skipping incompatible /system/lib64/liblog.so when searching for -llog
clang-12: error: linker command failed with exit code 1 (use -v to see invocation)
go tool dist: FAILED: /data/data/com.termux/files/home/Applications/go/pkg/tool/android_arm64/go_bootstrap install -gcflags=all= -ldflags=all= std cmd: exit status 2
@ianlancetaylor
Copy link
Member

This looks like a problem with the Android linker /data/data/com.termux/files/usr/bin/ld reading the Android library /system/lib64/liblog.so. I don't know what Termux is, but if the problem is the Android linker being unable to read an Android library, I don't think that we can fix that problem in Go.

Does Go 1.16 work? It has the same use of -llog in the runtime/cgo package. That use was added in Go 1.4.

@NatoBoram
Copy link
Author

NatoBoram commented Aug 16, 2021

Oh my god. You're right. This is a regression in Android 12 Developer Preview 4. Here's a neofetch.

OS: Android 12 aarch64
Host: google Pixel 3
Kernel: 4.9.270-gc51c7b10ee52-ab7522847
Uptime: 5 days, 1 hour, 38 mins
Packages: 109 (dpkg), 1 (pkg)
Shell: bash 5.1.8
CPU: Qualcomm SDM845 (8) @ 1.766GHz
Memory: 2690MiB / 3579MiB

Termux is a terminal emulator for Android : https://github.com/termux/termux-app

Google has been trying to forbid executing arbitrary code for a long time now, looks like they finally flipped the switch. Here's the latest updates from Termux

Since it will be no longer possible to build Go on Android, is it going to be dropped from Go's support or will Go continue to support up to Android 11?

@NatoBoram NatoBoram changed the title Couldn't build go1.17 on Termux Couldn't build on Android 12 Developer Preview 4 Aug 16, 2021
@NatoBoram NatoBoram changed the title Couldn't build on Android 12 Developer Preview 4 Couldn't build on Android 12 Aug 16, 2021
@ianlancetaylor
Copy link
Member

CC @hyangah to take a look

@ianlancetaylor ianlancetaylor changed the title Couldn't build on Android 12 build: Go does not build on Android 12 Aug 16, 2021
@ianlancetaylor ianlancetaylor added mobile Android, iOS, and x/mobile NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Aug 16, 2021
@ianlancetaylor ianlancetaylor added this to the Go1.18 milestone Aug 16, 2021
@cherrymui
Copy link
Member

Go supports and will probably continue to support Android as a target.

As far as I can tell, Android is mostly not a developer platform and most Android developers don't build on Android. And I'm not sure there is a "standard" development environment. As far as I can tell, building on Android is not really officially supported and tested.

@hyangah
Copy link
Contributor

hyangah commented Aug 16, 2021

As seen in

func HasGoBuild() bool {
if os.Getenv("GO_GCFLAGS") != "" {
// It's too much work to require every caller of the go command
// to pass along "-gcflags="+os.Getenv("GO_GCFLAGS").
// For now, if $GO_GCFLAGS is set, report that we simply can't
// run go build.
return false
}
switch runtime.GOOS {
case "android", "js", "ios":
return false
}
return true
, we even disabled many tests that require go toolchains. This mode of android support was never intended.

@Grimler91
Copy link

The -llog flag has been removed from the golang that is cross-compiled for termux. If you want to compile golang in termux and be able to use it successfully you probably need to apply all three patches that termux applies.

Since it will be no longer possible to build Go on Android, is it going to be dropped from Go's support or will Go continue to support up to Android 11?

The potential issues/drawbacks only happen for apps targeting android 11 or newer. As of now (and probably for the foreseeable future) termux targets android 10. This works fine for now except for that the app is not allowed to be updated through playstore (and will be removed at some points).

There's nothing golang itself can do to be compatible with android >= 11, as the issue is when an app (like termux) runs ("exec()s") golang, and not with golang itself. In theory it should be possible to create an android app that still allows you to use golang to compile code on android >= 11, but you would not be able to run any of the resulting binaries.

@NatoBoram
Copy link
Author

NatoBoram commented Aug 22, 2021

Only src-runtime-cgo-cgo.go.patch and src-runtime-cgo-gcc_android.c.patch worked for me, but I was able to build go1.17.

$ go version
go version go1.17 android/arm64
go env Output
$ go env
GO111MODULE=""
GOARCH="arm64"
GOBIN=""
GOCACHE="/data/data/com.termux/files/home/.cache/go-build"
GOENV="/data/data/com.termux/files/home/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="android"
GOINSECURE=""
GOMODCACHE="/data/data/com.termux/files/home/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="android"
GOPATH="/data/data/com.termux/files/home/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/data/data/com.termux/files/home/Applications/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/data/data/com.termux/files/home/Applications/go/pkg/tool/android_arm64"
GOVCS=""
GOVERSION="go1.17"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/data/data/com.termux/files/usr/tmp/go-build2291608239=/tmp/go-build -gno-record-gcc-switches"
git diff Output
diff --git a/src/runtime/cgo/cgo.go b/src/runtime/cgo/cgo.go
index 4d2caf6c4f..e2cece097a 100644
--- a/src/runtime/cgo/cgo.go
+++ b/src/runtime/cgo/cgo.go
@@ -15,7 +15,6 @@ package cgo
 #cgo darwin,arm64 LDFLAGS: -framework CoreFoundation
 #cgo dragonfly LDFLAGS: -lpthread
 #cgo freebsd LDFLAGS: -lpthread
-#cgo android LDFLAGS: -llog
 #cgo !android,linux LDFLAGS: -lpthread
 #cgo netbsd LDFLAGS: -lpthread
 #cgo openbsd LDFLAGS: -lpthread
diff --git a/src/runtime/cgo/gcc_android.c b/src/runtime/cgo/gcc_android.c
index 7ea213599d..3e6afc5b79 100644
--- a/src/runtime/cgo/gcc_android.c
+++ b/src/runtime/cgo/gcc_android.c
@@ -13,22 +13,12 @@ fatalf(const char* format, ...)
 {
 	va_list ap;
 
-	// Write to both stderr and logcat.
-	//
-	// When running from an .apk, /dev/stderr and /dev/stdout
-	// redirect to /dev/null. And when running a test binary
-	// via adb shell, it's easy to miss logcat.
-
 	fprintf(stderr, "runtime/cgo: ");
 	va_start(ap, format);
 	vfprintf(stderr, format, ap);
 	va_end(ap);
 	fprintf(stderr, "\n");
 
-	va_start(ap, format);
-	__android_log_vprint(ANDROID_LOG_FATAL, "runtime/cgo", format, ap);
-	va_end(ap);
-
 	abort();
 }
 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
FeatureRequest mobile Android, iOS, and x/mobile 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

5 participants