Skip to content

Commit

Permalink
Fix extension suffix for c-extensions on mingw
Browse files Browse the repository at this point in the history
Python is compiled with various compilers which previously
had same platform tags or extension suffix. This can be error
prone while loading c-extensions, so now each compiler or
runtime has a different extension suffix.

Also, changed all extension to end with .pyd rather than
.dll file.

Fixes msys2/MINGW-packages#8843

Signed-off-by: Naveen M K <naveen521kk@gmail.com>
  • Loading branch information
naveen521kk authored and lazka committed Jul 19, 2023
1 parent 778dd04 commit ac7de03
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 17 deletions.
6 changes: 3 additions & 3 deletions Makefile.pre.in
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ CONFINCLUDEPY= $(CONFINCLUDEDIR)/python$(LDVERSION)
# Symbols used for using shared libraries
SHLIB_SUFFIX= @SHLIB_SUFFIX@
EXT_SUFFIX= @EXT_SUFFIX@
PYD_PLATFORM_TAG = @PYD_PLATFORM_TAG@
LDSHARED= @LDSHARED@ $(PY_LDFLAGS)
BLDSHARED= @BLDSHARED@ $(PY_CORE_LDFLAGS)
LDCXXSHARED= @LDCXXSHARED@
Expand Down Expand Up @@ -1301,8 +1302,7 @@ Python/dynload_hpux.o: $(srcdir)/Python/dynload_hpux.c Makefile

Python/dynload_win.o: $(srcdir)/Python/dynload_win.c Makefile
$(CC) -c $(PY_CORE_CFLAGS) \
-DSHLIB_SUFFIX='"$(SHLIB_SUFFIX)"' \
-DEXT_SUFFIX='"$(EXT_SUFFIX)"' \
-DPYD_PLATFORM_TAG='"$(PYD_PLATFORM_TAG)"' \
-o $@ $(srcdir)/Python/dynload_win.c

Python/sysmodule.o: $(srcdir)/Python/sysmodule.c Makefile $(srcdir)/Include/pydtrace.h
Expand Down Expand Up @@ -2255,7 +2255,7 @@ libainstall: all python-config
@if test "$(STATIC_LIBPYTHON)" = 1; then \
if test -d $(LIBRARY); then :; else \
if test "$(PYTHONFRAMEWORKDIR)" = no-framework; then \
if test "$(SHLIB_SUFFIX)" = .dll; then \
if test "$(SHLIB_SUFFIX)" = .dll -o "$(SHLIB_SUFFIX)" = .pyd; then \
$(INSTALL_DATA) $(LDLIBRARY) $(DESTDIR)$(LIBPL) ; \
else \
$(INSTALL_DATA) $(LIBRARY) $(DESTDIR)$(LIBPL)/$(LIBRARY) ; \
Expand Down
6 changes: 0 additions & 6 deletions Python/dynload_win.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@
#define PYD_UNTAGGED_SUFFIX PYD_DEBUG_SUFFIX ".pyd"

const char *_PyImport_DynLoadFiletab[] = {
#ifdef EXT_SUFFIX
EXT_SUFFIX, /* include SOABI flags where is encoded debug */
#endif
#ifdef SHLIB_SUFFIX
"-abi" PYTHON_ABI_STRING SHLIB_SUFFIX,
#endif
PYD_TAGGED_SUFFIX,
PYD_UNTAGGED_SUFFIX,
NULL
Expand Down
77 changes: 69 additions & 8 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -3275,7 +3275,7 @@ if test -z "$SHLIB_SUFFIX"; then
*) SHLIB_SUFFIX=.so;;
esac
case $host_os in
mingw*) SHLIB_SUFFIX=.dll;;
mingw*) SHLIB_SUFFIX=.pyd;;
esac
fi
AC_MSG_RESULT($SHLIB_SUFFIX)
Expand Down Expand Up @@ -5934,6 +5934,68 @@ esac
# check for endianness
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

PYD_PLATFORM_TAG=""
case $host in
*-*-mingw*)
# check if we are linking to ucrt
AC_MSG_CHECKING(whether linking to ucrt)
AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
#include <stdio.h>
#ifndef _UCRT
#error no ucrt
#endif
int main(){ return 0; }
]])],[linking_to_ucrt=yes],[linking_to_ucrt=no])
AC_MSG_RESULT($linking_to_ucrt)
;;
esac
case $host_os in
mingw*)
AC_MSG_CHECKING(PYD_PLATFORM_TAG)
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
;;
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
;;
aarch64-*-mingw*)
PYD_PLATFORM_TAG+="mingw_aarch64"
;;
esac
AC_MSG_RESULT($PYD_PLATFORM_TAG)
esac

# ABI version string for Python extension modules. This appears between the
# periods in shared library file names, e.g. foo.<SOABI>.so. It is calculated
# from the following attributes which affect the ABI of this Python build (in
Expand Down Expand Up @@ -5966,7 +6028,12 @@ if test "$Py_DEBUG" = 'true' -a "$with_trace_refs" != "yes"; then
fi

AC_SUBST(EXT_SUFFIX)
EXT_SUFFIX=.${SOABI}${SHLIB_SUFFIX}
VERSION_NO_DOTS=$(echo $LDVERSION | tr -d .)
if test -n "${PYD_PLATFORM_TAG}"; then
EXT_SUFFIX=".cp${VERSION_NO_DOTS}-${PYD_PLATFORM_TAG}${SHLIB_SUFFIX}"
else
EXT_SUFFIX=.${SOABI}${SHLIB_SUFFIX}
fi

AC_MSG_CHECKING(LDVERSION)
LDVERSION='$(VERSION)$(ABIFLAGS)'
Expand Down Expand Up @@ -6592,12 +6659,6 @@ case "$ac_cv_computed_gotos" in yes*)
AC_DEFINE(HAVE_COMPUTED_GOTOS, 1,
[Define if the C compiler supports computed gotos.])
esac
case $host_os in
mingw*)
dnl Synchronized with _PyImport_DynLoadFiletab (dynload_win.c)
dnl Do not use more then one dot on this platform !
EXT_SUFFIX=-$SOABI$SHLIB_SUFFIX;;
esac

case $ac_sys_system in
AIX*)
Expand Down

0 comments on commit ac7de03

Please sign in to comment.