[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
|
---|
[331] | 86 | @cxx_exception_enabled = false
|
---|
| 87 | @cxx_exception_disabled = false
|
---|
[270] | 88 | @cxx_abi_enabled = false
|
---|
| 89 | @enable_bintest = false
|
---|
| 90 | @enable_test = false
|
---|
| 91 | @toolchains = []
|
---|
| 92 |
|
---|
| 93 | MRuby.targets[@name] = self
|
---|
| 94 | end
|
---|
| 95 |
|
---|
| 96 | MRuby::Build.current = MRuby.targets[@name]
|
---|
| 97 | MRuby.targets[@name].instance_eval(&block)
|
---|
| 98 |
|
---|
| 99 | build_mrbc_exec if name == 'host'
|
---|
| 100 | build_mrbtest if test_enabled?
|
---|
| 101 | end
|
---|
| 102 |
|
---|
| 103 | def enable_debug
|
---|
| 104 | compilers.each do |c|
|
---|
| 105 | c.defines += %w(MRB_DEBUG)
|
---|
| 106 | if toolchains.any? { |toolchain| toolchain == "gcc" }
|
---|
| 107 | c.flags += %w(-g3 -O0)
|
---|
| 108 | end
|
---|
| 109 | end
|
---|
| 110 | @mrbc.compile_options += ' -g'
|
---|
| 111 | end
|
---|
| 112 |
|
---|
| 113 | def disable_cxx_exception
|
---|
[331] | 114 | if @cxx_exception_enabled or @cxx_abi_enabled
|
---|
| 115 | raise "cxx_exception already enabled"
|
---|
| 116 | end
|
---|
[270] | 117 | @cxx_exception_disabled = true
|
---|
| 118 | end
|
---|
| 119 |
|
---|
[331] | 120 | def enable_cxx_exception
|
---|
| 121 | return if @cxx_exception_enabled
|
---|
| 122 | return if @cxx_abi_enabled
|
---|
| 123 | if @cxx_exception_disabled
|
---|
| 124 | raise "cxx_exception disabled"
|
---|
| 125 | end
|
---|
| 126 | @cxx_exception_enabled = true
|
---|
| 127 | compilers.each { |c|
|
---|
| 128 | c.defines += %w(MRB_ENABLE_CXX_EXCEPTION)
|
---|
| 129 | c.flags << c.cxx_exception_flag
|
---|
| 130 | }
|
---|
| 131 | linker.command = cxx.command if toolchains.find { |v| v == 'gcc' }
|
---|
| 132 | end
|
---|
| 133 |
|
---|
| 134 | def cxx_exception_enabled?
|
---|
| 135 | @cxx_exception_enabled
|
---|
| 136 | end
|
---|
| 137 |
|
---|
[270] | 138 | def cxx_abi_enabled?
|
---|
| 139 | @cxx_abi_enabled
|
---|
| 140 | end
|
---|
| 141 |
|
---|
| 142 | def enable_cxx_abi
|
---|
[331] | 143 | return if @cxx_abi_enabled
|
---|
| 144 | if @cxx_exception_enabled
|
---|
| 145 | raise "cxx_exception already enabled"
|
---|
| 146 | end
|
---|
| 147 | compilers.each { |c|
|
---|
| 148 | c.defines += %w(MRB_ENABLE_CXX_EXCEPTION MRB_ENABLE_CXX_ABI)
|
---|
| 149 | c.flags << c.cxx_compile_flag
|
---|
| 150 | }
|
---|
| 151 | compilers.each { |c| c.flags << c.cxx_compile_flag }
|
---|
[270] | 152 | linker.command = cxx.command if toolchains.find { |v| v == 'gcc' }
|
---|
| 153 | @cxx_abi_enabled = true
|
---|
| 154 | end
|
---|
| 155 |
|
---|
| 156 | def compile_as_cxx src, cxx_src, obj = nil, includes = []
|
---|
| 157 | src = File.absolute_path src
|
---|
| 158 | cxx_src = File.absolute_path cxx_src
|
---|
| 159 | obj = objfile(cxx_src) if obj.nil?
|
---|
| 160 |
|
---|
| 161 | file cxx_src => [src, __FILE__] do |t|
|
---|
| 162 | FileUtils.mkdir_p File.dirname t.name
|
---|
| 163 | IO.write t.name, <<EOS
|
---|
| 164 | #define __STDC_CONSTANT_MACROS
|
---|
| 165 | #define __STDC_LIMIT_MACROS
|
---|
| 166 |
|
---|
[331] | 167 | #ifndef MRB_ENABLE_CXX_ABI
|
---|
[270] | 168 | extern "C" {
|
---|
[331] | 169 | #endif
|
---|
[270] | 170 | #include "#{src}"
|
---|
[331] | 171 | #ifndef MRB_ENABLE_CXX_ABI
|
---|
[270] | 172 | }
|
---|
[331] | 173 | #endif
|
---|
[270] | 174 | EOS
|
---|
| 175 | end
|
---|
| 176 |
|
---|
| 177 | file obj => cxx_src do |t|
|
---|
| 178 | cxx.run t.name, t.prerequisites.first, [], ["#{MRUBY_ROOT}/src"] + includes
|
---|
| 179 | end
|
---|
| 180 |
|
---|
| 181 | obj
|
---|
| 182 | end
|
---|
| 183 |
|
---|
| 184 | def enable_bintest
|
---|
| 185 | @enable_bintest = true
|
---|
| 186 | end
|
---|
| 187 |
|
---|
| 188 | def bintest_enabled?
|
---|
| 189 | @enable_bintest
|
---|
| 190 | end
|
---|
| 191 |
|
---|
| 192 | def toolchain(name, params={})
|
---|
| 193 | tc = Toolchain.toolchains[name.to_s]
|
---|
| 194 | fail "Unknown #{name} toolchain" unless tc
|
---|
| 195 | tc.setup(self, params)
|
---|
| 196 | @toolchains.unshift name.to_s
|
---|
| 197 | end
|
---|
| 198 |
|
---|
| 199 | def primary_toolchain
|
---|
| 200 | @toolchains.first
|
---|
| 201 | end
|
---|
| 202 |
|
---|
| 203 | def root
|
---|
| 204 | MRUBY_ROOT
|
---|
| 205 | end
|
---|
| 206 |
|
---|
| 207 | def enable_test
|
---|
| 208 | @enable_test = true
|
---|
| 209 | end
|
---|
| 210 |
|
---|
| 211 | def test_enabled?
|
---|
| 212 | @enable_test
|
---|
| 213 | end
|
---|
| 214 |
|
---|
| 215 | def build_mrbtest
|
---|
| 216 | gem :core => 'mruby-test'
|
---|
| 217 | end
|
---|
| 218 |
|
---|
| 219 | def build_mrbc_exec
|
---|
| 220 | gem :core => 'mruby-bin-mrbc'
|
---|
| 221 | end
|
---|
| 222 |
|
---|
| 223 | def mrbcfile
|
---|
| 224 | return @mrbcfile if @mrbcfile
|
---|
| 225 |
|
---|
| 226 | mrbc_build = MRuby.targets['host']
|
---|
| 227 | gems.each { |v| mrbc_build = self if v.name == 'mruby-bin-mrbc' }
|
---|
| 228 | @mrbcfile = mrbc_build.exefile("#{mrbc_build.build_dir}/bin/mrbc")
|
---|
| 229 | end
|
---|
| 230 |
|
---|
| 231 | def compilers
|
---|
| 232 | COMPILERS.map do |c|
|
---|
| 233 | instance_variable_get("@#{c}")
|
---|
| 234 | end
|
---|
| 235 | end
|
---|
| 236 |
|
---|
| 237 | def define_rules
|
---|
| 238 | compilers.each do |compiler|
|
---|
| 239 | if respond_to?(:enable_gems?) && enable_gems?
|
---|
| 240 | compiler.defines -= %w(DISABLE_GEMS)
|
---|
| 241 | else
|
---|
| 242 | compiler.defines += %w(DISABLE_GEMS)
|
---|
| 243 | end
|
---|
| 244 | compiler.define_rules build_dir, File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
---|
| 245 | end
|
---|
| 246 | end
|
---|
| 247 |
|
---|
| 248 | def filename(name)
|
---|
| 249 | if name.is_a?(Array)
|
---|
| 250 | name.flatten.map { |n| filename(n) }
|
---|
| 251 | else
|
---|
| 252 | '"%s"' % name.gsub('/', file_separator)
|
---|
| 253 | end
|
---|
| 254 | end
|
---|
| 255 |
|
---|
| 256 | def cygwin_filename(name)
|
---|
| 257 | if name.is_a?(Array)
|
---|
| 258 | name.flatten.map { |n| cygwin_filename(n) }
|
---|
| 259 | else
|
---|
| 260 | '"%s"' % `cygpath -w "#{filename(name)}"`.strip
|
---|
| 261 | end
|
---|
| 262 | end
|
---|
| 263 |
|
---|
| 264 | def exefile(name)
|
---|
| 265 | if name.is_a?(Array)
|
---|
| 266 | name.flatten.map { |n| exefile(n) }
|
---|
| 267 | else
|
---|
| 268 | "#{name}#{exts.executable}"
|
---|
| 269 | end
|
---|
| 270 | end
|
---|
| 271 |
|
---|
| 272 | def objfile(name)
|
---|
| 273 | if name.is_a?(Array)
|
---|
| 274 | name.flatten.map { |n| objfile(n) }
|
---|
| 275 | else
|
---|
| 276 | "#{name}#{exts.object}"
|
---|
| 277 | end
|
---|
| 278 | end
|
---|
| 279 |
|
---|
| 280 | def libfile(name)
|
---|
| 281 | if name.is_a?(Array)
|
---|
| 282 | name.flatten.map { |n| libfile(n) }
|
---|
| 283 | else
|
---|
| 284 | "#{name}#{exts.library}"
|
---|
| 285 | end
|
---|
| 286 | end
|
---|
| 287 |
|
---|
| 288 | def build_mrbtest_lib_only
|
---|
| 289 | @build_mrbtest_lib_only = true
|
---|
| 290 | end
|
---|
| 291 |
|
---|
| 292 | def build_mrbtest_lib_only?
|
---|
| 293 | @build_mrbtest_lib_only
|
---|
| 294 | end
|
---|
| 295 |
|
---|
| 296 | def run_test
|
---|
| 297 | puts ">>> Test #{name} <<<"
|
---|
| 298 | mrbtest = exefile("#{build_dir}/bin/mrbtest")
|
---|
| 299 | sh "#{filename mrbtest.relative_path}#{$verbose ? ' -v' : ''}"
|
---|
| 300 | puts
|
---|
| 301 | run_bintest if bintest_enabled?
|
---|
| 302 | end
|
---|
| 303 |
|
---|
| 304 | def run_bintest
|
---|
| 305 | targets = @gems.select { |v| File.directory? "#{v.dir}/bintest" }.map { |v| filename v.dir }
|
---|
| 306 | targets << filename(".") if File.directory? "./bintest"
|
---|
| 307 | sh "ruby test/bintest.rb #{targets.join ' '}"
|
---|
| 308 | end
|
---|
| 309 |
|
---|
| 310 | def print_build_summary
|
---|
| 311 | puts "================================================"
|
---|
| 312 | puts " Config Name: #{@name}"
|
---|
| 313 | puts " Output Directory: #{self.build_dir.relative_path}"
|
---|
| 314 | puts " Binaries: #{@bins.join(', ')}" unless @bins.empty?
|
---|
| 315 | unless @gems.empty?
|
---|
| 316 | puts " Included Gems:"
|
---|
| 317 | @gems.map do |gem|
|
---|
| 318 | gem_version = " - #{gem.version}" if gem.version != '0.0.0'
|
---|
| 319 | gem_summary = " - #{gem.summary}" if gem.summary
|
---|
| 320 | puts " #{gem.name}#{gem_version}#{gem_summary}"
|
---|
| 321 | puts " - Binaries: #{gem.bins.join(', ')}" unless gem.bins.empty?
|
---|
| 322 | end
|
---|
| 323 | end
|
---|
| 324 | puts "================================================"
|
---|
| 325 | puts
|
---|
| 326 | end
|
---|
| 327 | end # Build
|
---|
| 328 |
|
---|
| 329 | class CrossBuild < Build
|
---|
| 330 | attr_block %w(test_runner)
|
---|
| 331 | # cross compiling targets for building native extensions.
|
---|
| 332 | # host - arch of where the built binary will run
|
---|
| 333 | # build - arch of the machine building the binary
|
---|
| 334 | attr_accessor :host_target, :build_target
|
---|
| 335 |
|
---|
| 336 | def initialize(name, build_dir=nil, &block)
|
---|
| 337 | @test_runner = Command::CrossTestRunner.new(self)
|
---|
| 338 | super
|
---|
| 339 | end
|
---|
| 340 |
|
---|
| 341 | def mrbcfile
|
---|
| 342 | MRuby.targets['host'].exefile("#{MRuby.targets['host'].build_dir}/bin/mrbc")
|
---|
| 343 | end
|
---|
| 344 |
|
---|
| 345 | def run_test
|
---|
| 346 | mrbtest = exefile("#{build_dir}/bin/mrbtest")
|
---|
| 347 | if (@test_runner.command == nil)
|
---|
| 348 | puts "You should run #{mrbtest} on target device."
|
---|
| 349 | puts
|
---|
| 350 | else
|
---|
| 351 | @test_runner.run(mrbtest)
|
---|
| 352 | end
|
---|
| 353 | end
|
---|
| 354 | end # CrossBuild
|
---|
| 355 | end # MRuby
|
---|