diff --git a/.gitignore b/.gitignore index f3fe80e..fd41261 100644 --- a/.gitignore +++ b/.gitignore @@ -55,4 +55,4 @@ docs/_build # build output (testing) skbuild/cmake_test_compile/* - +*.env diff --git a/projects/hello-pybind11/CMakeLists.txt b/projects/hello-pybind11/CMakeLists.txt index 15751ec..fa96362 100644 --- a/projects/hello-pybind11/CMakeLists.txt +++ b/projects/hello-pybind11/CMakeLists.txt @@ -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}") diff --git a/projects/hello-pybind11/README.md b/projects/hello-pybind11/README.md new file mode 100644 index 0000000..92e0e01 --- /dev/null +++ b/projects/hello-pybind11/README.md @@ -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 +``` diff --git a/projects/hello-pybind11/cmake/helloConfig.cmake.in b/projects/hello-pybind11/cmake/helloConfig.cmake.in deleted file mode 100644 index eb3d87d..0000000 --- a/projects/hello-pybind11/cmake/helloConfig.cmake.in +++ /dev/null @@ -1,8 +0,0 @@ -@PACKAGE_INIT@ - -include(CMakeFindDependencyMacro) - -get_filename_component(HELLO_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH) -if(NOT TARGET hellocore) - include ("${HELLO_CMAKE_DIR}/helloTargets.cmake") -endif() diff --git a/projects/hello-pybind11/include/hello.h b/projects/hello-pybind11/include/hello.h deleted file mode 100644 index 8778b48..0000000 --- a/projects/hello-pybind11/include/hello.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef HELLO_H_ -#define HELLO_H_ - - -namespace hello { -/** - * Say "Hello, World!" - * - */ -void hello(); -/** - * return the number 2 - * - * @return 2 - */ -int return_two(); -} // end ns hello -#endif diff --git a/projects/hello-pybind11/pyproject.toml b/projects/hello-pybind11/pyproject.toml new file mode 100644 index 0000000..0feeb0a --- /dev/null +++ b/projects/hello-pybind11/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["setuptools", "wheel", "scikit-build", "cmake", "ninja"] +build-backend = "setuptools.build_meta" diff --git a/projects/hello-pybind11/setup.py b/projects/hello-pybind11/setup.py index 7fcfe05..d936324 100644 --- a/projects/hello-pybind11/setup.py +++ b/projects/hello-pybind11/setup.py @@ -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", @@ -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' ) diff --git a/projects/hello-pybind11/src/CMakeLists.txt b/projects/hello-pybind11/src/CMakeLists.txt deleted file mode 100644 index 7979e8b..0000000 --- a/projects/hello-pybind11/src/CMakeLists.txt +++ /dev/null @@ -1,31 +0,0 @@ -set(hello_sources - hello.cpp - ) - -add_library(hellocore ${hello_sources} ) -target_include_directories(hellocore PUBLIC - $ - $ - ) - -# Required for pybind11 when building STATIC libraries. -# It's ON by default with SHARED or MODULE libraries. -set_property(TARGET hellocore PROPERTY POSITION_INDEPENDENT_CODE ON) - - -# install targets -install(TARGETS hellocore - EXPORT helloTargets - RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} - LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} - ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} - ) - -install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/ - DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/hello - PATTERN "*.txt" EXCLUDE) - -# export to the build tree -export( TARGETS hellocore - NAMESPACE hello:: - APPEND FILE ${hello_export_file}) diff --git a/projects/hello-pybind11/src/hello.cpp b/projects/hello-pybind11/src/hello.cpp deleted file mode 100644 index cb5f318..0000000 --- a/projects/hello-pybind11/src/hello.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include "hello.h" -#include - -namespace hello { -void hello() { - std::cout << "Hello, World!" << std::endl; -} -int return_two() { - return 2; -} -} // end ns hello diff --git a/projects/hello-pybind11/src/hello/__init__.py b/projects/hello-pybind11/src/hello/__init__.py new file mode 100644 index 0000000..2bd8e0f --- /dev/null +++ b/projects/hello-pybind11/src/hello/__init__.py @@ -0,0 +1 @@ +from ._hello import hello, return_two diff --git a/projects/hello-pybind11/src/hello/hello_py.cpp b/projects/hello-pybind11/src/hello/hello_py.cpp new file mode 100644 index 0000000..75253c5 --- /dev/null +++ b/projects/hello-pybind11/src/hello/hello_py.cpp @@ -0,0 +1,18 @@ +#include +#include + +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"); +} diff --git a/projects/hello-pybind11/wrap/test_python/test_hello_pybind11.py b/projects/hello-pybind11/tests/test_hello_pybind11.py similarity index 94% rename from projects/hello-pybind11/wrap/test_python/test_hello_pybind11.py rename to projects/hello-pybind11/tests/test_hello_pybind11.py index 8584ccd..6a2bb8a 100644 --- a/projects/hello-pybind11/wrap/test_python/test_hello_pybind11.py +++ b/projects/hello-pybind11/tests/test_hello_pybind11.py @@ -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): diff --git a/projects/hello-pybind11/tox.ini b/projects/hello-pybind11/tox.ini new file mode 100644 index 0000000..311cae8 --- /dev/null +++ b/projects/hello-pybind11/tox.ini @@ -0,0 +1,2 @@ +[testenv] +commands = python -m unittest discover -s tests/ diff --git a/projects/hello-pybind11/wrap/CMakeLists.txt b/projects/hello-pybind11/wrap/CMakeLists.txt deleted file mode 100644 index 00178f9..0000000 --- a/projects/hello-pybind11/wrap/CMakeLists.txt +++ /dev/null @@ -1,38 +0,0 @@ -# skbuild cmake macros not needed -# We'll use pybind11 functions to create and link libraries -# find_package(PythonExtensions REQUIRED) - -set(python_module_name _hello) -pybind11_add_module(${python_module_name} MODULE - hello_init_py.cpp - hello_py.cpp - ) -target_link_libraries(${python_module_name} PRIVATE hellocore) - -install(TARGETS ${python_module_name} DESTINATION hello) - -# build tree -file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/__init__.py - DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) -# install tree -install(FILES __init__.py DESTINATION hello) - -# tests -if(HELLO_BUILD_TESTING) - set(test_folder "${CMAKE_CURRENT_SOURCE_DIR}/test_python") - set(python_tests_ - test_hello_pybind11.py - ) - - # test files should start with "test_" - # unittest functions (in .py) should start with "test_" for discover to work - foreach(python_test ${python_tests_}) - add_test(NAME python||${python_module_name}||${python_test} - COMMAND ${PYTHON_EXECUTABLE} - -m unittest discover - -s ${test_folder} - -p ${python_test} - ) - endforeach() -endif() - diff --git a/projects/hello-pybind11/wrap/__init__.py b/projects/hello-pybind11/wrap/__init__.py deleted file mode 100644 index e9c1ca0..0000000 --- a/projects/hello-pybind11/wrap/__init__.py +++ /dev/null @@ -1,4 +0,0 @@ -# from ._hello import hello -# from ._hello import return_two -# OR: -import ._hello as hello diff --git a/projects/hello-pybind11/wrap/hello_init_py.cpp b/projects/hello-pybind11/wrap/hello_init_py.cpp deleted file mode 100644 index df543de..0000000 --- a/projects/hello-pybind11/wrap/hello_init_py.cpp +++ /dev/null @@ -1,13 +0,0 @@ -/** This file defines the module, the implementation can be - * splitted in different cpp files - */ -#include -namespace py = pybind11; - -void init_hello(py::module &); -// void init_a_new_submodule_in_other_cpp_file(py::module &) - -PYBIND11_MODULE(_hello, m) { - m.doc() = "_hello"; // optional module docstring - init_hello(m); -} diff --git a/projects/hello-pybind11/wrap/hello_py.cpp b/projects/hello-pybind11/wrap/hello_py.cpp deleted file mode 100644 index 3fb12ee..0000000 --- a/projects/hello-pybind11/wrap/hello_py.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include "hello.h" -#include - -namespace py = pybind11; -using namespace hello; - -void init_hello(py::module &m) { - m.def("hello", &hello::hello); - m.def("return_two", &return_two); -}