This repository has been archived by the owner on Feb 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
xmlstarlet_build.py
164 lines (137 loc) · 5.74 KB
/
xmlstarlet_build.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python
"""XMLStarlet CFFI Python bindings build script."""
import os
import subprocess
import sys
from glob import glob
from cffi import FFI
ROOT_DIR = os.path.dirname(os.path.realpath(__file__))
SOURCE_DIR = os.path.join(ROOT_DIR, "xmlstarlet")
C_SOURCE_DIR = os.path.join(SOURCE_DIR, "src")
# Ensure the git source directory is not dirty (e.g. when developing locally)
print(subprocess.getoutput("git checkout -- ./xmlstarlet/ || true"))
# Make sure all cases where stdlib.h exit() is called are replaced with
# returns as appropriate, otherwise any calls with non-zero exit code
# will terminate the interpreter!
print(subprocess.getoutput("patch -p1 < ./xmlstarlet/no-exit.patch || true"))
if os.name != "nt":
HAVE_CONFIG = os.path.exists(os.path.join(SOURCE_DIR, "config.h"))
HAVE_CONFIGURE = os.path.exists(os.path.join(SOURCE_DIR, "configure"))
HAVE_MAKEFILE = os.path.exists(os.path.join(SOURCE_DIR, "Makefile"))
if not HAVE_CONFIGURE:
print("Generating `configure` with `autoreconf`...")
exit_code, output = subprocess.getstatusoutput("cd ./xmlstarlet/ && autoreconf -sif && cd ..")
print(output)
if exit_code != 0:
sys.exit(exit_code)
HAVE_CONFIGURE = os.path.exists(os.path.join(SOURCE_DIR, "configure"))
LIBRARY_DIRS = ["/usr/lib"]
LIBRARIES = ["xml2", "xslt", "exslt"]
SOURCES = [
os.path.relpath(s, ROOT_DIR)
for s in glob(os.path.join(SOURCE_DIR, "**"), recursive=True)
if s.endswith(".c") and "_xmlstarlet" not in s and "win32" not in s
]
EXTRA_CFLAGS = (
subprocess.getoutput("xml2-config --cflags").split()
+ subprocess.getoutput("xslt-config --cflags").split()
+ [
"-Wno-error=implicit-function-declaration"
# needed to build on macos-10.15 for some reason
]
)
EXTRA_LDFLAGS = (
subprocess.getoutput("xml2-config --libs").split() + subprocess.getoutput("xslt-config --libs").split()
)
LIBRARY_DIRS += [ld.replace("-L", "") for ld in EXTRA_LDFLAGS if ld.startswith("-L")]
LIBRARIES += [lb.replace("-l", "") for lb in EXTRA_LDFLAGS if lb.startswith("-l")]
INCLUDE_DIRS = [SOURCE_DIR, C_SOURCE_DIR] + [d.replace("-I", "") for d in EXTRA_CFLAGS if d.startswith("-I")]
LIBRARIES += [lb.replace("-l", "") for lb in EXTRA_CFLAGS if lb.startswith("-l")]
os.environ.update(
CONFIG_PREFIX=os.environ.get("CONFIG_PREFIX", "/usr"),
INCLUDE_PATH=os.environ.get("INCLUDE_PATH", "/usr/include"),
CFLAGS=os.environ.get("CFLAGS", " ".join(EXTRA_CFLAGS)),
)
if not (HAVE_CONFIG or HAVE_MAKEFILE) and HAVE_CONFIGURE:
print("Running `./configure` to create a Makefile...")
prefix = os.environ["CONFIG_PREFIX"]
includedir = os.environ["INCLUDE_PATH"]
exit_code, output = subprocess.getstatusoutput(
"cd ./xmlstarlet/ && " f"./configure --prefix={prefix} --includedir={includedir} && cd .."
)
print(output)
if exit_code != 0:
sys.exit(exit_code)
HAVE_CONFIG = os.path.exists(os.path.join(SOURCE_DIR, "config.h"))
HAVE_MAKEFILE = os.path.exists(os.path.join(SOURCE_DIR, "Makefile"))
if HAVE_MAKEFILE:
print("Compiling xmlstarlet C sources...")
exit_code, output = subprocess.getstatusoutput("cd ./xmlstarlet/ && make -j && make check && cd ..")
print(output)
if exit_code != 0:
sys.exit(exit_code)
else:
os.environ["PREFIX"] = "C:\\opt" # configured by setup_msvc.bat.
SOURCES = [
os.path.relpath(s, ROOT_DIR)
for s in glob(os.path.join(C_SOURCE_DIR, "**"), recursive=True)
if s.endswith(".c") and "_xmlstarlet" not in s and ("win32_xml_ls.c" in s or "xml_ls.c" not in s)
]
INCLUDE_DIRS = [
SOURCE_DIR,
C_SOURCE_DIR,
os.path.join(os.environ["PREFIX"], "include"),
os.path.join(os.environ["PREFIX"], "include", "libxml2"),
]
LIBRARY_DIRS = [os.path.join(os.environ["PREFIX"], "lib")]
LIBRARIES = ["libxml2_a", "libxslt_a", "libexslt_a", "wsock32", "ws2_32", "shell32"]
if os.environ.get("Platform", "") == "x86":
LIBRARIES += ["vcruntime"]
sources = sorted(set(SOURCES))
include_dirs = sorted(set(INCLUDE_DIRS))
libraries = sorted(set(LIBRARIES))
library_dirs = sorted(set(LIBRARY_DIRS))
print("Build settings:")
print("-" * 40)
print("sources:\n\t{}".format("\n\t".join(sources)))
print("include_dirs:\n\t{}".format("\n\t".join(include_dirs)))
print("libraries:\n\t{}".format("\n\t".join(libraries)))
print("library_dirs:\n\t{}".format("\n\t".join(library_dirs)))
print("-" * 40)
FFIBUILDER = FFI()
# set_source() gives the name of the python extension module to
# produce, and some C source code as a string. This C code needs
# to make the declarated functions, types and globals available,
# so it is often just the "#include".
FFIBUILDER.set_source(
"xmlstarlet._xmlstarlet",
"""
#include <libxml/xmlversion.h>
#include <xmlstar.h>
#include <config.h>
#include <libexslt/exslt.h>
""",
sources=sources,
include_dirs=include_dirs,
libraries=libraries,
library_dirs=library_dirs,
)
# cdef() expects a single string declaring the C types, functions and
# globals needed to use the shared object. It must be in valid C syntax.
FFIBUILDER.cdef(
"""
int c14nMain(int argc, char **argv);
int depyxMain(int argc, char **argv);
int edMain(int argc, char **argv);
int elMain(int argc, char **argv);
int escMain(int argc, char **argv, int escape);
int foMain(int argc, char **argv);
int lsMain(int argc, char **argv);
int pyxMain(int argc, char **argv);
int selMain(int argc, char **argv);
int trMain(int argc, char **argv);
int valMain(int argc, char **argv);
"""
)
if __name__ == "__main__":
FFIBUILDER.compile(verbose=True)