# -*- python -*-
import Options

def options(opt):
  opt.add_option('--regexp-library',
                 default='oniguruma',
                 help='use regexp library: oniguruma (default), re2 or none')

def configure(conf):
  regexp_lib = Options.options.regexp_library
  if regexp_lib == 're2':
    conf.check_cxx(lib = 're2', define_name = 'HAVE_RE2',
                   errmsg = 'not found')
  elif regexp_lib == 'oniguruma':
    conf.check_cxx(lib = 'onig', define_name = 'HAVE_ONIGURUMA',
                   errmsg = 'not found')
  elif regexp_lib == 'none':
    pass
  else:
    conf.fatal('unsupported regexp library specified: ' + regexp_lib)

def make_test(bld, use, src):
  bld.program(
    features = 'gtest',
    source = src,
    target = src[0:src.rindex('.')],
    use = use,
    cxxflags = ['-DGTEST_HAS_POSIX_RE=0']
    )

def make_tests(bld, use, srcs):
  for src in srcs:
    make_test(bld, use, src)


def build(bld):
  source = [
    'util.cpp',
    'json_converter.cpp',
    'msgpack_converter.cpp',
    'datum_to_fv_converter.cpp',
    'space_splitter.cpp',
    'character_ngram.cpp',
    'without_split.cpp',
    'key_matcher_factory.cpp',
    'string_feature_factory.cpp',
    'num_feature_factory.cpp',
    'binary_feature_factory.cpp',
    'combination_feature_factory.cpp',
    'converter_config.cpp',
    'libsvm_converter.cpp',
    'string_filter_factory.cpp',
    'num_filter_factory.cpp',
    'revert.cpp',
    'weight_manager.cpp',
    'keyword_weights.cpp',
    'feature_hasher.cpp',
    'word_splitter.cpp',
    ]
  headers = [
      'string_feature.hpp',
      'word_splitter.hpp',
      'string_feature_factory.hpp',
      'string_filter.hpp',
      'string_filter_factory.hpp',
      'num_feature.hpp',
      'num_feature_factory.hpp',
      'num_filter.hpp',
      'num_filter_factory.hpp',
      'binary_feature.hpp',
      'combination_feature.hpp',
      'datum_to_fv_converter.hpp',
      'datum.hpp',
      'converter_config.hpp',
      'json_converter.hpp',
      'weight_manager.hpp',
      'mixable_weight_manager.hpp',
      'keyword_weights.hpp',
      'counter.hpp',
      'revert.hpp',
      'exception.hpp',
      'type.hpp',
      'factory.hpp',
      'util.hpp',
  ]
  test_source = [
      'json_converter_test.cpp',
      'msgpack_converter_test.cpp',
      'datum_to_fv_converter_test.cpp',
      'space_splitter_test.cpp',
      'character_ngram_test.cpp',
      'key_matcher_test.cpp',
      'key_matcher_factory_test.cpp',
      'string_feature_factory_test.cpp',
      'num_feature_factory_test.cpp',
      'combination_feature_factory_test.cpp',
      'converter_config_test.cpp',
      'libsvm_converter_test.cpp',
      'string_filter_factory_test.cpp',
      'num_filter_impl_test.cpp',
      'num_filter_factory_test.cpp',
      'counter_test.cpp',
      'revert_test.cpp',
      'weight_manager_test.cpp',
      'keyword_weights_test.cpp',
      'feature_hasher_test.cpp',
      'except_match_test.cpp',
      'mixable_weight_manager_test.cpp',
  ]
  regex_test_source = [
      'regexp_match_test.cpp',
      'regexp_filter_test.cpp',
      'regexp_splitter_test.cpp',
  ]

  use = ['jubatus_util', 'MSGPACK']

  if bld.env.HAVE_RE2:
    source += ['re2_match.cpp', 're2_filter.cpp', 're2_splitter.cpp' ]
    test_source += regex_test_source
    use += ['RE2']
  elif bld.env.HAVE_ONIGURUMA:
    source += ['onig_match.cpp', 'onig_filter.cpp', 'onig_splitter.cpp']
    test_source += regex_test_source
    use += ['ONIG']

  bld.core_sources.extend(bld.add_prefix(source))
  bld.core_headers.extend(bld.add_prefix(headers))
  bld.core_use.extend(use)

  n = bld.path.get_bld().make_node('test_input')
  n.mkdir()
  bld(rule = 'cp ${SRC} ${TGT}',
      source = bld.path.ant_glob('test_input/*'),
      target = n)

  test_use = ['jubatus_util', 'jubatus_core']

  make_tests(bld, test_use, test_source)
