-
Notifications
You must be signed in to change notification settings - Fork 593
/
Copy pathsetup.py
300 lines (260 loc) · 11.9 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# Copyright (c) ZeroC, Inc.
from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext as _build_ext
from setuptools.command.sdist import sdist as _sdist
import urllib.request
import os
import shutil
import sys
import pathlib
# Define versions and URLs for dependencies
mcpp_version = "2.7.2.18"
mcpp_url = f"https://github.com/zeroc-ice/mcpp/archive/refs/tags/v{mcpp_version}.tar.gz"
mcpp_local_filename = f"dist/mcpp-{mcpp_version}.tar.gz"
bzip2_version = "1.0.6.6"
bzip2_url = f"https://github.com/zeroc-ice/bzip2/archive/refs/tags/v{bzip2_version}.tar.gz"
bzip2_local_filename = f"dist/bzip2-{bzip2_version}.tar.gz"
script_directory = os.path.dirname(os.path.abspath(__file__))
# Used by the Windows build
platform = os.getenv('CPP_PLATFORM', "x64")
configuration = os.getenv('CPP_CONFIGURATION', "Release")
# Define Python packages to be included
packages = ['Ice', 'IceMX', 'slice']
# Define source directories for Ice C++
ice_cpp_sources = [
"../cpp/src/Ice",
"../cpp/src/IceDiscovery",
"../cpp/src/IceLocatorDiscovery",
"../cpp/src/slice2py",
"../cpp/src/Slice",
"../cpp/include/Ice"]
# Include directories for the build process
include_dirs = [
'dist/python/modules/IcePy',
'dist/ice/cpp/include',
'dist/ice/cpp/src',
'dist/mcpp']
# Platform-specific adjustments
if sys.platform == 'win32':
ice_cpp_sources.append(f"../cpp/include/generated/{platform}/{configuration}/Ice")
include_dirs.extend([
f"dist/ice/cpp/include/generated/{platform}/{configuration}",
f"dist/ice/cpp/src/IceDiscovery/msbuild/icediscovery/{platform}/{configuration}",
f"dist/ice/cpp/src/IceLocatorDiscovery/msbuild/icelocatordiscovery/{platform}/{configuration}",
f"dist/bzip2-{bzip2_version}"])
else:
ice_cpp_sources.append("../cpp/include/generated/Ice")
include_dirs.extend([
"dist/ice/cpp/include/generated",
"dist/ice/cpp/src/IceDiscovery/generated",
"dist/ice/cpp/src/IceLocatorDiscovery/generated"])
# Define macros used during the build process
# All the /**/ macros are necessary only on Windows
define_macros = [
('ICE_BUILDING_ICE', None),
('ICE_BUILDING_ICE_LOCATOR_DISCOVERY', None),
('ICE_API', '/**/'),
('ICE_DISCOVERY_API', '/**/'),
('ICE_LOCATOR_DISCOVERY_API', '/**/'),
('ICE_PLUGIN_REGISTER_DECLSPEC_IMPORT', '/**/')]
# Platform-specific compile and link arguments
if sys.platform == 'darwin':
extra_compile_args = ['-w']
cpp_extra_compile_args = ['-std=c++20']
libraries = []
extra_link_args = ['-framework', 'Security', '-framework', 'CoreFoundation']
elif sys.platform == 'win32':
define_macros.extend(
[('WIN32_LEAN_AND_MEAN', None),
('_WIN32_WINNT', '0x0A00'),
('BZ_EXPORT', None)])
extra_compile_args = ['/std:c++20', '/EHsc', '/Zi']
extra_compile_args.extend(['/wd4018', '/wd4146', '/wd4244', '/wd4250', '/wd4251', '/wd4267', '/wd4275', '/wd4996'])
extra_link_args = ['/DEBUG:FULL']
libraries = ['dbghelp', 'Shlwapi', 'rpcrt4', 'advapi32', 'Iphlpapi', 'secur32', 'crypt32', 'ws2_32']
else:
extra_compile_args = ['-w']
cpp_extra_compile_args = ['-std=c++17']
extra_link_args = []
libraries = ['ssl', 'crypto', 'bz2', 'rt']
if not sys.platform.startswith('freebsd'):
libraries.append('dl')
def filter_source(filename):
# Filter out sources that are not needed for building the extension depending on the target platform.
if "ios/" in filename:
return False
if "RegisterPluginsInit_min" in filename:
return False
# Bzip2lib sources
bzip2sources = ["blocksort.c", "bzlib.c", "compress.c", "crctable.c", "decompress.c", "huffman.c", "randtable.c"]
if "bzip2-" in filename and os.path.basename(filename) not in bzip2sources:
return False
if sys.platform == 'win32':
for exclude in ["SysLoggerI", "OpenSSL", "SecureTransport"]:
if exclude in filename:
# Skip SysLoggerI, OpenSSL and SecureTransport on Windows
return False
elif sys.platform == 'darwin':
for exclude in ["DLLMain", "Schannel", "OpenSSL", "bzip2-"]:
if exclude in filename:
# Skip Schannel, OpenSSL and bzip2 on macOS
return False
else:
for exclude in ["DLLMain", "Schannel", "SecureTransport", "bzip2-"]:
if exclude in filename:
# Skip Schannel, SecureTransport and bzip2 on Linux
return False
return True
# Customize the build_ext command to filter sources and add extra compile args for C++ files
class CustomBuildExtCommand(_build_ext):
def run(self):
sources = []
for root, dirs, files in os.walk("dist"):
for file in files:
if pathlib.Path(file).suffix.lower() in ['.c', '.cpp']:
sources.append(os.path.join(root, file))
filtered = list(filter(filter_source, sources))
self.distribution.ext_modules[0].sources = filtered
_build_ext.run(self)
def build_extension(self, ext):
original_compile = self.compiler._compile
# Monkey-patch the compiler to add extra compile args for C++ files. This works around errors with Clang and
# GCC as they don't accept --std=c++XX when compiling C files. The MSVC backend doesn't use _compile.
def _compile(obj, src, ext, cc_args, extra_postargs, pp_opts):
original_compile(
obj,
src,
ext,
cc_args,
cpp_extra_compile_args + extra_postargs if src.endswith('.cpp') else extra_postargs,
pp_opts)
self.compiler._compile = _compile
try:
_build_ext.build_extension(self, ext)
finally:
self.compiler._compile = original_compile
# Customize the sdist command to ensure that the third-party sources and the generated sources are included in the
# source distribution.
class CustomSdistCommand(_sdist):
def include_file(self, filename):
filename = os.path.normpath(filename)
# For Windows, ensure we only include generated files for the current platform
if pathlib.Path(filename).suffix.lower() not in ['.c', '.cpp', '.h', '.ice', '.py']:
return False
if sys.platform == 'win32':
if "generated" in filename and os.path.join(platform, configuration) not in filename:
return False
elif "msbuild" in filename and os.path.join(platform, configuration) not in filename:
return False
return True
def pre_build(self):
# MCPP sources
if not os.path.exists(mcpp_local_filename):
with urllib.request.urlopen(mcpp_url) as response:
with open(mcpp_local_filename, 'wb') as f:
f.write(response.read())
os.system(f"tar -xzf {mcpp_local_filename} -C dist")
# Bzip2 sources
if not os.path.exists(bzip2_local_filename):
with urllib.request.urlopen(bzip2_url) as response:
with open(bzip2_local_filename, 'wb') as f:
f.write(response.read())
os.system(f"tar -xzf {bzip2_local_filename} -C dist")
# Ice sources
if sys.platform == 'win32':
# Build slice2cpp and slice2py required to generate the C++ and Python sources included in the pip source dist
msbuild_args = f"/p:Configuration={configuration} /p:Platform={platform}"
solution_path = "../cpp/msbuild/ice.sln"
os.system(f"MSBuild /m {solution_path} {msbuild_args} /t:slice2cpp;slice2py")
# Build the SliceCompile target to generate the Ice, IceDiscovery, and IceLocatorDiscovery
# sources included in the pip source dist
for project in ["Ice", "IceDiscovery", "IceLocatorDiscovery"]:
project_path = f"../cpp/src/{project}/{project}/{project}.vcxproj"
os.system(f"MSBuild {project_path} {msbuild_args} /t:SliceCompile")
else:
cpp_source_dir = os.path.join(script_directory, '..', 'cpp')
cpp_targets = "slice2cpp slice2py generate-srcs"
os.system(f"make OPTIMIZE=yes -C {cpp_source_dir} {cpp_targets} {cpp_source_dir}")
for source_dir in ice_cpp_sources:
for root, dirs, files in os.walk(source_dir):
for file in files:
if self.include_file(os.path.join(root, file)):
source_file = os.path.join(root, file)
relative_path = os.path.relpath(source_file, '..')
target_file = os.path.join(script_directory, 'dist/ice', relative_path)
target_dir = os.path.dirname(target_file)
if not os.path.exists(target_dir):
os.makedirs(target_dir)
shutil.copy(source_file, target_file)
# IcePy sources
for root, dirs, files in os.walk("modules/IcePy"):
for file in files:
if file.endswith('.cpp') or file.endswith('.h'):
source_file = os.path.join(root, file)
relative_path = os.path.relpath(source_file, '.')
target_file = os.path.join(script_directory, 'dist/ice/python', relative_path)
target_dir = os.path.dirname(target_file)
if not os.path.exists(target_dir):
os.makedirs(target_dir)
shutil.copy(source_file, target_file)
# Slice sources
for root, dirs, files in os.walk("../slice"):
for file in files:
source_file = os.path.join(root, file)
relative_path = os.path.relpath(source_file, '..')
target_file = os.path.join(script_directory, 'dist/lib', relative_path)
target_dir = os.path.dirname(target_file)
if not os.path.exists(target_dir):
os.makedirs(target_dir)
shutil.copy(source_file, target_file)
# Python sources
if sys.platform == 'win32':
os.system(f"MSBuild msbuild/ice.proj /t:SliceCompile /p:Configuration={configuration} /p:Platform={platform}")
else:
os.system("make generate-srcs")
for root, dirs, files in os.walk("python"):
for file in files:
if file.endswith('.py'):
source_file = os.path.join(root, file)
relative_path = os.path.relpath(source_file, 'python')
target_file = os.path.join(script_directory, 'dist/lib', relative_path)
target_dir = os.path.dirname(target_file)
if not os.path.exists(target_dir):
os.makedirs(target_dir)
shutil.copy(source_file, target_file)
def run(self):
if os.path.exists("../cpp"):
self.pre_build()
global sources # Use the global sources list
sources = [] # Clear the sources list
for root, dirs, files in os.walk("dist"):
for file in files:
if self.include_file(os.path.join(root, file)):
sources.append(os.path.join(root, file))
self.distribution.ext_modules[0].sources = sources
_sdist.run(self)
# Initially empty, to be populated by PreBuildCommand
sources = []
# Define the IcePy extension module
ice_py = Extension(
'IcePy',
sources=sources,
include_dirs=include_dirs,
extra_compile_args=extra_compile_args,
extra_link_args=extra_link_args,
define_macros=define_macros,
libraries=libraries)
# Setup configuration for the package
setup(
name='zeroc-ice',
version='3.8.0a0',
packages=packages,
package_dir={'': 'dist/lib'},
package_data={'slice': ['*.ice']},
include_package_data=True,
ext_modules=[ice_py],
cmdclass={
'build_ext': CustomBuildExtCommand,
'sdist': CustomSdistCommand,
},
)