FindSQLite3.cmake 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. ``SQLite::SQLite3``
  16. Target encapsulating SQLite library usage requirements. It is available only
  17. when SQLite is found.
  18. Result Variables
  19. ^^^^^^^^^^^^^^^^
  20. This module defines the following variables:
  21. ``SQLite3_FOUND``
  22. Boolean indicating whether (the requested version of) SQLite library is
  23. found.
  24. ``SQLite3_VERSION``
  25. The version of SQLite library found.
  26. ``SQLite3_INCLUDE_DIRS``
  27. Include directories containing the ``<sqlite3.h>`` and related headers
  28. needed to use SQLite.
  29. ``SQLite3_LIBRARIES``
  30. Libraries needed to link against to use SQLite.
  31. Examples
  32. ^^^^^^^^
  33. Finding the SQLite library and linking it to a project target:
  34. .. code-block:: cmake
  35. find_package(SQLite3)
  36. target_link_libraries(project_target PRIVATE SQLite::SQLite3)
  37. #]=======================================================================]
  38. cmake_policy(PUSH)
  39. cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
  40. find_package(PkgConfig QUIET)
  41. if(PKG_CONFIG_FOUND)
  42. pkg_check_modules(PC_SQLite3 QUIET sqlite3)
  43. endif()
  44. # Look for the necessary header
  45. find_path(SQLite3_INCLUDE_DIR NAMES sqlite3.h
  46. HINTS
  47. ${PC_SQLite3_INCLUDE_DIRS}
  48. )
  49. mark_as_advanced(SQLite3_INCLUDE_DIR)
  50. # Look for the necessary library
  51. find_library(SQLite3_LIBRARY NAMES sqlite3 sqlite
  52. HINTS
  53. ${PC_SQLite3_LIBRARY_DIRS}
  54. )
  55. mark_as_advanced(SQLite3_LIBRARY)
  56. # Extract version information from the header file
  57. if(SQLite3_INCLUDE_DIR)
  58. file(STRINGS ${SQLite3_INCLUDE_DIR}/sqlite3.h _ver_line
  59. REGEX "^#define SQLITE_VERSION *\"[0-9]+\\.[0-9]+\\.[0-9]+\""
  60. LIMIT_COUNT 1)
  61. string(REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]+"
  62. SQLite3_VERSION "${_ver_line}")
  63. unset(_ver_line)
  64. endif()
  65. include(FindPackageHandleStandardArgs)
  66. find_package_handle_standard_args(SQLite3
  67. REQUIRED_VARS SQLite3_LIBRARY SQLite3_INCLUDE_DIR
  68. VERSION_VAR SQLite3_VERSION)
  69. # Create the imported target
  70. if(SQLite3_FOUND)
  71. set(SQLite3_INCLUDE_DIRS ${SQLite3_INCLUDE_DIR})
  72. set(SQLite3_LIBRARIES ${SQLite3_LIBRARY})
  73. if(NOT TARGET SQLite::SQLite3)
  74. add_library(SQLite::SQLite3 UNKNOWN IMPORTED)
  75. set_target_properties(SQLite::SQLite3 PROPERTIES
  76. IMPORTED_LOCATION "${SQLite3_LIBRARY}"
  77. INTERFACE_INCLUDE_DIRECTORIES "${SQLite3_INCLUDE_DIR}")
  78. endif()
  79. endif()
  80. cmake_policy(POP)