-
Notifications
You must be signed in to change notification settings - Fork 232
/
setup.py
195 lines (152 loc) · 5.66 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
#!/usr/bin/env python
# Copyright 2013 Donald Stufft and individual contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import errno
import functools
import glob
import os
import os.path
import platform
import shutil
import subprocess
import sys
from sysconfig import get_config_vars
from setuptools import Distribution, setup
from setuptools.command.build_clib import build_clib as _build_clib
from setuptools.command.build_ext import build_ext as _build_ext
if platform.python_implementation() == "PyPy":
if sys.pypy_version_info < (2, 6):
raise RuntimeError(
"PyNaCl is not compatible with PyPy < 2.6. Please "
"upgrade PyPy to use this library."
)
def here(*paths):
return os.path.relpath(os.path.join(*paths))
def abshere(*paths):
return os.path.abspath(here(*paths))
sodium = functools.partial(here, "src/libsodium/src/libsodium")
sys.path.insert(0, abshere("src"))
import nacl # noqa
def use_system():
install_type = os.environ.get("SODIUM_INSTALL")
if install_type == "system":
# If we are forcing system installs, don't compile the bundled one
return True
else:
# By default we just use the bundled copy
return False
class Distribution(Distribution):
def has_c_libraries(self):
return not use_system()
class build_clib(_build_clib):
def get_source_files(self):
files = glob.glob(here("src/libsodium/*"))
files += glob.glob(here("src/libsodium/*/*"))
files += glob.glob(here("src/libsodium/*/*/*"))
files += glob.glob(here("src/libsodium/*/*/*/*"))
files += glob.glob(here("src/libsodium/*/*/*/*/*"))
files += glob.glob(here("src/libsodium/*/*/*/*/*/*"))
return files
def build_libraries(self, libraries):
raise Exception("build_libraries")
def check_library_list(self, libraries):
raise Exception("check_library_list")
def get_library_names(self):
return ["sodium"]
def run(self):
if use_system():
return
# use Python's build environment variables
build_env = {
key: val
for key, val in get_config_vars().items()
if key in ("LDFLAGS", "CFLAGS", "CC", "CCSHARED", "LDSHARED")
and key not in os.environ
}
os.environ.update(build_env)
# Ensure our temporary build directory exists
build_temp = os.path.abspath(self.build_temp)
try:
os.makedirs(build_temp)
except OSError as e:
if e.errno != errno.EEXIST:
raise
# Ensure all of our executable files have their permission set
for filename in [
"src/libsodium/autogen.sh",
"src/libsodium/configure",
]:
os.chmod(here(filename), 0o755)
if not shutil.which("make"):
raise Exception("ERROR: The 'make' utility is missing from PATH")
# Locate our configure script
configure = abshere("src/libsodium/configure")
# Run ./configure
configure_flags = [
"--disable-shared",
"--enable-static",
"--disable-debug",
"--disable-dependency-tracking",
"--with-pic",
]
if platform.system() == "SunOS":
# On Solaris, libssp doesn't link statically and causes linker
# errors during import
configure_flags.append("--disable-ssp")
if os.environ.get("SODIUM_INSTALL_MINIMAL"):
configure_flags.append("--enable-minimal")
subprocess.check_call(
[configure]
+ configure_flags
+ ["--prefix", os.path.abspath(self.build_clib)],
cwd=build_temp,
)
make_args = os.environ.get("LIBSODIUM_MAKE_ARGS", "").split()
# Build the library
subprocess.check_call(["make"] + make_args, cwd=build_temp)
# Check the build library
subprocess.check_call(["make", "check"] + make_args, cwd=build_temp)
# Install the built library
subprocess.check_call(["make", "install"] + make_args, cwd=build_temp)
class build_ext(_build_ext):
def run(self):
if self.distribution.has_c_libraries():
build_clib = self.get_finalized_command("build_clib")
self.include_dirs.append(
os.path.join(build_clib.build_clib, "include"),
)
self.library_dirs.insert(
0,
os.path.join(build_clib.build_clib, "lib64"),
)
self.library_dirs.insert(
0,
os.path.join(build_clib.build_clib, "lib"),
)
return _build_ext.run(self)
README = open("README.rst").read()
INSTALL = open("INSTALL.rst").read()
CHANGELOG = open("CHANGELOG.rst").read()
setup(
long_description="\n".join((README, INSTALL, CHANGELOG)),
url=nacl.__uri__,
package_dir={"": "src"},
packages=["nacl", "nacl.pwhash", "nacl.bindings"],
package_data={"nacl": ["py.typed"]},
ext_package="nacl",
cffi_modules=["src/bindings/build.py:ffi"],
cmdclass={"build_clib": build_clib, "build_ext": build_ext},
distclass=Distribution,
zip_safe=False,
)