• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python -u
2#
3# Setup script for libxml2 and libxslt if found
4#
5import sys, os
6
7try:
8    import setuptools
9except ModuleNotFoundError:
10    pass
11
12from distutils.core import setup, Extension
13
14# Below ROOT, we expect to find include, include/libxml2, lib and bin.
15# On *nix, it is not needed (but should not harm),
16# on Windows, it is set by configure.js.
17ROOT = r'@prefix@'
18
19# Thread-enabled libxml2
20with_threads = @WITH_THREADS@
21
22# Features of libxml2 requiring external DLLs
23with_iconv = @WITH_ICONV@
24with_zlib = @WITH_ZLIB@
25with_lzma = @WITH_LZMA@
26with_icu = @WITH_ICU@
27
28icu_series = 69
29
30if icu_series is not None:
31    icu_series_s = str(icu_series)
32else:
33    icu_series_s = ''
34
35# If bundling DLLs, check the following to ensure things are correct
36# (Check the value of `icu_series` above as well)
37iconv_dll = 'iconv.dll'
38zlib_dll = 'zlib1.dll'
39lzma_dll = 'liblzma.dll'
40icu_dlls = ['icuuc%s.dll' % icu_series_s, 'icudt%s.dll' % icu_series_s]
41
42# If this flag is set (windows only),
43# a private copy of the dlls are included in the package.
44# If this flag is not set, the libxml2 and libxslt
45# dlls must be found somewhere in the PATH at runtime.
46WITHDLLS = 1 and sys.platform.startswith('win')
47
48def missing(file):
49    if os.access(file, os.R_OK) == 0:
50        return 1
51    return 0
52
53try:
54    HOME = os.environ['HOME']
55except:
56    HOME="C:"
57
58if sys.platform.startswith('win'):
59    libraryPrefix = 'lib'
60    platformLibs = []
61else:
62    libraryPrefix = ''
63    platformLibs = ["m","z"]
64
65# those are examined to find
66# - libxml2/libxml/tree.h
67# - libxslt/xsltconfig.h
68includes_dir = [
69"/usr/include",
70"/usr/local/include",
71"/opt/include",
72os.path.join(ROOT,'include'),
73HOME
74];
75
76xml_includes=""
77for dir in includes_dir:
78    if not missing(dir + "/libxml2/libxml/tree.h"):
79        xml_includes=dir + "/libxml2"
80        break;
81
82if xml_includes == "":
83    print("failed to find headers for libxml2: update includes_dir")
84    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",
93             "xmlgenerator.py", "README", "TODO", "drv_libxml2.py"]
94
95xslt_files = ["libxslt-api.xml", "libxslt-python-api.xml",
96             "libxslt.c", "libxsl.py", "libxslt_wrap.h",
97             "xsltgenerator.py"]
98
99if missing("libxml2-py.c") or missing("libxml2.py"):
100    try:
101        try:
102            import xmlgenerator
103        except:
104            import generator
105    except:
106        print("failed to find and generate stubs for libxml2, aborting ...")
107        print(sys.exc_info()[0], sys.exc_info()[1])
108        sys.exit(1)
109
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():
119        result.write(line)
120    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"):
127        print("libxslt stub generator not found, libxslt not built")
128    else:
129        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():
139                if WITHDLLS:
140                    result.write(altImport(line))
141                else:
142                    result.write(line)
143            for line in generated.readlines():
144                result.write(line)
145            head.close()
146            generated.close()
147            result.close()
148            with_xslt=1
149else:
150    with_xslt=1
151
152if with_xslt == 1:
153    xslt_includes=""
154    for dir in includes_dir:
155        if not missing(dir + "/libxslt/xsltconfig.h"):
156            xslt_includes=dir + "/libxslt"
157            break;
158
159    if xslt_includes == "":
160        print("failed to find headers for libxslt: update includes_dir")
161        with_xslt = 0
162
163if WITHDLLS:
164    # libxml dlls (expected in ROOT/bin)
165    dlls = [ 'libxml2.dll' ]
166
167    if with_zlib == 1:
168        dlls.append(zlib_dll)
169    if with_lzma == 1:
170        dlls.append(lzma_dll)
171    if with_iconv == 1:
172        dlls.append(iconv_dll)
173    if with_icu == 1:
174        dlls += icu_dlls
175    if with_xslt == 1:
176        dlls += ['libxslt.dll','libexslt.dll']
177
178    packaged_dlls = [os.path.join(ROOT,'bin',dll) for dll in dlls]
179
180    # create __init__.py for the libxmlmods package
181    if not os.path.exists("libxmlmods"):
182        os.mkdir("libxmlmods")
183        open("libxmlmods/__init__.py","w").close()
184
185    def altImport(s):
186        s = s.replace("import libxml2mod","from libxmlmods import libxml2mod")
187        s = s.replace("import libxsltmod","from libxmlmods import libxsltmod")
188        return s
189
190    packaged_dlls = [os.path.join(ROOT,'bin',dll) for dll in dlls]
191
192descr = "libxml2 package"
193modules = [ 'libxml2', 'drv_libxml2' ]
194if WITHDLLS:
195    modules.append('libxmlmods.__init__')
196c_files = ['libxml2-py.c', 'libxml.c', 'types.c' ]
197includes= [xml_includes]
198libs    = [libraryPrefix + "xml2"] + platformLibs
199macros  = []
200if with_threads:
201    macros.append(('_REENTRANT','1'))
202if with_xslt == 1:
203    descr = "libxml2 and libxslt package"
204    if not sys.platform.startswith('win'):
205        #
206        # We are gonna build 2 identical shared libs with merge initializing
207        # both libxml2mod and libxsltmod
208        #
209        c_files = c_files + ['libxslt-py.c', 'libxslt.c']
210        xslt_c_files = c_files
211        macros.append(('MERGED_MODULES', '1'))
212    else:
213        #
214        # On windows the MERGED_MODULE option is not needed
215        # (and does not work)
216        #
217        xslt_c_files = ['libxslt-py.c', 'libxslt.c', 'types.c']
218    libs.insert(0, libraryPrefix + 'exslt')
219    libs.insert(0, libraryPrefix + 'xslt')
220    includes.append(xslt_includes)
221    modules.append('libxslt')
222
223
224extens=[Extension('libxml2mod', c_files, include_dirs=includes,
225                  library_dirs=libdirs,
226                  libraries=libs, define_macros=macros)]
227if with_xslt == 1:
228    extens.append(Extension('libxsltmod', xslt_c_files, include_dirs=includes,
229                            library_dirs=libdirs,
230                            libraries=libs, define_macros=macros))
231
232if missing("MANIFEST"):
233
234    manifest = open("MANIFEST", "w")
235    manifest.write("setup.py\n")
236    for file in xml_files:
237        manifest.write(file + "\n")
238    if with_xslt == 1:
239        for file in xslt_files:
240            manifest.write(file + "\n")
241    manifest.close()
242
243if WITHDLLS:
244    ext_package = "libxmlmods"
245    if sys.version >= "2.2":
246        base = "lib/site-packages/"
247    else:
248        base = ""
249    data_files = [(base+"libxmlmods",packaged_dlls)]
250else:
251    ext_package = None
252    data_files = []
253
254setup (name = "libxml2-python",
255       # On *nix, the version number is created from setup.py.in
256       # On windows, it is set by configure.js
257       version = "@LIBXML_VERSION@",
258       description = descr,
259       author = "Daniel Veillard",
260       author_email = "veillard@redhat.com",
261       url = "https://gitlab.gnome.org/GNOME/libxml2",
262       licence="MIT Licence",
263       py_modules=modules,
264       ext_modules=extens,
265       ext_package=ext_package,
266       data_files=data_files,
267       )
268
269sys.exit(0)
270
271