[270] | 1 | require 'forwardable'
|
---|
[281] | 2 | require 'digest/md5'
|
---|
[270] | 3 |
|
---|
| 4 | module MRuby
|
---|
| 5 | class Command
|
---|
| 6 | include Rake::DSL
|
---|
| 7 | extend Forwardable
|
---|
| 8 | def_delegators :@build, :filename, :objfile, :libfile, :exefile, :cygwin_filename
|
---|
| 9 | attr_accessor :build, :command
|
---|
| 10 |
|
---|
| 11 | def initialize(build)
|
---|
| 12 | @build = build
|
---|
| 13 | end
|
---|
| 14 |
|
---|
| 15 | # clone is deep clone without @build
|
---|
| 16 | def clone
|
---|
| 17 | target = super
|
---|
| 18 | excepts = %w(@build)
|
---|
| 19 | instance_variables.each do |attr|
|
---|
| 20 | unless excepts.include?(attr.to_s)
|
---|
| 21 | val = Marshal::load(Marshal.dump(instance_variable_get(attr))) # deep clone
|
---|
| 22 | target.instance_variable_set(attr, val)
|
---|
| 23 | end
|
---|
| 24 | end
|
---|
| 25 | target
|
---|
| 26 | end
|
---|
| 27 |
|
---|
| 28 | NotFoundCommands = {}
|
---|
| 29 |
|
---|
| 30 | private
|
---|
| 31 | def _run(options, params={})
|
---|
| 32 | return sh command + ' ' + ( options % params ) if NotFoundCommands.key? @command
|
---|
| 33 | begin
|
---|
| 34 | sh build.filename(command) + ' ' + ( options % params )
|
---|
| 35 | rescue RuntimeError
|
---|
| 36 | NotFoundCommands[@command] = true
|
---|
| 37 | _run options, params
|
---|
| 38 | end
|
---|
| 39 | end
|
---|
| 40 | end
|
---|
| 41 |
|
---|
| 42 | class Command::Compiler < Command
|
---|
| 43 | attr_accessor :flags, :include_paths, :defines, :source_exts
|
---|
| 44 | attr_accessor :compile_options, :option_define, :option_include_path, :out_ext
|
---|
[331] | 45 | attr_accessor :cxx_compile_flag, :cxx_exception_flag
|
---|
[270] | 46 |
|
---|
| 47 | def initialize(build, source_exts=[])
|
---|
| 48 | super(build)
|
---|
| 49 | @command = ENV['CC'] || 'cc'
|
---|
| 50 | @flags = [ENV['CFLAGS'] || []]
|
---|
| 51 | @source_exts = source_exts
|
---|
| 52 | @include_paths = ["#{MRUBY_ROOT}/include"]
|
---|
| 53 | @defines = %w()
|
---|
| 54 | @option_include_path = '-I%s'
|
---|
| 55 | @option_define = '-D%s'
|
---|
| 56 | @compile_options = '%{flags} -o %{outfile} -c %{infile}'
|
---|
| 57 | end
|
---|
| 58 |
|
---|
| 59 | alias header_search_paths include_paths
|
---|
| 60 | def search_header_path(name)
|
---|
| 61 | header_search_paths.find do |v|
|
---|
| 62 | File.exist? build.filename("#{v}/#{name}").sub(/^"(.*)"$/, '\1')
|
---|
| 63 | end
|
---|
| 64 | end
|
---|
| 65 |
|
---|
| 66 | def search_header(name)
|
---|
| 67 | path = search_header_path name
|
---|
| 68 | path && build.filename("#{path}/#{name}").sub(/^"(.*)"$/, '\1')
|
---|
| 69 | end
|
---|
| 70 |
|
---|
| 71 | def all_flags(_defineds=[], _include_paths=[], _flags=[])
|
---|
| 72 | define_flags = [defines, _defineds].flatten.map{ |d| option_define % d }
|
---|
| 73 | include_path_flags = [include_paths, _include_paths].flatten.map do |f|
|
---|
| 74 | if MRUBY_BUILD_HOST_IS_CYGWIN
|
---|
| 75 | option_include_path % cygwin_filename(f)
|
---|
| 76 | else
|
---|
| 77 | option_include_path % filename(f)
|
---|
| 78 | end
|
---|
| 79 | end
|
---|
| 80 | [flags, define_flags, include_path_flags, _flags].flatten.join(' ')
|
---|
| 81 | end
|
---|
| 82 |
|
---|
| 83 | def run(outfile, infile, _defineds=[], _include_paths=[], _flags=[])
|
---|
| 84 | FileUtils.mkdir_p File.dirname(outfile)
|
---|
| 85 | _pp "CC", infile.relative_path, outfile.relative_path
|
---|
| 86 | if MRUBY_BUILD_HOST_IS_CYGWIN
|
---|
| 87 | _run compile_options, { :flags => all_flags(_defineds, _include_paths, _flags),
|
---|
[281] | 88 | :infile => cygwin_filename(infile), :outfile => cygwin_filename(outfile),
|
---|
| 89 | :outdir => File.dirname(outfile), :outfilebase => File.basename(outfile, ".*") }
|
---|
[270] | 90 | else
|
---|
| 91 | _run compile_options, { :flags => all_flags(_defineds, _include_paths, _flags),
|
---|
[281] | 92 | :infile => filename(infile), :outfile => filename(outfile),
|
---|
| 93 | :outdir => File.dirname(outfile), :outfilebase => File.basename(outfile, ".*") }
|
---|
[270] | 94 | end
|
---|
| 95 | end
|
---|
| 96 |
|
---|
| 97 | def define_rules(build_dir, source_dir='')
|
---|
| 98 | @out_ext = build.exts.object
|
---|
| 99 | gemrake = File.join(source_dir, "mrbgem.rake")
|
---|
| 100 | rakedep = File.exist?(gemrake) ? [ gemrake ] : []
|
---|
| 101 |
|
---|
| 102 | if build_dir.include? "mrbgems/"
|
---|
| 103 | generated_file_matcher = Regexp.new("^#{Regexp.escape build_dir}/(.*)#{Regexp.escape out_ext}$")
|
---|
| 104 | else
|
---|
| 105 | generated_file_matcher = Regexp.new("^#{Regexp.escape build_dir}/(?!mrbgems/.+/)(.*)#{Regexp.escape out_ext}$")
|
---|
| 106 | end
|
---|
| 107 | source_exts.each do |ext, compile|
|
---|
| 108 | rule generated_file_matcher => [
|
---|
| 109 | proc { |file|
|
---|
| 110 | file.sub(generated_file_matcher, "#{source_dir}/\\1#{ext}")
|
---|
| 111 | },
|
---|
| 112 | proc { |file|
|
---|
| 113 | get_dependencies(file) + rakedep
|
---|
| 114 | }
|
---|
| 115 | ] do |t|
|
---|
| 116 | run t.name, t.prerequisites.first
|
---|
| 117 | end
|
---|
| 118 |
|
---|
| 119 | rule generated_file_matcher => [
|
---|
| 120 | proc { |file|
|
---|
| 121 | file.sub(generated_file_matcher, "#{build_dir}/\\1#{ext}")
|
---|
| 122 | },
|
---|
| 123 | proc { |file|
|
---|
| 124 | get_dependencies(file) + rakedep
|
---|
| 125 | }
|
---|
| 126 | ] do |t|
|
---|
| 127 | run t.name, t.prerequisites.first
|
---|
| 128 | end
|
---|
| 129 | end
|
---|
| 130 | end
|
---|
| 131 |
|
---|
| 132 | private
|
---|
| 133 | def get_dependencies(file)
|
---|
| 134 | file = file.ext('d') unless File.extname(file) == '.d'
|
---|
| 135 | if File.exist?(file)
|
---|
[360] | 136 | deps = File.read(file).gsub("\\\n ", "").scan(/^\S+:\s+(.+)$/).flatten.map {|s| s.split(/(?<!\\) /) }.flatten
|
---|
| 137 | deps.collect{ |f| f.gsub(/(\\ )/, " ") }.flatten
|
---|
[270] | 138 | else
|
---|
| 139 | []
|
---|
| 140 | end + [ MRUBY_CONFIG ]
|
---|
| 141 | end
|
---|
| 142 | end
|
---|
| 143 |
|
---|
| 144 | class Command::Linker < Command
|
---|
| 145 | attr_accessor :flags, :library_paths, :flags_before_libraries, :libraries, :flags_after_libraries
|
---|
| 146 | attr_accessor :link_options, :option_library, :option_library_path
|
---|
| 147 |
|
---|
| 148 | def initialize(build)
|
---|
| 149 | super
|
---|
| 150 | @command = ENV['LD'] || 'ld'
|
---|
| 151 | @flags = (ENV['LDFLAGS'] || [])
|
---|
| 152 | @flags_before_libraries, @flags_after_libraries = [], []
|
---|
| 153 | @libraries = []
|
---|
| 154 | @library_paths = []
|
---|
| 155 | @option_library = '-l%s'
|
---|
| 156 | @option_library_path = '-L%s'
|
---|
| 157 | @link_options = "%{flags} -o %{outfile} %{objs} %{flags_before_libraries} %{libs} %{flags_after_libraries}"
|
---|
| 158 | end
|
---|
| 159 |
|
---|
| 160 | def all_flags(_library_paths=[], _flags=[])
|
---|
| 161 | library_path_flags = [library_paths, _library_paths].flatten.map do |f|
|
---|
| 162 | if MRUBY_BUILD_HOST_IS_CYGWIN
|
---|
| 163 | option_library_path % cygwin_filename(f)
|
---|
| 164 | else
|
---|
| 165 | option_library_path % filename(f)
|
---|
| 166 | end
|
---|
| 167 | end
|
---|
| 168 | [flags, library_path_flags, _flags].flatten.join(' ')
|
---|
| 169 | end
|
---|
| 170 |
|
---|
| 171 | def library_flags(_libraries)
|
---|
| 172 | [libraries, _libraries].flatten.map{ |d| option_library % d }.join(' ')
|
---|
| 173 | end
|
---|
| 174 |
|
---|
| 175 | def run(outfile, objfiles, _libraries=[], _library_paths=[], _flags=[], _flags_before_libraries=[], _flags_after_libraries=[])
|
---|
| 176 | FileUtils.mkdir_p File.dirname(outfile)
|
---|
| 177 | library_flags = [libraries, _libraries].flatten.map { |d| option_library % d }
|
---|
| 178 |
|
---|
| 179 | _pp "LD", outfile.relative_path
|
---|
| 180 | if MRUBY_BUILD_HOST_IS_CYGWIN
|
---|
| 181 | _run link_options, { :flags => all_flags(_library_paths, _flags),
|
---|
| 182 | :outfile => cygwin_filename(outfile) , :objs => cygwin_filename(objfiles).join(' '),
|
---|
| 183 | :flags_before_libraries => [flags_before_libraries, _flags_before_libraries].flatten.join(' '),
|
---|
| 184 | :flags_after_libraries => [flags_after_libraries, _flags_after_libraries].flatten.join(' '),
|
---|
[281] | 185 | :libs => library_flags.join(' '),
|
---|
| 186 | :outdir => File.dirname(outfile), :outfilebase => File.basename(outfile, ".*") }
|
---|
[270] | 187 | else
|
---|
| 188 | _run link_options, { :flags => all_flags(_library_paths, _flags),
|
---|
| 189 | :outfile => filename(outfile) , :objs => filename(objfiles).join(' '),
|
---|
| 190 | :flags_before_libraries => [flags_before_libraries, _flags_before_libraries].flatten.join(' '),
|
---|
| 191 | :flags_after_libraries => [flags_after_libraries, _flags_after_libraries].flatten.join(' '),
|
---|
[281] | 192 | :libs => library_flags.join(' '),
|
---|
| 193 | :outdir => File.dirname(outfile), :outfilebase => File.basename(outfile, ".*") }
|
---|
[270] | 194 | end
|
---|
| 195 | end
|
---|
| 196 | end
|
---|
| 197 |
|
---|
| 198 | class Command::Archiver < Command
|
---|
| 199 | attr_accessor :archive_options
|
---|
| 200 |
|
---|
| 201 | def initialize(build)
|
---|
| 202 | super
|
---|
| 203 | @command = ENV['AR'] || 'ar'
|
---|
| 204 | @archive_options = 'rs %{outfile} %{objs}'
|
---|
| 205 | end
|
---|
| 206 |
|
---|
| 207 | def run(outfile, objfiles)
|
---|
| 208 | FileUtils.mkdir_p File.dirname(outfile)
|
---|
| 209 | _pp "AR", outfile.relative_path
|
---|
[281] | 210 |
|
---|
| 211 | # reference from emar.py
|
---|
| 212 | outfilebase = File.dirname(outfile)
|
---|
| 213 | to_delete = []
|
---|
| 214 | newargs = []
|
---|
| 215 | objfiles.each do |orig_name|
|
---|
| 216 | dir_name = File.dirname(orig_name)
|
---|
| 217 | dir_name = dir_name.relative_path_from(Dir.pwd)
|
---|
| 218 | base_name = File.basename(orig_name)
|
---|
| 219 | parts = base_name.split('.')
|
---|
| 220 | # h = Digest::MD5.new.update(orig_name).to_s
|
---|
| 221 | h = Digest::MD5.new.update(orig_name).to_s.slice(0,4)
|
---|
| 222 | parts[0] += '_' + h
|
---|
| 223 | newname = parts.join('.')
|
---|
| 224 | full_newname = File.join(dir_name, newname)
|
---|
| 225 | if not File.exists?(full_newname)
|
---|
| 226 | begin # it is ok to fail here, we just don't get hashing
|
---|
| 227 | FileUtils.cp(orig_name, full_newname)
|
---|
| 228 | newargs << full_newname
|
---|
| 229 | to_delete << full_newname
|
---|
| 230 | rescue
|
---|
| 231 | end
|
---|
| 232 | end
|
---|
| 233 | end
|
---|
| 234 |
|
---|
[270] | 235 | if MRUBY_BUILD_HOST_IS_CYGWIN
|
---|
[281] | 236 | _run archive_options, { :outfile => cygwin_filename(outfile), :objs => cygwin_filename(newargs).join(' '), :outdir => File.dirname(outfile), :outfilebase => File.basename(outfile, ".*") }
|
---|
[270] | 237 | else
|
---|
[281] | 238 | _run archive_options, { :outfile => filename(outfile), :objs => filename(newargs).join(' '), :outdir => File.dirname(outfile), :outfilebase => File.basename(outfile, ".*") }
|
---|
[270] | 239 | end
|
---|
[281] | 240 |
|
---|
| 241 | to_delete.each do |d|
|
---|
| 242 | FileUtils.rm(d)
|
---|
| 243 | end
|
---|
[270] | 244 | end
|
---|
| 245 | end
|
---|
| 246 |
|
---|
| 247 | class Command::Yacc < Command
|
---|
| 248 | attr_accessor :compile_options
|
---|
| 249 |
|
---|
| 250 | def initialize(build)
|
---|
| 251 | super
|
---|
| 252 | @command = 'bison'
|
---|
| 253 | @compile_options = '-o %{outfile} %{infile}'
|
---|
| 254 | end
|
---|
| 255 |
|
---|
| 256 | def run(outfile, infile)
|
---|
| 257 | FileUtils.mkdir_p File.dirname(outfile)
|
---|
| 258 | _pp "YACC", infile.relative_path, outfile.relative_path
|
---|
| 259 | _run compile_options, { :outfile => filename(outfile) , :infile => filename(infile) }
|
---|
| 260 | end
|
---|
| 261 | end
|
---|
| 262 |
|
---|
| 263 | class Command::Gperf < Command
|
---|
| 264 | attr_accessor :compile_options
|
---|
| 265 |
|
---|
| 266 | def initialize(build)
|
---|
| 267 | super
|
---|
| 268 | @command = 'gperf'
|
---|
| 269 | @compile_options = '-L ANSI-C -C -p -j1 -i 1 -g -o -t -N mrb_reserved_word -k"1,3,$" %{infile} > %{outfile}'
|
---|
| 270 | end
|
---|
| 271 |
|
---|
| 272 | def run(outfile, infile)
|
---|
| 273 | FileUtils.mkdir_p File.dirname(outfile)
|
---|
| 274 | _pp "GPERF", infile.relative_path, outfile.relative_path
|
---|
| 275 | _run compile_options, { :outfile => filename(outfile) , :infile => filename(infile) }
|
---|
| 276 | end
|
---|
| 277 | end
|
---|
| 278 |
|
---|
| 279 | class Command::Git < Command
|
---|
| 280 | attr_accessor :flags
|
---|
| 281 | attr_accessor :clone_options, :pull_options, :checkout_options
|
---|
| 282 |
|
---|
| 283 | def initialize(build)
|
---|
| 284 | super
|
---|
| 285 | @command = 'git'
|
---|
| 286 | @flags = %w[]
|
---|
| 287 | @clone_options = "clone %{flags} %{url} %{dir}"
|
---|
| 288 | @pull_options = "pull"
|
---|
| 289 | @checkout_options = "checkout %{checksum_hash}"
|
---|
| 290 | end
|
---|
| 291 |
|
---|
| 292 | def run_clone(dir, url, _flags = [])
|
---|
| 293 | _pp "GIT", url, dir.relative_path
|
---|
| 294 | _run clone_options, { :flags => [flags, _flags].flatten.join(' '), :url => url, :dir => filename(dir) }
|
---|
| 295 | end
|
---|
| 296 |
|
---|
| 297 | def run_pull(dir, url)
|
---|
| 298 | root = Dir.pwd
|
---|
| 299 | Dir.chdir dir
|
---|
| 300 | _pp "GIT PULL", url, dir.relative_path
|
---|
| 301 | _run pull_options
|
---|
| 302 | Dir.chdir root
|
---|
| 303 | end
|
---|
| 304 |
|
---|
| 305 | def run_checkout(dir, checksum_hash)
|
---|
| 306 | root = Dir.pwd
|
---|
| 307 | Dir.chdir dir
|
---|
| 308 | _pp "GIT CHECKOUT", checksum_hash
|
---|
| 309 | _run checkout_options, { :checksum_hash => checksum_hash }
|
---|
| 310 | Dir.chdir root
|
---|
| 311 | end
|
---|
| 312 | end
|
---|
| 313 |
|
---|
| 314 | class Command::Mrbc < Command
|
---|
| 315 | attr_accessor :compile_options
|
---|
| 316 |
|
---|
| 317 | def initialize(build)
|
---|
| 318 | super
|
---|
| 319 | @command = nil
|
---|
| 320 | @compile_options = "-B%{funcname} -o-"
|
---|
| 321 | end
|
---|
| 322 |
|
---|
| 323 | def run(out, infiles, funcname)
|
---|
| 324 | @command ||= @build.mrbcfile
|
---|
| 325 | infiles = [infiles].flatten
|
---|
| 326 | infiles.each do |f|
|
---|
| 327 | _pp "MRBC", f.relative_path, nil, :indent => 2
|
---|
| 328 | end
|
---|
| 329 | IO.popen("#{filename @command} #{@compile_options % {:funcname => funcname}} #{filename(infiles).join(' ')}", 'r+') do |io|
|
---|
| 330 | out.puts io.read
|
---|
| 331 | end
|
---|
| 332 | # if mrbc execution fail, drop the file
|
---|
| 333 | if $?.exitstatus != 0
|
---|
| 334 | File.delete(out.path)
|
---|
| 335 | exit(-1)
|
---|
| 336 | end
|
---|
| 337 | end
|
---|
| 338 | end
|
---|
| 339 |
|
---|
| 340 | class Command::CrossTestRunner < Command
|
---|
| 341 | attr_accessor :runner_options
|
---|
| 342 | attr_accessor :verbose_flag
|
---|
| 343 | attr_accessor :flags
|
---|
| 344 |
|
---|
| 345 | def initialize(build)
|
---|
| 346 | super
|
---|
| 347 | @command = nil
|
---|
| 348 | @runner_options = '%{flags} %{infile}'
|
---|
| 349 | @verbose_flag = ''
|
---|
| 350 | @flags = []
|
---|
| 351 | end
|
---|
| 352 |
|
---|
| 353 | def run(testbinfile)
|
---|
| 354 | puts "TEST for " + @build.name
|
---|
| 355 | _run runner_options, { :flags => [flags, verbose_flag].flatten.join(' '), :infile => testbinfile }
|
---|
| 356 | end
|
---|
| 357 | end
|
---|
| 358 |
|
---|
| 359 | end
|
---|