• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2#
3# Setup script for libxml2 and libxslt if found
4#
5import sys, os
6
7try:
8    import setuptools
9except ImportError:
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
48if WITHDLLS:
49    def altImport(s):
50        s = s.replace("import libxml2mod","from libxmlmods import libxml2mod")
51        return s
52
53def missing(file):
54    if os.access(file, os.R_OK) == 0:
55        return 1
56    return 0
57
58try:
59    HOME = os.environ['HOME']
60except:
61    HOME="C:"
62
63if sys.platform.startswith('win'):
64    libraryPrefix = 'lib'
65    platformLibs = []
66else:
67    libraryPrefix = ''
68    platformLibs = ["m","z"]
69
70# those are examined to find
71# - libxml2/libxml/tree.h
72# - libxslt/xsltconfig.h
73includes_dir = [
74"/usr/include",
75"/usr/local/include",
76"/opt/include",
77os.path.join(ROOT,'include'),
78HOME
79];
80
81xml_includes=""
82for dir in includes_dir:
83    if not missing(dir + "/libxml2/libxml/tree.h"):
84        xml_includes=dir + "/libxml2"
85        break;
86
87if xml_includes == "":
88    print("failed to find headers for libxml2: update includes_dir")
89    sys.exit(1)
90
91# those are added in the linker search path for libraries
92libdirs = [
93os.path.join(ROOT,'lib'),
94]
95
96xml_files = ["libxml2-api.xml", "libxml2-python-api.xml",
97             "libxml.c", "libxml.py", "libxml_wrap.h", "types.c",
98             "xmlgenerator.py", "README", "TODO", "drv_libxml2.py"]
99
100xslt_files = ["libxslt-api.xml", "libxslt-python-api.xml",
101             "libxslt.c", "libxsl.py", "libxslt_wrap.h",
102             "xsltgenerator.py"]
103
104if missing("libxml2-py.c") or missing("libxml2.py"):
105    try:
106        try:
107            import xmlgenerator
108        except:
109            import generator
110    except:
111        print("failed to find and generate stubs for libxml2, aborting ...")
112        print(sys.exc_info()[0], sys.exc_info()[1])
113        sys.exit(1)
114
115    head = open("libxml.py", "r")
116    generated = open("libxml2class.py", "r")
117    result = open("libxml2.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
129with_xslt=0
130if missing("libxslt-py.c") or missing("libxslt.py"):
131    if missing("xsltgenerator.py") or missing("libxslt-api.xml"):
132        print("libxslt stub generator not found, libxslt not built")
133    else:
134        try:
135            import xsltgenerator
136        except:
137            print("failed to generate stubs for libxslt, aborting ...")
138            print(sys.exc_info()[0], sys.exc_info()[1])
139        else:
140            head = open("libxsl.py", "r")
141            generated = open("libxsltclass.py", "r")
142            result = open("libxslt.py", "w")
143            for line in head.readlines():
144                if WITHDLLS:
145                    result.write(altImport(line))
146                else:
147                    result.write(line)
148            for line in generated.readlines():
149                result.write(line)
150            head.close()
151            generated.close()
152            result.close()
153            with_xslt=1
154else:
155    with_xslt=1
156
157if with_xslt == 1:
158    xslt_includes=""
159    for dir in includes_dir:
160        if not missing(dir + "/libxslt/xsltconfig.h"):
161            xslt_includes=dir + "/libxslt"
162            break;
163
164    if xslt_includes == "":
165        print("failed to find headers for libxslt: update includes_dir")
166        with_xslt = 0
167
168if WITHDLLS:
169    # libxml dlls (expected in ROOT/bin)
170    dlls = [ 'libxml2.dll' ]
171
172    if with_zlib == 1:
173        dlls.append(zlib_dll)
174    if with_lzma == 1:
175        dlls.append(lzma_dll)
176    if with_iconv == 1:
177        dlls.append(iconv_dll)
178    if with_icu == 1:
179        dlls += icu_dlls
180    if with_xslt == 1:
181        dlls += ['libxslt.dll','libexslt.dll']
182
183    packaged_dlls = [os.path.join(ROOT,'bin',dll) for dll in dlls]
184
185    # create __init__.py for the libxmlmods package
186    if not os.path.exists("libxmlmods"):
187        os.mkdir("libxmlmods")
188        open("libxmlmods/__init__.py","w").close()
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