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

Having trouble building with Android NDK #42725

Closed
anderspitman opened this issue Nov 19, 2020 · 7 comments
Closed

Having trouble building with Android NDK #42725

anderspitman opened this issue Nov 19, 2020 · 7 comments

Comments

@anderspitman
Copy link

anderspitman commented Nov 19, 2020

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

$ go version
go version go1.15.5 linux/amd64

Does this issue reproduce with the latest release?

I don't have the latest release.

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/anders/.cache/go-build"
GOENV="/home/anders/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/anders/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/anders/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
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 -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build950721048=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I am attempting to build a go CLI program which I will then exec from an Android app and communicate with over HTTP.

Following the instructions here: https://github.com/golang/go/tree/master/misc/android

I tried to build the following go program

package main
import (
	"net/http"
)
func main() {
}

With this command:

GOOS=android GOARCH=arm64 CGO_ENABLED=1 CC_FOR_TARGET=$NDK_ROOT/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android30-clang go build

What did you expect to see?

I would expect to see a go error that net/http is not used, which I could fix by using it...

What did you see instead?

# runtime/cgo
gcc_android.c:6:10: fatal error: android/log.h: No such file or directory
    6 | #include <android/log.h>
      |          ^~~~~~~~~~~~~~~
compilation terminated.
@ericlagergren
Copy link
Contributor

You’ll need to properly set your include paths. This isn’t a problem on Go’s end—clang is complaining.

@anderspitman
Copy link
Author

anderspitman commented Nov 20, 2020

Hey @ericlagergren, thanks for the reply.

When building the following C program with the same compiler:

#include <android/log.h>

int main() {
}

Like this:

$NDK_ROOT/22.0.6917172/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android30-clang main.c

It builds no problem. I verified that it's picking up the file $NDK_ROOT/22.0.6917172/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/android/log.h (if I move that file it fails).

So it appears the interaction with Go is somehow involved. Maybe the Go compiler is changing the sysroot when invoking the C compiler, which I'm not sure how to override. Any other ideas?

@anderspitman
Copy link
Author

anderspitman commented Nov 20, 2020

Ok I believe this is sysroot related. If I run this command:

GOOS=android GOARCH=arm64 CGO_ENABLED=1 CC_FOR_TARGET=$NDK_ROOT/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android30-clang CGO_CFLAGS="--sysroot=$NDK_ROOT/toolchains/llvm/prebuilt/linux-x86_64/sysroot" go build

I get new errors:

# runtime/cgo
In file included from /home/anders/Android/Sdk/ndk/22.0.6917172/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/sys/types.h:36,
                 from /home/anders/Android/Sdk/ndk/22.0.6917172/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/stdio.h:42,
                 from /home/anders/Android/Sdk/ndk/22.0.6917172/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/malloc.h:30,
                 from /home/anders/Android/Sdk/ndk/22.0.6917172/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/stdlib.h:34,
                 from /home/anders/Android/Sdk/ndk/22.0.6917172/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/local/include/stdlib.h:31,
                 from _cgo_export.c:3:
/home/anders/Android/Sdk/ndk/22.0.6917172/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/linux/types.h:21:10: fatal error: asm/types.h: No such file or directory
   21 | #include <asm/types.h>
      |          ^~~~~~~~~~~~~
compilation terminated.

And sure enough that file doesn't exist in the NDK sysroot. I'm still missing something. If I change the simple C program in also include asm/types.h it works fine, so Go is doing something to break the default configuration of the compiler.

@AlexRouSg
Copy link
Contributor

AlexRouSg commented Nov 20, 2020

What happens if you set CC instead of CC_FOR_TARGET?

CC_FOR_TARGET is only meant for building Go itself.
i.e. running ./all.bash

@anderspitman
Copy link
Author

Different error:

# runtime/cgo
ld: error: duplicate symbol: x_cgo_inittls
>>> defined at gcc_android.c:90
>>>            $WORK/b078/_x003.o:(x_cgo_inittls)
>>> defined at gcc_linux_arm64.c:15
>>>            $WORK/b078/_x006.o:(.bss+0x8)
clang: error: linker command failed with exit code 1 (use -v to see invocation)

CC_FOR_TARGET is only meant for building Go itself

Are you sure? I see it used in lots of cross compiling examples, and the docs mention it explicitly:

When cross-compiling, you must specify a C cross-compiler for cgo to use. You can do this by setting the generic CC_FOR_TARGET

@AlexRouSg
Copy link
Contributor

You didn't read the whole thing ...

When cross-compiling, you must specify a C cross-compiler for cgo to use. You can do this by setting the generic CC_FOR_TARGET or the more specific CC_FOR_${GOOS}_${GOARCH} (for example, CC_FOR_linux_arm) environment variable when building the toolchain using make.bash or you can set the CC environment variable any time you run the go tool.

The new errors are a dupe of #42655 which is NDK version related.

@anderspitman
Copy link
Author

anderspitman commented Nov 20, 2020

Thank you. I've been beating my head against this for 2 days. I'll read more carefully next time.

I can confirm that downgrading to NDK version 21 and using the following command (note NDK version 21 and CC instead of CC_FOR_TARGET) works for me:

GOOS=android GOARCH=arm64 CGO_ENABLED=1 CC=$NDK_ROOT/21.3.6528147/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android30-clang go build

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

4 participants