diff --git a/Library/Homebrew/env_config.rb b/Library/Homebrew/env_config.rb index 108218aed0..e47f1ab848 100644 --- a/Library/Homebrew/env_config.rb +++ b/Library/Homebrew/env_config.rb @@ -111,6 +111,10 @@ module Homebrew description: "Output this many lines of output on formula `system` failures.", default: 15, }, + HOMEBREW_FORBIDDEN_LICENSES: { + description: "A space-separated list of licenses. Homebrew will refuse to install a " \ + "formula if that formula or any of its dependencies has a license on this list.", + }, HOMEBREW_FORCE_BREWED_CURL: { description: "If set, always use a Homebrew-installed `curl`(1) rather than the system version. " \ "Automatically set if the system version of `curl` is too old.", diff --git a/Library/Homebrew/formula_installer.rb b/Library/Homebrew/formula_installer.rb index f6a251b209..2ce2787f0c 100644 --- a/Library/Homebrew/formula_installer.rb +++ b/Library/Homebrew/formula_installer.rb @@ -17,6 +17,7 @@ require "linkage_checker" require "install" require "messages" require "cask/cask_loader" +require "cmd/install" require "find" class FormulaInstaller @@ -148,6 +149,8 @@ class FormulaInstaller def prelude Tab.clear_cache verify_deps_exist unless ignore_deps? + forbidden_license_check + check_install_sanity end @@ -1102,4 +1105,27 @@ class FormulaInstaller $stderr.puts @requirement_messages end + + def forbidden_license_check + forbidden_licenses = Homebrew::EnvConfig.forbidden_licenses.to_s.split(" ") + return if forbidden_licenses.blank? + + compute_dependencies.each do |dep, _| + next if @ignore_deps + + dep_f = dep.to_formula + next unless forbidden_licenses.include? dep_f.license + + raise CannotInstallFormulaError, <<~EOS + The installation of #{formula.name} has a dependency on #{dep.name} with a forbidden license #{dep_f.license}. + EOS + end + return if @only_deps + + return unless forbidden_licenses.include? formula.license + + raise CannotInstallFormulaError, <<~EOS + #{formula.name} has a forbidden license #{formula.license}. + EOS + end end diff --git a/Library/Homebrew/test/support/helper/spec/shared_context/integration_test.rb b/Library/Homebrew/test/support/helper/spec/shared_context/integration_test.rb index 7ecb936ec8..619eae5c80 100644 --- a/Library/Homebrew/test/support/helper/spec/shared_context/integration_test.rb +++ b/Library/Homebrew/test/support/helper/spec/shared_context/integration_test.rb @@ -161,6 +161,12 @@ RSpec.shared_context "integration test" do content = <<~RUBY url "https://brew.sh/#{name}-1.0" RUBY + + when "package_license" + content = <<~RUBY + url "https://brew.sh/#patchelf-1.0" + license "0BSD" + RUBY end Formulary.core_path(name).tap do |formula_path| diff --git a/docs/Manpage.md b/docs/Manpage.md index 2aa5bc9ed3..f6651dd689 100644 --- a/docs/Manpage.md +++ b/docs/Manpage.md @@ -1394,6 +1394,9 @@ Note that environment variables must have a value set to be detected. For exampl *Default:* `15`. + * `HOMEBREW_FORBIDDEN_LICENSES`: + A space-separated list of licenses. Homebrew will refuse to install a formula if that formula or any of its dependencies has a license on this list. + * `HOMEBREW_FORCE_BREWED_CURL`: If set, always use a Homebrew-installed `curl`(1) rather than the system version. Automatically set if the system version of `curl` is too old. diff --git a/manpages/brew.1 b/manpages/brew.1 index 525eeddfd8..28951194e7 100644 --- a/manpages/brew.1 +++ b/manpages/brew.1 @@ -1799,6 +1799,10 @@ Output this many lines of output on formula \fBsystem\fR failures\. \fIDefault:\fR \fB15\fR\. . .TP +\fBHOMEBREW_FORBIDDEN_LICENSES\fR +A space\-separated list of licenses\. Homebrew will refuse to install a formula if that formula or any of its dependencies has a license on this list\. +. +.TP \fBHOMEBREW_FORCE_BREWED_CURL\fR If set, always use a Homebrew\-installed \fBcurl\fR(1) rather than the system version\. Automatically set if the system version of \fBcurl\fR is too old\. .