0001-pipcl.py-allow-providing-python-config-externally.patch 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. From ca3417b8d605ccdb2e6c516c5e0c79180381627c Mon Sep 17 00:00:00 2001
  2. From: =?UTF-8?q?Rapha=C3=ABl=20M=C3=A9lotte?= <raphael.melotte@mind.be>
  3. Date: Sun, 4 Feb 2024 16:13:45 +0100
  4. Subject: [PATCH] pipcl.py: allow providing python-config externally
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. When cross-compiling (e.g. using Buildroot), the python-config
  9. executable that resides next to the host python executable provides
  10. incorrect includes (the ones for the host).
  11. Since the correct path to python-config cannot be guessed, add an
  12. additional environment variable to allow setting the path to the
  13. correct python-config executable externally.
  14. Signed-off-by: Raphaël Mélotte <raphael.melotte@mind.be>
  15. Upstream: https://github.com/pymupdf/PyMuPDF/commit/3a214b0c144d86fd1329b26030f6b9f2a6b27020
  16. ---
  17. pipcl.py | 72 +++++++++++++++++++++++++++++---------------------------
  18. setup.py | 3 +++
  19. 2 files changed, 40 insertions(+), 35 deletions(-)
  20. diff --git a/pipcl.py b/pipcl.py
  21. index 209f660..c154774 100644
  22. --- a/pipcl.py
  23. +++ b/pipcl.py
  24. @@ -1789,43 +1789,45 @@ class PythonFlags:
  25. self.ldflags = f'-L {_lib_dir}'
  26. else:
  27. - # We use python-config which appears to work better than pkg-config
  28. - # because it copes with multiple installed python's, e.g.
  29. - # manylinux_2014's /opt/python/cp*-cp*/bin/python*.
  30. - #
  31. - # But... on non-macos it seems that we should not attempt to specify
  32. - # libpython on the link command. The manylinux docker containers
  33. - # don't actually contain libpython.so, and it seems that this
  34. - # deliberate. And the link command runs ok.
  35. - #
  36. - python_exe = os.path.realpath( sys.executable)
  37. - if darwin():
  38. - # Basic install of dev tools with `xcode-select --install` doesn't
  39. - # seem to provide a `python3-config` or similar, but there is a
  40. - # `python-config.py` accessible via sysconfig.
  41. + python_config = os.environ.get("PYMUPDF_PYTHON_CONFIG")
  42. + if not python_config:
  43. + # We use python-config which appears to work better than pkg-config
  44. + # because it copes with multiple installed python's, e.g.
  45. + # manylinux_2014's /opt/python/cp*-cp*/bin/python*.
  46. #
  47. - # We try different possibilities and use the last one that
  48. - # works.
  49. + # But... on non-macos it seems that we should not attempt to specify
  50. + # libpython on the link command. The manylinux docker containers
  51. + # don't actually contain libpython.so, and it seems that this
  52. + # deliberate. And the link command runs ok.
  53. #
  54. - python_config = None
  55. - for pc in (
  56. - f'python3-config',
  57. - f'{sys.executable} {sysconfig.get_config_var("srcdir")}/python-config.py',
  58. - f'{python_exe}-config',
  59. - ):
  60. - e = subprocess.run(
  61. - f'{pc} --includes',
  62. - shell=1,
  63. - stdout=subprocess.DEVNULL,
  64. - stderr=subprocess.DEVNULL,
  65. - check=0,
  66. - ).returncode
  67. - log1(f'{e=} from {pc!r}.')
  68. - if e == 0:
  69. - python_config = pc
  70. - assert python_config, f'Cannot find python-config'
  71. - else:
  72. - python_config = f'{python_exe}-config'
  73. + python_exe = os.path.realpath( sys.executable)
  74. + if darwin():
  75. + # Basic install of dev tools with `xcode-select --install` doesn't
  76. + # seem to provide a `python3-config` or similar, but there is a
  77. + # `python-config.py` accessible via sysconfig.
  78. + #
  79. + # We try different possibilities and use the last one that
  80. + # works.
  81. + #
  82. + python_config = None
  83. + for pc in (
  84. + f'python3-config',
  85. + f'{sys.executable} {sysconfig.get_config_var("srcdir")}/python-config.py',
  86. + f'{python_exe}-config',
  87. + ):
  88. + e = subprocess.run(
  89. + f'{pc} --includes',
  90. + shell=1,
  91. + stdout=subprocess.DEVNULL,
  92. + stderr=subprocess.DEVNULL,
  93. + check=0,
  94. + ).returncode
  95. + log1(f'{e=} from {pc!r}.')
  96. + if e == 0:
  97. + python_config = pc
  98. + assert python_config, f'Cannot find python-config'
  99. + else:
  100. + python_config = f'{python_exe}-config'
  101. log1(f'Using {python_config=}.')
  102. try:
  103. self.includes = run( f'{python_config} --includes', capture=1).strip()
  104. diff --git a/setup.py b/setup.py
  105. index 23a5c78..4b3b5c7 100755
  106. --- a/setup.py
  107. +++ b/setup.py
  108. @@ -36,6 +36,9 @@ Environmental variables:
  109. PYMUPDF_MUPDF_LIB
  110. Directory containing MuPDF libraries, (libmupdf.so,
  111. libmupdfcpp.so).
  112. +
  113. + PYMUPDF_PYTHON_CONFIG
  114. + Optional path to python-config.
  115. PYMUPDF_SETUP_IMPLEMENTATIONS
  116. Must be one of 'a', 'b', 'ab'. If unset we use 'ab'.
  117. --
  118. 2.41.0