OpenCVGenPkgconfig.cmake 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. if(MSVC OR IOS)
  2. return()
  3. endif()
  4. # --------------------------------------------------------------------------------------------
  5. # according to man pkg-config
  6. # The package name specified on the pkg-config command line is defined to
  7. # be the name of the metadata file, minus the .pc extension. If a library
  8. # can install multiple versions simultaneously, it must give each version
  9. # its own name (for example, GTK 1.2 might have the package name "gtk+"
  10. # while GTK 2.0 has "gtk+-2.0").
  11. #
  12. # ${BIN_DIR}/unix-install/opencv.pc -> For use *with* "make install"
  13. # -------------------------------------------------------------------------------------------
  14. macro(fix_prefix lst isown)
  15. set(_lst)
  16. foreach(item ${${lst}})
  17. if(DEFINED TARGET_LOCATION_${item})
  18. set(item "${TARGET_LOCATION_${item}}")
  19. if(${isown})
  20. get_filename_component(item "${item}" NAME)
  21. ocv_get_libname(item "${item}")
  22. endif()
  23. endif()
  24. if(item MATCHES "^-l")
  25. list(APPEND _lst "${item}")
  26. elseif(item MATCHES "^-framework") # MacOS framework (assume single entry "-framework OpenCL")
  27. list(APPEND _lst "${item}")
  28. elseif(item MATCHES "[\\/]")
  29. get_filename_component(libdir "${item}" PATH)
  30. get_filename_component(_libname "${item}" NAME)
  31. ocv_get_libname(libname "${_libname}")
  32. list(APPEND _lst "-L${libdir}" "-l${libname}")
  33. else()
  34. list(APPEND _lst "-l${item}")
  35. endif()
  36. endforeach()
  37. set(${lst} ${_lst})
  38. unset(_lst)
  39. endmacro()
  40. if(NOT DEFINED CMAKE_HELPER_SCRIPT)
  41. if(INSTALL_TO_MANGLED_PATHS)
  42. ocv_update(OPENCV_PC_FILE_NAME "opencv-${OPENCV_VERSION}.pc")
  43. else()
  44. ocv_update(OPENCV_PC_FILE_NAME opencv4.pc)
  45. endif()
  46. # build the list of opencv libs and dependencies for all modules
  47. ocv_get_all_libs(_modules _extra _3rdparty)
  48. #build the list of components
  49. # Note:
  50. # when linking against static libraries, if libfoo depends on libbar, then
  51. # libfoo must come first in the linker flags.
  52. # world is a special target whose library should come first,
  53. # especially for static link.
  54. if(_modules MATCHES "opencv_world")
  55. set(_modules "opencv_world")
  56. endif()
  57. set(HELPER_SCRIPT "")
  58. ocv_cmake_script_append_var(HELPER_SCRIPT
  59. BUILD_SHARED_LIBS
  60. CMAKE_BINARY_DIR
  61. CMAKE_INSTALL_PREFIX
  62. OpenCV_SOURCE_DIR
  63. OPENCV_PC_FILE_NAME
  64. OPENCV_VERSION_PLAIN
  65. OPENCV_LIB_INSTALL_PATH
  66. OPENCV_INCLUDE_INSTALL_PATH
  67. OPENCV_3P_LIB_INSTALL_PATH
  68. _modules
  69. _extra
  70. _3rdparty
  71. )
  72. foreach(item ${_modules} ${_extra} ${_3rdparty})
  73. if(TARGET ${item})
  74. set(HELPER_SCRIPT "${HELPER_SCRIPT}
  75. set(TARGET_LOCATION_${item} \"$<TARGET_FILE:${item}>\")
  76. ")
  77. endif()
  78. endforeach()
  79. set(CMAKE_HELPER_SCRIPT "${CMAKE_BINARY_DIR}/OpenCVGenPkgConfig.info.cmake")
  80. file(GENERATE OUTPUT "${CMAKE_HELPER_SCRIPT}" CONTENT "${HELPER_SCRIPT}")
  81. add_custom_target(developer_scripts)
  82. add_custom_command(
  83. OUTPUT "${CMAKE_BINARY_DIR}/unix-install/${OPENCV_PC_FILE_NAME}"
  84. COMMAND ${CMAKE_COMMAND} "-DCMAKE_HELPER_SCRIPT=${CMAKE_HELPER_SCRIPT}" -P "${OpenCV_SOURCE_DIR}/cmake/OpenCVGenPkgconfig.cmake"
  85. DEPENDS "${CMAKE_BINARY_DIR}/OpenCVGenPkgConfig.info.cmake"
  86. "${OpenCV_SOURCE_DIR}/cmake/OpenCVGenPkgconfig.cmake"
  87. COMMENT "Generate ${OPENCV_PC_FILE_NAME}"
  88. )
  89. add_custom_target(gen-pkgconfig ALL SOURCES "${CMAKE_BINARY_DIR}/unix-install/${OPENCV_PC_FILE_NAME}")
  90. add_dependencies(developer_scripts gen-pkgconfig)
  91. if((UNIX AND NOT ANDROID) OR MINGW)
  92. install(FILES ${CMAKE_BINARY_DIR}/unix-install/${OPENCV_PC_FILE_NAME} DESTINATION ${OPENCV_LIB_INSTALL_PATH}/pkgconfig COMPONENT dev)
  93. endif()
  94. # =============================================================================
  95. else() # DEFINED CMAKE_HELPER_SCRIPT
  96. cmake_minimum_required(VERSION 2.8.12.2)
  97. cmake_policy(SET CMP0012 NEW)
  98. include("${CMAKE_HELPER_SCRIPT}")
  99. include("${OpenCV_SOURCE_DIR}/cmake/OpenCVUtils.cmake")
  100. fix_prefix(_modules 1)
  101. fix_prefix(_extra 0)
  102. fix_prefix(_3rdparty 1)
  103. ocv_list_unique(_modules)
  104. ocv_list_unique(_extra)
  105. ocv_list_unique(_3rdparty)
  106. set(OPENCV_PC_LIBS
  107. "-L\${exec_prefix}/${OPENCV_LIB_INSTALL_PATH}"
  108. "${_modules}"
  109. )
  110. if(BUILD_SHARED_LIBS)
  111. set(OPENCV_PC_LIBS_PRIVATE "${_extra}")
  112. else()
  113. set(OPENCV_PC_LIBS_PRIVATE
  114. "-L\${exec_prefix}/${OPENCV_3P_LIB_INSTALL_PATH}"
  115. "${_3rdparty}"
  116. "${_extra}"
  117. )
  118. endif()
  119. string(REPLACE ";" " " OPENCV_PC_LIBS "${OPENCV_PC_LIBS}")
  120. string(REPLACE ";" " " OPENCV_PC_LIBS_PRIVATE "${OPENCV_PC_LIBS_PRIVATE}")
  121. #generate the .pc file
  122. set(prefix "${CMAKE_INSTALL_PREFIX}")
  123. set(exec_prefix "\${prefix}")
  124. set(libdir "\${exec_prefix}/${OPENCV_LIB_INSTALL_PATH}")
  125. set(includedir "\${prefix}/${OPENCV_INCLUDE_INSTALL_PATH}")
  126. configure_file("${OpenCV_SOURCE_DIR}/cmake/templates/opencv-XXX.pc.in"
  127. "${CMAKE_BINARY_DIR}/unix-install/${OPENCV_PC_FILE_NAME}"
  128. @ONLY)
  129. endif() # DEFINED CMAKE_HELPER_SCRIPT