""" Builds a set of user-defined algorithms.

    The name of the library will be the same as the directory that the
    build is executed from, i.e. the stem returned from the current
    working directory
"""
import os
import platform

# Must be the same version that Mantid uses
MSVC_VERSION=14.0

include_dir = '../include'
install_dir = '../plugins'
link_flags = ['/OPT:REF','/OPT:ICF','/ltcg']
target_arch=None
if platform.architecture()[0] == '64bit':
    target_arch="x86_64"
    link_flags.append('/MACHINE:X64')
else:
    target_arch="x86"

env = Environment(ENV=os.environ,
                  MSVC_VERSION=str(MSVC_VERSION),
                  TARGET_ARCH=target_arch,
                  CPPPATH = include_dir, 
                  PATH = '.', 
                  LIBS = [
                           'MantidKernel',
                           'MantidAPI',
                           'MantidGeometry',
                           'MantidDataObjects',
                           'MantidCurveFitting',
                           'boost_date_time-mt'
                         ], 
                  LINKFLAGS = link_flags,
                  CCFLAGS = ['/nologo','/Ox', '/Ot','/GL', '/W3','/EHsc', '/MD', '/TP','/wd4251', '/wd4275', '/wd4996'],
                  CPPDEFINES = ['NDEBUG', '_SCL_SECURE_NO_WARNINGS', '_CRT_SECURE_NO_WARNINGS', '_CRT_SECURE_NO_DEPRECATE',\
                                '_CRT_NONSTDC_NO_DEPRECATE', '_SCL_SECURE_NO_DEPRECATE', ('MS_VISUAL_STUDIO','1'),\
                                ('WINVER','0x0500'), 'BOOST_ALL_DYN_LINK', 'BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG',\
                                'BOOST_ALL_NO_LIB','NOMINMAX','_USE_MATH_DEFINES', 'WNT','WIN32','_WINDOWS', 'POCO_DLL',\
                                'GSL_DLL']
                  )
# Check whether we should include the OpenMP flag
conf = Configure(env)
if conf.CheckLib('vcomp'):
    env.Append(CCFLAGS=['/openmp'])

# The output library name will be the same as the directory that the build is run from
cwd = os.getcwd()
lib_name = os.path.basename(os.getcwd()) 

# Compile all .cpp files on the current path
sources = Glob('*.cpp')
shared = env.SharedLibrary(lib_name, sources)

# Install to the destination directory
env.Install(install_dir,shared)
env.Alias('install',install_dir)
