Crystal supports a basic form of cross compilation.
Inside is a script cross-libs.sh. Type./cross-libs.sh make lua fftw and then sudo./cross-libs.sh install lua fftw. The script is already set up to download the libraries, cross compile them, and install them in the correct places. Now it is time to compile. Go into the place where you keep your source code and type scons -mac.
In order to achieve this, the compiler executable provides two flags:
--cross-compile
: When given enables cross compilation mode--target
: the LLVM Target Triple to use and set the default compile-time flags from
- Golang supports cross compilation, which generates executable programs on one platform for another. It has been used recently and is very easy to use. Compiling Linux and windows 64 bit executable program under mac CGOENABLED=0 GOOS=linux GOARCH=amd64 go build main.go CGOENABLED=0 GOOS=windows GOARCH=amd64 go build main.go Compiling Mac and windows 64.
- Load the Linux/GNU option. This will load a general Linux/GNU config. We’ll end up replacing the config but it gives us a good starting point. Ct-ng armv7-rpi2-linux-gnueabihf Note 8/29: this is a better starting point than my original suggestion. The config file below will change the remaining settings to accomodate for the different processor.
- How to cross compile from Linux for Mac. There are a lot of examples of cross compiling a go binary for usage on a linux and/or windows machine, but I can't find any info regarding the reverse. I have a linux machine that needs to build a go binary for use on a Mac.
- Cross-compiling on windows for linux free download. Mpv-i686-cross-compiling-MinGW32-Doc This project aims at setting up a MinGW-w64 Toolchain on a pure Linux-32 Bits system to cross-compil.
To get the
--target
flags you can execute llvm-config --host-target
using an installed LLVM 3.5. For example on a Linux it could say 'x86_64-unknown-linux-gnu'.If you need to set any compile-time flags not set implicitly through
--target
, you can use the -D
command line flag.Using these two, we can compile a program in a Mac that will run on that Linux like this:
This will generate a
.o
(Object file) and will print a line with a command to execute on the system we are trying to cross-compile to. For example:You must copy this
.o
file to that system and execute those commands. Once you do this the executable will be available in that target system.This procedure is usually done with the compiler itself to port it to new platforms where a compiler is not yet available. Because in order to compile a Crystal compiler we need an older Crystal compiler, the only two ways to generate a compiler for a system where there isn't a compiler yet are:
- We checkout the latest version of the compiler written in Ruby, and from that compiler we compile the next versions until the current one.
- We create a
.o
file in the target system and from that file we create a compiler.
The first alternative is long and cumbersome, while the second one is much easier.
Cross-compiling can be done for other executables, but its main target is the compiler. If Crystal isn't available in some system you can try cross-compiling it there.
So I picked up a 13' MacBook and have been fiddling around with it. I like it, sue me.One of the first things I did (as any Linux developer would) was to install darwin ports. I noticed some interesting things in there. A few that I needed (git) and a few that completely surprised me (dpkg and apt).
One thing that was missing was a Linux cross-compiler. So I did what any self-respecting Linux developer on a Mac would do: I built one.
Don't get too excited. I've only built one worthy of compiling a kernel (which means no C library, no userspace, etc).
The result of my work is here (built on 10.6.3):
You may notice the extra elf.h file, which is needed in /usr/include/elf.h for some programs in the kernel to compile natively on the host (e.g. modpost). The gcc and binutils will unpack in /opt/local/.
In order to cross-compile, you will need to add a few things to your kernel make command line:
You may notice, like I did, scripts/genksyms/parse.c has a #include for malloc.h which is not on Darwin. You may safely delete that line.