Skip to content

Commit 0bb755c

Browse files
committed
test installed pybind
1 parent 9b8bc85 commit 0bb755c

File tree

9 files changed

+110
-0
lines changed

9 files changed

+110
-0
lines changed

.appveyor.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ build_script:
3030
- cmake -A "%CMAKE_ARCH%" -DPYBIND11_WERROR=ON
3131
- set MSBuildLogger="C:\Program Files\AppVeyor\BuildAgent\Appveyor.MSBuildLogger.dll"
3232
- cmake --build . --config Release --target pytest -- /v:m /logger:%MSBuildLogger%
33+
- cmake --build . --config Release --target test_install -- /v:m /logger:%MSBuildLogger%

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,5 +110,6 @@ script:
110110
-DPYBIND11_CPP_STANDARD=$CPP
111111
-DPYBIND11_WERROR=ON
112112
- $SCRIPT_RUN_PREFIX make pytest -j 2
113+
- $SCRIPT_RUN_PREFIX make test_install
113114
after_script:
114115
- if [ -n "$DOCKER" ]; then docker stop "$containerid"; docker rm "$containerid"; fi

tests/CMakeLists.txt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,54 @@ if(PYBIND11_TEST_OVERRIDE)
105105
COMMAND ${CMAKE_COMMAND} -E echo "Note: not all tests run: -DPYBIND11_TEST_OVERRIDE is in effect")
106106
endif()
107107

108+
# test use of installation
109+
if(PYBIND11_INSTALL)
110+
# 2.8.12 needed for test_installed_module
111+
# 3.0 needed for interface library for test_installed_target
112+
# 3.1 needed for cmake -E env for testing
113+
if(NOT CMAKE_VERSION VERSION_LESS 3.1)
114+
add_custom_target(test_installed_target
115+
COMMAND ${CMAKE_COMMAND}
116+
"-DCMAKE_INSTALL_PREFIX=${PROJECT_BINARY_DIR}/test_install"
117+
-P "${PROJECT_BINARY_DIR}/cmake_install.cmake"
118+
COMMAND ${CMAKE_CTEST_COMMAND}
119+
--build-and-test "${CMAKE_CURRENT_SOURCE_DIR}/test_installed_target"
120+
"${CMAKE_CURRENT_BINARY_DIR}/test_installed_target"
121+
--build-noclean
122+
--build-generator ${CMAKE_GENERATOR}
123+
$<$<BOOL:${CMAKE_GENERATOR_PLATFORM}>:--build-generator-platform> ${CMAKE_GENERATOR_PLATFORM}
124+
--build-makeprogram ${CMAKE_MAKE_PROGRAM}
125+
--build-target check
126+
--build-options "-DCMAKE_PREFIX_PATH=${PROJECT_BINARY_DIR}/test_install"
127+
"-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
128+
"-DPYTHON_EXECUTABLE=${PYTHON_EXECUTABLE}"
129+
"-DPYBIND11_CPP_STANDARD=${PYBIND11_CPP_STANDARD}"
130+
)
131+
add_custom_target(test_installed_module
132+
COMMAND ${CMAKE_COMMAND}
133+
"-DCMAKE_INSTALL_PREFIX=${PROJECT_BINARY_DIR}/test_install"
134+
-P "${PROJECT_BINARY_DIR}/cmake_install.cmake"
135+
COMMAND ${CMAKE_CTEST_COMMAND}
136+
--build-and-test "${CMAKE_CURRENT_SOURCE_DIR}/test_installed_module"
137+
"${CMAKE_CURRENT_BINARY_DIR}/test_installed_module"
138+
--build-noclean
139+
--build-generator ${CMAKE_GENERATOR}
140+
$<$<BOOL:${CMAKE_GENERATOR_PLATFORM}>:--build-generator-platform> ${CMAKE_GENERATOR_PLATFORM}
141+
--build-makeprogram ${CMAKE_MAKE_PROGRAM}
142+
--build-target check
143+
--build-options "-DCMAKE_PREFIX_PATH=${PROJECT_BINARY_DIR}/test_install"
144+
"-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
145+
"-DPYTHON_EXECUTABLE=${PYTHON_EXECUTABLE}"
146+
"-DPYBIND11_CPP_STANDARD=${PYBIND11_CPP_STANDARD}"
147+
)
148+
else()
149+
add_custom_target(test_installed_target)
150+
add_custom_target(test_installed_module)
151+
endif()
152+
add_custom_target(test_install)
153+
add_dependencies(test_install test_installed_target test_installed_module)
154+
endif()
155+
108156
# And another to show the .so size and, if a previous size, compare it:
109157
add_custom_command(TARGET pybind11_tests POST_BUILD
110158
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_SOURCE_DIR}/tools/libsize.py
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
cmake_minimum_required(VERSION 2.8.12)
2+
project(test_installed_module CXX)
3+
4+
set(CMAKE_MODULE_PATH "")
5+
6+
find_package(pybind11 CONFIG REQUIRED)
7+
8+
message(STATUS "Found pybind11: ${pybind11_INCLUDE_DIRS} (found version ${pybind11_VERSION})")
9+
message(STATUS "Found Python: ${PYTHON_INCLUDE_DIRS} (found version ${PYTHON_VERSION_STRING})")
10+
11+
pybind11_add_module(test_installed_module SHARED main.cpp)
12+
13+
add_custom_target(check ${CMAKE_COMMAND} -E env PYTHONPATH=$<TARGET_FILE_DIR:test_installed_module>
14+
${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/test.py)

tests/test_installed_module/main.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <pybind11/pybind11.h>
2+
namespace py = pybind11;
3+
4+
PYBIND11_PLUGIN(test_installed_module) {
5+
py::module m("test_installed_module");
6+
7+
m.def("add", [](int i, int j) { return i + j; });
8+
9+
return m.ptr();
10+
}

tests/test_installed_module/test.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import test_installed_module
2+
assert test_installed_module.add(11, 22) == 33
3+
print('test_installed_module imports, runs, and adds: 11 + 22 = 33')
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
cmake_minimum_required(VERSION 3.0)
2+
project(test_installed_target CXX)
3+
4+
set(CMAKE_MODULE_PATH "")
5+
6+
find_package(pybind11 CONFIG REQUIRED)
7+
8+
message(STATUS "Found pybind11: ${pybind11_INCLUDE_DIRS} (found version ${pybind11_VERSION})")
9+
message(STATUS "Found Python: ${PYTHON_INCLUDE_DIRS} (found version ${PYTHON_VERSION_STRING})")
10+
11+
add_library(test_installed_target MODULE main.cpp)
12+
13+
target_link_libraries(test_installed_target PRIVATE pybind11::pybind11)
14+
15+
# make sure result is, for example, test_installed_target.so, not libtest_installed_target.dylib
16+
set_target_properties(test_installed_target PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX}"
17+
SUFFIX "${PYTHON_MODULE_EXTENSION}")
18+
19+
add_custom_target(check ${CMAKE_COMMAND} -E env PYTHONPATH=$<TARGET_FILE_DIR:test_installed_target>
20+
${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/test.py)

tests/test_installed_target/main.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <pybind11/pybind11.h>
2+
namespace py = pybind11;
3+
4+
PYBIND11_PLUGIN(test_installed_target) {
5+
py::module m("test_installed_target");
6+
7+
m.def("add", [](int i, int j) { return i + j; });
8+
9+
return m.ptr();
10+
}

tests/test_installed_target/test.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import test_installed_target
2+
assert test_installed_target.add(1, 2) == 3
3+
print('test_installed_target imports, runs, and adds: 1 + 2 = 3')

0 commit comments

Comments
 (0)