bindepend.py 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  1. #-----------------------------------------------------------------------------
  2. # Copyright (c) 2013-2021, PyInstaller Development Team.
  3. #
  4. # Distributed under the terms of the GNU General Public License (version 2
  5. # or later) with exception for distributing the bootloader.
  6. #
  7. # The full license is in the file COPYING.txt, distributed with this software.
  8. #
  9. # SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
  10. #-----------------------------------------------------------------------------
  11. """
  12. Find external dependencies of binary libraries.
  13. """
  14. import collections
  15. import ctypes.util
  16. import os
  17. import re
  18. import sys
  19. # Required for extracting eggs.
  20. import zipfile
  21. import subprocess
  22. from PyInstaller import compat
  23. from PyInstaller import log as logging
  24. from PyInstaller.depend import dylib, utils
  25. from PyInstaller.utils.win32 import winutils
  26. logger = logging.getLogger(__name__)
  27. seen = set()
  28. # Import windows specific stuff.
  29. if compat.is_win:
  30. from distutils.sysconfig import get_python_lib
  31. import pefile
  32. from PyInstaller.utils.win32 import winmanifest, winresource
  33. def getfullnameof(mod, xtrapath=None):
  34. """
  35. Return the full path name of MOD.
  36. * MOD is the basename of a dll or pyd.
  37. * XTRAPATH is a path or list of paths to search first.
  38. Return the full path name of MOD. Will search the full Windows search path, as well as sys.path
  39. """
  40. pywin32_paths = []
  41. if compat.is_win:
  42. pywin32_paths = [os.path.join(get_python_lib(), 'pywin32_system32')]
  43. if compat.is_venv:
  44. pywin32_paths.append(os.path.join(compat.base_prefix, 'Lib', 'site-packages', 'pywin32_system32'))
  45. epath = (
  46. sys.path + # Search sys.path first!
  47. pywin32_paths + winutils.get_system_path() + compat.getenv('PATH', '').split(os.pathsep)
  48. )
  49. if xtrapath is not None:
  50. if isinstance(xtrapath, str):
  51. epath.insert(0, xtrapath)
  52. else:
  53. epath = xtrapath + epath
  54. for p in epath:
  55. npth = os.path.join(p, mod)
  56. if os.path.exists(npth) and matchDLLArch(npth):
  57. return npth
  58. return ''
  59. def _getImports_pe(pth):
  60. """
  61. Find the binary dependencies of PTH.
  62. This implementation walks through the PE header and uses library pefile for that and supports 32/64bit Windows
  63. """
  64. dlls = set()
  65. # By default, pefile library parses all PE information. We are only interested in the list of dependent dlls.
  66. # Performance is improved by reading only needed information. https://code.google.com/p/pefile/wiki/UsageExamples
  67. pe = pefile.PE(pth, fast_load=True)
  68. pe.parse_data_directories(
  69. directories=[
  70. pefile.DIRECTORY_ENTRY['IMAGE_DIRECTORY_ENTRY_IMPORT'],
  71. pefile.DIRECTORY_ENTRY['IMAGE_DIRECTORY_ENTRY_EXPORT'],
  72. ],
  73. forwarded_exports_only=True,
  74. import_dllnames_only=True,
  75. )
  76. # Some libraries have no other binary dependencies. Use empty list in that case. Otherwise pefile would return None.
  77. # e.g., C:\windows\system32\kernel32.dll on Wine
  78. for entry in getattr(pe, 'DIRECTORY_ENTRY_IMPORT', []):
  79. dll_str = winutils.convert_dll_name_to_str(entry.dll)
  80. dlls.add(dll_str)
  81. # We must also read the exports table to find forwarded symbols:
  82. # http://blogs.msdn.com/b/oldnewthing/archive/2006/07/19/671238.aspx
  83. exportSymbols = getattr(pe, 'DIRECTORY_ENTRY_EXPORT', None)
  84. if exportSymbols:
  85. for sym in exportSymbols.symbols:
  86. if sym.forwarder is not None:
  87. # sym.forwarder is a bytes object. Convert it to a string.
  88. forwarder = winutils.convert_dll_name_to_str(sym.forwarder)
  89. # sym.forwarder is for example 'KERNEL32.EnterCriticalSection'
  90. dll = forwarder.split('.')[0]
  91. dlls.add(dll + ".dll")
  92. pe.close()
  93. return dlls
  94. def _extract_from_egg(toc):
  95. """
  96. Ensure all binary modules in zipped eggs get extracted and included with the frozen executable.
  97. return modified table of content
  98. """
  99. new_toc = []
  100. for item in toc:
  101. # Item is a tupple
  102. # (mod_name, path, type)
  103. modname, pth, typ = item
  104. if not os.path.isfile(pth):
  105. pth = check_extract_from_egg(pth)[0][0]
  106. # Add value to new data structure.
  107. new_toc.append((modname, pth, typ))
  108. return new_toc
  109. BindingRedirect = collections.namedtuple('BindingRedirect', 'name language arch oldVersion newVersion publicKeyToken')
  110. def match_binding_redirect(manifest, redirect):
  111. return all([
  112. manifest.name == redirect.name,
  113. manifest.version == redirect.oldVersion,
  114. manifest.language == redirect.language,
  115. manifest.processorArchitecture == redirect.arch,
  116. manifest.publicKeyToken == redirect.publicKeyToken,
  117. ])
  118. _exe_machine_type = None
  119. def matchDLLArch(filename):
  120. """
  121. Return True if the DLL given by filename matches the CPU type/architecture of the Python process running
  122. PyInstaller.
  123. Always returns True on non-Windows platforms.
  124. :param filename:
  125. :type filename:
  126. :return:
  127. :rtype:
  128. """
  129. # TODO: check machine type on other platforms?
  130. if not compat.is_win:
  131. return True
  132. global _exe_machine_type
  133. try:
  134. if _exe_machine_type is None:
  135. pefilename = compat.python_executable # for exception handling
  136. exe_pe = pefile.PE(pefilename, fast_load=True)
  137. _exe_machine_type = exe_pe.FILE_HEADER.Machine
  138. exe_pe.close()
  139. pefilename = filename # for exception handling
  140. pe = pefile.PE(filename, fast_load=True)
  141. match_arch = pe.FILE_HEADER.Machine == _exe_machine_type
  142. pe.close()
  143. except pefile.PEFormatError as exc:
  144. raise SystemExit('Cannot get architecture from file: %s\n Reason: %s' % (pefilename, exc))
  145. return match_arch
  146. def Dependencies(lTOC, xtrapath=None, manifest=None, redirects=None):
  147. """
  148. Expand LTOC to include all the closure of binary dependencies.
  149. `LTOC` is a logical table of contents, ie, a seq of tuples (name, path). Return LTOC expanded by all the binary
  150. dependencies of the entries in LTOC, except those listed in the module global EXCLUDES
  151. `manifest` may be a winmanifest.Manifest instance for a program manifest, so that all dependent assemblies of
  152. python.exe can be added to the built exe.
  153. `redirects` may be a list. Any assembly redirects found via policy files will be added to the list as
  154. BindingRedirect objects so they can later be used to modify any manifests that reference the redirected assembly.
  155. """
  156. # Extract all necessary binary modules from Python eggs to be included directly with PyInstaller.
  157. lTOC = _extract_from_egg(lTOC)
  158. for nm, pth, typ in lTOC:
  159. if nm.upper() in seen:
  160. continue
  161. logger.debug("Analyzing %s", pth)
  162. seen.add(nm.upper())
  163. if compat.is_win:
  164. for ftocnm, fn in getAssemblyFiles(pth, manifest, redirects):
  165. lTOC.append((ftocnm, fn, 'BINARY'))
  166. for lib, npth in selectImports(pth, xtrapath):
  167. if lib.upper() in seen or npth.upper() in seen:
  168. continue
  169. seen.add(npth.upper())
  170. lTOC.append((lib, npth, 'BINARY'))
  171. return lTOC
  172. def pkg_resources_get_default_cache():
  173. """
  174. Determine the default cache location
  175. This returns the ``PYTHON_EGG_CACHE`` environment variable, if set. Otherwise, on Windows, it returns a
  176. 'Python-Eggs' subdirectory of the 'Application Data' directory. On all other systems, it's '~/.python-eggs'.
  177. """
  178. # This function borrowed from setuptools/pkg_resources
  179. egg_cache = compat.getenv('PYTHON_EGG_CACHE')
  180. if egg_cache is not None:
  181. return egg_cache
  182. if os.name != 'nt':
  183. return os.path.expanduser('~/.python-eggs')
  184. app_data = 'Application Data' # XXX this may be locale-specific!
  185. app_homes = [
  186. (('APPDATA',), None), # best option, should be locale-safe
  187. (('USERPROFILE',), app_data),
  188. (('HOMEDRIVE', 'HOMEPATH'), app_data),
  189. (('HOMEPATH',), app_data),
  190. (('HOME',), None),
  191. (('WINDIR',), app_data), # 95/98/ME
  192. ]
  193. for keys, subdir in app_homes:
  194. dirname = ''
  195. for key in keys:
  196. if key in os.environ:
  197. dirname = os.path.join(dirname, compat.getenv(key))
  198. else:
  199. break
  200. else:
  201. if subdir:
  202. dirname = os.path.join(dirname, subdir)
  203. return os.path.join(dirname, 'Python-Eggs')
  204. else:
  205. raise RuntimeError("Please set the PYTHON_EGG_CACHE environment variable")
  206. def check_extract_from_egg(pth, todir=None):
  207. r"""
  208. Check if path points to a file inside a python egg file, extract the file from the egg to a cache directory (
  209. following pkg_resources convention) and return [(extracted path, egg file path, relative path inside egg file)].
  210. Otherwise, just return [(original path, None, None)]. If path points to an egg file directly, return a list with
  211. all files from the egg formatted like above.
  212. Example:
  213. >>> check_extract_from_egg(r'C:\Python26\Lib\site-packages\my.egg\mymodule\my.pyd')
  214. [(r'C:\Users\UserName\AppData\Roaming\Python-Eggs\my.egg-tmp\mymodule\my.pyd',
  215. r'C:\Python26\Lib\site-packages\my.egg', r'mymodule/my.pyd')]
  216. """
  217. rv = []
  218. if os.path.altsep:
  219. pth = pth.replace(os.path.altsep, os.path.sep)
  220. components = pth.split(os.path.sep)
  221. for i, name in enumerate(components):
  222. if name.lower().endswith(".egg"):
  223. eggpth = os.path.sep.join(components[:i + 1])
  224. if os.path.isfile(eggpth):
  225. # eggs can also be directories!
  226. try:
  227. egg = zipfile.ZipFile(eggpth)
  228. except zipfile.BadZipfile as e:
  229. raise SystemExit("Error: %s %s" % (eggpth, e))
  230. if todir is None:
  231. # Use the same directory as setuptools/pkg_resources. So, if the specific egg was accessed before
  232. # (not necessarily by pyinstaller), the extracted contents already exist (pkg_resources puts them
  233. # there) and can be used.
  234. todir = os.path.join(pkg_resources_get_default_cache(), name + "-tmp")
  235. if components[i + 1:]:
  236. members = ["/".join(components[i + 1:])]
  237. else:
  238. members = egg.namelist()
  239. for member in members:
  240. pth = os.path.join(todir, member)
  241. if not os.path.isfile(pth):
  242. dirname = os.path.dirname(pth)
  243. if not os.path.isdir(dirname):
  244. os.makedirs(dirname)
  245. with open(pth, "wb") as f:
  246. f.write(egg.read(member))
  247. rv.append((pth, eggpth, member))
  248. return rv
  249. return [(pth, None, None)]
  250. def getAssemblies(pth):
  251. """
  252. On Windows return the dependent Side-by-Side (SxS) assemblies of a binary as a list of Manifest objects.
  253. Dependent assemblies are required only by binaries compiled with MSVC 9.0. Python 2.7 and 3.2 are compiled with
  254. MSVC 9.0 and thus depend on Microsoft Redistributable runtime libraries 9.0.
  255. Python 3.3+ is compiled with version 10.0 and does not use SxS assemblies.
  256. FIXME: Can this be removed since we now only support Python 3.5+?
  257. FIXME: IS there some test-case covering this?
  258. """
  259. if pth.lower().endswith(".manifest"):
  260. return []
  261. # check for manifest file
  262. manifestnm = pth + ".manifest"
  263. if os.path.isfile(manifestnm):
  264. with open(manifestnm, "rb") as fd:
  265. res = {winmanifest.RT_MANIFEST: {1: {0: fd.read()}}}
  266. else:
  267. # check the binary for embedded manifest
  268. try:
  269. res = winmanifest.GetManifestResources(pth)
  270. except winresource.pywintypes.error as exc:
  271. if exc.args[0] == winresource.ERROR_BAD_EXE_FORMAT:
  272. logger.info('Cannot get manifest resource from non-PE file %s', pth)
  273. return []
  274. raise
  275. rv = []
  276. if winmanifest.RT_MANIFEST in res and len(res[winmanifest.RT_MANIFEST]):
  277. for name in res[winmanifest.RT_MANIFEST]:
  278. for language in res[winmanifest.RT_MANIFEST][name]:
  279. # check the manifest for dependent assemblies
  280. try:
  281. manifest = winmanifest.Manifest()
  282. manifest.filename = ":".join([
  283. pth,
  284. str(winmanifest.RT_MANIFEST),
  285. str(name),
  286. str(language),
  287. ])
  288. manifest.parse_string(res[winmanifest.RT_MANIFEST][name][language], False)
  289. except Exception:
  290. logger.error("Cannot parse manifest resource %s, %s from %s", name, language, pth, exc_info=1)
  291. else:
  292. if manifest.dependentAssemblies:
  293. logger.debug("Dependent assemblies of %s:", pth)
  294. logger.debug(", ".join([assembly.getid() for assembly in manifest.dependentAssemblies]))
  295. rv.extend(manifest.dependentAssemblies)
  296. return rv
  297. def getAssemblyFiles(pth, manifest=None, redirects=None):
  298. """
  299. Find all assemblies that are dependencies of the given binary and return the files that make up the assemblies as
  300. (name, fullpath) tuples.
  301. If a WinManifest object is passed as `manifest`, also updates that manifest to reference the returned assemblies.
  302. This is done only to update the built app's .exe with the dependencies of python.exe
  303. If a list is passed as `redirects`, and binding redirects in policy files are applied when searching for
  304. assemblies, BindingRedirect objects are appended to this list.
  305. Return a list of pairs (name, fullpath)
  306. """
  307. rv = []
  308. if manifest:
  309. _depNames = set(dep.name for dep in manifest.dependentAssemblies)
  310. for assembly in getAssemblies(pth):
  311. if assembly.getid().upper() in seen:
  312. continue
  313. if manifest and assembly.name not in _depNames:
  314. # Add assembly as dependency to our final output exe's manifest
  315. logger.info("Adding %s to dependent assemblies of final executable\n required by %s", assembly.name, pth)
  316. manifest.dependentAssemblies.append(assembly)
  317. _depNames.add(assembly.name)
  318. if not dylib.include_library(assembly.name):
  319. logger.debug("Skipping assembly %s", assembly.getid())
  320. continue
  321. if assembly.optional:
  322. logger.debug("Skipping optional assembly %s", assembly.getid())
  323. continue
  324. from PyInstaller.config import CONF
  325. if CONF.get("win_no_prefer_redirects"):
  326. files = assembly.find_files()
  327. else:
  328. files = []
  329. if not len(files):
  330. # If no files were found, it may be the case that the required version of the assembly is not installed, and
  331. # the policy file is redirecting it to a newer version. So, we collect the newer version instead.
  332. files = assembly.find_files(ignore_policies=False)
  333. if len(files) and redirects is not None:
  334. # New version was found, old version was not. Add a redirect in the app configuration.
  335. old_version = assembly.version
  336. new_version = assembly.get_policy_redirect()
  337. logger.info("Adding redirect %s version %s -> %s", assembly.name, old_version, new_version)
  338. redirects.append(
  339. BindingRedirect(
  340. name=assembly.name,
  341. language=assembly.language,
  342. arch=assembly.processorArchitecture,
  343. publicKeyToken=assembly.publicKeyToken,
  344. oldVersion=old_version,
  345. newVersion=new_version,
  346. )
  347. )
  348. if files:
  349. seen.add(assembly.getid().upper())
  350. for fn in files:
  351. fname, fext = os.path.splitext(fn)
  352. if fext.lower() == ".manifest":
  353. nm = assembly.name + fext
  354. else:
  355. nm = os.path.basename(fn)
  356. ftocnm = nm
  357. if assembly.language not in (None, "", "*", "neutral"):
  358. ftocnm = os.path.join(assembly.getlanguage(), ftocnm)
  359. nm, ftocnm, fn = [item.encode(sys.getfilesystemencoding()) for item in (nm, ftocnm, fn)]
  360. if fn.upper() not in seen:
  361. logger.debug("Adding %s", ftocnm)
  362. seen.add(nm.upper())
  363. seen.add(fn.upper())
  364. rv.append((ftocnm, fn))
  365. else:
  366. #logger.info("skipping %s part of assembly %s dependency of %s", ftocnm, assembly.name, pth)
  367. pass
  368. else:
  369. logger.error("Assembly %s not found", assembly.getid())
  370. # Convert items in list from 'bytes' type to 'str' type.
  371. # NOTE: with Python 3 we somehow get type 'bytes' and it then causes other issues and failures with PyInstaller.
  372. new_rv = []
  373. for item in rv:
  374. a = item[0].decode('ascii')
  375. b = item[1].decode('ascii')
  376. new_rv.append((a, b))
  377. rv = new_rv
  378. return rv
  379. def selectImports(pth, xtrapath=None):
  380. """
  381. Return the dependencies of a binary that should be included.
  382. Return a list of pairs (name, fullpath)
  383. """
  384. rv = []
  385. if xtrapath is None:
  386. xtrapath = [os.path.dirname(pth)]
  387. else:
  388. assert isinstance(xtrapath, list)
  389. xtrapath = [os.path.dirname(pth)] + xtrapath # make a copy
  390. dlls = getImports(pth)
  391. for lib in dlls:
  392. if lib.upper() in seen:
  393. continue
  394. if not compat.is_win:
  395. # all other platforms
  396. npth = lib
  397. lib = os.path.basename(lib)
  398. else:
  399. # plain win case
  400. npth = getfullnameof(lib, xtrapath)
  401. # Now npth is a candidate lib if found. Check again for excludes, but with regex. FIXME: split the list.
  402. if npth:
  403. candidatelib = npth
  404. else:
  405. candidatelib = lib
  406. if not dylib.include_library(candidatelib):
  407. if candidatelib.find('libpython') < 0 and candidatelib.find('Python.framework') < 0:
  408. # skip libs not containing (libpython or Python.framework)
  409. if npth.upper() not in seen:
  410. logger.debug("Skipping %s dependency of %s", lib, os.path.basename(pth))
  411. continue
  412. else:
  413. pass
  414. if npth:
  415. if npth.upper() not in seen:
  416. logger.debug("Adding %s dependency of %s from %s", lib, os.path.basename(pth), npth)
  417. rv.append((lib, npth))
  418. elif dylib.warn_missing_lib(lib):
  419. logger.warning("lib not found: %s dependency of %s", lib, pth)
  420. return rv
  421. def _getImports_ldd(pth):
  422. """
  423. Find the binary dependencies of PTH.
  424. This implementation is for ldd platforms (mostly unix).
  425. """
  426. rslt = set()
  427. if compat.is_aix:
  428. # Match libs of the form
  429. # 'archivelib.a(objectmember.so/.o)'
  430. # or
  431. # 'sharedlib.so'
  432. # Will not match the fake lib '/unix'
  433. lddPattern = re.compile(r"^\s*(((?P<libarchive>(.*\.a))(?P<objectmember>\(.*\)))|((?P<libshared>(.*\.so))))$")
  434. elif compat.is_hpux:
  435. # Match libs of the form
  436. # 'sharedlib.so => full-path-to-lib
  437. # e.g.
  438. # 'libpython2.7.so => /usr/local/lib/hpux32/libpython2.7.so'
  439. lddPattern = re.compile(r"^\s+(.*)\s+=>\s+(.*)$")
  440. elif compat.is_solar:
  441. # Match libs of the form
  442. # 'sharedlib.so => full-path-to-lib
  443. # e.g.
  444. # 'libpython2.7.so.1.0 => /usr/local/lib/libpython2.7.so.1.0'
  445. # Will not match the platform specific libs starting with '/platform'
  446. lddPattern = re.compile(r"^\s+(.*)\s+=>\s+(.*)$")
  447. else:
  448. lddPattern = re.compile(r"\s*(.*?)\s+=>\s+(.*?)\s+\(.*\)")
  449. p = subprocess.run(['ldd', pth], stderr=subprocess.PIPE, stdout=subprocess.PIPE, universal_newlines=True)
  450. for line in p.stderr.splitlines():
  451. if not line:
  452. continue
  453. # Python extensions (including stdlib ones) are not linked against python.so but rely on Python's symbols having
  454. # already been loaded into symbol space at runtime. musl's ldd issues a series of harmless warnings to stderr
  455. # telling us that those symbols are unfindable. These should be suppressed.
  456. elif line.startswith("Error relocating ") and line.endswith(" symbol not found"):
  457. continue
  458. # Propagate any other warnings it might have.
  459. print(line, file=sys.stderr)
  460. for line in p.stdout.splitlines():
  461. m = lddPattern.search(line)
  462. if m:
  463. if compat.is_aix:
  464. libarchive = m.group('libarchive')
  465. if libarchive:
  466. # We matched an archive lib with a request for a particular embedded shared object.
  467. # 'archivelib.a(objectmember.so/.o)'
  468. lib = libarchive
  469. name = os.path.basename(lib) + m.group('objectmember')
  470. else:
  471. # We matched a stand-alone shared library.
  472. # 'sharedlib.so'
  473. lib = m.group('libshared')
  474. name = os.path.basename(lib)
  475. elif compat.is_hpux:
  476. name, lib = m.group(1), m.group(2)
  477. else:
  478. name, lib = m.group(1), m.group(2)
  479. if name[:10] in ('linux-gate', 'linux-vdso'):
  480. # linux-gate is a fake library which does not exist and should be ignored. See also:
  481. # http://www.trilithium.com/johan/2005/08/linux-gate/
  482. continue
  483. if compat.is_cygwin:
  484. # exclude Windows system library
  485. if lib.lower().startswith('/cygdrive/c/windows/system'):
  486. continue
  487. if os.path.exists(lib):
  488. # Add lib if it is not already found.
  489. if lib not in rslt:
  490. rslt.add(lib)
  491. elif dylib.warn_missing_lib(name):
  492. logger.warning('Cannot find %s in path %s (needed by %s)', name, lib, pth)
  493. elif line.endswith("not found"):
  494. # On glibc-based linux distributions, missing libraries are marked with name.so => not found
  495. tokens = line.split('=>')
  496. if len(tokens) != 2:
  497. continue
  498. name = tokens[0].strip()
  499. if dylib.warn_missing_lib(name):
  500. logger.warning('Cannot find %s (needed by %s)', name, pth)
  501. return rslt
  502. def _getImports_macholib(pth):
  503. """
  504. Find the binary dependencies of PTH.
  505. This implementation is for Mac OS X and uses library macholib.
  506. """
  507. from macholib.dyld import dyld_find
  508. from macholib.mach_o import LC_RPATH
  509. from macholib.MachO import MachO
  510. from macholib.util import in_system_path
  511. rslt = set()
  512. seen = set() # Libraries read from binary headers.
  513. #- Walk through mach binary headers.
  514. m = MachO(pth)
  515. for header in m.headers:
  516. for idx, name, lib in header.walkRelocatables():
  517. # Sometimes libraries are present multiple times.
  518. if lib not in seen:
  519. seen.add(lib)
  520. # Walk through mach binary headers and look for LC_RPATH. macholib can't handle @rpath. LC_RPATH has to be read from
  521. # the MachO header.
  522. # TODO Do we need to remove LC_RPATH from MachO load commands? Will it cause any harm to leave them untouched?
  523. # Removing LC_RPATH should be implemented when getting files from the bincache if it is necessary.
  524. run_paths = set()
  525. for header in m.headers:
  526. for command in header.commands:
  527. # A command is a tupple like:
  528. # (<macholib.mach_o.load_command object at 0x>,
  529. # <macholib.mach_o.rpath_command object at 0x>,
  530. # '../lib\x00\x00')
  531. cmd_type = command[0].cmd
  532. if cmd_type == LC_RPATH:
  533. rpath = command[2].decode('utf-8')
  534. # Remove trailing '\x00' characters. E.g., '../lib\x00\x00'
  535. rpath = rpath.rstrip('\x00')
  536. # Replace the @executable_path and @loader_path keywords with the actual path to the binary.
  537. executable_path = os.path.dirname(pth)
  538. rpath = re.sub('^@(executable_path|loader_path|rpath)(/|$)', executable_path + r'\2', rpath)
  539. # Make rpath absolute. According to Apple doc LC_RPATH is always relative to the binary location.
  540. rpath = os.path.normpath(os.path.join(executable_path, rpath))
  541. run_paths.update([rpath])
  542. else:
  543. # Frameworks that have this structure Name.framework/Versions/N/Name need to search at the same level
  544. # as the framework dir. This is specifically needed so that the QtWebEngine dependencies can be found.
  545. if '.framework' in pth:
  546. run_paths.update(['../../../'])
  547. # For distributions like Anaconda, all of the dylibs are stored in the lib directory of the Python distribution, not
  548. # alongside of the .so's in each module's subdirectory.
  549. run_paths.add(os.path.join(compat.base_prefix, 'lib'))
  550. #- Try to find files in file system.
  551. # In cases with @loader_path or @executable_path try to look in the same directory as the checked binary is. This
  552. # seems to work in most cases.
  553. exec_path = os.path.abspath(os.path.dirname(pth))
  554. for lib in seen:
  555. # Suppose that @rpath is not used for system libraries and using macholib can be avoided. macholib cannot handle
  556. # @rpath.
  557. if lib.startswith('@rpath'):
  558. lib = lib.replace('@rpath', '.') # Make path relative.
  559. final_lib = None # Absolute path to existing lib on disk.
  560. # Try multiple locations.
  561. for run_path in run_paths:
  562. # @rpath may contain relative value. Use exec_path as base path.
  563. if not os.path.isabs(run_path):
  564. run_path = os.path.join(exec_path, run_path)
  565. # Stop looking for lib when found in first location.
  566. if os.path.exists(os.path.join(run_path, lib)):
  567. final_lib = os.path.abspath(os.path.join(run_path, lib))
  568. rslt.add(final_lib)
  569. break
  570. # Log warning if no existing file found.
  571. if not final_lib and dylib.warn_missing_lib(lib):
  572. logger.warning('Cannot find path %s (needed by %s)', lib, pth)
  573. # Macholib has to be used to get absolute path to libraries.
  574. else:
  575. # macholib cannot handle @loader_path. It has to be handled the same way as @executable_path. It is also
  576. # replaced by 'exec_path'.
  577. if lib.startswith('@loader_path'):
  578. lib = lib.replace('@loader_path', '@executable_path')
  579. try:
  580. lib = dyld_find(lib, executable_path=exec_path)
  581. rslt.add(lib)
  582. except ValueError:
  583. # Starting with Big Sur, system libraries are hidden. And we do not collect system libraries on any
  584. # macOS version anyway, so suppress the corresponding error messages.
  585. if not in_system_path(lib) and dylib.warn_missing_lib(lib):
  586. logger.warning('Cannot find path %s (needed by %s)', lib, pth)
  587. return rslt
  588. def getImports(pth):
  589. """
  590. Forwards to the correct getImports implementation for the platform.
  591. """
  592. if compat.is_win:
  593. if pth.lower().endswith(".manifest"):
  594. return []
  595. try:
  596. return _getImports_pe(pth)
  597. except Exception as exception:
  598. # Assemblies can pull in files which aren't necessarily PE, but are still needed by the assembly. Any
  599. # additional binary dependencies should already have been handled by selectAssemblies in that case, so just
  600. # warn, return an empty list and continue. For less specific errors also log the traceback.
  601. logger.warning('Cannot get binary dependencies for file: %s', pth)
  602. logger.warning(' Reason: %s', exception, exc_info=not isinstance(exception, pefile.PEFormatError))
  603. return []
  604. elif compat.is_darwin:
  605. return _getImports_macholib(pth)
  606. else:
  607. return _getImports_ldd(pth)
  608. def findLibrary(name):
  609. """
  610. Look for a library in the system.
  611. Emulate the algorithm used by dlopen. `name` must include the prefix, e.g., ``libpython2.4.so``.
  612. """
  613. assert compat.is_unix, "Current implementation for Unix only (Linux, Solaris, AIX, FreeBSD)"
  614. # Look in the LD_LIBRARY_PATH according to platform.
  615. if compat.is_aix:
  616. lp = compat.getenv('LIBPATH', '')
  617. elif compat.is_darwin:
  618. lp = compat.getenv('DYLD_LIBRARY_PATH', '')
  619. else:
  620. lp = compat.getenv('LD_LIBRARY_PATH', '')
  621. lib = _which_library(name, filter(None, lp.split(os.pathsep)))
  622. # Look in /etc/ld.so.cache
  623. # Solaris does not have /sbin/ldconfig. Just check if this file exists.
  624. if lib is None:
  625. utils.load_ldconfig_cache()
  626. lib = utils.LDCONFIG_CACHE.get(name)
  627. if lib:
  628. assert os.path.isfile(lib)
  629. # Look in the known safe paths.
  630. if lib is None:
  631. # Architecture independent locations.
  632. paths = ['/lib', '/usr/lib']
  633. # Architecture dependent locations.
  634. if compat.architecture == '32bit':
  635. paths.extend(['/lib32', '/usr/lib32'])
  636. else:
  637. paths.extend(['/lib64', '/usr/lib64'])
  638. # Machine dependent locations.
  639. if compat.machine == 'intel':
  640. if compat.architecture == '32bit':
  641. paths.extend(['/usr/lib/i386-linux-gnu'])
  642. else:
  643. paths.extend(['/usr/lib/x86_64-linux-gnu'])
  644. # On Debian/Ubuntu /usr/bin/python is linked statically with libpython. Newer Debian/Ubuntu with multiarch
  645. # support puts the libpythonX.Y.so in paths like /usr/lib/i386-linux-gnu/.
  646. try:
  647. # Module available only in Python 2.7+
  648. import sysconfig
  649. # 'multiarchsubdir' works on Debian/Ubuntu only in Python 2.7 and 3.3+.
  650. arch_subdir = sysconfig.get_config_var('multiarchsubdir')
  651. # Ignore if None is returned.
  652. if arch_subdir:
  653. arch_subdir = os.path.basename(arch_subdir)
  654. paths.append(os.path.join('/usr/lib', arch_subdir))
  655. else:
  656. logger.debug('Multiarch directory not detected.')
  657. except ImportError:
  658. logger.debug('Multiarch directory not detected.')
  659. # Termux (a Ubuntu like subsystem for Android) has an additional libraries directory.
  660. if os.path.isdir('/data/data/com.termux/files/usr/lib'):
  661. paths.append('/data/data/com.termux/files/usr/lib')
  662. if compat.is_aix:
  663. paths.append('/opt/freeware/lib')
  664. elif compat.is_hpux:
  665. if compat.architecture == '32bit':
  666. paths.append('/usr/local/lib/hpux32')
  667. else:
  668. paths.append('/usr/local/lib/hpux64')
  669. elif compat.is_freebsd or compat.is_openbsd:
  670. paths.append('/usr/local/lib')
  671. lib = _which_library(name, paths)
  672. # Give up :(
  673. if lib is None:
  674. return None
  675. # Resolve the file name into the soname
  676. if compat.is_freebsd or compat.is_aix or compat.is_openbsd:
  677. # On FreeBSD objdump does not show SONAME, and on AIX objdump does not exist, so we just return the lib we
  678. # have found.
  679. return lib
  680. else:
  681. dir = os.path.dirname(lib)
  682. return os.path.join(dir, _get_so_name(lib))
  683. def _which_library(name, dirs):
  684. """
  685. Search for a shared library in a list of directories.
  686. Args:
  687. name:
  688. The library name including the `lib` prefix but excluding any `.so` suffix.
  689. dirs:
  690. An iterable of folders to search in.
  691. Returns:
  692. The path to the library if found or None otherwise.
  693. """
  694. matcher = _library_matcher(name)
  695. for path in filter(os.path.exists, dirs):
  696. for _path in os.listdir(path):
  697. if matcher(_path):
  698. return os.path.join(path, _path)
  699. def _library_matcher(name):
  700. """
  701. Create a callable that matches libraries if **name** is a valid library prefix for input library full names.
  702. """
  703. return re.compile(name + r"[0-9]*\.").match
  704. def _get_so_name(filename):
  705. """
  706. Return the soname of a library.
  707. Soname is useful when there are multiple symplinks to one library.
  708. """
  709. # TODO verify that objdump works on other unixes and not Linux only.
  710. cmd = ["objdump", "-p", filename]
  711. pattern = r'\s+SONAME\s+([^\s]+)'
  712. if compat.is_solar:
  713. cmd = ["elfdump", "-d", filename]
  714. pattern = r'\s+SONAME\s+[^\s]+\s+([^\s]+)'
  715. m = re.search(pattern, compat.exec_command(*cmd))
  716. return m.group(1)
  717. def get_python_library_path():
  718. """
  719. Find dynamic Python library that will be bundled with frozen executable.
  720. NOTE: This is a fallback option when the Python executable is likely statically linked with the Python library and
  721. we need to search more for it. For example, this is the case on Debian/Ubuntu.
  722. Return full path to Python dynamic library or None when not found.
  723. We need to know name of the Python dynamic library for the bootloader. Bootloader has to know what library to
  724. load and not try to guess.
  725. Some linux distributions (e.g. debian-based) statically link the Python executable to the libpython,
  726. so bindepend does not include it in its output. In this situation let's try to find it.
  727. Custom Mac OS builds could possibly also have non-framework style libraries, so this method also checks for that
  728. variant as well.
  729. """
  730. def _find_lib_in_libdirs(*libdirs):
  731. for libdir in libdirs:
  732. for name in compat.PYDYLIB_NAMES:
  733. full_path = os.path.join(libdir, name)
  734. if os.path.exists(full_path):
  735. return full_path
  736. return None
  737. # If this is Microsoft App Store Python, check the compat.base_path first. While compat.python_executable resolves
  738. # to actual python.exe file, the latter contains relative library reference that does not get properly resolved by
  739. # getfullnameof().
  740. if compat.is_ms_app_store:
  741. python_libname = _find_lib_in_libdirs(compat.base_prefix)
  742. if python_libname:
  743. return python_libname
  744. # Try to get Python library name from the Python executable. It assumes that Python library is not statically
  745. # linked.
  746. dlls = getImports(compat.python_executable)
  747. for filename in dlls:
  748. for name in compat.PYDYLIB_NAMES:
  749. if os.path.basename(filename) == name:
  750. # On Windows filename is just like 'python27.dll'. Convert it to absolute path.
  751. if compat.is_win and not os.path.isabs(filename):
  752. filename = getfullnameof(filename)
  753. # Python library found. Return absolute path to it.
  754. return filename
  755. # Python library NOT found. Resume searching using alternative methods.
  756. # Work around for python venv having VERSION.dll rather than pythonXY.dll
  757. if compat.is_win and 'VERSION.dll' in dlls:
  758. pydll = 'python%d%d.dll' % sys.version_info[:2]
  759. return getfullnameof(pydll)
  760. # Applies only to non Windows platforms and conda.
  761. if compat.is_conda:
  762. # Conda needs to be the first here since it overrules the operating system specific paths.
  763. python_libname = _find_lib_in_libdirs(os.path.join(compat.base_prefix, 'lib'))
  764. if python_libname:
  765. return python_libname
  766. elif compat.is_unix:
  767. for name in compat.PYDYLIB_NAMES:
  768. python_libname = findLibrary(name)
  769. if python_libname:
  770. return python_libname
  771. if compat.is_darwin or compat.is_linux:
  772. # On MacPython, Analysis.assemble is able to find the libpython with no additional help, asking for
  773. # sys.executable dependencies. However, this fails on system python, because the shared library is not listed as
  774. # a dependency of the binary (most probably it is opened at runtime using some dlopen trickery). This happens on
  775. # Mac OS when Python is compiled as Framework.
  776. # Linux using pyenv is similarly linked so that sys.executable dependencies does not yield libpython.so.
  777. # Python compiled as Framework contains same values in sys.prefix and exec_prefix. That is why we can use just
  778. # sys.prefix. In virtualenv, PyInstaller is not able to find Python library. We need special care for this case.
  779. python_libname = _find_lib_in_libdirs(
  780. compat.base_prefix,
  781. os.path.join(compat.base_prefix, 'lib'),
  782. )
  783. if python_libname:
  784. return python_libname
  785. # Python library NOT found. Provide helpful feedback.
  786. msg = """Python library not found: %s
  787. This means your Python installation does not come with proper shared library files.
  788. This usually happens due to missing development package, or unsuitable build parameters of the Python installation.
  789. * On Debian/Ubuntu, you need to install Python development packages:
  790. * apt-get install python3-dev
  791. * apt-get install python-dev
  792. * If you are building Python by yourself, rebuild with `--enable-shared` (or, `--enable-framework` on macOS).
  793. """ % (", ".join(compat.PYDYLIB_NAMES),)
  794. raise IOError(msg)
  795. def findSystemLibrary(name):
  796. """
  797. Given a library name, try to resolve the path to that library.
  798. If the path is already an absolute path, return it without searching.
  799. """
  800. if os.path.isabs(name):
  801. return name
  802. if compat.is_unix:
  803. return findLibrary(name)
  804. elif compat.is_win:
  805. return getfullnameof(name)
  806. else:
  807. # This seems to work, and is similar to what we have above..
  808. return ctypes.util.find_library(name)