1 class GccAT11 < Formula
2 desc "GNU compiler collection"
3 homepage "https://gcc.gnu.org/"
4 # TODO: Remove maximum_macos if Xcode 16 support is added to https://github.com/iains/gcc-11-branch
5 url "https://ftp.gnu.org/gnu/gcc/gcc-11.5.0/gcc-11.5.0.tar.xz"
6 mirror "https://ftpmirror.gnu.org/gcc/gcc-11.5.0/gcc-11.5.0.tar.xz"
7 sha256 "a6e21868ead545cf87f0c01f84276e4b5281d672098591c1c896241f09363478"
8 license "GPL-3.0-or-later" => { with: "GCC-exception-3.1" }
12 regex(%r{href=["']?gcc[._-]v?(11(?:\.\d+)+)(?:/?["' >]|\.t)}i)
16 sha256 arm64_sonoma: "55ec3e36278e110b74b148fefe5ccacf82223d0fb2fceb3c8230a03bfbc2857f"
17 sha256 arm64_ventura: "b0936cf63d97795bfe8e17a9e4a4f75be0fd3f9407e467d5d02df1fba5caa499"
18 sha256 arm64_monterey: "85d430ddf596edf1f14639ca164d7370a46456c017008723392fcf9cd1f91bfa"
19 sha256 sonoma: "9c0f839a23e3b7f12c72f3833823ca9cbd9d2b0e3a744e1d436daeeffc77cc82"
20 sha256 ventura: "ceae737d0fe4ec0e8fc10356007d09c9250401443b5ccd632ee960a00004cba1"
21 sha256 monterey: "af5d635ec43e4fc786abafe5f963ea26ac147ac10880d3ab4e2eea49948051cc"
22 sha256 cellar: :any_skip_relocation, x86_64_linux: "c7f02fea0754abb9c33f142cfc8d30bdc16d5d80a27ad2ca72ba562540bb1a44"
25 # The bottles are built on systems with the CLT installed, and do not work
26 # out of the box on Xcode-only systems due to an incorrect sysroot.
27 pour_bottle? only_if: :clt_installed
29 depends_on maximum_macos: [:ventura, :build] # Xcode < 16
36 uses_from_macos "zlib"
42 # GCC bootstraps itself, so it is OK to have an incompatible C++ stdlib
45 # Branch from the Darwin maintainer of GCC, with a few generic fixes and
46 # Apple Silicon support, located at https://github.com/iains/gcc-11-branch
48 url "https://raw.githubusercontent.com/Homebrew/formula-patches/5c9419923ddb3e5302ddd277bc524f4d4b0f8722/gcc/gcc-11.5.0.diff"
49 sha256 "213b332bd09452e0cf081f874f32d028911fa871875f85b200b55c5b588ce193"
53 # GCC will suffer build errors if forced to use a particular linker.
56 # We avoiding building:
57 # - Ada, which requires a pre-existing GCC Ada compiler to bootstrap
58 # - Go, currently not supported on macOS
60 languages = %w[c c++ objc obj-c++ fortran]
61 languages << "d" if Hardware::CPU.intel?
63 pkgversion = "Homebrew GCC #{pkg_version} #{build.used_options*" "}".strip
66 --prefix=#{opt_prefix}
67 --libdir=#{opt_lib}/gcc/#{version.major}
69 --enable-checking=release
70 --with-gcc-major-version-only
71 --enable-languages=#{languages.join(",")}
72 --program-suffix=-#{version.major}
73 --with-gmp=#{Formula["gmp"].opt_prefix}
74 --with-mpfr=#{Formula["mpfr"].opt_prefix}
75 --with-mpc=#{Formula["libmpc"].opt_prefix}
76 --with-isl=#{Formula["isl"].opt_prefix}
77 --with-zstd=#{Formula["zstd"].opt_prefix}
78 --with-pkgversion=#{pkgversion}
79 --with-bugurl=#{tap.issues_url}
81 # libphobos is part of gdc
82 args << "--enable-libphobos" if Hardware::CPU.intel?
85 cpu = Hardware::CPU.arm? ? "aarch64" : "x86_64"
86 args << "--build=#{cpu}-apple-darwin#{OS.kernel_version.major}"
87 args << "--with-system-zlib"
89 # System headers may not be in /usr/include
90 sdk = MacOS.sdk_path_if_needed
91 args << "--with-sysroot=#{sdk}" if sdk
93 # Work around a bug in Xcode 15's new linker (FB13038083)
94 if DevelopmentTools.clang_build_version >= 1500
95 toolchain_path = "/Library/Developer/CommandLineTools"
96 args << "--with-ld=#{toolchain_path}/usr/bin/ld-classic"
99 # Fix cc1: error while loading shared libraries: libisl.so.15
100 args << "--with-boot-ldflags=-static-libstdc++ -static-libgcc #{ENV.ldflags}"
102 # Fix Linux error: gnu/stubs-32.h: No such file or directory.
103 args << "--disable-multilib"
105 # Change the default directory name for 64-bit libraries to `lib`
106 # https://stackoverflow.com/a/54038769
107 inreplace "gcc/config/i386/t-linux64", "m64=../lib64", "m64="
108 inreplace "gcc/config/aarch64/t-aarch64-linux", "lp64=../lib64", "lp64="
112 system "../configure", *args
115 # Do not strip the binaries on macOS, it makes them unsuitable
116 # for loading plugins
117 install_target = OS.mac? ? "install" : "install-strip"
119 # To make sure GCC does not record cellar paths, we configure it with
120 # opt_prefix as the prefix. Then we use DESTDIR to install into a
121 # temporary location, then move into the cellar path.
122 system "make", install_target, "DESTDIR=#{Pathname.pwd}/../instdir"
123 mv Dir[Pathname.pwd/"../instdir/#{opt_prefix}/*"], prefix
126 # Handle conflicts between GCC formulae and avoid interfering
127 # with system compilers.
129 man7.glob("*.7") { |file| add_suffix file, version.major }
130 # Even when we disable building info pages some are still installed.
133 # Work around GCC install bug
134 # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105664
135 rm_r(bin.glob("*-gcc-tmp"))
138 def add_suffix(file, suffix)
139 dir = File.dirname(file)
140 ext = File.extname(file)
141 base = File.basename(file, ext)
142 File.rename file, "#{dir}/#{base}-#{suffix}#{ext}"
147 gcc = bin/"gcc-#{version.major}"
148 libgcc = Pathname.new(Utils.safe_popen_read(gcc, "-print-libgcc-file-name")).parent
149 raise "command failed: #{gcc} -print-libgcc-file-name" if $CHILD_STATUS.exitstatus.nonzero?
151 glibc = Formula["glibc"]
152 glibc_installed = glibc.any_version_installed?
154 # Symlink system crt1.o and friends where gcc can find it.
155 crtdir = if glibc_installed
158 Pathname.new(Utils.safe_popen_read("/usr/bin/cc", "-print-file-name=crti.o")).parent
160 ln_sf Dir[crtdir/"*crt?.o"], libgcc
162 # Create the GCC specs file
163 # See https://gcc.gnu.org/onlinedocs/gcc/Spec-Files.html
165 # Locate the specs file
166 specs = libgcc/"specs"
167 ohai "Creating the GCC specs file: #{specs}"
168 specs_orig = Pathname.new("#{specs}.orig")
169 rm([specs_orig, specs].select(&:exist?))
171 system_header_dirs = ["#{HOMEBREW_PREFIX}/include"]
174 # https://github.com/Linuxbrew/brew/issues/724
175 system_header_dirs << glibc.opt_include
177 # Locate the native system header dirs if user uses system glibc
178 target = Utils.safe_popen_read(gcc, "-print-multiarch").chomp
179 raise "command failed: #{gcc} -print-multiarch" if $CHILD_STATUS.exitstatus.nonzero?
181 system_header_dirs += ["/usr/include/#{target}", "/usr/include"]
184 # Save a backup of the default specs file
185 specs_string = Utils.safe_popen_read(gcc, "-dumpspecs")
186 raise "command failed: #{gcc} -dumpspecs" if $CHILD_STATUS.exitstatus.nonzero?
188 specs_orig.write specs_string
190 # Set the library search path
192 # * `-isysroot #{HOMEBREW_PREFIX}/nonexistent` prevents gcc searching built-in
193 # system header files.
194 # * `-idirafter <dir>` instructs gcc to search system header
195 # files after gcc internal header files.
197 # * `-nostdlib -L#{libgcc} -L#{glibc.opt_lib}` instructs gcc to use brewed glibc
199 # * `-L#{libdir}` instructs gcc to find the corresponding gcc
200 # libraries. It is essential if there are multiple brewed gcc
201 # with different versions installed.
202 # Noted that it should only be passed for the `gcc@*` formulae.
203 # * `-L#{HOMEBREW_PREFIX}/lib` instructs gcc to find the rest
205 # Note: *link will silently add #{libdir} first to the RPATH
206 libdir = HOMEBREW_PREFIX/"lib/gcc/#{version.major}"
207 specs.write specs_string + <<~EOS
209 + -isysroot #{HOMEBREW_PREFIX}/nonexistent #{system_header_dirs.map { |p| "-idirafter #{p}" }.join(" ")}
212 #{glibc_installed ? "-nostdlib -L#{libgcc} -L#{glibc.opt_lib}" : "+"} -L#{libdir} -L#{HOMEBREW_PREFIX}/lib
215 + --dynamic-linker #{HOMEBREW_PREFIX}/lib/ld.so -rpath #{libdir}
218 -rpath #{HOMEBREW_PREFIX}/lib
221 inreplace(specs, " %o ", "\\0%(homebrew_rpath) ")
226 (testpath/"hello-c.c").write <<~C
230 puts("Hello, world!");
234 system bin/"gcc-#{version.major}", "-o", "hello-c", "hello-c.c"
235 assert_equal "Hello, world!\n", shell_output("./hello-c")
237 (testpath/"hello-cc.cc").write <<~CPP
239 struct exception { };
242 std::cout << "Hello, world!" << std::endl;
243 try { throw exception{}; }
244 catch (exception) { }
249 system bin/"g++-#{version.major}", "-o", "hello-cc", "hello-cc.cc"
250 assert_equal "Hello, world!\n", shell_output("./hello-cc")
252 (testpath/"test.f90").write <<~FORTRAN
253 integer,parameter::m=10000
257 do concurrent (i=1:m)
258 a(i) = a(i) + fact*b(i)
260 write(*,"(A)") "Done"
263 system bin/"gfortran-#{version.major}", "-o", "test", "test.f90"
264 assert_equal "Done\n", shell_output("./test")
266 return unless Hardware::CPU.intel?
268 (testpath/"hello_d.d").write <<~D
272 writeln("Hello, world!");
276 system bin/"gdc-#{version.major}", "-o", "hello-d", "hello_d.d"
277 assert_equal "Hello, world!\n", shell_output("./hello-d")