Skip to content

Instantly share code, notes, and snippets.

@bmoyles
Forked from FiloSottile/README.md
Created July 18, 2016 17:26
Show Gist options
  • Save bmoyles/933abd2250badd2d7c93582623d6dd31 to your computer and use it in GitHub Desktop.
Save bmoyles/933abd2250badd2d7c93582623d6dd31 to your computer and use it in GitHub Desktop.
Homebrew Formula for musl-based GCC cross-compilers.
class MuslCross < Formula
desc "Linux cross compilers based on musl libc"
homepage "https://github.com/richfelker/musl-cross-make"
head "https://github.com/richfelker/musl-cross-make.git"
bottle do
cellar :any_skip_relocation
end
option "with-arm-hf", "Build cross-compilers targeting arm-linux-musleabihf"
option "with-arm", "Build cross-compilers targeting arm-linux-musleabi"
option "with-i486", "Build cross-compilers targeting i486-linux-musl"
option "without-x86_64", "Do not build cross-compilers targeting x86_64-linux-musl"
depends_on "gnu-sed" => :build
depends_on "homebrew/dupes/make" => :build
resource "linux-4.4.10.tar.xz" do
url "https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.4.10.tar.xz"
sha256 "4ac22e4a619417213cfdab24714413bb9118fbaebe6012c6c89c279cdadef2ce"
end
resource "mpfr-3.1.4.tar.bz2" do
url "http://ftpmirror.gnu.org/mpfr/mpfr-3.1.4.tar.bz2"
sha256 "d3103a80cdad2407ed581f3618c4bed04e0c92d1cf771a65ead662cc397f7775"
end
resource "mpc-1.0.3.tar.gz" do
url "http://ftpmirror.gnu.org/mpc/mpc-1.0.3.tar.gz"
sha256 "617decc6ea09889fb08ede330917a00b16809b8db88c29c31bfbb49cbf88ecc3"
end
resource "gmp-6.1.0.tar.bz2" do
url "http://ftpmirror.gnu.org/gmp/gmp-6.1.0.tar.bz2"
sha256 "498449a994efeba527885c10405993427995d3f86b8768d8cdf8d9dd7c6b73e8"
end
resource "musl-1.1.14.tar.gz" do
url "https://www.musl-libc.org/releases/musl-1.1.14.tar.gz"
sha256 "35f6c00c84a6091bd5dab29eedde7508dae755ead92dcc0239f3677d1055b9b5"
end
resource "binutils-2.25.1.tar.bz2" do
url "http://ftpmirror.gnu.org/binutils/binutils-2.25.1.tar.bz2"
sha256 "b5b14added7d78a8d1ca70b5cb75fef57ce2197264f4f5835326b0df22ac9f22"
end
resource "config.sub" do
url "http://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.sub;hb=3d5db9ebe860"
sha256 "75d5d255a2a273b6e651f82eecfabf6cbcd8eaeae70e86b417384c8f4a58d8d3"
end
resource "gcc-5.3.0.tar.bz2" do
url "http://ftpmirror.gnu.org/gcc/gcc-5.3.0/gcc-5.3.0.tar.bz2"
sha256 "b84f5592e9218b73dbae612b5253035a7b34a9a1f7688d2e1bfaaf7267d5c4db"
end
def install
ENV.deparallelize
if build.with? "x86_64"
targets = ["x86_64-linux-musl"]
else
targets = []
end
if build.with? "arm-hf"
targets.push "arm-linux-musleabihf"
end
if build.with? "arm"
targets.push "arm-linux-musleabi"
end
if build.with? "i486"
targets.push "i486-linux-musl"
end
(buildpath/"resources").mkpath
resources.each do |resource|
cp resource.fetch, buildpath/"resources"/resource.name
end
(buildpath/"config.mak").write <<-EOS.undent
SOURCES = #{buildpath/"resources"}
OUTPUT = #{libexec}
# Recommended options for faster/simpler build:
COMMON_CONFIG += --disable-nls
GCC_CONFIG += --enable-languages=c,c++
GCC_CONFIG += --disable-libquadmath --disable-decimal-float
GCC_CONFIG += --disable-multilib
# Recommended options for smaller build for deploying binaries:
COMMON_CONFIG += CFLAGS="-g0 -Os" CXXFLAGS="-g0 -Os" LDFLAGS="-s"
# Keep the local build path out of binaries and libraries:
COMMON_CONFIG += --with-debug-prefix-map=$(PWD)=
# https://llvm.org/bugs/show_bug.cgi?id=19650
ifeq ($(shell $(CXX) -v 2>&1 | grep -c "clang"), 1)
TOOLCHAIN_CONFIG += CXX="$(CXX) -fbracket-depth=512"
endif
EOS
ENV.prepend_path "PATH", "#{Formula["gnu-sed"].opt_libexec}/gnubin"
targets.each do |target|
system Formula["make"].opt_bin/"gmake", "install", "TARGET=#{target}"
end
bin.install_symlink Dir["#{libexec}/bin/*"]
end
test do
(testpath/"hello.c").write <<-EOS.undent
#include <stdio.h>
main()
{
printf("Hello, world!");
}
EOS
if build.with? "x86_64"
system "#{bin}/x86_64-linux-musl-cc", (testpath/"hello.c")
end
if build.with? "i486"
system "#{bin}/i486-linux-musl-cc", (testpath/"hello.c")
end
if build.with? "arm-hf"
system "#{bin}/arm-linux-musleabihf-cc", (testpath/"hello.c")
end
if build.with? "arm"
system "#{bin}/arm-linux-musleabi-cc", (testpath/"hello.c")
end
end
end
==> Finishing up
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabi-addr2line arm-linux-musleabi-addr2line
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabi-ar arm-linux-musleabi-ar
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabi-as arm-linux-musleabi-as
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabi-c++ arm-linux-musleabi-c++
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabi-c++filt arm-linux-musleabi-c++filt
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabi-cc arm-linux-musleabi-cc
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabi-cpp arm-linux-musleabi-cpp
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabi-elfedit arm-linux-musleabi-elfedit
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabi-g++ arm-linux-musleabi-g++
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabi-gcc arm-linux-musleabi-gcc
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabi-gcc-5.3.0 arm-linux-musleabi-gcc-5.3.0
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabi-gcc-ar arm-linux-musleabi-gcc-ar
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabi-gcc-nm arm-linux-musleabi-gcc-nm
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabi-gcc-ranlib arm-linux-musleabi-gcc-ranlib
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabi-gcov arm-linux-musleabi-gcov
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabi-gcov-tool arm-linux-musleabi-gcov-tool
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabi-gprof arm-linux-musleabi-gprof
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabi-ld arm-linux-musleabi-ld
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabi-ld.bfd arm-linux-musleabi-ld.bfd
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabi-nm arm-linux-musleabi-nm
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabi-objcopy arm-linux-musleabi-objcopy
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabi-objdump arm-linux-musleabi-objdump
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabi-ranlib arm-linux-musleabi-ranlib
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabi-readelf arm-linux-musleabi-readelf
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabi-size arm-linux-musleabi-size
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabi-strings arm-linux-musleabi-strings
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabi-strip arm-linux-musleabi-strip
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabihf-addr2line arm-linux-musleabihf-addr2line
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabihf-ar arm-linux-musleabihf-ar
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabihf-as arm-linux-musleabihf-as
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabihf-c++ arm-linux-musleabihf-c++
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabihf-c++filt arm-linux-musleabihf-c++filt
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabihf-cc arm-linux-musleabihf-cc
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabihf-cpp arm-linux-musleabihf-cpp
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabihf-elfedit arm-linux-musleabihf-elfedit
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabihf-g++ arm-linux-musleabihf-g++
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabihf-gcc arm-linux-musleabihf-gcc
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabihf-gcc-5.3.0 arm-linux-musleabihf-gcc-5.3.0
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabihf-gcc-ar arm-linux-musleabihf-gcc-ar
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabihf-gcc-nm arm-linux-musleabihf-gcc-nm
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabihf-gcc-ranlib arm-linux-musleabihf-gcc-ranlib
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabihf-gcov arm-linux-musleabihf-gcov
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabihf-gcov-tool arm-linux-musleabihf-gcov-tool
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabihf-gprof arm-linux-musleabihf-gprof
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabihf-ld arm-linux-musleabihf-ld
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabihf-ld.bfd arm-linux-musleabihf-ld.bfd
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabihf-nm arm-linux-musleabihf-nm
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabihf-objcopy arm-linux-musleabihf-objcopy
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabihf-objdump arm-linux-musleabihf-objdump
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabihf-ranlib arm-linux-musleabihf-ranlib
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabihf-readelf arm-linux-musleabihf-readelf
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabihf-size arm-linux-musleabihf-size
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabihf-strings arm-linux-musleabihf-strings
ln -s ../Cellar/musl-cross/HEAD/bin/arm-linux-musleabihf-strip arm-linux-musleabihf-strip
ln -s ../Cellar/musl-cross/HEAD/bin/i486-linux-musl-addr2line i486-linux-musl-addr2line
ln -s ../Cellar/musl-cross/HEAD/bin/i486-linux-musl-ar i486-linux-musl-ar
ln -s ../Cellar/musl-cross/HEAD/bin/i486-linux-musl-as i486-linux-musl-as
ln -s ../Cellar/musl-cross/HEAD/bin/i486-linux-musl-c++ i486-linux-musl-c++
ln -s ../Cellar/musl-cross/HEAD/bin/i486-linux-musl-c++filt i486-linux-musl-c++filt
ln -s ../Cellar/musl-cross/HEAD/bin/i486-linux-musl-cc i486-linux-musl-cc
ln -s ../Cellar/musl-cross/HEAD/bin/i486-linux-musl-cpp i486-linux-musl-cpp
ln -s ../Cellar/musl-cross/HEAD/bin/i486-linux-musl-elfedit i486-linux-musl-elfedit
ln -s ../Cellar/musl-cross/HEAD/bin/i486-linux-musl-g++ i486-linux-musl-g++
ln -s ../Cellar/musl-cross/HEAD/bin/i486-linux-musl-gcc i486-linux-musl-gcc
ln -s ../Cellar/musl-cross/HEAD/bin/i486-linux-musl-gcc-5.3.0 i486-linux-musl-gcc-5.3.0
ln -s ../Cellar/musl-cross/HEAD/bin/i486-linux-musl-gcc-ar i486-linux-musl-gcc-ar
ln -s ../Cellar/musl-cross/HEAD/bin/i486-linux-musl-gcc-nm i486-linux-musl-gcc-nm
ln -s ../Cellar/musl-cross/HEAD/bin/i486-linux-musl-gcc-ranlib i486-linux-musl-gcc-ranlib
ln -s ../Cellar/musl-cross/HEAD/bin/i486-linux-musl-gcov i486-linux-musl-gcov
ln -s ../Cellar/musl-cross/HEAD/bin/i486-linux-musl-gcov-tool i486-linux-musl-gcov-tool
ln -s ../Cellar/musl-cross/HEAD/bin/i486-linux-musl-gprof i486-linux-musl-gprof
ln -s ../Cellar/musl-cross/HEAD/bin/i486-linux-musl-ld i486-linux-musl-ld
ln -s ../Cellar/musl-cross/HEAD/bin/i486-linux-musl-ld.bfd i486-linux-musl-ld.bfd
ln -s ../Cellar/musl-cross/HEAD/bin/i486-linux-musl-nm i486-linux-musl-nm
ln -s ../Cellar/musl-cross/HEAD/bin/i486-linux-musl-objcopy i486-linux-musl-objcopy
ln -s ../Cellar/musl-cross/HEAD/bin/i486-linux-musl-objdump i486-linux-musl-objdump
ln -s ../Cellar/musl-cross/HEAD/bin/i486-linux-musl-ranlib i486-linux-musl-ranlib
ln -s ../Cellar/musl-cross/HEAD/bin/i486-linux-musl-readelf i486-linux-musl-readelf
ln -s ../Cellar/musl-cross/HEAD/bin/i486-linux-musl-size i486-linux-musl-size
ln -s ../Cellar/musl-cross/HEAD/bin/i486-linux-musl-strings i486-linux-musl-strings
ln -s ../Cellar/musl-cross/HEAD/bin/i486-linux-musl-strip i486-linux-musl-strip
ln -s ../Cellar/musl-cross/HEAD/bin/x86_64-linux-musl-addr2line x86_64-linux-musl-addr2line
ln -s ../Cellar/musl-cross/HEAD/bin/x86_64-linux-musl-ar x86_64-linux-musl-ar
ln -s ../Cellar/musl-cross/HEAD/bin/x86_64-linux-musl-as x86_64-linux-musl-as
ln -s ../Cellar/musl-cross/HEAD/bin/x86_64-linux-musl-c++ x86_64-linux-musl-c++
ln -s ../Cellar/musl-cross/HEAD/bin/x86_64-linux-musl-c++filt x86_64-linux-musl-c++filt
ln -s ../Cellar/musl-cross/HEAD/bin/x86_64-linux-musl-cc x86_64-linux-musl-cc
ln -s ../Cellar/musl-cross/HEAD/bin/x86_64-linux-musl-cpp x86_64-linux-musl-cpp
ln -s ../Cellar/musl-cross/HEAD/bin/x86_64-linux-musl-elfedit x86_64-linux-musl-elfedit
ln -s ../Cellar/musl-cross/HEAD/bin/x86_64-linux-musl-g++ x86_64-linux-musl-g++
ln -s ../Cellar/musl-cross/HEAD/bin/x86_64-linux-musl-gcc x86_64-linux-musl-gcc
ln -s ../Cellar/musl-cross/HEAD/bin/x86_64-linux-musl-gcc-5.3.0 x86_64-linux-musl-gcc-5.3.0
ln -s ../Cellar/musl-cross/HEAD/bin/x86_64-linux-musl-gcc-ar x86_64-linux-musl-gcc-ar
ln -s ../Cellar/musl-cross/HEAD/bin/x86_64-linux-musl-gcc-nm x86_64-linux-musl-gcc-nm
ln -s ../Cellar/musl-cross/HEAD/bin/x86_64-linux-musl-gcc-ranlib x86_64-linux-musl-gcc-ranlib
ln -s ../Cellar/musl-cross/HEAD/bin/x86_64-linux-musl-gcov x86_64-linux-musl-gcov
ln -s ../Cellar/musl-cross/HEAD/bin/x86_64-linux-musl-gcov-tool x86_64-linux-musl-gcov-tool
ln -s ../Cellar/musl-cross/HEAD/bin/x86_64-linux-musl-gprof x86_64-linux-musl-gprof
ln -s ../Cellar/musl-cross/HEAD/bin/x86_64-linux-musl-ld x86_64-linux-musl-ld
ln -s ../Cellar/musl-cross/HEAD/bin/x86_64-linux-musl-ld.bfd x86_64-linux-musl-ld.bfd
ln -s ../Cellar/musl-cross/HEAD/bin/x86_64-linux-musl-nm x86_64-linux-musl-nm
ln -s ../Cellar/musl-cross/HEAD/bin/x86_64-linux-musl-objcopy x86_64-linux-musl-objcopy
ln -s ../Cellar/musl-cross/HEAD/bin/x86_64-linux-musl-objdump x86_64-linux-musl-objdump
ln -s ../Cellar/musl-cross/HEAD/bin/x86_64-linux-musl-ranlib x86_64-linux-musl-ranlib
ln -s ../Cellar/musl-cross/HEAD/bin/x86_64-linux-musl-readelf x86_64-linux-musl-readelf
ln -s ../Cellar/musl-cross/HEAD/bin/x86_64-linux-musl-size x86_64-linux-musl-size
ln -s ../Cellar/musl-cross/HEAD/bin/x86_64-linux-musl-strings x86_64-linux-musl-strings
ln -s ../Cellar/musl-cross/HEAD/bin/x86_64-linux-musl-strip x86_64-linux-musl-strip
==> Summary
🍺 /usr/local/Cellar/musl-cross/HEAD: 9,562 files, 599.4M, built in 103 minutes 56 seconds

To build a full cross compiler targeting musl Linux amd64, type this and wait. (A cup of coffee won't do. Going for a run might if you are more fit than me.)

brew install --HEAD https://gist.github.com/FiloSottile/01d2bbfaf63ae1b6e373e6bc874fefc6/raw/musl-cross.rb

You can then use x86_64-linux-musl- versions of the tools to build for the target. For example x86_64-linux-musl-cc will compile C code to run on musl Linux amd64.

The "musl" part of the target is important: the binaries will ONLY run on a musl-based system, like Alpine. However, if you build them as static binaries by passing -static as a LDFLAG they will run anywhere.

Other architectures are supported. For example to get a Raspberry Pi cross-compiler use:

brew install --HEAD https://gist.github.com/FiloSottile/01d2bbfaf63ae1b6e373e6bc874fefc6/raw/musl-cross.rb --without-x86_64 --with-arm-hf

You can also use --with-i486 (x86 32-bit) and --with-arm (ARM soft-float).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment