class MingwW64 < Formula desc "Minimalist GNU for Windows and GCC cross-compilers" homepage "https://sourceforge.net/projects/mingw-w64/" url "https://downloads.sourceforge.net/project/mingw-w64/mingw-w64/mingw-w64-release/mingw-w64-v11.0.1.tar.bz2" sha256 "3f66bce069ee8bed7439a1a13da7cb91a5e67ea6170f21317ac7f5794625ee10" license "ZPL-2.1" revision 1 livecheck do url :stable regex(%r{url=.*?release/mingw-w64[._-]v?(\d+(?:\.\d+)+)\.t}i) end bottle do rebuild 3 sha256 arm64_sonoma: "3a9ccfa83474eebd0139d97b37d552e460f2ab4c1cccf170d76e214d9950792d" sha256 arm64_ventura: "8aae383c22f21e3bd33de694cd02ed54768b91d08b639198552f4a720236a2d8" sha256 arm64_monterey: "d6ace4b87d25ede8f782e14dadaa1b4f683561d2da5854156f95ce360ea6adc8" sha256 sonoma: "01744df8fcf3dc75bdee3fa7cfc1b41da529ef695218e481eac2ca89ad4e19d7" sha256 ventura: "4f8e1c6ee226f4211cbea00d1d237f0b6769f7d97cdc946755c50c36fd031056" sha256 monterey: "8332eb7fb9a8f96b77a2f92edbbed74bfda5fa37440508fc832ecc2e7673e83e" sha256 x86_64_linux: "3b4d818996775a10f2b890a6f6cd6f78341dee29ebbe0fec4ae8d5d652fb5a5d" end # binutils searches for zstd using pkg-config depends_on "pkg-config" => :build # Apple's makeinfo is old and has bugs depends_on "texinfo" => :build depends_on "gmp" depends_on "isl" depends_on "libmpc" depends_on "mpfr" depends_on "zstd" uses_from_macos "zlib" resource "binutils" do url "https://ftp.gnu.org/gnu/binutils/binutils-2.42.tar.xz" mirror "https://ftpmirror.gnu.org/binutils/binutils-2.42.tar.xz" sha256 "f6e4d41fd5fc778b06b7891457b3620da5ecea1006c6a4a41ae998109f85a800" end resource "gcc" do url "https://ftp.gnu.org/gnu/gcc/gcc-14.1.0/gcc-14.1.0.tar.xz" mirror "https://ftpmirror.gnu.org/gcc/gcc-14.1.0/gcc-14.1.0.tar.xz" sha256 "e283c654987afe3de9d8080bc0bd79534b5ca0d681a73a11ff2b5d3767426840" end def target_archs ["i686", "x86_64"].freeze end def install target_archs.each do |arch| arch_dir = "#{prefix}/toolchain-#{arch}" target = "#{arch}-w64-mingw32" resource("binutils").stage do args = %W[ --target=#{target} --with-sysroot=#{arch_dir} --prefix=#{arch_dir} --enable-targets=#{target} --disable-multilib --disable-nls --with-system-zlib --with-zstd ] mkdir "build-#{arch}" do system "../configure", *args system "make" system "make", "install" end end # Put the newly built binutils into our PATH ENV.prepend_path "PATH", "#{arch_dir}/bin" mkdir "mingw-w64-headers/build-#{arch}" do system "../configure", "--host=#{target}", "--prefix=#{arch_dir}/#{target}" system "make" system "make", "install" end # Create a mingw symlink, expected by GCC ln_s "#{arch_dir}/#{target}", "#{arch_dir}/mingw" # Build the GCC compiler resource("gcc").stage buildpath/"gcc" args = %W[ --target=#{target} --with-sysroot=#{arch_dir} --prefix=#{arch_dir} --with-bugurl=#{tap.issues_url} --enable-languages=c,c++,objc,obj-c++,fortran --with-ld=#{arch_dir}/bin/#{target}-ld --with-as=#{arch_dir}/bin/#{target}-as --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-system-zlib --with-zstd --disable-multilib --disable-nls --enable-threads=posix ] mkdir "#{buildpath}/gcc/build-#{arch}" do system "../configure", *args system "make", "all-gcc" system "make", "install-gcc" end # Build the mingw-w64 runtime args = %W[ CC=#{target}-gcc CXX=#{target}-g++ CPP=#{target}-cpp --host=#{target} --with-sysroot=#{arch_dir}/#{target} --prefix=#{arch_dir}/#{target} ] case arch when "i686" args << "--enable-lib32" << "--disable-lib64" when "x86_64" args << "--disable-lib32" << "--enable-lib64" end mkdir "mingw-w64-crt/build-#{arch}" do system "../configure", *args # Resolves "Too many open files in system" # bfd_open failed open stub file dfxvs01181.o: Too many open files in system # bfd_open failed open stub file: dvxvs00563.o: Too many open files in systembfd_open # https://sourceware.org/bugzilla/show_bug.cgi?id=24723 # https://sourceware.org/bugzilla/show_bug.cgi?id=23573#c18 ENV.deparallelize do system "make" system "make", "install" end end # Build the winpthreads library # we need to build this prior to the # GCC runtime libraries, to have `-lpthread` # available, for `--enable-threads=posix` args = %W[ CC=#{target}-gcc CXX=#{target}-g++ CPP=#{target}-cpp --host=#{target} --with-sysroot=#{arch_dir}/#{target} --prefix=#{arch_dir}/#{target} ] mkdir "mingw-w64-libraries/winpthreads/build-#{arch}" do system "../configure", *args system "make" system "make", "install" end args = %W[ --host=#{target} --with-sysroot=#{arch_dir}/#{target} --prefix=#{arch_dir} --program-prefix=#{target}- ] mkdir "mingw-w64-tools/widl/build-#{arch}" do system "../configure", *args system "make" system "make", "install" end # Finish building GCC (runtime libraries) chdir "#{buildpath}/gcc/build-#{arch}" do system "make" system "make", "install" end # Symlinks all binaries into place mkdir_p bin Dir["#{arch_dir}/bin/*"].each { |f| ln_s f, bin } end end test do (testpath/"hello.c").write <<~EOS #include #include int main() { puts("Hello world!"); MessageBox(NULL, TEXT("Hello GUI!"), TEXT("HelloMsg"), 0); return 0; } EOS (testpath/"hello.cc").write <<~EOS #include int main() { std::cout << "Hello, world!" << std::endl; return 0; } EOS (testpath/"hello.f90").write <<~EOS program hello ; print *, "Hello, world!" ; end program hello EOS # https://docs.microsoft.com/en-us/windows/win32/rpc/using-midl (testpath/"example.idl").write <<~EOS [ uuid(ba209999-0c6c-11d2-97cf-00c04f8eea45), version(1.0) ] interface MyInterface { const unsigned short INT_ARRAY_LEN = 100; void MyRemoteProc( [in] int param1, [out] int outArray[INT_ARRAY_LEN] ); } EOS ENV["LC_ALL"] = "C" ENV.remove_macosxsdk if OS.mac? target_archs.each do |arch| target = "#{arch}-w64-mingw32" outarch = (arch == "i686") ? "i386" : "x86-64" system "#{bin}/#{target}-gcc", "-o", "test.exe", "hello.c" assert_match "file format pei-#{outarch}", shell_output("#{bin}/#{target}-objdump -a test.exe") system "#{bin}/#{target}-g++", "-o", "test.exe", "hello.cc" assert_match "file format pei-#{outarch}", shell_output("#{bin}/#{target}-objdump -a test.exe") system "#{bin}/#{target}-gfortran", "-o", "test.exe", "hello.f90" assert_match "file format pei-#{outarch}", shell_output("#{bin}/#{target}-objdump -a test.exe") system "#{bin}/#{target}-widl", "example.idl" assert_predicate testpath/"example_s.c", :exist?, "example_s.c should have been created" end end end