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

Cross compile with CGO dependencies? #233

Closed
jimafisk opened this issue Aug 6, 2020 · 8 comments
Closed

Cross compile with CGO dependencies? #233

jimafisk opened this issue Aug 6, 2020 · 8 comments

Comments

@jimafisk
Copy link

jimafisk commented Aug 6, 2020

I've been using this project to build linux, darwin, and windows binaries and it's worked great (thank you!). However, recently I've added a module that relies on cgo, which to my understanding is not directly supported by goreleaser itself. Is it possible to use this GitHub Action with cgo, or is that something I'll need to roll my own Docker container for?

Some additional build details in case that's helpful

My .goreleaser.yml has:

builds:
- env:
  - CGO_ENABLED=1

Failing build: https://github.com/plentico/plenti/runs/946698117#step:8:128

@jimafisk jimafisk changed the title Cross compile with CGO depencies? Cross compile with CGO dependencies? Aug 6, 2020
@crazy-max
Copy link
Member

Hi @jimafisk,

What you can do in this case is installing the gcc multilib:

      -
        name: GCC multilib
        run: |
          sudo apt-get install gcc-multilib g++-multilib
      -
        name: Run GoReleaser
        uses: goreleaser/goreleaser-action@v1
        with:
          version: latest
          args: release --rm-dist --debug
          key: ${{ secrets.YOUR_PRIVATE_KEY }}
        env:
          GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}

@jimafisk
Copy link
Author

jimafisk commented Aug 6, 2020

Thanks for the quick response @crazy-max! I'm still having a little trouble figuring this out, after installing the multilib packages I get could not determine kind of name for C.AI_MASK errors on the latest build: https://github.com/plentico/plenti/runs/954407001#step:9:119

Do I need to specifically tell the cross-compiler to use something other than default gcc in .goreleaser.yml?

builds:
- env:
  - CGO_ENABLED=1
  - CC=something?

Locally on my Ubuntu 18.04 laptop I tried referencing gcc-multilib and g++-multilib but they are not found in $PATH even though I apt-get installed them. Thank you for the help!

@jimafisk
Copy link
Author

jimafisk commented Aug 13, 2020

It seems like OSXCross is the best way build darwin (Mac) binaries from a linux machine based on posts like this and this. Locally on my Ubuntu 18.04 laptop I was able to accomplish this with the following steps:

  1. Download OSXCross: https://github.com/tpoechtrager/osxcross/archive/master.zip
  2. Unzip the downloaded archive from step 1 so the project lives at ~/Downloads/osxcross-master/
  3. Login and download XCode from https://developer.apple.com/download/more/ (version 11.6 worked for me, the beta release for 12 and even some of the older versions like mentioned here did not)
  4. Move the downloaded archive from step 3 so it lives at ~/Downloads/osxcross-master/Xcode_11.6.xip
  5. Move into the OSXCross folder (cd ~/Downloads/osxcross-master) and run ./tools/gen_sdk_package_pbzx.sh Xcode_11.6.xip which creates the SDK that lives at ~/Downloads/osxcross-master/MacOSX10.15.sdk.tar.xz. Note: I would have loved to used a precompiled SDK from https://github.com/phracker/MacOSX-SDKs but using those gave me the following errors:
# crypto/x509
osxcross: error: cannot find libc++ headers
osxcross: error: while detecting target
# rogchap.com/v8go
In file included from v8go.cc:3:
../../../../go/pkg/mod/rogchap.com/[email protected]/deps/include/v8.h:23:10: fatal error: 'type_traits' file not found
  1. Move the SDK created in step 5 into the "tarballs" directory: mv ~/Downloads/osxcross-master/MacOSX10.15.sdk.tar.xz ~/Downloads/osxcross-master/tarballs/MacOSX10.15.sdk.tar.xz
  2. While still in ~/Downloads/osxcross-master/ run the build: ./build.sh. That should create a "target" directory with a bunch of files and folders in it, one of which is ~/Downloads/osxcross-master/target/bin/ that contains various cross-compilers, including o64-clang that we'll be using. You can move the "target" directory into your path or just reference directly like I do below.

