1# Built-in in CMake 3.5+ 2include(CMakeParseArguments) 3 4add_custom_target(test_cmake_build) 5 6function(pybind11_add_build_test name) 7 cmake_parse_arguments(ARG "INSTALL" "" "" ${ARGN}) 8 9 set(build_options "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}") 10 11 if(PYBIND11_FINDPYTHON) 12 list(APPEND build_options "-DPYBIND11_FINDPYTHON=${PYBIND11_FINDPYTHON}") 13 14 if(DEFINED Python_ROOT_DIR) 15 list(APPEND build_options "-DPython_ROOT_DIR=${Python_ROOT_DIR}") 16 endif() 17 18 list(APPEND build_options "-DPython_EXECUTABLE=${Python_EXECUTABLE}") 19 else() 20 list(APPEND build_options "-DPYTHON_EXECUTABLE=${PYTHON_EXECUTABLE}") 21 endif() 22 23 if(DEFINED CMAKE_CXX_STANDARD) 24 list(APPEND build_options "-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}") 25 endif() 26 27 if(NOT ARG_INSTALL) 28 list(APPEND build_options "-Dpybind11_SOURCE_DIR=${pybind11_SOURCE_DIR}") 29 else() 30 list(APPEND build_options "-DCMAKE_PREFIX_PATH=${pybind11_BINARY_DIR}/mock_install") 31 endif() 32 33 add_custom_target( 34 test_build_${name} 35 ${CMAKE_CTEST_COMMAND} 36 --build-and-test 37 "${CMAKE_CURRENT_SOURCE_DIR}/${name}" 38 "${CMAKE_CURRENT_BINARY_DIR}/${name}" 39 --build-config 40 Release 41 --build-noclean 42 --build-generator 43 ${CMAKE_GENERATOR} 44 $<$<BOOL:${CMAKE_GENERATOR_PLATFORM}>:--build-generator-platform> 45 ${CMAKE_GENERATOR_PLATFORM} 46 --build-makeprogram 47 ${CMAKE_MAKE_PROGRAM} 48 --build-target 49 check_${name} 50 --build-options 51 ${build_options}) 52 if(ARG_INSTALL) 53 add_dependencies(test_build_${name} mock_install) 54 endif() 55 add_dependencies(test_cmake_build test_build_${name}) 56endfunction() 57 58possibly_uninitialized(PYTHON_MODULE_EXTENSION Python_INTERPRETER_ID) 59 60pybind11_add_build_test(subdirectory_function) 61pybind11_add_build_test(subdirectory_target) 62if("${PYTHON_MODULE_EXTENSION}" MATCHES "pypy" OR "${Python_INTERPRETER_ID}" STREQUAL "PyPy") 63 message(STATUS "Skipping embed test on PyPy") 64else() 65 pybind11_add_build_test(subdirectory_embed) 66endif() 67 68if(PYBIND11_INSTALL) 69 add_custom_target( 70 mock_install ${CMAKE_COMMAND} "-DCMAKE_INSTALL_PREFIX=${pybind11_BINARY_DIR}/mock_install" -P 71 "${pybind11_BINARY_DIR}/cmake_install.cmake") 72 73 pybind11_add_build_test(installed_function INSTALL) 74 pybind11_add_build_test(installed_target INSTALL) 75 if(NOT ("${PYTHON_MODULE_EXTENSION}" MATCHES "pypy" OR "${Python_INTERPRETER_ID}" STREQUAL "PyPy" 76 )) 77 pybind11_add_build_test(installed_embed INSTALL) 78 endif() 79endif() 80 81add_dependencies(check test_cmake_build) 82 83add_subdirectory(subdirectory_target EXCLUDE_FROM_ALL) 84add_subdirectory(subdirectory_embed EXCLUDE_FROM_ALL) 85