code: def build_extensions(self):
      library_dirs = []
      include_dirs = []
  
      pkg_config = None
      if _cmd_exists(os.environ.get("PKG_CONFIG", "pkg-config")):
          pkg_config = _pkg_config
  
      #
      # add configured kits
      for root_name, lib_name in {
          "JPEG_ROOT": "libjpeg",
          "JPEG2K_ROOT": "libopenjp2",
          "TIFF_ROOT": ("libtiff-5", "libtiff-4"),
          "ZLIB_ROOT": "zlib",
          "FREETYPE_ROOT": "freetype2",
          "HARFBUZZ_ROOT": "harfbuzz",
          "FRIBIDI_ROOT": "fribidi",
          "LCMS_ROOT": "lcms2",
          "IMAGEQUANT_ROOT": "libimagequant",
      }.items():
          root = globals()[root_name]
  
          if root is None and root_name in os.environ:
              prefix = os.environ[root_name]
              root = (
                  os.path.join(prefix, "lib"),
                  os.path.join(prefix, "include"),
              )
  
          if root is None and pkg_config:
              if isinstance(lib_name, tuple):
                  for lib_name2 in lib_name:
                      _dbg(
                          f"Looking for `{lib_name2}` using pkg-config."
                      )
                      root = pkg_config(lib_name2)
                      if root:
                          break
              else:
                  _dbg(f"Looking for `{lib_name}` using pkg-config.")
                  root = pkg_config(lib_name)
  
          if isinstance(root, tuple):
              lib_root, include_root = root
          else:
              lib_root = include_root = root
  
          if lib_root is not None:
              if not isinstance(lib_root, (tuple, list)):
                  lib_root = (lib_root,)
              for lib_dir in lib_root:
                  _add_directory(library_dirs, lib_dir)
          if include_root is not None:
              if not isinstance(include_root, (tuple, list)):
                  include_root = (include_root,)
              for include_dir in include_root:
                  _add_directory(include_dirs, include_dir)
  
      # respect CFLAGS/CPPFLAGS/LDFLAGS
      for k in ("CFLAGS", "CPPFLAGS", "LDFLAGS"):
          if k in os.environ:
              for match in re.finditer(r"-I([^\s]+)", os.environ[k]):
                  _add_directory(include_dirs, match.group(1))
              for match in re.finditer(r"-L([^\s]+)", os.environ[k]):
                  _add_directory(library_dirs, match.group(1))
  
      # include, rpath, if set as environment variables:
      for k in ("C_INCLUDE_PATH", "CPATH", "INCLUDE"):
          if k in os.environ:
              for d in os.environ[k].split(os.path.pathsep):
                  _add_directory(include_dirs, d)
  
      for k in ("LD_RUN_PATH", "LIBRARY_PATH", "LIB"):
          if k in os.environ:
              for d in os.environ[k].split(os.path.pathsep):
                  _add_directory(library_dirs, d)
  
      _add_directory(library_dirs, os.path.join(sys.prefix, "lib"))
      _add_directory(include_dirs, os.path.join(sys.prefix, "include"))
  
      #
      # add platform directories
  
      if self.disable_platform_guessing:
          pass
  
      elif sys.platform == "cygwin":
          # pythonX.Y.dll.a is in the /usr/lib/pythonX.Y/config directory
          self.compiler.shared_lib_extension = ".dll.a"
          _add_directory(
              library_dirs,
              os.path.join(
                  "/usr/lib",
                  "python{}.{}".format(*sys.version_info),
                  "config",
              ),
          )
  
      elif sys.platform == "darwin":
          # attempt to make sure we pick freetype2 over other versions
          _add_directory(include_dirs, "/sw/include/freetype2")
          _add_directory(include_dirs, "/sw/lib/freetype2/include")
          # fink installation directories
          _add_directory(library_dirs, "/sw/lib")
          _add_directory(include_dirs, "/sw/include")
          # darwin ports installation directories
          _add_directory(library_dirs, "/opt/local/lib")
          _add_directory(include_dirs, "/opt/local/include")
  
          # if Homebrew is installed, use its lib and include directories
          try:
              prefix = (
                  subprocess.check_output(["brew", "--prefix"])
                  .strip()
                  .decode("latin1")
              )
          except Exception:
              # Homebrew not installed
              prefix = None
  
          ft_prefix = None
  
          if prefix:
              # add Homebrew's include and lib directories
              _add_directory(library_dirs, os.path.join(prefix, "lib"))
              _add_directory(
                  include_dirs, os.path.join(prefix, "include")
              )
              _add_directory(
                  include_dirs,
                  os.path.join(prefix, "opt", "zlib", "include"),
              )
              ft_prefix = os.path.join(prefix, "opt", "freetype")
  
          if ft_prefix and os.path.isdir(ft_prefix):
              # freetype might not be linked into Homebrew's prefix
              _add_directory(
                  library_dirs, os.path.join(ft_prefix, "lib")
              )
              _add_directory(
                  include_dirs, os.path.join(ft_prefix, "include")
              )
          else:
              # fall back to freetype from XQuartz if
              # Homebrew's freetype is missing
              _add_directory(library_dirs, "/usr/X11/lib")
              _add_directory(include_dirs, "/usr/X11/include")
  
          sdk_path = self.get_macos_sdk_path()
          if sdk_path:
              _add_directory(
                  library_dirs, os.path.join(sdk_path, "usr", "lib")
              )
              _add_directory(
                  include_dirs, os.path.join(sdk_path, "usr", "include")
              )
      elif (
          sys.platform.startswith("linux")
          or sys.platform.startswith("gnu")
          or sys.platform.startswith("freebsd")
      ):
          for dirname in _find_library_dirs_ldconfig():
              _add_directory(library_dirs, dirname)
          if sys.platform.startswith("linux") and os.environ.get(
              "ANDROID_ROOT"
          ):
              # termux support for android.
              # system libraries (zlib) are installed in /system/lib
              # headers are at $PREFIX/include
              # user libs are at $PREFIX/lib
              _add_directory(
                  library_dirs,
                  os.path.join(
                      os.environ["ANDROID_ROOT"],
                      "lib" if struct.calcsize("l") == 4 else "lib64",
                  ),
              )
  
      elif sys.platform.startswith("netbsd"):
          _add_directory(library_dirs, "/usr/pkg/lib")
          _add_directory(include_dirs, "/usr/pkg/include")
  
      elif sys.platform.startswith("sunos5"):
          _add_directory(library_dirs, "/opt/local/lib")
          _add_directory(include_dirs, "/opt/local/include")
  
      # FIXME: check /opt/stuff directories here?
  
      # standard locations
      if not self.disable_platform_guessing:
          _add_directory(library_dirs, "/usr/local/lib")
          _add_directory(include_dirs, "/usr/local/include")
  
          _add_directory(library_dirs, "/usr/lib")
          _add_directory(include_dirs, "/usr/include")
          # alpine, at least
          _add_directory(library_dirs, "/lib")
  
      if sys.platform == "win32":
          # on Windows, look for the OpenJPEG libraries in the location that
          # the official installer puts them
          program_files = os.environ.get("ProgramFiles", "")
          best_version = (0, 0)
          best_path = None
          for name in os.listdir(program_files):
              if name.startswith("OpenJPEG "):
                  version = tuple(
                      int(x) for x in name[9:].strip().split(".")
                  )
                  if version > best_version:
                      best_version = version
                      best_path = os.path.join(program_files, name)
  
          if best_path:
              _dbg("Adding %s to search list", best_path)
              _add_directory(
                  library_dirs, os.path.join(best_path, "lib")
              )
              _add_directory(
                  include_dirs, os.path.join(best_path, "include")
              )
  
      #
      # insert new dirs *before* default libs, to avoid conflicts
      # between Python PYD stub libs and real libraries
  
      self.compiler.library_dirs = (
          library_dirs + self.compiler.library_dirs
      )
      self.compiler.include_dirs = (
          include_dirs + self.compiler.include_dirs
      )
  
      #
      # look for available libraries
  
      feature = self.feature
  
      if feature.want("zlib"):
          _dbg("Looking for zlib")
          if _find_include_file(self, "zlib.h"):
              if _find_library_file(self, "z"):
                  feature.zlib = "z"
              elif sys.platform == "win32" and _find_library_file(
                  self, "zlib"
              ):
                  feature.zlib = "zlib"  # alternative name
  
      if feature.want("jpeg"):
          _dbg("Looking for jpeg")
          if _find_include_file(self, "jpeglib.h"):
              if _find_library_file(self, "jpeg"):
                  feature.jpeg = "jpeg"
              elif sys.platform == "win32" and _find_library_file(
                  self, "libjpeg"
              ):
                  feature.jpeg = "libjpeg"  # alternative name
  
      feature.openjpeg_version = None
      if feature.want("jpeg2000"):
          _dbg("Looking for jpeg2000")
          best_version = None
          best_path = None
  
          # Find the best version
          for directory in self.compiler.include_dirs:
              _dbg("Checking for openjpeg-#.# in %s", directory)
              try:
                  listdir = os.listdir(directory)
              except Exception:
                  # OSError, FileNotFoundError
                  continue
              for name in listdir:
                  if name.startswith("openjpeg-") and os.path.isfile(
                      os.path.join(directory, name, "openjpeg.h")
                  ):
                      _dbg(
                          "Found openjpeg.h in %s/%s", (directory, name)
                      )
                      version = tuple(
                          int(x) for x in name[9:].split(".")
                      )
                      if best_version is None or version > best_version:
                          best_version = version
                          best_path = os.path.join(directory, name)
                          _dbg(
                              "Best openjpeg version %s so far in %s",
                              (best_version, best_path),
                          )
  
          if best_version and _find_library_file(self, "openjp2"):
              # Add the directory to the include path so we can include
              # <openjpeg.h> rather than having to cope with the versioned
              # include path
              _add_directory(self.compiler.include_dirs, best_path, 0)
              feature.jpeg2000 = "openjp2"
              feature.openjpeg_version = ".".join(
                  str(x) for x in best_version
              )
  
      if feature.want("imagequant"):
          _dbg("Looking for imagequant")
          if _find_include_file(self, "libimagequant.h"):
              if _find_library_file(self, "imagequant"):
                  feature.imagequant = "imagequant"
              elif _find_library_file(self, "libimagequant"):
                  feature.imagequant = "libimagequant"
  
      if feature.want("tiff"):
          _dbg("Looking for tiff")
          if _find_include_file(self, "tiff.h"):
              if _find_library_file(self, "tiff"):
                  feature.tiff = "tiff"
              if sys.platform in [
                  "win32",
                  "darwin",
              ] and _find_library_file(self, "libtiff"):
                  feature.tiff = "libtiff"
  
      if feature.want("freetype"):
          _dbg("Looking for freetype")
          if _find_library_file(self, "freetype"):
              # look for freetype2 include files
              freetype_version = 0
              for subdir in self.compiler.include_dirs:
                  _dbg(
                      "Checking for include file %s in %s",
                      ("ft2build.h", subdir),
                  )
                  if os.path.isfile(os.path.join(subdir, "ft2build.h")):
                      _dbg("Found %s in %s", ("ft2build.h", subdir))
                      freetype_version = 21
                      subdir = os.path.join(subdir, "freetype2")
                      break
                  subdir = os.path.join(subdir, "freetype2")
                  _dbg(
                      "Checking for include file %s in %s",
                      ("ft2build.h", subdir),
                  )
                  if os.path.isfile(os.path.join(subdir, "ft2build.h")):
                      _dbg("Found %s in %s", ("ft2build.h", subdir))
                      freetype_version = 21
                      break
              if freetype_version:
                  feature.freetype = "freetype"
                  if subdir:
                      _add_directory(
                          self.compiler.include_dirs, subdir, 0
                      )
  
      if feature.freetype and feature.want("raqm"):
          if not feature.want_vendor("raqm"):  # want system Raqm
              _dbg("Looking for Raqm")
              if _find_include_file(self, "raqm.h"):
                  if _find_library_file(self, "raqm"):
                      feature.raqm = "raqm"
                  elif _find_library_file(self, "libraqm"):
                      feature.raqm = "libraqm"
          else:  # want to build Raqm from src/thirdparty
              _dbg("Looking for HarfBuzz")
              feature.harfbuzz = None
              hb_dir = _find_include_dir(self, "harfbuzz", "hb.h")
              if hb_dir:
                  if isinstance(hb_dir, str):
                      _add_directory(
                          self.compiler.include_dirs, hb_dir, 0
                      )
                  if _find_library_file(self, "harfbuzz"):
                      feature.harfbuzz = "harfbuzz"
              if feature.harfbuzz:
                  if not feature.want_vendor(
                      "fribidi"
                  ):  # want system FriBiDi
                      _dbg("Looking for FriBiDi")
                      feature.fribidi = None
                      fribidi_dir = _find_include_dir(
                          self, "fribidi", "fribidi.h"
                      )
                      if fribidi_dir:
                          if isinstance(fribidi_dir, str):
                              _add_directory(
                                  self.compiler.include_dirs,
                                  fribidi_dir,
                                  0,
                              )
                          if _find_library_file(self, "fribidi"):
                              feature.fribidi = "fribidi"
                              feature.raqm = True
                  else:  # want to build FriBiDi shim from src/thirdparty
                      feature.raqm = True
  
      if feature.want("lcms"):
          _dbg("Looking for lcms")
          if _find_include_file(self, "lcms2.h"):
              if _find_library_file(self, "lcms2"):
                  feature.lcms = "lcms2"
              elif _find_library_file(self, "lcms2_static"):
                  # alternate Windows name.
                  feature.lcms = "lcms2_static"
  
      if feature.want("webp"):
          _dbg("Looking for webp")
          if _find_include_file(
              self, "webp/encode.h"
          ) and _find_include_file(self, "webp/decode.h"):
              # In Google's precompiled zip it is call "libwebp":
              if _find_library_file(self, "webp"):
                  feature.webp = "webp"
              elif _find_library_file(self, "libwebp"):
                  feature.webp = "libwebp"
  
      if feature.want("webpmux"):
          _dbg("Looking for webpmux")
          if _find_include_file(
              self, "webp/mux.h"
          ) and _find_include_file(self, "webp/demux.h"):
              if _find_library_file(
                  self, "webpmux"
              ) and _find_library_file(self, "webpdemux"):
                  feature.webpmux = "webpmux"
              if _find_library_file(
                  self, "libwebpmux"
              ) and _find_library_file(self, "libwebpdemux"):
                  feature.webpmux = "libwebpmux"
  
      if feature.want("xcb"):
          _dbg("Looking for xcb")
          if _find_include_file(self, "xcb/xcb.h"):
              if _find_library_file(self, "xcb"):
                  feature.xcb = "xcb"
  
      for f in feature:
          if not getattr(feature, f) and feature.require(f):
              if f in ("jpeg", "zlib"):
                  raise RequiredDependencyException(f)
              raise DependencyException(f)
  
      #
      # core library
  
      libs = self.add_imaging_libs.split()
      defs = []
      if feature.tiff:
          libs.append(feature.tiff)
          defs.append(("HAVE_LIBTIFF", None))
          if sys.platform == "win32":
              # This define needs to be defined if-and-only-if it was defined
              # when compiling LibTIFF. LibTIFF doesn't expose it in `tiffconf.h`,
              # so we have to guess; by default it is defined in all Windows builds.
              # See #4237, #5243, #5359 for more information.
              defs.append(("USE_WIN32_FILEIO", None))
      if feature.jpeg:
          libs.append(feature.jpeg)
          defs.append(("HAVE_LIBJPEG", None))
      if feature.jpeg2000:
          libs.append(feature.jpeg2000)
          defs.append(("HAVE_OPENJPEG", None))
          if sys.platform == "win32" and not PLATFORM_MINGW:
              defs.append(("OPJ_STATIC", None))
      if feature.zlib:
          libs.append(feature.zlib)
          defs.append(("HAVE_LIBZ", None))
      if feature.imagequant:
          libs.append(feature.imagequant)
          defs.append(("HAVE_LIBIMAGEQUANT", None))
      if feature.xcb:
          libs.append(feature.xcb)
          defs.append(("HAVE_XCB", None))
      if sys.platform == "win32":
          libs.extend(["kernel32", "user32", "gdi32"])
      if struct.unpack("h", b"\0\1")[0] == 1:
          defs.append(("WORDS_BIGENDIAN", None))
  
      defs.append(("PILLOW_VERSION", f'"{PILLOW_VERSION}"'))
  
      self._update_extension("PIL._imaging", libs, defs)
  
      #
      # additional libraries
  
      if feature.freetype:
          srcs = []
          libs = ["freetype"]
          defs = []
          if feature.raqm:
              if not feature.want_vendor("raqm"):  # using system Raqm
                  defs.append(("HAVE_RAQM", None))
                  defs.append(("HAVE_RAQM_SYSTEM", None))
                  libs.append(feature.raqm)
              else:  # building Raqm from src/thirdparty
                  defs.append(("HAVE_RAQM", None))
                  srcs.append("src/thirdparty/raqm/raqm.c")
                  libs.append(feature.harfbuzz)
                  if not feature.want_vendor(
                      "fribidi"
                  ):  # using system FriBiDi
                      defs.append(("HAVE_FRIBIDI_SYSTEM", None))
                      libs.append(feature.fribidi)
                  else:  # building FriBiDi shim from src/thirdparty
                      srcs.append(
                          "src/thirdparty/fribidi-shim/fribidi.c"
                      )
          self._update_extension("PIL._imagingft", libs, defs, srcs)
  
      else:
          self._remove_extension("PIL._imagingft")
  
      if feature.lcms:
          extra = []
          if sys.platform == "win32":
              extra.extend(["user32", "gdi32"])
          self._update_extension(
              "PIL._imagingcms", [feature.lcms] + extra
          )
      else:
          self._remove_extension("PIL._imagingcms")
  
      if feature.webp:
          libs = [feature.webp]
          defs = []
  
          if feature.webpmux:
              defs.append(("HAVE_WEBPMUX", None))
              libs.append(feature.webpmux)
              libs.append(feature.webpmux.replace("pmux", "pdemux"))
  
          self._update_extension("PIL._webp", libs, defs)
      else:
          self._remove_extension("PIL._webp")
  
      tk_libs = ["psapi"] if sys.platform in ("win32", "cygwin") else []
      self._update_extension("PIL._imagingtk", tk_libs)
  
      build_ext.build_extensions(self)
  
      #
      # sanity checks
  
      self.summary_report(feature)
  
imports: from __future__ import annotations
  import os
  import re
  import shutil
  import struct
  import subprocess
  import sys
  import warnings
  from setuptools import Extension, setup
  from setuptools.command.build_ext import build_ext
label_desc: Use image node_id 6 for calling the pil_build_ext obj's underlying member method code with example usage: obj.build_extensions() without return types