|
12 | 12 | import os
|
13 | 13 | import platform as plat
|
14 | 14 | import re
|
| 15 | +import string |
15 | 16 | import struct
|
16 | 17 | import subprocess
|
17 | 18 | import sys
|
@@ -74,6 +75,27 @@ def _dbg(s, tp=None):
|
74 | 75 | print(s)
|
75 | 76 |
|
76 | 77 |
|
| 78 | +def _find_strings_in_binary(filename, min=5): |
| 79 | + # based on https://stackoverflow.com/a/17197027/199332 |
| 80 | + if sys.version_info[0] == 3: |
| 81 | + fp = open(filename, errors="ignore") |
| 82 | + else: |
| 83 | + fp = open(filename, "rb") |
| 84 | + try: |
| 85 | + result = "" |
| 86 | + for c in fp.read(): |
| 87 | + if c in string.printable: |
| 88 | + result += c |
| 89 | + continue |
| 90 | + if len(result) >= min: |
| 91 | + yield result |
| 92 | + result = "" |
| 93 | + if len(result) >= min: # catch result at EOF |
| 94 | + yield result |
| 95 | + finally: |
| 96 | + fp.close() |
| 97 | + |
| 98 | + |
77 | 99 | def _add_directory(path, subdir, where=None):
|
78 | 100 | if subdir is None:
|
79 | 101 | return
|
@@ -334,60 +356,20 @@ def build_extensions(self):
|
334 | 356 | _add_directory(include_dirs, "/usr/X11/include")
|
335 | 357 |
|
336 | 358 | elif sys.platform.startswith("linux"):
|
337 |
| - arch_tp = (plat.processor(), plat.architecture()[0]) |
338 |
| - # This should be correct on debian derivatives. |
339 |
| - if os.path.exists('/etc/debian_version'): |
340 |
| - # If this doesn't work, don't just silently patch |
341 |
| - # downstream because it's going to break when people |
342 |
| - # try to build pillow from source instead of |
343 |
| - # installing from the system packages. |
344 |
| - self.add_multiarch_paths() |
345 |
| - |
346 |
| - elif arch_tp == ("x86_64", "32bit"): |
347 |
| - # Special Case: 32-bit build on 64-bit machine. |
348 |
| - _add_directory(library_dirs, "/usr/lib/i386-linux-gnu") |
349 |
| - else: |
350 |
| - libdirs = { |
351 |
| - 'x86_64': ["/lib64", "/usr/lib64", |
352 |
| - "/usr/lib/x86_64-linux-gnu"], |
353 |
| - '64bit': ["/lib64", "/usr/lib64", |
354 |
| - "/usr/lib/x86_64-linux-gnu"], |
355 |
| - 'i386': ["/usr/lib/i386-linux-gnu"], |
356 |
| - 'i686': ["/usr/lib/i386-linux-gnu"], |
357 |
| - '32bit': ["/usr/lib/i386-linux-gnu"], |
358 |
| - 'aarch64': ["/usr/lib64", "/usr/lib/aarch64-linux-gnu"], |
359 |
| - 'arm': ["/usr/lib/arm-linux-gnueabi"], |
360 |
| - 'armv71': ["/usr/lib/arm-linux-gnueabi"], |
361 |
| - 'armv7l': ["/usr/lib"], |
362 |
| - 'ppc64': ["/usr/lib64", "/usr/lib/ppc64-linux-gnu", |
363 |
| - "/usr/lib/powerpc64-linux-gnu"], |
364 |
| - 'ppc64le': ["/usr/lib64"], |
365 |
| - 'ppc': ["/usr/lib/ppc-linux-gnu", |
366 |
| - "/usr/lib/powerpc-linux-gnu"], |
367 |
| - 's390x': ["/usr/lib64", "/usr/lib/s390x-linux-gnu"], |
368 |
| - 's390': ["/usr/lib/s390-linux-gnu"], |
369 |
| - } |
370 |
| - |
371 |
| - for platform_ in arch_tp: |
372 |
| - dirs = libdirs.get(platform_, None) |
373 |
| - if not dirs: |
374 |
| - continue |
375 |
| - for path in dirs: |
376 |
| - _add_directory(library_dirs, path) |
377 |
| - break |
378 |
| - |
379 |
| - else: |
380 |
| - raise ValueError( |
381 |
| - "Unable to identify Linux platform: `%s`" % platform_) |
382 |
| - |
383 |
| - # termux support for android. |
384 |
| - # system libraries (zlib) are installed in /system/lib |
385 |
| - # headers are at $PREFIX/include |
386 |
| - # user libs are at $PREFIX/lib |
387 |
| - if os.environ.get('ANDROID_ROOT', None): |
388 |
| - _add_directory(library_dirs, |
389 |
| - os.path.join(os.environ['ANDROID_ROOT'], |
390 |
| - 'lib')) |
| 359 | + ld_so_cache = '/etc/ld.so.cache' |
| 360 | + if os.path.isfile(ld_so_cache): |
| 361 | + for candidate in _find_strings_in_binary(ld_so_cache): |
| 362 | + if candidate[0] == '/': |
| 363 | + dirname = os.path.dirname(candidate) |
| 364 | + _add_directory(library_dirs, dirname) |
| 365 | + # termux support for android. |
| 366 | + # system libraries (zlib) are installed in /system/lib |
| 367 | + # headers are at $PREFIX/include |
| 368 | + # user libs are at $PREFIX/lib |
| 369 | + if os.environ.get('ANDROID_ROOT', None): |
| 370 | + _add_directory(library_dirs, |
| 371 | + os.path.join(os.environ['ANDROID_ROOT'], |
| 372 | + 'lib')) |
391 | 373 |
|
392 | 374 | elif sys.platform.startswith("gnu"):
|
393 | 375 | self.add_multiarch_paths()
|
|
0 commit comments