Code Monkey home page Code Monkey logo

golang-cross's Introduction

goreleaser-cross

ARCHIVE NOTICE

The golang-cross is now part of the goreleaser team

Brief

Docker container to turn CGO cross-compilation pain into a pleasure. It tested on variety of platforms. Custom sysroots also can be used.

Although cross-compilation without CGO works well too, it is probably better to call goreleaser directly as it saves time on downloading quite big Docker image, especially on CI environment

Tip! Should you wish to see working examples instead of reading

Credits

This project is rather cookbook. Actual work to create cross-compile environment is done by osxcross

Docker

Docker image is placed on Github.

The image is multiarch and supports hosts below

Host Supported
amd64
arm64 (aka aarch64)

To run build with CGO each entry requires some environment variables

Env variable value required Notes
CGO_ENABLED 1 Yes instead of specifying it in each build it can be set globally during docker run -e CGO_ENABLED=1
CC see targets Optional
CXX see targets Optional
PKG_CONFIG_SYSROOT_DIR Required if sysroot is present
PKG_CONFIG_PATH Optional List of directories containing pkg-config files

PKG_CONFIG_SYSROOT_DIR Modifies -I and -L to use the directories located in target sys root. This option is required when cross-compiling packages that use pkg-config to determine CFLAGS and LDFLAGS. -I and -L are modified to point to the new system root. This means that a -I/usr/include/libfoo will become -I/var/target/usr/include/libfoo with a PKG_CONFIG_SYSROOT_DIR equal to /var/target (same rule apply to -L)

PKG_CONFIG_PATH - A colon-separated list of directories to search for .pc files. The default directory will always be searched after searching the path; the default is libdir/pkgconfig:datadir/pkgconfig where libdir is the libdir for pkg-config and datadir is the datadir for pkg-config when it was installed.

Supported toolchains/platforms

Platform Arch CC CXX Verified
Darwin amd64 o64-clang o64-clang++
Darwin (M1) arm64 oa64-clang oa64-clang++
Linux amd64 gcc g++
Linux arm64 aarch64-linux-gnu-gcc aarch64-linux-gnu-g++
Linux armhf (GOARM=5) arm-linux-gnueabihf-gcc arm-linux-gnueabihf-g++ Verification required
Linux armhf (GOARM=6) arm-linux-gnueabihf-gcc arm-linux-gnueabihf-g++ Verification required
Linux armhf (GOARM=7) arm-linux-gnueabihf-gcc arm-linux-gnueabihf-g++
Windows amd64 x86_64-w64-mingw32-gcc x86_64-w64-mingw32-g++ Verification required

Docker

Environment variables

  • GPG_KEY - defaults to /secrets/key.gpg. ignored if file not found
  • DOCKER_USERNAME
  • DOCKER_PASSWORD
  • DOCKER_HOST - defaults to hub.docker.io. ignored if DOCKER_USERNAME and DOCKER_PASSWORD are empty or DOCKER_CREDS_FILE is present
  • DOCKER_CREDS_FILE - path to file with docker login credentials in colon separated format user:password:<registry>. useful when push to multiple docker registries required
    user1:password1:hub.docker.io
    user2:password2:registry.gitlab.com
    
  • DOCKER_FAIL_ON_LOGIN_ERROR - fail on docker login error
  • GITHUB_TOKEN - github auth token to deploy release

Sysroot

Most reasonable way to make a sysroot seem to be rsync and the example is using it. You may want to use the script to create sysroot for your desired linux setup. Lets consider creating sysroot for Raspberry Pi 4 running Debian Buster.

  • install all required dev packages. for this example we will install libftdi1-dev, libusb-1.0-0-dev and opencv4
    ./sysroot-rsync.sh pi@<ipaddress> <local destination>

sshfs

Though sshfs is a good way to test sysroot before running rsync it unfortunately comes with one minor issue. Some packages are creating absolute links and thus pointing to wrong files when mounted (or appear as broken). For example RPI4 running Debian Buster this library /usr/lib/x86_x64-gnu-linux/libpthread.so is symlink to /lib/x86_x64-gnu-linux/libpthread.so instead of ../../../lib/x86_x64-gnu-linux/libpthread.so.

Contributing

Any contribution helping to make this project is welcome

Examples

Projects using

golang-cross's People

Contributors

climech avatar dependabot[bot] avatar gythialy avatar troian avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

golang-cross's Issues

cgo: C compiler "x86_64-w64-mingw32-gcc" not found

Hi troian,

