Skip to content

Commit

Permalink
sysconfig.get_platform(): use consistent naming
Browse files Browse the repository at this point in the history
Currently, the platform names are hardcoded in many places,
and are not named consistently. This commit fixes it by
standardizing the names to be returned by `sysconfig.get_platform`.
The naming is based on #167 (comment)

Similarly, `EXT_SUFFIX` is also standardized to be consistent with the platform names.

Signed-off-by: Naveen M K <[email protected]>
  • Loading branch information
naveen521kk committed Nov 2, 2024
1 parent ce773f3 commit d6355bc
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 75 deletions.
32 changes: 19 additions & 13 deletions Lib/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,21 +314,27 @@ def joinuser(*args):
def _get_platform():
if os.name == 'nt':
if 'gcc' in sys.version.lower():
platform = 'mingw'
if 'amd64' in sys.version.lower():
platform += '_x86_64'
elif 'arm64' in sys.version.lower():
platform += '_aarch64'
elif 'arm' in sys.version.lower():
platform += '_armv7'
else:
platform += '_i686'

if 'ucrt' in sys.version.lower():
if 'amd64' in sys.version.lower():
return 'mingw_x86_64_ucrt'
return 'mingw_i686_ucrt'
platform += '_ucrt'
else:
platform += "_msvcrt"

if 'clang' in sys.version.lower():
if 'amd64' in sys.version.lower():
return 'mingw_x86_64_clang'
if 'arm64' in sys.version.lower():
return 'mingw_aarch64'
if 'arm' in sys.version.lower():
return 'mingw_armv7'
return 'mingw_i686_clang'
if 'amd64' in sys.version.lower():
return 'mingw_x86_64'
return 'mingw_i686'
platform += "_llvm"
else:
platform += "_gnu"

return platform
return sys.platform

