-
Notifications
You must be signed in to change notification settings - Fork 207
/
setup.py
executable file
·1236 lines (1056 loc) · 42.4 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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
from setuptools import setup, Extension, __version__ as setuptools_version
from setuptools.command.build_ext import new_compiler
from pkg_resources import parse_version
from logging import info, warning, error
import argparse
import ctypes
import datetime
import glob
import stat
import os
import re
import shutil
import subprocess
import sys
import platform
from pathlib import Path
# The pylon version this source tree was designed for, by platform
ReferencePylonVersion = {
"Windows": "8.0.0",
# ATTENTION: This version is the pylon core version reported by pylon-config,
# which is not equal to the version on the outer tar.gz
"Linux": "8.0.0",
"Linux_armv7l": "6.2.0",
"Darwin": "7.3.1",
"Darwin_arm64": "7.3.1"
}
################################################################################
def get_machinewidth():
# From the documentation of 'platform.architecture()':
# "Note:
# On Mac OS X (and perhaps other platforms), executable files may be
# universal files containing multiple architectures. To get at the
# '64-bitness# of the current interpreter, it is more reliable to query
# the sys.maxsize attribute.
# "
if sys.maxsize > 2147483647:
return 64
else:
return 32
def get_platform():
return platform.system()
def get_machine():
if get_platform() == "Darwin" and "ARCHFLAGS" in os.environ:
# crossbuild settings: ARCHFLAGS="-arch arm64" or ARCHFLAGS="-arch x86_64"
return os.environ["ARCHFLAGS"].split()[1]
else:
return platform.machine()
def rxglob(path, pattern, recursive=False):
if isinstance(pattern, str):
if not pattern.endswith("$"):
pattern += "$"
pattern = re.compile(pattern)
for entry in Path(path).iterdir():
if pattern.match(entry.name):
yield entry
if recursive and entry.is_dir():
yield from rxglob(entry, pattern, recursive)
class BuildSupport(object):
# --- Constants ---
# Mapping from python platform to pylon platform dirname
BinPath = {
('Windows', 32): 'Win32',
('Windows', 64): 'x64',
('Linux', 32): 'lib',
('Linux', 64): 'lib64',
('Darwin', 64): 'lib64'
} [ (get_platform(), get_machinewidth()) ]
# Compatible swig versions
SwigVersions = ["4.0.0"]
SwigOptions = [
"-c++",
"-Wextra",
"-Wall",
"-threads",
#lots of debug output "-debug-tmsearch",
]
# Where to place generated code
GeneratedDir = os.path.join(".", "generated")
# Directory of the final package
PackageDir = os.path.join(".", "pypylon")
# What parts of the runtime should be deployed by default
RuntimeDefaultDeploy = {
"base",
"gige",
"usb",
"camemu",
"gentl",
"extra",
"pylondataprocessing",
}
# Global switch to toggle pylon data processing support on or off
# If set to true the pylon used for building must support at least pylon data processing 1.3 (pylon 7.4+)
IncludePylonDataProcessing = True
# --- Attributes to be set by init (may be platform specific) ---
# swig executable to be called
SwigExe = None
# Library dirs for compiling extensions
LibraryDirs = []
# Macro definitions for compiling extensions
DefineMacros = []
# Additional compiler arguments for extensions
ExtraCompileArgs = []
# Additional linker arguments for extensions
ExtraLinkArgs = []
# Runtime files needed for copy deployment
RuntimeFiles = {}
def get_swig_includes(self):
raise RuntimeError("Must be implemented by platform build support!")
def __init__(self):
self.SwigExe = "swig"
def dump(self):
for a in dir(self):
info("%s=%s" % (a, getattr(self, a)))
def find_swig(self):
# Find SWIG executable
swig_executable = None
# swig from pypi
try:
import swig
swig_executable = os.path.join(swig.BIN_DIR, "swig")
except ModuleNotFoundError:
# swig from path
swig_executable = shutil.which("swig")
if swig_executable and self.is_supported_swig_version(swig_executable):
info("Found swig: %s" % (swig_executable,))
return swig_executable
else:
raise RuntimeError("swig executable not found on path!")
def is_supported_swig_version(self, swig_executable):
if swig_executable is None:
return False
try:
output = subprocess.check_output(
[swig_executable, "-version"],
universal_newlines=True
)
except (subprocess.CalledProcessError, FileNotFoundError):
return False
res = re.search(r"SWIG Version ([\d\.]+)", output)
if res is None:
return False
if tuple(map(int, res.group(1).split('.'))) < (4, 0, 0):
msg = (
"The version of swig is %s which is too old. " +
"Minimum required version is 4.0.0"
)
warning(msg, res.group(1))
return False
return True
def call_swig(self, sourcedir, source, version, skip=False):
name = os.path.splitext(source)[0]
cpp_name = os.path.abspath(
os.path.join(self.GeneratedDir, "%s_wrap.cpp" % name)
)
if skip:
return cpp_name
outdir = os.path.abspath(self.PackageDir)
for inc in self.get_swig_includes():
self.SwigOptions.append("-I%s" % inc)
call_args = [self.SwigExe]
call_args.extend(["-python"])
call_args.extend(["-outdir", outdir])
call_args.extend(["-o", cpp_name])
call_args.extend(self.SwigOptions)
call_args.append(source)
print("call", " ".join(call_args))
subprocess.check_call(call_args, cwd=os.path.abspath(sourcedir))
# append module version property
with open(os.path.join(outdir, "%s.py" % name), 'at') as gpf:
gpf.write("\n__version__ = '%s'\n" % version)
# Python needs an __init__.py inside the package directory...
with open(os.path.join(bs.PackageDir, "__init__.py"), "a"):
pass
return cpp_name
def copy_runtime(self):
runtime_dir = os.path.join(
self.PylonDevDir,
"..",
"runtime",
self.BinPath
)
package_dir = os.path.abspath(self.PackageDir)
for package in self.get_deploy_list():
for src, dst in self.RuntimeFiles[package]:
dst = os.path.join(package_dir, dst)
if not os.path.exists(dst):
os.makedirs(dst)
src = os.path.join(runtime_dir, src)
for f in glob.glob(src):
print("Copy %s => %s" % (f, dst))
shutil.copy(f, dst)
if package in self.RuntimeFolders:
for src, dst, ignorepatterns in self.RuntimeFolders[package]:
dst = os.path.join(package_dir, dst)
src = os.path.join(runtime_dir, src)
shutil.rmtree(dst, ignore_errors=True)
print("Copy tree %s => %s (ignoring=%s)" % (src, dst, str(ignorepatterns)))
shutil.copytree(src, dst, ignore=shutil.ignore_patterns(*ignorepatterns))
def clean(self, mode, additional_dirs=None):
if mode == 'skip':
return
clean_dirs = [self.GeneratedDir, self.PackageDir]
if additional_dirs:
clean_dirs.extend(additional_dirs)
for cdir in clean_dirs:
print("Remove:", cdir)
shutil.rmtree(cdir, ignore_errors=True)
if mode == 'keep':
os.makedirs(self.GeneratedDir)
os.makedirs(self.PackageDir)
def get_pylon_version(self):
raise RuntimeError("Must be implemented by platform build support!")
def use_debug_configuration(self):
raise RuntimeError("Must be implemented by platform build support!")
@staticmethod
def get_git_version():
try:
# GIT describe as version
git_version = subprocess.check_output(
["git", "describe", "--tags", "--dirty"],
universal_newlines=True
)
git_version = git_version.strip()
m_rel = re.match(
r"^\d+(?:\.\d+){2,3}(?:(?:a|b|rc)\d*)?$",
git_version
)
#this will match something like 1.0.0-14-g123456 and
# 1.0.0-14-g123456-dirty and 1.0.0-dirty
rx_git_ver = re.compile(
r"""
^(\d+(?:\.\d+){2,3}
(?:(?:a|b|rc)\d*)?)
(?:(?:\+[a-zA-Z0-9](?:[a-zA-Z0-9\.]*[a-zA-Z0-9]?))?)
(?:-(\d+)-g[0-9a-f]+)?
(?:-dirty)?$
""",
re.VERBOSE
)
m_dev = rx_git_ver.match(git_version)
if m_rel:
# release build -> return as is
return git_version
if m_dev:
# development build
return "%s.dev%s" % (m_dev.group(1), m_dev.group(2) or 0)
warning("failed to parse git version '%s'", git_version)
raise OSError
except (OSError, subprocess.CalledProcessError) as e:
warning("git not found or invalid tag found.")
warning("-> Building version from date!")
now = datetime.datetime.now()
midnight = datetime.datetime(now.year, now.month, now.day)
todays_seconds = (now - midnight).seconds
return "%d.%d.%d.dev%d" % (
now.year,
now.month,
now.day,
todays_seconds
)
def get_version(self):
git_version = self.get_git_version()
pylon_version = self.get_pylon_version()
#strip the build number from the pylon version
#on linux an optional tag might be included in the version
match = re.match(r"^(\d+\.\d+\.\d+)\.\d+(.*)", pylon_version)
pylon_version_no_build = match.group(1)
pylon_version_tag = match.group(2)
reference_version = ReferencePylonVersion[get_platform()]
# check for a more specialized reference version
platform_machine = get_platform() + "_" + get_machine()
if platform_machine in ReferencePylonVersion:
reference_version = ReferencePylonVersion[platform_machine]
if (
pylon_version_no_build == reference_version and
pylon_version_tag == ''
):
pypylon_version = git_version
else:
# Build is against a non-reference version of pylon.
# Se we add that info to the pypylon version.
# Remove all characters forbidden in a local version
# (- and _ get normalized anyways)
pylon_version_tag_cleaned=re.sub(
r"[^a-zA-Z0-9\.-_]",
'',
pylon_version_tag
)
pypylon_version = "%s+pylon%s%s" % (
git_version,
pylon_version_no_build,
pylon_version_tag_cleaned
)
warning("pylon version differs from the reference version (got: %s, expected: %s)" % (pylon_version_no_build, reference_version))
return pypylon_version
def get_short_version(self, version):
return version.split('+')[0]
@staticmethod
def make():
if get_platform() == "Windows":
return BuildSupportWindows()
elif get_platform() == "Linux":
return BuildSupportLinux()
elif get_platform() == "Darwin":
return BuildSupportMacOS()
else:
error("Unsupported platform")
def get_package_data_files(self):
# patterns for files in self.PackageDir
data_files = ["*.dll", "*.zip", "*.so", "*.so.*", "*.sig"]
# also add all files of any sub-directories recursively
pdir = self.PackageDir
for entry in os.listdir(self.PackageDir):
jentry = os.path.join(pdir, entry)
if stat.S_ISDIR(os.stat(jentry).st_mode):
for (root, _, fnames) in os.walk(jentry):
for fname in fnames:
# file names have to be relative to self.PackageDir
jname = os.path.join(root, fname)
pdir_rel = os.path.relpath(jname, self.PackageDir)
data_files.append(pdir_rel)
return data_files
def get_pylon_version_tuple(self):
parts = self.get_pylon_version().split(".")
# parts[3] (patchlevel) might contain non numeric characters. We just
# take the leading numerals.
parts[3] = re.search(r'\d+', parts[3]).group()
return tuple(map(int, parts))
def include_pylon_data_processing(self):
# pylon Versions since 7.0 support data processing but the pypylon mapping has been introduced with 7.4.
# previous pylon versions are missing required header files used by pypylon
result = self.IncludePylonDataProcessing and self.get_pylon_version_tuple() >= (7, 4, 0, 0)
return result
def get_deploy_list(self):
if self.include_pylon_data_processing():
return self.RuntimeDefaultDeploy
else:
result = self.RuntimeDefaultDeploy.copy()
result.remove("pylondataprocessing")
return result
################################################################################
class BuildSupportWindows(BuildSupport):
# Base directory for pylon SDK on Windows
PylonDevDir = None
RuntimeFiles = {
"base": [
("PylonBase_*.dll", ""),
("GCBase_MD_*.dll", ""),
("GenApi_MD_*.dll", ""),
("log4cpp_MD_*.dll", ""),
("Log_MD_*.dll", ""),
("NodeMapData_MD_*.dll", ""),
("XmlParser_MD_*.dll", ""),
("MathParser_MD_*.dll", ""),
],
"gige": [
("PylonGigE_*.dll", ""),
("gxapi*.dll", ""),
],
"usb": [
("PylonUsb_*.dll", ""),
("uxapi*.dll", ""),
],
"camemu": [
("PylonCamEmu_*.dll", ""),
],
"extra": [
("PylonGUI_*.dll", ""),
("PylonUtility_*.dll", ""),
("PylonUtilityPcl_*.dll", ""),
],
"pylondataprocessing": [
("PylonDataProcessing_v*.dll", ""),
("PylonDataProcessing_v*.sig", ""),
("PylonDataProcessingCore_*.dll", ""),
],
"gentl": [
("PylonGtc_*.dll", ""),
],
}
PYLON_DATA_PROCESSING_VTOOLS_DIR = "pylonDataProcessingPlugins"
RuntimeFolders = {
"pylondataprocessing": [
(PYLON_DATA_PROCESSING_VTOOLS_DIR, PYLON_DATA_PROCESSING_VTOOLS_DIR, ("*Editor*.dll",)),
],
}
# Old versions of distutils use a layman's qouting of commandline
# parameters, that has to be amended with a 'hack'. Newer and fixed
# distutils are used if either (py >= 3.9.0) or (setuptools >= 60.0.0)
correct_qouting = (
sys.version_info >= (3, 9, 0) or
parse_version(setuptools_version) >= parse_version("60.0.0")
)
gentl_dir_fmt = r'L"%s\\bin"' if correct_qouting else r'L\"%s\\bin\"'
DefineMacros = [
("UNICODE", None),
("_UNICODE", None),
# let swig share its type information between the 'genicam' and the
# 'pylon' module by using the same name for the type table.
("SWIG_TYPE_TABLE", "pylon")
]
ExtraCompileArgs = [
'/Gy', # separate functions for linker
'/GL', # enable link-time code generation
'/EHsc', # set execption handling model
]
ExtraLinkArgs = [
'/OPT:REF', # eliminate unused functions
'/OPT:ICF', # eliminate identical COMDAT
'/LTCG' # link time code generation
]
def _detect_msvc_ver(self):
stderr = b""
try:
msvc = new_compiler(compiler='msvc')
msvc.initialize()
kw = {'stdout': subprocess.PIPE, 'stderr': subprocess.PIPE}
with subprocess.Popen([msvc.cc], **kw) as process:
_, stderr = process.communicate()
except Exception:
pass
stderr = stderr.decode("ascii", errors="backslashreplace")
m = re.search(r"\s+(\d+(?:\.\d+)+)\s+", stderr)
return tuple(map(int, m.group(1).split('.'))) if m else (16, 0)
def __init__(self):
super(BuildSupportWindows, self).__init__()
self.SwigExe = self.find_swig()
self.SwigOptions.append("-DHAVE_PYLON_GUI")
self.SwigOptions.append("-D_WIN32")
if get_machinewidth() != 32:
self.SwigOptions.append("-D_WIN64")
self.PylonDevDir = os.environ.get("PYLON_DEV_DIR")
if not self.PylonDevDir:
# Fallback, try to locate pylon installation via registry key
import winreg
try:
# Read install location from registry entry
basler_pylon_registry_key = winreg.OpenKeyEx(winreg.HKEY_LOCAL_MACHINE, r'SOFTWARE\Basler\pylon')
basler_pylon_install_folder = winreg.QueryValueEx(basler_pylon_registry_key, "InstallationFolder")[0]
# Check if install folder contain "Development" folder.
basler_pylon_install_dev_dir = os.path.join(basler_pylon_install_folder, "Development")
if os.path.exists(basler_pylon_install_dev_dir):
self.PylonDevDir = basler_pylon_install_dev_dir
except OSError as error:
print("Fallback, locate of pylon installation via registry failed:", error)
# Throw exception if registry fallback failed.
if not self.PylonDevDir:
raise EnvironmentError("PYLON_DEV_DIR is not set")
self.LibraryDirs = [
os.path.join(
self.PylonDevDir,
"lib",
self.BinPath
)
]
for inc in self.get_swig_includes():
self.ExtraCompileArgs.append('/I%s' % inc)
self.msvc_ver = self._detect_msvc_ver()
if self.msvc_ver >= (19, 13):
# add '/permissive-' to detect skipping initialization with goto
# (available since VS 2017)
self.ExtraCompileArgs.append('/permissive-')
def get_swig_includes(self):
return [os.path.join(self.PylonDevDir, "include")]
def use_debug_configuration(self):
self.ExtraCompileArgs.append('/Od') # disable optimizations
self.ExtraCompileArgs.append('/Zi') # create debug info
self.ExtraLinkArgs.append('/DEBUG') # create pdb file
def find_swig(self):
#this searches for swigwin-<version>\swig.exe at the usual places
env_names = ['PROGRAMFILES', 'PROGRAMFILES(X86)', 'PROGRAMW6432']
search = [os.environ[n] for n in env_names if n in os.environ]
for prg in search:
for swig_version in self.SwigVersions:
candidate = os.path.join(
prg,
"swigwin-%s" % swig_version,
"swig.exe"
)
if self.is_supported_swig_version(candidate):
info("Found swig: %s" % (candidate,))
return candidate
#fallback to the standard implementation
return BuildSupport.find_swig(self)
def copy_runtime(self):
super(BuildSupportWindows, self).copy_runtime()
# detect OS and target bitness
os_bits = 64
if os.environ['PROCESSOR_ARCHITECTURE'] == 'x86':
# might be WOW
wow = os.environ.get('PROCESSOR_ARCHITEW6432', False)
if not wow:
os_bits = 32
tgt_bits = get_machinewidth()
# Copy msvc runtime for pylon
runtime_dlls = ["vcruntime140.dll", "msvcp140.dll"]
if tgt_bits == 64 and self.msvc_ver >= (19, 20):
runtime_dlls.append("vcruntime140_1.dll")
sysname = "System32" if tgt_bits == 64 or os_bits == 32 else "SysWOW64"
sysdir = os.path.join(os.environ["windir"], sysname)
for dll in runtime_dlls:
src = os.path.join(sysdir, dll)
print("Copy %s => %s" % (src, self.PackageDir))
shutil.copy(src, self.PackageDir)
def get_pylon_version(self):
dll_dir = os.path.realpath(
os.path.join(
self.PylonDevDir,
"..",
"Runtime",
self.BinPath
)
)
dll_pattern = os.path.join(dll_dir, "PylonBase_*.dll")
lst = glob.glob(dll_pattern)
if lst:
dll_path = lst[0]
else:
raise EnvironmentError("could not find PylonBase")
# temporarily add dll dir to path
prev_path = os.environ['PATH']
os.environ['PATH'] = os.pathsep.join((dll_dir, prev_path))
pylon_version = [ctypes.c_uint() for _ in range(4)]
pylon_base = ctypes.CDLL(dll_path)
pylon_base.GetPylonVersion(*list(map(ctypes.byref, pylon_version)))
#restore path
os.environ['PATH'] = prev_path
return ".".join([str(v.value) for v in pylon_version])
################################################################################
class BuildSupportLinux(BuildSupport):
PylonConfig = os.path.join(
os.getenv('PYLON_ROOT', '/opt/pylon'),
'bin/pylon-config'
)
PylonDataProcessingConfig = os.path.join(
os.getenv('PYLON_ROOT', '/opt/pylon'),
'bin/pylon-dataprocessing-config'
)
DefineMacros = [
("SWIG_TYPE_TABLE", "pylon")
]
ExtraCompileArgs = [
'-Wno-unknown-pragmas',
'-fPIC',
'-g0',
'-Wall',
'-O3',
'-Wno-switch'
]
ExtraLinkArgs = [
'-g0',
'-Wl,--enable-new-dtags',
'-Wl,-rpath,$ORIGIN',
]
# N.B.: For libraries pylons library folder does not just contain an shared
# object file but also symlinks that refer to that shared object (
# possibly through a cascade of several links).
# When pypylon links to these libraries, the linker will follow the
# first symlink and will record the target of that link as a
# dependency in the pypylon binary. Since the copy operations that are
# necessary to build pypylon follow symlinks, the result of these
# copy operations - in the presence of symlinks - would be that we
# get several copies of the shared object, which of course is
# undesirable.
# So we have to ensure that the following patterns include at least
# the shared object itself. If there is no or only one symlink for
# this file, nothing more is needed. This is the case for all pylon
# versions up to 6.3.0.18933. Later versions use up to two symlinks
# and in that case we have to copy the second symlink with
# 'follow_symlinks=True' so that we get the contents of the shared
# object and the name of the dependency that was recorded in the
# pypylon binary.
# In addition to the change in the number of symlinks there was also
# a change in the naming scheme:
# - old: <basename>-<dotted-version>.so
# - new: <basename>.so.<dotted-version>, no more version numbers in
# TL libraries
#
# While it was sufficent to use glob patterns in the past, this does
# not work for the new naming scheme anymore - there simply is no glob
# pattern that matches <basename>.so.<major>.<minor> but NOT
# <basename>.so.<major>.<minor>.<subminor>. Therefore we have to
# switch to using 'real' regular expressions.
# no differences between versions, no symlinks involved
RuntimeFiles = {
"base": [
(r"libGCBase_.*\.so", ""),
(r"libGenApi_.*\.so", ""),
(r"liblog4cpp_.*\.so", ""),
(r"libLog_.*\.so", ""),
(r"libNodeMapData_.*\.so", ""),
(r"libXmlParser_.*\.so", ""),
(r"libMathParser_.*\.so", ""),
],
"usb": [
(r"pylon-libusb-.*\.so", ""),
],
}
# up to one symlink per library -> match shared objects only
RuntimeFiles_up_to_6_3_0_18933 = {
"base": [
(r"libpylonbase-.*\.so", ""),
],
"gige" : [
(r"libpylon_TL_gige-.*\.so", ""),
(r"libgxapi-.*\.so", ""),
],
"usb": [
(r"libpylon_TL_usb-.*\.so", ""),
(r"libuxapi-.*\.so", ""),
],
"camemu": [
(r"libpylon_TL_camemu-.*\.so", ""),
],
"extra": [
(r"libpylonutility-.*\.so", ""),
],
"gentl": [
(r"libpylon_TL_gtc-.*\.so", ""),
],
}
# match those shared objects without symlinks directly and where there are
# symlinks, match the second one (*.so.<major>.<minor>)
RuntimeFiles_after_6_3_0_18933 = {
"base": [
(r"libpylonbase\.so\.\d+\.\d+", ""),
],
"gige": [
(r"libpylon_TL_gige\.so", ""),
(r"libgxapi\.so\.\d+\.\d+", "")
],
"usb": [
(r"libpylon_TL_usb\.so", ""),
(r"libuxapi\.so\.\d+\.\d+", ""),
],
"camemu": [
(r"libpylon_TL_camemu\.so", "")
],
"extra": [
(r"libpylonutility\.so\.\d+\.\d+", ""),
(r"libpylonutilitypcl\.so\.\d+\.\d+", ""),
],
"gentl": [
(r"libpylon_TL_gtc\.so", ""),
],
"pylondataprocessing": [
(r"libPylonDataProcessing\.so\.\d+", ""),
(r"libPylonDataProcessing.sig", ""),
(r"libPylonDataProcessingCore\.so\.\d+", ""),
],
}
PYLON_DATA_PROCESSING_VTOOLS_DIR = "pylondataprocessingplugins"
RuntimeFolders = {
"pylondataprocessing": [
(PYLON_DATA_PROCESSING_VTOOLS_DIR, PYLON_DATA_PROCESSING_VTOOLS_DIR, ("*Editor*.so",)),
],
}
def __init__(self):
super(BuildSupportLinux, self).__init__()
self.SwigExe = self.find_swig()
self.SwigOptions.append("-DSWIGWORDSIZE%i" % (get_machinewidth(),) )
config_cflags = self.call_pylon_config("--cflags")
self.ExtraCompileArgs.extend(config_cflags.split())
if self.include_pylon_data_processing():
config_cflags = self.call_pylon_dataprocessing_config("--cflags")
self.ExtraCompileArgs.extend(config_cflags.split())
self.ExtraCompileArgs = list(dict.fromkeys(self.ExtraCompileArgs)) #remove duplicates
print("ExtraCompileArgs:", self.ExtraCompileArgs)
config_libs = self.call_pylon_config("--libs")
self.ExtraLinkArgs.extend(config_libs.split())
if self.include_pylon_data_processing():
config_libs = self.call_pylon_dataprocessing_config("--libs")
self.ExtraLinkArgs.extend(config_libs.split())
self.ExtraLinkArgs = list(dict.fromkeys(self.ExtraLinkArgs)) #remove duplicates
print("ExtraLinkArgs:", self.ExtraLinkArgs)
config_libdir = self.call_pylon_config("--libdir")
self.LibraryDirs.extend(config_libdir.split())
if self.include_pylon_data_processing():
config_libdir = self.call_pylon_dataprocessing_config("--libdir")
self.LibraryDirs.extend(config_libdir.split())
self.LibraryDirs = list(dict.fromkeys(self.LibraryDirs)) #remove duplicates
print("LibraryDirs:", self.LibraryDirs)
# adjust runtime files according to pylon version
olden_days = self.get_pylon_version_tuple() <= (6, 3, 0, 18933)
add_runtime = (
self.RuntimeFiles_up_to_6_3_0_18933 if olden_days
else self.RuntimeFiles_after_6_3_0_18933
)
for package in add_runtime:
if package in self.RuntimeFiles:
self.RuntimeFiles[package].extend(add_runtime[package])
else:
self.RuntimeFiles[package] = add_runtime[package]
def use_debug_configuration(self):
try:
self.ExtraCompileArgs.remove('-O3')
except ValueError:
pass
try:
self.ExtraCompileArgs.remove('-g0')
except ValueError:
pass
try:
self.ExtraLinkArgs.remove('-g0')
except ValueError:
pass
self.ExtraCompileArgs.append('-O0')
self.ExtraCompileArgs.append('-g3')
self.ExtraLinkArgs.append('-g3')
def get_swig_includes(self):
# add compiler include paths to list
includes = [i[2:] for i in self.ExtraCompileArgs if i.startswith("-I")]
return includes
def copy_runtime(self):
runtime_dir = self.call_pylon_config("--libdir")
for package in self.get_deploy_list():
for src, dst in self.RuntimeFiles[package]:
full_dst = os.path.abspath(os.path.join(self.PackageDir, dst))
if not os.path.exists(full_dst):
os.makedirs(full_dst)
for f in rxglob(runtime_dir, src):
print("Copy %s => %s" % (f, full_dst))
# Although 'True' is the default value for 'follow_symlinks'
# we set it explicitly to clarify that we depend on
# following symlinks.
shutil.copy(str(f), full_dst, follow_symlinks=True)
if package in self.RuntimeFolders:
for src, dst, ignorepatterns in self.RuntimeFolders[package]:
dst = os.path.join(self.PackageDir, dst)
src = os.path.join(runtime_dir, src)
shutil.rmtree(dst, ignore_errors=True)
print("Copy tree %s => %s (ignoring=%s)" % (src, dst, str(ignorepatterns)))
shutil.copytree(src, dst, ignore=shutil.ignore_patterns(*ignorepatterns))
def call_pylon_config(self, *args):
params = [self.PylonConfig]
params.extend(args)
try:
res = subprocess.check_output(params, universal_newlines=True)
except FileNotFoundError:
msg = (
"Couldn't find pylon. Please install pylon in /opt/pylon " +
"or tell us the installation location using the PYLON_ROOT " +
"env variable"
)
error(msg)
raise
return res.strip()
def call_pylon_dataprocessing_config(self, *args):
params = [self.PylonDataProcessingConfig]
params.extend(args)
try:
res = subprocess.check_output(params, universal_newlines=True)
except FileNotFoundError:
msg = (
"Couldn't find pylon. Please install pylon in /opt/pylon " +
"or tell us the installation location using the PYLON_ROOT " +
"env variable"
)
error(msg)
raise
return res.strip()
def get_pylon_version(self):
return self.call_pylon_config("--version")
################################################################################
class BuildSupportMacOS(BuildSupport):
FrameWorkPath_env = os.getenv('PYLON_FRAMEWORK_LOCATION', 'undef')
FrameworkPath = "/Library/Frameworks" if (FrameWorkPath_env=='undef' or FrameWorkPath_env=="") else FrameWorkPath_env
FrameworkName = 'pylon.framework'
PylonConfig = os.path.join(
FrameworkPath,
FrameworkName,
'Versions/Current/Resources/Tools/pylon-config'
)
DefineMacros = [
("SWIG_TYPE_TABLE", "pylon")
]
ExtraCompileArgs = [
'-Wno-unknown-pragmas',
'-fPIC',
'-g0',
'-Wall',
'-O3',
'-Wno-switch',
'-std=c++17'
]
ExtraLinkArgs = [
'-g0',
'-Wl,-rpath,@loader_path',
'-Wl,-framework,pylon',
'-F' + FrameworkPath
]
RuntimeFolders = {}
def __init__(self):
super(BuildSupportMacOS, self).__init__()
self.SwigExe = self.find_swig()
self.SwigOptions.append("-DSWIGWORDSIZE%i" % (get_machinewidth(),) )
includes_dir = os.path.abspath(
os.path.join('.' , "osx_includes")
)
old_cwd = os.getcwd()
if not os.path.isdir(includes_dir):
os.makedirs(includes_dir)
os.chdir(includes_dir)
# simulate implicit include path as swig is unaware of frameworks
fakeframeinclude = 'pylon'
if (os.path.islink(fakeframeinclude)):
os.remove(fakeframeinclude)
os.symlink(
os.path.join(self.FrameworkPath, self.FrameworkName, 'Headers'),
'pylon'
)
os.chdir(old_cwd)
self.ExtraCompileArgs.append("-I{}".format(includes_dir))
self.ExtraCompileArgs.append(
'-I' + os.path.join(
self.FrameworkPath,
self.FrameworkName,
'Headers',
'GenICam'
)
)
def call_pylon_config(self, *args):
params = [self.PylonConfig]
params.extend(args)
try:
res = subprocess.check_output(params, universal_newlines=True)
except FileNotFoundError:
msg = (
"Couldn't find pylon. Please install pylon in %s or tell us " +
"the framwork search path of the pylon.framework using the PYLON_FRAMEWORK_LOCATION environment " +
"variable"
)
error(msg, self.FrameworkPath)
raise
# work around simple shells
badprefix='-n '
if res.startswith(badprefix):
res = res[len(badprefix):]
return res.strip()
def get_pylon_version(self):
return self.call_pylon_config("--version")
def get_swig_includes(self):
# add compiler include paths to list
includes = [i[2:] for i in self.ExtraCompileArgs if i.startswith("-I")]