Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@ docs/_build

# build output (testing)
skbuild/cmake_test_compile/*

*.env
85 changes: 18 additions & 67 deletions projects/hello-pybind11/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,75 +1,26 @@
cmake_minimum_required(VERSION 3.4.0)
cmake_minimum_required(VERSION 3.14...3.17)

project(hello-pybind11)
set(HELLO_VERSION "0.1")
project(hello-pybind11 VERSION "0.1")

option(HELLO_BUILD_TESTING "Enable testing for this project" OFF)
option(HELLO_USE_SYSTEM_PYBIND11 "Use pybind11 installed in the system" OFF)
mark_as_advanced(HELLO_USE_SYSTEM_PYBIND11)
# Define CMAKE_INSTALL_xxx: LIBDIR, INCLUDEDIR
include(GNUInstallDirs)

# To use cmake modules/functions or FindXXX files:
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
include(GNUInstallDirs) # Define CMAKE_INSTALL_xxx: LIBDIR, INCLUDEDIR
set(hello_export_file "${PROJECT_BINARY_DIR}/helloTargets.cmake")
# Fetch pybind11
include(FetchContent)

if(NOT HELLO_USE_SYSTEM_PYBIND11)
# Fetch pybind11
include(FetchContent)
FetchContent_Declare(
pybind11
GIT_REPOSITORY https://github.com/pybind/pybind11
GIT_TAG v2.3
)
FetchContent_GetProperties(pybind11)
if(NOT pybind11_POPULATED)
FetchContent_Populate(pybind11)
add_subdirectory(${pybind11_SOURCE_DIR} ${pybind11_BINARY_DIR})
endif()
else()
find_package(pybind11 REQUIRED)
endif()

if(HELLO_BUILD_TESTING)
enable_testing()
endif()

add_subdirectory(src)
add_subdirectory(wrap)

# Install/package
set(install_cmake_dir "${CMAKE_INSTALL_LIBDIR}/cmake/hello")
install (EXPORT helloTargets
NAMESPACE hello::
DESTINATION ${install_cmake_dir} )
FetchContent_Declare(
pybind11
GIT_REPOSITORY https://github.com/pybind/pybind11
GIT_TAG v2.5.0
)
FetchContent_MakeAvailable(pybind11)

install(FILES ${CMAKE_CURRENT_BINARY_DIR}/cmake/helloConfig.cmake
${CMAKE_CURRENT_BINARY_DIR}/helloConfigVersion.cmake
DESTINATION ${install_cmake_dir} )

include(CMakePackageConfigHelpers)

write_basic_package_version_file(helloConfigVersion.cmake
VERSION ${HELLO_VERSION}
COMPATIBILITY SameMajorVersion)

# Build tree
set(HELLO_TARGETS_FILE ${hello_export_file})
configure_package_config_file(
${CMAKE_CURRENT_SOURCE_DIR}/cmake/helloConfig.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/helloConfig.cmake
INSTALL_DESTINATION ${install_cmake_dir}
PATH_VARS HELLO_TARGETS_FILE
NO_CHECK_REQUIRED_COMPONENTS_MACRO # hello does not provide components
INSTALL_PREFIX ${CMAKE_CURRENT_BINARY_DIR}
set(python_module_name _hello)
pybind11_add_module(${python_module_name} MODULE
src/hello/hello_py.cpp
)

# Install tree
set(HELLO_TARGETS_FILE ${CMAKE_INSTALL_PREFIX}/${install_cmake_dir}/helloTargets.cmake)
configure_package_config_file(
${CMAKE_CURRENT_SOURCE_DIR}/cmake/helloConfig.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/cmake/helloConfig.cmake
INSTALL_DESTINATION ${install_cmake_dir}
PATH_VARS HELLO_TARGETS_FILE
NO_CHECK_REQUIRED_COMPONENTS_MACRO # hello does not provide components
)
install(TARGETS ${python_module_name} DESTINATION .)

# Quiet a warning, since this project is only valid with SKBUILD
set(ignoreMe "${SKBUILD}")
25 changes: 25 additions & 0 deletions projects/hello-pybind11/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# PyBind11 + Scikit Build example


## Building

To build, you must have pip 10 or greater, *or* you need to manually install
`scikit-build` and `cmake`. Once you create a wheel, that wheel can be used in
earlier versions of pip.

Example build and install sequence:

```bash
pip install .
python -c "import hello; hello.hello()"
```

This should print "Hello, World!".

## Testing

Testing is managed by tox. This will build the package in a temp directory and runs the tests in the test dir.

```shell
tox
```
8 changes: 0 additions & 8 deletions projects/hello-pybind11/cmake/helloConfig.cmake.in

This file was deleted.

18 changes: 0 additions & 18 deletions projects/hello-pybind11/include/hello.h

This file was deleted.

3 changes: 3 additions & 0 deletions projects/hello-pybind11/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["setuptools", "wheel", "scikit-build", "cmake", "ninja"]
build-backend = "setuptools.build_meta"
11 changes: 5 additions & 6 deletions projects/hello-pybind11/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
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)
print('Please update pip, you need pip 10 or greater,\n'
' or you need to install the PEP 518 requirements in pyproject.toml yourself', file=sys.stderr)
raise

setup(
name="hello-pybind11",
Expand All @@ -16,5 +14,6 @@
author='Pablo Hernandez-Cerdan',
license="MIT",
packages=['hello'],
#cmake=['-DHELLO_BUILD_TESTING:BOOL=TRUE',]
package_dir={'': 'src'},
cmake_install_dir='src/hello'
)
31 changes: 0 additions & 31 deletions projects/hello-pybind11/src/CMakeLists.txt

This file was deleted.

11 changes: 0 additions & 11 deletions projects/hello-pybind11/src/hello.cpp

This file was deleted.

1 change: 1 addition & 0 deletions projects/hello-pybind11/src/hello/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from ._hello import hello, return_two
18 changes: 18 additions & 0 deletions projects/hello-pybind11/src/hello/hello_py.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <iostream>
#include <pybind11/pybind11.h>

namespace py = pybind11;

void hello() {
std::cout << "Hello, World!" << std::endl;
}

int return_two() {
return 2;
}

PYBIND11_MODULE(_hello, m) {
m.doc() = "_hello";
m.def("hello", &hello, "Prints \"Hello, World!\"");
m.def("return_two", &return_two, "Returns 2");
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This kind of import is automatically done when importing hello from outside
import _hello as hello
import hello
import unittest

class TestHello(unittest.TestCase):
Expand Down
2 changes: 2 additions & 0 deletions projects/hello-pybind11/tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[testenv]
commands = python -m unittest discover -s tests/
38 changes: 0 additions & 38 deletions projects/hello-pybind11/wrap/CMakeLists.txt

This file was deleted.

4 changes: 0 additions & 4 deletions projects/hello-pybind11/wrap/__init__.py

This file was deleted.

13 changes: 0 additions & 13 deletions projects/hello-pybind11/wrap/hello_init_py.cpp

This file was deleted.

10 changes: 0 additions & 10 deletions projects/hello-pybind11/wrap/hello_py.cpp

This file was deleted.