Merge pull request #207020 from Homebrew/bump-qpdf-11.10.0
[Homebrew/homebrew-core.git] / Formula / g / gcc@8.rb
blob76cc1e8e06c49e25821cbbe9103c0eca71b3a768
1 class GccAT8 < Formula
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"
7   license all_of: [
8     "LGPL-2.1-or-later",
9     "GPL-3.0-or-later" => { with: "GCC-exception-3.1" },
10   ]
12   bottle do
13     rebuild 1
14     sha256                               monterey:     "438d5902e5f21a5e8acb5920f1f5684ecfe0c645247d46c8d44c2bbe435966b2"
15     sha256                               big_sur:      "9bd772c8e9c9c27f5f02fcf8b1ea99aaba24f6913b0187362659a9080d1b7eb5"
16     sha256                               catalina:     "fd121adf0ae07df5d1cc03c851fb1da72fa531ca197adf5f0201124c78996337"
17     sha256 cellar: :any_skip_relocation, x86_64_linux: "fea151773e9877896dad386c3df913036b6be075e72727edb86572e264ed44e1"
18   end
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
30   depends_on "gmp"
31   depends_on "isl"
32   depends_on "libmpc"
33   depends_on "mpfr"
35   uses_from_macos "zlib"
37   on_linux do
38     depends_on "binutils"
39   end
41   # GCC bootstraps itself, so it is OK to have an incompatible C++ stdlib
42   cxxstdlib_check :skip
44   def version_suffix
45     version.major.to_s
46   end
48   def install
49     # Fix flat namespace use on macOS.
50     configure_paths = %w[
51       libatomic
52       libgfortran
53       libgomp
54       libitm
55       libobjc
56       libquadmath
57       libssp
58       libstdc++-v3
59     ]
60     configure_paths.each do |path|
61       inreplace buildpath/path/"configure", "${wl}-flat_namespace ${wl}-undefined ${wl}suppress",
62                                             "${wl}-undefined ${wl}dynamic_lookup"
63     end
65     # GCC will suffer build errors if forced to use a particular linker.
66     ENV.delete "LD"
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
76     #  - BRIG
77     languages = %w[c c++ objc obj-c++ fortran]
79     pkgversion = "Homebrew GCC #{pkg_version} #{build.used_options*" "}".strip
81     args = %W[
82       --prefix=#{prefix}
83       --libdir=#{lib}/gcc/#{version_suffix}
84       --disable-nls
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}
94     ]
96     if OS.mac?
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
105       if sdk
106         args << "--with-native-system-header-dir=/usr/include"
107         args << "--with-sysroot=#{sdk}"
108       end
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}"
117     else
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="
124     end
126     mkdir "build" do
127       system "../configure", *args
129       if OS.mac?
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"
135       else
136         system "make"
137         system "make", "install-strip"
138       end
139     end
141     # Handle conflicts between GCC formulae and avoid interfering
142     # with system compilers.
143     # Rename man7.
144     Dir.glob(man7/"*.7") { |file| add_suffix file, version_suffix }
145     # Even when we disable building info pages some are still installed.
146     rm_r(info)
147   end
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}"
154   end
156   def post_install
157     if OS.linux?
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
167         glibc.opt_lib
168       else
169         Pathname.new(Utils.safe_popen_read("/usr/bin/cc", "-print-file-name=crti.o")).parent
170       end
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"]
184       if glibc_installed
185         # https://github.com/Linuxbrew/brew/issues/724
186         system_header_dirs << glibc.opt_include
187       else
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"]
193       end
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
202       # For include 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.
207       # For libraries:
208       #   * `-nostdlib -L#{libgcc} -L#{glibc.opt_lib}` instructs gcc to use brewed glibc
209       #     if applied.
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
215       #     brew libraries.
216       libdir = HOMEBREW_PREFIX/"lib/gcc/#{version_suffix}"
217       specs.write specs_string + <<~EOS
218         *cpp_unique_options:
219         + -isysroot #{HOMEBREW_PREFIX}/nonexistent #{system_header_dirs.map { |p| "-idirafter #{p}" }.join(" ")}
221         *link_libgcc:
222         #{glibc_installed ? "-nostdlib -L#{libgcc} -L#{glibc.opt_lib}" : "+"} -L#{libdir} -L#{HOMEBREW_PREFIX}/lib
224         *link:
225         + --dynamic-linker #{HOMEBREW_PREFIX}/lib/ld.so -rpath #{libdir} -rpath #{HOMEBREW_PREFIX}/lib
227       EOS
228     end
229   end
231   test do
232     (testpath/"hello-c.c").write <<~C
233       #include <stdio.h>
234       int main()
235       {
236         puts("Hello, world!");
237         return 0;
238       }
239     C
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
244       #include <iostream>
245       struct exception { };
246       int main()
247       {
248         std::cout << "Hello, world!" << std::endl;
249         try { throw exception{}; }
250           catch (exception) { }
251           catch (...) { }
252         return 0;
253       }
254     CPP
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
260       real::a(m), b(m)
261       real::fact=0.5
263       do concurrent (i=1:m)
264         a(i) = a(i) + fact*b(i)
265       end do
266       write(*,"(A)") "Done"
267       end
268     FORTRAN
269     system bin/"gfortran-#{version.major}", "-o", "test", "test.f90"
270     assert_equal "Done\n", `./test`
271   end