Skip to content

Instantly share code, notes, and snippets.

@fxcoudert
Created January 18, 2017 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fxcoudert/9717a66e34ecde14f145e688053bff97 to your computer and use it in GitHub Desktop.
Save fxcoudert/9717a66e34ecde14f145e688053bff97 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 "mingw-w64-binutils"
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 "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 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",
# Disable multilib for now, because of mingw-w64 bug: https://sourceforge.net/p/mingw-w64/bugs/580/
# "--enable-targets=all",
"--disable-multilib",
"--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=#{Formula["mingw-w64-binutils"].opt_bin}/x86_64-w64-mingw32-ld",
"--with-as=#{Formula["mingw-w64-binutils"].opt_bin}/x86_64-w64-mingw32-as",
"--enable-languages=c,c++,fortran",
"--prefix=#{prefix}"
system "make", "all-gcc"
system "make", "install-gcc"
end
# We now add the freshly-built compiler to our PATH
ENV.prepend_path "PATH", bin
# 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",
# Disable multilib for now, because of mingw-w64 bug: https://sourceforge.net/p/mingw-w64/bugs/580/
# "--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 = "#{Formula["mingw-w64-binutils"].opt_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