Prysm: Add support for cross compilation in Bazel

Created on 24 Jun 2019  路  21Comments  路  Source: prysmaticlabs/prysm

Currently, we're unable to build for other OS / architecture whenever the binary target depends on a cgo library. This includes any dependency on go-ethereum in the beacon-chain and potentially a future BLS library.

To reproduce the issue, try building the beacon chain with a different toolchain specified.

Building in on linux targeting windows:

bazel build --platforms=@io_bazel_rules_go//go/toolchain:windows_amd64 //beacon-chain

Building on linux amd64 targeting arm64.

bazel build --platforms=@io_bazel_rules_go//go/toolchain:linux_arm64 //beacon-chain

Important caveats:

  • We're using a fork of go-ethereum which has the generated BUILD files to support bazel.
  • Removing cgo dependencies are not an option as we expect to have more cgo dependencies in the future. Pure go builds would be nice and we'd have support for windows / ARM from linux today if that were the case!
  • We must support linux -> [darwin_amd64, arm64, windows_amd64] targets. When you're writing cc toolchains, adding Mac OS as the host is optional, but appreciated (expect a bounty tip if Mac OS as host is supported!).
  • This is a pretty involved task. If you are up to taking it on, you'll be mostly on your own to figure it out. Good luck :)

These blocked issues may have some additional context from previous attempts to resolve this.

Blocking #654
Blocking #2546

Help Wanted High

Most helpful comment

PR submitted for all four working targets. 馃帀

All 21 comments

Id' like to get into this one. Wanted to delve into Bazel for a while now.

Issue Status: 1. Open 2. Started 3. Submitted 4. Done


__This issue now has a funding of 600.0 DAI (600.0 USD @ $1.0/DAI) attached to it.__

Issue Status: 1. Open 2. Started 3. Submitted 4. Done


__Workers have applied to start work__.

These users each claimed they can complete the work by 3聽months, 3聽weeks from now.
Please review their action plans below:

1) suburbandad has applied to start work _(Funders only: approve worker | reject worker)_.

Using https://github.com/emacski/tensorflow-serving-arm as an example, build cross compile configs for linux-arm64 and windows-x86_64, leveraging https://github.com/dockcross/dockcross as the build environment. The net result should be that any host execution with x86_64 architecture should be able to cross compile for windows_x86-64 and linux_arm64 targets.
2) thecydonian has applied to start work _(Funders only: approve worker | reject worker)_.

I will first familiarize myself with the existing code, then I will research solutions to the problem. This seems potentially challenging, but I am willing to take it on, and I will not give up easily. If I am able to solve this, I will be sure to do so for MacOS as well.

I will make sure to keep you consistantly updated on my progress, and should be able to make signifigant progress over the weekend.

In the unlikely case that I do fail at fixing this issue. I will produce a write up with everything I found out while trying to do this bounty. Thank you for your consideration

Learn more on the Gitcoin Issue Details page.

@lzxm160, it has to be bazel. We might be adding more complicated build logic that wouldn't be supported natively with gomod.

@prestonvanloon sure,I'll try if you approve

@lzxm160, I took a quick look at your profile on gitcoin and you have 3 abandoned tasks. I wouldn't want to award this task to you so that no one else has the opportunity to work on it then have it abandoned later.

Do you have a plan on how to do this? If you already know the solution then I'd accept it and award the bounty, but this is something that won't be easy for someone who isn't familiar with bazel toolchains.

@prestonvanloon understand,I'll try first

To anyone interested in working on this, please build from branch spec-v0.6 unless https://github.com/prysmaticlabs/prysm/pull/2312 is merged to master.

We've added some progress towards this goal with #2970. You can see the TODO here: https://github.com/prysmaticlabs/prysm/blob/25d8b485e6d45deaa001f877265f266466623f4b/tools/binary_targets.bzl#L1-L9

Suggested workflow here would be to uncomment those lines then build the following from linux:

If both of those targets build on linux, then this issue can be resolved.

I tried building the darwin_amd64 and windows_amd64 rules on my machine and it worked fine. is this still an issue?

This is resolved by toolchains in rules_go.

Reopening this as we still have cross compile issues.

I have this script to build and upload multiple platform binaries here: https://gist.github.com/prestonvanloon/32fdec43aba8bd96c656739ada69a23e#file-build_and_upload-sh

To consider this issue resolved, we should be able to run the above script on linux and successfully build all four targets:

Host machine: linux amd64

Targets:

  • linux amd64
  • linux arm64
  • darwin amd64
  • windows amd64

Issue Status: 1. Open 2. Started 3. Submitted 4. Done


__The funding of this issue was increased to 1000.0 SAI (1000.0 USD @ $1.0/SAI) .__

I am working on a cross-compiling docker container and toolchains that will support cross compiling arm64, darwin and windows from any x86-64 machine that has docker installed.

So far I have docker images and toolchain branches for :

arm64/linux/clang10 -> successfully builds prysm beacon-chain and validator
x86_64/darwin/clang9 -> partial success; builds bls target but failing on beacon and validator
x86_64/win/clang10 -> no progress to report yet
Good progress, but still a ways out before a PR

Awesome progress @SuburbanDad.

If possible, can you check in the ARM build? We can start providing arm64 docker images sooner

If possible, can you check in the ARM build? We can start providing arm64 docker images sooner

I will get it into a committable state tonight and publish the MVP docker cross compiler image :+1:

PR submitted for all four working targets. 馃帀

Update: @SuburbanDad's PR meets all of the requirements of the bounty. In PR review, I asked if we could make use of bazel's remote build execution and/or docker sandbox. Over a few days of offline discussion, @SuburbanDad and I were able to connect the plumbing from PR #4945 into the bazel RBE and bazel docker sandbox functionality for cross compile.

So far, we are able to cross compile and confirm the following:

| target | linux_amd64 | linux_arm64 | osx_amd64 | windows_amd64 |
|----------|-------------------|------------------|-----------------|-----------------------|
| //beacon-chain | :heavy_check_mark: docker-sandbox and RBE, libkafka supported locally only | :heavy_check_mark: docker-sandbox and RBE, no libkafka support | :heavy_check_mark: docker-sandbox, no libkafka support | :heavy_check_mark: docker-sandbox, no libkafka support |
| //validator| :heavy_check_mark: docker-sandbox and RBE | :heavy_check_mark: docker-sandbox and RBE | :heavy_check_mark: docker-sandbox | :x: Doesn't work. #5008 |

@SuburbanDad and I will work together to merge our changes into PR #4945 by the end of the week.This will enable us to leverage RBE in our continuous integration and more importantly provide signed executable binaries for each tagged release of Prysm's beacon-node and validator targets. :tada:

@ceresstation This is good to go! Please release the bounty to @SuburbanDad

鈿★笍 A tip worth 1000.00000 SAI (1000.0 USD @ $1.0/SAI) has been granted to @SuburbanDad for this issue from @ceresstation. 鈿★笍

Nice work @SuburbanDad! Your tip has automatically been deposited in the ETH address we have on file.

Issue Status: 1. Open 2. Started 3. Submitted 4. Done


__This Bounty has been completed.__

Additional Tips for this Bounty:

  • ceresstation tipped 1000.0000 SAI worth 1000.0 USD to SuburbanDad.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jrhea picture jrhea  路  4Comments

nisdas picture nisdas  路  4Comments

0xKiwi picture 0xKiwi  路  5Comments

mshean picture mshean  路  4Comments

shayzluf picture shayzluf  路  4Comments