• Stars
    star
    142
  • Rank 258,495 (Top 6 %)
  • Language
    Shell
  • License
    MIT License
  • Created almost 3 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Docker image for Golang cross-compiling with CGO

goreleaser-cross

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

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

Credits

This project is rather a cookbook combining various projects into one. Special thanks to osxcross for an amazing cross-compile environment for OSX.

Docker

Docker images are available on both GitHub and Docker hub. The image's tag version follows Golang release so if you use 1.19.4, you are effectively compiling your Go code using Golang at version 1.19.4. The actual version of the other tools installed within the Docker image like goreleaser can be found in the latest releases.

Images from version v1.17.4 are multi-arch. Supported hosts are listed in the table below.

Host Supported CC is set to CXX is set to
amd64 βœ… x86_64-linux-gnu-gcc x86_64-linux-gnu-g++
arm64 (aka aarch64) βœ… aarch64-linux-gnu-gcc aarch64-linux-gnu-gcc

Below are additional environment variables to set when cross compiling with CGO.

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's sysroot.
  • The value of PKG_CONFIG_SYSROOT_DIR is prefixed to -I and -L. For instance -I/usr/include/libfoo becomes -I/var/target/usr/include/libfoo with a PKG_CONFIG_SYSROOT_DIR set to /var/target (same rule apply to -L)
  • PKG_CONFIG_PATH - A colon-separated list of directories to search for .pc files.

Supported toolchains/platforms

Platform Arch CC CXX Verified
Darwin amd64 o64-clang o64-clang++ βœ…
Darwin (M1/M2) arm64 oa64-clang oa64-clang++ βœ…
Linux amd64 x86_64-linux-gnu-gcc x86_64-linux-gnu-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++ βœ…
Linux s390x s390x-linux-gnu-gcc s390x-linux-gnu-g++ βœ…
Windows amd64 x86_64-w64-mingw32-gcc x86_64-w64-mingw32-g++ βœ…
Windows arm64 /llvm-mingw/bin/aarch64-w64-mingw32-gcc /llvm-mingw/bin/aarch64-w64-mingw32-g++ βœ…

Docker

Environment variables

  • Goreleaser variables
  • GPG_KEY (optional) - defaults to /secrets/key.gpg. ignored if file not found
  • DOCKER_CREDS_FILE (optional) - path to JSON file with docker login credentials. Useful when push to multiple docker registries required
  • DOCKER_FAIL_ON_LOGIN_ERROR (optional) - fail on docker login error

Login to registry

Github Actions

Use docker login to auth to repos and mount docker config file. For example:

docker run -v $(HOME)/.docker/config.json:/root/.docker/config.json ...

Docker Creds file

To login from within goreleaser-cross container create creds file.

{
    "registries": [
        {
            "user": "<username>",
            "pass": "<password>",
            "registry": "<registry url>" // for example ghcr.io
        }
    ]
}

Sysroot howto

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 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 introduces cons. Some packages are creating absolute links and thus pointing to wrong files when mounted ( or appear as broken). For example RPI4 running Debian Buster the 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

More Repositories

1

goreleaser

Deliver Go binaries as fast and easily as possible
Go
13,560
star
2

nfpm

nFPM is Not FPM - a simple deb, rpm, apk, ipk, and arch linux packager written in Go
Go
2,128
star
3

goreleaser-action

GitHub Action for GoReleaser
TypeScript
849
star
4

godownloader

[DEPRECATED] Download Go binaries as fast and easily as possible.
Go
443
star
5

chglog

chglog is a changelog management library and tool
Go
251
star
6

fileglob

A file globbing library.
Go
66
star
7

goreleaser-pro

Deliver Go binaries as fast, easily and pro as possible.
Go
58
star
8

goreleaser-example-supply-chain

Example goreleaser + github actions config with keyless signing and SBOM generation
Go
55
star
9

goreleaser-example-zig-cgo

A showcase of using Zig to cross-compile Go application with GoReleaser
Go
39
star
10

goreleaser-cross-example

Go
33
star
11

example

Example GoReleaser project
Go
27
star
12

homebrew-tap

Homebrew Formulae to @goreleaser binaries, powered by @goreleaser
Ruby
12
star
13

old-go-releaser

[DEPRECATED] A script to build and release go binaries to github releases
Shell
10
star
14

community

GoReleaser Community Resources
9
star
15

goreleaser-example-slsa-provenance

A demonstration of showing how to use πŸ’ƒSLSA 3 Generic Generator with GoReleaser to release artifacts while generating signed SLSA provenance
Go
9
star
16

get

Get the latest goreleaser binary
Shell
8
star
17

scoop-bucket

Scoop bucket for @goreleaser binaries, powered by @goreleaser
6
star
18

archive

Moved to https://github.com/goreleaser/goreleaser/tree/master/pkg/archive
Go
5
star
19

artwork

logos & art
5
star
20

goreleaser-azure-devops-extension

Azure DevOps Extension for GoReleaser
TypeScript
4
star
21

goinstall

[DEPRECATED] curl | bash goreleaser projects
Shell
4
star
22

goreleaser-pro-split-merge-example

Example repository using the upcoming split/merge feature of @goreleaser pro
Dockerfile
4
star
23

.github

Smarty
4
star
24

goreleaser-pro-monorepo-example

GoReleaser Pro Monorepo example with GitHub Actions
Go
4
star
25

nur

nix user repository for goreleaser
Nix
3
star
26

goreleaser-pro-split-merge-example-real

Example repository using the split/merge feature of @goreleaser pro to build windows docker images
Dockerfile
3
star
27

fish-food

Gofish Rigs to @goreleaser binaries, powered by @goreleaser
Lua
2
star
28

users

graphs GoReleaser usage in the wild
Go
2
star
29

goreleaser-cross-toolchains

Dockerfile
2
star
30

example-mod-proxy

Example of project using @goreleaser's gomod.proxy feature.
Go
2
star
31

goreleaser-example-sign-with-env

Example signing with cosign using --key=env://KEY
Go
1
star
32

example-simple

Probably the simplest possible @goreleaser example
Makefile
1
star
33

tf

Terraform resources for GoReleaser's organization
HCL
1
star
34

acceptance-tests

Acceptance tests setup et al for GoReleaser.
Shell
1
star