# Same to sysconfig.get_path('purelib', os.name+'_user')
Expand Down
32 changes: 19 additions & 13 deletions Lib/sysconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -800,21 +800,27 @@ def get_platform():
"""
if os.name == 'nt':
if 'gcc' in sys.version.lower():
platform = 'mingw'
if 'amd64' in sys.version.lower():
platform += '_x86_64'
elif 'arm64' in sys.version.lower():
platform += '_aarch64'
elif 'arm' in sys.version.lower():
platform += '_armv7'
else:
platform += '_i686'

if 'ucrt' in sys.version.lower():
if 'amd64' in sys.version.lower():
return 'mingw_x86_64_ucrt'
return 'mingw_i686_ucrt'
platform += '_ucrt'
else:
platform += "_msvcrt"

if 'clang' in sys.version.lower():
if 'amd64' in sys.version.lower():
return 'mingw_x86_64_clang'
if 'arm64' in sys.version.lower():
return 'mingw_aarch64'
if 'arm' in sys.version.lower():
return 'mingw_armv7'
return 'mingw_i686_clang'
if 'amd64' in sys.version.lower():
return 'mingw_x86_64'
return 'mingw_i686'
platform += "_llvm"
else:
platform += "_gnu"

return platform
if 'amd64' in sys.version.lower():
return 'win-amd64'
if '(arm)' in sys.version.lower():
Expand Down
38 changes: 22 additions & 16 deletions Lib/test/test_importlib/test_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,28 @@ def get_platform():
'arm' : 'win-arm32',
}
if os.name == 'nt':
if 'gcc' in sys.version.lower():
if 'ucrt' in sys.version.lower():
if 'amd64' in sys.version.lower():
return 'mingw_x86_64_ucrt'
return 'mingw_i686_ucrt'
if 'clang' in sys.version.lower():
if 'amd64' in sys.version.lower():
return 'mingw_x86_64_clang'
if 'arm64' in sys.version.lower():
return 'mingw_aarch64'
if 'arm' in sys.version.lower():
return 'mingw_armv7'
return 'mingw_i686_clang'
if 'amd64' in sys.version.lower():
return 'mingw_x86_64'
return 'mingw_i686'
if "gcc" in sys.version.lower():
platform = "mingw"
if "amd64" in sys.version.lower():
platform += "_x86_64"
elif "arm64" in sys.version.lower():
platform += "_aarch64"
elif "arm" in sys.version.lower():
platform += "_armv7"
else:
platform += "_i686"

if "ucrt" in sys.version.lower():
platform += "_ucrt"
else:
platform += "_msvcrt"

if "clang" in sys.version.lower():
platform += "_llvm"
else:
platform += "_gnu"

return platform
if ('VSCMD_ARG_TGT_ARCH' in os.environ and
os.environ['VSCMD_ARG_TGT_ARCH'] in TARGET_TO_PLAT):
return TARGET_TO_PLAT[os.environ['VSCMD_ARG_TGT_ARCH']]
Expand Down
54 changes: 22 additions & 32 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -6311,16 +6311,12 @@ AC_C_BIGENDIAN

AC_SUBST(PYD_PLATFORM_TAG)
# Special case of PYD_PLATFORM_TAG with python build with mingw.
# Python can with compiled with clang or gcc and linked
# to msvcrt or ucrt. To avoid conflicts between them
# we are selecting the extension as based on the compiler
# and the runtime they link to
# gcc + x86_64 + msvcrt = cp{version number}-x86_64
# gcc + i686 + msvcrt = cp{version number}-i686
# gcc + x86_64 + ucrt = cp{version number}-x86_64-ucrt
# clang + x86_64 + ucrt = cp{version number}-x86_64-clang
# clang + i686 + ucrt = cp{version number}-i686-clang

# Python can with different cpu arch and c runtime as well as different
# toolchain. We follow this`mingw_<cpu_arch>_<c_runtime>_<toolchain>`
# convention for PYD_PLATFORM_TAG. Where:
# `cpu_arch` = `x86_64`, `aarch64` or `i686`
# `c_runtime` = `msvcrt` or `ucrt`
# `toolchain` = `gnu` or `llvm`
PYD_PLATFORM_TAG=""
case $host in
*-*-mingw*)
Expand All @@ -6339,38 +6335,32 @@ esac
case $host_os in
mingw*)
AC_MSG_CHECKING(PYD_PLATFORM_TAG)
PYD_PLATFORM_TAG="mingw"
case $host in
i686-*-mingw*)
if test -n "${cc_is_clang}"; then
# it is CLANG32
PYD_PLATFORM_TAG="mingw_i686_clang"
else
if test $linking_to_ucrt = no; then
PYD_PLATFORM_TAG="mingw_i686"
else
PYD_PLATFORM_TAG="mingw_i686_ucrt"
fi
fi
PYD_PLATFORM_TAG+="_i686"
;;
x86_64-*-mingw*)
if test -n "${cc_is_clang}"; then
# it is CLANG64
PYD_PLATFORM_TAG="mingw_x86_64_clang"
else
if test $linking_to_ucrt = no; then
PYD_PLATFORM_TAG="mingw_x86_64"
else
PYD_PLATFORM_TAG="mingw_x86_64_ucrt"
fi
fi
PYD_PLATFORM_TAG+="_x86_64"
;;
aarch64-*-mingw*)
PYD_PLATFORM_TAG+="mingw_aarch64"
PYD_PLATFORM_TAG+="_aarch64"
;;
armv7-*-mingw*)
PYD_PLATFORM_TAG+="mingw_armv7"
PYD_PLATFORM_TAG+="_armv7"
;;
esac
if test $linking_to_ucrt = no; then
PYD_PLATFORM_TAG+="_msvcrt"
else
PYD_PLATFORM_TAG += "_ucrt"
fi
if test -n "${cc_is_clang}"; then
# it is CLANG32
PYD_PLATFORM_TAG+="_llvm"
else
PYD_PLATFORM_TAG+="_gnu"
fi
AC_MSG_RESULT($PYD_PLATFORM_TAG)
esac

Expand Down
2 changes: 1 addition & 1 deletion mingw_smoketests.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
if sysconfig.is_python_build():
os.environ["PYTHONLEGACYWINDOWSDLLLOADING"] = "1"

_UCRT = sysconfig.get_platform() not in ('mingw_x86_64', 'mingw_i686')
_UCRT = 'ucrt' in sysconfig.get_platform()


class Tests(unittest.TestCase):
Expand Down

0 comments on commit d6355bc

Please sign in to comment.