Import("env", "env_etc")

# remove -ffast-math compile option
simtbx_gpu_env = env.Clone()
ccflags = simtbx_gpu_env['SHCCFLAGS']
o = '-ffast-math'
if o in ccflags:
  ccflags.remove(o)
simtbx_gpu_env.Replace(SHCCFLAGS=ccflags)

if (env_etc.enable_cuda):
  simtbx_gpu_env.Prepend(CPPDEFINES=["NANOBRAGG_HAVE_CUDA"])
  simtbx_gpu_env.Prepend(CPPFLAGS=["-DCUDAREAL=double"])
  simtbx_gpu_obj = simtbx_gpu_env.SharedObject(
    target="#simtbx/gpu/gpu_ext.o",
    source=[
      "gpu_ext.cpp",
    ]
  )

import os
import libtbx.load_env

Import("env_boost_python_ext")
env_bpl = env_boost_python_ext.Clone()

env_etc.include_registry.append(
    env=env_bpl,
    paths=env_etc.simtbx_common_includes + [env_etc.python_include])

if (env_etc.enable_cuda):
  # For now do all calculations in double precision to agree with CPU tests
  env_bpl.Prepend(NVCCFLAGS=["-DCUDAREAL=double"])
  # Flag header to declare the cuda wrapper
  env_bpl.Prepend(NVCCFLAGS=["-DNANOBRAGG_HAVE_CUDA"])
  env_bpl.Prepend(NVCCFLAGS=["-DHAVE_NANOBRAGG_SPOTS_CUDA"])
  cuda_src = []
  cuda_src.append("structure_factors.cu")
  cuda_src.append("detector.cu")
  cuda_src.append("simulation.cu")
  cuda_src.append("../nanoBragg/nanoBraggCUDA.cu") # duplicating a target *.o file

  env_bpl.Prepend(CPPPATH=[env_etc.boost_adaptbx_include,
    env_etc.boost_include])

  cuda_lib = env_bpl.cudaSharedLibrary(
    target = "#lib/libsimtbx_gpu",
    source = cuda_src)

  env.Prepend(LIBS=["simtbx_gpu"])

  env.SharedLibrary(
    target="#lib/simtbx_gpu_ext",
    source=[
      simtbx_gpu_obj,"#simtbx/nanoBragg/nanoBragg"
    ]
  )
