Fixed python detection on Windows with both 32bit and 64bit pythons installed.

This commit is contained in:
Andrey Kamaev 2012-04-08 17:36:41 +00:00
parent 35a7dfbd37
commit c16a1d86cc
3 changed files with 95 additions and 93 deletions

View File

@ -835,26 +835,30 @@ status(" Use TBB:" HAVE_TBB THEN YES ELSE NO)
status(" Use Cuda:" HAVE_CUDA THEN YES ELSE NO) status(" Use Cuda:" HAVE_CUDA THEN YES ELSE NO)
status(" Use Eigen:" HAVE_EIGEN THEN YES ELSE NO) status(" Use Eigen:" HAVE_EIGEN THEN YES ELSE NO)
status("")
status(" Python interpreter:" PYTHON_EXECUTABLE THEN "${PYTHON_EXECUTABLE} (ver ${PYTHON_VERSION_MAJOR_MINOR})" ELSE NO)
# interfaces to other languages # interfaces to other languages
status("") status("")
status(" Interfaces:") status(" Python:")
status(" Python:" HAVE_opencv_python THEN YES ELSE NO) status(" Interpreter:" PYTHON_EXECUTABLE THEN "${PYTHON_EXECUTABLE} (ver ${PYTHON_VERSION_FULL})" ELSE NO)
status(" Python numpy:" PYTHON_USE_NUMPY THEN YES ELSE "NO (Python wrappers can not be generated)") status(" Libraries:" HAVE_opencv_python THEN ${PYTHON_LIBRARIES} ELSE NO)
status(" numpy:" PYTHON_USE_NUMPY THEN "${PYTHON_NUMPY_INCLUDE_DIR} (ver ${PYTHON_NUMPY_VERSION})" ELSE "NO (Python wrappers can not be generated)")
status(" packages path:" PYTHON_EXECUTABLE THEN "${PYTHON_PACKAGES_PATH}" ELSE -)
if(ANDROID) if(ANDROID)
status(" Java:" HAVE_opencv_java THEN YES ELSE NO) status("")
status(" Java:" HAVE_opencv_java THEN YES ELSE NO)
endif() endif()
# documentation # documentation
status("") if(BUILD_DOCS)
status(" Documentation:") status("")
status(" Sphinx:" HAVE_SPHINX THEN "${SPHINX_BUILD} (ver ${SPHINX_VERSION})" ELSE NO) status(" Documentation:")
status(" PdfLaTeX compiler:" PDFLATEX_COMPILER THEN "${PDFLATEX_COMPILER}" ELSE NO) status(" Sphinx:" HAVE_SPHINX THEN "${SPHINX_BUILD} (ver ${SPHINX_VERSION})" ELSE NO)
if(BUILD_DOCS AND HAVE_SPHINX) status(" PdfLaTeX compiler:" PDFLATEX_COMPILER THEN "${PDFLATEX_COMPILER}" ELSE NO)
status(" Build Documentation:" PDFLATEX_COMPILER THEN YES ELSE "YES (only HTML without math expressions)") if(HAVE_SPHINX)
else() status(" Build Documentation:" PDFLATEX_COMPILER THEN YES ELSE "YES (only HTML without math expressions)")
status(" Build Documentation:" NO) else()
status(" Build Documentation:" NO)
endif()
endif() endif()
# samples and tests # samples and tests

View File

