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

Error: android/amd64 requires external (cgo) linking, but cgo is not enabled #50

Open
nicokosi opened this issue Feb 19, 2024 · 6 comments

Comments

@nicokosi
Copy link

nicokosi commented Feb 19, 2024

Hello,

I cannot release nicokosi/gh-collab-scanner, cf. this GitHub Actions' job:

Run cli/gh-extension-precompile@v1
  with:
    go_version: 1.22
Run actions/setup-go@v3
Setup go version spec 1.22
Found in cache @ /opt/hostedtoolcache/go/1.22.0/x64
Added go to the path
Successfully set up Go version 1.22
go version go1.22.0 linux/amd64

go env
Run if [ -n "$INPUT_TOKEN" ]; then
Run if [ -n "$INPUT_TAG" ]; then
Run if [ -n "$INPUT_TITLE" ]; then
Run ${GITHUB_ACTION_PATH//\\//}/build_and_release.sh
go: downloading github.com/cli/go-gh/v2 v2.5.0
✂️...✂️
go: downloading github.com/mattn/go-runewidth v0.0.14
go: downloading github.com/rivo/uniseg v0.4.4
android/amd64 requires external (cgo) linking, but cgo is not enabled
Error: Process completed with exit code 1.

The last release that succeeded, v1.5.3:

  • was using Go 1.20 instead of 1.22 ;
  • had module github.com/cli/go-gh which has not been updated to v2 yet ;
  • probably run on a previous version of the Ubuntu image.

I tried several changes: enable CGO (not sure it is correct), use a Windows runner etc. but I could not make the release succeed (see the GitHub Actions build history).

Do you have any advice/tip? Thank you!

nicokosi added a commit to nicokosi/gh-collab-scanner that referenced this issue Mar 6, 2024
Toolchain visibly does not need to be changed.

We'll see if it fixes the current release issue. 🤷
cli/gh-extension-precompile#50

See Toolchain documention:
https://go.dev/doc/toolchain
nicokosi added a commit to nicokosi/gh-collab-scanner that referenced this issue Mar 6, 2024
Toolchain visibly does not need to be changed.

We'll see if it fixes the current release issue. 🤷
cli/gh-extension-precompile#50

See Toolchain documention:
https://go.dev/doc/toolchain
nicokosi added a commit to nicokosi/gh-collab-scanner that referenced this issue Mar 6, 2024
Try releasing with Go 1.21 in order to fix the release failure:
cli/gh-extension-precompile#50

This reverts commit 4c052b0.
nicokosi added a commit to nicokosi/gh-collab-scanner that referenced this issue Mar 6, 2024
Try releasing with Go 1.21 in order to fix the release failure:
cli/gh-extension-precompile#50

This reverts commit 4c052b0.
@nicokosi
Copy link
Author

nicokosi commented Mar 6, 2024

No error after downgrading Go to 1.21, see gh-collab-scanner 1.5.4.
I presume that it is a Go issue related to GitHub Actions Runner Images, but maybe that this issue can be kept open (using Go 1.21 instead of latest 1.22 is a work-around)? 🤷

@Biswa96
Copy link

Biswa96 commented Mar 17, 2024

Try with CGO_ENABLED=1

@baruchiro
Copy link

With CGO_ENABLED=1:

gcc_android.c:6:10: fatal error: android/log.h: No such file or directory
    6 | #include <android/log.h>
      |          ^~~~~~~~~~~~~~~
compilation terminated.

Go version: 1.22

@the-hotmann
Copy link

the-hotmann commented Apr 17, 2024

Same error here. Since go v1.22 it requires cgo for at least those platforms:

  • android/386
  • android/amd64
  • android/arm/v7
  • ios/amd64
  • ios/arm64

... maybe more

@andyfeller
Copy link
Contributor

andyfeller commented Apr 25, 2024

@nicokosi @Biswa96 @baruchiro @the-hotmann : firstly, thank you all for raising this issue and starting exploring the nuances. ❤

I'm currently looking into this issue to see how this changing behavior with Go 1.22 should be taken into account here 🙇

Research

Source: https://github.com/golang/go/blob/1ca31eac40c0400efc8211bc2483fd621264be11/src/internal/platform/supported.go#L83-L127

// MustLinkExternal reports whether goos/goarch requires external linking
// with or without cgo dependencies.
func MustLinkExternal(goos, goarch string, withCgo bool) bool {
	if withCgo {
		switch goarch {
		case "loong64", "mips", "mipsle", "mips64", "mips64le":
			// Internally linking cgo is incomplete on some architectures.
			// https://go.dev/issue/14449
			return true
		case "arm64":
			if goos == "windows" {
				// windows/arm64 internal linking is not implemented.
				return true
			}
		case "ppc64":
			// Big Endian PPC64 cgo internal linking is not implemented for aix or linux.
			// https://go.dev/issue/8912
			if goos == "aix" || goos == "linux" {
				return true
			}
		}

		switch goos {
		case "android":
			return true
		case "dragonfly":
			// It seems that on Dragonfly thread local storage is
			// set up by the dynamic linker, so internal cgo linking
			// doesn't work. Test case is "go test runtime/cgo".
			return true
		}
	}

	switch goos {
	case "android":
		if goarch != "arm64" {
			return true
		}
	case "ios":
		if goarch == "arm64" {
			return true
		}
	}
	return false
}

@andyfeller
Copy link
Contributor

andyfeller commented Apr 26, 2024

Update

Doing a bit of digging, one option I need to carve out a little more time to try comes from golang/go#42725 (comment), which highlights the need to override CC when compiling for android to point to Android NDK which is maintained on GitHub-hosted runners:

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

This should live within $ANDROID_NDK_HOME on GitHub-managed runners

Finding similar approaches in the wild

  • https://github.com/tony114176876/Fclash/blob/e3b19ed66bdc35f8b7136a0cb653a4efd88cb56f/.github/workflows/package.yml#L215-L225

          - uses: nttld/setup-ndk@v1
            id: setup-ndk
            with:
              ndk-version: r25b
              add-to-path: true
          - name: Build Golang lib
            shell: bash
            env:
              ANDROID_NDK_HOME: ${{ steps.setup-ndk.outputs.ndk-path }}
            run: |
              pushd clash
              env GOOS=android GOARCH=arm64 CC=${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android29-clang GOARM=7 CGO_ENABLED=1 go build -buildmode=c-shared -o ./libclash.so
              popd
              mkdir -p ./android/app/src/main/jniLibs/arm64-v8a
              ${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-strip clash/libclash.so
              mv clash/libclash.so ./android/app/src/main/jniLibs/arm64-v8a/libclash.so
  • https://github.com/poom/rclone/blob/49a6533bc1fdff3d3c32e4a2e7a81629af874abf/.github/workflows/build.yml#L310-L321

          - name: arm64-v8a Set environment variables
            shell: bash
            run: |
              echo "CC=$(echo $ANDROID_NDK/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android${RCLONE_NDK_VERSION}-clang)" >> $GITHUB_ENV
              echo "CC_FOR_TARGET=$CC" >> $GITHUB_ENV
              echo 'GOOS=android' >> $GITHUB_ENV
              echo 'GOARCH=arm64' >> $GITHUB_ENV
              echo 'CGO_ENABLED=1' >> $GITHUB_ENV
              echo 'CGO_LDFLAGS=-fuse-ld=lld -s -w' >> $GITHUB_ENV
    
          - name: arm64-v8a build
            run: go build -v -tags android -trimpath -ldflags '-s -X github.com/rclone/rclone/fs.Version='${VERSION} -o build/rclone-android-${RCLONE_NDK_VERSION}-armv8a .
  • https://github.com/zllovesuki/t/blob/943ecd7237b6d2f676151c4173c0c4cfae4d634e/.github/workflows/build.yaml#L234-L238

          - name: Build android/arm64 Client w/ DNS Fix
            env:
              BUILD_VERSION: ${{ steps.get_version.outputs.VERSION }}
            run: |
              CC=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android28-clang CXX=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android28-clang++ CGO_ENABLED=1 GOARCH=arm64 GOOS=android go build -tags 'osusergo netgo' -ldflags "-X 'main.Version=$BUILD_VERSION' -s -w -extldflags -static" -a -o bin/t-client-android-arm64 ./cmd/client

shawnyu5 pushed a commit to shawnyu5/gh-ac that referenced this issue May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants