Skip to content

Commit 25597a5

Browse files
authored
Merge pull request python#14 from paulmon/win-arm32-xplat
Use cross-platform build environment variables
2 parents 0a8bce0 + d8558a5 commit 25597a5

File tree

10 files changed

+60
-23
lines changed

10 files changed

+60
-23
lines changed

Lib/distutils/_msvccompiler.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
CompileError, LibError, LinkError
2424
from distutils.ccompiler import CCompiler, gen_lib_options
2525
from distutils import log
26-
from distutils.util import get_platform
26+
from distutils.util import get_target_platform
2727

2828
from itertools import count
2929

@@ -88,13 +88,23 @@ def _getall():
8888
best_version = None
8989
return best_version, best_dir
9090

91+
PLAT_SPEC_TO_RUNTIME = {
92+
'x86' : 'x86',
93+
'x86_amd64' : 'x64',
94+
'x86_arm' : 'arm',
95+
}
96+
9197
def _find_vcvarsall(plat_spec):
9298
best_version, best_dir = _find_vc2017()
9399
vcruntime = None
94-
vcruntime_plat = 'x64' if 'amd64' in plat_spec else 'x86'
100+
if plat_spec in PLAT_SPEC_TO_RUNTIME:
101+
vcruntime_plat = PLAT_SPEC_TO_RUNTIME[plat_spec]
102+
else:
103+
vcruntime_plat = 'x64' if 'amd64' in plat_spec else 'x86'
104+
95105
if best_version:
96106
vcredist = os.path.join(best_dir, "..", "..", "redist", "MSVC", "**",
97-
"Microsoft.VC141.CRT", "vcruntime140.dll")
107+
vcruntime_plat, "Microsoft.VC141.CRT", "vcruntime140.dll")
98108
try:
99109
import glob
100110
vcruntime = glob.glob(vcredist, recursive=True)[-1]
@@ -171,7 +181,7 @@ def _find_exe(exe, paths=None):
171181
return fn
172182
return exe
173183

174-
# A map keyed by get_platform() return values to values accepted by
184+
# A map keyed by get_target_platform() return values to values accepted by
175185
# 'vcvarsall.bat'. Always cross-compile from x86 to work with the
176186
# lighter-weight MSVC installs that do not include native 64-bit tools.
177187
PLAT_TO_VCVARS = {
@@ -227,7 +237,8 @@ def initialize(self, plat_name=None):
227237
# multi-init means we would need to check platform same each time...
228238
assert not self.initialized, "don't init multiple times"
229239
if plat_name is None:
230-
plat_name = get_platform()
240+
plat_name = get_target_platform()
241+
231242
# sanity check for platforms to prevent obscure errors later.
232243
if plat_name not in PLAT_TO_VCVARS:
233244
raise DistutilsPlatformError("--plat-name must be one of {}"

Lib/distutils/command/bdist.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import os
77
from distutils.core import Command
88
from distutils.errors import *
9-
from distutils.util import get_platform
9+
from distutils.util import get_target_platform
1010

1111

1212
def show_formats():
@@ -29,7 +29,7 @@ class bdist(Command):
2929
"temporary directory for creating built distributions"),
3030
('plat-name=', 'p',
3131
"platform name to embed in generated filenames "
32-
"(default: %s)" % get_platform()),
32+
"(default: %s)" % get_target_platform()),
3333
('formats=', None,
3434
"formats for distribution (comma-separated list)"),
3535
('dist-dir=', 'd',
@@ -91,7 +91,7 @@ def finalize_options(self):
9191
# have to finalize 'plat_name' before 'bdist_base'
9292
if self.plat_name is None:
9393
if self.skip_build:
94-
self.plat_name = get_platform()
94+
self.plat_name = get_target_platform()
9595
else:
9696
self.plat_name = self.get_finalized_command('build').plat_name
9797

Lib/distutils/command/bdist_dumb.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import os
88
from distutils.core import Command
9-
from distutils.util import get_platform
9+
from distutils.util import get_target_platform
1010
from distutils.dir_util import remove_tree, ensure_relative
1111
from distutils.errors import *
1212
from distutils.sysconfig import get_python_version
@@ -20,7 +20,7 @@ class bdist_dumb(Command):
2020
"temporary directory for creating the distribution"),
2121
('plat-name=', 'p',
2222
"platform name to embed in generated filenames "
23-
"(default: %s)" % get_platform()),
23+
"(default: %s)" % get_target_platform()),
2424
('format=', 'f',
2525
"archive format to create (tar, gztar, bztar, xztar, "
2626
"ztar, zip)"),

