-
Notifications
You must be signed in to change notification settings - Fork 17
/
setup.py
executable file
·256 lines (223 loc) · 9.5 KB
/
setup.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/usr/bin/env python
import logging
import sys
import re
import os
import json
import shutil
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from setup_support import SetupApp, Config
PYTHON_DATA_SCRIPT = """
import json
from sysconfig import get_config_vars, get_path, get_path_names
if "platinclude" in get_path_names():
plat_include_key = "platinclude"
else:
plat_include_key = "include"
result = {'config_vars': get_config_vars(),
'python_inc': get_path("include"),
'python_inc_plat': get_path(plat_include_key),
'prefix': get_path("data")}
print(json.dumps(result))
"""
def fetch_python_config(config):
logging.info("Fetch Python information...")
python_output = config.run(
config.data["python_exec"], "-c", PYTHON_DATA_SCRIPT, grab=True
)
python_data = json.loads(python_output)
config_vars = python_data["config_vars"]
python_version = config_vars["VERSION"]
python_ldversion = config_vars.get("LDVERSION", python_version)
logging.info(" %-24s %s", "Python version:", python_version)
# Current python location
current_prefix = python_data["prefix"]
# Fetch prefix during the build process. Some paths of interest might
# still reference a location used during the Python build process.
build_prefix = (
[sys.prefix]
+ re.findall(r"'--prefix=([^']+)'", config_vars.get("CONFIG_ARGS", ""))
)[-1]
def relocate(path):
if os.path.isabs(path):
rel_path = os.path.relpath(path, build_prefix)
if not rel_path.startswith(os.pardir):
# If the input path is relative to the original build
# directory, replace build prefix by the current one.
return os.path.join(current_prefix, rel_path)
else:
# Otherwise, return it unchanged
return path
else:
# The input path is relative so assume it's relative to the current
# python prefix.
return os.path.join(current_prefix, path)
# Retrieve cflags, and linker flags
static_dir = relocate(config_vars.get("LIBPL", "libs"))
logging.info(" %-24s %s", "Static dir", static_dir)
shared_dir = relocate(config_vars.get("LIBDIR", "."))
logging.info(" %-24s %s", "Shared dir", shared_dir)
# Add first relocated include dirs followed by non-relocated version.
# Indeed, when using venv and maybe virtualenv, includes are not copied.
cflags = "-I" + relocate(python_data["python_inc"])
if python_data["python_inc"] != python_data["python_inc_plat"]:
cflags += " -I" + relocate(python_data["python_inc_plat"])
cflags += " -I" + python_data["python_inc_plat"]
cflags += " -I" + python_data["python_inc"]
logging.info(" %-24s %s", "CFLAGS", cflags)
if python_version.startswith("3"):
# In python 3.x MODLIBS seems to drag in too many libraries
python_libs = [
config_vars[v]
for v in ("LIBS", "SYSLIBS")
if v in config_vars and config_vars[v]
]
else:
python_libs = [
config_vars[v]
for v in ("LIBS", "SYSLIBS", "MODLIBS")
if v in config_vars and config_vars[v]
]
python_libs = " ".join(python_libs)
python_shared_libs = "-L%s -lpython%s %s" % (
shared_dir,
python_ldversion,
python_libs,
)
python_static_libs = python_libs
libpython_a = os.path.join(
static_dir, config_vars.get("LIBRARY", "libpython%s.a" % python_version)
)
if os.path.isfile(libpython_a):
config.set_data("GNATCOLL_PYTHON_STATIC_LIB", libpython_a, sub="gprbuild")
else:
logging.info("static python library not found")
if sys.platform.startswith("linux"):
# On Linux platform, even when linking with the static libpython,
# symbols not used by the application itself should be exported so
# that shared library present in Python can use the Python C API.
python_static_libs += " -export-dynamic"
python_shared_libs += " -export-dynamic"
logging.info(" %-24s %s", "Shared linker flags", python_shared_libs)
logging.info(" %-24s %s", "Static linker flags", python_static_libs)
# User does not have the choice between linking with static libpython
# and shared libpython. If --enable-shared or --enable-framework was
# passed to Python's configure during Python build, then we should
# link with the shared libpython, otherwise with the static one.
# Indeed otherwise some C modules might not work as expected or even
# crash. On Windows always link with shared version of libpython
# (if the static is present, this is just an indirection to the shared)
if (
"--enable-shared" in config_vars.get("CONFIG_ARGS", "")
or "--enable-framework" in config_vars.get("CONFIG_ARGS", "")
or sys.platform.startswith("win")
):
logging.info("Force link to shared python library")
config.set_data("GNATCOLL_PYTHON_LIBS", python_shared_libs, sub="gprbuild")
config.set_data("GNATCOLL_LIBPYTHON_KIND", "shared", sub="gprbuild")
else:
logging.info("Force link to static python library")
config.set_data("GNATCOLL_PYTHON_LIBS", python_static_libs, sub="gprbuild")
config.set_data("GNATCOLL_LIBPYTHON_KIND", "static", sub="gprbuild")
config.set_data("GNATCOLL_PYTHON_CFLAGS", cflags, sub="gprbuild")
class GNATCollPython(SetupApp):
name = "gnatcoll_python"
project = "gnatcoll_python.gpr"
description = "GNATColl Python bindings"
def create(self):
super(GNATCollPython, self).create()
self.build_cmd.add_argument(
"--python-exec",
help="set python executable location",
metavar="PATH",
default=sys.executable,
)
self.build_cmd.add_argument(
"--debug",
help="build project in debug mode",
action="store_true",
default=False,
)
def update_config(self, config, args):
# Fetch python information
config.set_data("python_exec", args.python_exec)
fetch_python_config(config)
logging.info(
"%-26s %s", "Libraries kind", ", ".join(config.data["library_types"])
)
# Set library version
with open(
os.path.join(config.source_dir, "..", "version_information"), "r"
) as fd:
version = fd.read().strip()
config.set_data("GNATCOLL_VERSION", version, sub="gprbuild")
logging.info("%-26s %s", "Version", version)
# Set build mode
config.set_data("BUILD", "DEBUG" if args.debug else "PROD", sub="gprbuild")
logging.info("%-26s %s", "Build mode", config.data["gprbuild"]["BUILD"])
# Set GNATCOLL_OS
if "darwin" in config.data["canonical_target"]:
gnatcoll_os = "osx"
elif "windows" in config.data["canonical_target"]:
gnatcoll_os = "windows"
else:
# Assume this is an Unix system
gnatcoll_os = "unix"
config.set_data("GNATCOLL_OS", gnatcoll_os, sub="gprbuild")
def variants(self, config, cmd):
result = []
for library_type in config.data["library_types"]:
gpr_vars = {
"LIBRARY_TYPE": library_type,
"XMLADA_BUILD": library_type,
"GPR_BUILD": library_type,
}
if cmd == "install":
result.append(
(
["--build-name=%s" % library_type, "--build-var=LIBRARY_TYPE"],
gpr_vars,
)
)
else:
result.append(([], gpr_vars))
return result
def install(self, args):
config = Config()
has_static_python = "GNATCOLL_PYTHON_STATIC_LIB" in config.data["gprbuild"]
if not has_static_python:
super(GNATCollPython, self).install(args)
else:
python_la = config.data["gprbuild"]["GNATCOLL_PYTHON_STATIC_LIB"]
prefix = args.prefix
if prefix is None:
prefix = config.data["prefix"]
rel_target = os.path.join(
"..", "..", "lib", "gnatcoll_python.static", os.path.basename(python_la)
)
abs_target = os.path.join(
prefix, "lib", "gnatcoll_python.static", os.path.basename(python_la)
)
shutil.copy(config.json_cache, config.json_cache + ".backup")
try:
# Temporary change the configuration to set a relative path to the
# static Python library.
config.set_data(
"GNATCOLL_PYTHON_STATIC_LIB", rel_target, sub="gprbuild"
)
config.save_data()
# Perform the installation
super(GNATCollPython, self).install(args)
# Copy over the libpython*.a
logging.info("Copy static libpython into target lib")
if not os.path.isdir(os.path.dirname(abs_target)):
os.mkdir(abs_target)
result = shutil.copy(python_la, abs_target)
logging.info(f"Python static lib in {result}")
finally:
# Restore the configuration cache
shutil.copy(config.json_cache + ".backup", config.json_cache)
os.unlink(config.json_cache + ".backup")
if __name__ == "__main__":
app = GNATCollPython()
sys.exit(app.run())