-
Notifications
You must be signed in to change notification settings - Fork 81
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
crosscompile: Add an example for cross compiling #402
Conversation
This adds an example for cross compiling on an ARM M1 Mac to x86_64 Linux. There are rough edges around this example like it will *only* compile for Linux and won't work for OSX, but I think that this problem should be discussed in the better API issue. Also I realized I missed a bug in the previous cross compilation PR, where when cross compiling, we probably want to use the linux_wrapper for building otherwise we end up trying to `install_name_tool` the final Linux binaries, which doesn't work. ``` [nix-shell:~/code/rules_nixpkgs/examples/toolchains/cc_cross_osx_to_linux_amd64]$ bazel build --config=cross :hello INFO: Invocation ID: f10b420f-2dca-42d3-ab4b-1179862791ff INFO: Analyzed target //:hello (3 packages loaded, 19 targets configured). INFO: Found 1 target... Target //:hello up-to-date: bazel-bin/hello INFO: Elapsed time: 8.212s, Critical Path: 5.59s INFO: 4 processes: 2 internal, 2 processwrapper-sandbox. INFO: Build completed successfully, 4 total actions [nix-shell:~/code/rules_nixpkgs/examples/toolchains/cc_cross_osx_to_linux_amd64]$ file bazel-bin/hello bazel-bin/hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /nix/store/jvjrf7rqflfx4nf65yrkk697sxggffki-glibc-x86_64-linux-2.35-224/lib/ld-linux-x86-64.so.2, for GNU/Linux 3.10.0, not stripped ``` note that when compiling for the first time, it'll build clang and gcc a bunch of times for cross compilations so the initial build will take a few hours...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for opening this PR @DolceTriade, and sorry for the delay in reviewing on my part.
note that when compiling for the first time, it'll build clang and gcc a bunch of times for cross compilations so the initial build will take a few hours...
We might need to make CI skip this example then, unless we can get a Nix cache with the appropriate cross-compilers prebuilt. It looks like the MacOS Examples job is currently failing for another reason though:
ld: library not found for -l:libboost_atomic.so.1.75.0
Any ideas? I'm trying this out myself on a MacOS machine now, but it'll probably take a while.
|
||
To run the example with Nix, issue the following command: | ||
``` | ||
nix-shell --command 'bazel run --config=cross:hello' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nix-shell --command 'bazel run --config=cross:hello' | |
nix-shell --command 'bazel run --config=cross :hello' |
It took about 50 mins, but I have a Linux ELF binary now 🙂
I'm having trouble running it on the Linux system I copied it to though. Looks like I also need to copy a bunch of shared libraries it was linked against. Will take a more in-depth look at this next week. I wonder if we can bundle the binary and its deps up into a docker image? Then the example would match a common use case. |
Yeah, can definitely do that. What's the state of the art around generating docker images for nix? I see a PR in progress around this. Do we use recursive nix and just call bazel from a nix flake or package? |
I was thinking you could use If you want to take a look yourself, I've just been running the container with |
I can probably take a look. Thanks for the example. |
I made a little progress by disabling linking against
This file is in the image, and apparently on the |
Thanks for your examples! I was able to get it running. Honestly, I think the weakest aspect of rules_nixpkgs is that its deployment story is not super clear. I think that the sanest option for deployment of bazel binaries is just to use recursive nix, otherwise you have to manage dependencies in the BUILD files and in your nix docker base image, which is pretty ugly and confusing with nix's more obscure compiler deps. What we do internally is probably less kosher, but basically we just use |
Yeah, this is definitely something that should be improved. I'm pleased we have this |
@@ -123,7 +123,7 @@ def _parse_cc_toolchain_info(content, filename): | |||
def _nixpkgs_cc_toolchain_config_impl(repository_ctx): | |||
host_cpu = get_cpu_value(repository_ctx) | |||
cross_cpu = repository_ctx.attr.cross_cpu or host_cpu | |||
darwin = host_cpu == "darwin" or host_cpu == "darwin_arm64" | |||
darwin = (host_cpu == "darwin" or host_cpu == "darwin_arm64") and cross_cpu == host_cpu |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before this change, when cross-compiling from Linux to MacOS, darwin
would be True
and the target_libc
would be "macosx"
, but after this change darwin
is False
, so target_libc
is "local"
. Is that correct/intentional? @benradf @DolceTriade
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch!
Is this bit correct though:
Before this change, when cross-compiling from Linux to MacOS,
darwin
would beTrue
I could well be mistaken, but in this case wouldn't host_cpu
be k8
, and so darwin
would be False
?
Either way, this needs fixing. I'll raise a pull request and we can discuss the details there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could well be mistaken, but in this case wouldn't host_cpu be k8, and so darwin would be False?
Yes, I think you are correct; I was reading that backwards, but it did look suspicious regardless! :)
This adds an example for cross compiling on an ARM M1 Mac to x86_64 Linux. There are rough edges around this example like it will only compile for Linux and won't work for OSX, but I think that this problem should be discussed in the better API issue.
Also I realized I missed a bug in the previous cross compilation PR, where when cross compiling, we probably want to use the linux_wrapper for building otherwise we end up trying to
install_name_tool
the final Linux binaries, which doesn't work.note that when compiling for the first time, it'll build clang and gcc a bunch of times for cross compilations so the initial build will take a few hours...