[270] | 1 | load "#{MRUBY_ROOT}/tasks/mruby_build_gem.rake"
|
---|
| 2 | load "#{MRUBY_ROOT}/tasks/mruby_build_commands.rake"
|
---|
| 3 |
|
---|
| 4 | module MRuby
|
---|
| 5 | class << self
|
---|
| 6 | def targets
|
---|
| 7 | @targets ||= {}
|
---|
| 8 | end
|
---|
| 9 |
|
---|
| 10 | def each_target(&block)
|
---|
| 11 | return to_enum(:each_target) if block.nil?
|
---|
| 12 | @targets.each do |key, target|
|
---|
| 13 | target.instance_eval(&block)
|
---|
| 14 | end
|
---|
| 15 | end
|
---|
| 16 | end
|
---|
| 17 |
|
---|
| 18 | class Toolchain
|
---|
| 19 | class << self
|
---|
| 20 | attr_accessor :toolchains
|
---|
| 21 | end
|
---|
| 22 |
|
---|
| 23 | def initialize(name, &block)
|
---|
| 24 | @name, @initializer = name.to_s, block
|
---|
| 25 | MRuby::Toolchain.toolchains ||= {}
|
---|
| 26 | MRuby::Toolchain.toolchains[@name] = self
|
---|
| 27 | end
|
---|
| 28 |
|
---|
| 29 | def setup(conf,params={})
|
---|
| 30 | conf.instance_exec(conf, params, &@initializer)
|
---|
| 31 | end
|
---|
| 32 |
|
---|
| 33 | def self.load
|
---|
| 34 | Dir.glob("#{MRUBY_ROOT}/tasks/toolchains/*.rake").each do |file|
|
---|
| 35 | Kernel.load file
|
---|
| 36 | end
|
---|
| 37 | end
|
---|
| 38 | end
|
---|
| 39 | Toolchain.load
|
---|
| 40 |
|
---|
| 41 | class Build
|
---|
| 42 | class << self
|
---|
| 43 | attr_accessor :current
|
---|
| 44 | end
|
---|
| 45 | include Rake::DSL
|
---|
| 46 | include LoadGems
|
---|
| 47 | attr_accessor :name, :bins, :exts, :file_separator, :build_dir, :gem_clone_dir
|
---|
| 48 | attr_reader :libmruby, :gems, :toolchains
|
---|
| 49 | attr_writer :enable_bintest, :enable_test
|
---|
| 50 |
|
---|
| 51 | COMPILERS = %w(cc cxx objc asm)
|
---|
| 52 | COMMANDS = COMPILERS + %w(linker archiver yacc gperf git exts mrbc)
|
---|
| 53 | attr_block MRuby::Build::COMMANDS
|
---|
| 54 |
|
---|
| 55 | Exts = Struct.new(:object, :executable, :library)
|
---|
| 56 |
|
---|
| 57 | def initialize(name='host', build_dir=nil, &block)
|
---|
| 58 | @name = name.to_s
|
---|
| 59 |
|
---|
| 60 | unless MRuby.targets[@name]
|
---|
| 61 | if ENV['OS'] == 'Windows_NT'
|
---|
| 62 | @exts = Exts.new('.o', '.exe', '.a')
|
---|
| 63 | else
|
---|
| 64 | @exts = Exts.new('.o', '', '.a')
|
---|
| 65 | end
|
---|
| 66 |
|
---|
| 67 | build_dir = build_dir || ENV['MRUBY_BUILD_DIR'] || "#{MRUBY_ROOT}/build"
|
---|
| 68 |
|
---|
| 69 | @file_separator = '/'
|
---|
| 70 | @build_dir = "#{build_dir}/#{@name}"
|
---|
| 71 | @gem_clone_dir = "#{build_dir}/mrbgems"
|
---|
| 72 | @cc = Command::Compiler.new(self, %w(.c))
|
---|
| 73 | @cxx = Command::Compiler.new(self, %w(.cc .cxx .cpp))
|
---|
| 74 | @objc = Command::Compiler.new(self, %w(.m))
|
---|
| 75 | @asm = Command::Compiler.new(self, %w(.S .asm))
|
---|
| 76 | @linker = Command::Linker.new(self)
|
---|
| 77 | @archiver = Command::Archiver.new(self)
|
---|
| 78 | @yacc = Command::Yacc.new(self)
|
---|
| 79 | @gperf = Command::Gperf.new(self)
|
---|
| 80 | @git = Command::Git.new(self)
|
---|
| 81 | @mrbc = Command::Mrbc.new(self)
|
---|
| 82 |
|
---|
| 83 | @bins = []
|
---|
| 84 | @gems, @libmruby = MRuby::Gem::List.new, []
|
---|
| 85 | @build_mrbtest_lib_only = false
|
---|
| 86 | @cxx_abi_enabled = false
|
---|
| 87 | @cxx_exception_disabled = false
|
---|
| 88 | @enable_bintest = false
|
---|
| 89 | @enable_test = false
|
---|
| 90 | @toolchains = []
|
---|
| 91 |
|
---|
| 92 | MRuby.targets[@name] = self
|
---|
| 93 | end
|
---|
| 94 |
|
---|
| 95 | MRuby::Build.current = MRuby.targets[@name]
|
---|
| 96 | MRuby.targets[@name].instance_eval(&block)
|
---|
| 97 |
|
---|
| 98 | build_mrbc_exec if name == 'host'
|
---|
| 99 | build_mrbtest if test_enabled?
|
---|
| 100 | end
|
---|
| 101 |
|
---|
| 102 | def enable_debug
|
---|
| 103 | compilers.each do |c|
|
---|
| 104 | c.defines += %w(MRB_DEBUG)
|
---|
| 105 | if toolchains.any? { |toolchain| toolchain == "gcc" }
|
---|
| 106 | c.flags += %w(-g3 -O0)
|
---|
| 107 | end
|
---|
| 108 | end
|
---|
| 109 | @mrbc.compile_options += ' -g'
|
---|
| 110 | end
|
---|
| 111 |
|
---|
| 112 | def disable_cxx_exception
|
---|
| 113 | @cxx_exception_disabled = true
|
---|
| 114 | end
|
---|
| 115 |
|
---|
| 116 | def cxx_abi_enabled?
|
---|
| 117 | @cxx_abi_enabled
|
---|
| 118 | end
|
---|
| 119 |
|
---|
| 120 | def enable_cxx_abi
|
---|
| 121 | return if @cxx_exception_disabled or @cxx_abi_enabled
|
---|
| 122 | compilers.each { |c| c.defines += %w(MRB_ENABLE_CXX_EXCEPTION) }
|
---|
| 123 | linker.command = cxx.command if toolchains.find { |v| v == 'gcc' }
|
---|
| 124 | @cxx_abi_enabled = true
|
---|
| 125 | end
|
---|
| 126 |
|
---|
| 127 | def compile_as_cxx src, cxx_src, obj = nil, includes = []
|
---|
| 128 | src = File.absolute_path src
|
---|
| 129 | cxx_src = File.absolute_path cxx_src
|
---|
| 130 | obj = objfile(cxx_src) if obj.nil?
|
---|
| 131 |
|
---|
| 132 | file cxx_src => [src, __FILE__] do |t|
|
---|
| 133 | FileUtils.mkdir_p File.dirname t.name
|
---|
| 134 | IO.write t.name, <<EOS
|
---|
| 135 | #define __STDC_CONSTANT_MACROS
|
---|
| 136 | #define __STDC_LIMIT_MACROS
|
---|
| 137 |
|
---|
| 138 | extern "C" {
|
---|
| 139 | #include "#{src}"
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | #{src == "#{MRUBY_ROOT}/src/error.c"? 'mrb_int mrb_jmpbuf::jmpbuf_id = 0;' : ''}
|
---|
| 143 | EOS
|
---|
| 144 | end
|
---|
| 145 |
|
---|
| 146 | file obj => cxx_src do |t|
|
---|
| 147 | cxx.run t.name, t.prerequisites.first, [], ["#{MRUBY_ROOT}/src"] + includes
|
---|
| 148 | end
|
---|
| 149 |
|
---|
| 150 | obj
|
---|
| 151 | end
|
---|
| 152 |
|
---|
| 153 | def enable_bintest
|
---|
| 154 | @enable_bintest = true
|
---|
| 155 | end
|
---|
| 156 |
|
---|
| 157 | def bintest_enabled?
|
---|
| 158 | @enable_bintest
|
---|
| 159 | end
|
---|
| 160 |
|
---|
| 161 | def toolchain(name, params={})
|
---|
| 162 | tc = Toolchain.toolchains[name.to_s]
|
---|
| 163 | fail "Unknown #{name} toolchain" unless tc
|
---|
| 164 | tc.setup(self, params)
|
---|
| 165 | @toolchains.unshift name.to_s
|
---|
| 166 | end
|
---|
| 167 |
|
---|
| 168 | def primary_toolchain
|
---|
| 169 | @toolchains.first
|
---|
| 170 | end
|
---|
| 171 |
|
---|
| 172 | def root
|
---|
| 173 | MRUBY_ROOT
|
---|
| 174 | end
|
---|
| 175 |
|
---|
| 176 | def enable_test
|
---|
| 177 | @enable_test = true
|
---|
| 178 | end
|
---|
| 179 |
|
---|
| 180 | def test_enabled?
|
---|
| 181 | @enable_test
|
---|
| 182 | end
|
---|
| 183 |
|
---|
| 184 | def build_mrbtest
|
---|
| 185 | gem :core => 'mruby-test'
|
---|
| 186 | end
|
---|
| 187 |
|
---|
| 188 | def build_mrbc_exec
|
---|
| 189 | gem :core => 'mruby-bin-mrbc'
|
---|
| 190 | end
|
---|
| 191 |
|
---|
| 192 | def mrbcfile
|
---|
| 193 | return @mrbcfile if @mrbcfile
|
---|
| 194 |
|
---|
| 195 | mrbc_build = MRuby.targets['host']
|
---|
| 196 | gems.each { |v| mrbc_build = self if v.name == 'mruby-bin-mrbc' }
|
---|
| 197 | @mrbcfile = mrbc_build.exefile("#{mrbc_build.build_dir}/bin/mrbc")
|
---|
| 198 | end
|
---|
| 199 |
|
---|
| 200 | def compilers
|
---|
| 201 | COMPILERS.map do |c|
|
---|
| 202 | instance_variable_get("@#{c}")
|
---|
| 203 | end
|
---|
| 204 | end
|
---|
| 205 |
|
---|
| 206 | def define_rules
|
---|
| 207 | compilers.each do |compiler|
|
---|
| 208 | if respond_to?(:enable_gems?) && enable_gems?
|
---|
| 209 | compiler.defines -= %w(DISABLE_GEMS)
|
---|
| 210 | else
|
---|
| 211 | compiler.defines += %w(DISABLE_GEMS)
|
---|
| 212 | end
|
---|
| 213 | compiler.define_rules build_dir, File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
---|
| 214 | end
|
---|
| 215 | end
|
---|
| 216 |
|
---|
| 217 | def filename(name)
|
---|
| 218 | if name.is_a?(Array)
|
---|
| 219 | name.flatten.map { |n| filename(n) }
|
---|
| 220 | else
|
---|
| 221 | '"%s"' % name.gsub('/', file_separator)
|
---|
| 222 | end
|
---|
| 223 | end
|
---|
| 224 |
|
---|
| 225 | def cygwin_filename(name)
|
---|
| 226 | if name.is_a?(Array)
|
---|
| 227 | name.flatten.map { |n| cygwin_filename(n) }
|
---|
| 228 | else
|
---|
| 229 | '"%s"' % `cygpath -w "#{filename(name)}"`.strip
|
---|
| 230 | end
|
---|
| 231 | end
|
---|
| 232 |
|
---|
| 233 | def exefile(name)
|
---|
| 234 | if name.is_a?(Array)
|
---|
| 235 | name.flatten.map { |n| exefile(n) }
|
---|
| 236 | else
|
---|
| 237 | "#{name}#{exts.executable}"
|
---|
| 238 | end
|
---|
| 239 | end
|
---|
| 240 |
|
---|
| 241 | def objfile(name)
|
---|
| 242 | if name.is_a?(Array)
|
---|
| 243 | name.flatten.map { |n| objfile(n) }
|
---|
| 244 | else
|
---|
| 245 | "#{name}#{exts.object}"
|
---|
| 246 | end
|
---|
| 247 | end
|
---|
| 248 |
|
---|
| 249 | def libfile(name)
|
---|
| 250 | if name.is_a?(Array)
|
---|
| 251 | name.flatten.map { |n| libfile(n) }
|
---|
| 252 | else
|
---|
| 253 | "#{name}#{exts.library}"
|
---|
| 254 | end
|
---|
| 255 | end
|
---|
| 256 |
|
---|
| 257 | def build_mrbtest_lib_only
|
---|
| 258 | @build_mrbtest_lib_only = true
|
---|
| 259 | end
|
---|
| 260 |
|
---|
| 261 | def build_mrbtest_lib_only?
|
---|
| 262 | @build_mrbtest_lib_only
|
---|
| 263 | end
|
---|
| 264 |
|
---|
| 265 | def run_test
|
---|
| 266 | puts ">>> Test #{name} <<<"
|
---|
| 267 | mrbtest = exefile("#{build_dir}/bin/mrbtest")
|
---|
| 268 | sh "#{filename mrbtest.relative_path}#{$verbose ? ' -v' : ''}"
|
---|
| 269 | puts
|
---|
| 270 | run_bintest if bintest_enabled?
|
---|
| 271 | end
|
---|
| 272 |
|
---|
| 273 | def run_bintest
|
---|
| 274 | targets = @gems.select { |v| File.directory? "#{v.dir}/bintest" }.map { |v| filename v.dir }
|
---|
| 275 | targets << filename(".") if File.directory? "./bintest"
|
---|
| 276 | sh "ruby test/bintest.rb #{targets.join ' '}"
|
---|
| 277 | end
|
---|
| 278 |
|
---|
| 279 | def print_build_summary
|
---|
| 280 | puts "================================================"
|
---|
| 281 | puts " Config Name: #{@name}"
|
---|
| 282 | puts " Output Directory: #{self.build_dir.relative_path}"
|
---|
| 283 | puts " Binaries: #{@bins.join(', ')}" unless @bins.empty?
|
---|
| 284 | unless @gems.empty?
|
---|
| 285 | puts " Included Gems:"
|
---|
| 286 | @gems.map do |gem|
|
---|
| 287 | gem_version = " - #{gem.version}" if gem.version != '0.0.0'
|
---|
| 288 | gem_summary = " - #{gem.summary}" if gem.summary
|
---|
| 289 | puts " #{gem.name}#{gem_version}#{gem_summary}"
|
---|
| 290 | puts " - Binaries: #{gem.bins.join(', ')}" unless gem.bins.empty?
|
---|
| 291 | end
|
---|
| 292 | end
|
---|
| 293 | puts "================================================"
|
---|
| 294 | puts
|
---|
| 295 | end
|
---|
| 296 | end # Build
|
---|
| 297 |
|
---|
| 298 | class CrossBuild < Build
|
---|
| 299 | attr_block %w(test_runner)
|
---|
| 300 | # cross compiling targets for building native extensions.
|
---|
| 301 | # host - arch of where the built binary will run
|
---|
| 302 | # build - arch of the machine building the binary
|
---|
| 303 | attr_accessor :host_target, :build_target
|
---|
| 304 |
|
---|
| 305 | def initialize(name, build_dir=nil, &block)
|
---|
| 306 | @test_runner = Command::CrossTestRunner.new(self)
|
---|
| 307 | super
|
---|
| 308 | end
|
---|
| 309 |
|
---|
| 310 | def mrbcfile
|
---|
| 311 | MRuby.targets['host'].exefile("#{MRuby.targets['host'].build_dir}/bin/mrbc")
|
---|
| 312 | end
|
---|
| 313 |
|
---|
| 314 | def run_test
|
---|
| 315 | mrbtest = exefile("#{build_dir}/bin/mrbtest")
|
---|
| 316 | if (@test_runner.command == nil)
|
---|
| 317 | puts "You should run #{mrbtest} on target device."
|
---|
| 318 | puts
|
---|
| 319 | else
|
---|
| 320 | @test_runner.run(mrbtest)
|
---|
| 321 | end
|
---|
| 322 | end
|
---|
| 323 | end # CrossBuild
|
---|
| 324 | end # MRuby
|
---|