pybind11/example/CMakeLists.txt
Jason Rhinelander 3f589379ec Improve constructor/destructor tracking
This commit rewrites the examples that look for constructor/destructor
calls to do so via static variable tracking rather than output parsing.

The added ConstructorStats class provides methods to keep track of
constructors and destructors, number of default/copy/move constructors,
and number of copy/move assignments.  It also provides a mechanism for
storing values (e.g. for value construction), and then allows all of
this to be checked at the end of a test by getting the statistics for a
C++ (or python mapping) class.

By not relying on the precise pattern of constructions/destructions,
but rather simply ensuring that every construction is matched with a
destruction on the same object, we ensure that everything that gets
created also gets destroyed as expected.

This replaces all of the various "std::cout << whatever" code in
constructors/destructors with
`print_created(this)`/`print_destroyed(this)`/etc. functions which
provide similar output, but now has a unified format across the
different examples, including a new ### prefix that makes mixed example
output and lifecycle events easier to distinguish.

With this change, relaxed mode is no longer needed, which enables
testing for proper destruction under MSVC, and under any other compiler
that generates code calling extra constructors, or optimizes away any
constructors.  GCC/clang are used as the baseline for move
constructors; the tests are adapted to allow more move constructors to
be evoked (but other types are constructors much have matching counts).

This commit also disables output buffering of tests, as the buffering
sometimes results in C++ output ending up in the middle of python
output (or vice versa), depending on the OS/python version.
2016-08-11 18:16:04 -04:00

69 lines
2.4 KiB
CMake

# Set a default build configuration if none is specified. 'MinSizeRel' produces the smallest binaries
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
message(STATUS "Setting build type to 'MinSizeRel' as none was specified.")
set(CMAKE_BUILD_TYPE MinSizeRel CACHE STRING "Choose the type of build." FORCE)
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release"
"MinSizeRel" "RelWithDebInfo")
endif()
set(PYBIND11_EXAMPLES
example-methods-and-attributes.cpp
example-python-types.cpp
example-operator-overloading.cpp
example-constants-and-functions.cpp
example-callbacks.cpp
example-sequences-and-iterators.cpp
example-buffers.cpp
example-smart-ptr.cpp
example-modules.cpp
example-numpy-vectorize.cpp
example-arg-keywords-and-defaults.cpp
example-virtual-functions.cpp
example-keep-alive.cpp
example-opaque-types.cpp
example-pickling.cpp
example-inheritance.cpp
example-stl-binder-vector.cpp
example-eval.cpp
example-custom-exceptions.cpp
issues.cpp
)
# Check if Eigen is available
find_package(Eigen3 QUIET)
if(EIGEN3_FOUND)
list(APPEND PYBIND11_EXAMPLES eigen.cpp)
message(STATUS "Building Eigen v${EIGEN3_VERSION} testcase")
else()
message(STATUS "NOT Building Eigen testcase")
endif()
# Create the binding library
pybind11_add_module(example example.cpp ${PYBIND11_EXAMPLES})
pybind11_enable_warnings(example)
if(EIGEN3_FOUND)
target_include_directories(example PRIVATE ${EIGEN3_INCLUDE_DIR})
target_compile_definitions(example PRIVATE -DPYBIND11_TEST_EIGEN)
endif()
# Always write the output file directly into the 'example' directory (even on MSVC)
set(CompilerFlags
LIBRARY_OUTPUT_DIRECTORY LIBRARY_OUTPUT_DIRECTORY_RELEASE LIBRARY_OUTPUT_DIRECTORY_DEBUG
LIBRARY_OUTPUT_DIRECTORY_MINSIZEREL LIBRARY_OUTPUT_DIRECTORY_RELWITHDEBINFO
RUNTIME_OUTPUT_DIRECTORY RUNTIME_OUTPUT_DIRECTORY_RELEASE RUNTIME_OUTPUT_DIRECTORY_DEBUG
RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO)
foreach(CompilerFlag ${CompilerFlags})
set_target_properties(example PROPERTIES ${CompilerFlag} ${PROJECT_SOURCE_DIR}/example)
endforeach()
set(RUN_TEST ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/run_test.py)
foreach(VALUE ${PYBIND11_EXAMPLES})
string(REGEX REPLACE "^(.+).cpp$" "\\1" EXAMPLE_NAME "${VALUE}")
add_test(NAME ${EXAMPLE_NAME} COMMAND ${RUN_TEST} ${EXAMPLE_NAME})
endforeach()