#!/usr/bin/env ruby

if( ARGV.include?("--help") )
  print <<-EOF
  --help             print this messages
  --enable-OPTION    enable yarv option
  --disable-OPTION   disable yarv option

  OPTION:
  
  dispatch option (default enable):
  * dispatch-threaded-code
  * dispatch-direct-threaded-code

  optimization option (default enable):
  * opt-basic-operations
  * opt-regexp-match
  * opt-insns-unification
  * opt-stack-caching
  * inline-method-cache
  
  others (default disable):
  * opt-jit-compile          (experimental, support only few insns)
  * test-aot-compile         (experimental)
  * fake-inline-method-cache (dengerous)
  * support-goto             (only joke)
  
  EOF
  exit(0)
end


if RUBY_VERSION != '1.9.0'
  raise "YARV requires ruby 1.9.0"
end

unless defined?(::YARV_PATCHED)
  raise "ruby should be applied yarv patch"
end

require 'mkmf.rb'


##
def check_arg arg, default=nil
  if enable_config(arg, default)
    $defs.push "-D#{arg.upcase.tr_s("^A-Z0-9_", "_")}"
  end
end

# dispatcher
check_arg('dispatch-threaded-code', true)
check_arg('dispatch-direct-threaded-code', true)

# optimizer
check_arg('opt-basic-operations', true)
check_arg('opt-regexp-match'    , true)

check_arg('opt-insns-unification', true)
check_arg('opt-stack-caching'   , true)

check_arg('inline-method-cache', true)

# danger
check_arg('opt-jit-compile', false)
check_arg('test-aot-compile', false)
check_arg('fake-inline-method-cache', false)
check_arg('support-goto', false)

have_type("ulong", "sys/types.h")

$cleanfiles += %w( *.inc ) - %w(vm_evalbody.inc call_cfunc.inc)

create_makefile('yarvcore')

if macro_defined?("__GNUC__", "") && try_compile(<<-EOS, '-fno-crossjumping')
  int main(){
    return 0;
  }
EOS
open("Makefile", "a"){|f|
f.puts <<EOS

vm.o : CFLAGS += -fno-crossjumping

# for GCC
vm.asm:
	$(CC) $(CFLAGS) -fno-crossjumping $(CPPFLAGS) -dA -S -o vm.asm.txt $(srcdir)/vm.c

EOS
  }
end
