FindSQLite3.cmake 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file LICENSE.rst or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. FindSQLite3
  5. -----------
  6. .. versionadded:: 3.14
  7. Finds the SQLite 3 library:
  8. .. code-block:: cmake
  9. find_package(SQLite3 [<version>] [...])
  10. SQLite is a small, fast, self-contained, high-reliability, and full-featured
  11. SQL database engine written in C, intended for embedding in applications.
  12. Imported Targets
  13. ^^^^^^^^^^^^^^^^
  14. This module provides the following :ref:`Imported Targets`:
  15. ``SQLite3::SQLite3``
  16. Target encapsulating SQLite library usage requirements. It is available only
  17. when SQLite is found.
  18. .. versionadded:: 4.3
  19. ``SQLite::SQLite3``
  20. Deprecated. Identical to ``SQLite3::SQLite3``.
  21. If your project needs to support CMake < 4.3, consider adding the following
  22. to your project after calling ``find_package(SQLite3 ...)``:
  23. .. code-block:: cmake
  24. if(NOT TARGET SQLite3::SQLite3) # CMake < 4.3
  25. add_library(SQLite3::SQLite3 ALIAS SQLite::SQLite3)
  26. endif()
  27. This will allow your project to use the new name while still permitting it to
  28. compile with older versions of CMake.
  29. Result Variables
  30. ^^^^^^^^^^^^^^^^
  31. This module defines the following variables:
  32. ``SQLite3_FOUND``
  33. Boolean indicating whether the (requested version of) SQLite library was
  34. found.
  35. ``SQLite3_VERSION``
  36. The version of SQLite library found.
  37. ``SQLite3_INCLUDE_DIRS``
  38. Include directories containing the ``<sqlite3.h>`` and related headers
  39. needed to use SQLite.
  40. ``SQLite3_LIBRARIES``
  41. Libraries needed to link against to use SQLite.
  42. Examples
  43. ^^^^^^^^
  44. Finding the SQLite library and linking it to a project target:
  45. .. code-block:: cmake
  46. find_package(SQLite3)
  47. target_link_libraries(project_target PRIVATE SQLite::SQLite3)
  48. #]=======================================================================]
  49. cmake_policy(PUSH)
  50. cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
  51. find_package(PkgConfig QUIET)
  52. if(PkgConfig_FOUND)
  53. pkg_check_modules(PC_SQLite3 QUIET sqlite3)
  54. endif()
  55. # Look for the necessary header
  56. find_path(SQLite3_INCLUDE_DIR NAMES sqlite3.h
  57. HINTS
  58. ${PC_SQLite3_INCLUDE_DIRS}
  59. )
  60. mark_as_advanced(SQLite3_INCLUDE_DIR)
  61. # Look for the necessary library
  62. find_library(SQLite3_LIBRARY NAMES sqlite3 sqlite
  63. HINTS
  64. ${PC_SQLite3_LIBRARY_DIRS}
  65. )
  66. mark_as_advanced(SQLite3_LIBRARY)
  67. # Extract version information from the header file
  68. if(SQLite3_INCLUDE_DIR)
  69. file(STRINGS ${SQLite3_INCLUDE_DIR}/sqlite3.h _ver_line
  70. REGEX "^#define SQLITE_VERSION *\"[0-9]+\\.[0-9]+\\.[0-9]+\""
  71. LIMIT_COUNT 1)
  72. string(REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]+"
  73. SQLite3_VERSION "${_ver_line}")
  74. unset(_ver_line)
  75. endif()
  76. include(FindPackageHandleStandardArgs)
  77. find_package_handle_standard_args(SQLite3
  78. REQUIRED_VARS SQLite3_LIBRARY SQLite3_INCLUDE_DIR
  79. VERSION_VAR SQLite3_VERSION)
  80. # Create the imported target
  81. if(SQLite3_FOUND)
  82. set(SQLite3_INCLUDE_DIRS ${SQLite3_INCLUDE_DIR})
  83. set(SQLite3_LIBRARIES ${SQLite3_LIBRARY})
  84. if(NOT TARGET SQLite3::SQLite3)
  85. add_library(SQLite3::SQLite3 UNKNOWN IMPORTED)
  86. set_target_properties(SQLite3::SQLite3 PROPERTIES
  87. IMPORTED_LOCATION "${SQLite3_LIBRARY}"
  88. INTERFACE_INCLUDE_DIRECTORIES "${SQLite3_INCLUDE_DIR}")
  89. endif()
  90. if(NOT TARGET SQLite::SQLite3)
  91. add_library(SQLite::SQLite3 UNKNOWN IMPORTED)
  92. set_target_properties(SQLite::SQLite3 PROPERTIES
  93. IMPORTED_LOCATION "${SQLite3_LIBRARY}"
  94. INTERFACE_INCLUDE_DIRECTORIES "${SQLite3_INCLUDE_DIR}"
  95. DEPRECATION "The target name SQLite::SQLite3 is deprecated. Please use SQLite3::SQLite3 instead.")
  96. endif()
  97. endif()
  98. cmake_policy(POP)