Skip to content

Commit

Permalink
Merge pull request python#14 from paulmon/win-arm32-xplat
Browse files Browse the repository at this point in the history
Use cross-platform build environment variables
  • Loading branch information
paulmon authored Nov 3, 2018
2 parents 0a8bce0 + d8558a5 commit 25597a5
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 23 deletions.
21 changes: 16 additions & 5 deletions Lib/distutils/_msvccompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
CompileError, LibError, LinkError
from distutils.ccompiler import CCompiler, gen_lib_options
from distutils import log
from distutils.util import get_platform
from distutils.util import get_target_platform

from itertools import count

Expand Down Expand Up @@ -88,13 +88,23 @@ def _getall():
best_version = None
return best_version, best_dir

PLAT_SPEC_TO_RUNTIME = {
'x86' : 'x86',
'x86_amd64' : 'x64',
'x86_arm' : 'arm',
}

def _find_vcvarsall(plat_spec):
best_version, best_dir = _find_vc2017()
vcruntime = None
vcruntime_plat = 'x64' if 'amd64' in plat_spec else 'x86'
if plat_spec in PLAT_SPEC_TO_RUNTIME:
vcruntime_plat = PLAT_SPEC_TO_RUNTIME[plat_spec]
else:
vcruntime_plat = 'x64' if 'amd64' in plat_spec else 'x86'

if best_version:
vcredist = os.path.join(best_dir, "..", "..", "redist", "MSVC", "**",
"Microsoft.VC141.CRT", "vcruntime140.dll")
vcruntime_plat, "Microsoft.VC141.CRT", "vcruntime140.dll")
try:
import glob
vcruntime = glob.glob(vcredist, recursive=True)[-1]
Expand Down Expand Up @@ -171,7 +181,7 @@ def _find_exe(exe, paths=None):
return fn
return exe

# A map keyed by get_platform() return values to values accepted by
# A map keyed by get_target_platform() return values to values accepted by
# 'vcvarsall.bat'. Always cross-compile from x86 to work with the
# lighter-weight MSVC installs that do not include native 64-bit tools.
PLAT_TO_VCVARS = {
Expand Down Expand Up @@ -227,7 +237,8 @@ def initialize(self, plat_name=None):
# multi-init means we would need to check platform same each time...
assert not self.initialized, "don't init multiple times"
if plat_name is None:
plat_name = get_platform()
plat_name = get_target_platform()

# sanity check for platforms to prevent obscure errors later.
if plat_name not in PLAT_TO_VCVARS:
raise DistutilsPlatformError("--plat-name must be one of {}"
Expand Down
6 changes: 3 additions & 3 deletions Lib/distutils/command/bdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import os
from distutils.core import Command
from distutils.errors import *
from distutils.util import get_platform
from distutils.util import get_target_platform


def show_formats():
Expand All @@ -29,7 +29,7 @@ class bdist(Command):
"temporary directory for creating built distributions"),
('plat-name=', 'p',
"platform name to embed in generated filenames "
"(default: %s)" % get_platform()),
"(default: %s)" % get_target_platform()),
('formats=', None,
"formats for distribution (comma-separated list)"),
('dist-dir=', 'd',
Expand Down Expand Up @@ -91,7 +91,7 @@ def finalize_options(self):
# have to finalize 'plat_name' before 'bdist_base'
if self.plat_name is None:
if self.skip_build:
self.plat_name = get_platform()
self.plat_name = get_target_platform()
else:
self.plat_name = self.get_finalized_command('build').plat_name

Expand Down
4 changes: 2 additions & 2 deletions Lib/distutils/command/bdist_dumb.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import os
from distutils.core import Command
from distutils.util import get_platform
from distutils.util import get_target_platform
from distutils.dir_util import remove_tree, ensure_relative
from distutils.errors import *
from distutils.sysconfig import get_python_version
Expand All @@ -20,7 +20,7 @@ class bdist_dumb(Command):
"temporary directory for creating the distribution"),
('plat-name=', 'p',
"platform name to embed in generated filenames "
"(default: %s)" % get_platform()),
"(default: %s)" % get_target_platform()),
('format=', 'f',
"archive format to create (tar, gztar, bztar, xztar, "
"ztar, zip)"),
Expand Down
4 changes: 2 additions & 2 deletions Lib/distutils/command/bdist_msi.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from distutils.sysconfig import get_python_version
from distutils.version import StrictVersion
from distutils.errors import DistutilsOptionError
from distutils.util import get_platform
from distutils.util import get_target_platform
from distutils import log
import msilib
from msilib import schema, sequence, text
Expand Down Expand Up @@ -88,7 +88,7 @@ class bdist_msi(Command):
"temporary directory for creating the distribution"),
('plat-name=', 'p',
"platform name to embed in generated filenames "
"(default: %s)" % get_platform()),
"(default: %s)" % get_target_platform()),
('keep-temp', 'k',
"keep the pseudo-installation tree around after " +
"creating the distribution archive"),
Expand Down
4 changes: 2 additions & 2 deletions Lib/distutils/command/bdist_wininst.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import sys, os
from distutils.core import Command
from distutils.util import get_platform
from distutils.util import get_target_platform
from distutils.dir_util import create_tree, remove_tree
from distutils.errors import *
from distutils.sysconfig import get_python_version
Expand All @@ -19,7 +19,7 @@ class bdist_wininst(Command):
"temporary directory for creating the distribution"),
('plat-name=', 'p',
"platform name to embed in generated filenames "
"(default: %s)" % get_platform()),
"(default: %s)" % get_target_platform()),
('keep-temp', 'k',
"keep the pseudo-installation tree around after " +
"creating the distribution archive"),
Expand Down
6 changes: 3 additions & 3 deletions Lib/distutils/command/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import sys, os
from distutils.core import Command
from distutils.errors import DistutilsOptionError
from distutils.util import get_platform
from distutils.util import get_target_platform


