#!/usr/bin/env python

import os

Import('enviscelib')
package = enviscelib['PACKAGE'] # 'library'
project = enviscelib['PROJECT'] # 'isceLib'
objFiles = enviscelib['ISCELIB_OBJ_LIST'] # Comes from src/SConscript building

install_main = os.path.join(enviscelib['PRJ_SCONS_INSTALL'], package, project)
install_src = os.path.join(install_main, 'src') # location of the built object files
install_pyx = os.path.join(install_main, 'pyx') # location of the Cythonizing outputs

pyx_files=['Ellipsoid.pyx',
           'LinAlg.pyx',
           'Orbit.pyx',
           'Peg.pyx',
           'Pegtrans.pyx',
           'Poly1d.pyx',
           'Poly2d.pyx',
           'Position.pyx']

a = enviscelib.Command(os.path.join(install_pyx,'isceLib.cpp'), 'isceLib.pyx', 'cython3 $SOURCE -o $TARGET --cplus') # Cythonize the isceLib.pyx file to the install dir
b = enviscelib.SharedObject(target=os.path.join(install_pyx,'isceLib.o'), source=os.path.join(install_pyx,'isceLib.cpp')) # Build the Cythonized isceLib.pyx

objs_with_paths = []
for obj in objFiles:
    objs_with_paths.append(os.path.join(install_src,obj)) # Add paths to list of object files
objs_with_paths.append(os.path.join(install_pyx,'isceLib.o')) # Add newly-Cythonized isceLib.pyx object

# Build Python module from shared objects
c = enviscelib.LoadableModule(target=os.path.join(install_main,'isceLib.abi3.so'), source=objs_with_paths)

# Use Depends() command to make sure that changing the .pyx files rebuilds the Python module
Depends(a, pyx_files) # Re-Cythonize isceLib.pyx
Depends(b, pyx_files) # Rebuild isceLib.o
Depends(c, pyx_files) # Rebuild isceLib Python module
