FindJPEG.cmake 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. FindJPEG
  5. --------
  6. Find the Joint Photographic Experts Group (JPEG) library (``libjpeg``)
  7. Imported targets
  8. ^^^^^^^^^^^^^^^^
  9. .. versionadded:: 3.12
  10. This module defines the following :prop_tgt:`IMPORTED` targets:
  11. ``JPEG::JPEG``
  12. The JPEG library, if found.
  13. Result variables
  14. ^^^^^^^^^^^^^^^^
  15. This module will set the following variables in your project:
  16. ``JPEG_FOUND``
  17. If false, do not try to use JPEG.
  18. ``JPEG_INCLUDE_DIRS``
  19. where to find jpeglib.h, etc.
  20. ``JPEG_LIBRARIES``
  21. the libraries needed to use JPEG.
  22. ``JPEG_VERSION``
  23. .. versionadded:: 3.12
  24. the version of the JPEG library found
  25. Cache variables
  26. ^^^^^^^^^^^^^^^
  27. The following cache variables may also be set:
  28. ``JPEG_INCLUDE_DIRS``
  29. where to find jpeglib.h, etc.
  30. ``JPEG_LIBRARY_RELEASE``
  31. where to find the JPEG library (optimized).
  32. ``JPEG_LIBRARY_DEBUG``
  33. where to find the JPEG library (debug).
  34. .. versionadded:: 3.12
  35. Debug and Release variand are found separately.
  36. Obsolete variables
  37. ^^^^^^^^^^^^^^^^^^
  38. ``JPEG_INCLUDE_DIR``
  39. where to find jpeglib.h, etc. (same as JPEG_INCLUDE_DIRS)
  40. ``JPEG_LIBRARY``
  41. where to find the JPEG library.
  42. #]=======================================================================]
  43. cmake_policy(PUSH)
  44. cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
  45. find_path(JPEG_INCLUDE_DIR jpeglib.h)
  46. set(jpeg_names ${JPEG_NAMES} jpeg jpeg-static libjpeg libjpeg-static)
  47. foreach(name ${jpeg_names})
  48. list(APPEND jpeg_names_debug "${name}d")
  49. endforeach()
  50. if(NOT JPEG_LIBRARY)
  51. find_library(JPEG_LIBRARY_RELEASE NAMES ${jpeg_names} NAMES_PER_DIR)
  52. find_library(JPEG_LIBRARY_DEBUG NAMES ${jpeg_names_debug} NAMES_PER_DIR)
  53. include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
  54. select_library_configurations(JPEG)
  55. mark_as_advanced(JPEG_LIBRARY_RELEASE JPEG_LIBRARY_DEBUG)
  56. endif()
  57. unset(jpeg_names)
  58. unset(jpeg_names_debug)
  59. if(JPEG_INCLUDE_DIR)
  60. file(GLOB _JPEG_CONFIG_HEADERS_FEDORA "${JPEG_INCLUDE_DIR}/jconfig*.h")
  61. file(GLOB _JPEG_CONFIG_HEADERS_DEBIAN "${JPEG_INCLUDE_DIR}/*/jconfig.h")
  62. set(_JPEG_CONFIG_HEADERS
  63. "${JPEG_INCLUDE_DIR}/jpeglib.h"
  64. ${_JPEG_CONFIG_HEADERS_FEDORA}
  65. ${_JPEG_CONFIG_HEADERS_DEBIAN})
  66. foreach (_JPEG_CONFIG_HEADER IN LISTS _JPEG_CONFIG_HEADERS)
  67. if (NOT EXISTS "${_JPEG_CONFIG_HEADER}")
  68. continue ()
  69. endif ()
  70. file(STRINGS "${_JPEG_CONFIG_HEADER}"
  71. jpeg_lib_version REGEX "^#define[\t ]+JPEG_LIB_VERSION[\t ]+.*")
  72. if (NOT jpeg_lib_version)
  73. continue ()
  74. endif ()
  75. string(REGEX REPLACE "^#define[\t ]+JPEG_LIB_VERSION[\t ]+([0-9]+).*"
  76. "\\1" JPEG_VERSION "${jpeg_lib_version}")
  77. break ()
  78. endforeach ()
  79. unset(jpeg_lib_version)
  80. unset(_JPEG_CONFIG_HEADER)
  81. unset(_JPEG_CONFIG_HEADERS)
  82. unset(_JPEG_CONFIG_HEADERS_FEDORA)
  83. unset(_JPEG_CONFIG_HEADERS_DEBIAN)
  84. endif()
  85. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  86. find_package_handle_standard_args(JPEG
  87. REQUIRED_VARS JPEG_LIBRARY JPEG_INCLUDE_DIR
  88. VERSION_VAR JPEG_VERSION)
  89. if(JPEG_FOUND)
  90. set(JPEG_LIBRARIES ${JPEG_LIBRARY})
  91. set(JPEG_INCLUDE_DIRS "${JPEG_INCLUDE_DIR}")
  92. if(NOT TARGET JPEG::JPEG)
  93. add_library(JPEG::JPEG UNKNOWN IMPORTED)
  94. if(JPEG_INCLUDE_DIRS)
  95. set_target_properties(JPEG::JPEG PROPERTIES
  96. INTERFACE_INCLUDE_DIRECTORIES "${JPEG_INCLUDE_DIRS}")
  97. endif()
  98. if(EXISTS "${JPEG_LIBRARY}")
  99. set_target_properties(JPEG::JPEG PROPERTIES
  100. IMPORTED_LINK_INTERFACE_LANGUAGES "C"
  101. IMPORTED_LOCATION "${JPEG_LIBRARY}")
  102. endif()
  103. if(EXISTS "${JPEG_LIBRARY_RELEASE}")
  104. set_property(TARGET JPEG::JPEG APPEND PROPERTY
  105. IMPORTED_CONFIGURATIONS RELEASE)
  106. set_target_properties(JPEG::JPEG PROPERTIES
  107. IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "C"
  108. IMPORTED_LOCATION_RELEASE "${JPEG_LIBRARY_RELEASE}")
  109. endif()
  110. if(EXISTS "${JPEG_LIBRARY_DEBUG}")
  111. set_property(TARGET JPEG::JPEG APPEND PROPERTY
  112. IMPORTED_CONFIGURATIONS DEBUG)
  113. set_target_properties(JPEG::JPEG PROPERTIES
  114. IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "C"
  115. IMPORTED_LOCATION_DEBUG "${JPEG_LIBRARY_DEBUG}")
  116. endif()
  117. endif()
  118. endif()
  119. mark_as_advanced(JPEG_LIBRARY JPEG_INCLUDE_DIR)
  120. cmake_policy(POP)