Lib/distutils/command/bdist_msi.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from distutils.sysconfig import get_python_version
1313
from distutils.version import StrictVersion
1414
from distutils.errors import DistutilsOptionError
15-
from distutils.util import get_platform
15+
from distutils.util import get_target_platform
1616
from distutils import log
1717
import msilib
1818
from msilib import schema, sequence, text
@@ -88,7 +88,7 @@ class bdist_msi(Command):
8888
"temporary directory for creating the distribution"),
8989
('plat-name=', 'p',
9090
"platform name to embed in generated filenames "
91-
"(default: %s)" % get_platform()),
91+
"(default: %s)" % get_target_platform()),
9292
('keep-temp', 'k',
9393
"keep the pseudo-installation tree around after " +
9494
"creating the distribution archive"),

Lib/distutils/command/bdist_wininst.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import sys, os
77
from distutils.core import Command
8-
from distutils.util import get_platform
8+
from distutils.util import get_target_platform
99
from distutils.dir_util import create_tree, remove_tree
1010
from distutils.errors import *
1111
from distutils.sysconfig import get_python_version
@@ -19,7 +19,7 @@ class bdist_wininst(Command):
1919
"temporary directory for creating the distribution"),
2020
('plat-name=', 'p',
2121
"platform name to embed in generated filenames "
22-
"(default: %s)" % get_platform()),
22+
"(default: %s)" % get_target_platform()),
2323
('keep-temp', 'k',
2424
"keep the pseudo-installation tree around after " +
2525
"creating the distribution archive"),

Lib/distutils/command/build.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import sys, os
66
from distutils.core import Command
77
from distutils.errors import DistutilsOptionError
8-
from distutils.util import get_platform
8+
from distutils.util import get_target_platform
99

1010

1111
def show_compilers():
@@ -33,7 +33,7 @@ class build(Command):
3333
"temporary build directory"),
3434
('plat-name=', 'p',
3535
"platform name to build for, if supported "
36-
"(default: %s)" % get_platform()),
36+
"(default: %s)" % get_target_platform()),
3737
('compiler=', 'c',
3838
"specify the compiler type"),
3939
('parallel=', 'j',
@@ -71,7 +71,7 @@ def initialize_options(self):
7171

7272
def finalize_options(self):
7373
if self.plat_name is None:
74-
self.plat_name = get_platform()
74+
self.plat_name = get_target_platform()
7575
else:
7676
# plat-name only supported for windows (other platforms are
7777
# supported via ./configure flags, if at all). Avoid misleading

Lib/distutils/command/build_ext.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from distutils.sysconfig import get_config_h_filename
1515
from distutils.dep_util import newer_group
1616
from distutils.extension import Extension
17-
from distutils.util import get_platform
17+
from distutils.util import get_platform, get_target_platform
1818
from distutils import log
1919

2020
from site import USER_BASE
@@ -60,7 +60,7 @@ class build_ext(Command):
6060
"directory for temporary files (build by-products)"),
6161
('plat-name=', 'p',
6262
"platform name to cross-compile for, if supported "
63-
"(default: %s)" % get_platform()),
63+
"(default: %s)" % get_target_platform()),
6464
('inplace', 'i',
6565
"ignore build-lib and put compiled extensions into the source " +
6666
"directory alongside your pure Python modules"),
@@ -679,6 +679,7 @@ def get_ext_filename(self, ext_name):
679679
from distutils.sysconfig import get_config_var
680680
ext_path = ext_name.split('.')
681681
ext_suffix = get_config_var('EXT_SUFFIX')
682+
682683
return os.path.join(*ext_path) + ext_suffix
683684

