forked from malrsrch/pywin32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
2604 lines (2417 loc) · 120 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
build_id="219" # may optionally include a ".{patchno}" suffix.
# Putting buildno at the top prevents automatic __doc__ assignment, and
# I *want* the build number at the top :)
__doc__="""This is a distutils setup-script for the pywin32 extensions
To build the pywin32 extensions, simply execute:
python setup.py -q build
or
python setup.py -q install
to build and install into your current Python installation.
These extensions require a number of libraries to build, some of which may
require you to install special SDKs or toolkits. This script will attempt
to build as many as it can, and at the end of the build will report any
extension modules that could not be built and why.
This has got complicated due to the various different versions of
Visual Studio used - some VS versions are not compatible with some SDK
versions. Below are the Windows SDK versions required (and the URL - although
these are subject to being changed by MS at any time:)
Python 2.4->2.5:
Microsoft Windows Software Development Kit Update for Windows Vista (version 6.0)
http://www.microsoft.com/downloads/en/details.aspx?FamilyID=4377f86d-c913-4b5c-b87e-ef72e5b4e065
Python 2.6+:
Microsoft Windows SDK for Windows 7 and .NET Framework 4 (version 7.1)
http://www.microsoft.com/downloads/en/details.aspx?FamilyID=6b6c21d2-2006-4afa-9702-529fa782d63b
If you multiple SDK versions on a single machine, set the MSSDK environment
variable to point at the one you want to use. Note that using the SDK for
a particular platform (eg, Windows 7) doesn't force you to use that OS as your
build environment. If the links above don't work, use google to find them.
Building:
---------
To install the pywin32 extensions, execute:
python setup.py -q install
This will install the built extensions into your site-packages directory,
create an appropriate .pth file, and should leave everything ready to use.
There is no need to modify the registry.
To build or install debug (_d) versions of these extensions, ensure you have
built or installed a debug version of Python itself, then pass the "--debug"
flag to the build command - eg:
python setup.py -q build --debug
or to build and install a debug version:
python setup.py -q build --debug install
To build 64bit versions of this:
* py2.5 and earlier - sorry, I've given up in disgust. Using VS2003 with
the Vista SDK is just too painful to make work, and VS2005 is not used for
any released versions of Python. See revision 1.69 of this file for the
last version that attempted to support and document this process.
* 2.6 and later: On a 64bit OS, just build as you would on a 32bit platform.
On a 32bit platform (ie, to cross-compile), you must use VS2008 to
cross-compile Python itself. Note that by default, the 64bit tools are not
installed with VS2008, so you may need to adjust your VS2008 setup. Then
use:
setup.py build --plat-name=win-amd64
see the distutils cross-compilation documentation for more details.
"""
# Originally by Thomas Heller, started in 2000 or so.
import os, string, sys
import types, glob
import re
from tempfile import gettempdir
import shutil
is_py3k = sys.version_info > (3,) # get this out of the way early on...
# We have special handling for _winreg so our setup3.py script can avoid
# using the 'imports' fixer and therefore start much faster...
if is_py3k:
import winreg as _winreg
else:
import _winreg
# The rest of our imports.
from distutils.core import setup, Extension, Command
from distutils.command.install import install
from distutils.command.install_lib import install_lib
from distutils.command.build_ext import build_ext
from distutils.command.build import build
from distutils.command.install_data import install_data
from distutils.command.build_py import build_py
from distutils.command.build_scripts import build_scripts
try:
from distutils.command.bdist_msi import bdist_msi
except ImportError:
# py24 and earlier
bdist_msi = None
from distutils.msvccompiler import get_build_version
from distutils import log
# some modules need a static CRT to avoid problems caused by them having a
# manifest.
static_crt_modules = ["winxpgui"]
from distutils.dep_util import newer_group, newer
from distutils import dir_util, file_util
from distutils.sysconfig import get_python_lib
from distutils.filelist import FileList
from distutils.errors import DistutilsExecError
import distutils.util
build_id_patch = build_id
if not "." in build_id_patch:
build_id_patch = build_id_patch + ".0"
pywin32_version="%d.%d.%s" % (sys.version_info[0], sys.version_info[1],
build_id_patch)
print "Building pywin32", pywin32_version
try:
this_file = __file__
except NameError:
this_file = sys.argv[0]
this_file = os.path.abspath(this_file)
# We get upset if the cwd is not our source dir, but it is a PITA to
# insist people manually CD there first!
if os.path.dirname(this_file):
os.chdir(os.path.dirname(this_file))
# Start address we assign base addresses from. See comment re
# dll_base_address later in this file...
dll_base_address = 0x1e200000
# We need to know the platform SDK dir before we can list the extensions.
def find_platform_sdk_dir():
# Finding the Platform SDK install dir is a treat. There can be some
# dead ends so we only consider the job done if we find the "windows.h"
# landmark.
DEBUG = False # can't use log.debug - not setup yet
landmark = r"include\um\windows.h"
# 1. The use might have their current environment setup for the
# SDK, in which case the "WindowsSdkDir" env var is set.
sdkdir = os.environ.get("WINDOWSSDKDIR")
if sdkdir:
if DEBUG:
print "PSDK: try %%WindowsSdkDir%%: '%s'" % sdkdir
if os.path.isfile(os.path.join(sdkdir, landmark)):
return sdkdir
# 2. The "Install Dir" value in the
# HKLM\Software\Microsoft\MicrosoftSDK\Directories registry key
# sometimes points to the right thing. However, after upgrading to
# the "Platform SDK for Windows Server 2003 SP1" this is dead end.
try:
key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,
r"Software\Microsoft\MicrosoftSDK\Directories")
sdkdir, ignore = _winreg.QueryValueEx(key, "Install Dir")
except EnvironmentError:
pass
else:
if DEBUG:
print r"PSDK: try 'HKLM\Software\Microsoft\MicrosoftSDK"\
"\Directories\Install Dir': '%s'" % sdkdir
if os.path.isfile(os.path.join(sdkdir, landmark)):
return sdkdir
# 3. Each installed SDK (not just the platform SDK) seems to have GUID
# subkey of HKLM\Software\Microsoft\MicrosoftSDK\InstalledSDKs and
# it *looks* like the latest installed Platform SDK will be the
# only one with an "Install Dir" sub-value.
try:
key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,
r"Software\Microsoft\MicrosoftSDK\InstalledSDKs")
i = 0
while True:
guid = _winreg.EnumKey(key, i)
guidkey = _winreg.OpenKey(key, guid)
try:
sdkdir, ignore = _winreg.QueryValueEx(guidkey, "Install Dir")
except EnvironmentError:
pass
else:
if DEBUG:
print r"PSDK: try 'HKLM\Software\Microsoft\MicrosoftSDK"\
"\InstallSDKs\%s\Install Dir': '%s'"\
% (guid, sdkdir)
if os.path.isfile(os.path.join(sdkdir, landmark)):
return sdkdir
i += 1
except EnvironmentError:
pass
# 4. Vista's SDK
try:
key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,
r"Software\Microsoft\Microsoft SDKs\Windows")
sdkdir, ignore = _winreg.QueryValueEx(key, "CurrentInstallFolder")
except EnvironmentError:
pass
else:
if DEBUG:
print r"PSDK: try 'HKLM\Software\Microsoft\MicrosoftSDKs"\
"\Windows\CurrentInstallFolder': '%s'" % sdkdir
if os.path.isfile(os.path.join(sdkdir, landmark)):
return sdkdir
# 5. Failing this just try a few well-known default install locations.
progfiles = os.environ.get("ProgramFiles", r"C:\Program Files")
defaultlocs = [
os.path.join(progfiles, "Microsoft Platform SDK"),
os.path.join(progfiles, "Microsoft SDK"),
]
for sdkdir in defaultlocs:
if DEBUG:
print "PSDK: try default location: '%s'" % sdkdir
if os.path.isfile(os.path.join(sdkdir, landmark)):
return sdkdir
# Some nasty hacks to prevent most of our extensions using a manifest, as
# the manifest - even without a reference to the CRT assembly - is enough
# to prevent the extension from loading. For more details, see
# http://bugs.python.org/issue7833 - that issue has a patch, but it is
# languishing and will probably never be fixed for Python 2.6...
if sys.version_info > (2,6):
from distutils.spawn import spawn
from distutils.msvc9compiler import MSVCCompiler
MSVCCompiler._orig_spawn = MSVCCompiler.spawn
MSVCCompiler._orig_link = MSVCCompiler.link
# We need to override this method for versions where issue7833 *has* landed
# (ie, 2.7 and 3.2+)
def manifest_get_embed_info(self, target_desc, ld_args):
_want_assembly_kept = getattr(self, '_want_assembly_kept', False)
if not _want_assembly_kept:
return None
for arg in ld_args:
if arg.startswith("/MANIFESTFILE:"):
orig_manifest = arg.split(":", 1)[1]
if target_desc==self.EXECUTABLE:
rid = 1
else:
rid = 2
return orig_manifest, rid
return None
# always monkeypatch it in even though it will only be called in 2.7
# and 3.2+.
MSVCCompiler.manifest_get_embed_info = manifest_get_embed_info
def monkeypatched_spawn(self, cmd):
is_link = cmd[0].endswith("link.exe") or cmd[0].endswith('"link.exe"')
is_mt = cmd[0].endswith("mt.exe") or cmd[0].endswith('"mt.exe"')
_want_assembly_kept = getattr(self, '_want_assembly_kept', False)
if not _want_assembly_kept and is_mt:
# We don't want mt.exe run...
return
if not _want_assembly_kept and is_link:
# remove /MANIFESTFILE:... and add MANIFEST:NO
# (but note that for winxpgui, which specifies a manifest via a
# .rc file, this is ignored by the linker - the manifest specified
# in the .rc file is still added)
for i in range(len(cmd)):
if cmd[i].startswith("/MANIFESTFILE:"):
cmd[i] = "/MANIFEST:NO"
break
if _want_assembly_kept and is_mt:
# We want mt.exe run with the original manifest
for i in range(len(cmd)):
if cmd[i] == "-manifest":
cmd[i+1] = cmd[i+1] + ".orig"
break
self._orig_spawn(cmd)
if _want_assembly_kept and is_link:
# We want a copy of the original manifest so we can use it later.
for i in range(len(cmd)):
if cmd[i].startswith("/MANIFESTFILE:"):
mfname = cmd[i][14:]
shutil.copyfile(mfname, mfname + ".orig")
break
def monkeypatched_link(self, target_desc, objects, output_filename, *args, **kw):
# no manifests for 3.3+
self._want_assembly_kept = sys.version_info < (3,3) and \
(os.path.basename(output_filename).startswith("PyISAPI_loader.dll") or \
os.path.basename(output_filename).startswith("perfmondata.dll") or \
os.path.basename(output_filename).startswith("win32ui.pyd") or \
target_desc==self.EXECUTABLE)
try:
return self._orig_link(target_desc, objects, output_filename, *args, **kw)
finally:
delattr(self, '_want_assembly_kept')
MSVCCompiler.spawn = monkeypatched_spawn
MSVCCompiler.link = monkeypatched_link
sdk_dir = find_platform_sdk_dir()
class WinExt (Extension):
# Base class for all win32 extensions, with some predefined
# library and include dirs, and predefined windows libraries.
# Additionally a method to parse .def files into lists of exported
# symbols, and to read
def __init__ (self, name, sources,
include_dirs=[],
define_macros=None,
undef_macros=None,
library_dirs=[],
libraries="",
runtime_library_dirs=None,
extra_objects=None,
extra_compile_args=None,
extra_link_args=None,
export_symbols=None,
export_symbol_file=None,
pch_header=None,
windows_h_version=None, # min version of windows.h needed.
extra_swig_commands=None,
is_regular_dll=False, # regular Windows DLL?
# list of headers which may not be installed forcing us to
# skip this extension
optional_headers=[],
base_address = None,
depends=None,
platforms=None, # none means 'all platforms'
unicode_mode=None, # 'none'==default or specifically true/false.
implib_name=None,
delay_load_libraries="",
):
libary_dirs = library_dirs,
include_dirs = ['com/win32com/src/include',
'win32/src'] + include_dirs
libraries=libraries.split()
self.delay_load_libraries=delay_load_libraries.split()
libraries.extend(self.delay_load_libraries)
if export_symbol_file:
export_symbols = export_symbols or []
export_symbols.extend(self.parse_def_file(export_symbol_file))
# Some of our swigged files behave differently in distutils vs
# MSVC based builds. Always define DISTUTILS_BUILD so they can tell.
define_macros = define_macros or []
define_macros.append(("DISTUTILS_BUILD", None))
define_macros.append(("_CRT_SECURE_NO_WARNINGS", None))
self.pch_header = pch_header
self.extra_swig_commands = extra_swig_commands or []
self.windows_h_version = windows_h_version
self.optional_headers = optional_headers
self.is_regular_dll = is_regular_dll
self.base_address = base_address
self.platforms = platforms
self.implib_name = implib_name
Extension.__init__ (self, name, sources,
include_dirs,
define_macros,
undef_macros,
library_dirs,
libraries,
runtime_library_dirs,
extra_objects,
extra_compile_args,
extra_link_args,
export_symbols)
self.depends = depends or [] # stash it here, as py22 doesn't have it.
self.unicode_mode = unicode_mode
def parse_def_file(self, path):
# Extract symbols to export from a def-file
result = []
for line in open(path).readlines():
line = line.rstrip()
if line and line[0] in string.whitespace:
tokens = line.split()
if not tokens[0][0] in string.ascii_letters:
continue
result.append(','.join(tokens))
return result
def get_source_files(self, dsp):
result = []
if dsp is None:
return result
dsp_path = os.path.dirname(dsp)
seen_swigs = []
for line in open(dsp, "r"):
fields = line.strip().split("=", 2)
if fields[0]=="SOURCE":
ext = os.path.splitext(fields[1])[1].lower()
if ext in ['.cpp', '.c', '.i', '.rc', '.mc']:
pathname = os.path.normpath(os.path.join(dsp_path, fields[1]))
result.append(pathname)
if ext == '.i':
seen_swigs.append(pathname)
# ack - .dsp files may have references to the generated 'foomodule.cpp'
# from 'foo.i' - but we now do things differently...
for ss in seen_swigs:
base, ext = os.path.splitext(ss)
nuke = base + "module.cpp"
try:
result.remove(nuke)
except ValueError:
pass
# Sort the sources so that (for example) the .mc file is processed first,
# building this may create files included by other source files.
build_order = ".i .mc .rc .cpp".split()
decorated = [(build_order.index(os.path.splitext(fname)[-1].lower()), fname)
for fname in result]
decorated.sort()
result = [item[1] for item in decorated]
return result
def finalize_options(self, build_ext):
# distutils doesn't define this function for an Extension - it is
# our own invention, and called just before the extension is built.
if not build_ext.mingw32:
if self.pch_header:
self.extra_compile_args = self.extra_compile_args or []
# /YX doesn't work in vs2008 or vs2003/64
if build_ext.plat_name == 'win32' and get_build_version() < 9.0:
self.extra_compile_args.append("/YX"+self.pch_header)
pch_name = os.path.join(build_ext.build_temp, self.name) + ".pch"
self.extra_compile_args.append("/Fp"+pch_name)
# bugger - add this to python!
if build_ext.plat_name=="win32":
self.extra_link_args.append("/MACHINE:x86")
else:
self.extra_link_args.append("/MACHINE:%s" % build_ext.plat_name[4:])
# Old vs2003 needs this defined (Python itself uses it)
if get_build_version() < 9.0 and build_ext.plat_name=="win-amd64":
self.extra_compile_args.append('/D_M_X64')
# Put our DLL base address in (but not for our executables!)
if self not in W32_exe_files:
base = self.base_address
if not base:
base = dll_base_addresses[self.name]
self.extra_link_args.append("/BASE:0x%x" % (base,))
# like Python, always use debug info, even in release builds
# (note the compiler doesn't include debug info, so you only get
# basic info - but its better than nothing!)
# For now use the temp dir - later we may package them, so should
# maybe move them next to the output file.
pch_dir = os.path.join(build_ext.build_temp)
if not build_ext.debug:
self.extra_compile_args.append("/Zi")
self.extra_compile_args.append("/Fd%s\%s_vc.pdb" %
(pch_dir, self.name))
self.extra_link_args.append("/DEBUG")
self.extra_link_args.append("/PDB:%s\%s.pdb" %
(pch_dir, self.name))
# enable unwind semantics - some stuff needs it and I can't see
# it hurting
self.extra_compile_args.append("/EHsc")
if self.delay_load_libraries:
self.libraries.append("delayimp")
for delay_lib in self.delay_load_libraries:
self.extra_link_args.append("/delayload:%s.dll" % delay_lib)
# If someone needs a specially named implib created, handle that
if self.implib_name:
implib = os.path.join(build_ext.build_temp, self.implib_name)
if build_ext.debug:
suffix = "_d"
else:
suffix = ""
self.extra_link_args.append("/IMPLIB:%s%s.lib" % (implib, suffix))
# Try and find the MFC source code, so we can reach inside for
# some of the ActiveX support we need. We need to do this late, so
# the environment is setup correctly.
# Only used by the win32uiole extensions, but I can't be
# bothered making a subclass just for this - so they all get it!
found_mfc = False
for incl in os.environ.get("INCLUDE", "").split(os.pathsep):
# first is a "standard" MSVC install, second is the Vista SDK.
for candidate in ("..\src\occimpl.h", "..\..\src\mfc\occimpl.h"):
check = os.path.join(incl, candidate)
if os.path.isfile(check):
self.extra_compile_args.append('/DMFC_OCC_IMPL_H=\\"%s\\"' % candidate)
found_mfc = True
break
if found_mfc:
break
# Handle Unicode - if unicode_mode is None, then it means True
# for py3k, false for py2
unicode_mode = self.unicode_mode
if unicode_mode is None:
unicode_mode = is_py3k
if unicode_mode:
self.extra_compile_args.append("/DUNICODE")
self.extra_compile_args.append("/D_UNICODE")
self.extra_compile_args.append("/DWINNT")
# Unicode, Windows executables seem to need this magic:
if "/SUBSYSTEM:WINDOWS" in self.extra_link_args:
self.extra_link_args.append("/ENTRY:wWinMainCRTStartup")
class WinExt_pythonwin(WinExt):
def __init__ (self, name, **kw):
if 'unicode_mode' not in kw:
kw['unicode_mode']=None
kw.setdefault("extra_compile_args", []).extend(
['-D_AFXDLL', '-D_AFXEXT','-D_MBCS'])
WinExt.__init__(self, name, **kw)
def get_pywin32_dir(self):
return "pythonwin"
class WinExt_win32(WinExt):
def __init__ (self, name, **kw):
WinExt.__init__(self, name, **kw)
def get_pywin32_dir(self):
return "win32"
class WinExt_ISAPI(WinExt):
def get_pywin32_dir(self):
return "isapi"
# Note this is used only for "win32com extensions", not pythoncom
# itself - thus, output is "win32comext"
class WinExt_win32com(WinExt):
def __init__ (self, name, **kw):
kw["libraries"] = kw.get("libraries", "") + " oleaut32 ole32"
# COM extensions require later windows headers.
if not kw.get("windows_h_version"):
kw["windows_h_version"] = 0x500
WinExt.__init__(self, name, **kw)
def get_pywin32_dir(self):
return "win32comext/" + self.name
# Exchange extensions get special treatment:
# * Look for the Exchange SDK in the registry.
# * Output directory is different than the module's basename.
# * Require use of the Exchange 2000 SDK - this works for both VC6 and 7
class WinExt_win32com_mapi(WinExt_win32com):
def __init__ (self, name, **kw):
# The Exchange 2000 SDK seems to install itself without updating
# LIB or INCLUDE environment variables. It does register the core
# directory in the registry tho - look it up
sdk_install_dir = None
libs = kw.get("libraries", "")
keyname = "SOFTWARE\Microsoft\Exchange\SDK"
flags = _winreg.KEY_READ
try:
flags |= _winreg.KEY_WOW64_32KEY
except AttributeError:
pass # this version doesn't support 64 bits, so must already be using 32bit key.
for root in _winreg.HKEY_LOCAL_MACHINE, _winreg.HKEY_CURRENT_USER:
try:
keyob = _winreg.OpenKey(root, keyname, 0, flags)
value, type_id = _winreg.QueryValueEx(keyob, "INSTALLDIR")
if type_id == _winreg.REG_SZ:
sdk_install_dir = value
break
except WindowsError:
pass
if sdk_install_dir is not None:
d = os.path.join(sdk_install_dir, "SDK", "Include")
if os.path.isdir(d):
kw.setdefault("include_dirs", []).insert(0, d)
d = os.path.join(sdk_install_dir, "SDK", "Lib")
if os.path.isdir(d):
kw.setdefault("library_dirs", []).insert(0, d)
# The stand-alone exchange SDK has these libs
if distutils.util.get_platform() == 'win-amd64':
# Additional utility functions are only available for 32-bit builds.
pass
else:
libs += " version user32 advapi32 Ex2KSdk sadapi netapi32"
kw["libraries"] = libs
WinExt_win32com.__init__(self, name, **kw)
def get_pywin32_dir(self):
# 'win32com.mapi.exchange' and 'win32com.mapi.exchdapi' currently only
# ones with this special requirement
return "win32comext/mapi"
class WinExt_win32com_axdebug(WinExt_win32com):
def __init__ (self, name, **kw):
# Later SDK versions again ship with activdbg.h, but if we attempt
# to use our own copy of that file with that SDK, we fail to link.
if os.path.isfile(os.path.join(sdk_dir, "include", 'um', "activdbg.h")):
kw.setdefault('extra_compile_args', []).append("/DHAVE_SDK_ACTIVDBG")
WinExt_win32com.__init__(self, name, **kw)
# A hacky extension class for pywintypesXX.dll and pythoncomXX.dll
class WinExt_system32(WinExt):
def get_pywin32_dir(self):
return "pywin32_system32"
################################################################
# Extensions to the distutils commands.
# Start with 2to3 related stuff for py3k.
do_2to3 = is_py3k
if do_2to3:
def refactor_filenames(filenames):
from lib2to3.refactor import RefactoringTool
# we only need some fixers.
fixers = """basestring exec except dict import imports next nonzero
print raise raw_input long standarderror types unicode
urllib xrange""".split()
fqfixers = ['lib2to3.fixes.fix_' + f for f in fixers]
options = dict(doctests_only=False, fix=[], list_fixes=[],
print_function=False, verbose=False,
write=True)
r = RefactoringTool(fqfixers, options)
for updated_file in filenames:
if os.path.splitext(updated_file)[1] not in ['.py', '.pys']:
continue
log.info("Refactoring %s" % updated_file)
try:
r.refactor_file(updated_file, write=True, doctests_only=False)
if os.path.exists(updated_file + ".bak"):
os.unlink(updated_file + ".bak")
except Exception:
log.warn("WARNING: Failed to 2to3 %s: %s" % (updated_file, sys.exc_info()[1]))
else:
# py2k - nothing to do.
def refactor_filenames(filenames):
pass
# 'build_py' command
if do_2to3:
# Force 2to3 to be run for py3k versions.
class my_build_py(build_py):
def finalize_options(self):
build_py.finalize_options(self)
# must force as the 2to3 conversion happens in place so an
# interrupted build can cause py2 syntax files in a py3k build.
self.force = True
def run(self):
self.updated_files = []
# Base class code
if self.py_modules:
self.build_modules()
if self.packages:
self.build_packages()
self.build_package_data()
# 2to3
refactor_filenames(self.updated_files)
# Remaining base class code
self.byte_compile(self.get_outputs(include_bytecode=0))
def build_module(self, module, module_file, package):
res = build_py.build_module(self, module, module_file, package)
if res[1]:
# file was copied
self.updated_files.append(res[0])
return res
else:
my_build_py = build_py # default version.
# 'build_scripts' command
if do_2to3:
class my_build_scripts(build_scripts):
def copy_file(self, src, dest):
dest, copied = build_scripts.copy_file(self, src, dest)
# 2to3
if not self.dry_run and copied:
refactor_filenames([dest])
return dest, copied
else:
my_build_scripts = build_scripts
# 'build' command
class my_build(build):
def run(self):
build.run(self)
# write a pywin32.version.txt.
ver_fname = os.path.join(gettempdir(), "pywin32.version.txt")
try:
f = open(ver_fname, "w")
f.write("%s\n" % build_id)
f.close()
except EnvironmentError, why:
print "Failed to open '%s': %s" % (ver_fname, why)
class my_build_ext(build_ext):
def finalize_options(self):
build_ext.finalize_options(self)
self.windows_h_version = None
# The pywintypes library is created in the build_temp
# directory, so we need to add this to library_dirs
self.library_dirs.append(self.build_temp)
self.mingw32 = (self.compiler == "mingw32")
if self.mingw32:
self.libraries.append("stdc++")
self.excluded_extensions = [] # list of (ext, why)
self.swig_cpp = True # hrm - deprecated - should use swig_opts=-c++??
if not hasattr(self, 'plat_name'):
# Old Python version that doesn't support cross-compile
self.plat_name = distutils.util.get_platform()
def _fixup_sdk_dirs(self):
# Adjust paths etc for the platform SDK - this prevents the user from
# needing to manually add these directories via the MSVC UI. Note
# that we currently ensure the SDK dirs are before the compiler
# dirs, so its no problem if they have added these dirs to the UI)
# (Note that just having them in INCLUDE/LIB does *not* work -
# distutils thinks it knows better, and resets those vars (see notes
# below about how the paths are put together)
# Called after the compiler is initialized, but before the extensions
# are built. NOTE: this means setting self.include_dirs etc will
# have no effect, so we poke our path changes directly into the
# compiler (we can't call this *before* the compiler is setup, as
# then our environment changes would have no effect - see below)
# distutils puts the path together like so:
# * compiler command line includes /I entries for each dir in
# ext.include_dir + build_ext.include_dir (ie, extension's come first)
# * The compiler initialization sets the INCLUDE/LIB etc env vars to the
# values read from the registry (ignoring anything that was there)
# We are also at the mercy of how MSVC processes command-line
# includes vs env vars (presumably environment comes last) - so,
# moral of the story:
# * To get a path at the start, it must be at the start of
# ext.includes
# * To get a path at the end, it must be at the end of
# os.environ("INCLUDE")
# Note however that the environment tweaking can only be done after
# the compiler has set these vars, which is quite late -
# build_ext.run() - so global environment hacks are done in our
# build_extensions() override)
#
# Also note that none of our extensions have individual include files
# that must be first - so for practical purposes, any entry in
# build_ext.include_dirs should 'win' over the compiler's dirs.
assert self.compiler.initialized # if not, our env changes will be lost!
is_64bit = self.plat_name == 'win-amd64'
extra = os.path.join(sdk_dir, 'include')
# should not be possible for the SDK dirs to already be in our
# include_dirs - they may be in the registry etc from MSVC, but
# those aren't reflected here...
assert extra not in self.include_dirs
# and we will not work as expected if the dirs don't exist
assert os.path.isdir(extra), "%s doesn't exist!" % (extra,)
self.compiler.add_include_dir(extra)
# and again for lib dirs.
extra = os.path.join(sdk_dir, 'lib')
if is_64bit:
extra = os.path.join(extra, 'x64')
# assert os.path.isdir(extra), extra
assert extra not in self.library_dirs # see above
# assert os.path.isdir(extra), "%s doesn't exist!" % (extra,)
# self.compiler.add_library_dir(extra)
# directx sdk sucks - how to locate it automatically?
# Must manually set DIRECTX_SDK_DIR for now.
# (but it appears November 2008 and later versions set DXSDK_DIR, so
# we allow both, allowing our "old" DIRECTX_SDK_DIR to override things
for dxsdk_dir_var in ("DIRECTX_SDK_DIR", "DXSDK_DIR"):
dxsdk_dir = os.environ.get(dxsdk_dir_var)
if dxsdk_dir:
extra = os.path.join(dxsdk_dir, 'include')
assert os.path.isdir(extra), "%s doesn't exist!" % (extra,)
self.compiler.add_include_dir(extra)
if is_64bit:
tail = 'x64'
else:
tail = 'x86'
extra = os.path.join(dxsdk_dir, 'lib', tail)
assert os.path.isdir(extra), "%s doesn't exist!" % (extra,)
self.compiler.add_library_dir(extra)
break
log.debug("After SDK processing, includes are %s", self.compiler.include_dirs)
log.debug("After SDK processing, libs are %s", self.compiler.library_dirs)
# Vista SDKs have a 'VC' directory with headers and libs for older
# compilers. We need to hack the support in here so that the
# directories are after the compiler's own. As noted above, the
# only way to ensure they are after the compiler's is to put them
# in the environment, which has the nice side-effect of working
# for the rc executable.
# We know its not needed on vs9...
if get_build_version() < 9.0:
if os.path.isdir(os.path.join(sdk_dir, 'VC', 'INCLUDE')):
os.environ["INCLUDE"] += ";" + os.path.join(sdk_dir, 'VC', 'INCLUDE')
log.debug("Vista SDK found: %%INCLUDE%% now %s", os.environ["INCLUDE"])
if os.path.isdir(os.path.join(sdk_dir, 'VC', 'LIB')):
os.environ["LIB"] += ";" + os.path.join(sdk_dir, 'VC', 'LIB')
log.debug("Vista SDK found: %%LIB%% now %s", os.environ["LIB"])
def _why_cant_build_extension(self, ext):
# Return None, or a reason it can't be built.
if ext.name in {'exchdapi', 'exchange', 'mapi'}:
# Dont need these modules, and they require headers not present in
# the windows SDK
return ext.name + " module is disabled"
include_dirs = self.compiler.include_dirs + \
os.environ.get("INCLUDE", "").split(os.pathsep)
if self.windows_h_version is None:
for d in include_dirs:
# We look for _WIN32_WINNT instead of WINVER as the Vista
# SDK defines _WIN32_WINNT as WINVER and we aren't that clever
# * Windows Server 2003 SDK sticks this version in WinResrc.h
# * Vista SDKs stick the version in sdkddkver.h
for header in ('WINDOWS.H', 'SDKDDKVER.H', "WinResrc.h"):
look = os.path.join(d, header)
if os.path.isfile(look):
# read the fist 100 lines, looking for #define WINVER 0xNN
# (Vista SDKs now define this based on _WIN32_WINNT,
# which should still be fine for old versions)
reob = re.compile("#define\W*_WIN32_WINNT\W*(0x[0-9a-fA-F]+)")
f = open(look, "r")
for i in range(500):
line = f.readline()
match = reob.match(line)
if match is not None:
self.windows_h_version = int(match.group(1), 16)
log.info("Found version 0x%x in %s" \
% (self.windows_h_version, look))
break
else:
log.debug("No version in %r - looking for another...", look)
if self.windows_h_version is not None:
break
if self.windows_h_version is not None:
break
else:
raise RuntimeError("Can't find a version in Windows.h")
if ext.windows_h_version is not None and \
ext.windows_h_version > self.windows_h_version:
return "WINDOWS.H with version 0x%x is required, but only " \
"version 0x%x is installed." \
% (ext.windows_h_version, self.windows_h_version)
look_dirs = include_dirs
for h in ext.optional_headers:
for d in look_dirs:
if os.path.isfile(os.path.join(d, h)):
break
else:
log.debug("Looked for %s in %s", h, look_dirs)
return "The header '%s' can not be located" % (h,)
common_dirs = self.compiler.library_dirs[:]
common_dirs += os.environ.get("LIB", "").split(os.pathsep)
patched_libs = []
for lib in ext.libraries:
if lib.lower() in self.found_libraries:
found = self.found_libraries[lib.lower()]
else:
look_dirs = common_dirs + ext.library_dirs
found = self.compiler.find_library_file(look_dirs, lib, self.debug)
if not found:
log.debug("Looked for %s in %s", lib, look_dirs)
return "No library '%s'" % lib
self.found_libraries[lib.lower()] = found
patched_libs.append(os.path.splitext(os.path.basename(found))[0])
if ext.platforms and self.plat_name not in ext.platforms:
return "Only available on platforms %s" % (ext.platforms,)
# We update the .libraries list with the resolved library name.
# This is really only so "_d" works.
ext.libraries = patched_libs
return None # no reason - it can be built!
def _build_scintilla(self):
path = 'pythonwin\\Scintilla'
makefile = 'makefile_pythonwin'
makeargs = []
if self.debug:
makeargs.append("DEBUG=1")
if not self.verbose:
makeargs.append("/C") # nmake: /C Suppress output messages
makeargs.append("QUIET=1")
# We build the DLL into our own temp directory, then copy it to the
# real directory - this avoids the generated .lib/.exp
build_temp = os.path.abspath(os.path.join(self.build_temp, "scintilla"))
self.mkpath(build_temp)
# Use short-names, as the scintilla makefiles barf with spaces.
if " " in build_temp:
# ack - can't use win32api!!! This is the best I could come up
# with:
# C:\>for %I in ("C:\Program Files",) do @echo %~sI
# C:\PROGRA~1
cs = os.environ.get('comspec', 'cmd.exe')
cmd = cs + ' /c for %I in ("' + build_temp + '",) do @echo %~sI'
build_temp = os.popen(cmd).read().strip()
assert os.path.isdir(build_temp), build_temp
makeargs.append("SUB_DIR_O=%s" % build_temp)
makeargs.append("SUB_DIR_BIN=%s" % build_temp)
makeargs.append("DIR_PYTHON=%s" % sys.prefix)
cwd = os.getcwd()
os.chdir(path)
try:
cmd = ["nmake.exe", "/nologo", "/f", makefile] + makeargs
self.spawn(cmd)
finally:
os.chdir(cwd)
# The DLL goes in the Pythonwin directory.
if self.debug:
base_name = "scintilla_d.dll"
else:
base_name = "scintilla.dll"
self.copy_file(
os.path.join(self.build_temp, "scintilla", base_name),
os.path.join(self.build_lib, "pythonwin"))
def _build_pycom_loader(self):
# the base compiler strips out the manifest from modules it builds
# which can't be done for this module - having the manifest is the
# reason it needs to exist!
# At least this is made easier by it not depending on Python itself,
# so the compile and link are simple...
suffix = "%d%d" % (sys.version_info[0], sys.version_info[1])
if self.debug:
suffix += '_d'
src = r"com\win32com\src\PythonCOMLoader.cpp"
build_temp = os.path.abspath(self.build_temp)
obj = os.path.join(build_temp, os.path.splitext(src)[0]+".obj")
dll = os.path.join(self.build_lib, "pywin32_system32", "pythoncomloader"+suffix+".dll")
if self.force or newer_group([src], obj, 'newer'):
ccargs = [self.compiler.cc, '/c']
if self.debug:
ccargs.extend(self.compiler.compile_options_debug)
else:
ccargs.extend(self.compiler.compile_options)
ccargs.append('/Fo' + obj)
ccargs.append(src)
ccargs.append('/DDLL_DELEGATE=\\"pythoncom%s.dll\\"' % (suffix,))
self.spawn(ccargs)
deffile = r"com\win32com\src\PythonCOMLoader.def"
if self.force or newer_group([obj, deffile], dll, 'newer'):
largs = [self.compiler.linker, '/DLL', '/nologo', '/incremental:no']
if self.debug:
largs.append("/DEBUG")
temp_manifest = os.path.join(build_temp, os.path.basename(dll) + ".manifest")
largs.append('/MANIFESTFILE:' + temp_manifest)
largs.append('/PDB:None')
largs.append("/OUT:" + dll)
largs.append("/DEF:" + deffile)
largs.append("/IMPLIB:" + os.path.join(build_temp, "PythonCOMLoader"+suffix+".lib"))
largs.append(obj)
self.spawn(largs)
# and the manifest if one exists.
if os.path.isfile(temp_manifest):
out_arg = '-outputresource:%s;2' % (dll,)
self.spawn(['mt.exe', '-nologo', '-manifest', temp_manifest, out_arg])
def build_extensions(self):
# First, sanity-check the 'extensions' list
self.check_extensions_list(self.extensions)
self.found_libraries = {}
if not hasattr(self.compiler, 'initialized'):
# 2.3 and earlier initialized at construction
self.compiler.initialized = True
else:
if not self.compiler.initialized:
self.compiler.initialize()
if sdk_dir:
self._fixup_sdk_dirs()
# Here we hack a "pywin32" directory (one of 'win32', 'win32com',
# 'pythonwin' etc), as distutils doesn't seem to like the concept
# of multiple top-level directories.
assert self.package is None
for ext in self.extensions:
try:
self.package = ext.get_pywin32_dir()
except AttributeError:
raise RuntimeError("Not a win32 package!")
self.build_extension(ext)
for ext in W32_exe_files:
ext.finalize_options(self)
why = self._why_cant_build_extension(ext)
if why is not None:
self.excluded_extensions.append((ext, why))
assert why, "please give a reason, or None"
print "Skipping %s: %s" % (ext.name, why)
continue
try: