import Options
import platform

PLATFORM_IS_DARWIN = platform.platform().find('Darwin') == 0
PLATFORM_IS_WIN32 = platform.platform().find('Win') >= 0

def set_options(opt):
  pass
  #opt.tool_options('compiler_cc')

def configure(conf):
  print "--- libev ---"
  #conf.check_tool('compiler_cc')

  # Why to the two checks? One is to define HAVE_SYS_EPOLL_H
  # the other is to define HAVE_EPOLL_CTL
  # Yes, WAF is a piece of shit.

  if conf.check_cc(header_name="sys/inotify.h"):
    conf.check_cc(header_name="sys/inotify.h", function_name="inotify_init")

  if conf.check_cc(header_name="sys/epoll.h"):
    conf.check_cc(header_name="sys/epoll.h", function_name="epoll_ctl")

  if conf.check_cc(header_name="port.h"):
    conf.check_cc(header_name="port.h", function_name="port_create")

  if conf.check_cc(header_name="poll.h"):
    conf.check_cc(header_name="poll.h", function_name="poll")

  kqueue_headers = []
  # On FreeBSD event.h is not selfcontained and requires types.h
  event_headers = ["sys/types.h", "sys/event.h"]
  if conf.check_cc(header_name=event_headers, define_name="HAVE_SYS_EVENT_H"):
    kqueue_headers += event_headers

  if conf.check_cc(header_name="sys/queue.h"):
    kqueue_headers.append("sys/queue.h")

  if kqueue_headers:
    conf.check_cc(header_name=kqueue_headers, function_name="kqueue")

  if PLATFORM_IS_WIN32:
    # Windows has sys/select.h and select but this config line doesn't detect it properly
    conf.define('HAVE_SYS_SELECT_H', 1);
    conf.define('HAVE_SELECT', 1);
  else:
    if conf.check_cc(header_name="sys/select.h"):
      conf.check_cc(header_name="sys/select.h", function_name="select")

  if conf.check_cc(header_name="sys/eventfd.h"):
    conf.check_cc(header_name="sys/eventfd.h", function_name="eventfd")


  code = """
      #include <syscall.h>
      #include <time.h>
      #include <stdio.h>

      int main() {
          struct timespec ts; 
          int status = syscall(SYS_clock_gettime, CLOCK_REALTIME, &ts);
          return 0;
      }
  """
  conf.check_cc(fragment=code, define_name="HAVE_CLOCK_SYSCALL", execute=True,
                msg="Checking for SYS_clock_gettime")

  have_librt = conf.check(lib='rt', uselib_store='RT')
  if have_librt:
    conf.check_cc(lib="rt", header_name="time.h", function_name="clock_gettime")
  if PLATFORM_IS_DARWIN:
    conf.check_cc(header_name="time.h", function_name="nanosleep")
  elif have_librt:
    conf.check_cc(lib="rt", header_name="time.h", function_name="nanosleep")

  conf.check_cc(lib="m", header_name="math.h", function_name="ceil")

  conf.define("HAVE_CONFIG_H", 1)
  # Not using these.
  conf.env.append_value('CPPFLAGS', ['-DEV_FORK_ENABLE=0',
                                     '-DEV_EMBED_ENABLE=0',
                                     '-DEV_MULTIPLICITY=0'])

def build(bld):
  libev = bld.new_task_gen("cc")
  libev.source = 'ev.c'
  libev.target = 'ev'
  libev.name = 'ev'
  libev.includes = '. ../..'
  libev.uselib = "RT"
  libev.install_path = None
  if bld.env["USE_DEBUG"]:
    libev.clone("debug");
  bld.install_files('${PREFIX}/include/node/', 'ev.h');