def show_compilers():
Expand Down Expand Up @@ -33,7 +33,7 @@ class build(Command):
"temporary build directory"),
('plat-name=', 'p',
"platform name to build for, if supported "
"(default: %s)" % get_platform()),
"(default: %s)" % get_target_platform()),
('compiler=', 'c',
"specify the compiler type"),
('parallel=', 'j',
Expand Down Expand Up @@ -71,7 +71,7 @@ def initialize_options(self):

def finalize_options(self):
if self.plat_name is None:
self.plat_name = get_platform()
self.plat_name = get_target_platform()
else:
# plat-name only supported for windows (other platforms are
# supported via ./configure flags, if at all). Avoid misleading
Expand Down
5 changes: 3 additions & 2 deletions Lib/distutils/command/build_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from distutils.sysconfig import get_config_h_filename
from distutils.dep_util import newer_group
from distutils.extension import Extension
from distutils.util import get_platform
from distutils.util import get_platform, get_target_platform
from distutils import log

from site import USER_BASE
Expand Down Expand Up @@ -60,7 +60,7 @@ class build_ext(Command):
"directory for temporary files (build by-products)"),
('plat-name=', 'p',
"platform name to cross-compile for, if supported "
"(default: %s)" % get_platform()),
"(default: %s)" % get_target_platform()),
('inplace', 'i',
"ignore build-lib and put compiled extensions into the source " +
"directory alongside your pure Python modules"),
Expand Down Expand Up @@ -679,6 +679,7 @@ def get_ext_filename(self, ext_name):
from distutils.sysconfig import get_config_var
ext_path = ext_name.split('.')
ext_suffix = get_config_var('EXT_SUFFIX')

return os.path.join(*ext_path) + ext_suffix

def get_export_symbols(self, ext):
Expand Down
6 changes: 3 additions & 3 deletions Lib/distutils/msvc9compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from distutils.ccompiler import CCompiler, gen_preprocess_options, \
gen_lib_options
from distutils import log
from distutils.util import get_platform
from distutils.util import get_platform, get_target_platform

import winreg

Expand All @@ -49,7 +49,7 @@
WINSDK_BASE = r"Software\Microsoft\Microsoft SDKs\Windows"
NET_BASE = r"Software\Microsoft\.NETFramework"

# A map keyed by get_platform() return values to values accepted by
# A map keyed by get_target_platform() return values to values accepted by
# 'vcvarsall.bat'. Note a cross-compile may combine these (eg, 'x86_amd64' is
# the param to cross-compile on x86 targeting amd64.)
PLAT_TO_VCVARS = {
Expand Down Expand Up @@ -341,7 +341,7 @@ def initialize(self, plat_name=None):
# multi-init means we would need to check platform same each time...
assert not self.initialized, "don't init multiple times"
if plat_name is None:
plat_name = get_platform()
plat_name = get_target_platform()
# sanity check for platforms to prevent obscure errors later.
ok_plats = 'win32', 'win-amd64'
if plat_name not in ok_plats:
Expand Down
10 changes: 9 additions & 1 deletion Lib/distutils/sysconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import sys

from .errors import DistutilsPlatformError
from .util import get_platform, get_target_platform

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

g['EXT_SUFFIX'] = _imp.extension_suffixes()[0]
# if cross-compiling replace hardcoded platform-specific EXT_SUFFIX
# with an EXT_SUFFIX that matches the target platform
if get_platform() == get_target_platform():
g['EXT_SUFFIX'] = _imp.extension_suffixes()[0]
else:
plat_tag = get_target_platform().replace('-', '_')
g['EXT_SUFFIX'] = '.cp{0.major}{0.minor}-{1}.pyd'.format(sys.version_info, plat_tag)

g['EXE'] = ".exe"
g['VERSION'] = get_python_version().replace(".", "")
g['BINDIR'] = os.path.dirname(os.path.abspath(sys.executable))
Expand Down
17 changes: 17 additions & 0 deletions Lib/distutils/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,23 @@ def get_platform ():

# get_platform ()

def get_target_platform ():
TARGET_TO_PLAT = {
'x86' : 'win32',
'x64' : 'win-amd64',
'arm' : 'win-arm',
}

targetPlatformFromEnvironment = os.environ.get('VSCMD_ARG_TGT_ARCH')

if targetPlatformFromEnvironment != None and targetPlatformFromEnvironment in TARGET_TO_PLAT:
targetPlatform = TARGET_TO_PLAT[targetPlatformFromEnvironment]
else:
targetPlatform = get_platform()

return targetPlatform

# get_target_platform ()

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

0 comments on commit 25597a5

Please sign in to comment.