2022-12-07 00:40:01 +03:00
#!/usr/bin/env python3
2002-02-23 01:51:13 +03:00
#
# Setup script for libxml2 and libxslt if found
#
import sys , os
2021-07-22 10:46:38 +03:00
try :
2023-10-23 11:16:34 +03:00
from setuptools import setup , Extension
2022-02-24 20:34:39 +03:00
except ImportError :
2023-10-23 11:16:34 +03:00
try :
# Using distutils, for python < 3.12
from distutils . core import setup , Extension
except ImportError :
# distutils is not present in python 3.12 and greater
print ( " setuptools is required for python >= 3.12 " )
sys . exit ( 1 )
2002-02-23 01:51:13 +03:00
2002-11-27 11:02:06 +03:00
# Below ROOT, we expect to find include, include/libxml2, lib and bin.
2013-11-29 00:42:08 +04:00
# On *nix, it is not needed (but should not harm),
2002-11-27 11:02:06 +03:00
# on Windows, it is set by configure.js.
2013-11-29 00:42:08 +04:00
ROOT = r ' @prefix@ '
2002-11-23 14:22:49 +03:00
2003-04-28 02:14:07 +04:00
# Thread-enabled libxml2
with_threads = @WITH_THREADS @
2021-07-22 10:36:15 +03:00
# Features of libxml2 requiring external DLLs
with_iconv = @WITH_ICONV @
with_zlib = @WITH_ZLIB @
with_lzma = @WITH_LZMA @
with_icu = @WITH_ICU @
icu_series = 69
if icu_series is not None :
icu_series_s = str ( icu_series )
else :
icu_series_s = ' '
# If bundling DLLs, check the following to ensure things are correct
# (Check the value of `icu_series` above as well)
iconv_dll = ' iconv.dll '
zlib_dll = ' zlib1.dll '
lzma_dll = ' liblzma.dll '
icu_dlls = [ ' icuuc %s .dll ' % icu_series_s , ' icudt %s .dll ' % icu_series_s ]
2013-11-29 00:42:08 +04:00
# If this flag is set (windows only),
2002-11-23 14:22:49 +03:00
# a private copy of the dlls are included in the package.
# If this flag is not set, the libxml2 and libxslt
# dlls must be found somewhere in the PATH at runtime.
WITHDLLS = 1 and sys . platform . startswith ( ' win ' )
2002-02-23 01:51:13 +03:00
2022-11-18 12:36:14 +03:00
if WITHDLLS :
def altImport ( s ) :
s = s . replace ( " import libxml2mod " , " from libxmlmods import libxml2mod " )
return s
2002-02-23 01:51:13 +03:00
def missing ( file ) :
if os . access ( file , os . R_OK ) == 0 :
return 1
return 0
2002-04-15 21:12:47 +04:00
try :
2002-11-23 14:22:49 +03:00
HOME = os . environ [ ' HOME ' ]
2002-04-15 21:12:47 +04:00
except :
HOME = " C: "
2002-11-23 14:22:49 +03:00
if sys . platform . startswith ( ' win ' ) :
libraryPrefix = ' lib '
platformLibs = [ ]
else :
libraryPrefix = ' '
platformLibs = [ " m " , " z " ]
2013-11-29 00:42:08 +04:00
# those are examined to find
2002-11-23 14:22:49 +03:00
# - libxml2/libxml/tree.h
# - libxslt/xsltconfig.h
2002-04-15 21:12:47 +04:00
includes_dir = [
" /usr/include " ,
" /usr/local/include " ,
" /opt/include " ,
2002-11-23 14:22:49 +03:00
os . path . join ( ROOT , ' include ' ) ,
2002-04-15 21:12:47 +04:00
HOME
] ;
xml_includes = " "
for dir in includes_dir :
if not missing ( dir + " /libxml2/libxml/tree.h " ) :
xml_includes = dir + " /libxml2 "
2013-11-29 00:42:08 +04:00
break ;
2002-04-15 21:12:47 +04:00
if xml_includes == " " :
2013-03-27 18:40:54 +04:00
print ( " failed to find headers for libxml2: update includes_dir " )
2002-04-15 21:12:47 +04:00
sys . exit ( 1 )
2002-11-23 14:22:49 +03:00
# those are added in the linker search path for libraries
libdirs = [
os . path . join ( ROOT , ' lib ' ) ,
]
2002-02-23 01:51:13 +03:00
xml_files = [ " libxml2-api.xml " , " libxml2-python-api.xml " ,
" libxml.c " , " libxml.py " , " libxml_wrap.h " , " types.c " ,
2013-11-29 00:42:08 +04:00
" xmlgenerator.py " , " README " , " TODO " , " drv_libxml2.py " ]
2002-02-23 01:51:13 +03:00
xslt_files = [ " libxslt-api.xml " , " libxslt-python-api.xml " ,
" libxslt.c " , " libxsl.py " , " libxslt_wrap.h " ,
2013-11-29 00:42:08 +04:00
" xsltgenerator.py " ]
2002-02-23 01:51:13 +03:00
if missing ( " libxml2-py.c " ) or missing ( " libxml2.py " ) :
try :
2013-11-29 00:42:08 +04:00
try :
import xmlgenerator
except :
import generator
2002-02-23 01:51:13 +03:00
except :
2013-11-29 00:42:08 +04:00
print ( " failed to find and generate stubs for libxml2, aborting ... " )
print ( sys . exc_info ( ) [ 0 ] , sys . exc_info ( ) [ 1 ] )
sys . exit ( 1 )
2002-02-23 01:51:13 +03:00
head = open ( " libxml.py " , " r " )
generated = open ( " libxml2class.py " , " r " )
result = open ( " libxml2.py " , " w " )
for line in head . readlines ( ) :
2002-11-23 14:22:49 +03:00
if WITHDLLS :
result . write ( altImport ( line ) )
else :
result . write ( line )
2002-02-23 01:51:13 +03:00
for line in generated . readlines ( ) :
2013-11-29 00:42:08 +04:00
result . write ( line )
2002-02-23 01:51:13 +03:00
head . close ( )
generated . close ( )
result . close ( )
with_xslt = 0
if missing ( " libxslt-py.c " ) or missing ( " libxslt.py " ) :
if missing ( " xsltgenerator.py " ) or missing ( " libxslt-api.xml " ) :
2013-03-27 18:40:54 +04:00
print ( " libxslt stub generator not found, libxslt not built " )
2002-02-23 01:51:13 +03:00
else :
2013-11-29 00:42:08 +04:00
try :
import xsltgenerator
except :
print ( " failed to generate stubs for libxslt, aborting ... " )
print ( sys . exc_info ( ) [ 0 ] , sys . exc_info ( ) [ 1 ] )
else :
head = open ( " libxsl.py " , " r " )
generated = open ( " libxsltclass.py " , " r " )
result = open ( " libxslt.py " , " w " )
for line in head . readlines ( ) :
2002-11-23 14:22:49 +03:00
if WITHDLLS :
result . write ( altImport ( line ) )
else :
result . write ( line )
2013-11-29 00:42:08 +04:00
for line in generated . readlines ( ) :
result . write ( line )
head . close ( )
generated . close ( )
result . close ( )
with_xslt = 1
2002-02-23 01:51:13 +03:00
else :
with_xslt = 1
2002-04-15 21:12:47 +04:00
if with_xslt == 1 :
xslt_includes = " "
for dir in includes_dir :
2013-11-29 00:42:08 +04:00
if not missing ( dir + " /libxslt/xsltconfig.h " ) :
xslt_includes = dir + " /libxslt "
break ;
2002-04-15 21:12:47 +04:00
if xslt_includes == " " :
2013-11-29 00:42:08 +04:00
print ( " failed to find headers for libxslt: update includes_dir " )
with_xslt = 0
2002-04-15 21:12:47 +04:00
2021-07-22 10:36:15 +03:00
if WITHDLLS :
# libxml dlls (expected in ROOT/bin)
dlls = [ ' libxml2.dll ' ]
if with_zlib == 1 :
dlls . append ( zlib_dll )
if with_lzma == 1 :
dlls . append ( lzma_dll )
if with_iconv == 1 :
dlls . append ( iconv_dll )
if with_icu == 1 :
dlls + = icu_dlls
if with_xslt == 1 :
dlls + = [ ' libxslt.dll ' , ' libexslt.dll ' ]
packaged_dlls = [ os . path . join ( ROOT , ' bin ' , dll ) for dll in dlls ]
# create __init__.py for the libxmlmods package
if not os . path . exists ( " libxmlmods " ) :
os . mkdir ( " libxmlmods " )
open ( " libxmlmods/__init__.py " , " w " ) . close ( )
packaged_dlls = [ os . path . join ( ROOT , ' bin ' , dll ) for dll in dlls ]
2002-02-23 01:51:13 +03:00
descr = " libxml2 package "
2003-01-04 15:47:20 +03:00
modules = [ ' libxml2 ' , ' drv_libxml2 ' ]
2002-11-23 14:22:49 +03:00
if WITHDLLS :
modules . append ( ' libxmlmods.__init__ ' )
2002-02-23 01:51:13 +03:00
c_files = [ ' libxml2-py.c ' , ' libxml.c ' , ' types.c ' ]
2021-07-22 10:36:15 +03:00
includes = [ xml_includes ]
2002-11-23 14:22:49 +03:00
libs = [ libraryPrefix + " xml2 " ] + platformLibs
2002-02-23 01:51:13 +03:00
macros = [ ]
2003-04-28 02:14:07 +04:00
if with_threads :
macros . append ( ( ' _REENTRANT ' , ' 1 ' ) )
2002-02-23 01:51:13 +03:00
if with_xslt == 1 :
descr = " libxml2 and libxslt package "
2002-11-27 11:02:06 +03:00
if not sys . platform . startswith ( ' win ' ) :
#
# We are gonna build 2 identical shared libs with merge initializing
# both libxml2mod and libxsltmod
#
c_files = c_files + [ ' libxslt-py.c ' , ' libxslt.c ' ]
xslt_c_files = c_files
macros . append ( ( ' MERGED_MODULES ' , ' 1 ' ) )
else :
#
# On windows the MERGED_MODULE option is not needed
# (and does not work)
#
xslt_c_files = [ ' libxslt-py.c ' , ' libxslt.c ' , ' types.c ' ]
libs . insert ( 0 , libraryPrefix + ' exslt ' )
2002-11-23 14:22:49 +03:00
libs . insert ( 0 , libraryPrefix + ' xslt ' )
2002-04-15 21:12:47 +04:00
includes . append ( xslt_includes )
2002-02-23 01:51:13 +03:00
modules . append ( ' libxslt ' )
extens = [ Extension ( ' libxml2mod ' , c_files , include_dirs = includes ,
2013-11-29 00:42:08 +04:00
library_dirs = libdirs ,
libraries = libs , define_macros = macros ) ]
2002-02-23 01:51:13 +03:00
if with_xslt == 1 :
2002-11-27 11:02:06 +03:00
extens . append ( Extension ( ' libxsltmod ' , xslt_c_files , include_dirs = includes ,
2013-11-29 00:42:08 +04:00
library_dirs = libdirs ,
2003-04-28 02:14:07 +04:00
libraries = libs , define_macros = macros ) )
2002-02-23 01:51:13 +03:00
if missing ( " MANIFEST " ) :
manifest = open ( " MANIFEST " , " w " )
manifest . write ( " setup.py \n " )
for file in xml_files :
manifest . write ( file + " \n " )
if with_xslt == 1 :
2013-11-29 00:42:08 +04:00
for file in xslt_files :
manifest . write ( file + " \n " )
2002-02-23 01:51:13 +03:00
manifest . close ( )
2002-11-23 14:22:49 +03:00
if WITHDLLS :
ext_package = " libxmlmods "
2002-12-23 17:43:32 +03:00
if sys . version > = " 2.2 " :
base = " lib/site-packages/ "
else :
base = " "
2021-07-22 10:36:15 +03:00
data_files = [ ( base + " libxmlmods " , packaged_dlls ) ]
2002-11-23 14:22:49 +03:00
else :
ext_package = None
data_files = [ ]
2002-02-23 01:51:13 +03:00
setup ( name = " libxml2-python " ,
2002-11-27 11:02:06 +03:00
# On *nix, the version number is created from setup.py.in
# On windows, it is set by configure.js
2002-03-09 13:20:00 +03:00
version = " @LIBXML_VERSION@ " ,
2002-02-23 01:51:13 +03:00
description = descr ,
author = " Daniel Veillard " ,
author_email = " veillard@redhat.com " ,
2022-02-14 00:52:53 +03:00
url = " https://gitlab.gnome.org/GNOME/libxml2 " ,
2002-02-23 01:51:13 +03:00
licence = " MIT Licence " ,
py_modules = modules ,
ext_modules = extens ,
2002-11-23 14:22:49 +03:00
ext_package = ext_package ,
data_files = data_files ,
2002-02-23 01:51:13 +03:00
)
sys . exit ( 0 )