setup.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. from __future__ import print_function
  2. import os
  3. import sys
  4. import platform
  5. import numpy
  6. from setuptools import find_packages
  7. from setuptools import setup, Extension
  8. from Cython.Build import build_ext
  9. def get_extras():
  10. cur = os.path.dirname(os.path.abspath(__file__))
  11. arch = '64' if '64bit' in platform.architecture() else ''
  12. # Find all the extra include files, libraries, and link arguments we need to install.
  13. x_includes = [os.path.join(cur, 'dist', 'include')] # Must include cbsdk headers
  14. x_libs = []
  15. x_link_args = []
  16. if sys.platform == "darwin":
  17. # Find Qt framework
  18. qtfwdir = '/usr/local/opt' # Default search dir
  19. import subprocess
  20. p = subprocess.Popen('brew ls --versions qt5', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  21. hasqt5 = len(p.stdout.read()) > 0
  22. if hasqt5:
  23. p = subprocess.Popen('brew --prefix qt5', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  24. qtfwdir = str(p.stdout.read().decode("utf-8")[:-1]) + "/Frameworks"
  25. else:
  26. p = subprocess.Popen('brew ls --versions qt', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  27. hasqt4 = len(p.stdout.read()) > 0
  28. if hasqt4:
  29. p = subprocess.Popen('brew --prefix qt', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  30. qtfwdir = str(p.stdout.read().decode("utf-8")[:-1]) + "/Frameworks"
  31. x_link_args += ['-F', qtfwdir,
  32. '-framework', 'QtCore',
  33. '-framework', 'QtXml']
  34. if hasqt5:
  35. x_link_args += ['-framework', 'QtConcurrent']
  36. elif "win32" in sys.platform:
  37. # Must include stdint (V2008 does not have it!)
  38. x_includes += [os.path.join(cur, 'compat')]
  39. # Must be explicit about cbsdk link path
  40. x_link_args += ['/LIBPATH:{path}'.format(path=os.path.join(cur, 'dist', 'lib{arch}'.format(arch=arch)))]
  41. # add winsock, timer, and system libraries
  42. x_libs += ["ws2_32", "winmm"]
  43. x_libs += ["kernel32", "user32", "gdi32", "winspool", "shell32",
  44. "ole32", "oleaut32", "uuid", "comdlg32", "advapi32"]
  45. # Find Qt library
  46. def _get_qt_path():
  47. # Windows does not have a canonical install place, so try some known locations
  48. path = ''
  49. try:
  50. import _winreg
  51. except ImportError:
  52. import winreg as _winreg
  53. try:
  54. path = os.environ['QTDIR'] # e.g. `set QTDIR=C:\Qt\Qt5.6.0\5.6\msvc2015_64`
  55. except:
  56. pass
  57. if not path:
  58. try:
  59. hk = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, r'SOFTWARE\Trolltech\Versions', 0)
  60. ver = _winreg.EnumKey(hk, 0)
  61. _winreg.CloseKey(hk)
  62. print('Using installed Qt{ver}'.format(ver=ver))
  63. hk = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,
  64. r'SOFTWARE\Trolltech\Versions\{ver}'.format(ver=ver),
  65. 0)
  66. ii = 0
  67. while True:
  68. try:
  69. install = _winreg.EnumValue(hk, ii)
  70. if install[0].lower() == 'installdir':
  71. path = install[1]
  72. break
  73. except:
  74. break
  75. _winreg.CloseKey(hk)
  76. if not path:
  77. raise ValueError("InstallDir not found")
  78. except:
  79. raise RuntimeError("Cannot find Qt in registry nor QTDIR is set")
  80. # Parse qt_path to see if it is qt5
  81. _is_qt5 = False
  82. tmp = (path, '')
  83. while tmp[0] is not '':
  84. tmp = os.path.split(tmp[0])
  85. mysplit = tmp[1].split('.')
  86. if len(mysplit) > 1:
  87. if mysplit[0][-1] == '5':
  88. _is_qt5 = True
  89. tmp = ('', '')
  90. return path, _is_qt5
  91. qt_path, is_qt5 = _get_qt_path()
  92. x_link_args += ['/LIBPATH:{path}'.format(path=os.path.join(qt_path, 'lib'))]
  93. x_includes += [os.path.join(qt_path, 'include')]
  94. if is_qt5:
  95. x_libs += ["Qt5Core", "Qt5Xml", "Qt5Concurrent"]
  96. else:
  97. x_libs += ["QtCore4", "QtXml4"]
  98. else: # Linux
  99. x_link_args += ['-L{path}'.format(path=os.path.join(cur, 'dist', 'lib{arch}'.format(arch=arch)))]
  100. # For Qt linking at run time, check `qtchooser -print-env`
  101. # If it is not pointing to correct QtDir, then edit /usr/lib/x86_64-linux-gnu/qt-default/qtchooser/default.conf
  102. # /opt/Qt/5.9/gcc_64/bin
  103. # /opt/Qt/5.9/gcc_64/lib
  104. # This should also take care of finding the link dir at compile time, (i.e. -L/path/to/qt not necessary)
  105. # but we need to specify lib names, and these are version dependent.
  106. import subprocess
  107. import re
  108. p = subprocess.Popen('qmake -v', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  109. qt_ver = re.findall('Qt version .', p.stdout.read().decode())[0][-1]
  110. # TODO: qmake -v might give different version string text for Qt4 so we will need a smarter parser.
  111. if qt_ver == '5':
  112. x_libs += ["Qt5Core", "Qt5Xml", "Qt5Concurrent"]
  113. elif qt_ver == '4':
  114. x_libs += ["QtCore", "QtXml"]
  115. return x_includes, x_libs, x_link_args
  116. extra_includes, extra_libs, extra_link_args = get_extras()
  117. extension_kwargs = {
  118. 'sources': ['cerebus/cbpy.pyx', 'cerebus/cbsdk_helper.cpp'],
  119. 'libraries': ["cbsdk_static"] + extra_libs,
  120. 'extra_link_args': extra_link_args,
  121. 'include_dirs': [numpy.get_include()] + extra_includes,
  122. 'language': 'c++'
  123. }
  124. cbpy_module = Extension('cerebus.cbpy', **extension_kwargs)
  125. setup(
  126. name='cerebus',
  127. version='0.0.3',
  128. description='Cerebus Link',
  129. long_description='Cerebus Link Python Package',
  130. author='dashesy',
  131. author_email='dashesy@gmail.com',
  132. install_requires=['Cython>=0.19.1', 'numpy'],
  133. url='https://github.com/dashesy/CereLink',
  134. packages=find_packages(),
  135. cmdclass={
  136. 'build_ext': build_ext,
  137. },
  138. ext_modules=[
  139. cbpy_module,
  140. ],
  141. )