From: Michael Fladischer <FladischerMichael@fladi.at>
Date: Mon, 24 Feb 2020 20:04:49 +0100
Subject: Drop homegrown build extension and replace it with standard cython
 setup.

---
 httptools/parser/cparser.pxd |   2 +-
 setup.py                     | 161 +++----------------------------------------
 2 files changed, 12 insertions(+), 151 deletions(-)

diff --git a/httptools/parser/cparser.pxd b/httptools/parser/cparser.pxd
index bad2060..7644a1c 100644
--- a/httptools/parser/cparser.pxd
+++ b/httptools/parser/cparser.pxd
@@ -1,7 +1,7 @@
 from libc.stdint cimport uint16_t, uint32_t, uint64_t
 
 
-cdef extern from "../../vendor/http-parser/http_parser.h":
+cdef extern from "http_parser.h":
     ctypedef int (*http_data_cb) (http_parser*,
                                   const char *at,
                                   size_t length) except -1
diff --git a/setup.py b/setup.py
index 3d6e63a..3bf517b 100644
--- a/setup.py
+++ b/setup.py
@@ -1,133 +1,14 @@
-import sys
+from setuptools import setup, Extension
+from setuptools.command.build_ext import build_ext as build_ext
 
-vi = sys.version_info
-if vi < (3, 5):
-    raise RuntimeError('httptools require Python 3.5 or greater')
-else:
-    import os.path
-    import pathlib
+from Cython.Build import cythonize
 
-    from setuptools import setup, Extension
-    from setuptools.command.build_ext import build_ext as build_ext
 
-
-CFLAGS = ['-O2']
-
-ROOT = pathlib.Path(__file__).parent
-
-CYTHON_DEPENDENCY = 'Cython==0.29.14'
-
-
-class httptools_build_ext(build_ext):
-    user_options = build_ext.user_options + [
-        ('cython-always', None,
-            'run cythonize() even if .c files are present'),
-        ('cython-annotate', None,
-            'Produce a colorized HTML version of the Cython source.'),
-        ('cython-directives=', None,
-            'Cythion compiler directives'),
-        ('use-system-http-parser', None,
-            'Use the system provided http-parser, instead of the bundled one'),
-    ]
-
-    boolean_options = build_ext.boolean_options + [
-        'cython-always',
-        'cython-annotate',
-        'use-system-http-parser',
-    ]
-
-    def initialize_options(self):
-        # initialize_options() may be called multiple times on the
-        # same command object, so make sure not to override previously
-        # set options.
-        if getattr(self, '_initialized', False):
-            return
-
-        super().initialize_options()
-        self.use_system_http_parser = False
-        self.cython_always = False
-        self.cython_annotate = None
-        self.cython_directives = None
-
-    def finalize_options(self):
-        # finalize_options() may be called multiple times on the
-        # same command object, so make sure not to override previously
-        # set options.
-        if getattr(self, '_initialized', False):
-            return
-
-        need_cythonize = self.cython_always
-        cfiles = {}
-
-        for extension in self.distribution.ext_modules:
-            for i, sfile in enumerate(extension.sources):
-                if sfile.endswith('.pyx'):
-                    prefix, ext = os.path.splitext(sfile)
-                    cfile = prefix + '.c'
-
-                    if os.path.exists(cfile) and not self.cython_always:
-                        extension.sources[i] = cfile
-                    else:
-                        if os.path.exists(cfile):
-                            cfiles[cfile] = os.path.getmtime(cfile)
-                        else:
-                            cfiles[cfile] = 0
-                        need_cythonize = True
-
-        if need_cythonize:
-            try:
-                import Cython
-            except ImportError:
-                raise RuntimeError(
-                    'please install Cython to compile httptools from source')
-
-            if Cython.__version__ < '0.29':
-                raise RuntimeError(
-                    'httptools requires Cython version 0.29 or greater')
-
-            from Cython.Build import cythonize
-
-            directives = {}
-            if self.cython_directives:
-                for directive in self.cython_directives.split(','):
-                    k, _, v = directive.partition('=')
-                    if v.lower() == 'false':
-                        v = False
-                    if v.lower() == 'true':
-                        v = True
-
-                    directives[k] = v
-
-            self.distribution.ext_modules[:] = cythonize(
-                self.distribution.ext_modules,
-                compiler_directives=directives,
-                annotate=self.cython_annotate)
-
-        super().finalize_options()
-
-        self._initialized = True
-
-    def build_extensions(self):
-        if self.use_system_http_parser:
-            self.compiler.add_library('http_parser')
-
-            if sys.platform == 'darwin' and \
-                    os.path.exists('/opt/local/include'):
-                # Support macports on Mac OS X.
-                self.compiler.add_include_dir('/opt/local/include')
-        else:
-            self.compiler.add_include_dir(str(ROOT / 'vendor' / 'http-parser'))
-            self.distribution.ext_modules[0].sources.append(
-                'vendor/http-parser/http_parser.c')
-
-        super().build_extensions()
-
-
-with open(str(ROOT / 'README.md')) as f:
+with open('README.md') as f:
     long_description = f.read()
 
 
-with open(str(ROOT / 'httptools' / '_version.py')) as f:
+with open('httptools/_version.py') as f:
     for line in f:
         if line.startswith('__version__ ='):
             _, _, version = line.partition('=')
@@ -137,15 +18,6 @@ with open(str(ROOT / 'httptools' / '_version.py')) as f:
         raise RuntimeError(
             'unable to read the version from httptools/_version.py')
 
-
-setup_requires = []
-
-if (not (ROOT / 'httptools' / 'parser' / 'parser.c').exists() or
-        '--cython-always' in sys.argv):
-    # No Cython output, require Cython to build.
-    setup_requires.append(CYTHON_DEPENDENCY)
-
-
 setup(
     name='httptools',
     version=VERSION,
@@ -168,24 +40,13 @@ setup(
     author_email='yury@magic.io',
     license='MIT',
     packages=['httptools', 'httptools.parser'],
-    cmdclass={
-        'build_ext': httptools_build_ext,
-    },
-    ext_modules=[
+    ext_modules=cythonize([
         Extension(
             "httptools.parser.parser",
-            sources=[
-                "httptools/parser/parser.pyx",
-            ],
-            extra_compile_args=CFLAGS,
-        ),
-    ],
+            ["httptools/parser/parser.pyx"],
+            libraries=['http_parser']
+        )
+    ]),
     include_package_data=False,
-    test_suite='tests.suite',
-    setup_requires=setup_requires,
-    extras_require={
-        'test': [
-            CYTHON_DEPENDENCY
-        ]
-    }
+    test_suite='tests.suite'
 )
