forked from mhammond/pywin32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
2602 lines (2397 loc) · 118 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
from __future__ import print_function
build_id="227.1" # may optionally include a ".{patchno}" suffix.
# Putting build_id 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.
Note that Python 2.7 is the only version of Python 2.x which is supported, and
that Python 3.5 is the earliest Python 3.x supported.
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.
At a minimum, you must have the same version of Microsoft Visual C++ that is
used for the Python version you are building, plus the Windows 8.1 SDK. Note
that the Windows 8.1 SDK is often bundled with Microsoft Visual C++.
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:
On a 64bit OS, just build as you would on a 32bit platform.
On a 32bit platform it may be possible to cross-compile, but this hasn't
been tested in many years. Regardless, this ability comes directly from
distutils/setuptools, so see their documentation for more details.
Creating Distributions:
-----------------------
The make_all.bat batch file will build and create distributions.
Once a distribution has been built and tested, you should ensure that
'git status' shows no dirty files, then create a tag with the format 'bXXX'
The executable installers are uploaded to github.
The "wheel" packages are uploaded to pypi using `twine upload dist/path-to.whl`
"""
# Originally by Thomas Heller, started in 2000 or so.
import os
import string
import sys
import glob
import re
from tempfile import gettempdir
import platform
import shutil
import subprocess
is_py3k = sys.version_info > (3,) # get this out of the way early on...
try:
import winreg # py3k
except ImportError:
import _winreg as winreg # py2k
# The rest of our imports.
from setuptools import setup
from distutils.core import Extension
from setuptools.command.install import install
from setuptools.command.install_lib import install_lib
from setuptools.command.build_ext import build_ext
from setuptools.command.build_py import build_py
from distutils.command.build import build
from distutils.command.install_data import install_data
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
from distutils.sysconfig import get_config_vars
from distutils.filelist import FileList
from distutils.errors import DistutilsExecError, DistutilsSetupError
import distutils.util
# prevent the new in 3.5 suffix of "cpXX-win32" from being added.
# (adjusting both .cp35-win_amd64.pyd and .cp35-win32.pyd to .pyd)
try:
get_config_vars()["EXT_SUFFIX"] = re.sub("\\.cp\d\d-win((32)|(_amd64))", "", get_config_vars()["EXT_SUFFIX"])
except KeyError:
pass # no EXT_SUFFIX in this build.
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():
# The user might have their current environment setup for the
# SDK, in which case "MSSDK_INCLUDE" and "MSSDK_LIB" vars must be set.
if "MSSDK_INCLUDE" in os.environ and "MSSDK_LIB" in os.environ:
print("Using SDK as specified in the environment")
return {
"include": os.environ["MSSDK_INCLUDE"].split(os.path.pathsep),
"lib": os.environ["MSSDK_LIB"].split(os.path.pathsep),
}
# Windows SDKs up to version 7 use a reg key SOFTWARE\Microsoft\Microsoft SDKs\Windows
# SDKs 8 and later use SOFTWARE\Microsoft\Windows Kits\Installed Roots
# We currently target version 8.1
# (and strangely, via #1293, it appears there may be a 32 bit version of
# the SDK available which works OK on 64 bit machines!)
flags_variants = [winreg.KEY_READ]
try:
flags_variants.append(winreg.KEY_READ | winreg.KEY_WOW64_32KEY)
except AttributeError:
pass
for flags in flags_variants:
try:
key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE,
r"SOFTWARE\Microsoft\Windows Kits\Installed Roots",
0,
flags)
installRoot = winreg.QueryValueEx(key, "KitsRoot81")[0]
break
except EnvironmentError:
pass
else:
print("Can't find a windows 8.1 sdk")
return None
# no idea what these 'um' and 'winv6.3' paths actually mean and whether
# hard-coding them is appropriate, but here we are...
include = [os.path.join(installRoot, "include", "um")]
if not os.path.exists(os.path.join(include[0], "windows.h")):
print("Found Windows 8.1 sdk in", include, "but it doesn't appear to have windows.h")
return None
include.append(os.path.join(installRoot, "include", "shared"))
lib = [os.path.join(installRoot, "lib", "winv6.3", "um")]
return {"include": include, "lib": lib}
# 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 (which has landed for Python 2.7 and on 3.2
# and later, which are all we care about currently)
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_info = find_platform_sdk_dir()
if not sdk_info:
print()
print("It looks like you are trying to build pywin32 in an environment without")
print("the necessary tools installed. It's much easier to grab binaries!")
print()
print("Please read the docstring at the top of this file, or read README.md")
print("for more information.")
print()
raise RuntimeError("Can't find the Windows SDK")
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="",
):
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)
extra_link_args = extra_link_args or []
if export_symbol_file:
extra_link_args.append("/DEF:" + 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 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 []
# 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:])
# 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")
# silence: warning C4163: '__cpuidex' : not available as an intrinsic function
self.extra_compile_args.append("/wd4163")
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 headers, 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 (r"..\src\occimpl.h", r"..\..\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
if self.unicode_mode or self.unicode_mode is None and is_py3k:
self.extra_compile_args.append("-DUNICODE")
self.extra_compile_args.append("-D_UNICODE")
self.extra_compile_args.append("-DWINNT")
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_pythonwin_subsys_win(WinExt_pythonwin):
def finalize_options(self, build_ext):
WinExt_pythonwin.finalize_options(self, build_ext)
if build_ext.mingw32:
self.extra_link_args.append('-mwindows')
else:
self.extra_link_args.append('/SUBSYSTEM:WINDOWS')
if self.unicode_mode or self.unicode_mode is None and is_py3k:
# Unicode, Windows executables seem to need this magic:
self.extra_link_args.append('/ENTRY:wWinMainCRTStartup')
class WinExt_win32(WinExt):
def __init__ (self, name, **kw):
WinExt.__init__(self, name, **kw)
def get_pywin32_dir(self):
return "win32"
class WinExt_win32_subsys_con(WinExt_win32):
def finalize_options(self, build_ext):
WinExt_win32.finalize_options(self, build_ext)
if build_ext.mingw32:
self.extra_link_args.append('-mconsole')
if self.unicode_mode:
self.extra_link_args.append('-municode')
else:
self.extra_link_args.append('/SUBSYSTEM:CONSOLE')
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
# NOTE: sadly the old Exchange SDK does *not* include MAPI files - these used
# to be bundled with the Windows SDKs and/or Visual Studio, but no longer are.
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 = r"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"
# 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 as 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 - the default paths used by
# distutils don't include the platform SDK.
# 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'
for extra in sdk_info["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.
for extra in sdk_info["lib"]:
extra = os.path.join(extra, 'x64' if is_64bit else 'x86')
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)
log.debug("After SDK processing, includes are %s", self.compiler.include_dirs)
log.debug("After SDK processing, libs are %s", self.compiler.library_dirs)
def _why_cant_build_extension(self, ext):
# Return None, or a reason it can't be built.
# Exclude exchange 32-bit utility libraries from 64-bit
# builds. Note that the exchange module now builds, but only
# includes interfaces for 64-bit builds.
if self.plat_name == 'win-amd64' and ext.name == 'exchdapi':
return "No 64-bit library for utility functions available."
if get_build_version() >=14:
if ext.name == 'exchange':
ext.libraries.append('legacy_stdio_definitions')
elif ext.name == 'exchdapi':
return "Haven't worked out how to build on vs2015"
include_dirs = self.compiler.include_dirs + \
os.environ.get("INCLUDE", "").split(os.pathsep)
if self.windows_h_version is None:
# Note that we used to try and find WINVER or _WIN32_WINNT macros
# here defining the version of the Windows SDK we use and check
# it was late enough for the extension being built. But since we
# moved to the Windows 8.1 SDK (or later), this isn't necessary
# as all modules require less than this.
pass
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 = "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 = "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 lookupMfcInVisualStudio(self, mfc_version, mfc_libraries):
# Looking for the MFC files in the installation paths of the Visual Studios
plat_dir_64 = "x64"
mfc_dir = "Microsoft.{}.MFC".format(mfc_version.upper())
mfc_contents = []
# 2.7, 3.0, 3.1 and 3.2 all use(d) vs2008 (compiler version 1500)
if sys.version_info < (3, 3):
product_key = "SOFTWARE\\Microsoft\\VisualStudio\\9.0\\Setup\\VC"
plat_dir_64 = "amd64"
mfc_files = mfc_libraries + ["Microsoft.VC90.MFC.manifest", ]
# 3.3 and 3.4 use(d) vs2010 (compiler version 1600, crt=10)
elif sys.version_info < (3, 5):
product_key = "SOFTWARE\\Microsoft\\VisualStudio\\10.0\\Setup\\VC"
mfc_files = mfc_libraries
# 3.5 and later on vs2015 (compiler version 1900, crt=14)
else:
product_key = "SOFTWARE\\Microsoft\\VisualStudio\\14.0\\Setup\\VC"
mfc_files = mfc_libraries
# On a 64bit host, the value we are looking for is actually in
# SysWow64Node - but that is only available on xp and later.
access = winreg.KEY_READ
if sys.getwindowsversion()[0] >= 5:
access = access | 512 # KEY_WOW64_32KEY
if self.plat_name == 'win-amd64':
plat_dir = plat_dir_64
else:
plat_dir = "x86"
# Find the redist directory.
vckey = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE,
product_key,
0,
access,
)
val = winreg.QueryValueEx(vckey, "ProductDir")[0]
mfc_dir = os.path.join(val, "redist", plat_dir, mfc_dir)
if os.path.isdir(mfc_dir):
# Ensuring absolute paths
mfc_contents = [os.path.join(mfc_dir, mfc_file) for mfc_file in mfc_files]
mfc_contents = [mfc_content for mfc_content in mfc_contents if os.path.exists(mfc_content)]
# Should have the same length - if not we lost a file!
if len(mfc_files) is not len(mfc_contents):
mfc_contents = []
return mfc_contents
def lookupMfcInWinSxS(self, mfc_version, mfc_libraries):
mfc_contents = []
windows_dir = os.getenv("windir", "C:\\Windows")
if os.path.isdir(windows_dir):
winsxs_path = os.path.join(windows_dir, "WinSxS")
if os.path.isdir(winsxs_path):
mfc_redist_path = None
winsxs_listdir = os.listdir(winsxs_path)
winsxs_listdir.sort()
for entry in winsxs_listdir:
if entry.startswith("{}_microsoft.{}.mfc_".format(platform.machine().lower(), mfc_version)) and os.path.isdir(os.path.join(winsxs_path, entry)):
for mfc_libary in mfc_libraries:
if not os.path.isfile(os.path.join(winsxs_path, entry, mfc_libary)):
continue
mfc_redist_path = entry
if mfc_redist_path:
mfc_contents = [os.path.join(winsxs_path, mfc_redist_path, mfc_libary) for mfc_libary in mfc_libraries]
mfc_manifest_file = os.path.join(winsxs_path, "Manifests", "{}.manifest".format(mfc_redist_path))
mfc_signature_file = os.path.join(winsxs_path, "Manifests", "{}.cat".format(mfc_redist_path))
if os.path.isfile(mfc_manifest_file): # Looking whether there is a manifest file
mfc_contents.append(mfc_manifest_file)
if os.path.isfile(mfc_signature_file): # If there is, also add the signaure file
mfc_contents.append(mfc_signature_file)
else:
print("Could not find any redist libraries in WinSxS!")
else:
print("Could not find WinSxS directory in %WINDIR%.")
else:
print("Windows directory not found!")
return mfc_contents
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()
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:
self.package = ext.get_pywin32_dir()
except AttributeError:
raise RuntimeError("Not a win32 package!")
self.build_exefile(ext)
# Not sure how to make this completely generic, and there is no
# need at this stage.
if sys.version_info > (2, 7) and sys.version_info < (3, 3):
# only stuff built with msvc9 needs this loader.
self._build_pycom_loader()
self._build_scintilla()
# Copy cpp lib files needed to create Python COM extensions
clib_files = (['win32', 'pywintypes%s.lib'],
['win32com', 'pythoncom%s.lib'],