Android NDK r23 build problem

You’ll have to switch compiler and linker.

https://developer.android.com/ndk/guides/other_build_systems

1 Like

Ok, I got this output. Anything useful or a hint in there?

(Too big for the forum, 34 K output) → # runtime/cgoAndroid (7284624, based on r416183b) clang version 12.0.5 (https: - Pastebin.com

Result:

go build -v -tags noupgrade -trimpath -ldflags -w -X github.com/syncthing/syncthing/lib/build.Version=v1.18.2 -X github.com/syncthing/syncthing/lib/build.Stamp=1629655487 -X github.com/syncthing/syncthing/lib/build.User=XXX -X github.com/syncthing/syncthing/lib/build.Host=XXX -X github.com/syncthing/syncthing/lib/build.Tags=noupgrade github.com/syncthing/syncthing/cmd/syncthing

I’ve also tried to modify “build.go” from …

args := []string{"build", "-v"}

to

args := []string{"build", "-v -x"}

but got an error:

runPrint: go build -v -x -tags noupgrade (...)
flag provided but not defined: -v -x
usage: go build [-o output] [build flags] [packages]
Run 'go help build' for details.
exit status 2

Ok, understood. But I’m clueless to what exactly I have to switch and if those LD_FLAGS, EXTRA_LDFLAGS, CGO_LDFLAGS get respected in the right place if I set them.

-v and -x should be separate variables in the array, also, it might be that -v needs to be passed to the C compiler.

Might be worth to check what gomobile is using.

gomobile build -x
1 Like

Looks like we only really use cgo for darwin/solaris filewatcher implementation. In theory cross-compiling should be a lot easier without cgo?

1 Like

Dns used to not work without cgo

1 Like

So whats the goal here? Build with an ndk newer than r22?

I might have a look at if it’s clear what we want to achieve.

2 Likes

Yes, exactly. If you’ve got some time for this, I’d appreciate you looking into :slight_smile:

Just vanilla bumping of numbers works fine for me:

1 Like

@AudriusButkevicius

I’ve merged your build script changes into my branch and it indeed worked. Strange, from code perspective I don’t see any significant changes, maybe it’s the NDK_VERSION value in build.gradle which causes some components to reconfigure things differently.

Thank you, we’re back into business with NDK r23 now ! :slight_smile: :+1: :rocket:

P.S. I’ve also migrated to Android Studio Arctic Fox. Had the 2019.x version before on my computer.

Do you have any idea how to proceed with command line compilation?

Under Windows,

set "CC=<full-path-to-ndk>\toolchains\llvm\prebuilt\windows-x86_64\bin\armv7a-linux-androideabi16-clang.cmd"
set "CGO_ENABLED=1"
go run build.go -goos android -no-upgrade -goarch arm zip

works with no problems with the older NDKs, but with NDK r23 I still only get

runtime/cgo
# runtime/cgo
ld: error: cannot open output file $WORK\b083\_cgo_.o: Function not implemented
clang: error: linker command failed with exit code 1 (use -v to see invocation)

:sweat:.

See the main branch of the fork (SyncthingNative_build.cmd) where I’ve adopted Audrius’ changes. Build works immediately using ndk r23.

ref: https://github.com/Catfriend1/syncthing-android/commit/28e84e929d37941b876b07d3310dfedf6f3dc9bd

1 Like

You can’t set CC, you need to pass it as -cc, as it tries to use cgo to build build.go, which is not what you want.

1 Like

I still have a problem when building under F-Droid vagrant and Debian amd64:

.../android-ndk-r23/toolchains/llvm/prebuilt/linux-x86_64/bin/clang: line 1: clang-12: command not found
apt-get install -y gcc gcc-arm-linux-gnueabi gcc-arm-linux-gnueabihf gcc-aarch64-linux-gnu

and then change the call in “build-syncthing.py” to “-cc /usr/bin/arm-linux-gnueabi-gcc”, I get a lot of include errors despite also setting the env var:

export SYSROOT=/root/work/syncthing-android-prereq/android-ndk-r23/toolchains/llvm/prebuilt/linux-x86_64/sysroot

I’ve read here it works on your docker image, what is the magic that CGO finds the correct gcc (or clang-12)?

Very strange, I’ve tried every mix of env vars - no luck. If I do:

echo "`dirname $0`/clang-12 $@" > /root/work/syncthing-android-prereq/android-ndk-r23/toolchains/llvm/prebuilt/linux-x86_64/bin/clang

the arm build starts to work magically. How can I do this “officially” without modding the Android NDK prebuilt files?

IHNI, but the Dockerfile is this:

and then we apparently build using a python script which I assume exists in your fork as well

which appears to do some gymnastics to set cc.

1 Like

Correct. Somehow a debian amd64 bullseye or F-Droid vagrant box isn’t able to search for the correct utility paths. I now did a hacky way and replaced the “clang” batch of the linux ndk r23 with

