setup.py.in 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #!/usr/bin/python -u
  2. #
  3. # Setup script for libxml2 and libxslt if found
  4. #
  5. import sys, os
  6. from distutils.core import setup, Extension
  7. # Below ROOT, we expect to find include, include/libxml2, lib and bin.
  8. # On *nix, it is not needed (but should not harm),
  9. # on Windows, it is set by configure.js.
  10. ROOT = r'@prefix@'
  11. # Thread-enabled libxml2
  12. with_threads = @WITH_THREADS@
  13. # If this flag is set (windows only),
  14. # a private copy of the dlls are included in the package.
  15. # If this flag is not set, the libxml2 and libxslt
  16. # dlls must be found somewhere in the PATH at runtime.
  17. WITHDLLS = 1 and sys.platform.startswith('win')
  18. def missing(file):
  19. if os.access(file, os.R_OK) == 0:
  20. return 1
  21. return 0
  22. try:
  23. HOME = os.environ['HOME']
  24. except:
  25. HOME="C:"
  26. if WITHDLLS:
  27. # libxml dlls (expected in ROOT/bin)
  28. dlls = [ 'iconv.dll','libxml2.dll','libxslt.dll','libexslt.dll' ]
  29. dlls = map(lambda dll: os.path.join(ROOT,'bin',dll),dlls)
  30. # create __init__.py for the libxmlmods package
  31. if not os.path.exists("libxmlmods"):
  32. os.mkdir("libxmlmods")
  33. open("libxmlmods/__init__.py","w").close()
  34. def altImport(s):
  35. s = s.replace("import libxml2mod","from libxmlmods import libxml2mod")
  36. s = s.replace("import libxsltmod","from libxmlmods import libxsltmod")
  37. return s
  38. if sys.platform.startswith('win'):
  39. libraryPrefix = 'lib'
  40. platformLibs = []
  41. else:
  42. libraryPrefix = ''
  43. platformLibs = ["m","z"]
  44. # those are examined to find
  45. # - libxml2/libxml/tree.h
  46. # - iconv.h
  47. # - libxslt/xsltconfig.h
  48. includes_dir = [
  49. "/usr/include",
  50. "/usr/local/include",
  51. "/opt/include",
  52. os.path.join(ROOT,'include'),
  53. HOME
  54. ];
  55. xml_includes=""
  56. for dir in includes_dir:
  57. if not missing(dir + "/libxml2/libxml/tree.h"):
  58. xml_includes=dir + "/libxml2"
  59. break;
  60. if xml_includes == "":
  61. print "failed to find headers for libxml2: update includes_dir"
  62. sys.exit(1)
  63. iconv_includes=""
  64. for dir in includes_dir:
  65. if not missing(dir + "/iconv.h"):
  66. iconv_includes=dir
  67. break;
  68. if iconv_includes == "":
  69. print "failed to find headers for libiconv: update includes_dir"
  70. sys.exit(1)
  71. # those are added in the linker search path for libraries
  72. libdirs = [
  73. os.path.join(ROOT,'lib'),
  74. ]
  75. xml_files = ["libxml2-api.xml", "libxml2-python-api.xml",
  76. "libxml.c", "libxml.py", "libxml_wrap.h", "types.c",
  77. "xmlgenerator.py", "README", "TODO", "drv_libxml2.py"]
  78. xslt_files = ["libxslt-api.xml", "libxslt-python-api.xml",
  79. "libxslt.c", "libxsl.py", "libxslt_wrap.h",
  80. "xsltgenerator.py"]
  81. if missing("libxml2-py.c") or missing("libxml2.py"):
  82. try:
  83. try:
  84. import xmlgenerator
  85. except:
  86. import generator
  87. except:
  88. print "failed to find and generate stubs for libxml2, aborting ..."
  89. print sys.exc_type, sys.exc_value
  90. sys.exit(1)
  91. head = open("libxml.py", "r")
  92. generated = open("libxml2class.py", "r")
  93. result = open("libxml2.py", "w")
  94. for line in head.readlines():
  95. if WITHDLLS:
  96. result.write(altImport(line))
  97. else:
  98. result.write(line)
  99. for line in generated.readlines():
  100. result.write(line)
  101. head.close()
  102. generated.close()
  103. result.close()
  104. with_xslt=0
  105. if missing("libxslt-py.c") or missing("libxslt.py"):
  106. if missing("xsltgenerator.py") or missing("libxslt-api.xml"):
  107. print "libxslt stub generator not found, libxslt not built"
  108. else:
  109. try:
  110. import xsltgenerator
  111. except:
  112. print "failed to generate stubs for libxslt, aborting ..."
  113. print sys.exc_type, sys.exc_value
  114. else:
  115. head = open("libxsl.py", "r")
  116. generated = open("libxsltclass.py", "r")
  117. result = open("libxslt.py", "w")
  118. for line in head.readlines():
  119. if WITHDLLS:
  120. result.write(altImport(line))
  121. else:
  122. result.write(line)
  123. for line in generated.readlines():
  124. result.write(line)
  125. head.close()
  126. generated.close()
  127. result.close()
  128. with_xslt=1
  129. else:
  130. with_xslt=1
  131. if with_xslt == 1:
  132. xslt_includes=""
  133. for dir in includes_dir:
  134. if not missing(dir + "/libxslt/xsltconfig.h"):
  135. xslt_includes=dir + "/libxslt"
  136. break;
  137. if xslt_includes == "":
  138. print "failed to find headers for libxslt: update includes_dir"
  139. with_xslt = 0
  140. descr = "libxml2 package"
  141. modules = [ 'libxml2', 'drv_libxml2' ]
  142. if WITHDLLS:
  143. modules.append('libxmlmods.__init__')
  144. c_files = ['libxml2-py.c', 'libxml.c', 'types.c' ]
  145. includes= [xml_includes, iconv_includes]
  146. libs = [libraryPrefix + "xml2"] + platformLibs
  147. macros = []
  148. if with_threads:
  149. macros.append(('_REENTRANT','1'))
  150. if with_xslt == 1:
  151. descr = "libxml2 and libxslt package"
  152. if not sys.platform.startswith('win'):
  153. #
  154. # We are gonna build 2 identical shared libs with merge initializing
  155. # both libxml2mod and libxsltmod
  156. #
  157. c_files = c_files + ['libxslt-py.c', 'libxslt.c']
  158. xslt_c_files = c_files
  159. macros.append(('MERGED_MODULES', '1'))
  160. else:
  161. #
  162. # On windows the MERGED_MODULE option is not needed
  163. # (and does not work)
  164. #
  165. xslt_c_files = ['libxslt-py.c', 'libxslt.c', 'types.c']
  166. libs.insert(0, libraryPrefix + 'exslt')
  167. libs.insert(0, libraryPrefix + 'xslt')
  168. includes.append(xslt_includes)
  169. modules.append('libxslt')
  170. extens=[Extension('libxml2mod', c_files, include_dirs=includes,
  171. library_dirs=libdirs,
  172. libraries=libs, define_macros=macros)]
  173. if with_xslt == 1:
  174. extens.append(Extension('libxsltmod', xslt_c_files, include_dirs=includes,
  175. library_dirs=libdirs,
  176. libraries=libs, define_macros=macros))
  177. if missing("MANIFEST"):
  178. manifest = open("MANIFEST", "w")
  179. manifest.write("setup.py\n")
  180. for file in xml_files:
  181. manifest.write(file + "\n")
  182. if with_xslt == 1:
  183. for file in xslt_files:
  184. manifest.write(file + "\n")
  185. manifest.close()
  186. if WITHDLLS:
  187. ext_package = "libxmlmods"
  188. if sys.version >= "2.2":
  189. base = "lib/site-packages/"
  190. else:
  191. base = ""
  192. data_files = [(base+"libxmlmods",dlls)]
  193. else:
  194. ext_package = None
  195. data_files = []
  196. setup (name = "libxml2-python",
  197. # On *nix, the version number is created from setup.py.in
  198. # On windows, it is set by configure.js
  199. version = "@LIBXML_VERSION@",
  200. description = descr,
  201. author = "Daniel Veillard",
  202. author_email = "veillard@redhat.com",
  203. url = "http://xmlsoft.org/python.html",
  204. licence="MIT Licence",
  205. py_modules=modules,
  206. ext_modules=extens,
  207. ext_package=ext_package,
  208. data_files=data_files,
  209. )
  210. sys.exit(0)