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-compiling from Linux to Mac #22

Open
luser opened this issue Jun 17, 2016 · 8 comments
Open

Cross-compiling from Linux to Mac #22

luser opened this issue Jun 17, 2016 · 8 comments

Comments

@luser
Copy link

luser commented Jun 17, 2016

Your FAQ says "you don't", but it's totally feasible. It'd be nice to make this easier. I cross-compiled Servo from Linux to Mac:
https://gist.github.com/luser/a33e5070d1c55a7d2c46fe763a9d1543

You need:

  1. clang
  2. cctools (Apple's binutils)
  3. An OS X SDK, you can copy it off of a Mac (this part sucks)

The gist above pulls the first two from Mozilla's tooltool (a content store), and requires you to provide the third yourself.

Getting plain Rust code to build is pretty straightforward, but once you hit a crate with a native code dependency it gets a little tricky, hence most of the stuff in the gist.

I think it would be possible to build a Rust-only OS X SDK that's redistributable since it doesn't need header files, which would make this a lot easier.

@japaric
Copy link
Owner

japaric commented Jun 18, 2016

Your FAQ says "you don't", but it's totally feasible.

I know it's possible. I've heard at least of one report before. Which is why I used "don't" (intended as a recommendation) instead of "can't" (because it is possible). Perhaps the language can be changed to make my intent clearer?

I cross-compiled Servo from Linux to Mac

Wow, that's some impressive feat! This is first time (*) I heard that someone managed to cross compile (across different OSes) a project with so many native dependencies. 👍

(*) OK, maybe the second. I think the servo team has infrastructure to compile servo from linux to android.

It'd be nice to make this easier.

Definitively! The Rust tooling team has plans to extend rustup functionality to make it possible to install SDK and cross-toolchains with it (ideally, automatically or with minimal user intervention).

I think it would be possible to build a Rust-only OS X SDK that's redistributable since it doesn't need header files, which would make this a lot easier.

My worry with this would be licensing issues. clang and cctools seem okay as they are already available via tooltool. But can OSX SDKs downloaded from somewhere? Are there any licensing "gotchas"? As in: Does the user has to explicitly agree to some sort of EULA? etc. If you have the answers to those questions, I'm sure the Rust tooling team (@brson et al) would love to hear them. 😄

@brson
Copy link

brson commented Jun 20, 2016

I think it would be possible to build a Rust-only OS X SDK that's redistributable since it doesn't need header files, which would make this a lot easier.

This does seem possible, and would be cool, as long as you never need to build any C code. There may even be clean-room redeclarations of various core Darwin libraries we could distribute that would cover some simple C/Obj-C cases, but I've never looked into it.

@brson
Copy link

brson commented Jun 20, 2016

This does seem possible, and would be cool, as long as you never need to build any C code. There may even be clean-room redeclarations of various core Darwin libraries we could distribute that would cover some simple C/Obj-C cases, but I've never looked into it.

Just more hand-waving: even if we can build and distribute our own gcc/clang that targets OS X, we'll have to have at least the headers for libc that are compatible with OS X's and free of licensing issues. I know that Mozilla has its own cross build of gcc for OS X but I believe they still need pieces of Apple's SDK. Don't know the details. Here's an old issue with details about what they did.

@luser
Copy link
Author

luser commented Jun 21, 2016

I think Apple's libc bits are all BSD licensed. It can be hard to tease out exactly what's what on their open source site, but libc is definitely there:
http://opensource.apple.com/source/Libc/Libc-1082.20.4/

I was trying to sort out whether the CRT initialization bits were open source, and it turns out that someone else has already fixed that:
https://github.com/skaht/Csu-85

re: cross-compiling Firefox, I did a lot of that work, and yeah, we just use a copy of an OS X SDK. Firefox is building a lot of C code though. For many Rust projects it might be feasible to get away with less. Since you're going to have to declare the things you need in Rust anyway, and not require the headers, it should be possible to provide fake versions of system libraries to link against. This is all a bit theoretical right now.

@beamspease
Copy link

I don't know if this project is still maintained, but this would be nice to have

@cmdln
Copy link

cmdln commented Nov 29, 2017

@luser Thank you for the gist in your first post. I came across this while trying to get a project using the jsonnet crate to cross compile on a Linux host for Mac users. I have been banging my head against cross compiling certain crates for months and your approach both works for me and I expect will be applicable across a lot more use cases.

@reissbaker
Copy link

reissbaker commented Jul 31, 2018

FYI, here's how I did it on a small Rust project pretty easily, via the crossbuild project:

docker run -it --rm $(pwd):/workdir multiarch/crossbuild bash

cat > macbuild.sh <<EOF
#!/bin/bash
export CROSS_TRIPLE="x86_64-apple-darwin"

# Have to set the minimum macos version here for thread-local vars
exec crossbuild cc -mmacosx-version-min=10.7 "$@"
EOF

chmod +x macbuild.sh

cat > .cargo/config <<EOF
[target.x86_64-apple-darwin]
linker = "/workdir/macbuild.sh"
EOF

# Muck with the PATH so we can find dsymutil
PATH=/usr/bin:/root/.cargo/bin/:/usr/x86_64-apple-darwin14/bin/ cargo build --target x86_64-apple-darwin

@zmoazeni
Copy link

zmoazeni commented Jun 9, 2019

I've linked to @reissbaker's comment to a few others who I've seen ask the same question. So I put together an example repository showing how to use it. https://github.com/zmoazeni/rust-crossbuild

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

7 participants