`dirname $0`/clang-12 $@

and everything works fine again.

I’m very sorry to bump this, but I’m still struggling to get the build to work. This time I’m not setting any CC variables myself. I’m just trying to follow the steps as described in detail in https://github.com/syncthing/syncthing-android/pull/1865/files, which basically are about setting the ANDROID_HOME variable and then executing ./gradlew buildNative.

However, the build still fails with a very similar error.

> Task :syncthing:checkPython
Using python 3.10.5 from C:\Users\User\AppData\Local\Programs\Python\Python310 (python)

> Task :syncthing:buildNative
[python] python -u ./build-syncthing.py
         Building syncthing for arm
         Notice: Next generation GUI will not be built; see --with-next-gen-gui.
         runtime/cgo
         net
         golang.org/x/net/internal/socks
         vendor/golang.org/x/net/http/httpproxy
         log/syslog
         net/textproto
         github.com/lucas-clemente/quic-go/internal/utils
         crypto/x509
         golang.org/x/net/internal/socket
         github.com/syncthing/syncthing/lib/protocol
         github.com/rcrowley/go-metrics
         github.com/jackpal/gateway
         golang.org/x/net/proxy
         github.com/jackpal/go-nat-pmp
         vendor/golang.org/x/net/http/httpguts
         github.com/ccding/go-stun/stun
         golang.org/x/net/ipv4
         golang.org/x/net/ipv6
         crypto/tls
         github.com/syncthing/syncthing/lib/beacon
         github.com/syncthing/syncthing/lib/relay/protocol
         net/http/httptrace
         github.com/syncthing/syncthing/lib/tlsutil
         github.com/marten-seemann/qtls-go1-19
         net/http
         github.com/lucas-clemente/quic-go/internal/qtls
         github.com/lucas-clemente/quic-go/internal/qerr
         github.com/lucas-clemente/quic-go/internal/wire
         github.com/lucas-clemente/quic-go/logging
         github.com/lucas-clemente/quic-go/internal/handshake
         github.com/lucas-clemente/quic-go
         github.com/syncthing/syncthing/lib/dialer
         github.com/go-ldap/ldap/v3
         github.com/syncthing/syncthing/lib/osutil
         github.com/syncthing/syncthing/lib/relay/client
         github.com/syncthing/syncthing/lib/config
         github.com/AudriusButkevicius/pfilter
         github.com/syncthing/syncthing/lib/nat
         github.com/syncthing/syncthing/lib/stun
         github.com/syncthing/syncthing/lib/discover
         github.com/syncthing/syncthing/cmd/syncthing/cli
         github.com/syncthing/syncthing/lib/pmp
         github.com/syncthing/syncthing/lib/upnp
         github.com/syncthing/syncthing/lib/connections
         github.com/syncthing/syncthing/lib/ur
         github.com/syncthing/syncthing/lib/model
         github.com/syncthing/syncthing/lib/api
         github.com/syncthing/syncthing/cmd/syncthing
         # github.com/syncthing/syncthing/cmd/syncthing
         C:\Program Files\Go\pkg\tool\windows_amd64\link.exe: running R:\commandlinetools-win-9123335_latest\ndk\23.0.7599858/toolchains/llvm/prebuilt//windows-x86_64/bin/armv7a-linux-androideabi16-clang failed: exit status 1
         ld: error: cannot open output file $WORK\b001\exe\a.out: Function not implemented
         clang: error: linker command failed with exit code 1 (use -v to see invocation)

         exit status 2
         exit status 1
         Traceback (most recent call last):
           File "R:\syncthing\syncthing-android\syncthing\build-syncthing.py", line 104, in <module>
             subprocess.check_call(
           File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 369, in check_call
             raise CalledProcessError(retcode, cmd)
         subprocess.CalledProcessError: Command '['go', 'run', 'build.go', '-goos', 'android', '-goarch', 'arm', '-cc', 'R:\\commandlinetools-win-9123335_latest\\ndk\\23.0.7599858/toolchains/llvm/prebuilt//windows-x86_64/bin/armv7a-linux-androideabi16-clang', '-pkgdir', 'R:\\syncthing\\syncthing-android\\syncthing\\gobuild\\go-packages\\arm', '-no-upgrade', 'build']' returned non-zero exit status 1.

> Task :syncthing:buildNative FAILED

FAILURE: Build failed with an exception.

I’ve tried checking both the Docker build script and build-syncthing.py, but I can’t see what is wrong with my build steps. Also, as far as I understand, when using ./gradlew buildNative, the build-syncthing.py is executed and CC is passed using -cc automatically, yet the compilation still fails, so I’m really out of ideas what can be wrong here :confused:.

If anyone can point at any specific clues or has an idea what needs to be done to get this to work, I will be extremely grateful.

For the record, I’m trying to build under Windows 10 x64.