import libtbx.load_env
import os
op = os.path

Import("env_scitbx_boost_python_ext", "env_etc")

source = [
  "linalg_ext.cpp",
  "matrix.cpp",
  "cholesky.cpp",
  "householder.cpp",
  "svd.cpp",
  "eigensystem.cpp",
  ]

fem_source = [ "lapack_fem_bpl.cpp" ]

shared_obj = []

env = env_scitbx_boost_python_ext.Clone()
env_fem = env.Clone()
if env_etc.gcc_version is not None and env_etc.gcc_version >= 30400:
  env_etc.enable_more_warnings(env=env_fem)
if env_etc.clang_version:
  env_fem.Append(SHCXXFLAGS=['-Wno-logical-op-parentheses'])

for src in source: shared_obj.extend(env.SharedObject(src))

if (libtbx.env.has_module("fable")):
  lapack_fem_dir = libtbx.env.find_in_repositories(relative_path="lapack_fem")
  if (lapack_fem_dir is not None):
    env_etc.include_registry.append(
      env=env_fem,
      paths=[
        op.dirname(lapack_fem_dir),
        env_etc.fable_include])
    env_fem.Append(SHCXXFLAGS=["-DSCITBX_LAPACK_FEM"])

lapack_for_a = libtbx.env.under_build(path="lib/lapack_for.a")
if (op.isfile(lapack_for_a)):
  env_fem.Append(SHCXXFLAGS=["-DSCITBX_LAPACK_FOR"])
  env.Prepend(LIBS=[File(lapack_for_a)])

for src in fem_source:
  shared_obj.extend(env_fem.SharedObject(source=src))

env_etc.enable_fast_linalg(env)

# TODO env_etc.enable_more_warnings(env=env)
env.SharedLibrary(target="#lib/scitbx_linalg_ext", source=shared_obj)
