Skip to content

Instantly share code, notes, and snippets.

@xu-cheng
Last active August 29, 2015 14:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xu-cheng/04a353ad7b5667e4f4fe to your computer and use it in GitHub Desktop.
Save xu-cheng/04a353ad7b5667e4f4fe to your computer and use it in GitHub Desktop.
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
@xu-cheng
Copy link
Author

Run with

chmod +x ./brew-pkg-config.rb
export PATH=$PWD:$PATH
brew pkg-config <formula-name> [--pkg-config-name=<pkg-config-search-name>]

e.g.

$ brew pkg-config fontconfig
freetype = Formula["freetype"]
flags = ENV["CPPFLAGS"].split + ENV["LDFLAGS"].split
flags += %W[
  -I#{freetype.opt_include}/freetype2
  -I#{include}
  -L#{freetype.opt_lib}
  -L#{lib}
  -lfontconfig
  -lfreetype
]

@tschoonj
Copy link

That's some impressive work!

Unfortunately it's not working for me. The reason is that generally speaking the Homebrew formula names do not match the corresponding pkg-config names. You got lucky with fontconfig, but if you try it for libxml++ it fails since the pkg-config name is libxml++-2.6

@xu-cheng
Copy link
Author

@tschoonj Updated to let you manually pass the pkg-config name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment