7 def_delegators :@build, :filename, :objfile, :libfile, :exefile
8 attr_accessor :build, :command
14 # clone is deep clone without @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)
28 if ENV['OS'] == 'Windows_NT'
38 def _run(options, params={})
39 return sh command + ' ' + ( options % params ) if NotFoundCommands.key? @command
41 sh build.filename(command) + ' ' + ( options % params )
43 NotFoundCommands[@command] = true
49 class Command::Compiler < Command
50 attr_accessor :flags, :include_paths, :defines, :source_exts
51 attr_accessor :compile_options, :option_define, :option_include_path, :out_ext
52 attr_accessor :cxx_compile_flag, :cxx_exception_flag, :cxx_invalid_flags
54 def initialize(build, source_exts=[])
56 @command = ENV['CC'] || 'cc'
57 @flags = [ENV['CFLAGS'] || []]
58 @source_exts = source_exts
59 @include_paths = ["#{MRUBY_ROOT}/include"]
61 @option_include_path = %q[-I"%s"]
62 @option_define = %q[-D"%s"]
63 @compile_options = %q[%{flags} -o "%{outfile}" -c "%{infile}"]
64 @cxx_invalid_flags = []
67 alias header_search_paths include_paths
68 def search_header_path(name)
69 header_search_paths.find do |v|
70 File.exist? build.filename("#{v}/#{name}").sub(/^"(.*)"$/, '\1')
74 def search_header(name)
75 path = search_header_path name
76 path && build.filename("#{path}/#{name}").sub(/^"(.*)"$/, '\1')
79 def all_flags(_defines=[], _include_paths=[], _flags=[])
80 define_flags = [defines, _defines].flatten.map{ |d| option_define % d }
81 include_path_flags = [include_paths, _include_paths].flatten.map do |f|
82 option_include_path % filename(f)
84 [flags, define_flags, include_path_flags, _flags].flatten.join(' ')
87 def run(outfile, infile, _defines=[], _include_paths=[], _flags=[])
88 mkdir_p File.dirname(outfile)
89 _pp "CC", infile.relative_path, outfile.relative_path
90 _run compile_options, { :flags => all_flags(_defines, _include_paths, _flags),
91 :infile => filename(infile), :outfile => filename(outfile) }
94 def define_rules(build_dir, source_dir='')
95 @out_ext = build.exts.object
96 gemrake = File.join(source_dir, "mrbgem.rake")
97 rakedep = File.exist?(gemrake) ? [ gemrake ] : []
99 if build_dir.include? "mrbgems/"
100 generated_file_matcher = Regexp.new("^#{Regexp.escape build_dir}/(.*)#{Regexp.escape out_ext}$")
102 generated_file_matcher = Regexp.new("^#{Regexp.escape build_dir}/(?!mrbgems/.+/)(.*)#{Regexp.escape out_ext}$")
104 source_exts.each do |ext, compile|
105 rule generated_file_matcher => [
107 file.sub(generated_file_matcher, "#{source_dir}/\\1#{ext}")
110 get_dependencies(file) + rakedep
113 run t.name, t.prerequisites.first
116 rule generated_file_matcher => [
118 file.sub(generated_file_matcher, "#{build_dir}/\\1#{ext}")
121 get_dependencies(file) + rakedep
124 run t.name, t.prerequisites.first
132 # === Example of +.d+ file
134 # ==== Without <tt>-MP</tt> compiler flag
136 # /build/host/src/array.o: \
138 # /include/mruby/common.h \
139 # /include/mruby/value.h \
142 # ==== With <tt>-MP</tt> compiler flag
144 # /build/host/src/array.o: \
146 # /include/mruby/common.h \
147 # /include/mruby/value.h \
150 # /include/mruby/common.h:
152 # /include/mruby/value.h:
154 # /src/value_array.h:
156 def get_dependencies(file)
157 file = file.ext('d') unless File.extname(file) == '.d'
160 File.foreach(file){|line| deps << $1 if /^ +(.*?)(?: *\\)?$/ =~ line}
167 class Command::Linker < Command
168 attr_accessor :flags, :library_paths, :flags_before_libraries, :libraries, :flags_after_libraries
169 attr_accessor :link_options, :option_library, :option_library_path
171 def initialize(build)
173 @command = ENV['LD'] || 'ld'
174 @flags = (ENV['LDFLAGS'] || [])
175 @flags_before_libraries, @flags_after_libraries = [], []
178 @option_library = %q[-l"%s"]
179 @option_library_path = %q[-L"%s"]
180 @link_options = %Q[%{flags} -o "%{outfile}" %{objs} %{flags_before_libraries} %{libs} %{flags_after_libraries}]
183 def all_flags(_library_paths=[], _flags=[])
184 library_path_flags = [library_paths, _library_paths].flatten.map do |f|
185 option_library_path % filename(f)
187 [flags, library_path_flags, _flags].flatten.join(' ')
190 def library_flags(_libraries)
191 [libraries, _libraries].flatten.map{ |d| option_library % d }.join(' ')
194 def run(outfile, objfiles, _libraries=[], _library_paths=[], _flags=[], _flags_before_libraries=[], _flags_after_libraries=[])
195 mkdir_p File.dirname(outfile)
196 library_flags = [libraries, _libraries].flatten.map { |d| option_library % d }
198 _pp "LD", outfile.relative_path
199 _run link_options, { :flags => all_flags(_library_paths, _flags),
200 :outfile => filename(outfile) , :objs => filename(objfiles).map{|f| %Q["#{f}"]}.join(' '),
201 :flags_before_libraries => [flags_before_libraries, _flags_before_libraries].flatten.join(' '),
202 :flags_after_libraries => [flags_after_libraries, _flags_after_libraries].flatten.join(' '),
203 :libs => library_flags.join(' ') }
207 class Command::Archiver < Command
208 attr_accessor :archive_options
210 def initialize(build)
212 @command = ENV['AR'] || 'ar'
213 @archive_options = 'rs "%{outfile}" %{objs}'
216 def run(outfile, objfiles)
217 mkdir_p File.dirname(outfile)
218 _pp "AR", outfile.relative_path
219 _run archive_options, { :outfile => filename(outfile), :objs => filename(objfiles).map{|f| %Q["#{f}"]}.join(' ') }
223 class Command::Yacc < Command
224 attr_accessor :compile_options
226 def initialize(build)
229 @compile_options = %q[-o "%{outfile}" "%{infile}"]
232 def run(outfile, infile)
233 mkdir_p File.dirname(outfile)
234 _pp "YACC", infile.relative_path, outfile.relative_path
235 _run compile_options, { :outfile => filename(outfile) , :infile => filename(infile) }
239 class Command::Gperf < Command
240 attr_accessor :compile_options
242 def initialize(build)
245 @compile_options = %q[-L ANSI-C -C -p -j1 -i 1 -g -o -t -N mrb_reserved_word -k"1,3,$" "%{infile}" > "%{outfile}"]
248 def run(outfile, infile)
249 mkdir_p File.dirname(outfile)
250 _pp "GPERF", infile.relative_path, outfile.relative_path
251 _run compile_options, { :outfile => filename(outfile) , :infile => filename(infile) }
255 class Command::Git < Command
257 attr_accessor :clone_options, :pull_options, :checkout_options, :checkout_detach_options, :reset_options
259 def initialize(build)
263 @clone_options = "clone %{flags} %{url} %{dir}"
264 @pull_options = "--git-dir %{repo_dir}/.git --work-tree %{repo_dir} pull"
265 @checkout_options = "--git-dir %{repo_dir}/.git --work-tree %{repo_dir} checkout %{checksum_hash}"
266 @checkout_detach_options = "--git-dir %{repo_dir}/.git --work-tree %{repo_dir} checkout --detach %{checksum_hash}"
267 @reset_options = "--git-dir %{repo_dir}/.git --work-tree %{repo_dir} reset %{checksum_hash}"
270 def run_clone(dir, url, _flags = [])
271 _pp "GIT", url, dir.relative_path
272 _run clone_options, { :flags => [flags, _flags].flatten.join(' '), :url => shellquote(url), :dir => shellquote(filename(dir)) }
275 def run_pull(dir, url)
276 _pp "GIT PULL", url, dir.relative_path
277 _run pull_options, { :repo_dir => shellquote(dir) }
280 def run_checkout(dir, checksum_hash)
281 _pp "GIT CHECKOUT", dir, checksum_hash
282 _run checkout_options, { :checksum_hash => checksum_hash, :repo_dir => shellquote(dir) }
285 def run_checkout_detach(dir, checksum_hash)
286 _pp "GIT CHECKOUT DETACH", dir, checksum_hash
287 _run checkout_detach_options, { :checksum_hash => checksum_hash, :repo_dir => shellquote(dir) }
290 def run_reset_hard(dir, checksum_hash)
291 _pp "GIT RESET", dir, checksum_hash
292 _run reset_options, { :checksum_hash => checksum_hash, :repo_dir => shellquote(dir) }
296 `#{@command} --git-dir #{shellquote(dir +'/.git')} --work-tree #{shellquote(dir)} rev-parse --verify HEAD`.strip
299 def current_branch(dir)
300 `#{@command} --git-dir #{shellquote(dir + '/.git')} --work-tree #{shellquote(dir)} rev-parse --abbrev-ref HEAD`.strip
304 class Command::Mrbc < Command
305 attr_accessor :compile_options
307 def initialize(build)
310 @compile_options = "-B%{funcname} -o-"
313 def run(out, infiles, funcname)
314 @command ||= @build.mrbcfile
315 infiles = [infiles].flatten
317 _pp "MRBC", f.relative_path, nil, :indent => 2
319 cmd = %Q["#{filename @command}" #{@compile_options % {:funcname => funcname}} #{filename(infiles).map{|f| %Q["#{f}"]}.join(' ')}]
320 puts cmd if Rake.verbose
321 IO.popen(cmd, 'r+') do |io|
324 # if mrbc execution fail, drop the file
325 if $?.exitstatus != 0
326 File.delete(out.path)
332 class Command::CrossTestRunner < Command
333 attr_accessor :runner_options
334 attr_accessor :verbose_flag
337 def initialize(build)
340 @runner_options = '%{flags} %{infile}'
346 puts "TEST for " + @build.name
347 _run runner_options, { :flags => [flags, verbose_flag].flatten.join(' '), :infile => testbinfile }