import libtbx.load_env
Import("env_base","env_etc")

try:
  env_etc.annlib_dist = libtbx.env.dist_path("annlib")
except KeyError:
  from libtbx.env_config import get_local_env
  local_env = get_local_env(build_dir=GetLaunchDir())
  if local_env is not None:
    env_etc.annlib_dist = local_env.find_in_repositories("annlib")
  else:
    raise
env_etc.annlib_include = [env_etc.norm_join(env_etc.annlib_dist,"src"),
                          env_etc.norm_join(env_etc.annlib_dist,"include")]
env_etc.annlib_adaptbx_dist = libtbx.env.dist_path("annlib_adaptbx")
env_etc.annlib_adaptbx_include = [
  libtbx.env.under_dist("annlib_adaptbx", "include"),
  libtbx.env.under_build("annlib_adaptbx/include")]

env_etc.annlib_common_includes = [
  env_etc.libtbx_include,
  env_etc.scitbx_include,
  env_etc.boost_adaptbx_include,
  env_etc.boost_include,
  env_etc.annlib_dist,    # for the adaptor cpp files
  env_etc.annlib_include[0],
  env_etc.annlib_include[1],
  env_etc.annlib_adaptbx_include[0],
  env_etc.annlib_adaptbx_include[1],
]

if not libtbx.env.module_is_installed("annlib_adaptbx"):
  env = env_base.Clone(
    SHLINKFLAGS=env_etc.shlinkflags)
  env.Append(CPPPATH=env_etc.annlib_common_includes)
  if (libtbx.manual_date_stamp < 20090819):
    # XXX backward compatibility 2009-08-19
    env.Replace(CCFLAGS=env_etc.ccflags_base)
    env.Replace(CXXFLAGS=env_etc.cxxflags_base)
    env.Replace(SHCXXFLAGS=env_etc.cxxflags_base)

  if (env_etc.static_libraries): builder = env.StaticLibrary
  else:                          builder = env.SharedLibrary
  builder(target='#lib/ann',
    source = ["../annlib/src/ANN.cpp",
              "../annlib/src/bd_fix_rad_search.cpp",
              "../annlib/src/bd_pr_search.cpp",
              "../annlib/src/bd_search.cpp",
              "../annlib/src/bd_tree.cpp",
              "../annlib/src/brute.cpp",
              "../annlib/src/kd_dump.cpp",
              "../annlib/src/kd_fix_rad_search.cpp",
              "../annlib/src/kd_pr_search.cpp",
              "../annlib/src/kd_search.cpp",
              "../annlib/src/kd_split.cpp",
              "../annlib/src/kd_tree.cpp",
              "../annlib/src/kd_util.cpp",
              "../annlib/src/perf.cpp",
              "self_include/ANN.cpp",
              "self_include/bd_fix_rad_search.cpp",
              "self_include/bd_pr_search.cpp",
              "self_include/bd_search.cpp",
              "self_include/bd_tree.cpp",
              "self_include/brute.cpp",
              "self_include/kd_dump.cpp",
              "self_include/kd_fix_rad_search.cpp",
              "self_include/kd_pr_search.cpp",
              "self_include/kd_search.cpp",
              "self_include/kd_split.cpp",
              "self_include/kd_tree.cpp",
              "self_include/kd_util.cpp",
              "self_include/perf.cpp",
              "ann/ann_adaptor.cpp"
            ])

  if (not env_etc.no_boost_python):
    Import("env_no_includes_boost_python_ext")

    env_annlib_boost_python_ext = env_no_includes_boost_python_ext.Clone()

    env_etc.include_registry.append(
      env=env_annlib_boost_python_ext,
      paths=env_etc.annlib_common_includes + [env_etc.python_include])

    Export("env_annlib_boost_python_ext")

    SConscript("ann/boost_python/SConscript")
