OpenCVPluginStandalone.cmake 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # Standalone OpenCV plugins build scripts
  2. #
  3. # Useful OpenCV common build variables:
  4. # - CMAKE_BUILD_TYPE=Release/Debug
  5. # - BUILD_WITH_DEBUG_INFO=ON
  6. # - ENABLE_BUILD_HARDENING=ON
  7. #
  8. # Plugin configuration variables:
  9. # - OPENCV_PLUGIN_DEPS - set of extra dependencies (modules), used for include dirs, target_link_libraries
  10. # - OPENCV_PLUGIN_SUFFIX
  11. # - OPENCV_PLUGIN_NAME
  12. # - OPENCV_PLUGIN_OUTPUT_NAME_FULL (overrides both OPENCV_PLUGIN_NAME / OPENCV_PLUGIN_SUFFIX)
  13. #
  14. #=============================================
  15. if(NOT OpenCV_SOURCE_DIR)
  16. message(FATAL_ERROR "OpenCV_SOURCE_DIR must be set to build the plugin!")
  17. endif()
  18. if(NOT DEFINED CMAKE_BUILD_TYPE)
  19. set(CMAKE_BUILD_TYPE "Release")
  20. endif()
  21. message(STATUS "CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}")
  22. set(BUILD_SHARED_LIBS ON CACHE BOOL "")
  23. if(NOT BUILD_SHARED_LIBS)
  24. message(FATAL_ERROR "Static plugin build does not make sense")
  25. endif()
  26. # re-use OpenCV build scripts
  27. include("${OpenCV_SOURCE_DIR}/cmake/OpenCVUtils.cmake")
  28. include("${OpenCV_SOURCE_DIR}/cmake/OpenCVDetectCXXCompiler.cmake")
  29. include("${OpenCV_SOURCE_DIR}/cmake/OpenCVCompilerOptions.cmake")
  30. function(ocv_create_plugin module default_name dependency_target dependency_target_desc)
  31. set(OPENCV_PLUGIN_NAME ${default_name} CACHE STRING "")
  32. set(OPENCV_PLUGIN_DESTINATION "" CACHE PATH "")
  33. project(${OPENCV_PLUGIN_NAME} LANGUAGES CXX)
  34. if(NOT TARGET ${dependency_target})
  35. message(FATAL_ERROR "${dependency_target_desc} was not found! (missing target ${dependency_target})")
  36. endif()
  37. set(modules_ROOT "${OpenCV_SOURCE_DIR}/modules")
  38. set(module_ROOT "${modules_ROOT}/${module}")
  39. foreach(src ${ARGN})
  40. list(APPEND sources "${module_ROOT}/${src}")
  41. endforeach()
  42. add_library(${OPENCV_PLUGIN_NAME} MODULE
  43. "${sources}"
  44. ${OPENCV_PLUGIN_EXTRA_SRC_FILES}
  45. )
  46. if(OPENCV_PLUGIN_DEPS)
  47. foreach(d ${OPENCV_PLUGIN_DEPS})
  48. list(APPEND OPENCV_PLUGIN_EXTRA_INCLUDES "${modules_ROOT}/${d}/include")
  49. endforeach()
  50. endif()
  51. target_include_directories(${OPENCV_PLUGIN_NAME} PRIVATE
  52. "${CMAKE_CURRENT_BINARY_DIR}"
  53. "${module_ROOT}/src"
  54. "${module_ROOT}/include"
  55. ${OPENCV_PLUGIN_EXTRA_INCLUDES}
  56. )
  57. target_compile_definitions(${OPENCV_PLUGIN_NAME} PRIVATE "BUILD_PLUGIN=1")
  58. target_link_libraries(${OPENCV_PLUGIN_NAME} PRIVATE ${dependency_target})
  59. set_target_properties(${OPENCV_PLUGIN_NAME} PROPERTIES
  60. CXX_STANDARD 11
  61. CXX_VISIBILITY_PRESET hidden
  62. )
  63. if(DEFINED OPENCV_PLUGIN_MODULE_PREFIX)
  64. set_target_properties(${OPENCV_PLUGIN_NAME} PROPERTIES PREFIX "${OPENCV_PLUGIN_MODULE_PREFIX}")
  65. endif()
  66. if(WIN32 OR NOT APPLE)
  67. set(OPENCV_PLUGIN_NO_LINK FALSE CACHE BOOL "")
  68. else()
  69. set(OPENCV_PLUGIN_NO_LINK TRUE CACHE BOOL "")
  70. endif()
  71. if(OPENCV_PLUGIN_NO_LINK)
  72. if(APPLE)
  73. set_target_properties(${OPENCV_PLUGIN_NAME} PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
  74. endif()
  75. else()
  76. find_package(OpenCV REQUIRED ${module} ${OPENCV_PLUGIN_DEPS})
  77. target_link_libraries(${OPENCV_PLUGIN_NAME} PRIVATE ${OpenCV_LIBRARIES})
  78. endif()
  79. if(NOT OpenCV_FOUND) # build against sources (Linux)
  80. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/opencv2/opencv_modules.hpp" "#pragma once")
  81. endif()
  82. if(WIN32)
  83. ocv_update(OPENCV_DEBUG_POSTFIX d)
  84. endif()
  85. set_target_properties(${OPENCV_PLUGIN_NAME} PROPERTIES DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}")
  86. if(DEFINED OPENCV_PLUGIN_SUFFIX)
  87. # custom value
  88. else()
  89. if(WIN32)
  90. ocv_update(OPENCV_PLUGIN_VERSION "${OpenCV_VERSION_MAJOR}${OpenCV_VERSION_MINOR}${OpenCV_VERSION_PATCH}")
  91. if(CMAKE_CXX_SIZEOF_DATA_PTR EQUAL 8)
  92. ocv_update(OPENCV_PLUGIN_ARCH "_64")
  93. else()
  94. ocv_update(OPENCV_PLUGIN_ARCH "")
  95. endif()
  96. else()
  97. # empty
  98. endif()
  99. ocv_update(OPENCV_PLUGIN_SUFFIX "${OPENCV_PLUGIN_VERSION}${OPENCV_PLUGIN_ARCH}")
  100. endif()
  101. if(OPENCV_PLUGIN_DESTINATION)
  102. set_target_properties(${OPENCV_PLUGIN_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${OPENCV_PLUGIN_DESTINATION}")
  103. message(STATUS "Output destination: ${OPENCV_PLUGIN_DESTINATION}")
  104. endif()
  105. if(OPENCV_PLUGIN_OUTPUT_NAME_FULL)
  106. set_target_properties(${OPENCV_PLUGIN_NAME} PROPERTIES OUTPUT_NAME "${OPENCV_PLUGIN_OUTPUT_NAME_FULL}")
  107. elseif(OPENCV_PLUGIN_OUTPUT_NAME)
  108. set_target_properties(${OPENCV_PLUGIN_NAME} PROPERTIES OUTPUT_NAME "${OPENCV_PLUGIN_OUTPUT_NAME}${OPENCV_PLUGIN_SUFFIX}")
  109. else()
  110. set_target_properties(${OPENCV_PLUGIN_NAME} PROPERTIES OUTPUT_NAME "${OPENCV_PLUGIN_NAME}${OPENCV_PLUGIN_SUFFIX}")
  111. endif()
  112. install(TARGETS ${OPENCV_PLUGIN_NAME} LIBRARY DESTINATION . COMPONENT plugins)
  113. message(STATUS "Library name: ${OPENCV_PLUGIN_NAME}")
  114. endfunction()