mirror of https://github.com/opencv/opencv.git
If both Python 2 and Python 3 are found, then build bindings for both of them during the build process. Currently, one version of Python is detected automatically, and building for the other requires changes the CMake config. The largest chunk of this change generalizes OpenCVDetectPython.cmake to find both a Python 2 and Python 3 version of Python. Secondly, the opencv_python module is split into two modules, opencv_python2 and opencv_python3. Both are built from the same source. but for different versions of Python.pull/2912/head
parent
2d81595ed4
commit
cac1218eef
11 changed files with 440 additions and 228 deletions
@ -1,5 +1,6 @@ |
||||
set(MIN_VER_CMAKE 2.8.7) |
||||
set(MIN_VER_CUDA 4.2) |
||||
set(MIN_VER_PYTHON 2.6) |
||||
set(MIN_VER_PYTHON2 2.6) |
||||
set(MIN_VER_PYTHON3 3.2) |
||||
set(MIN_VER_ZLIB 1.2.3) |
||||
set(MIN_VER_GTK 2.18.0) |
||||
|
@ -0,0 +1,128 @@ |
||||
# This file is included from a subdirectory |
||||
set(PYTHON_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../") |
||||
|
||||
ocv_module_include_directories( |
||||
"${PYTHON_INCLUDE_PATH}" |
||||
${PYTHON_NUMPY_INCLUDE_DIRS} |
||||
"${PYTHON_SOURCE_DIR}/src2" |
||||
) |
||||
|
||||
set(opencv_hdrs |
||||
"${OPENCV_MODULE_opencv_core_LOCATION}/include/opencv2/core.hpp" |
||||
"${OPENCV_MODULE_opencv_core_LOCATION}/include/opencv2/core/base.hpp" |
||||
"${OPENCV_MODULE_opencv_core_LOCATION}/include/opencv2/core/types.hpp" |
||||
"${OPENCV_MODULE_opencv_core_LOCATION}/include/opencv2/core/persistence.hpp" |
||||
"${OPENCV_MODULE_opencv_core_LOCATION}/include/opencv2/core/utility.hpp" |
||||
"${OPENCV_MODULE_opencv_core_LOCATION}/include/opencv2/core/ocl.hpp" |
||||
"${OPENCV_MODULE_opencv_flann_LOCATION}/include/opencv2/flann/miniflann.hpp" |
||||
"${OPENCV_MODULE_opencv_imgproc_LOCATION}/include/opencv2/imgproc.hpp" |
||||
"${OPENCV_MODULE_opencv_video_LOCATION}/include/opencv2/video/background_segm.hpp" |
||||
"${OPENCV_MODULE_opencv_video_LOCATION}/include/opencv2/video/tracking.hpp" |
||||
"${OPENCV_MODULE_opencv_photo_LOCATION}/include/opencv2/photo.hpp" |
||||
"${OPENCV_MODULE_opencv_highgui_LOCATION}/include/opencv2/highgui.hpp" |
||||
"${OPENCV_MODULE_opencv_ml_LOCATION}/include/opencv2/ml.hpp" |
||||
"${OPENCV_MODULE_opencv_features2d_LOCATION}/include/opencv2/features2d.hpp" |
||||
"${OPENCV_MODULE_opencv_calib3d_LOCATION}/include/opencv2/calib3d.hpp" |
||||
"${OPENCV_MODULE_opencv_objdetect_LOCATION}/include/opencv2/objdetect.hpp" |
||||
) |
||||
|
||||
if(HAVE_opencv_nonfree) |
||||
list(APPEND opencv_hdrs "${OPENCV_MODULE_opencv_nonfree_LOCATION}/include/opencv2/nonfree/features2d.hpp" |
||||
"${OPENCV_MODULE_opencv_nonfree_LOCATION}/include/opencv2/nonfree.hpp") |
||||
endif() |
||||
|
||||
set(cv2_generated_hdrs |
||||
"${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_include.h" |
||||
"${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_funcs.h" |
||||
"${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_func_tab.h" |
||||
"${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_types.h" |
||||
"${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_type_reg.h" |
||||
"${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_const_reg.h") |
||||
|
||||
add_custom_command( |
||||
OUTPUT ${cv2_generated_hdrs} |
||||
COMMAND ${PYTHON_EXECUTABLE} "${PYTHON_SOURCE_DIR}/src2/gen2.py" ${CMAKE_CURRENT_BINARY_DIR} ${opencv_hdrs} |
||||
DEPENDS ${PYTHON_SOURCE_DIR}/src2/gen2.py |
||||
DEPENDS ${PYTHON_SOURCE_DIR}/src2/hdr_parser.py |
||||
DEPENDS ${opencv_hdrs}) |
||||
|
||||
add_library(${the_module} SHARED ${PYTHON_SOURCE_DIR}/src2/cv2.cpp ${cv2_generated_hdrs}) |
||||
set_target_properties(${the_module} PROPERTIES COMPILE_DEFINITIONS OPENCV_NOSTL) |
||||
|
||||
if(PYTHON_DEBUG_LIBRARIES AND NOT PYTHON_LIBRARIES MATCHES "optimized.*debug") |
||||
target_link_libraries(${the_module} debug ${PYTHON_DEBUG_LIBRARIES} optimized ${PYTHON_LIBRARIES}) |
||||
else() |
||||
target_link_libraries(${the_module} ${PYTHON_LIBRARIES}) |
||||
endif() |
||||
target_link_libraries(${the_module} ${OPENCV_MODULE_${the_module}_DEPS}) |
||||
|
||||
execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import distutils.sysconfig; print(distutils.sysconfig.get_config_var('SO'))" |
||||
RESULT_VARIABLE PYTHON_CVPY_PROCESS |
||||
OUTPUT_VARIABLE CVPY_SUFFIX |
||||
OUTPUT_STRIP_TRAILING_WHITESPACE) |
||||
|
||||
set_target_properties(${the_module} PROPERTIES |
||||
PREFIX "" |
||||
OUTPUT_NAME cv2 |
||||
SUFFIX ${CVPY_SUFFIX}) |
||||
|
||||
if(ENABLE_SOLUTION_FOLDERS) |
||||
set_target_properties(${the_module} PROPERTIES FOLDER "bindings") |
||||
endif() |
||||
|
||||
if(MSVC) |
||||
add_definitions(-DCVAPI_EXPORTS) |
||||
endif() |
||||
|
||||
if(CMAKE_COMPILER_IS_GNUCXX AND NOT ENABLE_NOISY_WARNINGS) |
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-function") |
||||
endif() |
||||
|
||||
if(MSVC AND NOT ENABLE_NOISY_WARNINGS) |
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4100") #unreferenced formal parameter |
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4127") #conditional expression is constant |
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4505") #unreferenced local function has been removed |
||||
string(REPLACE "/W4" "/W3" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") |
||||
endif() |
||||
|
||||
if(MSVC AND NOT BUILD_SHARED_LIBS) |
||||
set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:atlthunk.lib /NODEFAULTLIB:atlsd.lib /DEBUG") |
||||
endif() |
||||
|
||||
if(MSVC AND NOT PYTHON_DEBUG_LIBRARIES) |
||||
set(PYTHON_INSTALL_CONFIGURATIONS CONFIGURATIONS Release) |
||||
else() |
||||
set(PYTHON_INSTALL_CONFIGURATIONS "") |
||||
endif() |
||||
|
||||
if(WIN32) |
||||
set(PYTHON_INSTALL_ARCHIVE "") |
||||
else() |
||||
set(PYTHON_INSTALL_ARCHIVE ARCHIVE DESTINATION ${PYTHON_PACKAGES_PATH} COMPONENT python) |
||||
endif() |
||||
|
||||
if(NOT INSTALL_CREATE_DISTRIB) |
||||
install(TARGETS ${the_module} |
||||
${PYTHON_INSTALL_CONFIGURATIONS} |
||||
RUNTIME DESTINATION ${PYTHON_PACKAGES_PATH} COMPONENT python |
||||
LIBRARY DESTINATION ${PYTHON_PACKAGES_PATH} COMPONENT python |
||||
${PYTHON_INSTALL_ARCHIVE} |
||||
) |
||||
else() |
||||
if(DEFINED PYTHON_VERSION_MAJOR) |
||||
set(__ver "${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}") |
||||
else() |
||||
set(__ver "unknown") |
||||
endif() |
||||
install(TARGETS ${the_module} |
||||
CONFIGURATIONS Release |
||||
RUNTIME DESTINATION python/${__ver}/${OpenCV_ARCH} COMPONENT python |
||||
LIBRARY DESTINATION python/${__ver}/${OpenCV_ARCH} COMPONENT python |
||||
) |
||||
endif() |
||||
|
||||
unset(PYTHON_SRC_DIR) |
||||
unset(PYTHON_CVPY_PROCESS) |
||||
unset(CVPY_SUFFIX) |
||||
unset(PYTHON_INSTALL_CONFIGURATIONS) |
||||
unset(PYTHON_INSTALL_ARCHIVE) |
@ -0,0 +1,26 @@ |
||||
if(NOT PYTHON2LIBS_FOUND OR NOT PYTHON2_NUMPY_INCLUDE_DIRS) |
||||
ocv_module_disable(python2) |
||||
endif() |
||||
|
||||
set(the_description "The python2 bindings") |
||||
ocv_add_module(python2 BINDINGS opencv_core opencv_flann opencv_imgproc opencv_video opencv_ml opencv_features2d opencv_highgui opencv_calib3d opencv_photo opencv_objdetect OPTIONAL opencv_nonfree) |
||||
|
||||
set(PYTHON_INCLUDE_PATH ${PYTHON2_INCLUDE_PATH}) |
||||
set(PYTHON_NUMPY_INCLUDE_PATH ${PYTHON2_NUMPY_INCLUDE_PATH}) |
||||
set(PYTHON_EXECUTABLE ${PYTHON2_EXECUTABLE}) |
||||
set(PYTHON_DEBUG_LIBRARIES ${PYTHON2_DEBUG_LIBRARIES}) |
||||
set(PYTHON_LIBRARIES ${PYTHON2_LIBRARIES}) |
||||
set(PYTHON_PACKAGES_PATH ${PYTHON2_PACKAGES_PATH}) |
||||
set(PYTHON_VERSION_MAJOR ${PYTHON2_VERSION_MAJOR}) |
||||
set(PYTHON_VERSION_MINOR ${PYTHON2_VERSION_MINOR}) |
||||
|
||||
include(../common.cmake) |
||||
|
||||
unset(PYTHON_INCLUDE_PATH) |
||||
unset(PYTHON_NUMPY_INCLUDE_PATH) |
||||
unset(PYTHON_EXECUTABLE) |
||||
unset(PYTHON_DEBUG_LIBRARIES) |
||||
unset(PYTHON_LIBRARIES) |
||||
unset(PYTHON_PACKAGES_PATH) |
||||
unset(PYTHON_VERSION_MAJOR) |
||||
unset(PYTHON_VERSION_MINOR) |
@ -0,0 +1,26 @@ |
||||
if(NOT PYTHON3LIBS_FOUND OR NOT PYTHON3_NUMPY_INCLUDE_DIRS) |
||||
ocv_module_disable(python3) |
||||
endif() |
||||
|
||||
set(the_description "The python3 bindings") |
||||
ocv_add_module(python3 BINDINGS opencv_core opencv_flann opencv_imgproc opencv_video opencv_ml opencv_features2d opencv_highgui opencv_calib3d opencv_photo opencv_objdetect OPTIONAL opencv_nonfree) |
||||
|
||||
set(PYTHON_INCLUDE_PATH ${PYTHON3_INCLUDE_PATH}) |
||||
set(PYTHON_NUMPY_INCLUDE_PATH ${PYTHON3_NUMPY_INCLUDE_PATH}) |
||||
set(PYTHON_EXECUTABLE ${PYTHON3_EXECUTABLE}) |
||||
set(PYTHON_DEBUG_LIBRARIES ${PYTHON3_DEBUG_LIBRARIES}) |
||||
set(PYTHON_LIBRARIES ${PYTHON3_LIBRARIES}) |
||||
set(PYTHON_PACKAGES_PATH ${PYTHON3_PACKAGES_PATH}) |
||||
set(PYTHON_VERSION_MAJOR ${PYTHON3_VERSION_MAJOR}) |
||||
set(PYTHON_VERSION_MINOR ${PYTHON3_VERSION_MINOR}) |
||||
|
||||
include(../common.cmake) |
||||
|
||||
unset(PYTHON_INCLUDE_PATH) |
||||
unset(PYTHON_NUMPY_INCLUDE_PATH) |
||||
unset(PYTHON_EXECUTABLE) |
||||
unset(PYTHON_DEBUG_LIBRARIES) |
||||
unset(PYTHON_LIBRARIES) |
||||
unset(PYTHON_PACKAGES_PATH) |
||||
unset(PYTHON_VERSION_MAJOR) |
||||
unset(PYTHON_VERSION_MINOR) |
Loading…
Reference in new issue