Skip to content
This repository was archived by the owner on Dec 6, 2023. It is now read-only.

Commit 15ce1c8

Browse files
authored
Merge pull request #108 from lesteve/build-from-cython
[MRG] Build directly from cython sources
2 parents 211c35b + f54f041 commit 15ce1c8

17 files changed

+95
-128655
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
*.pyc
2+
*.cpp
23
*.so
34
*~
45
.#*

Makefile

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,16 @@ DATADIR=$(HOME)/lightning_data
55

66
# Compilation...
77

8-
CYTHONSRC= $(wildcard lightning/impl/*.pyx lightning/impl/randomkit/*.pyx)
9-
CSRC= $(CYTHONSRC:.pyx=.cpp)
10-
118
inplace:
129
$(PYTHON) setup.py build_ext -i
1310

14-
all: cython inplace
15-
16-
cython: $(CSRC)
11+
all: inplace
1712

1813
clean:
19-
rm -f lightning/impl/*.c lightning/impl/*.html
14+
rm -f lightning/impl/*.cpp lightning/impl/*.html
2015
rm -f `find lightning -name "*.pyc"`
2116
rm -f `find lightning -name "*.so"`
2217

23-
%.cpp: %.pyx
24-
$(CYTHON) --cplus $<
25-
2618
# Tests...
2719
#
2820
test-code: inplace

lightning/_build_utils.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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

Comments
 (0)