diff --git a/.appveyor.yml b/.appveyor.yml index c65785827..740bd5c62 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -30,3 +30,4 @@ build_script: - cmake -A "%CMAKE_ARCH%" -DPYBIND11_WERROR=ON - set MSBuildLogger="C:\Program Files\AppVeyor\BuildAgent\Appveyor.MSBuildLogger.dll" - cmake --build . --config Release --target pytest -- /v:m /logger:%MSBuildLogger% +- cmake --build . --config Release --target test_install -- /v:m /logger:%MSBuildLogger% diff --git a/.travis.yml b/.travis.yml index 39b436248..28d3e840b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -110,5 +110,6 @@ script: -DPYBIND11_CPP_STANDARD=$CPP -DPYBIND11_WERROR=ON - $SCRIPT_RUN_PREFIX make pytest -j 2 +- $SCRIPT_RUN_PREFIX make test_install after_script: - if [ -n "$DOCKER" ]; then docker stop "$containerid"; docker rm "$containerid"; fi diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index cb87fd812..27cb65291 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -105,6 +105,54 @@ if(PYBIND11_TEST_OVERRIDE) COMMAND ${CMAKE_COMMAND} -E echo "Note: not all tests run: -DPYBIND11_TEST_OVERRIDE is in effect") endif() +# test use of installation +if(PYBIND11_INSTALL) + # 2.8.12 needed for test_installed_module + # 3.0 needed for interface library for test_installed_target + # 3.1 needed for cmake -E env for testing + if(NOT CMAKE_VERSION VERSION_LESS 3.1) + add_custom_target(test_installed_target + COMMAND ${CMAKE_COMMAND} + "-DCMAKE_INSTALL_PREFIX=${PROJECT_BINARY_DIR}/test_install" + -P "${PROJECT_BINARY_DIR}/cmake_install.cmake" + COMMAND ${CMAKE_CTEST_COMMAND} + --build-and-test "${CMAKE_CURRENT_SOURCE_DIR}/test_installed_target" + "${CMAKE_CURRENT_BINARY_DIR}/test_installed_target" + --build-noclean + --build-generator ${CMAKE_GENERATOR} + $<$:--build-generator-platform> ${CMAKE_GENERATOR_PLATFORM} + --build-makeprogram ${CMAKE_MAKE_PROGRAM} + --build-target check + --build-options "-DCMAKE_PREFIX_PATH=${PROJECT_BINARY_DIR}/test_install" + "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}" + "-DPYTHON_EXECUTABLE=${PYTHON_EXECUTABLE}" + "-DPYBIND11_CPP_STANDARD=${PYBIND11_CPP_STANDARD}" + ) + add_custom_target(test_installed_module + COMMAND ${CMAKE_COMMAND} + "-DCMAKE_INSTALL_PREFIX=${PROJECT_BINARY_DIR}/test_install" + -P "${PROJECT_BINARY_DIR}/cmake_install.cmake" + COMMAND ${CMAKE_CTEST_COMMAND} + --build-and-test "${CMAKE_CURRENT_SOURCE_DIR}/test_installed_module" + "${CMAKE_CURRENT_BINARY_DIR}/test_installed_module" + --build-noclean + --build-generator ${CMAKE_GENERATOR} + $<$:--build-generator-platform> ${CMAKE_GENERATOR_PLATFORM} + --build-makeprogram ${CMAKE_MAKE_PROGRAM} + --build-target check + --build-options "-DCMAKE_PREFIX_PATH=${PROJECT_BINARY_DIR}/test_install" + "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}" + "-DPYTHON_EXECUTABLE=${PYTHON_EXECUTABLE}" + "-DPYBIND11_CPP_STANDARD=${PYBIND11_CPP_STANDARD}" + ) + else() + add_custom_target(test_installed_target) + add_custom_target(test_installed_module) + endif() + add_custom_target(test_install) + add_dependencies(test_install test_installed_target test_installed_module) +endif() + # And another to show the .so size and, if a previous size, compare it: add_custom_command(TARGET pybind11_tests POST_BUILD COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_SOURCE_DIR}/tools/libsize.py diff --git a/tests/test_installed_module/CMakeLists.txt b/tests/test_installed_module/CMakeLists.txt new file mode 100644 index 000000000..77fd49dc2 --- /dev/null +++ b/tests/test_installed_module/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 2.8.12) +project(test_installed_module CXX) + +set(CMAKE_MODULE_PATH "") + +find_package(pybind11 CONFIG REQUIRED) + +message(STATUS "Found pybind11: ${pybind11_INCLUDE_DIRS} (found version ${pybind11_VERSION})") +message(STATUS "Found Python: ${PYTHON_INCLUDE_DIRS} (found version ${PYTHON_VERSION_STRING})") + +pybind11_add_module(test_installed_module SHARED main.cpp) + +add_custom_target(check ${CMAKE_COMMAND} -E env PYTHONPATH=$ + ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/test.py) diff --git a/tests/test_installed_module/main.cpp b/tests/test_installed_module/main.cpp new file mode 100644 index 000000000..a0bda4542 --- /dev/null +++ b/tests/test_installed_module/main.cpp @@ -0,0 +1,10 @@ +#include +namespace py = pybind11; + +PYBIND11_PLUGIN(test_installed_module) { + py::module m("test_installed_module"); + + m.def("add", [](int i, int j) { return i + j; }); + + return m.ptr(); +} diff --git a/tests/test_installed_module/test.py b/tests/test_installed_module/test.py new file mode 100644 index 000000000..2f0632049 --- /dev/null +++ b/tests/test_installed_module/test.py @@ -0,0 +1,3 @@ +import test_installed_module +assert test_installed_module.add(11, 22) == 33 +print('test_installed_module imports, runs, and adds: 11 + 22 = 33') diff --git a/tests/test_installed_target/CMakeLists.txt b/tests/test_installed_target/CMakeLists.txt new file mode 100644 index 000000000..4333dc107 --- /dev/null +++ b/tests/test_installed_target/CMakeLists.txt @@ -0,0 +1,20 @@ +cmake_minimum_required(VERSION 3.0) +project(test_installed_target CXX) + +set(CMAKE_MODULE_PATH "") + +find_package(pybind11 CONFIG REQUIRED) + +message(STATUS "Found pybind11: ${pybind11_INCLUDE_DIRS} (found version ${pybind11_VERSION})") +message(STATUS "Found Python: ${PYTHON_INCLUDE_DIRS} (found version ${PYTHON_VERSION_STRING})") + +add_library(test_installed_target MODULE main.cpp) + +target_link_libraries(test_installed_target PRIVATE pybind11::pybind11) + +# make sure result is, for example, test_installed_target.so, not libtest_installed_target.dylib +set_target_properties(test_installed_target PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX}" + SUFFIX "${PYTHON_MODULE_EXTENSION}") + +add_custom_target(check ${CMAKE_COMMAND} -E env PYTHONPATH=$ + ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/test.py) diff --git a/tests/test_installed_target/main.cpp b/tests/test_installed_target/main.cpp new file mode 100644 index 000000000..2a84c11ce --- /dev/null +++ b/tests/test_installed_target/main.cpp @@ -0,0 +1,10 @@ +#include +namespace py = pybind11; + +PYBIND11_PLUGIN(test_installed_target) { + py::module m("test_installed_target"); + + m.def("add", [](int i, int j) { return i + j; }); + + return m.ptr(); +} diff --git a/tests/test_installed_target/test.py b/tests/test_installed_target/test.py new file mode 100644 index 000000000..b2888a72b --- /dev/null +++ b/tests/test_installed_target/test.py @@ -0,0 +1,3 @@ +import test_installed_target +assert test_installed_target.add(1, 2) == 3 +print('test_installed_target imports, runs, and adds: 1 + 2 = 3')