|
| 1 | +# --------------------------------------------------------- |
| 2 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +# --------------------------------------------------------- |
| 4 | + |
| 5 | +import os |
| 6 | +import os |
| 7 | +import numpy as np |
| 8 | +from distutils.errors import CCompilerError, DistutilsExecError, DistutilsPlatformError |
| 9 | + |
| 10 | +# See if Cython is installed |
| 11 | +try: |
| 12 | + from Cython.Build import cythonize |
| 13 | +# Do nothing if Cython is not available |
| 14 | +except ImportError: |
| 15 | + # Got to provide this function. Otherwise, poetry will fail |
| 16 | + def build(setup_kwargs): |
| 17 | + pass |
| 18 | +# Cython is installed. Compile |
| 19 | +else: |
| 20 | + from setuptools import Extension |
| 21 | + from setuptools.dist import Distribution |
| 22 | + from setuptools.command.build_ext import build_ext |
| 23 | +# use cythonize to build the extensions |
| 24 | +modules = ["./anomaly_detector/univariate/*.pyx"] |
| 25 | +extensions = cythonize(modules, |
| 26 | + language_level=3, |
| 27 | + compiler_directives={'linetrace': True}, |
| 28 | + ) |
| 29 | + |
| 30 | + |
| 31 | +# cmdclass = {'build_ext': build_ext} |
| 32 | +class BuildFailed(Exception): |
| 33 | + pass |
| 34 | + |
| 35 | + |
| 36 | +class ExtBuilder(build_ext): |
| 37 | + |
| 38 | + def run(self): |
| 39 | + try: |
| 40 | + build_ext.run(self) |
| 41 | + except (DistutilsPlatformError, FileNotFoundError): |
| 42 | + raise BuildFailed('File not found. Could not compile C extension.') |
| 43 | + |
| 44 | + def build_extension(self, ext): |
| 45 | + try: |
| 46 | + build_ext.build_extension(self, ext) |
| 47 | + except (CCompilerError, DistutilsExecError, DistutilsPlatformError, ValueError): |
| 48 | + raise BuildFailed('Could not compile C extension.') |
| 49 | + |
| 50 | + |
| 51 | +cmdclass = {"build_ext": ExtBuilder} |
| 52 | + |
| 53 | + |
| 54 | +def build(setup_kwargs): |
| 55 | + """Needed for the poetry building interface.""" |
| 56 | + |
| 57 | + os.environ['CFLAGS'] = '-O3' |
| 58 | + |
| 59 | + setup_kwargs.update({ |
| 60 | + 'ext_modules': extensions, |
| 61 | + 'include_dirs': [np.get_include()], |
| 62 | + 'cmdclass': cmdclass |
| 63 | + }) |
0 commit comments