wycats / ruby-spidermonkey

A Ruby Binding to Spidermonkey

ruby-spidermonkey / pkg-config.rb
100644 127 lines (105 sloc) 2.783 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#
# pkg-config.rb
#
# Wrapper of pkg-config tool.
#
# Copyright(C) 2003-2005 Ruby-GNOME2 Project.
#
# This program is licenced under the same
# license of Ruby-GNOME2.
#
 
require 'mkmf'
require 'shellwords'
 
module PKGConfig
  @@cmd = with_config('pkg-config', ENV["PKG_CONFIG"] || 'pkg-config')
  if /mswin32/ =~ RUBY_PLATFORM and /^cl\b/ =~ Config::CONFIG['CC']
    @@cmd += ' --msvc-syntax'
  end
 
  @@list = {}
  `#{@@cmd} --list-all`.chomp.split(/\n/).each{|v|
    pkg, name, desc = /(\S+?)\s+(.*?)\s-\s(.*)/.match(v).to_a[1..3]
    @@list[pkg] = [name, desc]
  }
 
  module_function
  def exist?(pkg)
    system("#{@@cmd} --exists #{pkg}")
  end
 
  def libs(pkg)
    `#{@@cmd} --libs #{pkg}`.chomp
  end
 
  def libs_only_L(pkg)
    `#{@@cmd} --libs-only-L #{pkg}`.chomp
  end
 
  def libs_only_l(pkg)
    `#{@@cmd} --libs-only-l #{pkg}`.chomp
  end
 
  def cflags(pkg)
    `#{@@cmd} --cflags #{pkg}`.chomp
  end
 
  def cflags_only_I(pkg)
    `#{@@cmd} --cflags-only-I #{pkg}`.chomp
  end
 
  def cflags_only_other(pkg)
    `#{@@cmd} --cflags-only-other #{pkg}`.chomp
  end
 
  def variable(pkg, var)
    `#{@@cmd} --variable=#{var} #{pkg}`.chomp
  end
 
  def modversion(pkg)
    `#{@@cmd} --modversion #{pkg}`.chomp
  end
 
  def version
    `#{@@cmd} --version`.chomp
  end
 
  def list_all
    # Returns [pkg, name, description]
    @@list.keys.collect{|key| [key] + @@list[key]}.sort
  end
 
  def name(pkg)
    @@list[pkg][0]
  end
 
  def description(pkg)
    @@list[pkg][1]
  end
 
  def provides(pkg)
    `#{@@cmd} --print-provides #{pkg}`.chomp
  end
 
  def requires(pkg)
    `#{@@cmd} --print-requires #{pkg}`.chomp.gsub("\n", ", ")
  end
 
  def check_version?(pkg, major = 0, minor = 0, micro = 0)
    return false unless exist?(pkg)
    ver = modversion(pkg).split(".").collect{|item| item.to_i}
    (0..2).each {|i| ver[i] = 0 unless ver[i]}
 
    (ver[0] > major ||
     (ver[0] == major && ver[1] > minor) ||
     (ver[0] == major && ver[1] == minor &&
      ver[2] >= micro))
  end
 
  def have_package(pkg, major = nil, minor = 0, micro = 0)
    if major.nil?
      STDOUT.print("checking for #{pkg}... ")
    else
      STDOUT.print("checking for #{pkg} version (>= #{major}.#{minor}.#{micro})... ")
    end
    major ||= 0
    STDOUT.flush
    if check_version?(pkg, major, minor, micro)
      STDOUT.print "yes\n"
      libraries = libs_only_l(pkg)
      dldflags = libs(pkg)
      dldflags = (Shellwords.shellwords(dldflags) - Shellwords.shellwords(libraries)).map{|s| /\s/ =~ s ? "\"#{s}\"" : s }.join(' ')
      $libs += ' ' + libraries
      if /mswin32/ =~ RUBY_PLATFORM
$DLDFLAGS += ' ' + dldflags
      else
$LDFLAGS += ' ' + dldflags
      end
      $CFLAGS += ' ' + cflags(pkg)
      true
    else
      STDOUT.print "no\n"
      false
    end
  end
end