Issue building Restic in Termux

I am not that familiar with building go applications, so I can’t tell if this is bug in Restic, a bug in Termux, or expected behavior. Here are the steps I took in Termux:

  1. pkg install golang git
  2. git clone https://github.com/restic/restic
  3. cd restic
  4. go run build.go

The last command failed with this output:

build: detected Go version Go 1.10.2
build: adding build-tag release
build: build tags: [release]
build: create GOPATH at /data/data/com.termux/files/usr/tmp/restic-build-626089241
build: copy contents of /data/data/com.termux/files/home/restic to /data/data/com.termux/files/usr/tmp/restic-build-626089241/github.com/restic/restic
build: copy contents of /data/data/com.termux/files/home/restic/vendor to /data/data/com.termux/files/usr/tmp/restic-build-626089241/github.com/restic/restic/vendor
build: git version is v0.8.3-226-gabdd59ea
build: version from file 'VERSION' is "0.8.3", version from git "v0.8.3-226-gabdd59ea"
build: ldflags: -s -w -X "main.version=0.8.3 (v0.8.3-226-gabdd59ea)"
build: Go version is at least 1.10, using new syntax for -gcflags
build: go [build -asmflags all=-trimpath=/data/data/com.termux/files/usr/tmp/restic-build-626089241 -gcflags all=-trimpath=/data/data/com.termux/files/usr/tmp/restic-build-626089241 -tags release -ldflags -s -w -X "main.version=0.8.3 (v0.8.3-226-gabdd59ea)" -o /data/data/com.termux/files/home/restic/restic github.com/restic/restic/cmd/restic]
# github.com/restic/restic/cmd/restic
/data/data/com.termux/files/usr/lib/go/pkg/tool/android_arm/link: running arm-linux-androideabi-clang failed: exit status 1
/data/data/com.termux/files/usr/tmp/go-link-771939528/go.o:(.data+0x0): undefined reference to `x_cgo_init'
/data/data/com.termux/files/usr/tmp/go-link-771939528/go.o:(.data+0x4): undefined reference to `x_cgo_notify_runtime_init_done'
/data/data/com.termux/files/usr/tmp/go-link-771939528/go.o:(.data+0x8): undefined reference to `x_cgo_thread_start'
/data/data/com.termux/files/usr/tmp/go-link-771939528/go.o:(.data+0x150): undefined reference to `x_cgo_setenv'
/data/data/com.termux/files/usr/tmp/go-link-771939528/go.o:(.data+0x154): undefined reference to `x_cgo_unsetenv'
/data/data/com.termux/files/usr/tmp/go-link-771939528/go.o:(.data+0x158): undefined reference to `_cgo_yield'
clang-6.0: error: linker command failed with exit code 1 (use -v to see invocation)

build failed: exit status 2
exit status 1

I found an old Restic issue that led to a Termux issue. Since all my error messages referenced cgo and the workaround to the previous issue was to edit build.go by removing a CGO_ENABLED=0 line, I re-ran the last line as go run build.go --enable-cgo and the build succeeded.

Should I need to use --enable-cgo to build Restic in Termux, or does this indicate that something is broken in the way build.go interacts with Termux’s Go package like it was before?

Well not sure how in termux but this is how i have done it in debian and ubuntu in go

download go

         wget https://dl.google.com/go/go1.10.1.linux-amd64.tar.gz

then move it

      mv go /usr/local

then export

      export GOROOT=/usr/local/go

then set the paths

      export PATH=$GOPATH/bin:$GOROOT/bin:$PATH

then check version

   go version

then to install restic go the download page and download depending on your linux then winscp and put that file inside the root folder

     https://beta.restic.net/restic-v0.8.3-191-g6d4c40f8/

then give permissions

   chmod +x restic_v0.8.3-191-g6d4c40f8_linux_amd64

then move restic

       mv restic_v0.8.3-191-g6d4c40f8_linux_amd64 /usr/local/bin/restic

then check version

    restic version

Hope this helps

Thanks for the response. Your suggestion does work. In particular, after doing pkg install golang, the restic binary works for me (for my phone, I needed the _linux_arm version from https://github.com/restic/restic/releases). I didn’t try the official go binary. I didn’t see an armv7 one, but maybe the armv6 one works.

Compiling on my phone in Termux also works if I use the --enable-cgo flag. What I don’t understand is why --enable-cgo is necessary. From the previous issue I linked to, it seemed that there was an issue with the Termux build of Go that caused the restic build to fail unless cgo was enabled, but the resolution of that issue was that the patch was fixed so cgo would not be needed (I thought). I don’t really understand what cgo is doing. My guess is that the Termux go binary links to C libraries for some of the functions used in restic (though the official go binaries are now self-hosted without links to C libraries). One thing I noticed in the Termux package build script is that the helper function for building go packages enables cgo, so that seems to imply that cgo is always necessary (in contradiction to the issue about restic and Termux).

So I have two ways of getting restic to work in Termux. I’d like to understand what is happening with cgo better. I would also like to cross-compile the restic binary from an x86_64 machine. I have had problems doing that with Termux’s build system. I am guessing it would probably work with restic’s build system since the official binary works for me.