-
Notifications
You must be signed in to change notification settings - Fork 9
/
setup.py
194 lines (172 loc) · 7.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
# This file is part of pylibczi.
# Copyright (c) 2018 Center of Advanced European Studies and Research (caesar)
#
# pylibczi is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pylibczi is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with pylibczi. If not, see <https://www.gnu.org/licenses/>.
from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext
from distutils.command.clean import clean
import sys, os, shutil
import glob
import numpy
import sysconfig
import platform
import subprocess
# several platform specifc build options
platform_ = platform.system()
architecture = platform.architecture()
# to statically link libCZI
build_static = (platform_ != 'Windows')
# libczi cloned as submodule, get paths to library and build locations.
script_dir = os.path.dirname(os.path.abspath(__file__))
libczi_dir = os.path.join(script_dir, 'libCZI')
build_temp = os.path.join(libczi_dir,'build')
include_libCZI = os.path.join(libczi_dir, 'Src')
lib_libCZI = os.path.join(build_temp, 'Src', 'libCZI')
lib_JxrDecode = os.path.join(build_temp, 'Src', 'JxrDecode')
if platform_ == 'Windows':
if build_static:
# xxx - does not work, not sure why
# copy libCZI dll using data_files instead
libCZI_win_release = 'static\ Release'
else:
libCZI_win_release = 'Release'
win_arch = 'x64' if architecture[0]=='64bit' else 'x86'
lib_libCZI = os.path.join(lib_libCZI, libCZI_win_release)
lib_JxrDecode = os.path.join(lib_JxrDecode, libCZI_win_release)
def build_libCZI():
env = os.environ.copy()
cmake_args = []
build_args = []
if platform_ == 'Windows':
cmake_args += ['-DCMAKE_GENERATOR_PLATFORM=' + win_arch]
build_args += ['--config', libCZI_win_release]
else:
cmake_args += ['-DCMAKE_BUILD_TYPE:STRING=Release']
if not os.path.exists(build_temp):
os.makedirs(build_temp)
def run_cmake(cmake_exe):
config_cmd_list = [cmake_exe, libczi_dir] + cmake_args
build_cmd_list = [cmake_exe, '--build', '.'] + build_args
if platform_ == 'Windows':
print(subprocess.list2cmdline(config_cmd_list))
print(subprocess.list2cmdline(build_cmd_list))
subprocess.check_call(config_cmd_list, cwd=build_temp, env=env)
subprocess.check_call(build_cmd_list, cwd=build_temp, env=env)
try:
# try to use the pip installed cmake first
run_cmake(os.path.join(os.path.dirname(sys.executable), 'cmake'))
except OSError:
# xxx - anaconda windows pip install cmake goes into a Scripts dir
# better option here?
run_cmake('cmake')
class specialized_clean(clean):
"""Subclass of clean subcommand to clean libCZI.
"""
def run(self, *args, **kwargs):
print('Remove libCZI build dir')
shutil.rmtree(build_temp, ignore_errors=True)
clean.run(self, *args, **kwargs)
def safe_get_env_var_list(var):
vlist = sysconfig.get_config_var(var)
return ([] if vlist is None else vlist.split())
# platform specific compiler / linker options
extra_compile_args = safe_get_env_var_list('CFLAGS')
extra_link_args = safe_get_env_var_list('LDFLAGS')
if platform_ == 'Windows':
extra_compile_args += safe_get_env_var_list('CL')
extra_compile_args += safe_get_env_var_list('_CL_')
extra_link_args += safe_get_env_var_list('LINK')
extra_link_args += safe_get_env_var_list('_LINK_')
extra_compile_args += ['/Ox']
else:
extra_compile_args += ["-std=c++11", "-Wall", "-O3"]
if platform_ == 'Linux':
extra_compile_args += ["-fPIC"]
if build_static:
# need to link with g++ linker for static libstdc++ to work
os.environ["LDSHARED"] = os.environ["CXX"] if 'CXX' in os.environ else 'g++'
extra_link_args += ['-static-libstdc++', '-shared']
#extra_link_args += ["-Wl,--no-undefined"] # will not work with manylinux
elif platform_ == 'Darwin':
mac_ver = platform.mac_ver()[0] # xxx - how to know min mac version?
extra_compile_args += ["-stdlib=libc++", "-mmacosx-version-min="+mac_ver]
extra_link_args += extra_compile_args
include_dirs = [numpy.get_include(), include_libCZI]
static_libraries = []
static_lib_dirs = []
libraries = []
library_dirs = []
extra_objects = []
if build_static:
static_libraries += ['libCZIStatic', 'JxrDecodeStatic']
static_lib_dirs += [lib_libCZI, lib_JxrDecode]
else:
libraries += ['libCZI']
library_dirs += [lib_libCZI]
# second answer at
# https://stackoverflow.com/questions/4597228/how-to-statically-link-a-library-when-compiling-a-python-module-extension
if platform_ == 'Windows':
libraries.extend(static_libraries)
library_dirs.extend(static_lib_dirs)
else: # POSIX
extra_objects += ['{}/lib{}.a'.format(d, l) for d,l in zip(static_lib_dirs, static_libraries)]
include_dirs.append('/usr/local/include')
library_dirs.append('/usr/local/lib')
sources = ['_pylibczi.cpp']
version = open("pylibczi/_version.py").readlines()[-1].split()[-1].strip("\"'")
module1 = Extension('_pylibczi',
define_macros = [('PYLIBCZI_VERSION', version),],
include_dirs = include_dirs,
libraries = libraries,
library_dirs = library_dirs,
sources = [os.path.join('_pylibczi',x) for x in sources],
extra_compile_args=extra_compile_args,
extra_link_args=extra_link_args,
language='c++11',
extra_objects=extra_objects,
)
# stackoverflow.com/questions/41169711/python-setuptools-distutils-custom-build-for-the-extra-package-with-makefile
class specialized_build_ext(build_ext):
"""Subclass of build_ext subcommand to build libCZI.
"""
special_extension = module1.name
def build_extension(self, ext):
if ext.name==self.special_extension:
build_libCZI()
if not build_static:
# xxx - hack to copy the dlls in case static build is not working (windows)
data_files = [('', glob.glob(os.path.join(lib_libCZI,'*.dll')))]
if self.distribution.data_files is None:
self.distribution.data_files = data_files
else:
self.distribution.data_files += data_files
# Build the c library's python interface with the parent build_extension method
super(specialized_build_ext, self).build_extension(ext)
setup (name = 'pylibczi',
version=version,
description = 'Python module utilizing libCZI for reading Zeiss CZI files.',
author = 'Paul Watkins',
author_email = '[email protected]',
url = 'https://github.com/elhuhdron/pylibczi',
long_description = '''
Python module to expose libCZI functionality for reading (subset of) Zeiss CZI files and meta-data.
''',
ext_modules = [module1],
packages = ['pylibczi'],
#data_files = data_files,
install_requires=['scipy', 'numpy', 'lxml'],
cmdclass={'build_ext': specialized_build_ext,
'clean': specialized_clean,
}
)