""" Cythonize the .pyx file for sdist, and compile it for wheels. NB: produced wheels are not valid, but the sdist should be. """ import os from Cython.Build import cythonize from subprocess import check_output, CalledProcessError, DEVNULL, call from tempfile import TemporaryFile from setuptools.command.build_py import build_py def check_include(library_name, header): command = [os.environ.get("PKG_CONFIG", "pkg-config"), "--cflags", library_name] try: cflags = check_output(command).decode("utf-8").split() except FileNotFoundError: print("pkg-config not found.") return False except CalledProcessError: # pkg-config already prints the missing libraries on stderr. return False command = [os.environ.get("CC", "cc")] + cflags + ["-E", "-"] with TemporaryFile("w+") as c_file: c_file.write("#include <%s>" % header) c_file.seek(0) try: return call(command, stdin=c_file, stdout=DEVNULL, stderr=DEVNULL) == 0 except FileNotFoundError: print("%s headers not found." % library_name) return False class Build(build_py): def run(self): self.run_command("build_ext") return super().run() def initialize_options(self): super().initialize_options() has_python_headers = check_include("python3", "Python.h") has_stringprep_headers = check_include("libidn", "stringprep.h") if has_python_headers and has_stringprep_headers: self.distribution.ext_modules = cythonize("slixmpp/stringprep.pyx") else: print("Falling back to the slow stringprep module.")