Skip to content

Commit

Permalink
setup: use scikit-build for building cmatrices and cshape C extensions
Browse files Browse the repository at this point in the history
See #283

This commit introduces new dependencies: "scikit-built" and "cmake".

scikit-build is a drop-in replacement to setuptools.setup function
allowing to easily compile and package extensions (C/C++/Cython) by
bridging CMake and setuptools. See http://scikit-build.org

CMake is is an open-source, cross-platform family of tools designed
to build, test and package software. See https://cmake.org

Currently, scikit-build and cmake have to be explicitly (see [1]) installed
on the system. This could be done by simply doing:

  pip install -U scikit-build cmake

How does it work ?
------------------

In addition to simplifying setup.py, two new file have been added:

<root>
  |
  |---- CMakeLists.txt                          (1)
  .
  .
  .
  |--- ...src
  |        |--- CMakeLists.txt                  (2)
  .
  .

The first CMakeLists.txt specifies requirements for

* Python interpreter,

* the associated python libraries

* and a "PythonExtension" modules containing convenience CMake
  functions allowing to easily build extension by encapsulating
  system introspection and platform specific logic.

and include the subdirectory associated with the other CMakeLists.txt.

The second CMakeLists.txt is specific to "cmatrices" and "cshape"
extensions and it specifies:

* requirement for NumPy

Then, it declares:

* libraries for each extension, associate python extension specific properties

* and finally add an install rule specifying where in the package
  hierarchy the extension should be installed.

Et voila,

[1] Note that improvement to python packaging will be available shortly and
it will be possible to include scikit-build and cmake directly in pyproject.toml
See here for more details: https://www.python.org/dev/peps/pep-0518/
  • Loading branch information
jcfr committed Oct 11, 2017
1 parent aca0d08 commit 1663e1d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 15 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,7 @@ docs/_build/

# PyBuilder
target/

# scikit-build
_skbuild
MANIFEST
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
cmake_minimum_required(VERSION 3.7)

project(radiomics)

find_package(PythonInterp REQUIRED)
find_package(PythonLibs REQUIRED)
find_package(PythonExtensions REQUIRED)

add_subdirectory(radiomics/src)
11 changes: 11 additions & 0 deletions radiomics/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
find_package(NumPy REQUIRED)

add_library(_cmatrices MODULE _cmatrices.c cmatrices.c)
python_extension_module(_cmatrices)
target_include_directories(_cmatrices PRIVATE ${NumPy_INCLUDE_DIR})

add_library(_cshape MODULE _cshape.c cshape.c)
python_extension_module(_cshape)
target_include_directories(_cshape PRIVATE ${NumPy_INCLUDE_DIR})

install(TARGETS _cmatrices _cshape LIBRARY DESTINATION radiomics)
5 changes: 3 additions & 2 deletions scikit-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ install:
commands:
- python --version
- python -m pip install --disable-pip-version-check --upgrade pip
- $<RUN_ENV> pip install wheel>=0.29.0
- $<RUN_ENV> pip install setuptools>=28.0.0
- $<RUN_ENV> pip install -U scikit-build
- $<RUN_ENV> pip install -U cmake
- $<RUN_ENV> pip install --trusted-host www.itk.org -f https://itk.org/SimpleITKDoxygen/html/PyDownloadPage.html SimpleITK>=0.9.1
- $<RUN_ENV> python -c "import SimpleITK; print('SimpleITK Version:' + SimpleITK.Version_VersionString())"
- $<RUN_ENV> pip install -r requirements.txt
Expand All @@ -54,6 +54,7 @@ build:

test:
commands:
- $<RUN_ENV> pip install -e .
- $<RUN_ENV> python setup.py test --args="--with-xunit --logging-level=DEBUG"

circleci:
Expand Down
24 changes: 11 additions & 13 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
#!/usr/bin/env python

from distutils import sysconfig
from __future__ import print_function

import numpy

from setuptools import Extension, setup
import sys
import versioneer

from setuptools.command.test import test as TestCommand


import versioneer
try:
from skbuild import setup
except ImportError:
print('scikit-build is required to build from source.', file=sys.stderr)
print('Please run:', file=sys.stderr)
print('', file=sys.stderr)
print(' python -m pip install scikit-build')
sys.exit(1)

with open('requirements.txt', 'r') as fp:
requirements = list(filter(bool, (line.strip() for line in fp)))
Expand Down Expand Up @@ -45,13 +51,6 @@ def run_tests(self):
commands = versioneer.get_cmdclass()
commands['test'] = NoseTestCommand

incDirs = [sysconfig.get_python_inc(), numpy.get_include()]

ext = [Extension("radiomics._cmatrices", ["radiomics/src/_cmatrices.c", "radiomics/src/cmatrices.c"],
include_dirs=incDirs),
Extension("radiomics._cshape", ["radiomics/src/_cshape.c", "radiomics/src/cshape.c"],
include_dirs=incDirs)]

setup(
name='pyradiomics',

Expand All @@ -64,7 +63,6 @@ def run_tests(self):
cmdclass=commands,

packages=['radiomics', 'radiomics.scripts'],
ext_modules=ext,
zip_safe=False,
package_data={'radiomics': ['schemas/paramSchema.yaml', 'schemas/schemaFuncs.py']},

Expand Down

0 comments on commit 1663e1d

Please sign in to comment.