#!/usr/bin/env python
import sys
import subprocess

Import('envPyCuAmpcor')
package = envPyCuAmpcor['PACKAGE']
project = envPyCuAmpcor['PROJECT']
build = envPyCuAmpcor['PRJ_LIB_DIR']
install = envPyCuAmpcor['PRJ_SCONS_INSTALL'] + '/' + package + '/' + project
listFiles = ['GDALImage.cpp', 'cuArrays.cpp', 'cuArraysCopy.cu',
             'cudaError.cpp', 'cudaUtil.cpp',
             'cuArraysPadding.cu', 'cuOverSampler.cu',
             'cuSincOverSampler.cpp', 'cuDeramp.cu',
             'cuOffset.cu', 'cuCorrNormalization.cu',
             'cuCorrNormalizationSAT.cu', 'cuCorrNormalizer.cpp',
             'cuAmpcorParameter.cpp', 'cuCorrTimeDomain.cu',
             'cuAmpcorController.cpp', 'cuCorrFrequency.cu',
             'cuAmpcorChunk.cpp', 'cuEstimateStats.cu']

lib = envPyCuAmpcor.SharedLibrary(target = 'PyCuAmpcor', source= listFiles, SHLIBPREFIX='')

# add gdal include path
gdal_cflags = subprocess.check_output('gdal-config --cflags', shell=True)[:-1].decode('utf-8')
envPyCuAmpcor.Append(ENABLESHAREDNVCCFLAG = ' -DNDEBUG ' + gdal_cflags)

envPyCuAmpcor.Install(build,lib)
envPyCuAmpcor.Alias('install', build)

def pybind11PseudoBuilder(env, src, bld, inst):
    listFiles = [ src ]
    env.MergeFlags('-fopenmp -O3 -std=c++11 -fPIC -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -Wall -Wstrict-prototypes')
    libList = ['gdal']
    env.PrependUnique(LIBS=libList)
    lib = env.LoadableModule(target='PyCuAmpcor.abi3.so', source=listFiles, CPPDEFINES='GPU_ACC_ENABLED')

    env.Install(inst, lib)
    env.Alias('install', inst)
    env.Install(bld, lib)
    env.Alias('build', bld)


envPyCuAmpcor.AddMethod(pybind11PseudoBuilder, 'Pybind11')
envPyCuAmpcor.Pybind11(['PyCuAmpcor.cpp'] + listFiles, build, install)
