Imported Upstream version 1.46.0
[platform/upstream/nghttp2.git] / third-party / mruby / lib / mruby / gem.rb
1 require 'forwardable'
2 autoload :TSort, 'tsort'
3 autoload :Shellwords, 'shellwords'
4
5 module MRuby
6   module Gem
7     class << self
8       attr_accessor :current
9     end
10     LinkerConfig = Struct.new(:libraries, :library_paths, :flags, :flags_before_libraries, :flags_after_libraries)
11
12     class Specification
13       include Rake::DSL
14       extend Forwardable
15       def_delegators :@build, :filename, :objfile, :libfile, :exefile
16
17       attr_accessor :name, :dir, :build
18       alias mruby build
19       attr_accessor :build_config_initializer
20       attr_accessor :mrblib_dir, :objs_dir
21
22       attr_accessor :version
23       attr_accessor :description, :summary
24       attr_accessor :homepage
25       attr_accessor :licenses, :authors
26       alias :license= :licenses=
27       alias :author= :authors=
28
29       attr_accessor :rbfiles, :objs
30       attr_accessor :test_objs, :test_rbfiles, :test_args
31       attr_accessor :test_preload
32
33       attr_accessor :bins
34
35       attr_accessor :requirements
36       attr_reader :dependencies, :conflicts
37
38       attr_accessor :export_include_paths
39
40       attr_reader :generate_functions
41
42       attr_block MRuby::Build::COMMANDS
43
44       def initialize(name, &block)
45         @name = name
46         @initializer = block
47         @version = "0.0.0"
48         @mrblib_dir = "mrblib"
49         @objs_dir = "src"
50         MRuby::Gem.current = self
51       end
52
53       def setup
54         return if defined?(@linker)  # return if already set up
55
56         MRuby::Gem.current = self
57         MRuby::Build::COMMANDS.each do |command|
58           instance_variable_set("@#{command}", @build.send(command).clone)
59         end
60         @linker = LinkerConfig.new([], [], [], [], [])
61
62         @rbfiles = Dir.glob("#{@dir}/#{@mrblib_dir}/**/*.rb").sort
63         @objs = Dir.glob("#{@dir}/#{@objs_dir}/*.{c,cpp,cxx,cc,m,asm,s,S}").map do |f|
64           objfile(f.relative_path_from(@dir).to_s.pathmap("#{build_dir}/%X"))
65         end
66
67         @test_rbfiles = Dir.glob("#{dir}/test/**/*.rb").sort
68         @test_objs = Dir.glob("#{dir}/test/*.{c,cpp,cxx,cc,m,asm,s,S}").map do |f|
69           objfile(f.relative_path_from(dir).to_s.pathmap("#{build_dir}/%X"))
70         end
71         @custom_test_init = !@test_objs.empty?
72         @test_preload = nil # 'test/assert.rb'
73         @test_args = {}
74
75         @bins = []
76
77         @requirements = []
78         @dependencies, @conflicts = [], []
79         @export_include_paths = []
80         @export_include_paths << "#{dir}/include" if File.directory? "#{dir}/include"
81
82         instance_eval(&@initializer)
83
84         @generate_functions = !(@rbfiles.empty? && @objs.empty?)
85         @objs << objfile("#{build_dir}/gem_init") if @generate_functions
86
87         if !name || !licenses || !authors
88           fail "#{name || dir} required to set name, license(s) and author(s)"
89         end
90
91         build.libmruby_objs << @objs
92
93         instance_eval(&@build_config_initializer) if @build_config_initializer
94
95         repo_url = build.gem_dir_to_repo_url[dir]
96         build.locks[repo_url]['version'] = version if repo_url
97       end
98
99       def setup_compilers
100         compilers.each do |compiler|
101           compiler.define_rules build_dir, "#{dir}"
102           compiler.defines << %Q[MRBGEM_#{funcname.upcase}_VERSION=#{version}]
103           compiler.include_paths << "#{dir}/include" if File.directory? "#{dir}/include"
104         end
105
106         define_gem_init_builder if @generate_functions
107       end
108
109       def for_windows?
110         if build.kind_of?(MRuby::CrossBuild)
111           return %w(x86_64-w64-mingw32 i686-w64-mingw32).include?(build.host_target)
112         elsif build.kind_of?(MRuby::Build)
113           return ('A'..'Z').to_a.any? { |vol| Dir.exist?("#{vol}:") }
114         end
115         return false
116       end
117
118       def add_dependency(name, *requirements)
119         default_gem = requirements.last.kind_of?(Hash) ? requirements.pop : nil
120         requirements = ['>= 0.0.0'] if requirements.empty?
121         requirements.flatten!
122         @dependencies << {:gem => name, :requirements => requirements, :default => default_gem}
123       end
124
125       def add_test_dependency(*args)
126         add_dependency(*args) if build.test_enabled? || build.bintest_enabled?
127       end
128
129       def add_conflict(name, *req)
130         @conflicts << {:gem => name, :requirements => req.empty? ? nil : req}
131       end
132
133       def build_dir
134         "#{build.build_dir}/mrbgems/#{name}"
135       end
136
137       def test_rbireps
138         "#{build_dir}/gem_test.c"
139       end
140
141       def search_package(name, version_query=nil)
142         package_query = name
143         package_query += " #{version_query}" if version_query
144         _pp "PKG-CONFIG", package_query
145         escaped_package_query = Shellwords.escape(package_query)
146         if system("pkg-config --exists #{escaped_package_query}")
147           cc.flags += [`pkg-config --cflags #{escaped_package_query}`.strip]
148           cxx.flags += [`pkg-config --cflags #{escaped_package_query}`.strip]
149           linker.flags_before_libraries += [`pkg-config --libs #{escaped_package_query}`.strip]
150           true
151         else
152           false
153         end
154       end
155
156       def funcname
157         @funcname ||= @name.gsub('-', '_')
158       end
159
160       def compilers
161         MRuby::Build::COMPILERS.map do |c|
162           instance_variable_get("@#{c}")
163         end
164       end
165
166       def define_gem_init_builder
167         file objfile("#{build_dir}/gem_init") => [ "#{build_dir}/gem_init.c", File.join(dir, "mrbgem.rake") ]
168         file "#{build_dir}/gem_init.c" => [build.mrbcfile, __FILE__] + [rbfiles].flatten do |t|
169           mkdir_p build_dir
170           generate_gem_init("#{build_dir}/gem_init.c")
171         end
172       end
173
174       def generate_gem_init(fname)
175         open(fname, 'w') do |f|
176           print_gem_init_header f
177           build.mrbc.run f, rbfiles, "gem_mrblib_irep_#{funcname}" unless rbfiles.empty?
178           f.puts %Q[void mrb_#{funcname}_gem_init(mrb_state *mrb);]
179           f.puts %Q[void mrb_#{funcname}_gem_final(mrb_state *mrb);]
180           f.puts %Q[]
181           f.puts %Q[void GENERATED_TMP_mrb_#{funcname}_gem_init(mrb_state *mrb) {]
182           f.puts %Q[  int ai = mrb_gc_arena_save(mrb);]
183           f.puts %Q[  mrb_#{funcname}_gem_init(mrb);] if objs != [objfile("#{build_dir}/gem_init")]
184           unless rbfiles.empty?
185             f.puts %Q[  mrb_load_irep(mrb, gem_mrblib_irep_#{funcname});]
186             f.puts %Q[  if (mrb->exc) {]
187             f.puts %Q[    mrb_print_error(mrb);]
188             f.puts %Q[    mrb_close(mrb);]
189             f.puts %Q[    exit(EXIT_FAILURE);]
190             f.puts %Q[  }]
191           end
192           f.puts %Q[  mrb_gc_arena_restore(mrb, ai);]
193           f.puts %Q[}]
194           f.puts %Q[]
195           f.puts %Q[void GENERATED_TMP_mrb_#{funcname}_gem_final(mrb_state *mrb) {]
196           f.puts %Q[  mrb_#{funcname}_gem_final(mrb);] if objs != [objfile("#{build_dir}/gem_init")]
197           f.puts %Q[}]
198         end
199       end # generate_gem_init
200
201       def print_gem_comment(f)
202         f.puts %Q[/*]
203         f.puts %Q[ * This file is loading the irep]
204         f.puts %Q[ * Ruby GEM code.]
205         f.puts %Q[ *]
206         f.puts %Q[ * IMPORTANT:]
207         f.puts %Q[ *   This file was generated!]
208         f.puts %Q[ *   All manual changes will get lost.]
209         f.puts %Q[ */]
210       end
211
212       def print_gem_init_header(f)
213         print_gem_comment(f)
214         f.puts %Q[#include <stdlib.h>] unless rbfiles.empty?
215         f.puts %Q[#include <mruby.h>]
216         f.puts %Q[#include <mruby/irep.h>] unless rbfiles.empty?
217       end
218
219       def print_gem_test_header(f)
220         print_gem_comment(f)
221         f.puts %Q[#include <stdio.h>]
222         f.puts %Q[#include <stdlib.h>]
223         f.puts %Q[#include <mruby.h>]
224         f.puts %Q[#include <mruby/irep.h>]
225         f.puts %Q[#include <mruby/variable.h>]
226         f.puts %Q[#include <mruby/hash.h>] unless test_args.empty?
227       end
228
229       def test_dependencies
230         [@name]
231       end
232
233       def custom_test_init?
234         @custom_test_init
235       end
236
237       def version_ok?(req_versions)
238         req_versions.map do |req|
239           cmp, ver = req.split
240           cmp_result = Version.new(version) <=> Version.new(ver)
241           case cmp
242           when '=' then cmp_result == 0
243           when '!=' then cmp_result != 0
244           when '>' then cmp_result == 1
245           when '<' then cmp_result == -1
246           when '>=' then cmp_result >= 0
247           when '<=' then cmp_result <= 0
248           when '~>'
249             Version.new(version).twiddle_wakka_ok?(Version.new(ver))
250           else
251             fail "Comparison not possible with '#{cmp}'"
252           end
253         end.all?
254       end
255     end # Specification
256
257     class Version
258       include Comparable
259       include Enumerable
260
261       def <=>(other)
262         ret = 0
263         own = to_enum
264
265         other.each do |oth|
266           begin
267             ret = own.next <=> oth
268           rescue StopIteration
269             ret = 0 <=> oth
270           end
271
272           break unless ret == 0
273         end
274
275         ret
276       end
277
278       # ~> compare algorithm
279       #
280       # Example:
281       #    ~> 2     means >= 2.0.0 and < 3.0.0
282       #    ~> 2.2   means >= 2.2.0 and < 3.0.0
283       #    ~> 2.2.2 means >= 2.2.2 and < 2.3.0
284       def twiddle_wakka_ok?(other)
285         gr_or_eql = (self <=> other) >= 0
286         still_major_or_minor = (self <=> other.skip_major_or_minor) < 0
287         gr_or_eql and still_major_or_minor
288       end
289
290       def skip_major_or_minor
291         a = @ary.dup
292         a << 0 if a.size == 1 # ~> 2 can also be represented as ~> 2.0
293         a.slice!(-1)
294         a[-1] = a[-1].succ
295         a
296       end
297
298       def initialize(str)
299         @str = str
300         @ary = @str.split('.').map(&:to_i)
301       end
302
303       def each(&block); @ary.each(&block); end
304       def [](index); @ary[index]; end
305       def []=(index, value)
306         @ary[index] = value
307         @str = @ary.join('.')
308       end
309       def slice!(index)
310         @ary.slice!(index)
311         @str = @ary.join('.')
312       end
313     end # Version
314
315     class List
316       include Enumerable
317
318       def initialize
319         @ary = []
320       end
321
322       def each(&b)
323         @ary.each(&b)
324       end
325
326       def <<(gem)
327         unless @ary.detect {|g| g.dir == gem.dir }
328           @ary << gem
329         else
330           # GEM was already added to this list
331         end
332       end
333
334       def empty?
335         @ary.empty?
336       end
337
338       def default_gem_params dep
339         if dep[:default]; dep
340         elsif File.exist? "#{MRUBY_ROOT}/mrbgems/#{dep[:gem]}" # check core
341           { :gem => dep[:gem], :default => { :core => dep[:gem] } }
342         else # fallback to mgem-list
343           { :gem => dep[:gem], :default => { :mgem => dep[:gem] } }
344         end
345       end
346
347       def generate_gem_table build
348         gem_table = each_with_object({}) { |spec, h| h[spec.name] = spec }
349
350         default_gems = {}
351         each do |g|
352           g.dependencies.each do |dep|
353             default_gems[dep[:gem]] ||= default_gem_params(dep)
354           end
355         end
356
357         until default_gems.empty?
358           def_name, def_gem = default_gems.shift
359           next if gem_table[def_name]
360
361           spec = gem_table[def_name] = build.gem(def_gem[:default])
362           fail "Invalid gem name: #{spec.name} (Expected: #{def_name})" if spec.name != def_name
363           spec.setup
364
365           spec.dependencies.each do |dep|
366             default_gems[dep[:gem]] ||= default_gem_params(dep)
367           end
368         end
369
370         each do |g|
371           g.dependencies.each do |dep|
372             name = dep[:gem]
373             req_versions = dep[:requirements]
374             dep_g = gem_table[name]
375
376             # check each GEM dependency against all available GEMs
377             if dep_g.nil?
378               fail "The GEM '#{g.name}' depends on the GEM '#{name}' but it could not be found"
379             end
380             unless dep_g.version_ok? req_versions
381               fail "#{name} version should be #{req_versions.join(' and ')} but was '#{dep_g.version}'"
382             end
383           end
384
385           cfls = g.conflicts.select { |c|
386             cfl_g = gem_table[c[:gem]]
387             cfl_g and cfl_g.version_ok?(c[:requirements] || ['>= 0.0.0'])
388           }.map { |c| "#{c[:gem]}(#{gem_table[c[:gem]].version})" }
389           fail "Conflicts of gem `#{g.name}` found: #{cfls.join ', '}" unless cfls.empty?
390         end
391
392         gem_table
393       end
394
395       def tsort_dependencies ary, table, all_dependency_listed = false
396         unless all_dependency_listed
397           left = ary.dup
398           until left.empty?
399             v = left.pop
400             table[v].dependencies.each do |dep|
401               left.push dep[:gem]
402               ary.push dep[:gem]
403             end
404           end
405         end
406
407         ary.uniq!
408         table.instance_variable_set :@root_gems, ary
409         class << table
410           include TSort
411           def tsort_each_node &b
412             @root_gems.each &b
413           end
414
415           def tsort_each_child(n, &b)
416             fetch(n).dependencies.each do |v|
417               b.call v[:gem]
418             end
419           end
420         end
421
422         begin
423           table.tsort.map { |v| table[v] }
424         rescue TSort::Cyclic => e
425           fail "Circular mrbgem dependency found: #{e.message}"
426         end
427       end
428
429       def check(build)
430         gem_table = generate_gem_table build
431
432         @ary = tsort_dependencies gem_table.keys, gem_table, true
433
434         each(&:setup_compilers)
435
436         each do |g|
437           import_include_paths(g)
438         end
439       end
440
441       def import_include_paths(g)
442         gem_table = each_with_object({}) { |spec, h| h[spec.name] = spec }
443
444         g.dependencies.each do |dep|
445           dep_g = gem_table[dep[:gem]]
446           # We can do recursive call safely
447           # as circular dependency has already detected in the caller.
448           import_include_paths(dep_g)
449
450           dep_g.export_include_paths.uniq!
451           g.compilers.each do |compiler|
452             compiler.include_paths += dep_g.export_include_paths
453             g.export_include_paths += dep_g.export_include_paths
454             compiler.include_paths.uniq!
455             g.export_include_paths.uniq!
456           end
457         end
458       end
459     end # List
460   end # Gem
461
462   GemBox = Object.new
463   class << GemBox
464     attr_accessor :path
465
466     def new(&block); block.call(self); end
467     def config=(obj); @config = obj; end
468     def gem(gemdir, &block); @config.gem(gemdir, &block); end
469     def gembox(gemfile); @config.gembox(gemfile); end
470   end # GemBox
471 end # MRuby