Merge pull request #207012 from Homebrew/bump-tailwindcss-4.0.5
[Homebrew/homebrew-core.git] / Formula / g / gcc@11.rb
blob41e6c973b7997a91c1de9da5d4dca642e5783531
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" }
10   livecheck do
11     url :stable
12     regex(%r{href=["']?gcc[._-]v?(11(?:\.\d+)+)(?:/?["' >]|\.t)}i)
13   end
15   bottle do
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"
23   end
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
30   depends_on "gmp"
31   depends_on "isl"
32   depends_on "libmpc"
33   depends_on "mpfr"
34   depends_on "zstd"
36   uses_from_macos "zlib"
38   on_linux do
39     depends_on "binutils"
40   end
42   # GCC bootstraps itself, so it is OK to have an incompatible C++ stdlib
43   cxxstdlib_check :skip
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
47   patch do
48     url "https://raw.githubusercontent.com/Homebrew/formula-patches/5c9419923ddb3e5302ddd277bc524f4d4b0f8722/gcc/gcc-11.5.0.diff"
49     sha256 "213b332bd09452e0cf081f874f32d028911fa871875f85b200b55c5b588ce193"
50   end
52   def install
53     # GCC will suffer build errors if forced to use a particular linker.
54     ENV.delete "LD"
56     # We avoiding building:
57     #  - Ada, which requires a pre-existing GCC Ada compiler to bootstrap
58     #  - Go, currently not supported on macOS
59     #  - BRIG
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
65     args = %W[
66       --prefix=#{opt_prefix}
67       --libdir=#{opt_lib}/gcc/#{version.major}
68       --disable-nls
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}
80     ]
81     # libphobos is part of gdc
82     args << "--enable-libphobos" if Hardware::CPU.intel?
84     if OS.mac?
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"
97       end
98     else
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="
109     end
111     mkdir "build" do
112       system "../configure", *args
113       system "make"
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
124     end
126     # Handle conflicts between GCC formulae and avoid interfering
127     # with system compilers.
128     # Rename man7.
129     man7.glob("*.7") { |file| add_suffix file, version.major }
130     # Even when we disable building info pages some are still installed.
131     rm_r(info)
133     # Work around GCC install bug
134     # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105664
135     rm_r(bin.glob("*-gcc-tmp"))
136   end
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}"
143   end
145   def post_install
146     if OS.linux?
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
156         glibc.opt_lib
157       else
158         Pathname.new(Utils.safe_popen_read("/usr/bin/cc", "-print-file-name=crti.o")).parent
159       end
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"]
173       if glibc_installed
174         # https://github.com/Linuxbrew/brew/issues/724
175         system_header_dirs << glibc.opt_include
176       else
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"]
182       end
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
191       # For include 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.
196       # For libraries:
197       #   * `-nostdlib -L#{libgcc} -L#{glibc.opt_lib}` instructs gcc to use brewed glibc
198       #     if applied.
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
204       #     brew libraries.
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
208         *cpp_unique_options:
209         + -isysroot #{HOMEBREW_PREFIX}/nonexistent #{system_header_dirs.map { |p| "-idirafter #{p}" }.join(" ")}
211         *link_libgcc:
212         #{glibc_installed ? "-nostdlib -L#{libgcc} -L#{glibc.opt_lib}" : "+"} -L#{libdir} -L#{HOMEBREW_PREFIX}/lib
214         *link:
215         + --dynamic-linker #{HOMEBREW_PREFIX}/lib/ld.so -rpath #{libdir}
217         *homebrew_rpath:
218         -rpath #{HOMEBREW_PREFIX}/lib
220       EOS
221       inreplace(specs, " %o ", "\\0%(homebrew_rpath) ")
222     end
223   end
225   test do
226     (testpath/"hello-c.c").write <<~C
227       #include <stdio.h>
228       int main()
229       {
230         puts("Hello, world!");
231         return 0;
232       }
233     C
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
238       #include <iostream>
239       struct exception { };
240       int main()
241       {
242         std::cout << "Hello, world!" << std::endl;
243         try { throw exception{}; }
244           catch (exception) { }
245           catch (...) { }
246         return 0;
247       }
248     CPP
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
254       real::a(m), b(m)
255       real::fact=0.5
257       do concurrent (i=1:m)
258         a(i) = a(i) + fact*b(i)
259       end do
260       write(*,"(A)") "Done"
261       end
262     FORTRAN
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
269       import std.stdio;
270       int main()
271       {
272         writeln("Hello, world!");
273         return 0;
274       }
275     D
276     system bin/"gdc-#{version.major}", "-o", "hello-d", "hello_d.d"
277     assert_equal "Hello, world!\n", shell_output("./hello-d")
278   end