|
| 1 | +""" |
| 2 | +Utilities useful during the build. |
| 3 | +""" |
| 4 | +# author: Loic Esteve |
| 5 | +# license: BSD |
| 6 | +import os |
| 7 | + |
| 8 | +from distutils.version import LooseVersion |
| 9 | + |
| 10 | +DEFAULT_ROOT = 'sklearn' |
| 11 | +CYTHON_MIN_VERSION = '0.23' |
| 12 | + |
| 13 | +try: |
| 14 | + from sklearn._build_utils import maybe_cythonize_extensions |
| 15 | +except ImportError: |
| 16 | + # maybe_cythonize_extensions exists from scikit-learn 0.18.1 onwards |
| 17 | + def build_from_c_and_cpp_files(extensions): |
| 18 | + """Modify the extensions to build from the .c and .cpp files. |
| 19 | +
|
| 20 | + This is useful for releases, this way cython is not required to |
| 21 | + run python setup.py install. |
| 22 | + """ |
| 23 | + for extension in extensions: |
| 24 | + sources = [] |
| 25 | + for sfile in extension.sources: |
| 26 | + path, ext = os.path.splitext(sfile) |
| 27 | + if ext in ('.pyx', '.py'): |
| 28 | + if extension.language == 'c++': |
| 29 | + ext = '.cpp' |
| 30 | + else: |
| 31 | + ext = '.c' |
| 32 | + sfile = path + ext |
| 33 | + sources.append(sfile) |
| 34 | + extension.sources = sources |
| 35 | + |
| 36 | + |
| 37 | + def maybe_cythonize_extensions(top_path, config): |
| 38 | + """Tweaks for building extensions between release and development mode.""" |
| 39 | + is_release = os.path.exists(os.path.join(top_path, 'PKG-INFO')) |
| 40 | + |
| 41 | + if is_release: |
| 42 | + build_from_c_and_cpp_files(config.ext_modules) |
| 43 | + else: |
| 44 | + message = ('Please install cython with a version >= {0} in order ' |
| 45 | + 'to build a scikit-learn development version.').format( |
| 46 | + CYTHON_MIN_VERSION) |
| 47 | + try: |
| 48 | + import Cython |
| 49 | + if LooseVersion(Cython.__version__) < CYTHON_MIN_VERSION: |
| 50 | + message += ' Your version of Cython was {0}.'.format( |
| 51 | + Cython.__version__) |
| 52 | + raise ValueError(message) |
| 53 | + from Cython.Build import cythonize |
| 54 | + except ImportError as exc: |
| 55 | + exc.args += (message,) |
| 56 | + raise |
| 57 | + |
| 58 | + config.ext_modules = cythonize(config.ext_modules) |
| 59 | + |
0 commit comments