docs: fix case of mruby
[mruby.git] / lib / mruby / gem.rb
blobfa0bb3a496bfeee6aaed9d6acd6655f1190f11a8
1 require 'forwardable'
2 autoload :TSort, 'tsort'
3 autoload :Shellwords, 'shellwords'
5 module MRuby
6   module Gem
7     class << self
8       attr_accessor :current
9     end
11     class Specification
12       include Rake::DSL
13       extend Forwardable
14       def_delegators :@build, :filename, :objfile, :libfile, :exefile
16       attr_accessor :name, :dir, :build
17       alias mruby build
18       attr_accessor :build_config_initializer
20       attr_accessor :version
21       attr_accessor :description, :summary
22       attr_accessor :homepage
23       attr_accessor :licenses, :authors
24       alias :license= :licenses=
25       alias :author= :authors=
27       attr_accessor :rbfiles, :objs
28       attr_writer :test_objs, :test_rbfiles
29       attr_accessor :test_args, :test_preload
31       attr_accessor :bins
33       attr_accessor :requirements
34       attr_reader :dependencies, :conflicts
36       attr_accessor :export_include_paths
38       attr_reader :generate_functions
40       attr_block MRuby::Build::COMMANDS
42       def initialize(name, &block)
43         @name = name
44         @initializer = block
45         @version = "0.0.0"
46         @dependencies = []
47         @conflicts = []
48         MRuby::Gem.current = self
49       end
51       def setup
52         return if defined?(@bins)  # return if already set up
54         MRuby::Gem.current = self
55         MRuby::Build::COMMANDS.each do |command|
56           instance_variable_set("@#{command}", @build.send(command).clone)
57         end
58         @linker.run_attrs.each(&:clear)
60         @rbfiles = Dir.glob("#{@dir}/mrblib/**/*.rb").sort
61         @objs = srcs_to_objs("src")
63         @test_preload = nil # 'test/assert.rb'
64         @test_args = {}
66         @bins = []
67         @cdump = true
69         @requirements = []
70         @export_include_paths = []
71         @export_include_paths << "#{dir}/include" if File.directory? "#{dir}/include"
73         instance_eval(&@initializer)
75         @generate_functions = !(@rbfiles.empty? && @objs.empty?)
76         @objs << objfile("#{build_dir}/gem_init") if @generate_functions
78         if !name || !licenses || !authors
79           fail "#{name || dir} required to set name, license(s) and author(s)"
80         end
82         build.libmruby_objs << @objs
84         instance_eval(&@build_config_initializer) if @build_config_initializer
86         repo_url = build.gem_dir_to_repo_url[dir]
87         build.locks[repo_url]['version'] = version if repo_url
88       end
90       def setup_compilers
91         (core? ? [@cc, *(@cxx if build.cxx_exception_enabled?)] : compilers).each do |compiler|
92           compiler.define_rules build_dir, @dir, @build.exts.presym_preprocessed if build.presym_enabled?
93           compiler.define_rules build_dir, @dir, @build.exts.object
94           compiler.defines << %Q[MRBGEM_#{funcname.upcase}_VERSION=#{version}]
95           compiler.include_paths << "#{@dir}/include" if File.directory? "#{@dir}/include"
96         end
98         define_gem_init_builder if @generate_functions
99       end
101       def for_windows?
102         if build.kind_of?(MRuby::CrossBuild)
103           return %w(x86_64-w64-mingw32 i686-w64-mingw32).include?(build.host_target)
104         elsif build.kind_of?(MRuby::Build)
105           return ('A'..'Z').to_a.any? { |vol| Dir.exist?("#{vol}:") }
106         end
107         return false
108       end
110       def disable_cdump
111         @cdump = false
112       end
114       def cdump?
115         build.presym_enabled? && @cdump
116       end
118       def core?
119         @dir.start_with?("#{MRUBY_ROOT}/mrbgems/")
120       end
122       def bin?
123         @bins.size > 0
124       end
126       def add_dependency(name, *requirements)
127         default_gem = requirements.last.kind_of?(Hash) ? requirements.pop : nil
128         requirements = ['>= 0.0.0'] if requirements.empty?
129         requirements.flatten!
130         @dependencies << {:gem => name, :requirements => requirements, :default => default_gem}
131       end
133       def add_test_dependency(*args)
134         add_dependency(*args) if build.test_enabled? || build.bintest_enabled?
135       end
137       def add_conflict(name, *req)
138         @conflicts << {:gem => name, :requirements => req.empty? ? nil : req}
139       end
141       def build_dir
142         "#{build.build_dir}/mrbgems/#{name}"
143       end
145       def test_rbireps
146         "#{build_dir}/gem_test.c"
147       end
149       def test_objs
150         @test_objs ||= srcs_to_objs("test")
151       end
153       def test_rbfiles
154         @test_rbfiles ||= Dir["#{@dir}/test/**/*.rb"].sort!
155       end
157       def search_package(name, version_query=nil)
158         package_query = name
159         package_query += " #{version_query}" if version_query
160         _pp "PKG-CONFIG", package_query
161         escaped_package_query = Shellwords.escape(package_query)
162         if system("pkg-config --exists #{escaped_package_query}")
163           cc.flags += [`pkg-config --cflags #{escaped_package_query}`.strip]
164           cxx.flags += [`pkg-config --cflags #{escaped_package_query}`.strip]
165           linker.flags_before_libraries += [`pkg-config --libs #{escaped_package_query}`.strip]
166           true
167         else
168           false
169         end
170       end
172       def funcname
173         @funcname ||= @name.gsub('-', '_')
174       end
176       def compilers
177         MRuby::Build::COMPILERS.map do |c|
178           instance_variable_get("@#{c}")
179         end
180       end
182       def srcs_to_objs(src_dir_from_gem_dir)
183         exts = compilers.flat_map{|c| c.source_exts} * ","
184         Dir["#{@dir}/#{src_dir_from_gem_dir}/*{#{exts}}"].map do |f|
185           objfile(f.relative_path_from(@dir).to_s.pathmap("#{build_dir}/%X"))
186         end
187       end
189       def define_gem_init_builder
190         file "#{build_dir}/gem_init.c" => [build.mrbcfile, __FILE__] + [rbfiles].flatten do |t|
191           mkdir_p build_dir
192           generate_gem_init("#{build_dir}/gem_init.c")
193         end
194       end
196       def generate_gem_init(fname)
197         _pp "GEN", fname.relative_path
198         open(fname, 'w') do |f|
199           print_gem_init_header f
200           unless rbfiles.empty?
201             opts = {cdump: cdump?, static: true}
202             if cdump?
203               build.mrbc.run f, rbfiles, "gem_mrblib_#{funcname}_proc", **opts
204             else
205               build.mrbc.run f, rbfiles, "gem_mrblib_irep_#{funcname}", **opts
206             end
207           end
208           f.puts %Q[void mrb_#{funcname}_gem_init(mrb_state *mrb);]
209           f.puts %Q[void mrb_#{funcname}_gem_final(mrb_state *mrb);]
210           f.puts %Q[]
211           f.puts %Q[void GENERATED_TMP_mrb_#{funcname}_gem_init(mrb_state *mrb) {]
212           f.puts %Q[  int ai = mrb_gc_arena_save(mrb);]
213           f.puts %Q[  gem_mrblib_#{funcname}_proc_init_syms(mrb);] if !rbfiles.empty? && cdump?
214           f.puts %Q[  mrb_#{funcname}_gem_init(mrb);] if objs != [objfile("#{build_dir}/gem_init")]
215           unless rbfiles.empty?
216             if cdump?
217               f.puts %Q[  mrb_load_proc(mrb, gem_mrblib_#{funcname}_proc);]
218             else
219               f.puts %Q[  mrb_load_irep(mrb, gem_mrblib_irep_#{funcname});]
220             end
221             f.puts %Q[  if (mrb->exc) {]
222             f.puts %Q[    mrb_print_error(mrb);]
223             f.puts %Q[    mrb_close(mrb);]
224             f.puts %Q[    exit(EXIT_FAILURE);]
225             f.puts %Q[  }]
226             f.puts %Q[  struct REnv *e = mrb_vm_ci_env(mrb->c->cibase);]
227             f.puts %Q[  mrb_vm_ci_env_set(mrb->c->cibase, NULL);]
228             f.puts %Q[  mrb_env_unshare(mrb, e);]
229           end
230           f.puts %Q[  mrb_gc_arena_restore(mrb, ai);]
231           f.puts %Q[}]
232           f.puts %Q[]
233           f.puts %Q[void GENERATED_TMP_mrb_#{funcname}_gem_final(mrb_state *mrb) {]
234           f.puts %Q[  mrb_#{funcname}_gem_final(mrb);] if objs != [objfile("#{build_dir}/gem_init")]
235           f.puts %Q[}]
236         end
237       end # generate_gem_init
239       def print_gem_comment(f)
240         f.puts %Q[/*]
241         f.puts %Q[ * This file is loading the irep]
242         f.puts %Q[ * Ruby GEM code.]
243         f.puts %Q[ *]
244         f.puts %Q[ * IMPORTANT:]
245         f.puts %Q[ *   This file was generated!]
246         f.puts %Q[ *   All manual changes will get lost.]
247         f.puts %Q[ */]
248       end
250       def print_gem_init_header(f)
251         print_gem_comment(f)
252         if rbfiles.empty?
253           f.puts %Q[#include <mruby.h>]
254         else
255           f.puts %Q[#include <stdlib.h>]
256           unless cdump?
257             f.puts %Q[#include <mruby.h>]
258             f.puts %Q[#include <mruby/proc.h>]
259           end
260         end
261       end
263       def print_gem_test_header(f)
264         print_gem_comment(f)
265         f.puts %Q[#include <stdio.h>]
266         f.puts %Q[#include <stdlib.h>]
267         f.puts %Q[#include <mruby.h>]
268         f.puts %Q[#include <mruby/irep.h>]
269         f.puts %Q[#include <mruby/variable.h>]
270         f.puts %Q[#include <mruby/hash.h>] unless test_args.empty?
271       end
273       def custom_test_init?
274         !test_objs.empty?
275       end
277       def version_ok?(req_versions)
278         req_versions.map do |req|
279           cmp, ver = req.split
280           cmp_result = Version.new(version) <=> Version.new(ver)
281           case cmp
282           when '=' then cmp_result == 0
283           when '!=' then cmp_result != 0
284           when '>' then cmp_result == 1
285           when '<' then cmp_result == -1
286           when '>=' then cmp_result >= 0
287           when '<=' then cmp_result <= 0
288           when '~>'
289             Version.new(version).twiddle_wakka_ok?(Version.new(ver))
290           else
291             fail "Comparison not possible with '#{cmp}'"
292           end
293         end.all?
294       end
295     end # Specification
297     class Version
298       include Comparable
299       include Enumerable
301       def <=>(other)
302         ret = 0
303         own = to_enum
305         other.each do |oth|
306           begin
307             ret = own.next <=> oth
308           rescue StopIteration
309             ret = 0 <=> oth
310           end
312           break unless ret == 0
313         end
315         ret
316       end
318       # ~> compare algorithm
319       #
320       # Example:
321       #    ~> 2     means >= 2.0.0 and < 3.0.0
322       #    ~> 2.2   means >= 2.2.0 and < 3.0.0
323       #    ~> 2.2.2 means >= 2.2.2 and < 2.3.0
324       def twiddle_wakka_ok?(other)
325         gr_or_eql = (self <=> other) >= 0
326         still_major_or_minor = (self <=> other.skip_major_or_minor) < 0
327         gr_or_eql and still_major_or_minor
328       end
330       def skip_major_or_minor
331         a = @ary.dup
332         a << 0 if a.size == 1 # ~> 2 can also be represented as ~> 2.0
333         a.slice!(-1)
334         a[-1] = a[-1].succ
335         a
336       end
338       def initialize(str)
339         @str = str
340         @ary = @str.split('.').map(&:to_i)
341       end
343       def each(&block); @ary.each(&block); end
344       def [](index); @ary[index]; end
345       def []=(index, value)
346         @ary[index] = value
347         @str = @ary.join('.')
348       end
349       def slice!(index)
350         @ary.slice!(index)
351         @str = @ary.join('.')
352       end
353     end # Version
355     class List
356       include Enumerable
358       def initialize
359         @ary = []
360       end
362       def each(&b)
363         @ary.each(&b)
364       end
366       def [](name)
367         @ary.detect {|g| g.name == name}
368       end
370       def <<(gem)
371         unless @ary.detect {|g| g.dir == gem.dir }
372           @ary << gem
373         else
374           # GEM was already added to this list
375         end
376       end
378       def empty?