Skip to content

Instantly share code, notes, and snippets.

@fxcoudert
Created January 20, 2017 13:16
Show Gist options
  • Save fxcoudert/538c3d1ff2e55a348018a00e03d2f156 to your computer and use it in GitHub Desktop.
Save fxcoudert/538c3d1ff2e55a348018a00e03d2f156 to your computer and use it in GitHub Desktop.
class MingwW64 < Formula
desc "Windows mingw-w64 headers, runtime, and GCC cross-compilers"
homepage "https://mingw-w64.org/"
url "http://sourceforge.net/projects/mingw-w64/files/mingw-w64/mingw-w64-release/mingw-w64-v5.0.1.tar.bz2"
sha256 "9bb5cd7df78817377841a63555e73596dc0af4acbb71b09bd48de7cf24aeadd2"
depends_on "gmp"
depends_on "mpfr"
depends_on "libmpc"
depends_on "isl"
# Apple's makeinfo is old and has bugs
depends_on "texinfo" => :build
# FIXME: remove keg_only when ready
keg_only "keg-only while we are testing"
resource "binutils" do
url "https://ftpmirror.gnu.org/binutils/binutils-2.27.tar.gz"
mirror "https://ftp.gnu.org/gnu/binutils/binutils-2.27.tar.gz"
sha256 "26253bf0f360ceeba1d9ab6965c57c6a48a01a8343382130d1ed47c468a3094f"
end
resource "gcc" do
url "https://ftpmirror.gnu.org/gcc/gcc-6.3.0/gcc-6.3.0.tar.bz2"
mirror "https://ftp.gnu.org/gnu/gcc/gcc-6.3.0/gcc-6.3.0.tar.bz2"
sha256 "f06ae7f3f790fbf0f018f6d40e844451e6bc3b7bc96e128e63b09825c1f8b29f"
end
def install
# Build and install binutils
resource("binutils").stage do
system "./configure", "--disable-werror",
"--target=x86_64-w64-mingw32",
"--enable-targets=x86_64-w64-mingw32,i686-w64-mingw32",
"--prefix=#{prefix}",
"--with-sysroot=#{prefix}"
system "make"
system "make", "install"
# Info pages and localization files conflict with native tools
info.rmtree
(share/"locale").rmtree
end
# We add the binutils (and future compiler) to our PATH
ENV.prepend_path "PATH", bin
# Build and install the mingw-w64 headers
mkdir "build-headers" do
system "../mingw-w64-headers/configure", "--host=x86_64-w64-mingw32",
"--prefix=#{prefix}/x86_64-w64-mingw32"
system "make"
system "make", "install"
# GCC expects headers in the mingw directory, so create symlink
ln_s "x86_64-w64-mingw32", prefix/"mingw"
end
# Unpack the GCC sources
resource("gcc").stage buildpath/"gcc-source"
# Build and install the GCC compiler (without runtime libraries for now)
mkdir "build-gcc" do
system "../gcc-source/configure",
"--target=x86_64-w64-mingw32",
"--enable-targets=all",
"--disable-werror",
"--enable-version-specific-runtime-libs",
"--with-gmp=#{Formula["gmp"].opt_prefix}",
"--with-mpfr=#{Formula["mpfr"].opt_prefix}",
"--with-mpc=#{Formula["libmpc"].opt_prefix}",
"--with-isl=#{Formula["isl"].opt_prefix}",
"--with-ld=#{bin}/x86_64-w64-mingw32-ld",
"--with-as=#{bin}/x86_64-w64-mingw32-as",
"--enable-languages=c,c++,fortran",
"--prefix=#{prefix}",
"--with-sysroot=#{prefix}"
system "make", "all-gcc"
system "make", "install-gcc"
end
# Build the mingw-w64 runtime
mkdir "build-crt" do
# Environment variables point to host compilers, not target.
# Remove them so the configure script can detect the cross-compiler
ENV.delete "CC"
ENV.delete "CXX"
system "../mingw-w64-crt/configure", "--host=x86_64-w64-mingw32",
"--prefix=#{prefix}/x86_64-w64-mingw32",
"--enable-lib32",
"--enable-lib64"
system "make"
system "make", "install"
end
# Finish building GCC (the runtime libraries)
cd "build-gcc" do
system "make"
system "make", "install"
end
end
test do
objdump = "#{bin}/x86_64-w64-mingw32-objdump"
# Compile a simple C program and check it
(testpath/"hello.c").write <<-EOS.undent
#include <stdio.h>
#include <windows.h>
int main (void)
{
fprintf (stdout, "Hello, console!");
MessageBox (NULL, TEXT("Hello, Windows 98!"), TEXT("HelloMsg"), 0);
exit (EXIT_SUCCESS);
}
EOS
system "#{bin}/x86_64-w64-mingw32-gcc", "-o", "hello-c.exe", "hello.c"
assert_match "PE32+ executable (console) x86-64, for MS Windows", shell_output("file hello-c.exe")
assert_match "file format pei-x86-64", shell_output("#{objdump} -a hello-c.exe")
# Compile a simple C++ program and check it
(testpath/"hello.cc").write <<-EOS.undent
#include <iostream>
int main (void)
{
std::cout << "Hello, world!" << std::endl;
return 0;
}
EOS
system "#{bin}/x86_64-w64-mingw32-g++", "-o", "hello-cc.exe", "hello.cc"
assert_match "PE32+ executable (console) x86-64, for MS Windows", shell_output("file hello-cc.exe")
assert_match "file format pei-x86-64", shell_output("#{objdump} -a hello-cc.exe")
# Compile a simple Fortran program and check it
(testpath/"hello.f90").write <<-EOS.undent
program test
character(len=10) :: d, t
double precision :: x
call date_and_time(date = d, time = t)
print *, "The date is " // trim(d) // " and the time is " // trim(t)
read(*,*) x
print *, sqrt(x)
end program test
EOS
system "#{bin}/x86_64-w64-mingw32-gfortran", "-o", "hello-fortran.exe", "hello.f90"
assert_match "PE32+ executable (console) x86-64, for MS Windows", shell_output("file hello-fortran.exe")
assert_match "file format pei-x86-64", shell_output("#{objdump} -a hello-fortran.exe")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment