# -*- ruby -*-

require 'fileutils'
module FileUtils
  def chdir(dir, options = {}, &block) # :yield: dir
    dir = File.expand_path(dir)
    fu_output_message "Entering directory `#{dir}'" if options[:verbose]
    Dir.chdir(dir, &block)
    fu_output_message "Leaving directory `#{dir}'" if options[:verbose] and block
  end
  module_function :chdir
end



task :default => [:test]


$objDir = 'obj'

exeext = (/cygwin/ =~ RUBY_PLATFORM) ? '.exe' : ''
testerBin = 'tester' + exeext
tabcpp = 'errortest.tab.cpp'
houken = '../houken/houken' + exeext


file tabcpp => ['errortest.peg', houken] do
  sh houken, 'errortest.peg'
end

task :tab => [houken] do
  sh houken, 'errortest.peg'
end


srcs = FileList["../src/*.cpp"]
srcs += FileList['*.cpp']
srcs.include('../etc/printSyntaxTree.cpp')
srcs.include('../machdep/unix/sysDep.cpp')
srcs.include(tabcpp) unless srcs.include?(tabcpp)

desc "create tester"
task :makeTester => [tabcpp, testerBin]

file testerBin => srcs do
  sh 'g++', '-I../src', '-I../machdep/unix', '-I.', '-g', '-o', testerBin, *srcs
end

task :test => [testerBin] do
  sh './'+testerBin
end

file houken => :houken

task :houken do
  chdir '../houken', :verbose=>true do
    sh 'rake'
  end
end

desc "cleanup"
task :clean do
  FileUtils.rm_f(Dir["errortest.tab.*"], :verbose => true)
  FileUtils.rm_f(testerBin, :verbose => true)
  FileUtils.rm_f(testerBin+'.stackdump', :verbose => true)
end
