-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup.py
512 lines (430 loc) · 16.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
import abc
import os
import sys
import shutil
from typing import Optional, List, Dict
import contextlib
import pathlib
import platform
from setuptools import setup, errors, Distribution
try:
from pybind11.setup_helpers import Pybind11Extension
except ImportError:
from setuptools import Extension as Pybind11Extension
from setuptools.command.build_clib import build_clib
MIN_MACOSX_DEPLOYMENT_TARGET = '10.15'
cmd_class = {}
extension_modules = []
try:
from uiucprescon.build.conan_libs import BuildConan
from uiucprescon.build.conan.files import locate_conanbuildinfo, ConanBuildInfoTXT
cmd_class["build_conan"] = BuildConan
from uiucprescon.build.pybind11_builder import BuildPybind11Extension, parse_conan_build_info
cmd_class["build_ext"] = BuildPybind11Extension
exiv2_extension = Pybind11Extension(
"py3exiv2bind.core",
sources=[
"src/py3exiv2bind/core/core.cpp",
"src/py3exiv2bind/core/glue/ExifStrategy.cpp",
"src/py3exiv2bind/core/glue/glue.cpp",
"src/py3exiv2bind/core/glue/Image.cpp",
"src/py3exiv2bind/core/glue/IPTC_Strategy.cpp",
"src/py3exiv2bind/core/glue/XmpStrategy.cpp",
"src/py3exiv2bind/core/glue/MetadataProcessor.cpp",
],
libraries=[
"exiv2",
],
include_dirs=[
"src/py3exiv2bind/core/glue",
],
language='c++',
cxx_std=17
)
extension_modules.append(exiv2_extension)
except ImportError:
pass
PACKAGE_NAME = "py3exiv2bind"
class AbsCMakePlatform(abc.ABC):
def __init__(self, clib_builder) -> None:
super().__init__()
self.clib_builder = clib_builder
@abc.abstractmethod
def platform_specific_configs(self) -> List[str]:
"""Get cmake arguments specifically for a given platform"""
class WinCMakelib(AbsCMakePlatform):
def platform_specific_configs(self) -> List[str]:
return []
class UnixCMakePlatform(AbsCMakePlatform):
def platform_specific_configs(self) -> List[str]:
return ['-DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=ON']
@contextlib.contextmanager
def set_compiler_env(envvars: Dict[str, str]):
og_env = os.environ.copy()
os.environ = {**os.environ, **envvars}
yield
os.environ = og_env
class OSXCMakelib(UnixCMakePlatform):
def platform_specific_configs(self):
configs: List[str] = super().platform_specific_configs()
sys_root = self.get_cmake_osx_sysroot()
if sys_root is not None:
configs.append(sys_root)
return configs
@staticmethod
def get_cmake_osx_sysroot() -> Optional[str]:
if os.getenv('HOMEBREW_SDKROOT'):
return f"-DCMAKE_OSX_SYSROOT={os.getenv('HOMEBREW_SDKROOT')}"
return None
class LinuxCMakePlatform(UnixCMakePlatform):
def platform_specific_configs(self) -> List[str]:
configs: List[str] = super().platform_specific_configs()
if platform.system() == "Linux":
configs.append('-DEXIV2_BUILD_EXIV2_COMMAND:BOOL=OFF')
return configs
class BuildCMakeLib(build_clib):
user_options = [
(
'cmake-exec=',
None,
"Location of CMake. Defaults of CMake located on path"
)
]
@property
def package_dir(self):
build_py = self.get_finalized_command('build_py')
return build_py.get_package_dir(PACKAGE_NAME)
def initialize_options(self):
super().initialize_options()
self.cmake_exec = None
def __init__(self, dist):
super().__init__(dist)
self.extra_cmake_options = []
self.cmake_api_dir = None
self._cmake_platform: AbsCMakePlatform = {
"Windows": WinCMakelib,
"Darwin": OSXCMakelib,
"Linux": LinuxCMakePlatform
}[platform.system()](self)
def finalize_options(self):
super().finalize_options()
import cmake
self.cmake_exec = shutil.which("cmake", path=cmake.CMAKE_BIN_DIR)
self.cmake_api_dir = \
os.path.join(self.build_temp, "deps", ".cmake", "api", "v1")
@staticmethod
def _get_new_compiler():
build_ext = Distribution().get_command_obj("build_ext")
build_ext.finalize_options()
# register an extension to ensure a compiler is created
build_ext.extensions = [Pybind11Extension("ignored", ["ignored.c"])]
# disable building fake extensions
build_ext.build_extensions = lambda: None
# run to populate self.compiler
build_ext.run()
return build_ext.compiler
def run(self):
if not self.libraries:
return
self.compiler = self._get_new_compiler()
if self.include_dirs is not None:
self.compiler.set_include_dirs(self.include_dirs)
if self.define is not None:
# 'define' option is a list of (name,value) tuples
for (name, value) in self.define:
self.compiler.define_macro(name, value)
if self.undef is not None:
for macro in self.undef:
self.compiler.undefine_macro(macro)
for library in self.libraries:
self.build_extension(library)
def build_extension(self, ext):
if self.compiler.compiler_type != "unix":
if not self.compiler.initialized:
self.compiler.initialize()
if self.cmake_exec is None:
raise FileNotFoundError("CMake path not located on path")
self.configure_cmake(ext)
self.build_cmake(ext)
self.build_install_cmake(ext)
def configure_cmake(self, extension: Pybind11Extension):
source_dir = os.path.abspath(os.path.dirname(__file__))
self.announce("Configuring CMake Project", level=3)
dep_build_path = os.path.join(self.build_temp, "deps")
self.mkpath(dep_build_path)
if self.debug:
build_configuration_name = 'Debug'
else:
build_configuration_name = 'Release'
self.mkpath(os.path.join(self.cmake_api_dir, "query"))
codemodel_file = \
os.path.join(self.cmake_api_dir, "query", "codemodel-v2")
pathlib.Path(codemodel_file).touch()
toolchain_locations = [
self.build_temp,
os.path.join(self.build_temp, "conan"),
os.path.join(self.build_temp, "Release"),
os.path.join(self.build_temp, "Release", "conan"),
]
cmake_toolchain = locate_file("conan_paths.cmake", toolchain_locations)
if cmake_toolchain is None:
raise FileNotFoundError("Missing toolchain file conan_paths.cmake")
configure_command = [
self.cmake_exec, f'-S{source_dir}'
]
ninja = shutil.which("ninja")
if ninja:
configure_command += [
"-G", "Ninja",
f"-DCMAKE_MAKE_PROGRAM:FILEPATH={ninja}",
]
configure_command += [
f'-B{dep_build_path}',
f"-DCMAKE_TOOLCHAIN_FILE={cmake_toolchain}",
f'-DCMAKE_BUILD_TYPE={build_configuration_name}',
f'-DCMAKE_INSTALL_PREFIX={os.path.abspath(self.build_clib)}',
]
configure_command += extension[1].get('CMAKE_CONFIG', [])
configure_command += self._cmake_platform.platform_specific_configs()
configure_command += self.extra_cmake_options
if platform.system() == "Darwin":
env_vars = {
'MACOSX_DEPLOYMENT_TARGET':
os.environ.get(
'MACOSX_DEPLOYMENT_TARGET',
MIN_MACOSX_DEPLOYMENT_TARGET
)
}
else:
env_vars = {}
if platform.system() == "Windows":
if not self.compiler.initialized:
self.compiler.initialize()
with set_compiler_env(env_vars):
try:
self.compiler.spawn(configure_command)
except errors.ExecError:
print("Failed at CMake config time", file=sys.stderr)
raise
def find_target(self, target_name: str, build_type=None) -> Optional[str]:
for f in os.scandir(os.path.join(self.cmake_api_dir, "reply")):
if f"target-{target_name}-" not in f.name:
continue
if build_type is not None:
if build_type not in f.name:
continue
return f.path
return None
def build_cmake(self, extension: Pybind11Extension):
dep_build_path = os.path.join(self.build_temp, "deps")
build_command = [
self.cmake_exec,
"--build", dep_build_path,
]
if self.verbose == 1:
build_command.append("--verbose")
# Add config
build_command.append("--config")
if self.debug:
build_command.append("Debug")
else:
build_command.append("Release")
build_ext_cmd = self.get_finalized_command("build_ext")
if build_ext_cmd.parallel:
build_command.extend(["-j", str(build_ext_cmd.parallel)])
try:
if platform.system() == "Darwin":
env_vars = {
'MACOSX_DEPLOYMENT_TARGET':
os.environ.get(
'MACOSX_DEPLOYMENT_TARGET',
MIN_MACOSX_DEPLOYMENT_TARGET
)
}
else:
env_vars = {}
with set_compiler_env(env_vars):
self.compiler.spawn(build_command)
except errors.ExecError:
print(f"Conan failed when running {build_command}", file=sys.stderr)
raise
def build_install_cmake(self, extension: Pybind11Extension):
dep_build_path = os.path.join(self.build_temp, "deps")
install_command = [
self.cmake_exec,
"--build", dep_build_path
]
self.announce("Adding binaries to Python build path", level=3)
install_command.append("--config")
if self.debug:
install_command.append("Debug")
else:
install_command.append("Release")
install_command += ["--target", "install"]
build_ext_cmd = self.get_finalized_command("build_ext")
if build_ext_cmd.parallel:
install_command.extend(["-j", str(build_ext_cmd.parallel)])
ext: Pybind11Extension
for ext in build_ext_cmd.extensions:
ext.library_dirs.insert(
0,
os.path.abspath(os.path.join(build_ext_cmd.build_lib, "src/py3exiv2bind"))
)
build_ext_cmd.include_dirs.insert(
0,
os.path.abspath(
os.path.join(build_ext_cmd.build_temp, "include")
)
)
build_ext_cmd.library_dirs.insert(
0, os.path.abspath(os.path.join(build_ext_cmd.build_temp, "lib"))
)
self.mkpath(os.path.join(self.build_clib, "bin"))
try:
self.compiler.spawn(install_command)
except errors.ExecError:
print("CMake failed at install time")
raise
install_manifest = os.path.join(self.build_temp, "deps", "install_manifest.txt")
self.add_cmake_built_libraries(install_manifest)
lib_dirs = list(self.locate_cmake_installed_lib_dirs(install_manifest))
ext: Pybind11Extension
for ext in build_ext_cmd.extensions:
ext.library_dirs = lib_dirs + [
dir_ for dir_ in ext.library_dirs if os.path.exists(dir_)
]
if sys.platform == "darwin":
if "@loader_path" not in ext.runtime_library_dirs:
ext.runtime_library_dirs.append("@loader_path")
elif sys.platform == "linux":
if "$ORIGIN" not in ext.runtime_library_dirs:
ext.runtime_library_dirs.append("$ORIGIN")
def add_cmake_built_libraries(self, install_manifest):
if not os.path.exists(install_manifest):
raise FileNotFoundError(f"Missing {install_manifest}")
with open(install_manifest, "r", encoding="utf8") as f:
build_py = self.get_finalized_command("build_py")
install_dir = os.path.abspath(
os.path.join(
build_py.build_lib,
build_py.get_package_dir(build_py.packages[0]))
)
for line in f.readlines():
file_path = line.strip()
if ".dylib" not in file_path:
continue
shutil.copy(file_path, install_dir, follow_symlinks=False)
def locate_cmake_installed_lib_dirs(self, install_manifest):
with open(install_manifest, "r", encoding="utf8") as f:
file_paths = set()
# don't return any dup paths
for line in f.readlines():
file_path = line.strip()
if platform.system() == "Windows":
if ".lib" not in file_path:
continue
else:
if ".a" not in file_path:
continue
path = str(pathlib.Path(file_path).parent)
if path not in file_paths:
file_paths.add(path)
yield path
def locate_file(file_name, search_locations):
for location in search_locations:
file_candidate = os.path.join(location, file_name)
if os.path.exists(file_candidate):
return file_candidate
class BuildExiv2(BuildCMakeLib):
def __init__(self, dist):
super().__init__(dist)
self.extra_cmake_options += [
"-DBUILD_SHARED_LIBS:BOOL=OFF",
"-DBUILD_TESTING:BOOL=OFF",
]
def run(self):
conan_cmd = self.get_finalized_command("build_conan")
build_clib = self.get_finalized_command("build_clib")
try:
conan_cmd.run()
except Exception:
self.announce("Building with conan failed.", level=3)
raise
toolchain_locations = [
self.build_temp,
os.path.join(self.build_temp, "conan"),
os.path.join(self.build_temp, "Release"),
os.path.join(self.build_temp, "Release", "conan"),
]
cmake_toolchain = locate_file("conan_paths.cmake", toolchain_locations)
if cmake_toolchain is None:
raise FileNotFoundError(
f"Missing toolchain file conan_paths.cmake. "
f"Searching in {toolchain_locations}",
)
self.extra_cmake_options.append(
f'-DCMAKE_TOOLCHAIN_FILE={cmake_toolchain}'
)
super().run()
ext_command = self.get_finalized_command("build_ext")
core_ext = ext_command.ext_map['py3exiv2bind.core']
build_locations = [
build_clib.build_temp,
os.path.join(build_clib.build_temp, "conan"),
os.path.join(build_clib.build_temp, "conan", "Release"),
os.path.join(build_clib.build_temp, "Release"),
]
conanbuildinfotext = locate_conanbuildinfo(build_locations)
metadata_strategy = ConanBuildInfoTXT()
text_md = metadata_strategy.parse(conanbuildinfotext)
for lib in text_md["libs"]:
if lib not in core_ext.libraries:
core_ext.libraries.append(lib)
for path in reversed(text_md["lib_paths"]):
if path not in core_ext.library_dirs:
core_ext.library_dirs.insert(0, path)
core_ext.include_dirs.insert(0, os.path.join(self.build_temp, "include"))
if os.name == 'nt':
core_ext.libraries.append("shell32")
core_ext.libraries.append("psapi")
core_ext.libraries.append("Ws2_32")
temp_lib_dir = os.path.join(self.build_temp, "lib")
if temp_lib_dir not in core_ext.library_dirs:
core_ext.library_dirs.insert(0, temp_lib_dir)
exiv2 = ("exiv2", {
"sources": [],
"CMAKE_SOURCE_DIR": os.path.dirname(__file__),
"CMAKE_CONFIG": [
'-Dpyexiv2bind_generate_python_bindings:BOOL=NO',
'-DEXIV2_ENABLE_NLS:BOOL=NO',
'-DEXIV2_ENABLE_VIDEO:BOOL=OFF',
'-DEXIV2_ENABLE_PNG:BOOL=OFF',
'-DEXIV2_BUILD_EXIV2_COMMAND:BOOL=OFF',
"-DCMAKE_CXX_STANDARD=17"
],
})
cmd_class['build_clib'] = BuildExiv2
exiv2_extension = Pybind11Extension(
"py3exiv2bind.core",
sources=[
"src/py3exiv2bind/core/core.cpp",
"src/py3exiv2bind/core/glue/ExifStrategy.cpp",
"src/py3exiv2bind/core/glue/glue.cpp",
"src/py3exiv2bind/core/glue/Image.cpp",
"src/py3exiv2bind/core/glue/IPTC_Strategy.cpp",
"src/py3exiv2bind/core/glue/XmpStrategy.cpp",
"src/py3exiv2bind/core/glue/MetadataProcessor.cpp",
],
libraries=[
"exiv2",
],
include_dirs=[
"src/py3exiv2bind/core/glue"
],
language='c++',
)
setup(
libraries=[exiv2],
ext_modules=extension_modules,
cmdclass=cmd_class
)