Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 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 | |
Daniel Veillard | 9d5ea17 | 2002-11-27 08:02:06 +0000 | [diff] [blame] | 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'@prefix@' |
Daniel Veillard | a1196ed | 2002-11-23 11:22:49 +0000 | [diff] [blame] | 12 | |
Daniel Veillard | 94bb2f1 | 2003-04-27 22:14:07 +0000 | [diff] [blame] | 13 | # Thread-enabled libxml2 |
| 14 | with_threads = @WITH_THREADS@ |
| 15 | |
Daniel Veillard | a1196ed | 2002-11-23 11:22:49 +0000 | [diff] [blame] | 16 | # If this flag is set (windows only), |
| 17 | # 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. |
| 20 | WITHDLLS = 1 and sys.platform.startswith('win') |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 21 | |
| 22 | def missing(file): |
| 23 | if os.access(file, os.R_OK) == 0: |
| 24 | return 1 |
| 25 | return 0 |
| 26 | |
Daniel Veillard | a7084cd | 2002-04-15 17:12:47 +0000 | [diff] [blame] | 27 | try: |
Daniel Veillard | a1196ed | 2002-11-23 11:22:49 +0000 | [diff] [blame] | 28 | HOME = os.environ['HOME'] |
Daniel Veillard | a7084cd | 2002-04-15 17:12:47 +0000 | [diff] [blame] | 29 | except: |
| 30 | HOME="C:" |
| 31 | |
Daniel Veillard | a1196ed | 2002-11-23 11:22:49 +0000 | [diff] [blame] | 32 | if WITHDLLS: |
| 33 | # libxml dlls (expected in ROOT/bin) |
| 34 | dlls = [ 'iconv.dll','libxml2.dll','libxslt.dll','libexslt.dll' ] |
| 35 | dlls = map(lambda dll: os.path.join(ROOT,'bin',dll),dlls) |
| 36 | |
| 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 | |
| 47 | if sys.platform.startswith('win'): |
| 48 | libraryPrefix = 'lib' |
| 49 | platformLibs = [] |
| 50 | else: |
| 51 | libraryPrefix = '' |
| 52 | platformLibs = ["m","z"] |
| 53 | |
| 54 | # those are examined to find |
| 55 | # - libxml2/libxml/tree.h |
| 56 | # - iconv.h |
| 57 | # - libxslt/xsltconfig.h |
Daniel Veillard | a7084cd | 2002-04-15 17:12:47 +0000 | [diff] [blame] | 58 | includes_dir = [ |
| 59 | "/usr/include", |
| 60 | "/usr/local/include", |
| 61 | "/opt/include", |
Daniel Veillard | a1196ed | 2002-11-23 11:22:49 +0000 | [diff] [blame] | 62 | os.path.join(ROOT,'include'), |
Daniel Veillard | a7084cd | 2002-04-15 17:12:47 +0000 | [diff] [blame] | 63 | HOME |
| 64 | ]; |
| 65 | |
| 66 | xml_includes="" |
| 67 | for dir in includes_dir: |
| 68 | if not missing(dir + "/libxml2/libxml/tree.h"): |
| 69 | xml_includes=dir + "/libxml2" |
| 70 | break; |
| 71 | |
| 72 | if xml_includes == "": |
| 73 | print "failed to find headers for libxml2: update includes_dir" |
| 74 | sys.exit(1) |
Daniel Veillard | a1196ed | 2002-11-23 11:22:49 +0000 | [diff] [blame] | 75 | |
| 76 | iconv_includes="" |
| 77 | for dir in includes_dir: |
| 78 | if not missing(dir + "/iconv.h"): |
| 79 | iconv_includes=dir |
| 80 | break; |
| 81 | |
| 82 | if iconv_includes == "": |
| 83 | print "failed to find headers for libiconv: update includes_dir" |
| 84 | sys.exit(1) |
| 85 | |
| 86 | # those are added in the linker search path for libraries |
| 87 | libdirs = [ |
| 88 | os.path.join(ROOT,'lib'), |
| 89 | ] |
| 90 | |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 91 | xml_files = ["libxml2-api.xml", "libxml2-python-api.xml", |
| 92 | "libxml.c", "libxml.py", "libxml_wrap.h", "types.c", |
Daniel Veillard | 2032d29 | 2003-03-25 11:09:40 +0000 | [diff] [blame] | 93 | "xmlgenerator.py", "README", "TODO", "drv_libxml2.py"] |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 94 | |
| 95 | xslt_files = ["libxslt-api.xml", "libxslt-python-api.xml", |
| 96 | "libxslt.c", "libxsl.py", "libxslt_wrap.h", |
| 97 | "xsltgenerator.py"] |
| 98 | |
| 99 | if 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_type, sys.exc_value |
| 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(): |
Daniel Veillard | a1196ed | 2002-11-23 11:22:49 +0000 | [diff] [blame] | 114 | if WITHDLLS: |
| 115 | result.write(altImport(line)) |
| 116 | else: |
| 117 | result.write(line) |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 118 | for line in generated.readlines(): |
| 119 | result.write(line) |
| 120 | head.close() |
| 121 | generated.close() |
| 122 | result.close() |
| 123 | |
| 124 | with_xslt=0 |
| 125 | if 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_type, sys.exc_value |
| 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 Veillard | a1196ed | 2002-11-23 11:22:49 +0000 | [diff] [blame] | 139 | if WITHDLLS: |
| 140 | result.write(altImport(line)) |
| 141 | else: |
| 142 | result.write(line) |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 143 | for line in generated.readlines(): |
| 144 | result.write(line) |
| 145 | head.close() |
| 146 | generated.close() |
| 147 | result.close() |
| 148 | with_xslt=1 |
| 149 | else: |
| 150 | with_xslt=1 |
| 151 | |
Daniel Veillard | a7084cd | 2002-04-15 17:12:47 +0000 | [diff] [blame] | 152 | if 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 | |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 163 | |
| 164 | descr = "libxml2 package" |
Daniel Veillard | 623a9eb | 2003-01-04 12:47:20 +0000 | [diff] [blame] | 165 | modules = [ 'libxml2', 'drv_libxml2' ] |
Daniel Veillard | a1196ed | 2002-11-23 11:22:49 +0000 | [diff] [blame] | 166 | if WITHDLLS: |
| 167 | modules.append('libxmlmods.__init__') |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 168 | c_files = ['libxml2-py.c', 'libxml.c', 'types.c' ] |
Daniel Veillard | a1196ed | 2002-11-23 11:22:49 +0000 | [diff] [blame] | 169 | includes= [xml_includes, iconv_includes] |
| 170 | libs = [libraryPrefix + "xml2"] + platformLibs |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 171 | macros = [] |
Daniel Veillard | 94bb2f1 | 2003-04-27 22:14:07 +0000 | [diff] [blame] | 172 | if with_threads: |
| 173 | macros.append(('_REENTRANT','1')) |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 174 | if with_xslt == 1: |
| 175 | descr = "libxml2 and libxslt package" |
Daniel Veillard | 9d5ea17 | 2002-11-27 08:02:06 +0000 | [diff] [blame] | 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') |
Daniel Veillard | a1196ed | 2002-11-23 11:22:49 +0000 | [diff] [blame] | 191 | libs.insert(0, libraryPrefix + 'xslt') |
Daniel Veillard | a7084cd | 2002-04-15 17:12:47 +0000 | [diff] [blame] | 192 | includes.append(xslt_includes) |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 193 | modules.append('libxslt') |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 194 | |
| 195 | |
| 196 | extens=[Extension('libxml2mod', c_files, include_dirs=includes, |
Daniel Veillard | a1196ed | 2002-11-23 11:22:49 +0000 | [diff] [blame] | 197 | library_dirs=libdirs, |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 198 | libraries=libs, define_macros=macros)] |
| 199 | if with_xslt == 1: |
Daniel Veillard | 9d5ea17 | 2002-11-27 08:02:06 +0000 | [diff] [blame] | 200 | extens.append(Extension('libxsltmod', xslt_c_files, include_dirs=includes, |
Daniel Veillard | a1196ed | 2002-11-23 11:22:49 +0000 | [diff] [blame] | 201 | library_dirs=libdirs, |
Daniel Veillard | 94bb2f1 | 2003-04-27 22:14:07 +0000 | [diff] [blame] | 202 | libraries=libs, define_macros=macros)) |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 203 | |
| 204 | if missing("MANIFEST"): |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 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: |
| 211 | for file in xslt_files: |
| 212 | manifest.write(file + "\n") |
| 213 | manifest.close() |
| 214 | |
Daniel Veillard | a1196ed | 2002-11-23 11:22:49 +0000 | [diff] [blame] | 215 | if WITHDLLS: |
| 216 | ext_package = "libxmlmods" |
Daniel Veillard | 0d132cf | 2002-12-23 14:43:32 +0000 | [diff] [blame] | 217 | if sys.version >= "2.2": |
| 218 | base = "lib/site-packages/" |
| 219 | else: |
| 220 | base = "" |
| 221 | data_files = [(base+"libxmlmods",dlls)] |
Daniel Veillard | a1196ed | 2002-11-23 11:22:49 +0000 | [diff] [blame] | 222 | else: |
| 223 | ext_package = None |
| 224 | data_files = [] |
| 225 | |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 226 | setup (name = "libxml2-python", |
Daniel Veillard | 9d5ea17 | 2002-11-27 08:02:06 +0000 | [diff] [blame] | 227 | # On *nix, the version number is created from setup.py.in |
| 228 | # On windows, it is set by configure.js |
Daniel Veillard | fa49d87 | 2002-03-09 10:20:00 +0000 | [diff] [blame] | 229 | version = "@LIBXML_VERSION@", |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 230 | description = descr, |
| 231 | author = "Daniel Veillard", |
| 232 | author_email = "veillard@redhat.com", |
| 233 | url = "http://xmlsoft.org/python.html", |
| 234 | licence="MIT Licence", |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 235 | py_modules=modules, |
| 236 | ext_modules=extens, |
Daniel Veillard | a1196ed | 2002-11-23 11:22:49 +0000 | [diff] [blame] | 237 | ext_package=ext_package, |
| 238 | data_files=data_files, |
Daniel Veillard | 0fea6f4 | 2002-02-22 22:51:13 +0000 | [diff] [blame] | 239 | ) |
| 240 | |
| 241 | sys.exit(0) |
| 242 | |