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