import Options

def options(opt):
  opt.add_option('--enable-re2',
                 action='store_true',
                 default=False,
                 help='do use re2 instead of oniguruma')

def configure(conf):
  if Options.options.enable_re2:
    conf.check_cxx(lib = 're2', define_name = 'HAVE_RE2',
                   errmsg = 'not found')
  else:
    conf.check_cxx(lib = 'onig', define_name = 'HAVE_ONIGURUMA')

  libpat = conf.env.cxxshlib_PATTERN
  conf.define('LIBSPLITTER_SAMPLE', libpat % 'splitter_sample')
  conf.define('LIBSPLITTER_NULL', libpat % 'splitter_null')
  conf.define('LIBFILTER_SAMPLE', libpat % 'filter_sample')
  conf.define('LIBNUM_FEATURE_SAMPLE', libpat % 'num_feature_sample')
  conf.define('LIBNUM_FILTER_SAMPLE', libpat % 'num_filter_sample')
  conf.define('LIBBINARY_FEATURE_SAMPLE', libpat % 'binary_feature_sample')

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',
    'splitter_factory.cpp',
    'num_feature_factory.cpp',
    'binary_feature_factory.cpp',
    'converter_config.cpp',
    'libsvm_converter.cpp',
    'string_filter_factory.cpp',
    'num_filter_factory.cpp',
    'dynamic_loader.cpp',
    'dynamic_splitter.cpp',
    'dynamic_num_feature.cpp',
    'dynamic_binary_feature.cpp',
    'dynamic_string_filter.cpp',
    'dynamic_num_filter.cpp',
    'revert.cpp',
    'weight_manager.cpp',
    'mixable_weight_manager.cpp',
    'keyword_weights.cpp',
    'feature_hasher.cpp',
    ]
  use = 'jubatus_util MSGPACK DL jubacommon'

  if not bld.is_defined('JUBATUS_DISABLE_ASSERTIONS'):
    use += ' LIBGLOG'

  if bld.env.HAVE_RE2:
    source.append('re2_match.cpp')
    source.append('re2_filter.cpp')
    source.append('re2_splitter.cpp')
    use += ' RE2'
  else:
    source.append('onig_match.cpp')
    source.append('onig_filter.cpp')
    source.append('onig_splitter.cpp')
    use += ' ONIG'

  bld.shlib(
    source = source,
    target = 'jubaconverter',
    name = 'jubaconverter',
    includes = '.',
    use = 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)

  bld.shlib(
    source = 'test_splitter.cpp',
    target = 'splitter_sample',
    install_path = None,
    name = 'splitter_sample',
    )

  bld.shlib(
    source = 'test_splitter_null.cpp',
    target = 'splitter_null',
    install_path = None,
    name = 'splitter_null',
    )

  bld.shlib(
    source = 'test_num_feature.cpp',
    target = 'num_feature_sample',
    install_path = None,
    name = 'num_feature_sample',
    )

  bld.shlib(
    source = 'test_num_filter.cpp',
    target = 'num_filter_sample',
    install_path = None,
    name = 'num_filter_sample',
    )

  bld.shlib(
    source = 'test_binary_feature.cpp',
    target = 'binary_feature_sample',
    install_path = None,
    name = 'binary_feature_sample',
    )

  bld.shlib(
    source = 'test_string_filter.cpp',
    target = 'filter_sample',
    install_path = None,
    name = 'filter_sample',
    )

  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',
      'splitter_factory_test.cpp',
      'num_feature_factory_test.cpp',
      'converter_config_test.cpp',
      'libsvm_converter_test.cpp',
      'dynamic_splitter_test.cpp',
      'dynamic_num_feature_test.cpp',
      'dynamic_binary_feature_test.cpp',
      'dynamic_string_filter_test.cpp',
      'dynamic_num_filter_test.cpp',
      'string_filter_factory_test.cpp',
      'num_filter_impl_test.cpp',
      'num_filter_factory_test.cpp',
      'dynamic_loader_test.cpp',
      'counter_test.cpp',
      'revert_test.cpp',
      'weight_manager_test.cpp',
      'keyword_weights_test.cpp',
      'feature_hasher_test.cpp',
      'except_match_test.cpp',
      'regexp_match_test.cpp',
      'regexp_filter_test.cpp',
      'regexp_splitter_test.cpp',
      ]
  test_use = 'jubatus_util MSGPACK jubaconverter'

  make_tests(bld, test_use, test_source)

  bld.install_files('${PREFIX}/include/jubatus/core/fv_converter',
                    [ 'word_splitter.hpp',
                      'string_filter.hpp',
                      'num_feature.hpp',
                      'num_filter.hpp',
                      'binary_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',
                    ])

