Skip to content

Commit

Permalink
Copy libs/pythonXY.lib to support building of C extensions without di…
Browse files Browse the repository at this point in the history
…stutils

This issue was discovered while using virtualenv to create an
isolated environment and test that a source distribution would compile
without using the knowledge hardcoded in distutils.sysconfig and
virtualenv/virtualenv_embedded/distutils-init.py

In other word, without relying on the fact virtualenv monkey patch distutils
to ensure the value associated with LIBDIR variable is correct.

This is relevant because the library location is implicitly specified
in PC/pyconfig.h

8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---
[...]

/* For an MSVC DLL, we can nominate the .lib files used by extensions */
#ifdef MS_COREDLL
#       ifndef Py_BUILD_CORE /* not building the core - must be an ext */
#               if defined(_MSC_VER)
                        /* So MSVC users need not specify the .lib file in
                        their Makefile (other compilers are generally
                        taken care of by distutils.) */
#                       if defined(_DEBUG)
#                               pragma comment(lib,"python35_d.lib")
#                       elif defined(Py_LIMITED_API)
#                               pragma comment(lib,"python3.lib")
#                       else
#                               pragma comment(lib,"python35.lib")
#                       endif /* _DEBUG */
#               endif /* _MSC_VER */
#       endif /* Py_BUILD_CORE */
#endif /* MS_COREDLL */

[...]
8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---

References:
 * https://docs.python.org/2/extending/windows.html#using-dlls-in-practice
 * https://docs.python.org/3.6/extending/windows.html#using-dlls-in-practice

Tested-by: Samuele Maci <macisamuele@gmail.com>
  • Loading branch information
jcfr committed Jan 14, 2019
1 parent 50b6092 commit 121cc1e
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions virtualenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -1395,6 +1395,20 @@ def install_python(home_dir, lib_dir, inc_dir, bin_dir, site_packages, clear, sy
elif os.path.exists(python_dll_d_dest):
logger.info("Removed %s as the source does not exist", python_dll_d_dest)
os.unlink(python_dll_d_dest)

if not IS_PYPY:
# considering that on windows, python import libraries are located in
# the "<root>/libs" directory, the following code will look for and
# copy "pythonXY.lib".
pythonlib_name = "python%s%s.lib" % (sys.version_info[0], sys.version_info[1])
pythonlib = os.path.join(os.path.dirname(sys.executable), "libs", pythonlib_name)
pythonlib_dest_dir = os.path.join(lib_dir, "..", "libs")
pythonlib_dest = os.path.join(pythonlib_dest_dir, pythonlib_name)
if os.path.exists(pythonlib):
logger.info("Also created %s" % pythonlib_name)
mkdir(pythonlib_dest_dir)
shutil.copyfile(pythonlib, pythonlib_dest)

if IS_PYPY:
# make a symlink python --> pypy-c
python_executable = os.path.join(os.path.dirname(py_executable), "python")
Expand Down

0 comments on commit 121cc1e

Please sign in to comment.