-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsetup.py
264 lines (236 loc) · 11 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
257
258
259
260
261
262
263
264
"""Holds all relevant information for packaging and publishing to PyPI."""
import os
import platform
import re
import subprocess # nosec blacklist
import sys
from distutils.version import LooseVersion
from pathlib import Path
from typing import List
from setuptools import Extension, setup
from setuptools.command.build_ext import build_ext
# DO NOT CHANGE
# THIS IS UPDATED AUTOMATICALLY THROUGH PYTHON-SEMANTIC-RELEASE
VERSION = "0.0.0"
with open("INFO.md", encoding="utf-8") as info_file:
info = info_file.read()
with open("CHANGELOG.md", encoding="utf-8") as changelog_file:
changelog = changelog_file.read()
# List any runtime requirements here
requirements = ["numpy", "cmake", "xmltodict", "validators"]
class CMakeExtension(Extension):
"""Manages CMake specifics of this module."""
def __init__(self, name: str, sourcedir: str = ""):
Extension.__init__(self, name, sources=[])
self.sourcedir = os.path.abspath(sourcedir)
class CMakeBuild(build_ext):
"""Manages CMake build specifics of this module."""
def run(self) -> None:
"""Runs CMake build."""
try:
out = subprocess.check_output(["cmake", "--version"]) # nosec
except OSError as exc:
raise RuntimeError(
f"CMake must be installed and available at PATH ({os.environ.get('PATH')}) "
f"to build the following extensions: {', '.join(e.name for e in self.extensions)}"
) from exc
cmake_version = LooseVersion(
re.search(r"version\s*([\d.]+)", out.decode()).group(1) # type: ignore[union-attr]
)
if platform.system() == "Windows":
cmake_version = LooseVersion(
re.search(r"version\s*([\d.]+)", out.decode()).group(1) # type: ignore[union-attr]
)
if cmake_version < "3.1.0":
raise RuntimeError("CMake >= 3.1.0 is required on Windows")
for ext in self.extensions:
self.build_extension(ext)
def build_extension(self, ext): # type: ignore[no-untyped-def]
"""Builds CMake extension."""
path_var = os.environ["PATH"]
path_var = str(Path(sys.executable).parent) + ":" + path_var
env = dict(os.environ.copy(), PATH=path_var)
extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
cmake_args = [
"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=" + extdir,
"-DPYTHON_EXECUTABLE=" + sys.executable, # used for pybind11
]
cfg = "Debug" if self.debug else "Release"
build_args = ["--config", cfg]
cmake_args += ["-DLIBCZI_BUILD_DYNLIB=OFF"] # we don't need the dynamic library
cmake_args += ["-DLIBCZI_BUILD_UNITTESTS=OFF"] # also, we don't need the unit tests
cmake_args += ["-DLIBCZI_BUILD_CURL_BASED_STREAM=ON"] # and we want a version which is "libcurl-enabled"
cmake_args += ["-DPYLIBCZIRW_VERSION=" + VERSION] # Have the same version as the Python package
if platform.system() == "Windows":
cmake_args += [f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{cfg.upper()}={extdir}"]
cmake_args += ["-DVCPKG_TARGET_TRIPLET=x64-windows-static"]
vcpkg_installation_root = os.environ.get("VCPKG_INSTALLATION_ROOT", "C:\\vcpkg")
if os.path.exists(vcpkg_installation_root):
check_and_install_packages(
packages=["curl[ssl]", "eigen3"],
triplet="x64-windows-static",
vcpkg_root=vcpkg_installation_root,
)
cmake_args += [
"-DLIBCZI_BUILD_PREFER_EXTERNALPACKAGE_LIBCURL=ON"
] # instruct to use the package-manager provided libcurl
cmake_args += [
"-DLIBCZI_BUILD_PREFER_EXTERNALPACKAGE_EIGEN3=ON"
] # instruct to use the package-manager provided eigen3
cmake_args += [
"-DCMAKE_TOOLCHAIN_FILE="
+ os.path.join(
vcpkg_installation_root,
"scripts",
"buildsystems",
"vcpkg.cmake",
)
]
else:
raise RuntimeError("vcpkg installation not found, please define your VCPKG_INSTALLATION_ROOT path")
build_args += ["--", "/m"]
else: # Linux
# Get the value of the environment variable
manylinux_env_variable = os.environ.get("AUDITWHEEL_PLAT", "").lower()
if "manylinux" in manylinux_env_variable:
# When running in manylinux-container, we want to build openssl ourselves and link it statically.
# On the CI/CD-server, we set the variable "BUILDWHEEL_STATIC_OPENSSL" in order to instruct running a
# local build of openSSL (and instruct the libCZI-build to use it)
print("Building static openssl and zlib dependencies locally.")
subprocess.run( # nosec
"cd /tmp && "
"git clone --branch v1.3 https://github.com/madler/zlib.git && "
"cd zlib && "
"CFLAGS=-fPIC ./configure --static && "
"make -j2 && "
"make install",
shell=True, # nosec
check=False,
)
subprocess.run( # nosec
"cd /tmp && "
"git clone --branch openssl-3.2.0 https://github.com/openssl/openssl.git && "
"cd openssl && "
"mkdir build && "
"./config no-shared -static zlib -fPIC -L/usr/lib no-docs no-tests && "
"make -j2",
shell=True, # nosec
check=False,
)
cmake_args += [
"-DOPENSSL_USE_STATIC_LIBS=TRUE"
] # instruct to use the static version of libssl and libcrypto
cmake_args += ["-DOPENSSL_ROOT_DIR=/tmp/openssl"]
cmake_args += ["-DZLIB_USE_STATIC_LIBS=TRUE"]
# Test install curl using vcpkg on linux
print("env root is: " + os.environ.get("VCPKG_INSTALLATION_ROOT", ""))
vcpkg_installation_root = os.environ.get("VCPKG_INSTALLATION_ROOT", r"/usr/local/share/vcpkg")
print("set env root is: " + vcpkg_installation_root)
test = os.path.exists(vcpkg_installation_root)
print(f"path exists is: {test} ")
if os.path.exists(vcpkg_installation_root):
check_and_install_packages(
packages=["curl[ssl]"],
triplet="x64-linux",
vcpkg_root=vcpkg_installation_root,
)
cmake_args += [
"-DCMAKE_TOOLCHAIN_FILE="
+ os.path.join(
vcpkg_installation_root,
"scripts",
"buildsystems",
"vcpkg.cmake",
)
]
cmake_args += [
"-DLIBCZI_BUILD_PREFER_EXTERNALPACKAGE_LIBCURL=ON"
] # if curl is available via vcpkg, then instruct to use the package-manager provided libcurl
else:
print("Pacakge manager missing, attempting to build libcurl dependency locally.")
cmake_args += [
"-DLIBCZI_BUILD_PREFER_EXTERNALPACKAGE_LIBCURL=OFF"
] # otherwise, we try to build libcurl ourselves (note: probably requires libssl-dev to be installed)
cmake_args += ["-DCMAKE_BUILD_TYPE=" + cfg]
build_args += ["--", "-j2"]
env["CXXFLAGS"] = '{} -DVERSION_INFO=\\"{}\\"'.format( # pylint: disable=consider-using-f-string
env.get("CXXFLAGS", ""), self.distribution.get_version()
)
if not os.path.exists(self.build_temp):
os.makedirs(self.build_temp)
if self.debug:
print("cmake build path: " + self.build_temp)
print("cmake compile: " + " ".join(["cmake", str(ext.sourcedir), str(cmake_args)]))
subprocess.check_call(["cmake", ext.sourcedir] + cmake_args, cwd=self.build_temp, env=env) # nosec
if self.debug:
print(" ".join(["cmake build:", "cmake", "--build", ".", "--target", "_pylibCZIrw", str(build_args)]))
subprocess.check_call( # nosec
["cmake", "--build", ".", "--target", "_pylibCZIrw"] + build_args,
cwd=self.build_temp,
env=env,
)
def check_and_install_packages(packages: List[str], triplet: str, vcpkg_root: str) -> None:
"""Checks and installs required packages."""
for package in packages:
vcpkg_executable = os.path.join(vcpkg_root, "vcpkg")
result = subprocess.run( # nosec
[
vcpkg_executable,
"list",
package,
f"--triplet={triplet}",
f"--vcpkg-root={vcpkg_root}",
],
capture_output=True,
text=True,
check=False,
)
if package in result.stdout:
print(f"{package} is already installed.")
else:
print(f"Installing {package}")
subprocess.run( # nosec
[
vcpkg_executable,
"install",
package,
f"--triplet={triplet}",
f"--vcpkg-root={vcpkg_root}",
],
check=False,
)
print("Installations complete")
setup(
name="pylibCZIrw",
version=VERSION,
author="Sebastian Soyer",
author_email="[email protected]",
description="A python wrapper around the libCZI C++ library with reading and writing functionality.",
long_description="\n".join([info, changelog]),
long_description_content_type="text/markdown",
# See https://setuptools.pypa.io/en/latest/userguide/datafiles.html
include_package_data=True,
keywords="czi, imaging",
ext_modules=[CMakeExtension("_pylibCZIrw")],
packages=["pylibCZIrw"],
cmdclass={"build_ext": CMakeBuild},
install_requires=requirements,
# we require at least python version 3.7
python_requires=">=3.7,<3.12",
license_files=["COPYING", "COPYING.LESSER", "NOTICE"],
# Classifiers help users find your project by categorizing it.
# For a list of valid classifiers, see https://pypi.org/classifiers/
classifiers=[
"Development Status :: 5 - Production/Stable",
"Topic :: Scientific/Engineering :: Image Processing",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)",
"Operating System :: Microsoft :: Windows",
"Operating System :: Unix",
],
zip_safe=False,
)