| 1 |
require 'forwardable' |
| 2 |
|
| 3 |
module MRuby |
| 4 |
class Command |
| 5 |
include Rake::DSL |
| 6 |
extend Forwardable |
| 7 |
def_delegators :@build, :filename, :objfile, :libfile, :exefile |
| 8 |
attr_accessor :build, :command |
| 9 |
|
| 10 |
def initialize(build) |
| 11 |
@build = build |
| 12 |
end |
| 13 |
|
| 14 |
# clone is deep clone without @build |
| 15 |
def clone |
| 16 |
target = super |
| 17 |
excepts = %w(@build) |
| 18 |
instance_variables.each do |attr| |
| 19 |
unless excepts.include?(attr.to_s) |
| 20 |
val = Marshal::load(Marshal.dump(instance_variable_get(attr))) # deep clone |
| 21 |
target.instance_variable_set(attr, val) |
| 22 |
end |
| 23 |
end |
| 24 |
target |
| 25 |
end |
| 26 |
|
| 27 |
def shellquote(s) |
| 28 |
"\"#{s}\"" |
| 29 |
end |
| 30 |
|
| 31 |
private |
| 32 |
def _run(options, params={}) |
| 33 |
sh "#{build.filename(command)} #{options % params}" |
| 34 |
end |
| 35 |
end |
| 36 |
|
| 37 |
class Command::Compiler < Command |
| 38 |
attr_accessor :label, :flags, :include_paths, :defines, :source_exts |
| 39 |
attr_accessor :compile_options, :option_define, :option_include_path, :out_ext |
| 40 |
attr_accessor :cxx_compile_flag, :cxx_exception_flag, :cxx_invalid_flags |
| 41 |
attr_writer :preprocess_options |
| 42 |
|
| 43 |
def initialize(build, source_exts=[], label: "CC") |
| 44 |
super(build) |
| 45 |
@command = ENV['CC'] || 'cc' |
| 46 |
@label = label |
| 47 |
@flags = [ENV['CFLAGS'] || []] |
| 48 |
@source_exts = source_exts |
| 49 |
@include_paths = ["#{MRUBY_ROOT}/include"] |
| 50 |
@defines = [] |
| 51 |
@option_include_path = %q[-I"%s"] |
| 52 |
@option_define = %q[-D"%s"] |
| 53 |
@compile_options = %q[%{flags} -o "%{outfile}" -c "%{infile}"] |
| 54 |
@cxx_invalid_flags = [] |
| 55 |
@out_ext = build.exts.object |
| 56 |
end |
| 57 |
|
| 58 |
alias header_search_paths include_paths |
| 59 |
|
| 60 |
def preprocess_options |
| 61 |
@preprocess_options ||= @compile_options.sub(/(?:\A|\s)\K-c(?=\s)/, "-E -P") |
| 62 |
end |
| 63 |
|
| 64 |
def search_header_path(name) |
| 65 |
header_search_paths.find do |v| |
| 66 |
File.exist? build.filename("#{v}/#{name}").sub(/^"(.*)"$/, '\1') |
| 67 |
end |
| 68 |
end |
| 69 |
|
| 70 |
def search_header(name) |
| 71 |
path = search_header_path name |
| 72 |
path && build.filename("#{path}/#{name}").sub(/^"(.*)"$/, '\1') |
| 73 |
end |
| 74 |
|
| 75 |
def all_flags(_defines=[], _include_paths=[], _flags=[]) |
| 76 |
define_flags = [defines, _defines, build.defines].flatten.map{ |d| option_define % d } |
| 77 |
include_path_flags = [include_paths, _include_paths].flatten.map do |f| |
| 78 |
option_include_path % filename(f) |
| 79 |
end |
| 80 |
[flags, define_flags, include_path_flags, _flags].flatten.join(' ') |
| 81 |
end |
| 82 |
|
| 83 |
def run(outfile, infile, _defines=[], _include_paths=[], _flags=[]) |
| 84 |
mkdir_p File.dirname(outfile) |
| 85 |
flags = all_flags(_defines, _include_paths, _flags) |
| 86 |
if object_ext?(outfile) |
| 87 |
label = @label |
| 88 |
opts = compile_options |
| 89 |
else |
| 90 |
label = "CPP" |
| 91 |
opts = preprocess_options |
| 92 |
flags << " -DMRB_PRESYM_SCANNING" |
| 93 |
end |
| 94 |
_pp label, infile.relative_path, outfile.relative_path |
| 95 |
_run opts, flags: flags, infile: filename(infile), outfile: filename(outfile) |
| 96 |
end |
| 97 |
|
| 98 |
def define_rules(build_dir, source_dir='', out_ext=build.exts.object) |
| 99 |
gemrake = File.join(source_dir, "mrbgem.rake") |
| 100 |
rakedep = File.exist?(gemrake) ? [ gemrake ] : [] |
| 101 |
|
| 102 |
bd = build_dir |
| 103 |
if bd.start_with?(MRUBY_ROOT) |
| 104 |
bd = bd.sub(MRUBY_ROOT, '') |
| 105 |
end |
| 106 |
if bd.include? "mrbgems/" |
| 107 |
generated_file_matcher = Regexp.new("^#{Regexp.escape build_dir}/(?!mrbc/)(.*)#{Regexp.escape out_ext}$") |
| 108 |
else |
| 109 |
generated_file_matcher = Regexp.new("^#{Regexp.escape build_dir}/(?!mrbc/|mrbgems/.+/)(.*)#{Regexp.escape out_ext}$") |
| 110 |
end |
| 111 |
source_exts.each do |ext| |
| 112 |
rule generated_file_matcher => [ |
| 113 |
proc { |file| |
| 114 |
file.sub(generated_file_matcher, "#{source_dir}/\\1#{ext}") |
| 115 |
}, |
| 116 |
proc { |file| |
| 117 |
get_dependencies(file) + rakedep |
| 118 |
} |
| 119 |
] do |t| |
| 120 |
run t.name, t.prerequisites.first |
| 121 |
end |
| 122 |
|
| 123 |
rule generated_file_matcher => [ |
| 124 |
proc { |file| |
| 125 |
file.sub(generated_file_matcher, "#{build_dir}/\\1#{ext}") |
| 126 |
}, |
| 127 |
proc { |file| |
| 128 |
get_dependencies(file) + rakedep |
| 129 |
} |
| 130 |
] do |t| |
| 131 |
run t.name, t.prerequisites.first |
| 132 |
end |
| 133 |
end |
| 134 |
end |
| 135 |
|
| 136 |
# This method can be redefined as a singleton method where appropriate. |
| 137 |
# Manipulate `flags`, `include_paths` and/or more if necessary. |
| 138 |
def setup_debug(conf) |
| 139 |
nil |
| 140 |
end |
| 141 |
|
| 142 |
private |
| 143 |
|
| 144 |
# |
| 145 |
# === Example of +.d+ file |
| 146 |
# |
| 147 |
# ==== Without <tt>-MP</tt> compiler flag |
| 148 |
# |
| 149 |
# /build/host/src/array.o: /src/array.c \ |
| 150 |
# /include/mruby/common.h /include/mruby/value.h \ |
| 151 |
# /src/value_array.h |
| 152 |
# |
| 153 |
# ==== With <tt>-MP</tt> compiler flag |
| 154 |
# |
| 155 |
# /build/host/src/array.o: /src/array.c \ |
| 156 |
# /include/mruby/common.h /include/mruby/value.h \ |
| 157 |
# /src/value_array.h |
| 158 |
# |
| 159 |
# /include/mruby/common.h: |
| 160 |
# |
| 161 |
# /include/mruby/value.h: |
| 162 |
# |
| 163 |
# /src/value_array.h: |
| 164 |
# |
| 165 |
def get_dependencies(file) |
| 166 |
dep_file = file.ext(".d") |
| 167 |
return [MRUBY_CONFIG] unless object_ext?(file) && File.exist?(dep_file) |
| 168 |
|
| 169 |
deps = File.read(dep_file).gsub("\\\n ", "").split("\n").map do |dep_line| |
| 170 |
# dep_line: |
| 171 |
# - "/build/host/src/array.o: /src/array.c /include/mruby/common.h ..." |
| 172 |
# - "" |
| 173 |
# - "/include/mruby/common.h:" |
| 174 |
dep_line.scan(/^\S+:\s+(.+)$/).flatten.map { |s| s.split(' ') }.flatten |
| 175 |
# => ["/src/array.c", "/include/mruby/common.h" , ...] |
| 176 |
# [] |
| 177 |
# [] |
| 178 |
end.flatten.uniq |
| 179 |
deps << MRUBY_CONFIG |
| 180 |
end |
| 181 |
|
| 182 |
def object_ext?(path) |
| 183 |
File.extname(path) == build.exts.object |
| 184 |
end |
| 185 |
end |
| 186 |
|
| 187 |
class Command::Linker < Command |
| 188 |
attr_accessor :flags, :library_paths, :flags_before_libraries, :libraries, :flags_after_libraries |
| 189 |
attr_accessor :link_options, :option_library, :option_library_path |
| 190 |
|
| 191 |
def initialize(build) |
| 192 |
super |
| 193 |
@command = ENV['LD'] || 'ld' |
| 194 |
@flags = (ENV['LDFLAGS'] || []) |
| 195 |
@flags_before_libraries, @flags_after_libraries = [], [] |
| 196 |
@libraries = [] |
| 197 |
@library_paths = [] |
| 198 |
@option_library = %q[-l"%s"] |
| 199 |
@option_library_path = %q[-L"%s"] |
| 200 |
@link_options = %Q[%{flags} -o "%{outfile}" %{objs} %{flags_before_libraries} %{libs} %{flags_after_libraries}] |
| 201 |
end |
| 202 |
|
| 203 |
def all_flags(_library_paths=[], _flags=[]) |
| 204 |
library_path_flags = [library_paths, _library_paths].flatten.map do |f| |
| 205 |
option_library_path % filename(f) |
| 206 |
end |
| 207 |
[flags, library_path_flags, _flags].flatten.join(' ') |
| 208 |
end |
| 209 |
|
| 210 |
def library_flags(_libraries) |
| 211 |
[libraries, _libraries].flatten.map{ |d| option_library % d }.join(' ') |
| 212 |
end |
| 213 |
|
| 214 |
def run_attrs |
| 215 |
[@libraries, @library_paths, @flags, @flags_before_libraries, @flags_after_libraries] |
| 216 |
end |
| 217 |
|
| 218 |
def run(outfile, objfiles, _libraries=[], _library_paths=[], _flags=[], _flags_before_libraries=[], _flags_after_libraries=[]) |
| 219 |
mkdir_p File.dirname(outfile) |
| 220 |
library_flags = [libraries, _libraries].flatten.map { |d| option_library % d } |
| 221 |
|
| 222 |
_pp "LD", outfile.relative_path |
| 223 |
_run link_options, { :flags => all_flags(_library_paths, _flags), |
| 224 |
:outfile => filename(outfile) , :objs => filename(objfiles).map{|f| %Q["#{f}"]}.join(' '), |
| 225 |
:flags_before_libraries => [flags_before_libraries, _flags_before_libraries].flatten.join(' '), |
| 226 |
:flags_after_libraries => [flags_after_libraries, _flags_after_libraries].flatten.join(' '), |
| 227 |
:libs => library_flags.join(' ') } |
| 228 |
end |
| 229 |
end |
| 230 |
|
| 231 |
class Command::Archiver < Command |
| 232 |
attr_accessor :archive_options |
| 233 |
|
| 234 |
def initialize(build) |
| 235 |
super |
| 236 |
@command = ENV['AR'] || 'ar' |
| 237 |
@archive_options = 'rs "%{outfile}" %{objs}' |
| 238 |
end |
| 239 |
|
| 240 |
def run(outfile, objfiles) |
| 241 |
mkdir_p File.dirname(outfile) |
| 242 |
_pp "AR", outfile.relative_path |
| 243 |
_run archive_options, { :outfile => filename(outfile), :objs => filename(objfiles).map{|f| %Q["#{f}"]}.join(' ') } |
| 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 = %q[-o "%{outfile}" "%{infile}"] |
| 254 |
end |
| 255 |
|
| 256 |
def run(outfile, infile) |
| 257 |
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 = %q[-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 |
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, :checkout_detach_options, :reset_options |
| 282 |
|
| 283 |
def initialize(build) |
| 284 |
super |
| 285 |
@command = 'git' |
| 286 |
@flags = [] |
| 287 |
@clone_options = "clone %{flags} %{url} %{dir}" |
| 288 |
@pull_options = "--git-dir %{repo_dir}/.git --work-tree %{repo_dir} pull" |
| 289 |
@checkout_options = "--git-dir %{repo_dir}/.git --work-tree %{repo_dir} checkout %{checksum_hash}" |
| 290 |
@checkout_detach_options = "--git-dir %{repo_dir}/.git --work-tree %{repo_dir} checkout --detach %{checksum_hash}" |
| 291 |
@reset_options = "--git-dir %{repo_dir}/.git --work-tree %{repo_dir} reset %{checksum_hash}" |
| 292 |
end |
| 293 |
|
| 294 |
def run_clone(dir, url, _flags = []) |
| 295 |
_pp "GIT", url, dir.relative_path |
| 296 |
_run clone_options, { :flags => [flags, _flags].flatten.join(' '), :url => shellquote(url), :dir => shellquote(filename(dir)) } |
| 297 |
end |
| 298 |
|
| 299 |
def run_pull(dir, url) |
| 300 |
_pp "GIT PULL", url, dir.relative_path |
| 301 |
_run pull_options, { :repo_dir => shellquote(dir) } |
| 302 |
end |
| 303 |
|
| 304 |
def run_checkout(dir, checksum_hash) |
| 305 |
_pp "GIT CHECKOUT", dir, checksum_hash |
| 306 |
_run checkout_options, { :checksum_hash => checksum_hash, :repo_dir => shellquote(dir) } |
| 307 |
end |
| 308 |
|
| 309 |
def run_checkout_detach(dir, checksum_hash) |
| 310 |
_pp "GIT CHECKOUT DETACH", dir, checksum_hash |
| 311 |
_run checkout_detach_options, { :checksum_hash => checksum_hash, :repo_dir => shellquote(dir) } |
| 312 |
end |
| 313 |
|
| 314 |
def run_reset_hard(dir, checksum_hash) |
| 315 |
_pp "GIT RESET", dir, checksum_hash |
| 316 |
_run reset_options, { :checksum_hash => checksum_hash, :repo_dir => shellquote(dir) } |
| 317 |
end |
| 318 |
|
| 319 |
def commit_hash(dir) |
| 320 |
`#{@command} --git-dir #{shellquote(dir +'/.git')} --work-tree #{shellquote(dir)} rev-parse --verify HEAD`.strip |
| 321 |
end |
| 322 |
|
| 323 |
def current_branch(dir) |
| 324 |
`#{@command} --git-dir #{shellquote(dir + '/.git')} --work-tree #{shellquote(dir)} rev-parse --abbrev-ref HEAD`.strip |
| 325 |
end |
| 326 |
end |
| 327 |
|
| 328 |
class Command::Mrbc < Command |
| 329 |
attr_accessor :compile_options |
| 330 |
|
| 331 |
def initialize(build) |
| 332 |
super |
| 333 |
@command = nil |
| 334 |
@compile_options = "-B%{funcname} -o-" |
| 335 |
end |
| 336 |
|
| 337 |
def run(out, infiles, funcname, cdump: true, static: false) |
| 338 |
@command ||= @build.mrbcfile |
| 339 |
infiles = [infiles].flatten |
| 340 |
infiles.each_with_index do |f, i| |
| 341 |
_pp i == 0 ? "MRBC" : "", f.relative_path, indent: 2 |
| 342 |
end |
| 343 |
opt = @compile_options % {funcname: funcname} |
| 344 |
opt << " -S" if cdump |
| 345 |
opt << " -s" if static |
| 346 |
cmd = %["#{filename @command}" #{opt} #{filename(infiles).map{|f| %["#{f}"]}.join(' ')}] |
| 347 |
puts cmd if Rake.verbose |
| 348 |
IO.popen(cmd, 'r') do |io| |
| 349 |
out.puts io.read |
| 350 |
end |
| 351 |
# if mrbc execution fail, drop the file |
| 352 |
unless $?.success? |
| 353 |
rm_f out.path |
| 354 |
fail "Command failed with status (#{$?.exitstatus}): [#{cmd[0,42]}...]" |
| 355 |
end |
| 356 |
end |
| 357 |
end |
| 358 |
|
| 359 |
class Command::CrossTestRunner < Command |
| 360 |
attr_accessor :runner_options |
| 361 |
attr_accessor :verbose_flag |
| 362 |
attr_accessor :flags |
| 363 |
|
| 364 |
def initialize(build) |
| 365 |
super |
| 366 |
@command = nil |
| 367 |
@runner_options = '%{flags} %{infile}' |
| 368 |
@verbose_flag = '' |
| 369 |
@flags = [] |
| 370 |
end |
| 371 |
|
| 372 |
def emulator |
| 373 |
return "" unless @command |
| 374 |
return [@command, *@flags].map{|c| shellquote(c)}.join(' ') |
| 375 |
end |
| 376 |
|
| 377 |
def run(testbinfile) |
| 378 |
puts "TEST for " + @build.name |
| 379 |
_run runner_options, { :flags => [flags, verbose_flag].flatten.join(' '), :infile => testbinfile } |
| 380 |
end |
| 381 |
end |
| 382 |
|
| 383 |
end |