684685
def get_export_symbols(self, ext):

Lib/distutils/msvc9compiler.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from distutils.ccompiler import CCompiler, gen_preprocess_options, \
2323
gen_lib_options
2424
from distutils import log
25-
from distutils.util import get_platform
25+
from distutils.util import get_platform, get_target_platform
2626

2727
import winreg
2828

@@ -49,7 +49,7 @@
4949
WINSDK_BASE = r"Software\Microsoft\Microsoft SDKs\Windows"
5050
NET_BASE = r"Software\Microsoft\.NETFramework"
5151

52-
# A map keyed by get_platform() return values to values accepted by
52+
# A map keyed by get_target_platform() return values to values accepted by
5353
# 'vcvarsall.bat'. Note a cross-compile may combine these (eg, 'x86_amd64' is
5454
# the param to cross-compile on x86 targeting amd64.)
5555
PLAT_TO_VCVARS = {
@@ -341,7 +341,7 @@ def initialize(self, plat_name=None):
341341
# multi-init means we would need to check platform same each time...
342342
assert not self.initialized, "don't init multiple times"
343343
if plat_name is None:
344-
plat_name = get_platform()
344+
plat_name = get_target_platform()
345345
# sanity check for platforms to prevent obscure errors later.
346346
ok_plats = 'win32', 'win-amd64'
347347
if plat_name not in ok_plats:

Lib/distutils/sysconfig.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import sys
1616

1717
from .errors import DistutilsPlatformError
18+
from .util import get_platform, get_target_platform
1819

1920
# These are needed in a couple of spots, so just compute them once.
2021
PREFIX = os.path.normpath(sys.prefix)
@@ -438,7 +439,14 @@ def _init_nt():
438439
# XXX hmmm.. a normal install puts include files here
439440
g['INCLUDEPY'] = get_python_inc(plat_specific=0)
440441

441-
g['EXT_SUFFIX'] = _imp.extension_suffixes()[0]
442+
# if cross-compiling replace hardcoded platform-specific EXT_SUFFIX
443+
# with an EXT_SUFFIX that matches the target platform
444+
if get_platform() == get_target_platform():
445+
g['EXT_SUFFIX'] = _imp.extension_suffixes()[0]
446+
else:
447+
plat_tag = get_target_platform().replace('-', '_')
448+
g['EXT_SUFFIX'] = '.cp{0.major}{0.minor}-{1}.pyd'.format(sys.version_info, plat_tag)
449+
442450
g['EXE'] = ".exe"
443451
g['VERSION'] = get_python_version().replace(".", "")
444452
g['BINDIR'] = os.path.dirname(os.path.abspath(sys.executable))

Lib/distutils/util.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,23 @@ def get_platform ():
9494

9595
# get_platform ()
9696

97+
def get_target_platform ():
98+
TARGET_TO_PLAT = {
99+
'x86' : 'win32',
100+
'x64' : 'win-amd64',
101+
'arm' : 'win-arm',
102+
}
103+
104+
targetPlatformFromEnvironment = os.environ.get('VSCMD_ARG_TGT_ARCH')
105+
106+
if targetPlatformFromEnvironment != None and targetPlatformFromEnvironment in TARGET_TO_PLAT:
107+
targetPlatform = TARGET_TO_PLAT[targetPlatformFromEnvironment]
108+
else:
109+
targetPlatform = get_platform()
110+
111+
return targetPlatform
112+
113+
# get_target_platform ()
97114

98115
def convert_path (pathname):
99116
"""Return 'pathname' as a name that will work on the native filesystem,

0 commit comments

Comments
 (0)