diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/mruby-core-ext.rb | 40 | ||||
| -rw-r--r-- | lib/mruby/build.rb | 66 | ||||
| -rw-r--r-- | lib/mruby/build/command.rb | 45 | ||||
| -rw-r--r-- | lib/mruby/gem.rb | 42 |
4 files changed, 126 insertions, 67 deletions
diff --git a/lib/mruby-core-ext.rb b/lib/mruby-core-ext.rb index 4c6d3ca76..08e6f6148 100644 --- a/lib/mruby-core-ext.rb +++ b/lib/mruby-core-ext.rb @@ -18,18 +18,20 @@ class String end # Compatible with 1.9 on 1.8 - def %(params) - if params.is_a?(Hash) - str = self.clone - params.each do |k, v| - str.gsub!("%{#{k}}") { v } - end - str - else - if params.is_a?(Array) - sprintf(self, *params) + unless (sprintf("%{a}", :a => 1) rescue false) + def %(params) + if params.is_a?(Hash) + str = self.clone + params.each do |k, v| + str.gsub!("%{#{k}}") { v } + end + str else - sprintf(self, params) + if params.is_a?(Array) + sprintf(self, *params) + else + sprintf(self, params) + end end end end @@ -37,17 +39,21 @@ end class Symbol # Compatible with 1.9 on 1.8 - def to_proc - proc { |obj, *args| obj.send(self, *args) } + unless method_defined?(:to_proc) + def to_proc + proc { |obj, *args| obj.send(self, *args) } + end end end module Enumerable # Compatible with 1.9 on 1.8 - def each_with_object(memo) - return to_enum :each_with_object, memo unless block_given? - each { |obj| yield obj, memo } - memo + unless method_defined?(:each_with_object) + def each_with_object(memo) + return to_enum :each_with_object, memo unless block_given? + each { |obj| yield obj, memo } + memo + end end end diff --git a/lib/mruby/build.rb b/lib/mruby/build.rb index 57bd9c51e..d9d52948b 100644 --- a/lib/mruby/build.rb +++ b/lib/mruby/build.rb @@ -22,7 +22,6 @@ module MRuby def initialize(name, &block) @name, @initializer = name.to_s, block - MRuby::Toolchain.toolchains ||= {} MRuby::Toolchain.toolchains[@name] = self end @@ -30,13 +29,8 @@ module MRuby conf.instance_exec(conf, params, &@initializer) end - def self.load - Dir.glob("#{MRUBY_ROOT}/tasks/toolchains/*.rake").each do |file| - Kernel.load file - end - end + self.toolchains = {} end - Toolchain.load class Build class << self @@ -45,9 +39,11 @@ module MRuby include Rake::DSL include LoadGems attr_accessor :name, :bins, :exts, :file_separator, :build_dir, :gem_clone_dir - attr_reader :libmruby, :gems, :toolchains + attr_reader :libmruby_objs, :gems, :toolchains attr_writer :enable_bintest, :enable_test + alias libmruby libmruby_objs + COMPILERS = %w(cc cxx objc asm) COMMANDS = COMPILERS + %w(linker archiver yacc gperf git exts mrbc) attr_block MRuby::Build::COMMANDS @@ -81,7 +77,7 @@ module MRuby @mrbc = Command::Mrbc.new(self) @bins = [] - @gems, @libmruby = MRuby::Gem::List.new, [] + @gems, @libmruby_objs = MRuby::Gem::List.new, [] @build_mrbtest_lib_only = false @cxx_exception_enabled = false @cxx_exception_disabled = false @@ -100,6 +96,10 @@ module MRuby build_mrbtest if test_enabled? end + def debug_enabled? + @enable_debug + end + def enable_debug compilers.each do |c| c.defines += %w(MRB_DEBUG) @@ -108,6 +108,8 @@ module MRuby end end @mrbc.compile_options += ' -g' + + @enable_debug = true end def disable_cxx_exception @@ -154,8 +156,6 @@ module MRuby end def compile_as_cxx src, cxx_src, obj = nil, includes = [] - src = File.absolute_path src - cxx_src = File.absolute_path cxx_src obj = objfile(cxx_src) if obj.nil? file cxx_src => [src, __FILE__] do |t| @@ -167,7 +167,7 @@ module MRuby #ifndef MRB_ENABLE_CXX_ABI extern "C" { #endif -#include "#{src}" +#include "#{File.absolute_path src}" #ifndef MRB_ENABLE_CXX_ABI } #endif @@ -190,10 +190,15 @@ EOS end def toolchain(name, params={}) - tc = Toolchain.toolchains[name.to_s] - fail "Unknown #{name} toolchain" unless tc + name = name.to_s + tc = Toolchain.toolchains[name] || begin + path = "#{MRUBY_ROOT}/tasks/toolchains/#{name}.rake" + fail "Unknown #{name} toolchain" unless File.exist?(path) + load path + Toolchain.toolchains[name] + end tc.setup(self, params) - @toolchains.unshift name.to_s + @toolchains.unshift name end def primary_toolchain @@ -249,7 +254,7 @@ EOS if name.is_a?(Array) name.flatten.map { |n| filename(n) } else - '"%s"' % name.gsub('/', file_separator) + name.gsub('/', file_separator) end end @@ -257,15 +262,18 @@ EOS if name.is_a?(Array) name.flatten.map { |n| cygwin_filename(n) } else - '"%s"' % `cygpath -w "#{filename(name)}"`.strip + `cygpath -w "#{filename(name)}"`.strip end end def exefile(name) if name.is_a?(Array) name.flatten.map { |n| exefile(n) } - else + elsif File.extname(name).empty? "#{name}#{exts.executable}" + else + # `name` sometimes have (non-standard) extension (e.g. `.bat`). + name end end @@ -293,18 +301,22 @@ EOS @build_mrbtest_lib_only end + def verbose_flag + $verbose ? ' -v' : '' + end + def run_test puts ">>> Test #{name} <<<" mrbtest = exefile("#{build_dir}/bin/mrbtest") - sh "#{filename mrbtest.relative_path}#{$verbose ? ' -v' : ''}" + sh "#{filename mrbtest.relative_path}#{verbose_flag}" puts - run_bintest if bintest_enabled? end def run_bintest + puts ">>> Bintest #{name} <<<" targets = @gems.select { |v| File.directory? "#{v.dir}/bintest" }.map { |v| filename v.dir } targets << filename(".") if File.directory? "./bintest" - sh "ruby test/bintest.rb #{targets.join ' '}" + sh "ruby test/bintest.rb#{verbose_flag} #{targets.join ' '}" end def print_build_summary @@ -324,6 +336,18 @@ EOS puts "================================================" puts end + + def libmruby_static + libfile("#{build_dir}/lib/libmruby") + end + + def libmruby_core_static + libfile("#{build_dir}/lib/libmruby_core") + end + + def libraries + [libmruby_static] + end end # Build class CrossBuild < Build diff --git a/lib/mruby/build/command.rb b/lib/mruby/build/command.rb index 694b4a24c..d4354225e 100644 --- a/lib/mruby/build/command.rb +++ b/lib/mruby/build/command.rb @@ -67,8 +67,8 @@ module MRuby path && build.filename("#{path}/#{name}").sub(/^"(.*)"$/, '\1') end - def all_flags(_defineds=[], _include_paths=[], _flags=[]) - define_flags = [defines, _defineds].flatten.map{ |d| option_define % d } + def all_flags(_defines=[], _include_paths=[], _flags=[]) + define_flags = [defines, _defines].flatten.map{ |d| option_define % d } include_path_flags = [include_paths, _include_paths].flatten.map do |f| if MRUBY_BUILD_HOST_IS_CYGWIN option_include_path % cygwin_filename(f) @@ -79,14 +79,14 @@ module MRuby [flags, define_flags, include_path_flags, _flags].flatten.join(' ') end - def run(outfile, infile, _defineds=[], _include_paths=[], _flags=[]) + def run(outfile, infile, _defines=[], _include_paths=[], _flags=[]) FileUtils.mkdir_p File.dirname(outfile) _pp "CC", infile.relative_path, outfile.relative_path if MRUBY_BUILD_HOST_IS_CYGWIN - _run compile_options, { :flags => all_flags(_defineds, _include_paths, _flags), + _run compile_options, { :flags => all_flags(_defines, _include_paths, _flags), :infile => cygwin_filename(infile), :outfile => cygwin_filename(outfile) } else - _run compile_options, { :flags => all_flags(_defineds, _include_paths, _flags), + _run compile_options, { :flags => all_flags(_defines, _include_paths, _flags), :infile => filename(infile), :outfile => filename(outfile) } end end @@ -127,13 +127,40 @@ module MRuby end private + + # + # === Example of +.d+ file + # + # ==== Without <tt>-MP</tt> compiler flag + # + # /build/host/src/array.o: \ + # /src/array.c \ + # /include/mruby/common.h \ + # /include/mruby/value.h \ + # /src/value_array.h + # + # ==== With <tt>-MP</tt> compiler flag + # + # /build/host/src/array.o: \ + # /src/array.c \ + # /include/mruby/common.h \ + # /include/mruby/value.h \ + # /src/value_array.h + # + # /include/mruby/common.h: + # + # /include/mruby/value.h: + # + # /src/value_array.h: + # def get_dependencies(file) file = file.ext('d') unless File.extname(file) == '.d' + deps = [] if File.exist?(file) - File.read(file).gsub("\\\n ", "").scan(/^\S+:\s+(.+)$/).flatten.map {|s| s.split(' ') }.flatten - else - [] - end + [ MRUBY_CONFIG ] + File.foreach(file){|line| deps << $1 if /^ +(.*?)(?: *\\)?$/ =~ line} + deps.uniq! + end + deps << MRUBY_CONFIG end end diff --git a/lib/mruby/gem.rb b/lib/mruby/gem.rb index a83ca639f..95c1d4bc3 100644 --- a/lib/mruby/gem.rb +++ b/lib/mruby/gem.rb @@ -52,6 +52,8 @@ module MRuby end def setup + return if defined?(@linker) # return if already set up + MRuby::Gem.current = self MRuby::Build::COMMANDS.each do |command| instance_variable_set("@#{command}", @build.send(command).clone) @@ -63,7 +65,7 @@ module MRuby objfile(f.relative_path_from(@dir).to_s.pathmap("#{build_dir}/%X")) end - @test_rbfiles = Dir.glob("#{dir}/test/**/*.rb") + @test_rbfiles = Dir.glob("#{dir}/test/**/*.rb").sort @test_objs = Dir.glob("#{dir}/test/*.{c,cpp,cxx,cc,m,asm,s,S}").map do |f| objfile(f.relative_path_from(dir).to_s.pathmap("#{build_dir}/%X")) end @@ -87,7 +89,7 @@ module MRuby fail "#{name || dir} required to set name, license(s) and author(s)" end - build.libmruby << @objs + build.libmruby_objs << @objs instance_eval(&@build_config_initializer) if @build_config_initializer end @@ -110,17 +112,13 @@ module MRuby end def add_test_dependency(*args) - add_dependency(*args) if build.test_enabled? + add_dependency(*args) if build.test_enabled? || build.bintest_enabled? end def add_conflict(name, *req) @conflicts << {:gem => name, :requirements => req.empty? ? nil : req} end - def self.bin=(bin) - @bins = [bin].flatten - end - def build_dir "#{build.build_dir}/mrbgems/#{name}" end @@ -269,16 +267,18 @@ module MRuby # ~> compare algorithm # # Example: + # ~> 2 means >= 2.0.0 and < 3.0.0 # ~> 2.2 means >= 2.2.0 and < 3.0.0 - # ~> 2.2.0 means >= 2.2.0 and < 2.3.0 + # ~> 2.2.2 means >= 2.2.2 and < 2.3.0 def twiddle_wakka_ok?(other) gr_or_eql = (self <=> other) >= 0 - still_minor = (self <=> other.skip_minor) < 0 - gr_or_eql and still_minor + still_major_or_minor = (self <=> other.skip_major_or_minor) < 0 + gr_or_eql and still_major_or_minor end - def skip_minor + def skip_major_or_minor a = @ary.dup + a << 0 if a.size == 1 # ~> 2 can also be represented as ~> 2.0 a.slice!(-1) a[-1] = a[-1].succ a @@ -334,26 +334,26 @@ module MRuby end def generate_gem_table build - gem_table = @ary.reduce({}) { |res,v| res[v.name] = v; res } + gem_table = each_with_object({}) { |spec, h| h[spec.name] = spec } - default_gems = [] + default_gems = {} each do |g| g.dependencies.each do |dep| - default_gems << default_gem_params(dep) unless gem_table.key? dep[:gem] + default_gems[dep[:gem]] ||= default_gem_params(dep) end end until default_gems.empty? - def_gem = default_gems.pop + def_name, def_gem = default_gems.shift + next if gem_table[def_name] - spec = build.gem def_gem[:default] - fail "Invalid gem name: #{spec.name} (Expected: #{def_gem[:gem]})" if spec.name != def_gem[:gem] + spec = gem_table[def_name] = build.gem(def_gem[:default]) + fail "Invalid gem name: #{spec.name} (Expected: #{def_name})" if spec.name != def_name spec.setup spec.dependencies.each do |dep| - default_gems << default_gem_params(dep) unless gem_table.key? dep[:gem] + default_gems[dep[:gem]] ||= default_gem_params(dep) end - gem_table[spec.name] = spec end each do |g| @@ -428,7 +428,8 @@ module MRuby end def import_include_paths(g) - gem_table = @ary.reduce({}) { |res,v| res[v.name] = v; res } + gem_table = each_with_object({}) { |spec, h| h[spec.name] = spec } + g.dependencies.each do |dep| dep_g = gem_table[dep[:gem]] # We can do recursive call safely @@ -454,5 +455,6 @@ module MRuby def new(&block); block.call(self); end def config=(obj); @config = obj; end def gem(gemdir, &block); @config.gem(gemdir, &block); end + def gembox(gemfile); @config.gembox(gemfile); end end # GemBox end # MRuby |
