forked from kdschlosser/EventGhost-x64-Python3.5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
msvc.py
1407 lines (1195 loc) · 44.8 KB
/
msvc.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
# -*- coding: utf-8 -*-
#
# This file is part of EventGhost.
# Copyright © 2005-2018 EventGhost Project <http://www.eventghost.net/>
#
# EventGhost 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 2 of the License, or (at your option)
# any later version.
#
# EventGhost 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 EventGhost. If not, see <http://www.gnu.org/licenses/>.
import sys
import os
import platform
import shutil
from ctypes import (
POINTER,
byref,
c_char,
c_uint,
cast,
pointer,
sizeof,
Structure,
windll,
WinError
)
from ctypes.wintypes import (
BOOL,
DWORD,
LPCVOID,
LPCWSTR,
LPVOID
)
try:
_winreg = __import__('winreg')
except ImportError:
_winreg = __import__('_winreg')
try:
import setuptools
setup_tools_version = float(
'.'.join(setuptools.__version__.split('.')[:2]))
if setup_tools_version < 40.2:
raise ImportError
del setuptools
del setup_tools_version
except ImportError:
raise RuntimeError('This library requires setuptools >= 40.2')
PUINT = POINTER(c_uint)
LPDWORD = POINTER(DWORD)
_GetFileVersionInfoSize = windll.version.GetFileVersionInfoSizeW
_GetFileVersionInfoSize.restype = DWORD
_GetFileVersionInfoSize.argtypes = [LPCWSTR, LPDWORD]
_GetFileVersionInfo = windll.version.GetFileVersionInfoW
_GetFileVersionInfo.restype = BOOL
_GetFileVersionInfo.argtypes = [LPCWSTR, DWORD, DWORD, LPVOID]
_VerQueryValue = windll.version.VerQueryValueW
_VerQueryValue.restype = BOOL
_VerQueryValue.argtypes = [LPCVOID, LPCWSTR, POINTER(LPVOID), PUINT]
class VS_FIXEDFILEINFO(Structure):
_fields_ = [
("dwSignature", DWORD), # will be 0xFEEF04BD
("dwStrucVersion", DWORD),
("dwFileVersionMS", DWORD),
("dwFileVersionLS", DWORD),
("dwProductVersionMS", DWORD),
("dwProductVersionLS", DWORD),
("dwFileFlagsMask", DWORD),
("dwFileFlags", DWORD),
("dwFileOS", DWORD),
("dwFileType", DWORD),
("dwFileSubtype", DWORD),
("dwFileDateMS", DWORD),
("dwFileDateLS", DWORD)
]
def _get_file_version(filename):
dwLen = _GetFileVersionInfoSize(filename, None)
if not dwLen:
raise WinError()
lpData = (c_char * dwLen)()
if not _GetFileVersionInfo(filename, 0, sizeof(lpData), lpData):
raise WinError()
uLen = c_uint()
lpffi = POINTER(VS_FIXEDFILEINFO)()
lplpBuffer = cast(pointer(lpffi), POINTER(LPVOID))
if not _VerQueryValue(lpData, "\\", lplpBuffer, byref(uLen)):
raise WinError()
ffi = lpffi.contents
return (
ffi.dwFileVersionMS >> 16,
ffi.dwFileVersionMS & 0xFFFF,
ffi.dwFileVersionLS >> 16,
ffi.dwFileVersionLS & 0xFFFF,
)
def _get_reg_value(path, key):
d = _read_reg_values(path)
if key in d:
return d[key]
return ''
def _read_reg_keys(key):
try:
handle = _winreg.OpenKeyEx(
_winreg.HKEY_LOCAL_MACHINE,
'SOFTWARE\\Wow6432Node\\Microsoft\\' + key
)
except _winreg.error:
return []
res = []
for i in range(_winreg.QueryInfoKey(handle)[0]):
res += [_winreg.EnumKey(handle, i)]
return res
def _read_reg_values(key):
try:
handle = _winreg.OpenKeyEx(
_winreg.HKEY_LOCAL_MACHINE,
'SOFTWARE\\Wow6432Node\\Microsoft\\' + key
)
except _winreg.error:
return {}
res = {}
for i in range(_winreg.QueryInfoKey(handle)[1]):
name, value, _ = _winreg.EnumValue(handle, i)
res[_convert_mbcs(name)] = _convert_mbcs(value)
return res
def _convert_mbcs(s):
dec = getattr(s, "decode", None)
if dec is not None:
try:
s = dec("mbcs")
except UnicodeError:
pass
return s
class Environment(object):
def __init__(self, strict_compiler_version=False, dll_build=False):
self.strict_compiler_version = strict_compiler_version
self.dll_build = dll_build
self._win32 = None
@property
def msvc_dll_version(self):
msvc_dll_path = self.msvc_dll_path
for f in os.listdir(msvc_dll_path):
if f.endswith('dll'):
version = _get_file_version(os.path.join(msvc_dll_path, f))
return '.'.join(str(v) for v in version)
@property
def msvc_dll_path(self):
x64 = self.machine_architecture == 'x64'
folder_name = 'Microsoft.VC{0}.CRT'.format(self.platform_toolset[1:])
redist_path = os.path.join(self.visual_c_path, 'redist')
for root, dirs, files in os.walk(redist_path):
if 'onecore' not in root:
if folder_name in dirs:
if x64 and ('amd64' in root or 'x64' in root):
return os.path.join(root, folder_name)
elif not x64 and 'amd64' not in root and 'x64' not in root:
return os.path.join(root, folder_name)
@property
def machine_architecture(self):
return 'x64' if '64' in platform.machine() else 'x86'
@property
def architecture(self):
"""
:return: x86 or x64
"""
win_64 = '64' in platform.machine()
python_64 = platform.architecture()[0] == '64bit' and win_64
return 'x64' if python_64 else 'x86'
@property
def platform(self):
"""
This is used to tell MSBuild what configuration to build
:return: Win32 or x64
"""
if self._win32 is True:
return 'Win32' if self.architecture == 'x86' else 'x64'
if self._win32 is False:
return self.architecture
return '???' if self.architecture == 'x86' else 'x64'
@property
def platform_toolset(self):
"""
The platform toolset gets written to the solution file. this instructs
the compiler to use the matching MSVCPxxx.dll file.
:return: one of the following
Visual C Visual Studio Returned Value
VC 15.0 - VS 2017: v141
VC 14.0 - VS 2015: v140
VC 12.0 - VS 2013: v120
VC 11.0 - VS 2012: v110
VC 10.0 - VS 2010: v100
VC 9.0 - VS 2008: v90
"""
platform_toolsets = {
15.0: 'v141',
14.0: 'v140',
12.0: 'v120',
11.0: 'v110',
10.0: 'v100',
9.0: 'v90'
}
return platform_toolsets[self.visual_c_version]
@property
def py_architecture(self):
return 'x64' if platform.architecture()[0] == '64bit' else 'x86'
@property
def py_version(self):
return '.'.join(str(v) for v in sys.version_info)
@property
def py_dependency(self):
return 'Python%d%d.lib' % sys.version_info[:2]
@property
def py_includes(self):
python_path = os.path.dirname(sys.executable)
python_include = os.path.join(python_path, 'include')
python_includes = [python_include]
for root, dirs, files in os.walk(python_include):
for d in dirs:
python_includes += [os.path.join(root, d)]
return python_includes
@property
def py_libraries(self):
python_path = os.path.dirname(sys.executable)
python_lib = os.path.join(python_path, 'libs')
python_libs = [python_lib]
for root, dirs, files in os.walk(python_lib):
for d in dirs:
python_libs += [os.path.join(root, d)]
return python_libs
@property
def target_framework(self):
"""
.NET Version
:return: returns the version associated with the architecture
"""
if self.architecture == 'x64':
return self.framework_version_64
else:
return self.framework_version_32
@property
def framework_dir_32(self):
"""
.NET 32bit path
:return: path to x86 .NET
"""
directory = _get_reg_value(
'VisualStudio\\SxS\\VC7\\',
'FrameworkDir32'
)
if directory is None:
return os.path.join(
os.environ.get('WINDIR', r'C:\Windows'),
'Microsoft.NET',
'Framework'
)
return directory[:-1]
@property
def framework_dir_64(self):
"""
.NET 64bit path
:return: path to x64 .NET
"""
guess_fw = os.path.join(
os.environ.get('WINDIR', r'C:\Windows'),
'Microsoft.NET',
'Framework64'
)
return (
_get_reg_value('VisualStudio\\SxS\\VC7\\', 'FrameworkDir64') or
guess_fw
)
@property
def framework_version_32(self):
"""
.NET 32bit framework version
:return: x86 .NET framework version
"""
target_frameworks = {
'v141': ('4.7*', '4.6*', '4.5*', '4.0*', '3.5*', '3.0*', '2.0*'),
'v140': ('4.6*', '4.5*', '4.0*', '3.5*', '3.0*', '2.0*'),
'v120': ('4.5*', '4.0*', '3.5*', '3.0*', '2.0*'),
'v110': ('4.5*', '4.0*', '3.5*', '3.0*', '2.0*'),
'v100': ('4.0*', '3.5*', '3.0*', '2.0*'),
'v90': ('3.5*', '3.0*', '2.0*')
}
target_framework = _get_reg_value(
'VisualStudio\\SxS\\VC7',
'FrameworkVer32'
)
if not target_framework:
import fnmatch
versions = list(
key for key in _read_reg_keys('.NETFramework\\')
if key.startswith('v')
)
target_frameworks = target_frameworks[self.platform_toolset]
for version in versions:
for target_framework in target_frameworks:
if fnmatch.fnmatch(version, 'v' + target_framework):
target_framework = version
break
else:
continue
break
else:
raise RuntimeError(
'No Suitable .NET Framework found %s' %
(target_frameworks,)
)
return target_framework
@property
def framework_version_64(self):
"""
.NET 64bit framework version
:return: x64 .NET framework version
"""
target_framework = _get_reg_value(
'VisualStudio\\SxS\\VC7',
'FrameworkVer64'
)
if not target_framework:
target_framework = self.framework_version_32
return target_framework
@property
def configuration(self):
"""
Build configuration
:return: one of ReleaseDLL, DebugDLL
"""
if os.path.splitext(sys.executable)[0].endswith('_d'):
config = 'Debug'
else:
config = 'Release'
if self.dll_build:
config += 'DLL'
return config
@property
def min_visual_c_version(self):
"""
Minimum Visual C version
This property is here for completeness. Because the building of
openzwave does not seem to care if it matches the same compiler
version that was used to compile python we have the ability to set
a minimum that is the same for all python versions.
the reason the version is not set to 9.0 is due to libopenzwave.pyd
making use of map. map does not have the method "at" in VC 9.0.
this is a method that is used quite a few times, and i do not think
it is something that can easily be replaced
:return: always 10.0
"""
py_version = sys.version_info[:2]
if py_version in ((2, 6), (2, 7), (3, 0), (3, 1), (3, 2)):
return 9.0
elif py_version in ((3, 3), (3, 4)):
return 10.0
elif py_version in ((3, 5), (3, 6), (3, 7)):
return 14.0
else:
raise RuntimeError(
'This library does not support '
'python version %d.%d' % py_version
)
@property
def visual_c_version(self):
"""
Visual C version
:return: found Visual C version or raises
distutils.errors.DistutilsPlatformError
"""
from setuptools.msvc import EnvironmentInfo
min_visual_c_version = self.min_visual_c_version
env_info = EnvironmentInfo(
self.architecture,
vc_min_ver=min_visual_c_version
)
vc_ver = env_info.vc_ver
if vc_ver != min_visual_c_version and self.strict_compiler_version:
raise RuntimeError(
'No Compatible Visual C version found.'
)
return vc_ver
@property
def msbuild_version(self):
"""
MSBuild versions are specific to the Visual C version
:return: MSBuild version, 3.5, 4.0, 12, 14, 15
"""
vc_version = self.visual_c_version
if vc_version == 9.0:
return 3.5
if vc_version in (10.0, 11.0):
return 4.0
else:
return vc_version
@property
def vc_tools_redist_path(self):
tools_install_path = self.tools_install_path
if 'MSVC' in tools_install_path:
return tools_install_path.replace('Tools', 'Redist')
return os.path.join(self.visual_c_path, 'redist')
@property
def tools_install_path(self):
"""
Visual C compiler tools path.
:return: Path to the compiler tools
"""
visual_c_version = str(self.visual_c_version)
if visual_c_version == '15.0':
tools_install_path = os.path.join(
self.visual_c_path,
'Tools',
'MSVC'
)
if os.path.exists(tools_install_path):
from pkg_resources import parse_version
max_version = '0.0.0'
versions = list(
item for item in os.listdir(tools_install_path)
if '.' in item
)
for version in versions:
if parse_version(version) > parse_version(max_version):
max_version = version
tools_install_path = os.path.join(
tools_install_path,
max_version
)
else:
tools_install_path = _get_reg_value(
'MSBuild\\' + str(self.msbuild_version),
'MSBuildOverrideTasksPath'
)
if not tools_install_path:
tools_install_path = _get_reg_value(
'MSBuild\\ToolsVersions\\' + str(self.msbuild_version),
'MSBuildToolsPath'
)
if tools_install_path:
if tools_install_path.endswith('\\'):
tools_install_path = tools_install_path[:-1]
return tools_install_path
raise RuntimeError('Unable to locate Visual C Tools Path')
def __iter__(self):
for item in self.build_environment.items():
yield item
@property
def build_environment(self):
"""
This would be the work horse. This is where all of the gathered
information is put into a single container and returned.
The information is then added to os.environ in order to allow the
build process to run properly.
List of environment variables generated:
PATH
LIBPATH
LIB
INCLUDE
PLATFORM
FRAMEWORKDIR
FRAMEWORKVERSION
FRAMEWORKDIR32
FRAMEWORKVERSION32
FRAMEWORKDIR64
FRAMEWORKVERSION64
VCTOOLSREDISTDIR
VCINSTALLDIR
VCTOOLSINSTALLDIR
VCTOOLSVERSION
WINDOWSLIBPATH
WINDOWSSDKDIR
WINDOWSSDKVERSION
WINDOWSSDKBINPATH
WINDOWSSDKVERBINPATH
WINDOWSSDKLIBVERSION
__DOTNET_ADD_32BIT
__DOTNET_ADD_64BIT
__DOTNET_PREFERRED_BITNESS
FRAMEWORK{framework version}VERSION
These last 2 are set to ensure that distuils uses these environment
variables when compiling libopenzwave.pyd
MSSDK
DISTUTILS_USE_SDK
:return: dict of environment variables
"""
from setuptools.msvc import EnvironmentInfo
env_info = EnvironmentInfo(
self.architecture,
vc_min_ver=self.min_visual_c_version
)
target_platform_path = self.target_platform_path
target_platform = self.target_platform
bin_path = os.path.join(target_platform_path, 'bin')
env = env_info.return_env()
for key, value in list(env.items())[:]:
del env[key]
env[key.upper()] = value
env['PLATFORM'] = self.architecture
env['WINDOWSSDKBINPATH'] = bin_path
env['MSSDK'] = target_platform_path
env['WINDOWSSDKLIBVERSION'] = self.windows_sdk_version + '\\'
if os.path.exists(os.path.join(bin_path, target_platform)):
env['WINDOWSSDKVERBINPATH'] = os.path.join(
bin_path,
target_platform
)
else:
env['WINDOWSSDKVERBINPATH'] = bin_path
env['VCTOOLSREDISTDIR'] = self.vc_tools_redist_path
env['VCINSTALLDIR'] = self.visual_c_path
env['VCTOOLSINSTALLDIR'] = self.tools_install_path
env['VCTOOLSVERSION'] = os.path.split(env['VCTOOLSINSTALLDIR'])[1]
env['WINDOWSSDKDIR'] = target_platform_path
env['WINDOWSSDKVERSION'] = env['WINDOWSSDKLIBVERSION']
base_include = os.path.join(
target_platform_path,
'include',
target_platform,
)
if not os.path.exists(base_include):
base_include = os.path.join(
target_platform_path,
'include'
)
if os.path.exists(base_include):
ucrt = os.path.join(base_include, 'ucrt')
shared = os.path.join(base_include, 'shared')
um = os.path.join(base_include, 'um')
if os.path.exists(ucrt) and ucrt not in env['INCLUDE']:
env['INCLUDE'] += ';' + ucrt
if os.path.exists(shared) and shared not in env['INCLUDE']:
env['INCLUDE'] += ';' + shared
if os.path.exists(um) and um not in env['INCLUDE']:
env['INCLUDE'] += ';' + um
base_lib = os.path.join(
target_platform_path,
'lib',
target_platform,
)
if not os.path.exists(base_lib):
base_lib = os.path.join(
target_platform_path,
'lib'
)
if os.path.exists(base_lib):
ucrt = os.path.join(base_lib, 'ucrt', self.architecture)
um = os.path.join(base_lib, 'um', self.architecture)
if os.path.exists(ucrt) and ucrt not in env['LIB']:
env['LIB'] += ';' + ucrt
if os.path.exists(um) and um not in env['LIB']:
env['LIB'] += ';' + um
arc_bin_ver_sdk = os.path.join(
env['WINDOWSSDKVERBINPATH'],
self.architecture
)
arc_bin_sdk = os.path.join(
env['WINDOWSSDKBINPATH'],
self.architecture
)
if (
os.path.exists(arc_bin_ver_sdk) and
arc_bin_ver_sdk not in env['PATH']
):
env['PATH'] += ';' + arc_bin_ver_sdk
if (
os.path.exists(arc_bin_sdk) and
arc_bin_sdk not in env['PATH']
):
env['PATH'] += ';' + arc_bin_sdk
union_meta_data = os.path.join(
target_platform_path,
'UnionMetadata',
target_platform
)
references = os.path.join(
target_platform_path,
'References',
target_platform
)
if os.path.exists(union_meta_data):
env['WINDOWSLIBPATH'] = union_meta_data + ';'
if os.path.exists(references):
if 'WINDOWSLIBPATH' not in env:
env['WINDOWSLIBPATH'] = ''
env['WINDOWSLIBPATH'] += references + ';'
if self.architecture == 'x86':
env['FRAMEWORKVERSION32'] = self.framework_version_32
env['FRAMEWORKDIR32'] = self.framework_dir_32
env['__DOTNET_ADD_32BIT'] = '1'
env['__DOTNET_PREFERRED_BITNESS'] = '32'
env['FRAMEWORKDIR'] = env['FRAMEWORKDIR32']
env['FRAMEWORKVERSION'] = env['FRAMEWORKVERSION32']
else:
env['FRAMEWORKVERSION64'] = self.framework_version_64
env['FRAMEWORKDIR64'] = self.framework_dir_64
env['__DOTNET_ADD_64BIT'] = '1'
env['__DOTNET_PREFERRED_BITNESS'] = '64'
env['FRAMEWORKDIR'] = env['FRAMEWORKDIR64']
env['FRAMEWORKVERSION'] = env['FRAMEWORKVERSION64']
framework = env['FRAMEWORKVERSION'][1:].split('.')[:2]
framework_version_key = (
'FRAMEWORK{framework}VERSION'.format(framework=''.join(framework))
)
env[framework_version_key] = 'v' + '.'.join(framework)
framework_lib_path = os.path.join(
env['FRAMEWORKDIR'],
env['FRAMEWORKVERSION']
)
if framework_lib_path not in env['LIBPATH']:
env['LIBPATH'] += ';' + framework_lib_path
env['DISTUTILS_USE_SDK'] = '1'
return env
@property
def windows_sdks(self):
"""
Windows SDK versions that are compatible with Visual C
:return: compatible Windows SDK versions
"""
ver = self.visual_c_version
if ver <= 9.0:
return '7.0', '6.1', '6.0a'
elif ver == 10.0:
return '7.1', '7.0a'
elif ver == 11.0:
return '8.0', '8.0a'
elif ver == 12.0:
return '8.1', '8.1a'
elif ver >= 14.0:
return '10.0', '8.1'
@property
def target_platform(self):
"""
This is used in the solution file to tell the compiler what SDK to use.
We obtain a list of compatible Windows SDK versions for the
Visual C version. We check and see if any of the compatible SDK's are
installed and if so we return that version.
:return: Installed Windows SDK version
"""
for sdk in self.windows_sdks:
sdk_version = _get_reg_value(
'Microsoft SDKs\\Windows\\v' + sdk,
'ProductVersion'
)
if sdk == '10.0':
return sdk_version + '.0'
else:
return sdk
raise RuntimeError(
'Unable to locate suitable SDK %s' % (self.windows_sdks,)
)
@property
def windows_sdk_version(self):
"""
This is almost identical to target_platform. Except it returns the
actual version of the Windows SDK not the truncated version.
:return: actual Windows SDK version
"""
for sdk in self.windows_sdks:
sdk_version = _get_reg_value(
'Microsoft SDKs\\Windows\\v' + sdk,
'ProductVersion'
)
return sdk_version + '.0'
raise RuntimeError(
'Unable to locate suitable SDK %s' % (self.windows_sdks,)
)
@property
def target_platform_path(self):
"""
Path to the Windows SDK version that has been found.
:return: Windows SDK path
"""
for sdk in self.windows_sdks:
sdk_installation_folder = _get_reg_value(
'Microsoft SDKs\\Windows\\v' + sdk,
'InstallationFolder'
)
if sdk_installation_folder:
return sdk_installation_folder[:-1]
raise RuntimeError(
'Unable to locate suitable SDK %s' % (self.windows_sdks,)
)
@property
def tools_version(self):
"""
Used in the solution to identify the compiler version
:return: MSBuild version
"""
return self.msbuild_version
@property
def visual_c_path(self):
"""
Visual C path
:return: Visual C path
"""
visual_c_version = str(self.visual_c_version)
if visual_c_version == '15.0':
visual_c_path = _get_reg_value(
'VisualStudio\SxS\VS7',
visual_c_version
)
if visual_c_path:
visual_c_path = os.path.join(visual_c_path, 'VC')
else:
visual_c_path = _get_reg_value(
'VisualStudio\\SxS\\VC7',
visual_c_version
)
if not visual_c_path:
raise RuntimeError('Unable to locate Visual C Installation')
return visual_c_path
@property
def msbuild_path(self):
"""
MSBuild path
:return: MSBuild path
"""
from setuptools.msvc import EnvironmentInfo
env_info = EnvironmentInfo(
self.architecture,
vc_min_ver=self.min_visual_c_version
)
x64 = self.architecture == 'x64'
msbuild_path = env_info.MSBuild
if msbuild_path:
msbuild_path = msbuild_path[0]
else:
msbuild_path = self.tools_install_path
for root, dirs, files in os.walk(msbuild_path):
if (
((x64 and 'amd64' in root) or 'amd64' not in root) and
'MSBuild.exe' in files
):
msbuild_path = os.path.join(root, 'MSBuild.exe')
break
else:
raise RuntimeError('Unable to locate MSBuild.exe')
return msbuild_path
def _command(self, solution):
with open(solution, 'r') as f:
self._win32 = 'Win32' in f.read()
template = (
'"{msbuild_path}" '
'"{solution}" '
'/property:Configuration={configuration} '
'/property:Platform={platform} '
'/t:'
)
command = template.format(
msbuild_path=self.msbuild_path,
solution=solution,
configuration=self.configuration,
platform=self.platform
)
return command
def get_clean_command(self, solution):
"""
Command to instruct MSBuild to clean the solution
:return: subprocess command
"""
return self._command(solution) + 'Clean'
def get_build_command(self, solution):
"""
Command to instruct MSBuild to compile the solution.
:return: subprocess command
"""
return self._command(solution) + 'Build'
def update_solution(self, src, dst):
"""
Updates a Visual Studio Solution
This currently only supports Visual Studio 2012 and newer.
This is going to update the solution to include x64 build
configurations and put into place any additional includes/libs that are
needed when compiling.
:param src: the path to the directory containing the solution files
:param dst: output path.
:return: path to the sln file, path to the build folder
"""
if os.path.exists(dst):
shutil.rmtree(dst, True)
shutil.copytree(src, dst)
for sln_file in os.listdir(dst):
if sln_file.endswith('.sln'):
break
else:
raise RuntimeError('Unable to locate sln file')
for vcxproj_file in os.listdir(dst):
if vcxproj_file.endswith('.vcxproj'):
break
else:
raise RuntimeError('Unable to locate vcxproj file')
sln_file = os.path.join(dst, sln_file)
update_vs_solution(sln_file)
update_vs_project(self, os.path.join(dst, vcxproj_file))
if self.architecture == 'x64':
return sln_file, os.path.join(dst, 'x64', self.configuration)
else:
return sln_file, os.path.join(dst, self.configuration)
def __str__(self):
template = (
'Machine architecture: {machine_architecture}\n'
'Build architecture: {architecture}\n'
'Build configuration: {platform}|{configuration}\n'
'\n'
'== Windows SDK ================================================\n'
' version: {target_platform}-{windows_sdk_version}\n'
' path: {target_platform_path}\n'
'\n'
'== .NET =======================================================\n'
' version: {target_framework}\n'
'\n'
' -- x86 -----------------------------------------------------\n'
' version: {framework_version_32}\n'
' path: {framework_dir_32}\n'
' -- x64 -----------------------------------------------------\n'
' version: {framework_version_64}\n'
' path: {framework_dir_64}\n'
'\n'
'== Visual C ===================================================\n'
' version: {visual_c_version}\n'
' path: {visual_c_path}\n'
'\n'
' -- Tools ---------------------------------------------------\n'
' version: {tools_version}\n'
' path: {tools_install_path}\n'
' redist path: {vc_tools_redist_path}\n'
' -- DLL -----------------------------------------------------\n'
' version: {platform_toolset}-{msvc_dll_version}\n'
' path: {msvc_dll_path}\n'
'\n'
'== MSBuild ====================================================\n'
' version: {msbuild_version}\n'
' path: {msbuild_path}\n'
'\n'
'== Python =====================================================\n'
' version: {py_version}\n'
' architecture: {py_architecture}\n'
' library: {py_dependency}\n'
' libs: {py_libraries}\n'
' includes: {py_includes}\n'
'\n'
)
return template.format(
machine_architecture=self.machine_architecture,
architecture=self.architecture,
platform=self.platform,
configuration=self.configuration,