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