From 221983dbcff442fa98d28c36cb2fb74af376d937 Mon Sep 17 00:00:00 2001 From: Michka Popoff Date: Wed, 3 Feb 2021 21:28:11 +0100 Subject: [PATCH] compilers: prefer gcc 5 on linux Fixes #10170 by preferring gcc@5 on linux This makes sure ENV.cc and ENV.cxx is correctly set: If a formula does not explicitely depend on a brewed gcc, ENV.cc is set to gcc-5 (system gcc-5 or brewed gcc-5) with this change, even if other gcc versions are installed on the system. --- Library/Homebrew/compilers.rb | 10 +++++++++- Library/Homebrew/extend/os/compilers.rb | 4 ++++ Library/Homebrew/extend/os/linux/compilers.rb | 11 +++++++++++ .../Homebrew/test/compiler_selector_spec.rb | 18 +++++++++++++++++- 4 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 Library/Homebrew/extend/os/compilers.rb create mode 100644 Library/Homebrew/extend/os/linux/compilers.rb diff --git a/Library/Homebrew/compilers.rb b/Library/Homebrew/compilers.rb index 8881c84133..4802530d73 100644 --- a/Library/Homebrew/compilers.rb +++ b/Library/Homebrew/compilers.rb @@ -78,6 +78,7 @@ end # # @api private class CompilerSelector + extend T::Sig include CompilerConstants Compiler = Struct.new(:name, :version) @@ -111,9 +112,14 @@ class CompilerSelector private + sig { returns(String) } + def preferred_gcc + "gcc" + end + def gnu_gcc_versions # prioritize gcc version provided by gcc formula. - v = Formulary.factory("gcc").version.to_s.slice(/\d+/) + v = Formulary.factory(preferred_gcc).version.to_s.slice(/\d+/) GNU_GCC_VERSIONS - [v] + [v] # move the version to the end of the list rescue FormulaUnavailableError GNU_GCC_VERSIONS @@ -150,3 +156,5 @@ class CompilerSelector end end end + +require "extend/os/compilers" diff --git a/Library/Homebrew/extend/os/compilers.rb b/Library/Homebrew/extend/os/compilers.rb new file mode 100644 index 0000000000..19e999889f --- /dev/null +++ b/Library/Homebrew/extend/os/compilers.rb @@ -0,0 +1,4 @@ +# typed: strict +# frozen_string_literal: true + +require "extend/os/linux/compilers" if OS.linux? diff --git a/Library/Homebrew/extend/os/linux/compilers.rb b/Library/Homebrew/extend/os/linux/compilers.rb new file mode 100644 index 0000000000..93698765de --- /dev/null +++ b/Library/Homebrew/extend/os/linux/compilers.rb @@ -0,0 +1,11 @@ +# typed: strict +# frozen_string_literal: true + +class CompilerSelector + sig { returns(String) } + def preferred_gcc + # gcc-5 is the lowest gcc version we support on Linux. + # gcc-5 is the default gcc in Ubuntu 16.04 (used for our CI) + "gcc@5" + end +end diff --git a/Library/Homebrew/test/compiler_selector_spec.rb b/Library/Homebrew/test/compiler_selector_spec.rb index 58017c2b20..46c62fe8a3 100644 --- a/Library/Homebrew/test/compiler_selector_spec.rb +++ b/Library/Homebrew/test/compiler_selector_spec.rb @@ -22,6 +22,7 @@ describe CompilerSelector do case name when "gcc-7" then Version.create("7.1") when "gcc-6" then Version.create("6.1") + when "gcc-5" then Version.create("5.1") else Version::NULL end end @@ -42,16 +43,31 @@ describe CompilerSelector do expect(selector.compiler).to eq("gcc-7") end - it "returns gcc-6 if gcc formula offers gcc-6" do + it "returns gcc-6 if gcc formula offers gcc-6 on mac", :needs_macos do software_spec.fails_with(:clang) allow(Formulary).to receive(:factory).with("gcc").and_return(double(version: "6.0")) expect(selector.compiler).to eq("gcc-6") end + it "returns gcc-5 if gcc formula offers gcc-5 on linux", :needs_linux do + software_spec.fails_with(:clang) + allow(Formulary).to receive(:factory).with("gcc@5").and_return(double(version: "5.0")) + expect(selector.compiler).to eq("gcc-5") + end + + it "returns gcc-6 if gcc formula offers gcc-6 and fails with gcc-5 and gcc-7 on linux", :needs_linux do + software_spec.fails_with(:clang) + software_spec.fails_with(gcc: "5") + software_spec.fails_with(gcc: "7") + allow(Formulary).to receive(:factory).with("gcc@5").and_return(double(version: "5.0")) + expect(selector.compiler).to eq("gcc-6") + end + it "raises an error when gcc or llvm is missing" do software_spec.fails_with(:clang) software_spec.fails_with(gcc: "7") software_spec.fails_with(gcc: "6") + software_spec.fails_with(gcc: "5") expect { selector.compiler }.to raise_error(CompilerSelectionError) end