2 autoload :TSort, 'tsort'
3 autoload :Shellwords, 'shellwords'
14 def_delegators :@build, :filename, :objfile, :libfile, :exefile
16 attr_accessor :name, :dir, :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
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)
48 MRuby::Gem.current = self
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)
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'
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)"
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
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"
98 define_gem_init_builder if @generate_functions
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}:") }
115 build.presym_enabled? && @cdump
119 @dir.start_with?("#{MRUBY_ROOT}/mrbgems/")
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}
133 def add_test_dependency(*args)
134 add_dependency(*args) if build.test_enabled? || build.bintest_enabled?
137 def add_conflict(name, *req)
138 @conflicts << {:gem => name, :requirements => req.empty? ? nil : req}
142 "#{build.build_dir}/mrbgems/#{name}"
146 "#{build_dir}/gem_test.c"
150 @test_objs ||= srcs_to_objs("test")
154 @test_rbfiles ||= Dir["#{@dir}/test/**/*.rb"].sort!
157 def search_package(name, version_query=nil)
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]
173 @funcname ||= @name.gsub('-', '_')
177 MRuby::Build::COMPILERS.map do |c|
178 instance_variable_get("@#{c}")
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"))
189 def define_gem_init_builder
190 file "#{build_dir}/gem_init.c" => [build.mrbcfile, __FILE__] + [rbfiles].flatten do |t|
192 generate_gem_init("#{build_dir}/gem_init.c")
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}
203 build.mrbc.run f, rbfiles, "gem_mrblib_#{funcname}_proc", **opts
205 build.mrbc.run f, rbfiles, "gem_mrblib_irep_#{funcname}", **opts
208 f.puts %Q[void mrb_#{funcname}_gem_init(mrb_state *mrb);]
209 f.puts %Q[void mrb_#{funcname}_gem_final(mrb_state *mrb);]
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?
217 f.puts %Q[ mrb_load_proc(mrb, gem_mrblib_#{funcname}_proc);]
219 f.puts %Q[ mrb_load_irep(mrb, gem_mrblib_irep_#{funcname});]
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);]
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);]
230 f.puts %Q[ mrb_gc_arena_restore(mrb, ai);]
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")]
237 end # generate_gem_init
239 def print_gem_comment(f)
241 f.puts %Q[ * This file is loading the irep]
242 f.puts %Q[ * Ruby GEM code.]
244 f.puts %Q[ * IMPORTANT:]
245 f.puts %Q[ * This file was generated!]
246 f.puts %Q[ * All manual changes will get lost.]
250 def print_gem_init_header(f)
253 f.puts %Q[#include <mruby.h>]
255 f.puts %Q[#include <stdlib.h>]
257 f.puts %Q[#include <mruby.h>]
258 f.puts %Q[#include <mruby/proc.h>]
263 def print_gem_test_header(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?
273 def custom_test_init?
277 def version_ok?(req_versions)
278 req_versions.map do |req|
280 cmp_result = Version.new(version) <=> Version.new(ver)
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
289 Version.new(version).twiddle_wakka_ok?(Version.new(ver))
291 fail "Comparison not possible with '#{cmp}'"
307 ret = own.next <=> oth
312 break unless ret == 0
318 # ~> compare algorithm
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
330 def skip_major_or_minor
332 a << 0 if a.size == 1 # ~> 2 can also be represented as ~> 2.0
340 @ary = @str.split('.').map(&:to_i)
343 def each(&block); @ary.each(&block); end
344 def [](index); @ary[index]; end
345 def []=(index, value)
347 @str = @ary.join('.')
351 @str = @ary.join('.')
367 @ary.detect {|g| g.name == name}
371 unless @ary.detect {|g| g.dir == gem.dir }
374 # GEM was already added to this list