#!/usr/bin/env python
# (c)quasardb SAS. All rights reserved.
# qdb is a trademark of quasardb SAS

import ez_setup
ez_setup.use_setuptools()

from setuptools import setup, find_packages, Extension

import glob
import os
import sys

qdb_version = "2.0.1".lower();

is_clang = 'Clang' in 'Clang'
is_windows = os.name == 'nt'
is_freebsd = sys.platform.startswith('freebsd')
is_osx = sys.platform == 'darwin'
is_64_bits = sys.maxsize > 2**32
arch = "x64" if is_64_bits else "x86"

if is_windows:
    extra_compile_args = [
        '/DSTATIC_LINKED', # a hack to avoid double export definition because we already set "dllexport" in the source code generated by SWIG except when STATIC_LINKED is set
        '/EHsc',
        '/MT'] # overrides the /MD that is send by distutils, this with generate command line warning D9025
    extra_link_args = ['/INCREMENTAL:NO','/OPT:REF','/RELEASE','/DYNAMICBASE','/NXCOMPAT','/FUNCTIONPADMIN','/MANIFEST']
    if is_64_bits:
        extra_link_args += ['/machine:x64']
    else:
        extra_link_args += ['/machine:x86', '/LARGEADDRESSAWARE']
else:
    if is_clang:
        extra_compile_args = ['-std=c++11','-stdlib=libc++','-fcxx-exceptions','-fexceptions']
        extra_link_args = []
    else:
        extra_compile_args = ['-std=c++11','-Wno-unused-function']
        extra_link_args = ['-static-libgcc','-static-libstdc++']
    if not is_osx:
        extra_link_args += ['-Wl,-z,origin','-Wl,--gc-sections','-Wl,-R$ORIGIN']


if is_freebsd:
    # on FreeBSD everything should be in the system
    qdb_libraries = ['qdb_api', 'c++']
else:
    qdb_libraries = ['qdb_api']

package_modules = glob.glob(os.path.join('qdb', 'lib*'))

package_name = 'qdb-python-api'

setup(name=package_name,
      version=qdb_version,
      description='Python API for quasardb',
      author='quasardb SAS',
      author_email='contact@quasardb.net',
      url='https://www.quasardb.net/',
      setup_requires = [ "setuptools_git >= 0.3", "xmlrunner" ],
      install_requires = [ "xmlrunner" ],
      packages=['qdb'],
      package_data={'qdb': [os.path.basename(mod) for mod in package_modules]},
      include_package_data=True,
      ext_modules=[Extension(
        'qdb._qdb', [os.path.join('src', 'qdb_python_wrapper.cxx')],
        include_dirs=['include'],
        library_dirs=['qdb'],
        libraries=qdb_libraries,
        extra_compile_args=extra_compile_args,
        extra_link_args=extra_link_args)])

