0002-Merge-pull-request-11983-from-anntzer-builddepchecks.patch 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. From 923ce72409f184bd8e8c61b196260891036ba87e Mon Sep 17 00:00:00 2001
  2. From: Antony Lee <anntzer.lee@gmail.com>
  3. Date: Thu, 30 Aug 2018 15:27:55 +0200
  4. Subject: [PATCH] Simplify version checks for freetype and libpng.
  5. Currently, setupext.py replicates a lot of work done by the compiler to
  6. check whether header files are present, and whether freetype and libpng
  7. have sufficiently recent versions.
  8. Instead, we can just add a small stub source file at the top of the
  9. extension sources which just tries to include the header and checks the
  10. version macros. If the header is not found, compilation will
  11. immediately abort with `foo.h: No such file or directory`; if the
  12. version is too old, we can emit an appropriate error message (`#pragma
  13. message` is supported by all major compilers and allows expanding of
  14. macros in the error message).
  15. [Retrieved from:
  16. https://github.com/matplotlib/matplotlib/commit/d1060a885309ec7ac19ca912d3011a5eb1673bd5]
  17. Signed-off-by: Fabrice Fontaine <fontaine.fabrice@gmail.com>
  18. ---
  19. setupext.py | 83 +++++-----------------------------------
  20. src/checkdep_freetype2.c | 13 +++++++
  21. src/checkdep_libpng.c | 5 +++
  22. 3 files changed, 28 insertions(+), 73 deletions(-)
  23. create mode 100644 src/checkdep_freetype2.c
  24. create mode 100644 src/checkdep_libpng.c
  25. diff --git a/setupext.py b/setupext.py
  26. index d5f4b81f562..a5163e39288 100644
  27. --- a/setupext.py
  28. +++ b/setupext.py
  29. @@ -814,6 +814,13 @@ def add_flags(self, ext, add_sources=True):
  30. for x in agg_sources)
  31. +# For FreeType2 and libpng, we add a separate checkdep_foo.c source to at the
  32. +# top of the extension sources. This file is compiled first and immediately
  33. +# aborts the compilation either with "foo.h: No such file or directory" if the
  34. +# header is not found, or an appropriate error message if the header indicates
  35. +# a too-old version.
  36. +
  37. +
  38. class FreeType(SetupPackage):
  39. name = "freetype"
  40. pkg_names = {
  41. @@ -825,59 +832,8 @@ class FreeType(SetupPackage):
  42. "windows_url": "http://gnuwin32.sourceforge.net/packages/freetype.htm"
  43. }
  44. - def check(self):
  45. - if options.get('local_freetype'):
  46. - return "Using local version for testing"
  47. -
  48. - if sys.platform == 'win32':
  49. - try:
  50. - check_include_file(get_include_dirs(), 'ft2build.h', 'freetype')
  51. - except CheckFailed:
  52. - check_include_file(get_include_dirs(), os.path.join('freetype2', 'ft2build.h'), 'freetype')
  53. - return 'Using unknown version found on system.'
  54. -
  55. - status, output = subprocess.getstatusoutput(
  56. - "freetype-config --ftversion")
  57. - if status == 0:
  58. - version = output
  59. - else:
  60. - version = None
  61. -
  62. - # Early versions of freetype grep badly inside freetype-config,
  63. - # so catch those cases. (tested with 2.5.3).
  64. - if version is None or 'No such file or directory\ngrep:' in version:
  65. - version = self.version_from_header()
  66. -
  67. - # pkg_config returns the libtool version rather than the
  68. - # freetype version so we need to explicitly pass the version
  69. - # to _check_for_pkg_config
  70. - return self._check_for_pkg_config(
  71. - 'freetype2', 'ft2build.h',
  72. - min_version='2.3', version=version)
  73. -
  74. - def version_from_header(self):
  75. - version = 'unknown'
  76. - ext = self.get_extension()
  77. - if ext is None:
  78. - return version
  79. - # Return the first version found in the include dirs.
  80. - for include_dir in ext.include_dirs:
  81. - header_fname = os.path.join(include_dir, 'freetype.h')
  82. - if os.path.exists(header_fname):
  83. - major, minor, patch = 0, 0, 0
  84. - with open(header_fname, 'r') as fh:
  85. - for line in fh:
  86. - if line.startswith('#define FREETYPE_'):
  87. - value = line.rsplit(' ', 1)[1].strip()
  88. - if 'MAJOR' in line:
  89. - major = value
  90. - elif 'MINOR' in line:
  91. - minor = value
  92. - else:
  93. - patch = value
  94. - return '.'.join([major, minor, patch])
  95. -
  96. def add_flags(self, ext):
  97. + ext.sources.insert(0, 'src/checkdep_freetype2.c')
  98. if options.get('local_freetype'):
  99. src_path = os.path.join(
  100. 'build', 'freetype-{0}'.format(LOCAL_FREETYPE_VERSION))
  101. @@ -1058,30 +1014,11 @@ class Png(SetupPackage):
  102. "windows_url": "http://gnuwin32.sourceforge.net/packages/libpng.htm"
  103. }
  104. - def check(self):
  105. - if sys.platform == 'win32':
  106. - check_include_file(get_include_dirs(), 'png.h', 'png')
  107. - return 'Using unknown version found on system.'
  108. -
  109. - status, output = subprocess.getstatusoutput("libpng-config --version")
  110. - if status == 0:
  111. - version = output
  112. - else:
  113. - version = None
  114. -
  115. - try:
  116. - return self._check_for_pkg_config(
  117. - 'libpng', 'png.h',
  118. - min_version='1.2', version=version)
  119. - except CheckFailed as e:
  120. - if has_include_file(get_include_dirs(), 'png.h'):
  121. - return str(e) + ' Using unknown version found on system.'
  122. - raise
  123. -
  124. def get_extension(self):
  125. sources = [
  126. + 'src/checkdep_libpng.c',
  127. 'src/_png.cpp',
  128. - 'src/mplutils.cpp'
  129. + 'src/mplutils.cpp',
  130. ]
  131. ext = make_extension('matplotlib._png', sources)
  132. pkg_config.setup_extension(
  133. diff --git a/src/checkdep_freetype2.c b/src/checkdep_freetype2.c
  134. new file mode 100644
  135. index 00000000000..bf9a8c94e38
  136. --- /dev/null
  137. +++ b/src/checkdep_freetype2.c
  138. @@ -0,0 +1,13 @@
  139. +#include <ft2build.h>
  140. +#include FT_FREETYPE_H
  141. +
  142. +#define XSTR(x) STR(x)
  143. +#define STR(x) #x
  144. +
  145. +#pragma message("Compiling with FreeType version " \
  146. + XSTR(FREETYPE_MAJOR) "." XSTR(FREETYPE_MINOR) "." XSTR(FREETYPE_PATCH) ".")
  147. +#if FREETYPE_MAJOR << 16 + FREETYPE_MINOR << 8 + FREETYPE_PATCH < 0x020300
  148. + #error "FreeType version 2.3 or higher is required." \
  149. + "Consider setting the MPLLOCALFREETYPE environment variable to 1."
  150. + #error
  151. +#endif
  152. diff --git a/src/checkdep_libpng.c b/src/checkdep_libpng.c
  153. new file mode 100644
  154. index 00000000000..5ebe5cbe4d7
  155. --- /dev/null
  156. +++ b/src/checkdep_libpng.c
  157. @@ -0,0 +1,5 @@
  158. +#include <png.h>
  159. +#pragma message("Compiling with libpng version " PNG_LIBPNG_VER_STRING ".")
  160. +#if PNG_LIBPNG_VER < 10200
  161. + #error "libpng version 1.2 or higher is required."
  162. +#endif