from __future__ import print_function
import libtbx.load_env
Import("env_base", "env_etc")
import sys

if not libtbx.env.build_options.enable_boost_threads:
  print(
    "Module smtbx requires Boost.Threads.\n" +
    "Please rerun libtbx/configure.py with --enable_boost_threads.\n" +
    "** Aborting build **", file=sys.stderr)
  Exit(1)

if not env_etc.boost_thread_support:
  print(
    "Module smtbx requires Boost.Threads which is not supported on this " +
    "Platform.\n" +
    "** Aborting build **", file=sys.stderr)
  Exit(1)

if libtbx.env.boost_version < 105600:
  v = libtbx.env.boost_version
  d1 = v/100000
  v %= 100000
  d2 = v/100
  print(' '.join((
    "Module smtbx requires Boost version 1.56: ",
    "you have only version %i.%i. " % (d1,d2),
    "This cctbx build uses Boost installed at %s: "
    "please put a supported version there." %
    abs(libtbx.env.find_dist_path('boost')),
  )), file=sys.stderr)
  print("** Aborting build **", file=sys.stderr)
  Exit(1)


env_etc.smtbx_dist = libtbx.env.dist_path("smtbx")
env_etc.smtbx_common_includes = [
  env_etc.libtbx_include,
  env_etc.cctbx_include,
  env_etc.scitbx_include,
  env_etc.chiltbx_include,
  env_etc.boost_include,
]

if not libtbx.env.module_is_installed("smtbx"):
  env = env_base.Clone(
    SHLINKFLAGS=env_etc.shlinkflags)
  env_etc.enable_fast_linalg(env)

  envlm = env.Clone(
    LIBS=env_etc.libm
  )
  if (env_etc.static_libraries): builder = envlm.StaticLibrary
  else:                          builder = envlm.SharedLibrary

  SConscript("refinement/constraints/SConscript")

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

    env_smtbx_boost_python_ext = env_no_includes_boost_python_ext.Clone()
    env_etc.include_registry.append(
      env=env_smtbx_boost_python_ext,
      paths=env_etc.smtbx_common_includes + [env_etc.python_include])
    env_etc.enable_fast_linalg(env_smtbx_boost_python_ext)
    env_smtbx_boost_python_ext.Prepend(LIBS=["cctbx"])
    Export("env_smtbx_boost_python_ext")

    SConscript("stl/SConscript")
    SConscript("array_family/SConscript")
    SConscript("refinement/boost_python/SConscript")
    SConscript("refinement/constraints/boost_python/SConscript")
    SConscript("refinement/restraints/boost_python/SConscript")
    SConscript("ab_initio/SConscript")
    SConscript("structure_factors/direct/boost_python/SConscript")
