Last active
August 29, 2015 14:20
-
-
Save xu-cheng/04a353ad7b5667e4f4fe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
formula = ARGV.formulae.first | |
pkg_config_name = ARGV.value("pkg-config-name") || formula.name | |
dep_formulae_list = [] | |
env_methods_list = [] | |
cflags_I = `pkg-config --cflags-only-I #{pkg_config_name}`.chomp.split. | |
map { |f| f.sub(/^-I/, "") }.map do |path| | |
if path =~ %r{^#{Regexp.escape(HOMEBREW_CELLAR.to_s)}/([^/]+)/[^/]+/(include|lib)(/.*)?} || | |
path =~ %r{^#{Regexp.escape(HOMEBREW_PREFIX.to_s)}/opt/([^/]+)/(include|lib)(/.*)?} | |
if $1 == formula.name | |
%[-I\#{#{$2}}#{$3}] | |
else | |
dep_formulae_list << $1 | |
include_or_lib = $2 | |
trailing_path = $3 | |
%[-I\#{#{$1.gsub("+", "x").gsub("-", "_")}.opt_#{include_or_lib}}#{trailing_path}] | |
end | |
elsif MacOS::X11.installed? && path =~ %r{^#{Regexp.escape(MacOS::X11.include.to_s)}(/.*)?} | |
env_methods_list << "x11" | |
next if $1.nil? || $1 == "/freetype2" | |
%[-I\#{MacOS::X11.include}#{$1}] | |
elsif path == "/usr/include/libxml2" || path == "#{MacOS.sdk_path}/usr/include/libxml2" | |
env_methods_list << "libxml2" | |
next | |
elsif path == "#{HOMEBREW_PREFIX}/include" || path == "/usr/include" || | |
path == "#{MacOS.sdk_path}/usr/include" | |
next | |
else | |
%[-I#{path}] | |
end | |
end.compact.uniq.sort | |
cflags_other = `pkg-config --cflags-only-other #{pkg_config_name}`.chomp.split.uniq.sort | |
ldflags_L = `pkg-config --libs-only-L #{pkg_config_name}`.chomp.split. | |
map { |f| f.sub(/^-L/, "") }.map do |path| | |
if path =~ %r{^#{Regexp.escape(HOMEBREW_CELLAR.to_s)}/([^/]+)/[^/]+/(include|lib)(/.*)?} || | |
path =~ %r{^#{Regexp.escape(HOMEBREW_PREFIX.to_s)}/opt/([^/]+)/(include|lib)(/.*)?} | |
if $1 == formula.name | |
%[-L\#{#{$2}}#{$3}] | |
else | |
dep_formulae_list << $1 | |
include_or_lib = $2 | |
trailing_path = $3 | |
%[-L\#{#{$1.gsub("+", "x").gsub("-", "_")}.opt_#{include_or_lib}}#{trailing_path}] | |
end | |
elsif MacOS::X11.installed? && path =~ %r{^#{Regexp.escape(MacOS::X11.lib.to_s)}(/.*)?} | |
env_methods_list << "x11" | |
next if $1.nil? | |
%[-L\#{MacOS::X11.lib}#{$1}] | |
elsif path == "#{HOMEBREW_PREFIX}/lib" || path == "/usr/lib" || | |
path == "#{MacOS.sdk_path}/usr/lib" | |
next | |
else | |
%[-L#{path}] | |
end | |
end.compact.uniq.sort | |
ldflags_l = `pkg-config --libs-only-l #{pkg_config_name}`.chomp.split.uniq.sort | |
ldflags_other = `pkg-config --libs-only-other #{pkg_config_name}`.chomp.split.uniq.sort | |
all_flags = cflags_I + cflags_other + ldflags_L + ldflags_l + ldflags_other | |
res = "" | |
res += env_methods_list.uniq.sort.map { |method| "ENV.#{method}\n" }.join | |
res += dep_formulae_list.uniq.sort.map { |f| %[#{f.gsub("+", "x").gsub("-", "_")} = Formula["#{f}"]\n] }.join | |
res += <<-EOS.undent | |
flags = (ENV.cflags || "").split + (ENV.cppflags || "").split + (ENV.ldflags || "").split | |
flags += %W[ | |
EOS | |
res += all_flags.map { |flag| " #{flag}\n" }.join | |
res += "]" | |
puts res |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@tschoonj Updated to let you manually pass the pkg-config name.