FindSQLite3.cmake 2.8 KB

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