From 21b1cfd6eb95779dd52fc4ddfe9f3de4ee8e5c0c Mon Sep 17 00:00:00 2001 From: Marcelo Duarte Date: Sat, 30 May 2020 17:53:36 -0300 Subject: [PATCH] Ensure the copy of default python libraries - windows (#640) * Ensure the copy of default python libraries - windows * don't break mingw * refactor --- cx_Freeze/freezer.py | 53 +++++++++++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 16 deletions(-) diff --git a/cx_Freeze/freezer.py b/cx_Freeze/freezer.py index 9d4c9f198..0e3eddd0b 100644 --- a/cx_Freeze/freezer.py +++ b/cx_Freeze/freezer.py @@ -14,6 +14,7 @@ import stat import struct import sys +import sysconfig import time import zipfile @@ -177,10 +178,10 @@ def _FreezeExecutable(self, exe): ".py") finder.IncludeFile(startupModule) - # Always copy the python dynamic libraries into lib folder + # Copy the python dynamic libraries + copyDependentFiles = True if sys.platform == "linux": - self._CopyFile(exe.base, exe.targetName, - copyDependentFiles=False, includeMode=True) + # Always copy the python dynamic libraries into lib folder targetDir = os.path.join(os.path.dirname(exe.targetName), 'lib') dependentFiles = self._GetDependentFiles(exe.base) or \ self._GetDependentFiles(sys.executable) @@ -188,9 +189,25 @@ def _FreezeExecutable(self, exe): target = os.path.join(targetDir, os.path.basename(source)) self._CopyFile(source, target, copyDependentFiles=True, includeMode=True) - else: - self._CopyFile(exe.base, exe.targetName, - copyDependentFiles=True, includeMode=True) + copyDependentFiles = False + elif sys.platform == "win32": + # Copy the python dynamic libraries into build folder + targetDir = os.path.dirname(exe.targetName) + dependentFiles = self._GetDependentFiles(exe.base) or \ + self._GetDependentFiles(sys.executable) + # Ensure the copy of default python libraries + sourceDir = os.path.dirname(dependentFiles[0]) + for name in self._GetDefaultBinIncludes(): + source = os.path.join(sourceDir, os.path.normcase(name)) + if source not in dependentFiles: + dependentFiles.append(source) + for source in dependentFiles: + target = os.path.join(targetDir, os.path.basename(source)) + self._CopyFile(source, target, + copyDependentFiles=True, includeMode=True) + copyDependentFiles = False + self._CopyFile(exe.base, exe.targetName, + copyDependentFiles=copyDependentFiles, includeMode=True) if not os.access(exe.targetName, os.W_OK): mode = os.stat(exe.targetName).st_mode os.chmod(exe.targetName, mode | stat.S_IWUSR) @@ -225,17 +242,21 @@ def _GetDefaultBinExcludes(self): def _GetDefaultBinIncludes(self): """Return the file names of libraries which must be included for the frozen executable to work.""" + python_shared_libs = [] if sys.platform == "win32": - pythonDlls = ["python%s.dll" % sys.version_info[0], - "python%s%s.dll" % sys.version_info[:2], - "vcruntime140.dll"] - return pythonDlls + if sysconfig.get_platform() == "mingw": + name = distutils.sysconfig.get_config_var("INSTSONAME") + if name: + python_shared_libs.append(name.replace(".dll.a", ".dll")) + else: + python_shared_libs += ["python%s.dll" % sys.version_info[0], + "python%s%s.dll" % sys.version_info[:2], + "vcruntime140.dll"] else: - soName = distutils.sysconfig.get_config_var("INSTSONAME") - if soName is None: - return [] - pythonSharedLib = self._RemoveVersionNumbers(soName) - return [pythonSharedLib] + name = distutils.sysconfig.get_config_var("INSTSONAME") + if name: + python_shared_libs.append(self._RemoveVersionNumbers(name)) + return python_shared_libs def _GetDefaultBinPathExcludes(self): """Return the paths of directories which contain files that should not @@ -323,7 +344,7 @@ def _GetDependentFiles(self, path): dependentFiles = [self._CheckDependentFile(f, dirname) for f in dependentFiles if self._ShouldCopyFile(f)] else: - dependentFiles = [f + dependentFiles = [os.path.normcase(f) for f in dependentFiles if self._ShouldCopyFile(f)] self.dependentFiles[path] = dependentFiles return dependentFiles