setupinfo.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. import sys, os, os.path
  2. from distutils.core import Extension
  3. from distutils.errors import DistutilsOptionError
  4. from versioninfo import get_base_dir, split_version
  5. try:
  6. from Cython.Distutils import build_ext as build_pyx
  7. import Cython.Compiler.Version
  8. CYTHON_INSTALLED = True
  9. except ImportError:
  10. CYTHON_INSTALLED = False
  11. EXT_MODULES = ["lxml.etree", "lxml.objectify"]
  12. PACKAGE_PATH = "src%slxml%s" % (os.path.sep, os.path.sep)
  13. INCLUDE_PACKAGE_PATH = PACKAGE_PATH + 'includes'
  14. if sys.version_info[0] >= 3:
  15. _system_encoding = sys.getdefaultencoding()
  16. if _system_encoding is None:
  17. _system_encoding = "iso-8859-1" # :-)
  18. def decode_input(data):
  19. if isinstance(data, str):
  20. return data
  21. return data.decode(_system_encoding)
  22. else:
  23. def decode_input(data):
  24. return data
  25. def env_var(name):
  26. value = os.getenv(name)
  27. if value:
  28. value = decode_input(value)
  29. if sys.platform == 'win32' and ';' in value:
  30. return value.split(';')
  31. else:
  32. return value.split()
  33. else:
  34. return []
  35. def ext_modules(static_include_dirs, static_library_dirs,
  36. static_cflags, static_binaries):
  37. global XML2_CONFIG, XSLT_CONFIG
  38. if OPTION_BUILD_LIBXML2XSLT:
  39. from buildlibxml import build_libxml2xslt, get_prebuilt_libxml2xslt
  40. if sys.platform.startswith('win'):
  41. get_prebuilt_libxml2xslt(
  42. OPTION_DOWNLOAD_DIR, static_include_dirs, static_library_dirs)
  43. else:
  44. XML2_CONFIG, XSLT_CONFIG = build_libxml2xslt(
  45. OPTION_DOWNLOAD_DIR, 'build/tmp',
  46. static_include_dirs, static_library_dirs,
  47. static_cflags, static_binaries,
  48. libiconv_version=OPTION_LIBICONV_VERSION,
  49. libxml2_version=OPTION_LIBXML2_VERSION,
  50. libxslt_version=OPTION_LIBXSLT_VERSION,
  51. multicore=OPTION_MULTICORE)
  52. if OPTION_WITHOUT_OBJECTIFY:
  53. modules = [ entry for entry in EXT_MODULES
  54. if 'objectify' not in entry ]
  55. else:
  56. modules = EXT_MODULES
  57. c_files_exist = [ os.path.exists('%s%s.c' % (PACKAGE_PATH, module)) for module in modules ]
  58. if CYTHON_INSTALLED and (OPTION_WITH_CYTHON or False in c_files_exist):
  59. source_extension = ".pyx"
  60. print("Building with Cython %s." % Cython.Compiler.Version.version)
  61. # generate module cleanup code
  62. from Cython.Compiler import Options
  63. Options.generate_cleanup_code = 3
  64. Options.clear_to_none = False
  65. elif not OPTION_WITHOUT_CYTHON and False in c_files_exist:
  66. for exists, module in zip(c_files_exist, modules):
  67. if not exists:
  68. raise RuntimeError(
  69. "ERROR: Trying to build without Cython, but pre-generated '%s%s.c' "
  70. "is not available (pass --without-cython to ignore this error)." % (
  71. PACKAGE_PATH, module))
  72. else:
  73. if False in c_files_exist:
  74. for exists, module in zip(c_files_exist, modules):
  75. if not exists:
  76. print("WARNING: Trying to build without Cython, but pre-generated "
  77. "'%s%s.c' is not available." % (PACKAGE_PATH, module))
  78. source_extension = ".c"
  79. print("Building without Cython.")
  80. lib_versions = get_library_versions()
  81. if lib_versions[0]:
  82. print("Using build configuration of libxml2 %s and libxslt %s" %
  83. lib_versions)
  84. else:
  85. print("Using build configuration of libxslt %s" %
  86. lib_versions[1])
  87. _include_dirs = include_dirs(static_include_dirs)
  88. _library_dirs = library_dirs(static_library_dirs)
  89. _cflags = cflags(static_cflags)
  90. _define_macros = define_macros()
  91. _libraries = libraries()
  92. _include_dirs.append(os.path.join(get_base_dir(), INCLUDE_PACKAGE_PATH))
  93. if _library_dirs:
  94. message = "Building against libxml2/libxslt in "
  95. if len(_library_dirs) > 1:
  96. print(message + "one of the following directories:")
  97. for dir in _library_dirs:
  98. print(" " + dir)
  99. else:
  100. print(message + "the following directory: " +
  101. _library_dirs[0])
  102. if OPTION_AUTO_RPATH:
  103. runtime_library_dirs = _library_dirs
  104. else:
  105. runtime_library_dirs = []
  106. if CYTHON_INSTALLED and OPTION_SHOW_WARNINGS:
  107. from Cython.Compiler import Errors
  108. Errors.LEVEL = 0
  109. result = []
  110. for module in modules:
  111. main_module_source = PACKAGE_PATH + module + source_extension
  112. result.append(
  113. Extension(
  114. module,
  115. sources = [main_module_source],
  116. depends = find_dependencies(module),
  117. extra_compile_args = _cflags,
  118. extra_objects = static_binaries,
  119. define_macros = _define_macros,
  120. include_dirs = _include_dirs,
  121. library_dirs = _library_dirs,
  122. runtime_library_dirs = runtime_library_dirs,
  123. libraries = _libraries,
  124. ))
  125. if CYTHON_INSTALLED and OPTION_WITH_CYTHON_GDB:
  126. for ext in result:
  127. ext.cython_gdb = True
  128. if CYTHON_INSTALLED and source_extension == '.pyx':
  129. # build .c files right now and convert Extension() objects
  130. from Cython.Build import cythonize
  131. result = cythonize(result)
  132. return result
  133. def find_dependencies(module):
  134. if not CYTHON_INSTALLED:
  135. return []
  136. base_dir = get_base_dir()
  137. package_dir = os.path.join(base_dir, PACKAGE_PATH)
  138. includes_dir = os.path.join(base_dir, INCLUDE_PACKAGE_PATH)
  139. pxd_files = [ os.path.join(includes_dir, filename)
  140. for filename in os.listdir(includes_dir)
  141. if filename.endswith('.pxd') ]
  142. if 'etree' in module:
  143. pxi_files = [ os.path.join(PACKAGE_PATH, filename)
  144. for filename in os.listdir(package_dir)
  145. if filename.endswith('.pxi')
  146. and 'objectpath' not in filename ]
  147. pxd_files = [ filename for filename in pxd_files
  148. if 'etreepublic' not in filename ]
  149. elif 'objectify' in module:
  150. pxi_files = [ os.path.join(PACKAGE_PATH, 'objectpath.pxi') ]
  151. else:
  152. pxi_files = []
  153. return pxd_files + pxi_files
  154. def extra_setup_args():
  155. result = {}
  156. if CYTHON_INSTALLED:
  157. result['cmdclass'] = {'build_ext': build_pyx}
  158. return result
  159. def libraries():
  160. if sys.platform in ('win32',):
  161. libs = ['libxslt', 'libexslt', 'libxml2', 'iconv']
  162. if OPTION_STATIC:
  163. libs = ['%s_a' % lib for lib in libs]
  164. libs.extend(['zlib', 'WS2_32'])
  165. elif OPTION_STATIC:
  166. libs = ['z', 'm']
  167. else:
  168. libs = ['xslt', 'exslt', 'xml2', 'z', 'm']
  169. return libs
  170. def library_dirs(static_library_dirs):
  171. if OPTION_STATIC:
  172. if not static_library_dirs:
  173. static_library_dirs = env_var('LIBRARY')
  174. assert static_library_dirs, "Static build not configured, see doc/build.txt"
  175. return static_library_dirs
  176. # filter them from xslt-config --libs
  177. result = []
  178. possible_library_dirs = flags('libs')
  179. for possible_library_dir in possible_library_dirs:
  180. if possible_library_dir.startswith('-L'):
  181. result.append(possible_library_dir[2:])
  182. return result
  183. def include_dirs(static_include_dirs):
  184. if OPTION_STATIC:
  185. if not static_include_dirs:
  186. static_include_dirs = env_var('INCLUDE')
  187. return static_include_dirs
  188. # filter them from xslt-config --cflags
  189. result = []
  190. possible_include_dirs = flags('cflags')
  191. for possible_include_dir in possible_include_dirs:
  192. if possible_include_dir.startswith('-I'):
  193. result.append(possible_include_dir[2:])
  194. return result
  195. def cflags(static_cflags):
  196. result = []
  197. if not OPTION_SHOW_WARNINGS:
  198. result.append('-w')
  199. if OPTION_DEBUG_GCC:
  200. result.append('-g2')
  201. if OPTION_STATIC:
  202. if not static_cflags:
  203. static_cflags = env_var('CFLAGS')
  204. result.extend(static_cflags)
  205. else:
  206. # anything from xslt-config --cflags that doesn't start with -I
  207. possible_cflags = flags('cflags')
  208. for possible_cflag in possible_cflags:
  209. if not possible_cflag.startswith('-I'):
  210. result.append(possible_cflag)
  211. if sys.platform in ('darwin',):
  212. for opt in result:
  213. if 'flat_namespace' in opt:
  214. break
  215. else:
  216. result.append('-flat_namespace')
  217. return result
  218. def define_macros():
  219. macros = []
  220. if OPTION_WITHOUT_ASSERT:
  221. macros.append(('PYREX_WITHOUT_ASSERTIONS', None))
  222. if OPTION_WITHOUT_THREADING:
  223. macros.append(('WITHOUT_THREADING', None))
  224. if OPTION_WITH_REFNANNY:
  225. macros.append(('CYTHON_REFNANNY', None))
  226. if OPTION_WITH_UNICODE_STRINGS:
  227. macros.append(('LXML_UNICODE_STRINGS', '1'))
  228. return macros
  229. _ERROR_PRINTED = False
  230. def run_command(cmd, *args):
  231. if not cmd:
  232. return ''
  233. if args:
  234. cmd = ' '.join((cmd,) + args)
  235. try:
  236. import subprocess
  237. except ImportError:
  238. # Python 2.3
  239. sf, rf, ef = os.popen3(cmd)
  240. sf.close()
  241. errors = ef.read()
  242. stdout_data = rf.read()
  243. else:
  244. # Python 2.4+
  245. p = subprocess.Popen(cmd, shell=True,
  246. stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  247. stdout_data, errors = p.communicate()
  248. global _ERROR_PRINTED
  249. if errors and not _ERROR_PRINTED:
  250. _ERROR_PRINTED = True
  251. print("ERROR: %s" % errors)
  252. print("** make sure the development packages of libxml2 and libxslt are installed **\n")
  253. return decode_input(stdout_data).strip()
  254. def get_library_versions():
  255. xml2_version = run_command(find_xml2_config(), "--version")
  256. xslt_version = run_command(find_xslt_config(), "--version")
  257. return xml2_version, xslt_version
  258. def flags(option):
  259. xml2_flags = run_command(find_xml2_config(), "--%s" % option)
  260. xslt_flags = run_command(find_xslt_config(), "--%s" % option)
  261. flag_list = xml2_flags.split()
  262. for flag in xslt_flags.split():
  263. if flag not in flag_list:
  264. flag_list.append(flag)
  265. return flag_list
  266. XSLT_CONFIG = None
  267. XML2_CONFIG = None
  268. def find_xml2_config():
  269. global XML2_CONFIG
  270. if XML2_CONFIG:
  271. return XML2_CONFIG
  272. option = '--with-xml2-config='
  273. for arg in sys.argv:
  274. if arg.startswith(option):
  275. sys.argv.remove(arg)
  276. XML2_CONFIG = arg[len(option):]
  277. return XML2_CONFIG
  278. else:
  279. # default: do nothing, rely only on xslt-config
  280. XML2_CONFIG = os.getenv('XML2_CONFIG', '')
  281. return XML2_CONFIG
  282. def find_xslt_config():
  283. global XSLT_CONFIG
  284. if XSLT_CONFIG:
  285. return XSLT_CONFIG
  286. option = '--with-xslt-config='
  287. for arg in sys.argv:
  288. if arg.startswith(option):
  289. sys.argv.remove(arg)
  290. XSLT_CONFIG = arg[len(option):]
  291. return XSLT_CONFIG
  292. else:
  293. XSLT_CONFIG = os.getenv('XSLT_CONFIG', 'xslt-config')
  294. return XSLT_CONFIG
  295. ## Option handling:
  296. def has_option(name):
  297. try:
  298. sys.argv.remove('--%s' % name)
  299. return True
  300. except ValueError:
  301. pass
  302. # allow passing all cmd line options also as environment variables
  303. env_val = os.getenv(name.upper().replace('-', '_'), 'false').lower()
  304. if env_val == "true":
  305. return True
  306. return False
  307. def option_value(name):
  308. for index, option in enumerate(sys.argv):
  309. if option == '--' + name:
  310. if index+1 >= len(sys.argv):
  311. raise DistutilsOptionError(
  312. 'The option %s requires a value' % option)
  313. value = sys.argv[index+1]
  314. sys.argv[index:index+2] = []
  315. return value
  316. if option.startswith('--' + name + '='):
  317. value = option[len(name)+3:]
  318. sys.argv[index:index+1] = []
  319. return value
  320. env_val = os.getenv(name.upper().replace('-', '_'))
  321. return env_val
  322. staticbuild = bool(os.environ.get('STATICBUILD', ''))
  323. # pick up any commandline options and/or env variables
  324. OPTION_WITHOUT_OBJECTIFY = has_option('without-objectify')
  325. OPTION_WITH_UNICODE_STRINGS = has_option('with-unicode-strings')
  326. OPTION_WITHOUT_ASSERT = has_option('without-assert')
  327. OPTION_WITHOUT_THREADING = has_option('without-threading')
  328. OPTION_WITHOUT_CYTHON = has_option('without-cython')
  329. OPTION_WITH_CYTHON = has_option('with-cython')
  330. OPTION_WITH_CYTHON_GDB = has_option('cython-gdb')
  331. OPTION_WITH_REFNANNY = has_option('with-refnanny')
  332. if OPTION_WITHOUT_CYTHON:
  333. CYTHON_INSTALLED = False
  334. OPTION_STATIC = staticbuild or has_option('static')
  335. OPTION_DEBUG_GCC = has_option('debug-gcc')
  336. OPTION_SHOW_WARNINGS = has_option('warnings')
  337. OPTION_AUTO_RPATH = has_option('auto-rpath')
  338. OPTION_BUILD_LIBXML2XSLT = staticbuild or has_option('static-deps')
  339. if OPTION_BUILD_LIBXML2XSLT:
  340. OPTION_STATIC = True
  341. OPTION_LIBXML2_VERSION = option_value('libxml2-version')
  342. OPTION_LIBXSLT_VERSION = option_value('libxslt-version')
  343. OPTION_LIBICONV_VERSION = option_value('libiconv-version')
  344. OPTION_MULTICORE = option_value('multicore')
  345. OPTION_DOWNLOAD_DIR = option_value('download-dir')
  346. if OPTION_DOWNLOAD_DIR is None:
  347. OPTION_DOWNLOAD_DIR = 'libs'