#!/usr/bin/python3

import os,sys,subprocess,math
from complib2 import *

import shutil
#from distutils.dir_util import copy_tree as copy_tree #this version does overwrites
from shutil import copytree as copytree

libname = 'amsculib2.linux64' #prefix static library name to generate
targetname = 'test' #create this executable when compiling tests
commonincdir = "../../linux64/include"
commonlibdir = "../../linux64/lib"
localbindir = "./bin_linux64"
cc = 'nvcc' #compiler
srcexts = ['.c','.cpp','.cu']
mainsrc = ['main.c','main.cpp','main.cu'] #ignore these files when compiling the static library

kwargs = dict()
include = "-I./include -I{}".format(commonincdir)
kwargs['include'] = include
#-dc flag: relocatable device code - needed for device functions to link in different "execution units"
kwargs['flags'] = "-dc"
kwargs['libdir'] = "-L{} -L{}".format(localbindir,commonlibdir)
kwargs['libflags'] = "-l{}".format(libname)
kwargs['linkerflags'] = ""
kwargs['recurse'] = True

#find all source files, except the main project files
files = flist('./src',exts = srcexts, recurse=True)
files = except_contains(files,mainsrc)
objfiles = replaceexts(files,'.o')
objfiles_sss = list_to_sss(objfiles)

#compile all the source files in the list
gs_compile_list(cc,files,**kwargs)

#archive all the source files into a static library
ar_list(objfiles,'{}/lib{}.a'.format(localbindir,libname))

#Push any libraries to the common lib folder
shutil.copy('{}/lib{}.a'.format(localbindir,libname),commonlibdir)

#Copy include files to the common include folder
copytree('./include/',commonincdir+'/',dirs_exist_ok=True)
