From 74c77db5bd51c330fc1da267b58d0774d3616bab Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Thu, 26 Jul 2018 10:48:25 +0100 Subject: [PATCH 1/3] shell: tweak export_value parameters. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Let’s have a default value for shell (considering this isn’t a public API) to make it easier to use. --- Library/Homebrew/cmd/--env.rb | 4 +++- Library/Homebrew/utils/shell.rb | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/cmd/--env.rb b/Library/Homebrew/cmd/--env.rb index e0a2b9f191..3d45a8fec7 100644 --- a/Library/Homebrew/cmd/--env.rb +++ b/Library/Homebrew/cmd/--env.rb @@ -37,7 +37,9 @@ module Homebrew if shell.nil? dump_build_env ENV else - env_keys.each { |key| puts Utils::Shell.export_value(shell, key, ENV[key]) } + env_keys.each do |key| + puts Utils::Shell.export_value(key, ENV[key], shell) + end end end end diff --git a/Library/Homebrew/utils/shell.rb b/Library/Homebrew/utils/shell.rb index 7799dc1db1..fb602a2726 100644 --- a/Library/Homebrew/utils/shell.rb +++ b/Library/Homebrew/utils/shell.rb @@ -21,7 +21,7 @@ module Utils end # quote values. quoting keys is overkill - def export_value(shell, key, value) + def export_value(key, value, shell = preferred) case shell when :bash, :ksh, :sh, :zsh "export #{key}=\"#{sh_quote(value)}\"" From b89367e6d90d39c6a58fc039d09799223d56c435 Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Thu, 26 Jul 2018 10:49:07 +0100 Subject: [PATCH 2/3] caveats: tweak keg_only_text - make it a public method - allow skipping the reason - output how to set the various variables in your current shell --- Library/Homebrew/caveats.rb | 89 ++++++++++++++++++++++++------------- 1 file changed, 58 insertions(+), 31 deletions(-) diff --git a/Library/Homebrew/caveats.rb b/Library/Homebrew/caveats.rb index 9fb788a46f..0c4f56bd1b 100644 --- a/Library/Homebrew/caveats.rb +++ b/Library/Homebrew/caveats.rb @@ -30,6 +30,64 @@ class Caveats delegate [:empty?, :to_s] => :caveats + def keg_only_text(skip_reason: false) + return unless f.keg_only? + + s = if skip_reason + "" + else + <<~EOS + #{f.name} is keg-only, which means it was not symlinked into #{HOMEBREW_PREFIX}, + because #{f.keg_only_reason.to_s.chomp}. + EOS + end + + if f.bin.directory? || f.sbin.directory? + s << <<~EOS + + If you need to have #{f.name} first in your PATH run: + EOS + if f.bin.directory? + s << " #{Utils::Shell.prepend_path_in_profile(f.opt_bin.to_s)}\n" + end + if f.sbin.directory? + s << " #{Utils::Shell.prepend_path_in_profile(f.opt_sbin.to_s)}\n" + end + end + + if f.lib.directory? || f.include.directory? + s << <<~EOS + + For compilers to find #{f.name} you may need to set: + EOS + + if f.lib.directory? + s << " #{Utils::Shell.export_value("LDFLAGS", "-L#{f.opt_lib}")}\n" + end + + if f.include.directory? + s << " #{Utils::Shell.export_value("CPPFLAGS", "-I#{f.opt_include}")}\n" + end + + if which("pkg-config", ENV["HOMEBREW_PATH"]) && + ((f.lib/"pkgconfig").directory? || (f.share/"pkgconfig").directory?) + s << <<~EOS + + For pkg-config to find #{f.name} you may need to set: + EOS + + if (f.lib/"pkgconfig").directory? + s << " #{Utils::Shell.export_value("PKG_CONFIG_PATH", "#{f.opt_lib}/pkgconfig")}\n" + end + + if (f.share/"pkgconfig").directory? + s << " #{Utils::Shell.export_value("PKG_CONFIG_PATH", "#{f.opt_share}/pkgconfig")}\n" + end + end + end + s << "\n" + end + private def keg @@ -42,37 +100,6 @@ class Caveats end.compact.first end - def keg_only_text - return unless f.keg_only? - - s = <<~EOS - This formula is keg-only, which means it was not symlinked into #{HOMEBREW_PREFIX}, - because #{f.keg_only_reason.to_s.chomp}. - EOS - if f.bin.directory? || f.sbin.directory? - s << "\nIf you need to have this software first in your PATH run:\n" - if f.bin.directory? - s << " #{Utils::Shell.prepend_path_in_profile(f.opt_bin.to_s)}\n" - end - if f.sbin.directory? - s << " #{Utils::Shell.prepend_path_in_profile(f.opt_sbin.to_s)}\n" - end - end - - if f.lib.directory? || f.include.directory? - s << "\nFor compilers to find this software you may need to set:\n" - s << " LDFLAGS: -L#{f.opt_lib}\n" if f.lib.directory? - s << " CPPFLAGS: -I#{f.opt_include}\n" if f.include.directory? - if which("pkg-config", ENV["HOMEBREW_PATH"]) && - ((f.lib/"pkgconfig").directory? || (f.share/"pkgconfig").directory?) - s << "For pkg-config to find this software you may need to set:\n" - s << " PKG_CONFIG_PATH: #{f.opt_lib}/pkgconfig\n" if (f.lib/"pkgconfig").directory? - s << " PKG_CONFIG_PATH: #{f.opt_share}/pkgconfig\n" if (f.share/"pkgconfig").directory? - end - end - s << "\n" - end - def function_completion_caveats(shell) return unless keg return unless which(shell.to_s, ENV["HOMEBREW_PATH"]) From 19d87bf15f61fd5a814ca7abc7f1dc3c59846434 Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Thu, 26 Jul 2018 10:49:55 +0100 Subject: [PATCH 3/3] link: when refusing link display keg only caveats These are a bit easier to follow and have been recently improved. Inspired by conversation in #4441. --- Library/Homebrew/cmd/link.rb | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Library/Homebrew/cmd/link.rb b/Library/Homebrew/cmd/link.rb index dac871ade3..4d0d8e3155 100644 --- a/Library/Homebrew/cmd/link.rb +++ b/Library/Homebrew/cmd/link.rb @@ -13,6 +13,7 @@ #: If `--force` (or `-f`) is passed, Homebrew will allow keg-only formulae to be linked. require "ostruct" +require "caveats" module Homebrew module_function @@ -52,14 +53,16 @@ module Homebrew if keg_only if HOMEBREW_PREFIX.to_s == "/usr/local" - if keg.to_formula.keg_only_reason.reason == :provided_by_macos && + f = keg.to_formula + caveats = Caveats.new(f) + + if f.keg_only_reason.reason == :provided_by_macos && (MacOS.version >= :mojave || MacOS::Xcode.version >= "10.0" || MacOS::CLT.version >= "10.0") opoo <<~EOS Refusing to link macOS-provided software: #{keg.name} - Instead, pass the full include/library paths to your compiler e.g.: - -I#{HOMEBREW_PREFIX}/opt/#{keg.name}/include -L#{HOMEBREW_PREFIX}/opt/#{keg.name}/lib + #{caveats.keg_only_text(skip_reason: true).strip} EOS next end @@ -69,8 +72,7 @@ module Homebrew Refusing to link: #{keg.name} Linking keg-only #{keg.name} means you may end up linking against the insecure, deprecated system OpenSSL while using the headers from Homebrew's #{keg.name}. - Instead, pass the full include/library paths to your compiler e.g.: - -I#{HOMEBREW_PREFIX}/opt/#{keg.name}/include -L#{HOMEBREW_PREFIX}/opt/#{keg.name}/lib + #{caveats.keg_only_text(skip_reason: true).strip} EOS next end