1
0

FindSQLite.cmake 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. # Find SQLite.
  2. #
  3. # Set this variable to any additional path you want the module to search:
  4. # SQLite_DIR
  5. #
  6. # Imported targets defined by this module:
  7. # SQLite::sqlite - shared library if available, or static if that's all there is
  8. # SQLite::sqlite_shared - shared library
  9. # SQLite::sqlite_static - static library
  10. #
  11. # Informational variables:
  12. # SQLite_FOUND - sqlite (or all requested components of sqlite) was found.
  13. # SQLite_VERSION - the version of sqlite that was found
  14. #
  15. include(CacheLog)
  16. include(FindPackageHandleStandardArgs)
  17. # If SQLite_DIR has changed since the last invocation, wipe internal cache variables so we can search for everything
  18. # again.
  19. if (NOT "${SQLite_DIR}" STREQUAL "${_internal_old_sqlite_dir}")
  20. unset_cachelog_entries()
  21. endif ()
  22. reset_cachelog()
  23. set(_old_suffixes "${CMAKE_FIND_LIBRARY_SUFFIXES}")
  24. find_package(PkgConfig QUIET)
  25. if (PKG_CONFIG_FOUND)
  26. PKG_CHECK_MODULES(PC_SQLite QUIET sqlite)
  27. endif ()
  28. if (CMAKE_HOST_WIN32)
  29. if (CMAKE_SIZEOF_VOID_P EQUAL 8)
  30. set(archdir "$ENV{ProgramFiles}")
  31. set(notarchdir "$ENV{ProgramFiles\(x86\)}")
  32. else ()
  33. set(archdir "$ENV{ProgramFiles\(x86\)}")
  34. set(notarchdir "$ENV{ProgramFiles}")
  35. endif ()
  36. set(_paths
  37. "${archdir}/sqlite"
  38. "${archdir}/sqlite3"
  39. "${notarchdir}/sqlite"
  40. "${notarchdir}/sqlite3"
  41. )
  42. else ()
  43. if (CMAKE_SIZEOF_VOID_P EQUAL 8)
  44. set(arch 64)
  45. else ()
  46. set(arch 32)
  47. endif ()
  48. set(_paths
  49. /usr/local/sqlite${arch}
  50. /usr/locla/sqlite
  51. /usr/local/sqlite3
  52. )
  53. endif ()
  54. find_path(SQLite_INCLUDE_DIR
  55. NAMES sqlite3.h
  56. HINTS ${SQLite_DIR}
  57. $ENV{SQLite_DIR}
  58. ${SQLITE_ROOT_DIR}
  59. ${PC_SQLite_INCLUDE_DIRS}
  60. PATHS ${_paths}
  61. PATH_SUFFIXES include
  62. )
  63. add_to_cachelog(SQLite_INCLUDE_DIR)
  64. if (SQLite_INCLUDE_DIR)
  65. # Calculate root directory of installation from include directory.
  66. string(REGEX REPLACE "(/)*include(/)*$" "" _root_dir "${SQLite_INCLUDE_DIR}")
  67. set(SQLite_DIR "${_root_dir}" CACHE PATH "Root directory of SQLite installation" FORCE)
  68. set(_internal_old_sqlite_dir "${SQLite_DIR}" CACHE INTERNAL "" FORCE)
  69. mark_as_advanced(FORCE SQLite_DIR)
  70. # Find version by parsing SQLite header.
  71. if (EXISTS "${SQLite_INCLUDE_DIR}/sqlite3.h")
  72. file(STRINGS "${SQLite_INCLUDE_DIR}/sqlite3.h" sqlite_version_str
  73. REGEX "^[ \t]*#[ \t]*define[\t ]+SQLITE_VERSION[ \t]+\".*\"")
  74. if (sqlite_version_str MATCHES "^[ \t]*#[ \t]*define[\t ]+SQLITE_VERSION[ \t]+\"([^\"]*)\"")
  75. set(SQLite_VERSION "${CMAKE_MATCH_1}")
  76. endif ()
  77. endif ()
  78. # Find static library.
  79. set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_STATIC_LIBRARY_SUFFIX})
  80. foreach(defpath "NO_DEFAULT_PATH" "")
  81. find_library(SQLite_STATIC_LIBRARY
  82. NAMES sqlite_static sqlite3_static sqlitestatic sqlite3static sqlite sqlite3
  83. NAMES_PER_DIR
  84. HINTS ${SQLite_DIR} ${PC_SQLite_LIBRARY_DIRS}
  85. PATH_SUFFIXES lib ""
  86. ${defpath}
  87. )
  88. if (SQLite_STATIC_LIBRARY)
  89. break()
  90. endif ()
  91. endforeach()
  92. add_to_cachelog(SQLite_STATIC_LIBRARY)
  93. if (SQLite_STATIC_LIBRARY)
  94. set(SQLite_sqlite_static_FOUND TRUE)
  95. set(SQLite_sqlite_FOUND TRUE)
  96. endif ()
  97. # Find shared library (will pick up static lib if shared not found).
  98. set(CMAKE_FIND_LIBRARY_SUFFIXES "${_old_suffixes}")
  99. foreach(defpath "NO_DEFAULT_PATH" "")
  100. find_library(SQLite_LIBRARY
  101. NAMES sqlite sqlite3 sqlite_satic sqlite3_static sqlitestatic sqlite3static
  102. NAMES_PER_DIR
  103. HINTS ${SQLite_DIR} ${PC_SQLite_LIBRARY_DIRS}
  104. PATH_SUFFIXES lib ""
  105. ${defpath}
  106. )
  107. if (SQLite_LIBRARY)
  108. break()
  109. endif ()
  110. endforeach()
  111. add_to_cachelog(SQLite_LIBRARY)
  112. if (SQLite_LIBRARY AND NOT SQLite_LIBRARY STREQUAL SQLite_STATIC_LIBRARY)
  113. set(SQLite_sqlite_shared_FOUND TRUE)
  114. set(SQLite_sqlite_FOUND TRUE)
  115. endif ()
  116. # Look for the DLL.
  117. if (WIN32)
  118. set(CMAKE_FIND_LIBRARY_SUFFIXES .dll)
  119. foreach(defpath "NO_DEFAULT_PATH" "")
  120. find_library(SQLite_DLL_LIBRARY
  121. NAMES sqlite sqlite3
  122. NAMES_PER_DIR
  123. HINTS ${SQLite_DIR}
  124. PATH_SUFFIXES bin lib ""
  125. ${defpath}
  126. )
  127. if (SQLite_DLL_LIBRARY)
  128. break()
  129. endif ()
  130. endforeach()
  131. add_to_cachelog(SQLite_DLL_LIBRARY)
  132. endif ()
  133. endif ()
  134. set(_reqs SQLite_INCLUDE_DIR)
  135. if (NOT SQLite_FIND_COMPONENTS) # If user didn't request any particular component explicitly:
  136. list(APPEND _reqs SQLite_LIBRARY) # Will contain shared lib, or static lib if no shared lib present
  137. endif ()
  138. find_package_handle_standard_args(SQLite
  139. REQUIRED_VARS ${_reqs}
  140. VERSION_VAR SQLite_VERSION
  141. HANDLE_COMPONENTS
  142. FAIL_MESSAGE "SQLite not found, try -DSQLite_DIR=<path>"
  143. )
  144. # Static library.
  145. if (SQLite_sqlite_static_FOUND AND NOT TARGET SQLite::sqlite_static)
  146. add_library(SQLite::sqlite_static STATIC IMPORTED)
  147. set_target_properties(SQLite::sqlite_static PROPERTIES
  148. IMPORTED_LOCATION "${SQLite_STATIC_LIBRARY}"
  149. IMPORTED_LINK_INTERFACE_LANGUAGES "C"
  150. INTERFACE_INCLUDE_DIRECTORIES "${SQLite_INCLUDE_DIR}"
  151. )
  152. set(_sqlite_any SQLite::sqlite_static)
  153. endif ()
  154. # Shared library.
  155. if (SQLite_sqlite_shared_FOUND AND NOT TARGET SQLite::sqlite_shared)
  156. add_library(SQLite::sqlite_shared SHARED IMPORTED)
  157. set_target_properties(SQLite::sqlite_shared PROPERTIES
  158. IMPORTED_LINK_INTERFACE_LANGUAGES "C"
  159. INTERFACE_INCLUDE_DIRECTORIES "${SQLite_INCLUDE_DIR}"
  160. )
  161. if (WIN32)
  162. set_target_properties(SQLite::sqlite_shared PROPERTIES
  163. IMPORTED_IMPLIB "${SQLite_LIBRARY}"
  164. )
  165. if (SQLite_DLL_LIBRARY)
  166. set_target_properties(SQLite::sqlite_shared PROPERTIES
  167. IMPORTED_LOCATION "${SQLite_DLL_LIBRARY}"
  168. )
  169. endif ()
  170. else ()
  171. set_target_properties(SQLite::sqlite_shared PROPERTIES
  172. IMPORTED_LOCATION "${SQLite_LIBRARY}"
  173. )
  174. endif ()
  175. set(_sqlite_any SQLite::sqlite_shared)
  176. endif ()
  177. # I-don't-care library (shared, or static if shared not available).
  178. if (SQLite_sqlite_FOUND AND NOT TARGET SQLite::sqlite)
  179. add_library(SQLite::sqlite INTERFACE IMPORTED)
  180. set_target_properties(SQLite::sqlite PROPERTIES
  181. INTERFACE_LINK_LIBRARIES ${_sqlite_any}
  182. )
  183. endif ()
  184. set(CMAKE_FIND_LIBRARY_SUFFIXES "${_old_suffixes}")
  185. # From https://github.com/Monetra/mstdlib/blob/master/CMakeModules
  186. # The MIT License (MIT)
  187. # Copyright (c) 2015-2017 Main Street Softworks, Inc.
  188. # Permission is hereby granted, free of charge, to any person obtaining a copy
  189. # of this software and associated documentation files (the "Software"), to deal
  190. # in the Software without restriction, including without limitation the rights
  191. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  192. # copies of the Software, and to permit persons to whom the Software is
  193. # furnished to do so, subject to the following conditions:
  194. # The above copyright notice and this permission notice shall be included in
  195. # all copies or substantial portions of the Software.
  196. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  197. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  198. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  199. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  200. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  201. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  202. # THE SOFTWARE.