2 desc "GNU compiler collection"
3 homepage "https://gcc.gnu.org/"
4 url "https://ftp.gnu.org/gnu/gcc/gcc-8.5.0/gcc-8.5.0.tar.xz"
5 mirror "https://ftpmirror.gnu.org/gcc/gcc-8.5.0/gcc-8.5.0.tar.xz"
6 sha256 "d308841a511bb830a6100397b0042db24ce11f642dab6ea6ee44842e5325ed50"
9 "GPL-3.0-or-later" => { with: "GCC-exception-3.1" },
14 sha256 monterey: "438d5902e5f21a5e8acb5920f1f5684ecfe0c645247d46c8d44c2bbe435966b2"
15 sha256 big_sur: "9bd772c8e9c9c27f5f02fcf8b1ea99aaba24f6913b0187362659a9080d1b7eb5"
16 sha256 catalina: "fd121adf0ae07df5d1cc03c851fb1da72fa531ca197adf5f0201124c78996337"
17 sha256 cellar: :any_skip_relocation, x86_64_linux: "fea151773e9877896dad386c3df913036b6be075e72727edb86572e264ed44e1"
20 # Unsupported per https://gcc.gnu.org/gcc-8/
21 # Last release on 2021-05-14
22 disable! date: "2024-02-22", because: :deprecated_upstream
24 # The bottles are built on systems with the CLT installed, and do not work
25 # out of the box on Xcode-only systems due to an incorrect sysroot.
26 pour_bottle? only_if: :clt_installed
28 depends_on maximum_macos: [:monterey, :build]
29 depends_on arch: :x86_64
35 uses_from_macos "zlib"
41 # GCC bootstraps itself, so it is OK to have an incompatible C++ stdlib
49 # Fix flat namespace use on macOS.
60 configure_paths.each do |path|
61 inreplace buildpath/path/"configure", "${wl}-flat_namespace ${wl}-undefined ${wl}suppress",
62 "${wl}-undefined ${wl}dynamic_lookup"
65 # GCC will suffer build errors if forced to use a particular linker.
68 # Even when suffixes are appended, the info pages conflict when
69 # install-info is run so pretend we have an outdated makeinfo
70 # to prevent their build.
71 ENV["gcc_cv_prog_makeinfo_modern"] = "no"
73 # We avoiding building:
74 # - Ada, which requires a pre-existing GCC Ada compiler to bootstrap
75 # - Go, currently not supported on macOS
77 languages = %w[c c++ objc obj-c++ fortran]
79 pkgversion = "Homebrew GCC #{pkg_version} #{build.used_options*" "}".strip
83 --libdir=#{lib}/gcc/#{version_suffix}
85 --enable-checking=release
86 --enable-languages=#{languages.join(",")}
87 --program-suffix=-#{version_suffix}
88 --with-gmp=#{Formula["gmp"].opt_prefix}
89 --with-mpfr=#{Formula["mpfr"].opt_prefix}
90 --with-mpc=#{Formula["libmpc"].opt_prefix}
91 --with-isl=#{Formula["isl"].opt_prefix}
92 --with-pkgversion=#{pkgversion}
93 --with-bugurl=#{tap.issues_url}
97 args << "--build=x86_64-apple-darwin#{OS.kernel_version.major}"
98 args << "--with-system-zlib"
100 # Xcode 10 dropped 32-bit support
101 args << "--disable-multilib" if DevelopmentTools.clang_build_version >= 1000
103 # System headers may not be in /usr/include
104 sdk = MacOS.sdk_path_if_needed
106 args << "--with-native-system-header-dir=/usr/include"
107 args << "--with-sysroot=#{sdk}"
110 # Workaround for Xcode 12.5 bug on Intel
111 # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100340
112 args << "--without-build-config" if DevelopmentTools.clang_build_version >= 1205
114 # Ensure correct install names when linking against libgcc_s;
115 # see discussion in https://github.com/Homebrew/legacy-homebrew/pull/34303
116 inreplace "libgcc/config/t-slibgcc-darwin", "@shlib_slibdir@", "#{HOMEBREW_PREFIX}/lib/gcc/#{version_suffix}"
118 # Fix Linux error: gnu/stubs-32.h: No such file or directory.
119 args << "--disable-multilib"
121 # Change the default directory name for 64-bit libraries to `lib`
122 # https://www.linuxfromscratch.org/lfs/view/development/chapter06/gcc-pass2.html
123 inreplace "gcc/config/i386/t-linux64", "m64=../lib64", "m64="
127 system "../configure", *args
130 # Use -headerpad_max_install_names in the build,
131 # otherwise updated load commands won't fit in the Mach-O header.
132 # This is needed because `gcc` avoids the superenv shim.
133 system "make", "BOOT_LDFLAGS=-Wl,-headerpad_max_install_names"
134 system "make", "install"
137 system "make", "install-strip"
141 # Handle conflicts between GCC formulae and avoid interfering
142 # with system compilers.
144 Dir.glob(man7/"*.7") { |file| add_suffix file, version_suffix }
145 # Even when we disable building info pages some are still installed.
149 def add_suffix(file, suffix)
150 dir = File.dirname(file)
151 ext = File.extname(file)
152 base = File.basename(file, ext)
153 File.rename file, "#{dir}/#{base}-#{suffix}#{ext}"
158 gcc = bin/"gcc-#{version_suffix}"
159 libgcc = Pathname.new(Utils.safe_popen_read(gcc, "-print-libgcc-file-name")).parent
160 raise "command failed: #{gcc} -print-libgcc-file-name" if $CHILD_STATUS.exitstatus.nonzero?
162 glibc = Formula["glibc"]
163 glibc_installed = glibc.any_version_installed?
165 # Symlink crt1.o and friends where gcc can find it.
166 crtdir = if glibc_installed
169 Pathname.new(Utils.safe_popen_read("/usr/bin/cc", "-print-file-name=crti.o")).parent
171 ln_sf Dir[crtdir/"*crt?.o"], libgcc
173 # Create the GCC specs file
174 # See https://gcc.gnu.org/onlinedocs/gcc/Spec-Files.html
176 # Locate the specs file
177 specs = libgcc/"specs"
178 ohai "Creating the GCC specs file: #{specs}"
179 specs_orig = Pathname.new("#{specs}.orig")
180 rm([specs_orig, specs].select(&:exist?))
182 system_header_dirs = ["#{HOMEBREW_PREFIX}/include"]
185 # https://github.com/Linuxbrew/brew/issues/724
186 system_header_dirs << glibc.opt_include
188 # Locate the native system header dirs if user uses system glibc
189 target = Utils.safe_popen_read(gcc, "-print-multiarch").chomp
190 raise "command failed: #{gcc} -print-multiarch" if $CHILD_STATUS.exitstatus.nonzero?
192 system_header_dirs += ["/usr/include/#{target}", "/usr/include"]
195 # Save a backup of the default specs file
196 specs_string = Utils.safe_popen_read(gcc, "-dumpspecs")
197 raise "command failed: #{gcc} -dumpspecs" if $CHILD_STATUS.exitstatus.nonzero?
199 specs_orig.write specs_string
201 # Set the library search path
203 # * `-isysroot #{HOMEBREW_PREFIX}/nonexistent` prevents gcc searching built-in
204 # system header files.
205 # * `-idirafter <dir>` instructs gcc to search system header
206 # files after gcc internal header files.
208 # * `-nostdlib -L#{libgcc} -L#{glibc.opt_lib}` instructs gcc to use brewed glibc
210 # * `-L#{libdir}` instructs gcc to find the corresponding gcc
211 # libraries. It is essential if there are multiple brewed gcc
212 # with different versions installed.
213 # Noted that it should only be passed for the `gcc@*` formulae.
214 # * `-L#{HOMEBREW_PREFIX}/lib` instructs gcc to find the rest
216 libdir = HOMEBREW_PREFIX/"lib/gcc/#{version_suffix}"
217 specs.write specs_string + <<~EOS
219 + -isysroot #{HOMEBREW_PREFIX}/nonexistent #{system_header_dirs.map { |p| "-idirafter #{p}" }.join(" ")}
222 #{glibc_installed ? "-nostdlib -L#{libgcc} -L#{glibc.opt_lib}" : "+"} -L#{libdir} -L#{HOMEBREW_PREFIX}/lib
225 + --dynamic-linker #{HOMEBREW_PREFIX}/lib/ld.so -rpath #{libdir} -rpath #{HOMEBREW_PREFIX}/lib
232 (testpath/"hello-c.c").write <<~C
236 puts("Hello, world!");
240 system bin/"gcc-#{version.major}", "-o", "hello-c", "hello-c.c"
241 assert_equal "Hello, world!\n", `./hello-c`
243 (testpath/"hello-cc.cc").write <<~CPP
245 struct exception { };
248 std::cout << "Hello, world!" << std::endl;
249 try { throw exception{}; }
250 catch (exception) { }
255 system bin/"g++-#{version.major}", "-o", "hello-cc", "hello-cc.cc"
256 assert_equal "Hello, world!\n", `./hello-cc`
258 (testpath/"test.f90").write <<~FORTRAN
259 integer,parameter::m=10000
263 do concurrent (i=1:m)
264 a(i) = a(i) + fact*b(i)
266 write(*,"(A)") "Done"
269 system bin/"gfortran-#{version.major}", "-o", "test", "test.f90"
270 assert_equal "Done\n", `./test`