@ -1,7 +1,18 @@
if(MSVC AND NOT PYTHON_EXECUTABLE)
# search for executable with the same bitness as resulting binaries
# standard FindPythonInterp always prefers executable from system path
foreach(_CURRENT_VERSION ${Python_ADDITIONAL_VERSIONS} 2.7 2.6 2.5 2.4 2.3 2.2 2.1 2.0 1.6 1.5)
find_host_program(PYTHON_EXECUTABLE
NAMES python${_CURRENT_VERSION} python
PATHS [HKEY_LOCAL_MACHINE\\\\SOFTWARE\\\\Python\\\\PythonCore\\\\${_CURRENT_VERSION}\\\\InstallPath]
NO_SYSTEM_ENVIRONMENT_PATH
)
endforeach()
endif()
find_host_package(PythonInterp) find_host_package(PythonInterp)
set(PYTHON_USE_NUMPY 0) unset(PYTHON_USE_NUMPY CACHE)
set(HAVE_SPHINX 0) unset(HAVE_SPHINX CACHE)
if(PYTHON_EXECUTABLE) if(PYTHON_EXECUTABLE)
if(NOT ANDROID AND NOT IOS) if(NOT ANDROID AND NOT IOS)
@ -14,9 +25,10 @@ if(PYTHON_EXECUTABLE)
execute_process(COMMAND ${PYTHON_EXECUTABLE} --version execute_process(COMMAND ${PYTHON_EXECUTABLE} --version
ERROR_VARIABLE PYTHON_VERSION_FULL ERROR_VARIABLE PYTHON_VERSION_FULL
OUTPUT_STRIP_TRAILING_WHITESPACE) ERROR_STRIP_TRAILING_WHITESPACE)
string(REGEX MATCH "[0-9]+.[0-9]+" PYTHON_VERSION_MAJOR_MINOR "${PYTHON_VERSION_FULL}") string(REGEX MATCH "[0-9]+.[0-9]+" PYTHON_VERSION_MAJOR_MINOR "${PYTHON_VERSION_FULL}")
string(REGEX MATCH "[0-9]+.[0-9]+.[0-9]+" PYTHON_VERSION_FULL "${PYTHON_VERSION_FULL}")
if(NOT ANDROID AND NOT IOS) if(NOT ANDROID AND NOT IOS)
if(CMAKE_HOST_UNIX) if(CMAKE_HOST_UNIX)
@ -46,18 +58,25 @@ if(PYTHON_EXECUTABLE)
set(PYTHON_PACKAGES_PATH "${PYTHON_PATH}/Lib/site-packages") set(PYTHON_PACKAGES_PATH "${PYTHON_PATH}/Lib/site-packages")
endif() endif()
# Attempt to discover the NumPy include directory. If this succeeds, then build python API with NumPy if(NOT PYTHON_NUMPY_INCLUDE_DIR)
execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import os; os.environ['DISTUTILS_USE_SDK']='1'; import numpy.distutils; print numpy.distutils.misc_util.get_numpy_include_dirs()[0]" # Attempt to discover the NumPy include directory. If this succeeds, then build python API with NumPy
RESULT_VARIABLE PYTHON_NUMPY_PROCESS execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import os; os.environ['DISTUTILS_USE_SDK']='1'; import numpy.distutils; print numpy.distutils.misc_util.get_numpy_include_dirs()[0]"
OUTPUT_VARIABLE PYTHON_NUMPY_INCLUDE_DIRS RESULT_VARIABLE PYTHON_NUMPY_PROCESS
OUTPUT_STRIP_TRAILING_WHITESPACE) OUTPUT_VARIABLE PYTHON_NUMPY_INCLUDE_DIR
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(PYTHON_NUMPY_PROCESS EQUAL 0)
set(PYTHON_USE_NUMPY 1) if(PYTHON_NUMPY_PROCESS EQUAL 0)
add_definitions(-DPYTHON_USE_NUMPY=1) file(TO_CMAKE_PATH "${PYTHON_NUMPY_INCLUDE_DIR}" PYTHON_NUMPY_INCLUDE_DIR)
file(TO_CMAKE_PATH "${PYTHON_NUMPY_INCLUDE_DIRS}" PYTHON_NUMPY_INCLUDE_DIRS) set(PYTHON_NUMPY_INCLUDE_DIR ${PYTHON_NUMPY_INCLUDE_DIR} CACHE PATH "Path to numpy headers")
ocv_include_directories(${PYTHON_NUMPY_INCLUDE_DIRS}) endif()
message(STATUS " Use NumPy headers from: ${PYTHON_NUMPY_INCLUDE_DIRS}") endif()
if(PYTHON_NUMPY_INCLUDE_DIR)
set(PYTHON_USE_NUMPY TRUE)
execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import numpy; print numpy.version.version"
RESULT_VARIABLE PYTHON_NUMPY_PROCESS
OUTPUT_VARIABLE PYTHON_NUMPY_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE)
endif() endif()
endif(NOT ANDROID AND NOT IOS) endif(NOT ANDROID AND NOT IOS)
@ -73,7 +92,7 @@ if(PYTHON_EXECUTABLE)
find_host_program(SPHINX_BUILD sphinx-build) find_host_program(SPHINX_BUILD sphinx-build)
if(SPHINX_BUILD) if(SPHINX_BUILD)
set(HAVE_SPHINX 1) set(HAVE_SPHINX 1)
message(STATUS " Found Sphinx ${SPHINX_VERSION}: ${SPHINX_BUILD}") message(STATUS " Found Sphinx ${SPHINX_VERSION}: ${SPHINX_BUILD}")
endif() endif()
endif() endif()
endif(BUILD_DOCS) endif(BUILD_DOCS)

