1possibly_uninitialized(PYTHON_MODULE_EXTENSION Python_INTERPRETER_ID)
2
3if("${PYTHON_MODULE_EXTENSION}" MATCHES "pypy" OR "${Python_INTERPRETER_ID}" STREQUAL "PyPy")
4  message(STATUS "Skipping embed test on PyPy")
5  add_custom_target(cpptest) # Dummy target on PyPy. Embedding is not supported.
6  set(_suppress_unused_variable_warning "${DOWNLOAD_CATCH}")
7  return()
8endif()
9
10find_package(Catch 2.13.2)
11
12if(CATCH_FOUND)
13  message(STATUS "Building interpreter tests using Catch v${CATCH_VERSION}")
14else()
15  message(STATUS "Catch not detected. Interpreter tests will be skipped. Install Catch headers"
16                 " manually or use `cmake -DDOWNLOAD_CATCH=ON` to fetch them automatically.")
17  return()
18endif()
19
20find_package(Threads REQUIRED)
21
22add_executable(test_embed catch.cpp test_interpreter.cpp)
23pybind11_enable_warnings(test_embed)
24
25target_link_libraries(test_embed PRIVATE pybind11::embed Catch2::Catch2 Threads::Threads)
26
27if(NOT CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_CURRENT_BINARY_DIR)
28  file(COPY test_interpreter.py DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
29endif()
30
31add_custom_target(
32  cpptest
33  COMMAND "$<TARGET_FILE:test_embed>"
34  WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
35
36pybind11_add_module(external_module THIN_LTO external_module.cpp)
37set_target_properties(external_module PROPERTIES LIBRARY_OUTPUT_DIRECTORY
38                                                 "${CMAKE_CURRENT_BINARY_DIR}")
39foreach(config ${CMAKE_CONFIGURATION_TYPES})
40  string(TOUPPER ${config} config)
41  set_target_properties(external_module PROPERTIES LIBRARY_OUTPUT_DIRECTORY_${config}
42                                                   "${CMAKE_CURRENT_BINARY_DIR}")
43endforeach()
44add_dependencies(cpptest external_module)
45
46add_dependencies(check cpptest)
47