Home | History | Annotate | Download | only in python
      1 # This file is included from a subdirectory
      2 set(PYTHON_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../")
      3 
      4 ocv_add_module(${MODULE_NAME} BINDINGS)
      5 
      6 ocv_module_include_directories(
      7     "${PYTHON_INCLUDE_PATH}"
      8     ${PYTHON_NUMPY_INCLUDE_DIRS}
      9     "${PYTHON_SOURCE_DIR}/src2"
     10     )
     11 
     12 # get list of modules to wrap
     13 # message(STATUS "Wrapped in ${MODULE_NAME}:")
     14 set(OPENCV_PYTHON_MODULES)
     15 foreach(m ${OPENCV_MODULES_BUILD})
     16   if (";${OPENCV_MODULE_${m}_WRAPPERS};" MATCHES ";${MODULE_NAME};" AND HAVE_${m})
     17     list(APPEND OPENCV_PYTHON_MODULES ${m})
     18     # message(STATUS "\t${m}")
     19   endif()
     20 endforeach()
     21 
     22 set(opencv_hdrs "")
     23 foreach(m ${OPENCV_PYTHON_MODULES})
     24   list(APPEND opencv_hdrs ${OPENCV_MODULE_${m}_HEADERS})
     25 endforeach(m)
     26 
     27 # header blacklist
     28 ocv_list_filterout(opencv_hdrs ".h$")
     29 ocv_list_filterout(opencv_hdrs "cuda")
     30 ocv_list_filterout(opencv_hdrs "cudev")
     31 
     32 set(cv2_generated_hdrs
     33     "${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_include.h"
     34     "${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_funcs.h"
     35     "${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_types.h"
     36     "${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_type_reg.h"
     37     "${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_ns_reg.h")
     38 
     39 file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/headers.txt" "${opencv_hdrs}")
     40 add_custom_command(
     41    OUTPUT ${cv2_generated_hdrs}
     42    COMMAND ${PYTHON_EXECUTABLE} "${PYTHON_SOURCE_DIR}/src2/gen2.py" ${CMAKE_CURRENT_BINARY_DIR} "${CMAKE_CURRENT_BINARY_DIR}/headers.txt"
     43    DEPENDS ${PYTHON_SOURCE_DIR}/src2/gen2.py
     44    DEPENDS ${PYTHON_SOURCE_DIR}/src2/hdr_parser.py
     45    DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/headers.txt
     46    DEPENDS ${opencv_hdrs})
     47 
     48 ocv_add_library(${the_module} SHARED ${PYTHON_SOURCE_DIR}/src2/cv2.cpp ${cv2_generated_hdrs})
     49 
     50 if(PYTHON_DEBUG_LIBRARIES AND NOT PYTHON_LIBRARIES MATCHES "optimized.*debug")
     51   ocv_target_link_libraries(${the_module} debug ${PYTHON_DEBUG_LIBRARIES} optimized ${PYTHON_LIBRARIES})
     52 else()
     53   if(APPLE)
     54     set_target_properties(${the_module} PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
     55   else()
     56     ocv_target_link_libraries(${the_module} ${PYTHON_LIBRARIES})
     57   endif()
     58 endif()
     59 ocv_target_link_libraries(${the_module} ${OPENCV_MODULE_${the_module}_DEPS})
     60 
     61 execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import distutils.sysconfig; print(distutils.sysconfig.get_config_var('SO'))"
     62                 RESULT_VARIABLE PYTHON_CVPY_PROCESS
     63                 OUTPUT_VARIABLE CVPY_SUFFIX
     64                 OUTPUT_STRIP_TRAILING_WHITESPACE)
     65 
     66 set_target_properties(${the_module} PROPERTIES
     67                       LIBRARY_OUTPUT_DIRECTORY  "${LIBRARY_OUTPUT_PATH}/${MODULE_INSTALL_SUBDIR}"
     68                       PREFIX ""
     69                       OUTPUT_NAME cv2
     70                       SUFFIX ${CVPY_SUFFIX})
     71 
     72 if(ENABLE_SOLUTION_FOLDERS)
     73   set_target_properties(${the_module} PROPERTIES FOLDER "bindings")
     74 endif()
     75 
     76 if(MSVC)
     77   add_definitions(-DCVAPI_EXPORTS)
     78 endif()
     79 
     80 if(CMAKE_COMPILER_IS_GNUCXX AND NOT ENABLE_NOISY_WARNINGS)
     81   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-function")
     82 endif()
     83 
     84 if(MSVC AND NOT ENABLE_NOISY_WARNINGS)
     85   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4100") #unreferenced formal parameter
     86   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4127") #conditional expression is constant
     87   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4505") #unreferenced local function has been removed
     88   string(REPLACE "/W4" "/W3" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
     89 endif()
     90 
     91 if(MSVC AND NOT BUILD_SHARED_LIBS)
     92   set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:atlthunk.lib /NODEFAULTLIB:atlsd.lib /DEBUG")
     93 endif()
     94 
     95 if(MSVC AND NOT PYTHON_DEBUG_LIBRARIES)
     96   set(PYTHON_INSTALL_CONFIGURATIONS CONFIGURATIONS Release)
     97 else()
     98   set(PYTHON_INSTALL_CONFIGURATIONS "")
     99 endif()
    100 
    101 if(WIN32)
    102   set(PYTHON_INSTALL_ARCHIVE "")
    103 else()
    104   set(PYTHON_INSTALL_ARCHIVE ARCHIVE DESTINATION ${PYTHON_PACKAGES_PATH} COMPONENT python)
    105 endif()
    106 
    107 if(NOT INSTALL_CREATE_DISTRIB)
    108   install(TARGETS ${the_module} OPTIONAL
    109           ${PYTHON_INSTALL_CONFIGURATIONS}
    110           RUNTIME DESTINATION ${PYTHON_PACKAGES_PATH} COMPONENT python
    111           LIBRARY DESTINATION ${PYTHON_PACKAGES_PATH} COMPONENT python
    112           ${PYTHON_INSTALL_ARCHIVE}
    113           )
    114 else()
    115   if(DEFINED PYTHON_VERSION_MAJOR)
    116     set(__ver "${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}")
    117   else()
    118     set(__ver "unknown")
    119   endif()
    120   install(TARGETS ${the_module}
    121           CONFIGURATIONS Release
    122           RUNTIME DESTINATION python/${__ver}/${OpenCV_ARCH} COMPONENT python
    123           LIBRARY DESTINATION python/${__ver}/${OpenCV_ARCH} COMPONENT python
    124           )
    125 endif()
    126 
    127 unset(PYTHON_SRC_DIR)
    128 unset(PYTHON_CVPY_PROCESS)
    129 unset(CVPY_SUFFIX)
    130 unset(PYTHON_INSTALL_CONFIGURATIONS)
    131 unset(PYTHON_INSTALL_ARCHIVE)
    132