I'm trying to cross-compile my go app for linux and windows, but in my pipeline I'm getting this:

release failed after 78.89s error=failed to build for windows_amd64: exit status 2: # runtime/cgo
cgo: C compiler "x86_64-w64-mingw32-gcc" not found: exec: "x86_64-w64-mingw32-gcc": executable file not found in $PATH

Using this command:

docker run --rm --privileged \
      -v $PWD:/go/src/gitlab.com/pmoscode/tarpit-analyzer \
      -w /go/src/gitlab.com/pmoscode/tarpit-analyzer \
      -v /var/run/docker.sock:/var/run/docker.sock \
      -e DOCKER_USERNAME -e DOCKER_PASSWORD -e DOCKER_REGISTRY  \
      -e GITLAB_TOKEN \
      -e CGO_ENABLED=1 \
      troian/golang-cross:v1.17.2-base release --rm-dist

I have neither sysroot nor --env-file .release-env set.

And this is my build section inside .goreleaser.yaml:

builds:
  - id: linux-amd64
    env:
      - CC=gcc
      - CXX=g++
    goos:
      - linux
    goarch:
      - amd64
  - id: windows-amd64
    env:
      - CC=x86_64-w64-mingw32-gcc
      - CXX=x86_64-w64-mingw32-g++
    goos:
      - windows
    goarch:
      - amd64

What have I missed?

Thx

Including additional libraries

Hi @troian, first of all thank you for your work here!

I am trying to cross-compile a Go project (https://github.com/hazcod/ssh-ca-provider) but failing because it requires CGO to include libpcsc.

Usage:

% docker run -ti --rm -v $(pwd)/app --workdir /app troian/golang-cross build --rm-dist --snapshot --config=.github/goreleaser.yml

goreleaser.yml:

before:
  hooks:
  - apt-get update && apt-get install -y pkgconf pcsc-lite-dev gcc libpcsclite-dev libpcsclite
  - go mod download

dockers:
-
  image_templates:
  - "ghcr.io/hazcod/ssh-ca-provider/sscp:{{ .Tag }}"
  - "ghcr.io/hazcod/ssh-ca-provider/sscp:v{{ .Major }}"
  - "ghcr.io/hazcod/ssh-ca-provider/sscp:latest"

builds:
-
  id: darwin-amd64
  dir: ./cmd/client/
  goos:
  - darwin
  goarch:
  - amd64
  env:
  - CGO_ENABLED=1
  - PKG_CONFIG_SYSROOT_DIR=/sysroot/macos/amd64
  - PKG_CONFIG_PATH=/sysroot/macos/amd64/usr/local/lib/pkgconfig
  - CC=o64-clang
  - CXX=o64-clang++
  flags:
  - -mod=readonly
  ldflags:
  - -s -w -X main.version={{.Version}}
-
  id: linux-armhf
  dir: ./cmd/client/
  goos:
  - linux
  goarch:
  - arm
  goarm:
  - 7
  env:
  - CGO_ENABLED=1
  - CC=arm-linux-gnueabihf-gcc
  - CXX=arm-linux-gnueabihf-g++
  - CGO_FLAGS=--sysroot=/sysroot/linux/armhf
  - CGO_LDFLAGS=--sysroot=/sysroot/linux/armhf
  - PKG_CONFIG_SYSROOT_DIR=/sysroot/linux/armhf
  - PKG_CONFIG_PATH=/sysroot/linux/armhf/opt/vc/lib/pkgconfig:/sysroot/linux/armhf/usr/lib/arm-linux-gnueabihf/pkgconfig:/sysroot/linux/armhf/usr/lib/pkgconfig:/sysroot/linux/armhf/usr/local/lib/pkgconfig
  flags:
  - -mod=readonly
  ldflags:
  - -s -w -X main.version={{.Version}}

archives:
- replacements:
    darwin: Darwin
    linux: Linux
    windows: Windows
    386: i386
    amd64: x86_64
checksum:
  name_template: 'checksums.txt'
snapshot:
  name_template: "{{ .Tag }}-next"
changelog:
  sort: asc
  filters:
    exclude:
    - '^docs:'
    - '^test:'
    - '^chore'

And the logs:

   ⨯ build failed after 269.69s error=failed to build for linux_arm_7: # pkg-config --cflags  -- libpcsclite
Package libpcsclite was not found in the pkg-config search path.
Perhaps you should add the directory containing `libpcsclite.pc'
to the PKG_CONFIG_PATH environment variable
No package 'libpcsclite' found
pkg-config: exit status 1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.