Skip to content

Commit bd4fe01

Browse files
committed
Merge pull request pybind#3 from dean0x7d/master
General improvements
2 parents 37c3aec + 1b97821 commit bd4fe01

12 files changed

+126
-78
lines changed

.appveyor.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ version: 1.0.{build}
22
image: Visual Studio 2015
33
environment:
44
matrix:
5+
- PATH: C:\Python27\;C:\Python27\Scripts\;%PATH%
6+
- PATH: C:\Python27-x64\;C:\Python27-x64\Scripts\;%PATH%
7+
- PATH: C:\Python35\;C:\Python35\Scripts\;%PATH%
58
- PATH: C:\Python35-x64\;C:\Python35-x64\Scripts\;%PATH%
6-
- PATH: C:\Miniconda35-x64\;C:\Miniconda35-x64\Scripts\;%PATH%
79
install:
810
- cmd: git submodule update -q --init --recursive
911
build_script:
10-
- cmd: pip install .
12+
- cmd: python setup.py sdist
13+
- cmd: pip install --verbose dist\cmake_example-0.0.1.zip
1114
test_script:
12-
- cmd: python test.py
15+
- cmd: python tests\test.py

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
build/
2+
dist/
23
*.so
34
*.py[cod]
45
*.egg-info

.travis.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ before_install:
2323
- virtualenv -p python$PYTHON venv
2424
- source venv/bin/activate
2525
install:
26-
- pip install .
26+
- python setup.py sdist
27+
- pip install --verbose dist/*.tar.gz
2728
script:
28-
- python test.py
29+
- python tests/test.py

CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
cmake_minimum_required(VERSION 2.8.12)
2-
project(pybind11_cmake_example)
2+
project(cmake_example)
33

44
add_subdirectory(pybind11)
5-
65
pybind11_add_module(cmake_example src/main.cpp)

MANIFEST.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
include README.md LICENSE
2+
global-include CMakeLists.txt *.cmake
3+
recursive-include src *
4+
recursive-include pybind11/include *.h

README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# cmake_example for pybind11
2+
3+
[![Build Status](https://travis-ci.org/pybind/cmake_example.svg?branch=master)](https://travis-ci.org/pybind/cmake_example)
4+
[![Build status](https://ci.appveyor.com/api/projects/status/8gtjyogdmy9amqm1/branch/master?svg=true)](https://ci.appveyor.com/project/dean0x7d/cmake-example/branch/master)
5+
6+
An example [pybind11](https://github.com/pybind/pybind11) module built with a
7+
CMake-based build system. This is useful for C++ codebases that have an existing
8+
CMake project structure.
9+
10+
11+
## Prerequisites
12+
13+
**On Unix (Linux, OS X)**
14+
15+
* A compiler with C++11 support
16+
* CMake >= 2.8.12
17+
18+
**On Windows**
19+
20+
* Visual Studio 2015 (required for all Python versions, see notes below)
21+
* CMake >= 3.1
22+
23+
24+
## Installation
25+
26+
Just clone this repository and pip install. Note the `--recursive` option which is
27+
needed for the pybind11 submodule:
28+
29+
```bash
30+
git clone --recursive https://github.com/pybind/cmake_example.git
31+
pip install ./cmake_example
32+
```
33+
34+
With the `setup.py` file included in this example, the `pip install` command will
35+
invoke CMake and build the pybind11 module as specified in `CMakeLists.txt`.
36+
37+
38+
## Special notes for Windows
39+
40+
**Compiler requirements**
41+
42+
Pybind11 requires a C++11 compliant compiler, i.e Visual Studio 2015 on Windows.
43+
This applies to all Python versions, including 2.7. Unlike regular C extension
44+
modules, it's perfectly fine to compile a pybind11 module with a VS version newer
45+
than the target Python's VS version. See the [FAQ] for more details.
46+
47+
**Runtime requirements**
48+
49+
The Visual C++ 2015 redistributable packages are a runtime requirement for this
50+
project. It can be found [here][vs2015_runtime]. If you use the Anaconda Python
51+
distribution, you can add `vs2015_runtime` as a platform-dependent runtime
52+
requirement for you package: see the `conda.recipe/meta.yaml` file in this example.
53+
54+
55+
## License
56+
57+
Pybind11 is provided under a BSD-style license that can be found in the LICENSE
58+
file. By using, distributing, or contributing to this project, you agree to the
59+
terms and conditions of this license.
60+
61+
62+
## Test call
63+
64+
```python
65+
import cmake_example
66+
cmake_example.add(1, 2)
67+
```
68+
69+
70+
[FAQ]: http://pybind11.rtfd.io/en/latest/faq.html#working-with-ancient-visual-studio-2009-builds-on-windows
71+
[vs2015_runtime]: https://www.microsoft.com/en-us/download/details.aspx?id=48145

license.md

Lines changed: 0 additions & 24 deletions
This file was deleted.

readme.md

Lines changed: 0 additions & 17 deletions
This file was deleted.

setup.py

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,57 @@
11
import os
2+
import re
23
import sys
34
import platform
45
import subprocess
56

67
from setuptools import setup, Extension
78
from setuptools.command.build_ext import build_ext
8-
from distutils.spawn import find_executable
9+
from distutils.version import LooseVersion
910

1011

1112
class CMakeExtension(Extension):
1213
def __init__(self, name, sourcedir=''):
1314
Extension.__init__(self, name, sources=[])
14-
self.sourcedir = sourcedir
15+
self.sourcedir = os.path.abspath(sourcedir)
1516

1617

1718
class CMakeBuild(build_ext):
1819
def run(self):
19-
if find_executable('cmake') is None:
20-
print("CMake must be installed to build this extension")
21-
sys.exit(-1)
20+
try:
21+
out = subprocess.check_output(['cmake', '--version'])
22+
except OSError:
23+
raise RuntimeError("CMake must be installed to build the following extensions: " +
24+
", ".join(e.name for e in self.extensions))
25+
26+
if platform.system() == "Windows":
27+
cmake_version = LooseVersion(re.search(r'version\s*([\d.]+)', out.decode()).group(1))
28+
if cmake_version < '3.1.0':
29+
raise RuntimeError("CMake >= 3.1.0 is required on Windows")
2230

2331
for ext in self.extensions:
24-
build_dir = os.path.join(os.path.dirname(__file__), 'build', 'cmake')
25-
if not os.path.exists(build_dir):
26-
os.makedirs(build_dir)
27-
cmake_dir = os.path.abspath(ext.sourcedir)
28-
29-
extpath = self.get_ext_fullpath(ext.name)
30-
extfulldir = os.path.abspath(os.path.dirname(extpath))
31-
cmake_args = ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + extfulldir,
32-
'-DPYTHON_EXECUTABLE=' + sys.executable]
33-
build_args = ['--config', 'Release']
34-
35-
if platform.system() == "Windows":
36-
cmake_args += ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE=' + extfulldir]
37-
if sys.maxsize > 2**32:
38-
cmake_args += ['-A', 'x64']
39-
build_args += ['--', '/m']
40-
else:
41-
build_args += ['--', '-j2']
42-
43-
subprocess.check_call(['cmake', cmake_dir] + cmake_args, cwd=build_dir)
44-
subprocess.check_call(['cmake', '--build', '.'] + build_args, cwd=build_dir)
32+
self.build_extension(ext)
33+
34+
def build_extension(self, ext):
35+
extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
36+
cmake_args = ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + extdir,
37+
'-DPYTHON_EXECUTABLE=' + sys.executable]
38+
39+
cfg = 'Debug' if self.debug else 'Release'
40+
build_args = ['--config', cfg]
41+
42+
if platform.system() == "Windows":
43+
cmake_args += ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}'.format(cfg.upper(), extdir)]
44+
if sys.maxsize > 2**32:
45+
cmake_args += ['-A', 'x64']
46+
build_args += ['--', '/m']
47+
else:
48+
cmake_args += ['-DCMAKE_BUILD_TYPE=' + cfg]
49+
build_args += ['--', '-j2']
50+
51+
if not os.path.exists(self.build_temp):
52+
os.makedirs(self.build_temp)
53+
subprocess.check_call(['cmake', ext.sourcedir] + cmake_args, cwd=self.build_temp)
54+
subprocess.check_call(['cmake', '--build', '.'] + build_args, cwd=self.build_temp)
4555

4656
setup(
4757
name='cmake_example',

test.py

Lines changed: 0 additions & 3 deletions
This file was deleted.

tests/test.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from cmake_example import add
2+
3+
assert add(1, 2) == 3

0 commit comments

Comments
 (0)