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