You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

226 lines
6.8 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
6 years ago
3 years ago
6 years ago
  1. # -*- coding: utf-8 -*-
  2. # This file is under GNU Affero General Public License 3.0
  3. # see LICENSE.txt
  4. #
  5. # For more debugging, export DISTUTILS_DEBUG=True
  6. from __future__ import print_function
  7. import sys
  8. import os
  9. from os import environ
  10. from os.path import join
  11. import platform
  12. from setuptools import setup, Extension
  13. from glob import glob
  14. from setuptools.command.build_ext import build_ext
  15. def pEpLog(*msg):
  16. import inspect
  17. msgstr = ''
  18. separator = ' '
  19. for m in msg:
  20. msgstr += str(m)
  21. msgstr += separator
  22. func = inspect.currentframe().f_back.f_code
  23. print(func.co_filename + " : " + func.co_name + " : " + msgstr)
  24. class BuildExtCommand(build_ext):
  25. user_options = build_ext.user_options + [
  26. ('prefix=', None, 'Use pEp-base installation in prefix (libs/includes)'),
  27. ]
  28. def initialize_options(self):
  29. build_ext.initialize_options(self)
  30. self.prefix = getattr(self, "prefix=", None)
  31. def windowsGetInstallLocation(self):
  32. reg_path = "SOFTWARE\\Classes\\TypeLib\\{564A4350-419E-47F1-B0DF-6FCCF0CD0BBC}\\1.0\\0\\win32"
  33. KeyName = None
  34. regKey = None
  35. pEpLog("Registry Lookup:", reg_path, KeyName)
  36. try:
  37. regKey = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, reg_path, 0, winreg.KEY_READ)
  38. # Keys: Description, FileName, FriendlyName, LoadBehavior
  39. com_server, _ = winreg.QueryValueEx(regKey, KeyName)
  40. except WindowsError as error:
  41. pEpLog("Error ocurred: " + error)
  42. com_server = None
  43. finally:
  44. if winreg:
  45. winreg.CloseKey(regKey)
  46. dirname = os.path.dirname
  47. ret = dirname(dirname(com_server))
  48. pEpLog("Value:", ret)
  49. return ret
  50. def windowsGetBoostDirs(self):
  51. for dir in [f.path for f in os.scandir(join(os.getcwd(), 'build-windows', 'packages')) if f.is_dir()]:
  52. if 'boost.' in dir or 'boost_python' in dir or 'boost_locale' in dir:
  53. yield join(dir, 'lib', 'native'), join(dir, 'lib', 'native', 'include')
  54. def get_build_info_win32(self):
  55. home = environ.get('PER_USER_DIRECTORY') or environ.get('USERPROFILE')
  56. inst_prefix = self.windowsGetInstallLocation()
  57. sys_includes = [
  58. join(inst_prefix),
  59. ] + [d[1] for d in self.windowsGetBoostDirs()]
  60. sys_libdirs = [ join(inst_prefix, 'Debug')] if self.debug else [ join(inst_prefix, 'Release')]
  61. sys_libdirs += [d[0] for d in self.windowsGetBoostDirs()]
  62. libs = [
  63. 'pEpEngine',
  64. 'libpEpAdapter',
  65. 'boost_python38-vc142-mt-x32-1_72',
  66. 'boost_locale-vc142-mt-x32-1_72'
  67. ]
  68. compile_flags = ['/std:c++14', '/permissive']
  69. if self.debug:
  70. pEpLog("debug mode")
  71. compile_flags += ['/Od', '/Zi', '/DEBUG']
  72. return (home, sys_includes, sys_libdirs, libs, compile_flags)
  73. def get_build_info_darwin(self):
  74. home = environ.get('PER_USER_DIRECTORY') or environ.get('HOME')
  75. sys_includes = [
  76. '/opt/local/include',
  77. ]
  78. sys_libdirs = [
  79. '/opt/local/lib',
  80. ]
  81. libs = [
  82. 'pEpEngine',
  83. 'pEpAdapter',
  84. 'boost_python38-mt',
  85. 'boost_locale-mt'
  86. ]
  87. compile_flags = ['-std=c++14', '-fpermissive']
  88. if self.debug:
  89. pEpLog("debug mode")
  90. compile_flags += ['-O0', '-g', '-UNDEBUG']
  91. return (home, sys_includes, sys_libdirs, libs, compile_flags)
  92. def get_build_info_linux(self):
  93. home = environ.get('PER_USER_DIRECTORY') or environ.get('HOME')
  94. sys_includes = [
  95. '/usr/local/include',
  96. '/usr/include',
  97. ]
  98. sys_libdirs = [
  99. '/usr/local/lib',
  100. '/usr/lib',
  101. '/usr/lib/{}-linux-gnu'.format(platform.machine())
  102. ]
  103. libs = [
  104. 'pEpEngine',
  105. 'pEpAdapter',
  106. 'boost_python3',
  107. 'boost_locale'
  108. ]
  109. compile_flags = ['-std=c++14', '-fpermissive']
  110. if self.debug:
  111. pEpLog("debug mode")
  112. compile_flags += ['-O0', '-g', '-UNDEBUG']
  113. return (home, sys_includes, sys_libdirs, libs, compile_flags)
  114. def finalize_options(self):
  115. build_ext.finalize_options(self)
  116. pEpLog("prefix: ", self.prefix)
  117. pEpLog("sys.platform: ", sys.platform)
  118. # get build information for platform
  119. if sys.platform == 'win32':
  120. build_info = self.get_build_info_win32()
  121. elif sys.platform == 'darwin':
  122. build_info = self.get_build_info_darwin()
  123. elif sys.platform == 'linux':
  124. build_info = self.get_build_info_linux()
  125. else:
  126. pEpLog("Platform not supported:" + sys.platform)
  127. exit()
  128. (home, sys_includes, sys_libdirs, libs, compile_flags) = build_info
  129. # Build the Includes -I and Library paths -L
  130. # Start empty
  131. includes = []
  132. libdirs = []
  133. # Append prefix-dir
  134. if self.prefix:
  135. prefix_include = [join(self.prefix, 'include')]
  136. prefix_libdirs = [join(self.prefix, 'lib')]
  137. includes += prefix_include
  138. libdirs += prefix_libdirs
  139. # Append default system dirs
  140. includes += sys_includes
  141. libdirs += sys_libdirs
  142. # Apply the build information
  143. global module_pEp
  144. module_pEp.include_dirs = includes
  145. module_pEp.library_dirs = libdirs
  146. module_pEp.libraries = libs
  147. module_pEp.extra_compile_args = compile_flags
  148. pEpLog("Include Dirs:", module_pEp.include_dirs)
  149. pEpLog("Libs Dirs:", module_pEp.library_dirs)
  150. pEpLog("Libraries:", module_pEp.libraries)
  151. pEpLog("Compile flags:", module_pEp.extra_compile_args)
  152. def run(self):
  153. build_ext.run(self)
  154. if sys.platform == 'win32':
  155. if sys.version_info[0] >= 3:
  156. import winreg
  157. else:
  158. import _winreg as winreg
  159. if sys.version_info[0] < 3:
  160. FileNotFoundError = EnvironmentError
  161. module_pEp = Extension(
  162. 'pEp._pEp',
  163. sources=[
  164. 'src/pEp/_pEp/pEpmodule.cc',
  165. 'src/pEp/_pEp/basic_api.cc',
  166. 'src/pEp/_pEp/identity.cc',
  167. 'src/pEp/_pEp/message.cc',
  168. 'src/pEp/_pEp/message_api.cc',
  169. 'src/pEp/_pEp/str_attr.cc',
  170. # 'src/pEp/_pEp/user_interface.cc',
  171. ],
  172. )
  173. # "MAIN" Function
  174. setup(
  175. package_dir={'': 'src'},
  176. packages=['pEp'],
  177. ext_modules=[module_pEp],
  178. cmdclass={
  179. 'build_ext': BuildExtCommand,
  180. },
  181. # While not using a pyproject.toml, support setuptools_scm setup.cfg usage,
  182. # see https://github.com/pypa/setuptools_scm/#setupcfg-usage
  183. use_scm_version={
  184. 'write_to': 'src/pEp/__version__.py',
  185. #TODO: fallback_version does not seem to work in case os missing tag
  186. 'fallback_version' : '0.0.0-RC0'
  187. }
  188. )