From 03bfe0e00ac2319f87e324b92a0f20e73a6c9f60 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Fri, 26 Jun 2020 00:18:08 -0400 Subject: [PATCH 01/12] fix: Working build with static library --- .gitignore | 2 +- projects/hello-pybind11/CMakeLists.txt | 41 ++++++++++--------- projects/hello-pybind11/README.md | 19 +++++++++ projects/hello-pybind11/pyproject.toml | 2 + projects/hello-pybind11/setup.py | 11 +++-- projects/hello-pybind11/src/CMakeLists.txt | 3 ++ .../{wrap => src/hello}/CMakeLists.txt | 10 +---- projects/hello-pybind11/src/hello/__init__.py | 1 + .../{wrap => src/hello}/hello_init_py.cpp | 0 .../{wrap => src/hello}/hello_py.cpp | 0 .../hello}/test_python/test_hello_pybind11.py | 0 projects/hello-pybind11/wrap/__init__.py | 4 -- 12 files changed, 55 insertions(+), 38 deletions(-) create mode 100644 projects/hello-pybind11/README.md create mode 100644 projects/hello-pybind11/pyproject.toml rename projects/hello-pybind11/{wrap => src/hello}/CMakeLists.txt (76%) create mode 100644 projects/hello-pybind11/src/hello/__init__.py rename projects/hello-pybind11/{wrap => src/hello}/hello_init_py.cpp (100%) rename projects/hello-pybind11/{wrap => src/hello}/hello_py.cpp (100%) rename projects/hello-pybind11/{wrap => src/hello}/test_python/test_hello_pybind11.py (100%) delete mode 100644 projects/hello-pybind11/wrap/__init__.py 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..89670ef 100644 --- a/projects/hello-pybind11/CMakeLists.txt +++ b/projects/hello-pybind11/CMakeLists.txt @@ -1,42 +1,45 @@ -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) +include(CTest) +option(BUILD_TESTING "Build the tests too" OFF) # defined above, but on by default 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") +# CMake files not included if installed via pyproject.toml, so manually doing it here. if(NOT HELLO_USE_SYSTEM_PYBIND11) # Fetch pybind11 include(FetchContent) + FetchContent_Declare( pybind11 GIT_REPOSITORY https://github.com/pybind/pybind11 - GIT_TAG v2.3 + GIT_TAG v2.5.0 ) - FetchContent_GetProperties(pybind11) - if(NOT pybind11_POPULATED) - FetchContent_Populate(pybind11) - add_subdirectory(${pybind11_SOURCE_DIR} ${pybind11_BINARY_DIR}) - endif() + FetchContent_MakeAvailable(pybind11) + else() - find_package(pybind11 REQUIRED) + find_package(pybind11 REQUIRED) endif() -if(HELLO_BUILD_TESTING) - enable_testing() -endif() add_subdirectory(src) -add_subdirectory(wrap) -# Install/package +# Quiet a warning, since this project is only valid with SKBUILD +set(ignoreMe "${SKBUILD}") + +# Everything below here is optional +# This installs CMake config tools +# And also a version file - which is not used by Python. + + set(install_cmake_dir "${CMAKE_INSTALL_LIBDIR}/cmake/hello") install (EXPORT helloTargets NAMESPACE hello:: @@ -49,7 +52,7 @@ install(FILES ${CMAKE_CURRENT_BINARY_DIR}/cmake/helloConfig.cmake include(CMakePackageConfigHelpers) write_basic_package_version_file(helloConfigVersion.cmake - VERSION ${HELLO_VERSION} + VERSION ${PACKAGE_VERSION} COMPATIBILITY SameMajorVersion) # Build tree diff --git a/projects/hello-pybind11/README.md b/projects/hello-pybind11/README.md new file mode 100644 index 0000000..f92204a --- /dev/null +++ b/projects/hello-pybind11/README.md @@ -0,0 +1,19 @@ +# 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 +python3 -m venv .env +. .env/bin/activate +pip install . +python -c "import hello; hello.hello()" +``` + +This should print "Hello, World!". diff --git a/projects/hello-pybind11/pyproject.toml b/projects/hello-pybind11/pyproject.toml new file mode 100644 index 0000000..7533528 --- /dev/null +++ b/projects/hello-pybind11/pyproject.toml @@ -0,0 +1,2 @@ +[build-system] +requires = ["setuptools", "wheel", "scikit-build", "cmake", "ninja"] diff --git a/projects/hello-pybind11/setup.py b/projects/hello-pybind11/setup.py index 7fcfe05..082e9ce 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=['-DBUILD_TESTING:BOOL=TRUE',] ) diff --git a/projects/hello-pybind11/src/CMakeLists.txt b/projects/hello-pybind11/src/CMakeLists.txt index 7979e8b..30b80b6 100644 --- a/projects/hello-pybind11/src/CMakeLists.txt +++ b/projects/hello-pybind11/src/CMakeLists.txt @@ -29,3 +29,6 @@ install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/ export( TARGETS hellocore NAMESPACE hello:: APPEND FILE ${hello_export_file}) + +# Add the wrapper +add_subdirectory(hello) diff --git a/projects/hello-pybind11/wrap/CMakeLists.txt b/projects/hello-pybind11/src/hello/CMakeLists.txt similarity index 76% rename from projects/hello-pybind11/wrap/CMakeLists.txt rename to projects/hello-pybind11/src/hello/CMakeLists.txt index 00178f9..19379f0 100644 --- a/projects/hello-pybind11/wrap/CMakeLists.txt +++ b/projects/hello-pybind11/src/hello/CMakeLists.txt @@ -9,16 +9,10 @@ pybind11_add_module(${python_module_name} MODULE ) 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) +install(TARGETS ${python_module_name} DESTINATION src/hello) # tests -if(HELLO_BUILD_TESTING) +if(BUILD_TESTING) set(test_folder "${CMAKE_CURRENT_SOURCE_DIR}/test_python") set(python_tests_ test_hello_pybind11.py 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/wrap/hello_init_py.cpp b/projects/hello-pybind11/src/hello/hello_init_py.cpp similarity index 100% rename from projects/hello-pybind11/wrap/hello_init_py.cpp rename to projects/hello-pybind11/src/hello/hello_init_py.cpp diff --git a/projects/hello-pybind11/wrap/hello_py.cpp b/projects/hello-pybind11/src/hello/hello_py.cpp similarity index 100% rename from projects/hello-pybind11/wrap/hello_py.cpp rename to projects/hello-pybind11/src/hello/hello_py.cpp diff --git a/projects/hello-pybind11/wrap/test_python/test_hello_pybind11.py b/projects/hello-pybind11/src/hello/test_python/test_hello_pybind11.py similarity index 100% rename from projects/hello-pybind11/wrap/test_python/test_hello_pybind11.py rename to projects/hello-pybind11/src/hello/test_python/test_hello_pybind11.py 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 From 00c8c84fc157399d0bc8f6f9ed081ad925e28db8 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Fri, 26 Jun 2020 07:40:06 -0500 Subject: [PATCH 02/12] Support build pybind11 example with SHARED library --- projects/hello-pybind11/src/CMakeLists.txt | 8 ++++---- projects/hello-pybind11/src/hello/CMakeLists.txt | 5 +++++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/projects/hello-pybind11/src/CMakeLists.txt b/projects/hello-pybind11/src/CMakeLists.txt index 30b80b6..cc3a195 100644 --- a/projects/hello-pybind11/src/CMakeLists.txt +++ b/projects/hello-pybind11/src/CMakeLists.txt @@ -2,7 +2,7 @@ set(hello_sources hello.cpp ) -add_library(hellocore ${hello_sources} ) +add_library(hellocore SHARED ${hello_sources} ) target_include_directories(hellocore PUBLIC $ $ @@ -16,9 +16,9 @@ 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} + RUNTIME DESTINATION src/hello + LIBRARY DESTINATION src/hello + ARCHIVE DESTINATION src/hello ) install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/ diff --git a/projects/hello-pybind11/src/hello/CMakeLists.txt b/projects/hello-pybind11/src/hello/CMakeLists.txt index 19379f0..ff8161b 100644 --- a/projects/hello-pybind11/src/hello/CMakeLists.txt +++ b/projects/hello-pybind11/src/hello/CMakeLists.txt @@ -8,6 +8,11 @@ pybind11_add_module(${python_module_name} MODULE hello_py.cpp ) target_link_libraries(${python_module_name} PRIVATE hellocore) +if (APPLE) + set_target_properties(${python_module_name} PROPERTIES BUILD_WITH_INSTALL_RPATH TRUE INSTALL_RPATH "@loader_path") +else() + set_target_properties(${python_module_name} PROPERTIES BUILD_RPATH_USE_ORIGIN TRUE) +endif() install(TARGETS ${python_module_name} DESTINATION src/hello) From 133181178e1b7918f790058e7181aecedd72e0e8 Mon Sep 17 00:00:00 2001 From: Kaleb Barrett Date: Sat, 27 Jun 2020 17:06:38 -0500 Subject: [PATCH 03/12] Fix RPATH of pybind11 example on Linux --- projects/hello-pybind11/src/hello/CMakeLists.txt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/projects/hello-pybind11/src/hello/CMakeLists.txt b/projects/hello-pybind11/src/hello/CMakeLists.txt index ff8161b..bcaac25 100644 --- a/projects/hello-pybind11/src/hello/CMakeLists.txt +++ b/projects/hello-pybind11/src/hello/CMakeLists.txt @@ -9,10 +9,12 @@ pybind11_add_module(${python_module_name} MODULE ) target_link_libraries(${python_module_name} PRIVATE hellocore) if (APPLE) - set_target_properties(${python_module_name} PROPERTIES BUILD_WITH_INSTALL_RPATH TRUE INSTALL_RPATH "@loader_path") + set(rpath "@loader_path") else() - set_target_properties(${python_module_name} PROPERTIES BUILD_RPATH_USE_ORIGIN TRUE) + set(rpath "$ORIGIN") endif() +set_target_properties(${python_module_name} PROPERTIES + INSTALL_RPATH ${rpath}) install(TARGETS ${python_module_name} DESTINATION src/hello) From 25a1b92d268c4c57901c89c3b599d393feaddaf0 Mon Sep 17 00:00:00 2001 From: Kaleb Barrett Date: Sat, 27 Jun 2020 17:11:50 -0500 Subject: [PATCH 04/12] Stop exporting targets config files in pybind11 example --- projects/hello-pybind11/CMakeLists.txt | 46 ------------------- .../hello-pybind11/cmake/helloConfig.cmake.in | 8 ---- projects/hello-pybind11/src/CMakeLists.txt | 5 -- 3 files changed, 59 deletions(-) delete mode 100644 projects/hello-pybind11/cmake/helloConfig.cmake.in diff --git a/projects/hello-pybind11/CMakeLists.txt b/projects/hello-pybind11/CMakeLists.txt index 89670ef..ca82403 100644 --- a/projects/hello-pybind11/CMakeLists.txt +++ b/projects/hello-pybind11/CMakeLists.txt @@ -9,10 +9,6 @@ option(HELLO_USE_SYSTEM_PYBIND11 "Use pybind11 installed in the system" OFF) # 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") -set(hello_export_file "${PROJECT_BINARY_DIR}/helloTargets.cmake") - # CMake files not included if installed via pyproject.toml, so manually doing it here. if(NOT HELLO_USE_SYSTEM_PYBIND11) # Fetch pybind11 @@ -34,45 +30,3 @@ add_subdirectory(src) # Quiet a warning, since this project is only valid with SKBUILD set(ignoreMe "${SKBUILD}") - -# Everything below here is optional -# This installs CMake config tools -# And also a version file - which is not used by Python. - - -set(install_cmake_dir "${CMAKE_INSTALL_LIBDIR}/cmake/hello") -install (EXPORT helloTargets - NAMESPACE hello:: - DESTINATION ${install_cmake_dir} ) - -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 ${PACKAGE_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} - ) - -# 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 - ) - 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/src/CMakeLists.txt b/projects/hello-pybind11/src/CMakeLists.txt index cc3a195..4c37194 100644 --- a/projects/hello-pybind11/src/CMakeLists.txt +++ b/projects/hello-pybind11/src/CMakeLists.txt @@ -25,10 +25,5 @@ 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}) - # Add the wrapper add_subdirectory(hello) From 24d22ed1531fba4f9e14dc53611f19a1de4a052e Mon Sep 17 00:00:00 2001 From: Kaleb Barrett Date: Sat, 27 Jun 2020 17:18:50 -0500 Subject: [PATCH 05/12] Stop installing headers in pybind11 example --- projects/hello-pybind11/src/CMakeLists.txt | 4 ---- 1 file changed, 4 deletions(-) diff --git a/projects/hello-pybind11/src/CMakeLists.txt b/projects/hello-pybind11/src/CMakeLists.txt index 4c37194..f54dc1e 100644 --- a/projects/hello-pybind11/src/CMakeLists.txt +++ b/projects/hello-pybind11/src/CMakeLists.txt @@ -21,9 +21,5 @@ install(TARGETS hellocore ARCHIVE DESTINATION src/hello ) -install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/ - DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/hello - PATTERN "*.txt" EXCLUDE) - # Add the wrapper add_subdirectory(hello) From 9059c1585e564137b7b6f35e3f701bee2f4b9f3e Mon Sep 17 00:00:00 2001 From: Kaleb Barrett Date: Sat, 27 Jun 2020 17:31:20 -0500 Subject: [PATCH 06/12] pybind11 example: merge init and body files --- projects/hello-pybind11/src/hello/CMakeLists.txt | 1 - projects/hello-pybind11/src/hello/hello_init_py.cpp | 13 ------------- projects/hello-pybind11/src/hello/hello_py.cpp | 3 ++- 3 files changed, 2 insertions(+), 15 deletions(-) delete mode 100644 projects/hello-pybind11/src/hello/hello_init_py.cpp diff --git a/projects/hello-pybind11/src/hello/CMakeLists.txt b/projects/hello-pybind11/src/hello/CMakeLists.txt index bcaac25..3500077 100644 --- a/projects/hello-pybind11/src/hello/CMakeLists.txt +++ b/projects/hello-pybind11/src/hello/CMakeLists.txt @@ -4,7 +4,6 @@ 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) diff --git a/projects/hello-pybind11/src/hello/hello_init_py.cpp b/projects/hello-pybind11/src/hello/hello_init_py.cpp deleted file mode 100644 index df543de..0000000 --- a/projects/hello-pybind11/src/hello/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/src/hello/hello_py.cpp b/projects/hello-pybind11/src/hello/hello_py.cpp index 3fb12ee..963dead 100644 --- a/projects/hello-pybind11/src/hello/hello_py.cpp +++ b/projects/hello-pybind11/src/hello/hello_py.cpp @@ -4,7 +4,8 @@ namespace py = pybind11; using namespace hello; -void init_hello(py::module &m) { +PYBIND11_MODULE(_hello, m) { + m.doc() = "_hello"; // optional module docstring { m.def("hello", &hello::hello); m.def("return_two", &return_two); } From 2b2edab2a457fb871632d572dd2a90527e6d78c2 Mon Sep 17 00:00:00 2001 From: Kaleb Barrett Date: Sat, 27 Jun 2020 18:48:33 -0500 Subject: [PATCH 07/12] pybind11 example: merge library implementation into pybind sources This is just a little too "extra" for what is supposed to be an example of how to use scikit-build with a pybind11 project. --- projects/hello-pybind11/CMakeLists.txt | 2 +- projects/hello-pybind11/include/hello.h | 18 ------------- projects/hello-pybind11/src/CMakeLists.txt | 25 ------------------- projects/hello-pybind11/src/hello.cpp | 11 -------- .../hello-pybind11/src/hello/CMakeLists.txt | 8 ------ .../hello-pybind11/src/hello/hello_py.cpp | 17 +++++++++---- 6 files changed, 13 insertions(+), 68 deletions(-) delete mode 100644 projects/hello-pybind11/include/hello.h delete mode 100644 projects/hello-pybind11/src/CMakeLists.txt delete mode 100644 projects/hello-pybind11/src/hello.cpp diff --git a/projects/hello-pybind11/CMakeLists.txt b/projects/hello-pybind11/CMakeLists.txt index ca82403..432b622 100644 --- a/projects/hello-pybind11/CMakeLists.txt +++ b/projects/hello-pybind11/CMakeLists.txt @@ -26,7 +26,7 @@ else() endif() -add_subdirectory(src) +add_subdirectory(src/hello) # Quiet a warning, since this project is only valid with SKBUILD set(ignoreMe "${SKBUILD}") 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/src/CMakeLists.txt b/projects/hello-pybind11/src/CMakeLists.txt deleted file mode 100644 index f54dc1e..0000000 --- a/projects/hello-pybind11/src/CMakeLists.txt +++ /dev/null @@ -1,25 +0,0 @@ -set(hello_sources - hello.cpp - ) - -add_library(hellocore SHARED ${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 src/hello - LIBRARY DESTINATION src/hello - ARCHIVE DESTINATION src/hello - ) - -# Add the wrapper -add_subdirectory(hello) 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/CMakeLists.txt b/projects/hello-pybind11/src/hello/CMakeLists.txt index 3500077..9eb87fb 100644 --- a/projects/hello-pybind11/src/hello/CMakeLists.txt +++ b/projects/hello-pybind11/src/hello/CMakeLists.txt @@ -6,14 +6,6 @@ set(python_module_name _hello) pybind11_add_module(${python_module_name} MODULE hello_py.cpp ) -target_link_libraries(${python_module_name} PRIVATE hellocore) -if (APPLE) - set(rpath "@loader_path") -else() - set(rpath "$ORIGIN") -endif() -set_target_properties(${python_module_name} PROPERTIES - INSTALL_RPATH ${rpath}) install(TARGETS ${python_module_name} DESTINATION src/hello) diff --git a/projects/hello-pybind11/src/hello/hello_py.cpp b/projects/hello-pybind11/src/hello/hello_py.cpp index 963dead..75253c5 100644 --- a/projects/hello-pybind11/src/hello/hello_py.cpp +++ b/projects/hello-pybind11/src/hello/hello_py.cpp @@ -1,11 +1,18 @@ -#include "hello.h" +#include #include namespace py = pybind11; -using namespace hello; + +void hello() { + std::cout << "Hello, World!" << std::endl; +} + +int return_two() { + return 2; +} PYBIND11_MODULE(_hello, m) { - m.doc() = "_hello"; // optional module docstring { - m.def("hello", &hello::hello); - m.def("return_two", &return_two); + m.doc() = "_hello"; + m.def("hello", &hello, "Prints \"Hello, World!\""); + m.def("return_two", &return_two, "Returns 2"); } From baca405d27296b9ee58b8d081157646156b5f16c Mon Sep 17 00:00:00 2001 From: Kaleb Barrett Date: Sat, 27 Jun 2020 19:30:22 -0500 Subject: [PATCH 08/12] pybind11 example: remove ctest support This is supposed to be a Python extension module, why are we using ctest? --- projects/hello-pybind11/CMakeLists.txt | 2 -- .../hello-pybind11/src/hello/CMakeLists.txt | 19 ------------------- 2 files changed, 21 deletions(-) diff --git a/projects/hello-pybind11/CMakeLists.txt b/projects/hello-pybind11/CMakeLists.txt index 432b622..4f48b89 100644 --- a/projects/hello-pybind11/CMakeLists.txt +++ b/projects/hello-pybind11/CMakeLists.txt @@ -2,8 +2,6 @@ cmake_minimum_required(VERSION 3.14...3.17) project(hello-pybind11 VERSION "0.1") -include(CTest) -option(BUILD_TESTING "Build the tests too" OFF) # defined above, but on by default option(HELLO_USE_SYSTEM_PYBIND11 "Use pybind11 installed in the system" OFF) # Define CMAKE_INSTALL_xxx: LIBDIR, INCLUDEDIR diff --git a/projects/hello-pybind11/src/hello/CMakeLists.txt b/projects/hello-pybind11/src/hello/CMakeLists.txt index 9eb87fb..35b37c4 100644 --- a/projects/hello-pybind11/src/hello/CMakeLists.txt +++ b/projects/hello-pybind11/src/hello/CMakeLists.txt @@ -9,22 +9,3 @@ pybind11_add_module(${python_module_name} MODULE install(TARGETS ${python_module_name} DESTINATION src/hello) -# tests -if(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() - From 9f57ce32787c13cc96899a5204bd6b537bb48476 Mon Sep 17 00:00:00 2001 From: Kaleb Barrett Date: Sat, 27 Jun 2020 19:59:32 -0500 Subject: [PATCH 09/12] pybind11 example: use tox, move test dir --- projects/hello-pybind11/README.md | 10 ++++++++-- projects/hello-pybind11/pyproject.toml | 1 + .../hello/test_python => tests}/test_hello_pybind11.py | 2 +- projects/hello-pybind11/tox.ini | 2 ++ 4 files changed, 12 insertions(+), 3 deletions(-) rename projects/hello-pybind11/{src/hello/test_python => tests}/test_hello_pybind11.py (94%) create mode 100644 projects/hello-pybind11/tox.ini diff --git a/projects/hello-pybind11/README.md b/projects/hello-pybind11/README.md index f92204a..92e0e01 100644 --- a/projects/hello-pybind11/README.md +++ b/projects/hello-pybind11/README.md @@ -10,10 +10,16 @@ earlier versions of pip. Example build and install sequence: ```bash -python3 -m venv .env -. .env/bin/activate 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/pyproject.toml b/projects/hello-pybind11/pyproject.toml index 7533528..0feeb0a 100644 --- a/projects/hello-pybind11/pyproject.toml +++ b/projects/hello-pybind11/pyproject.toml @@ -1,2 +1,3 @@ [build-system] requires = ["setuptools", "wheel", "scikit-build", "cmake", "ninja"] +build-backend = "setuptools.build_meta" diff --git a/projects/hello-pybind11/src/hello/test_python/test_hello_pybind11.py b/projects/hello-pybind11/tests/test_hello_pybind11.py similarity index 94% rename from projects/hello-pybind11/src/hello/test_python/test_hello_pybind11.py rename to projects/hello-pybind11/tests/test_hello_pybind11.py index 8584ccd..6a2bb8a 100644 --- a/projects/hello-pybind11/src/hello/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/ From b0bb8b92c3f4ad29a33b19cca88f7dcfb7bf535d Mon Sep 17 00:00:00 2001 From: Kaleb Barrett Date: Thu, 2 Jul 2020 12:10:42 -0500 Subject: [PATCH 10/12] pybind11 example: merge CMakeLists --- projects/hello-pybind11/CMakeLists.txt | 6 +++++- projects/hello-pybind11/src/hello/CMakeLists.txt | 11 ----------- 2 files changed, 5 insertions(+), 12 deletions(-) delete mode 100644 projects/hello-pybind11/src/hello/CMakeLists.txt diff --git a/projects/hello-pybind11/CMakeLists.txt b/projects/hello-pybind11/CMakeLists.txt index 4f48b89..1953139 100644 --- a/projects/hello-pybind11/CMakeLists.txt +++ b/projects/hello-pybind11/CMakeLists.txt @@ -23,8 +23,12 @@ else() find_package(pybind11 REQUIRED) endif() +set(python_module_name _hello) +pybind11_add_module(${python_module_name} MODULE + src/hello/hello_py.cpp + ) -add_subdirectory(src/hello) +install(TARGETS ${python_module_name} DESTINATION src/hello) # Quiet a warning, since this project is only valid with SKBUILD set(ignoreMe "${SKBUILD}") diff --git a/projects/hello-pybind11/src/hello/CMakeLists.txt b/projects/hello-pybind11/src/hello/CMakeLists.txt deleted file mode 100644 index 35b37c4..0000000 --- a/projects/hello-pybind11/src/hello/CMakeLists.txt +++ /dev/null @@ -1,11 +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_py.cpp - ) - -install(TARGETS ${python_module_name} DESTINATION src/hello) - From 83eb1ab24f631153030e2c2428b80c9155ef86b6 Mon Sep 17 00:00:00 2001 From: Kaleb Barrett Date: Thu, 2 Jul 2020 12:12:03 -0500 Subject: [PATCH 11/12] pybind11 example: remove USE_SYSTEM_PYBIND11 option Unnecessary for a simple example. --- projects/hello-pybind11/CMakeLists.txt | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/projects/hello-pybind11/CMakeLists.txt b/projects/hello-pybind11/CMakeLists.txt index 1953139..1273714 100644 --- a/projects/hello-pybind11/CMakeLists.txt +++ b/projects/hello-pybind11/CMakeLists.txt @@ -2,26 +2,18 @@ cmake_minimum_required(VERSION 3.14...3.17) project(hello-pybind11 VERSION "0.1") -option(HELLO_USE_SYSTEM_PYBIND11 "Use pybind11 installed in the system" OFF) - # Define CMAKE_INSTALL_xxx: LIBDIR, INCLUDEDIR include(GNUInstallDirs) -# CMake files not included if installed via pyproject.toml, so manually doing it here. -if(NOT HELLO_USE_SYSTEM_PYBIND11) - # Fetch pybind11 - include(FetchContent) - - FetchContent_Declare( - pybind11 - GIT_REPOSITORY https://github.com/pybind/pybind11 - GIT_TAG v2.5.0 - ) - FetchContent_MakeAvailable(pybind11) +# Fetch pybind11 +include(FetchContent) -else() - find_package(pybind11 REQUIRED) -endif() +FetchContent_Declare( + pybind11 + GIT_REPOSITORY https://github.com/pybind/pybind11 + GIT_TAG v2.5.0 +) +FetchContent_MakeAvailable(pybind11) set(python_module_name _hello) pybind11_add_module(${python_module_name} MODULE From a110a8f1e7a27e16668065060b5489ee984ababf Mon Sep 17 00:00:00 2001 From: Kaleb Barrett Date: Thu, 16 Jul 2020 07:59:31 -0500 Subject: [PATCH 12/12] pybind11 example: use cmake_install_dir, which is best practice --- projects/hello-pybind11/CMakeLists.txt | 2 +- projects/hello-pybind11/setup.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/projects/hello-pybind11/CMakeLists.txt b/projects/hello-pybind11/CMakeLists.txt index 1273714..fa96362 100644 --- a/projects/hello-pybind11/CMakeLists.txt +++ b/projects/hello-pybind11/CMakeLists.txt @@ -20,7 +20,7 @@ pybind11_add_module(${python_module_name} MODULE src/hello/hello_py.cpp ) -install(TARGETS ${python_module_name} DESTINATION src/hello) +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/setup.py b/projects/hello-pybind11/setup.py index 082e9ce..d936324 100644 --- a/projects/hello-pybind11/setup.py +++ b/projects/hello-pybind11/setup.py @@ -14,6 +14,6 @@ author='Pablo Hernandez-Cerdan', license="MIT", packages=['hello'], - package_dir={'': 'src'} - #cmake=['-DBUILD_TESTING:BOOL=TRUE',] + package_dir={'': 'src'}, + cmake_install_dir='src/hello' )