After completing the steps above, you should be able to create a darwin binary using the following command (Update the paths to use your computer's username, not jimafisk):

env GOOS=darwin GOARCH=amd64 CGO_ENABLED=1 CC=/home/jimafisk/Downloads/osxcross-master/target/bin/o64-clang CXX=/home/jimafisk/Downloads/osxcross-master/target/bin/o64-clang++ go build -ldflags -s

Note: Before adding the -ldflags -s flags as recommended here I was getting the following error:

# plenti
/usr/local/go/pkg/tool/linux_amd64/link: /usr/local/go/pkg/tool/linux_amd64/link: running dsymutil failed: exec: "dsymutil": executable file not found in $PATH

@crazy-max
Copy link
Member

@jimafisk Ok that looks hacky 😅 and I don't think something will be changed in our action to handle this. AFAIK GoReleaser doesn't support CGO. See also goreleaser/goreleaser#708. Thoughts @caarlos0?

@jimafisk
Copy link
Author

Hahah yeah it's not the nicest thing ever. I did manage to pull it into a GitHub Action step that works:

-
  name: OSXCross for CGO Support
  run: |
    mkdir ../plenti-osxcross
    git clone https://github.com/plentico/plenti-osxcross.git ../plenti-osxcross/target

I set up this repo which is the "target" directory that's created in step 7 above. If someone else is interested in setting something like this up, keep in mind that "target" directory has hardcoded paths (you can't create it in one folder then move it somewhere else and have it continue working). I got around this by creating a directory structure on my local Ubuntu computer that mirrored my GitHub Actions CI (/home/runner/work/plenti/). In the GitHub Action step I cloned that repo up one directory to avoid a dirty git state that breaks the build if you clone it into your working project directory - I suppose I could even move it up another directory to make the repo general purpose for other folks.

Then just make sure to break .goreleaser.yml into multiple builds, for example if building Linux and Darwin:

builds:
- id: linux-build
  env:
  - CGO_ENABLED=1
  goos:
  - linux
  ignore:
  - goos: linux
    goarch: 386
- id: darwin-build
  ldflags:
  - -s
  env:
  - CGO_ENABLED=1
  - CC=/home/runner/work/plenti/plenti-osxcross/target/bin/o64-clang
  - CXX=/home/runner/work/plenti/plenti-osxcross/target/bin/o64-clang++
  goos:
  - darwin
  ignore:
  - goos: darwin
    goarch: 386

@jimafisk
Copy link
Author

I updated the osxcross-target project so other folks that come across this can use it with goreleaser-action: https://github.com/plentico/osxcross-target

Your .github/workflows/release.yml should include this:

-
  name: OSXCross for CGO Support
  run: |
    mkdir ../../osxcross
    git clone https://github.com/plentico/osxcross-target.git ../../osxcross/target

Then your .goreleaser.yml should have separate builds for linux and mac like this:

builds:
- id: linux-build
  env:
  - CGO_ENABLED=1
  goos:
  - linux
  ignore:
  - goos: linux
    goarch: 386
- id: darwin-build
  ldflags:
  - -s
  env:
  - CGO_ENABLED=1
  - CC=/home/runner/work/osxcross/target/bin/o64-clang
  - CXX=/home/runner/work/osxcross/target/bin/o64-clang++
  goos:
  - darwin
  ignore:
  - goos: darwin
    goarch: 386

Feel free to open an issue over there if it doesn't work for you!

@vikulin
Copy link

vikulin commented May 16, 2021

o64-clang++

I did successfully compiled using XCode 12.5.

I followed your steps with one difference: I got an error in

./tools/gen_sdk_package_pbzx.sh Xcode_12.5.xip

I disabled tmp_xxxx folder removing by the sh script correction and completed the script steps manually:

cd ~/osxcross/build/tmp_28411
~/osxcross/target/SDK/tools/bin/pbzx -n Content | cpio -i
cd ../..
XCODEDIR=/home/vadym/osxcross/build/tmp_28411 ./tools/gen_sdk_package.sh

As result I got MacOSX11.3.sdk.tar.xz file.

ohkinozomu added a commit to ohkinozomu/redash-visualizer that referenced this issue Aug 16, 2021
mrnugget added a commit to sourcegraph/sg that referenced this issue Sep 13, 2021
@jettero
Copy link

jettero commented Feb 17, 2022

It looks like there were plans to add an example using the (new?) docker images, but the follow-through dropped off maybe? or maybe this repo was created really recently and it's just not done yet? I can't quite figure out why this is empty, nor can I seem to figure out how to use the cross images in github actions.

https://github.com/goreleaser/goreleaser-cross-example

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

4 participants