blob: c44269a6b451b6b16706560c46024b1e78db8787 [file] [log] [blame]
Daniel Veillard39801e52008-06-03 16:08:54 +00001#!/usr/bin/python -u
2#
3# Setup script for libxml2 and libxslt if found
4#
5import sys, os
6from distutils.core import setup, Extension
7
8# Below ROOT, we expect to find include, include/libxml2, lib and bin.
Daniel Veillard4b61cf62013-10-02 22:18:21 +08009# On *nix, it is not needed (but should not harm),
Daniel Veillard39801e52008-06-03 16:08:54 +000010# on Windows, it is set by configure.js.
Daniel Veillard4b61cf62013-10-02 22:18:21 +080011ROOT = r'/usr'
Daniel Veillard39801e52008-06-03 16:08:54 +000012
13# Thread-enabled libxml2
Daniel Veillard96bb7402009-10-06 18:38:15 +020014with_threads = 1
Daniel Veillard39801e52008-06-03 16:08:54 +000015
Daniel Veillard4b61cf62013-10-02 22:18:21 +080016# If this flag is set (windows only),
Daniel Veillard39801e52008-06-03 16:08:54 +000017# a private copy of the dlls are included in the package.
18# If this flag is not set, the libxml2 and libxslt
19# dlls must be found somewhere in the PATH at runtime.
20WITHDLLS = 1 and sys.platform.startswith('win')
21
22def missing(file):
23 if os.access(file, os.R_OK) == 0:
24 return 1
25 return 0
26
27try:
28 HOME = os.environ['HOME']
29except:
30 HOME="C:"
31
32if WITHDLLS:
33 # libxml dlls (expected in ROOT/bin)
34 dlls = [ 'iconv.dll','libxml2.dll','libxslt.dll','libexslt.dll' ]
Daniel Veillard3cb1ae22013-03-27 22:40:54 +080035 dlls = [os.path.join(ROOT,'bin',dll) for dll in dlls]
Daniel Veillard39801e52008-06-03 16:08:54 +000036
37 # create __init__.py for the libxmlmods package
38 if not os.path.exists("libxmlmods"):
39 os.mkdir("libxmlmods")
40 open("libxmlmods/__init__.py","w").close()
41
42 def altImport(s):
43 s = s.replace("import libxml2mod","from libxmlmods import libxml2mod")
44 s = s.replace("import libxsltmod","from libxmlmods import libxsltmod")
45 return s
46
47if sys.platform.startswith('win'):
48 libraryPrefix = 'lib'
49 platformLibs = []
50else:
51 libraryPrefix = ''
52 platformLibs = ["m","z"]
53
Daniel Veillard4b61cf62013-10-02 22:18:21 +080054# those are examined to find
Daniel Veillard39801e52008-06-03 16:08:54 +000055# - libxml2/libxml/tree.h
Daniel Veillard4b61cf62013-10-02 22:18:21 +080056# - iconv.h
Daniel Veillard39801e52008-06-03 16:08:54 +000057# - libxslt/xsltconfig.h
58includes_dir = [
59"/usr/include",
60"/usr/local/include",
61"/opt/include",
62os.path.join(ROOT,'include'),
63HOME
Daniel Veillard34e3f642008-07-29 09:02:27 +000064];
Daniel Veillard39801e52008-06-03 16:08:54 +000065
66xml_includes=""
67for dir in includes_dir:
68 if not missing(dir + "/libxml2/libxml/tree.h"):
69 xml_includes=dir + "/libxml2"
Daniel Veillard4b61cf62013-10-02 22:18:21 +080070 break;
Daniel Veillard39801e52008-06-03 16:08:54 +000071
72if xml_includes == "":
Daniel Veillard3cb1ae22013-03-27 22:40:54 +080073 print("failed to find headers for libxml2: update includes_dir")
Daniel Veillard39801e52008-06-03 16:08:54 +000074 sys.exit(1)
75
76iconv_includes=""
77for dir in includes_dir:
78 if not missing(dir + "/iconv.h"):
79 iconv_includes=dir
Daniel Veillard4b61cf62013-10-02 22:18:21 +080080 break;
Daniel Veillard39801e52008-06-03 16:08:54 +000081
82if iconv_includes == "":
Daniel Veillard3cb1ae22013-03-27 22:40:54 +080083 print("failed to find headers for libiconv: update includes_dir")
Daniel Veillard39801e52008-06-03 16:08:54 +000084 sys.exit(1)
85
86# those are added in the linker search path for libraries
87libdirs = [
88os.path.join(ROOT,'lib'),
89]
90
91xml_files = ["libxml2-api.xml", "libxml2-python-api.xml",
92 "libxml.c", "libxml.py", "libxml_wrap.h", "types.c",
Daniel Veillard4b61cf62013-10-02 22:18:21 +080093 "xmlgenerator.py", "README", "TODO", "drv_libxml2.py"]
Daniel Veillard39801e52008-06-03 16:08:54 +000094
95xslt_files = ["libxslt-api.xml", "libxslt-python-api.xml",
96 "libxslt.c", "libxsl.py", "libxslt_wrap.h",
Daniel Veillard4b61cf62013-10-02 22:18:21 +080097 "xsltgenerator.py"]
Daniel Veillard39801e52008-06-03 16:08:54 +000098
99if missing("libxml2-py.c") or missing("libxml2.py"):
100 try:
Daniel Veillard4b61cf62013-10-02 22:18:21 +0800101 try:
102 import xmlgenerator
103 except:
104 import generator
Daniel Veillard39801e52008-06-03 16:08:54 +0000105 except:
Daniel Veillard4b61cf62013-10-02 22:18:21 +0800106 print("failed to find and generate stubs for libxml2, aborting ...")
107 print(sys.exc_info()[0], sys.exc_info()[1])
108 sys.exit(1)
Daniel Veillard39801e52008-06-03 16:08:54 +0000109
110 head = open("libxml.py", "r")
111 generated = open("libxml2class.py", "r")
112 result = open("libxml2.py", "w")
113 for line in head.readlines():
114 if WITHDLLS:
115 result.write(altImport(line))
116 else:
117 result.write(line)
118 for line in generated.readlines():
Daniel Veillard4b61cf62013-10-02 22:18:21 +0800119 result.write(line)
Daniel Veillard39801e52008-06-03 16:08:54 +0000120 head.close()
121 generated.close()
122 result.close()
123
124with_xslt=0
125if missing("libxslt-py.c") or missing("libxslt.py"):
126 if missing("xsltgenerator.py") or missing("libxslt-api.xml"):
Daniel Veillard3cb1ae22013-03-27 22:40:54 +0800127 print("libxslt stub generator not found, libxslt not built")
Daniel Veillard39801e52008-06-03 16:08:54 +0000128 else:
Daniel Veillard4b61cf62013-10-02 22:18:21 +0800129 try:
130 import xsltgenerator
131 except:
132 print("failed to generate stubs for libxslt, aborting ...")
133 print(sys.exc_info()[0], sys.exc_info()[1])
134 else:
135 head = open("libxsl.py", "r")
136 generated = open("libxsltclass.py", "r")
137 result = open("libxslt.py", "w")
138 for line in head.readlines():
Daniel Veillard39801e52008-06-03 16:08:54 +0000139 if WITHDLLS:
140 result.write(altImport(line))
141 else:
142 result.write(line)
Daniel Veillard4b61cf62013-10-02 22:18:21 +0800143 for line in generated.readlines():
144 result.write(line)
145 head.close()
146 generated.close()
147 result.close()
148 with_xslt=1
Daniel Veillard39801e52008-06-03 16:08:54 +0000149else:
150 with_xslt=1
151
152if with_xslt == 1:
153 xslt_includes=""
154 for dir in includes_dir:
Daniel Veillard4b61cf62013-10-02 22:18:21 +0800155 if not missing(dir + "/libxslt/xsltconfig.h"):
156 xslt_includes=dir + "/libxslt"
157 break;
Daniel Veillard39801e52008-06-03 16:08:54 +0000158
159 if xslt_includes == "":
Daniel Veillard4b61cf62013-10-02 22:18:21 +0800160 print("failed to find headers for libxslt: update includes_dir")
161 with_xslt = 0
Daniel Veillard39801e52008-06-03 16:08:54 +0000162
163
164descr = "libxml2 package"
165modules = [ 'libxml2', 'drv_libxml2' ]
166if WITHDLLS:
167 modules.append('libxmlmods.__init__')
168c_files = ['libxml2-py.c', 'libxml.c', 'types.c' ]
169includes= [xml_includes, iconv_includes]
170libs = [libraryPrefix + "xml2"] + platformLibs
171macros = []
172if with_threads:
173 macros.append(('_REENTRANT','1'))
174if with_xslt == 1:
175 descr = "libxml2 and libxslt package"
176 if not sys.platform.startswith('win'):
177 #
178 # We are gonna build 2 identical shared libs with merge initializing
179 # both libxml2mod and libxsltmod
180 #
181 c_files = c_files + ['libxslt-py.c', 'libxslt.c']
182 xslt_c_files = c_files
183 macros.append(('MERGED_MODULES', '1'))
184 else:
185 #
186 # On windows the MERGED_MODULE option is not needed
187 # (and does not work)
188 #
189 xslt_c_files = ['libxslt-py.c', 'libxslt.c', 'types.c']
190 libs.insert(0, libraryPrefix + 'exslt')
191 libs.insert(0, libraryPrefix + 'xslt')
192 includes.append(xslt_includes)
193 modules.append('libxslt')
194
195
196extens=[Extension('libxml2mod', c_files, include_dirs=includes,
Daniel Veillard4b61cf62013-10-02 22:18:21 +0800197 library_dirs=libdirs,
198 libraries=libs, define_macros=macros)]
Daniel Veillard39801e52008-06-03 16:08:54 +0000199if with_xslt == 1:
200 extens.append(Extension('libxsltmod', xslt_c_files, include_dirs=includes,
Daniel Veillard4b61cf62013-10-02 22:18:21 +0800201 library_dirs=libdirs,
Daniel Veillard39801e52008-06-03 16:08:54 +0000202 libraries=libs, define_macros=macros))
203
204if missing("MANIFEST"):
205
206 manifest = open("MANIFEST", "w")
207 manifest.write("setup.py\n")
208 for file in xml_files:
209 manifest.write(file + "\n")
210 if with_xslt == 1:
Daniel Veillard4b61cf62013-10-02 22:18:21 +0800211 for file in xslt_files:
212 manifest.write(file + "\n")
Daniel Veillard39801e52008-06-03 16:08:54 +0000213 manifest.close()
214
215if WITHDLLS:
216 ext_package = "libxmlmods"
217 if sys.version >= "2.2":
218 base = "lib/site-packages/"
219 else:
220 base = ""
221 data_files = [(base+"libxmlmods",dlls)]
222else:
223 ext_package = None
224 data_files = []
225
226setup (name = "libxml2-python",
227 # On *nix, the version number is created from setup.py.in
228 # On windows, it is set by configure.js
Daniel Veillard8effcb52016-05-09 10:31:09 +0800229 version = "2.9.4",
Daniel Veillard39801e52008-06-03 16:08:54 +0000230 description = descr,
231 author = "Daniel Veillard",
232 author_email = "veillard@redhat.com",
233 url = "http://xmlsoft.org/python.html",
234 licence="MIT Licence",
235 py_modules=modules,
236 ext_modules=extens,
237 ext_package=ext_package,
238 data_files=data_files,
239 )
240
241sys.exit(0)
242