View File

@ -10,45 +10,29 @@ if(ANDROID OR IOS OR NOT PYTHONLIBS_FOUND OR NOT PYTHON_USE_NUMPY)
endif() endif()
set(the_description "The python bindings") set(the_description "The python bindings")
ocv_add_module(python BINDINGS opencv_core opencv_flann opencv_imgproc opencv_video opencv_ml opencv_features2d opencv_highgui opencv_calib3d opencv_photo opencv_nonfree opencv_objdetect opencv_legacy opencv_contrib) ocv_add_module(python BINDINGS opencv_core opencv_flann opencv_imgproc opencv_video opencv_ml opencv_features2d opencv_highgui opencv_calib3d opencv_photo opencv_nonfree opencv_objdetect opencv_legacy)
ocv_include_directories(${PYTHON_INCLUDE_PATH}) add_definitions(-DPYTHON_USE_NUMPY=1)
ocv_include_directories(
ocv_module_include_directories(
"${PYTHON_INCLUDE_PATH}"
"${PYTHON_NUMPY_INCLUDE_DIR}"
"${CMAKE_CURRENT_SOURCE_DIR}/src2" "${CMAKE_CURRENT_SOURCE_DIR}/src2"
"${OpenCV_SOURCE_DIR}/modules/core/include"
"${OpenCV_SOURCE_DIR}/modules/flann/include"
"${OpenCV_SOURCE_DIR}/modules/imgproc/include"
"${OpenCV_SOURCE_DIR}/modules/video/include"
"${OpenCV_SOURCE_DIR}/modules/photo/include"
"${OpenCV_SOURCE_DIR}/modules/highgui/include"
"${OpenCV_SOURCE_DIR}/modules/ml/include"
"${OpenCV_SOURCE_DIR}/modules/features2d/include"
"${OpenCV_SOURCE_DIR}/modules/flann/include"
"${OpenCV_SOURCE_DIR}/modules/calib3d/include"
"${OpenCV_SOURCE_DIR}/modules/objdetect/include"
"${OpenCV_SOURCE_DIR}/modules/nonfree/include"
"${OpenCV_SOURCE_DIR}/modules/legacy/include"
"${OpenCV_SOURCE_DIR}/modules/contrib/include"
) )
include_directories(${CMAKE_CURRENT_BINARY_DIR}) set(opencv_hdrs
"${OPENCV_MODULE_opencv_core_LOCATION}/include/opencv2/core/core.hpp"
set(opencv_hdrs "${OpenCV_SOURCE_DIR}/modules/core/include/opencv2/core/core.hpp" "${OPENCV_MODULE_opencv_flann_LOCATION}/include/opencv2/flann/miniflann.hpp"
"${OpenCV_SOURCE_DIR}/modules/flann/include/opencv2/flann/miniflann.hpp" "${OPENCV_MODULE_opencv_imgproc_LOCATION}/include/opencv2/imgproc/imgproc.hpp"
"${OpenCV_SOURCE_DIR}/modules/imgproc/include/opencv2/imgproc/imgproc.hpp" "${OPENCV_MODULE_opencv_video_LOCATION}/include/opencv2/video/background_segm.hpp"
"${OpenCV_SOURCE_DIR}/modules/video/include/opencv2/video/background_segm.hpp" "${OPENCV_MODULE_opencv_video_LOCATION}/include/opencv2/video/tracking.hpp"
"${OpenCV_SOURCE_DIR}/modules/video/include/opencv2/video/tracking.hpp" "${OPENCV_MODULE_opencv_photo_LOCATION}/include/opencv2/photo/photo.hpp"
"${OpenCV_SOURCE_DIR}/modules/photo/include/opencv2/photo/photo.hpp" "${OPENCV_MODULE_opencv_highgui_LOCATION}/include/opencv2/highgui/highgui.hpp"
"${OpenCV_SOURCE_DIR}/modules/highgui/include/opencv2/highgui/highgui.hpp" "${OPENCV_MODULE_opencv_ml_LOCATION}/include/opencv2/ml/ml.hpp"
"${OpenCV_SOURCE_DIR}/modules/ml/include/opencv2/ml/ml.hpp" "${OPENCV_MODULE_opencv_features2d_LOCATION}/include/opencv2/features2d/features2d.hpp"
"${OpenCV_SOURCE_DIR}/modules/features2d/include/opencv2/features2d/features2d.hpp" "${OPENCV_MODULE_opencv_nonfree_LOCATION}/include/opencv2/nonfree/features2d.hpp"
"${OpenCV_SOURCE_DIR}/modules/nonfree/include/opencv2/nonfree/features2d.hpp" "${OPENCV_MODULE_opencv_calib3d_LOCATION}/include/opencv2/calib3d/calib3d.hpp"
"${OpenCV_SOURCE_DIR}/modules/calib3d/include/opencv2/calib3d/calib3d.hpp" "${OPENCV_MODULE_opencv_objdetect_LOCATION}/include/opencv2/objdetect/objdetect.hpp")
"${OpenCV_SOURCE_DIR}/modules/objdetect/include/opencv2/objdetect/objdetect.hpp")
if(MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3")
endif()
set(cv2_generated_hdrs set(cv2_generated_hdrs
"${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_funcs.h" "${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_funcs.h"
@ -57,53 +41,48 @@ set(cv2_generated_hdrs
"${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_type_reg.h" "${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_type_reg.h"
"${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_const_reg.h") "${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_const_reg.h")
add_custom_command( add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/generated0.i OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/generated0.i
COMMAND ${PYTHON_EXECUTABLE} "${CMAKE_CURRENT_SOURCE_DIR}/src2/gen.py" "${CMAKE_CURRENT_SOURCE_DIR}/src2" COMMAND ${PYTHON_EXECUTABLE} "${CMAKE_CURRENT_SOURCE_DIR}/src2/gen.py" "${CMAKE_CURRENT_SOURCE_DIR}/src2"
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src2/api DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src2/api
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src2/defs DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src2/defs
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src2/gen.py DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src2/gen.py)
)
add_custom_command( add_custom_command(
OUTPUT ${cv2_generated_hdrs} OUTPUT ${cv2_generated_hdrs}
COMMAND ${PYTHON_EXECUTABLE} "${CMAKE_CURRENT_SOURCE_DIR}/src2/gen2.py" ${CMAKE_CURRENT_BINARY_DIR} ${opencv_hdrs} COMMAND ${PYTHON_EXECUTABLE} "${CMAKE_CURRENT_SOURCE_DIR}/src2/gen2.py" ${CMAKE_CURRENT_BINARY_DIR} ${opencv_hdrs}
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src2/gen2.py DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src2/gen2.py
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src2/hdr_parser.py DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src2/hdr_parser.py
DEPENDS ${opencv_hdrs}) DEPENDS ${opencv_hdrs})
set(cv2_target "opencv_python") add_library(${the_module} SHARED src2/cv2.cpp ${CMAKE_CURRENT_BINARY_DIR}/generated0.i ${cv2_generated_hdrs} src2/cv2.cv.hpp)
add_library(${cv2_target} SHARED src2/cv2.cpp ${CMAKE_CURRENT_BINARY_DIR}/generated0.i ${cv2_generated_hdrs} src2/cv2.cv.hpp) target_link_libraries(${the_module} ${PYTHON_LIBRARIES} ${OPENCV_MODULE_${the_module}_DEPS})
target_link_libraries(${cv2_target} ${PYTHON_LIBRARIES} opencv_core opencv_flann opencv_imgproc opencv_video opencv_ml opencv_features2d opencv_highgui opencv_calib3d opencv_objdetect opencv_legacy opencv_contrib opencv_photo)
set_target_properties(${cv2_target} PROPERTIES PREFIX "")
set_target_properties(${cv2_target} PROPERTIES OUTPUT_NAME "cv2")
execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import distutils.sysconfig; print distutils.sysconfig.get_config_var('SO')" execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import distutils.sysconfig; print distutils.sysconfig.get_config_var('SO')"
RESULT_VARIABLE PYTHON_CVPY_PROCESS RESULT_VARIABLE PYTHON_CVPY_PROCESS
OUTPUT_VARIABLE CVPY_SUFFIX OUTPUT_VARIABLE CVPY_SUFFIX
OUTPUT_STRIP_TRAILING_WHITESPACE) OUTPUT_STRIP_TRAILING_WHITESPACE)
set_target_properties(${cv2_target} PROPERTIES SUFFIX ${CVPY_SUFFIX}) set_target_properties(${the_module} PROPERTIES
PREFIX ""
OUTPUT_NAME cv2
SUFFIX ${CVPY_SUFFIX})
if (MSVC AND NOT BUILD_SHARED_LIBS) if(MSVC AND NOT BUILD_SHARED_LIBS)
set_target_properties(${cv2_target} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:atlthunk.lib /NODEFAULTLIB:atlsd.lib /DEBUG") set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:atlthunk.lib /NODEFAULTLIB:atlsd.lib /DEBUG")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3")
endif() endif()
set(cvpymodules ${cv2_target}) if(MSVC)
if(WIN32)
set(CVPY_PATH "Python${PYTHON_VERSION_MAJOR_MINOR}/Lib/site-packages")
set(PYTHON_INSTALL_CONFIGURATIONS CONFIGURATIONS Release) set(PYTHON_INSTALL_CONFIGURATIONS CONFIGURATIONS Release)
else() else()
set(CVPY_PATH ${PYTHON_PACKAGES_PATH})
set(PYTHON_INSTALL_CONFIGURATIONS "") set(PYTHON_INSTALL_CONFIGURATIONS "")
endif() endif()
install(TARGETS ${cvpymodules} ${PYTHON_INSTALL_CONFIGURATIONS} install(TARGETS ${the_module}
RUNTIME DESTINATION ${CVPY_PATH} COMPONENT main ${PYTHON_INSTALL_CONFIGURATIONS}
LIBRARY DESTINATION ${CVPY_PATH} COMPONENT main RUNTIME DESTINATION ${PYTHON_PACKAGES_PATH} COMPONENT main
ARCHIVE DESTINATION ${CVPY_PATH} COMPONENT main LIBRARY DESTINATION ${PYTHON_PACKAGES_PATH} COMPONENT main
) ARCHIVE DESTINATION ${PYTHON_PACKAGES_PATH} COMPONENT main
install(FILES src2/cv.py ${PYTHON_INSTALL_CONFIGURATIONS} DESTINATION ${CVPY_PATH} COMPONENT main) )
install(FILES src2/cv.py ${PYTHON_INSTALL_CONFIGURATIONS} DESTINATION ${PYTHON_PACKAGES_PATH} COMPONENT main)