FindGflags.cmake 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. # Ceres Solver - A fast non-linear least squares minimizer
  2. # Copyright 2015 Google Inc. All rights reserved.
  3. # http://ceres-solver.org/
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions are met:
  7. #
  8. # * Redistributions of source code must retain the above copyright notice,
  9. # this list of conditions and the following disclaimer.
  10. # * Redistributions in binary form must reproduce the above copyright notice,
  11. # this list of conditions and the following disclaimer in the documentation
  12. # and/or other materials provided with the distribution.
  13. # * Neither the name of Google Inc. nor the names of its contributors may be
  14. # used to endorse or promote products derived from this software without
  15. # specific prior written permission.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. # POSSIBILITY OF SUCH DAMAGE.
  28. #
  29. # Author: alexs.mac@gmail.com (Alex Stewart)
  30. #
  31. # FindGflags.cmake - Find Google gflags logging library.
  32. #
  33. # This module will attempt to find gflags, either via an exported CMake
  34. # configuration (generated by gflags >= 2.1 which are built with CMake), or
  35. # by performing a standard search for all gflags components. The order of
  36. # precedence for these two methods of finding gflags is controlled by:
  37. # GFLAGS_PREFER_EXPORTED_GFLAGS_CMAKE_CONFIGURATION.
  38. #
  39. # This module defines the following variables:
  40. #
  41. # GFLAGS_FOUND: TRUE iff gflags is found.
  42. # GFLAGS_INCLUDE_DIRS: Include directories for gflags.
  43. # GFLAGS_LIBRARIES: Libraries required to link gflags.
  44. # GFLAGS_NAMESPACE: The namespace in which gflags is defined. In versions of
  45. # gflags < 2.1, this was google, for versions >= 2.1 it is
  46. # by default gflags, although can be configured when building
  47. # gflags to be something else (i.e. google for legacy
  48. # compatibility).
  49. #
  50. # The following variables control the behaviour of this module when an exported
  51. # gflags CMake configuration is not found.
  52. #
  53. # GFLAGS_PREFER_EXPORTED_GFLAGS_CMAKE_CONFIGURATION: TRUE/FALSE, iff TRUE then
  54. # then prefer using an exported CMake configuration
  55. # generated by gflags >= 2.1 over searching for the
  56. # gflags components manually. Otherwise (FALSE)
  57. # ignore any exported gflags CMake configurations and
  58. # always perform a manual search for the components.
  59. # Default: TRUE iff user does not define this variable
  60. # before we are called, and does NOT specify either
  61. # GFLAGS_INCLUDE_DIR_HINTS or GFLAGS_LIBRARY_DIR_HINTS
  62. # otherwise FALSE.
  63. # GFLAGS_INCLUDE_DIR_HINTS: List of additional directories in which to
  64. # search for gflags includes, e.g: /timbuktu/include.
  65. # GFLAGS_LIBRARY_DIR_HINTS: List of additional directories in which to
  66. # search for gflags libraries, e.g: /timbuktu/lib.
  67. #
  68. # The following variables are also defined by this module, but in line with
  69. # CMake recommended FindPackage() module style should NOT be referenced directly
  70. # by callers (use the plural variables detailed above instead). These variables
  71. # do however affect the behaviour of the module via FIND_[PATH/LIBRARY]() which
  72. # are NOT re-called (i.e. search for library is not repeated) if these variables
  73. # are set with valid values _in the CMake cache_. This means that if these
  74. # variables are set directly in the cache, either by the user in the CMake GUI,
  75. # or by the user passing -DVAR=VALUE directives to CMake when called (which
  76. # explicitly defines a cache variable), then they will be used verbatim,
  77. # bypassing the HINTS variables and other hard-coded search locations.
  78. #
  79. # GFLAGS_INCLUDE_DIR: Include directory for gflags, not including the
  80. # include directory of any dependencies.
  81. # GFLAGS_LIBRARY: gflags library, not including the libraries of any
  82. # dependencies.
  83. # Reset CALLERS_CMAKE_FIND_LIBRARY_PREFIXES to its value when FindGflags was
  84. # invoked, necessary for MSVC.
  85. macro(GFLAGS_RESET_FIND_LIBRARY_PREFIX)
  86. if (MSVC)
  87. set(CMAKE_FIND_LIBRARY_PREFIXES "${CALLERS_CMAKE_FIND_LIBRARY_PREFIXES}")
  88. endif (MSVC)
  89. endmacro(GFLAGS_RESET_FIND_LIBRARY_PREFIX)
  90. # Called if we failed to find gflags or any of it's required dependencies,
  91. # unsets all public (designed to be used externally) variables and reports
  92. # error message at priority depending upon [REQUIRED/QUIET/<NONE>] argument.
  93. macro(GFLAGS_REPORT_NOT_FOUND REASON_MSG)
  94. unset(GFLAGS_FOUND)
  95. unset(GFLAGS_INCLUDE_DIRS)
  96. unset(GFLAGS_LIBRARIES)
  97. # Do not use unset, as we want to keep GFLAGS_NAMESPACE in the cache,
  98. # but simply clear its value.
  99. set(GFLAGS_NAMESPACE "" CACHE STRING
  100. "gflags namespace (google or gflags)" FORCE)
  101. # Make results of search visible in the CMake GUI if gflags has not
  102. # been found so that user does not have to toggle to advanced view.
  103. mark_as_advanced(CLEAR GFLAGS_INCLUDE_DIR
  104. GFLAGS_LIBRARY
  105. GFLAGS_NAMESPACE)
  106. gflags_reset_find_library_prefix()
  107. # Note <package>_FIND_[REQUIRED/QUIETLY] variables defined by FindPackage()
  108. # use the camelcase library name, not uppercase.
  109. if (Gflags_FIND_QUIETLY)
  110. message(STATUS "Failed to find gflags - " ${REASON_MSG} ${ARGN})
  111. elseif (Gflags_FIND_REQUIRED)
  112. message(FATAL_ERROR "Failed to find gflags - " ${REASON_MSG} ${ARGN})
  113. else()
  114. # Neither QUIETLY nor REQUIRED, use no priority which emits a message
  115. # but continues configuration and allows generation.
  116. message("-- Failed to find gflags - " ${REASON_MSG} ${ARGN})
  117. endif ()
  118. return()
  119. endmacro(GFLAGS_REPORT_NOT_FOUND)
  120. # Verify that all variable names passed as arguments are defined (can be empty
  121. # but must be defined) or raise a fatal error.
  122. macro(GFLAGS_CHECK_VARS_DEFINED)
  123. foreach(CHECK_VAR ${ARGN})
  124. if (NOT DEFINED ${CHECK_VAR})
  125. if(NOT Gflags_FIND_REQUIRED)
  126. gflags_report_not_found("Ceres Bug: ${CHECK_VAR} is not defined.")
  127. else()
  128. message(FATAL_ERROR "Ceres Bug: ${CHECK_VAR} is not defined.")
  129. endif()
  130. endif()
  131. endforeach()
  132. endmacro(GFLAGS_CHECK_VARS_DEFINED)
  133. # Use check_cxx_source_compiles() to compile trivial test programs to determine
  134. # the gflags namespace. This works on all OSs except Windows. If using Visual
  135. # Studio, it fails because msbuild forces check_cxx_source_compiles() to use
  136. # CMAKE_BUILD_TYPE=Debug for the test project, which usually breaks detection
  137. # because MSVC requires that the test project use the same build type as gflags,
  138. # which would normally be built in Release.
  139. #
  140. # Defines: GFLAGS_NAMESPACE in the caller's scope with the detected namespace,
  141. # which is blank (empty string, will test FALSE is CMake conditionals)
  142. # if detection failed.
  143. function(GFLAGS_CHECK_GFLAGS_NAMESPACE_USING_TRY_COMPILE)
  144. # Verify that all required variables are defined.
  145. gflags_check_vars_defined(
  146. GFLAGS_INCLUDE_DIR GFLAGS_LIBRARY)
  147. # Ensure that GFLAGS_NAMESPACE is always unset on completion unless
  148. # we explicitly set if after having the correct namespace.
  149. set(GFLAGS_NAMESPACE "" PARENT_SCOPE)
  150. include(CheckCXXSourceCompiles)
  151. # Setup include path & link library for gflags for CHECK_CXX_SOURCE_COMPILES.
  152. set(CMAKE_REQUIRED_INCLUDES ${GFLAGS_INCLUDE_DIR})
  153. set(CMAKE_REQUIRED_LIBRARIES ${GFLAGS_LIBRARY} ${GFLAGS_LINK_LIBRARIES})
  154. # First try the (older) google namespace. Note that the output variable
  155. # MUST be unique to the build type as otherwise the test is not repeated as
  156. # it is assumed to have already been performed.
  157. check_cxx_source_compiles(
  158. "#include <gflags/gflags.h>
  159. int main(int argc, char * argv[]) {
  160. google::ParseCommandLineFlags(&argc, &argv, true);
  161. return 0;
  162. }"
  163. GFLAGS_IN_GOOGLE_NAMESPACE)
  164. if (GFLAGS_IN_GOOGLE_NAMESPACE)
  165. set(GFLAGS_NAMESPACE google PARENT_SCOPE)
  166. return()
  167. endif()
  168. # Try (newer) gflags namespace instead. Note that the output variable
  169. # MUST be unique to the build type as otherwise the test is not repeated as
  170. # it is assumed to have already been performed.
  171. set(CMAKE_REQUIRED_INCLUDES ${GFLAGS_INCLUDE_DIR})
  172. set(CMAKE_REQUIRED_LIBRARIES ${GFLAGS_LIBRARY} ${GFLAGS_LINK_LIBRARIES})
  173. check_cxx_source_compiles(
  174. "#include <gflags/gflags.h>
  175. int main(int argc, char * argv[]) {
  176. gflags::ParseCommandLineFlags(&argc, &argv, true);
  177. return 0;
  178. }"
  179. GFLAGS_IN_GFLAGS_NAMESPACE)
  180. if (GFLAGS_IN_GFLAGS_NAMESPACE)
  181. set(GFLAGS_NAMESPACE gflags PARENT_SCOPE)
  182. return()
  183. endif (GFLAGS_IN_GFLAGS_NAMESPACE)
  184. endfunction(GFLAGS_CHECK_GFLAGS_NAMESPACE_USING_TRY_COMPILE)
  185. # Use regex on the gflags headers to attempt to determine the gflags namespace.
  186. # Checks both gflags.h (contained namespace on versions < 2.1.2) and
  187. # gflags_declare.h, which contains the namespace on versions >= 2.1.2.
  188. # In general, this method should only be used when
  189. # GFLAGS_CHECK_GFLAGS_NAMESPACE_USING_TRY_COMPILE() cannot be used, or has
  190. # failed.
  191. #
  192. # Defines: GFLAGS_NAMESPACE in the caller's scope with the detected namespace,
  193. # which is blank (empty string, will test FALSE is CMake conditionals)
  194. # if detection failed.
  195. function(GFLAGS_CHECK_GFLAGS_NAMESPACE_USING_REGEX)
  196. # Verify that all required variables are defined.
  197. gflags_check_vars_defined(GFLAGS_INCLUDE_DIR)
  198. # Ensure that GFLAGS_NAMESPACE is always undefined on completion unless
  199. # we explicitly set if after having the correct namespace.
  200. set(GFLAGS_NAMESPACE "" PARENT_SCOPE)
  201. # Scan gflags.h to identify what namespace gflags was built with. On
  202. # versions of gflags < 2.1.2, gflags.h was configured with the namespace
  203. # directly, on >= 2.1.2, gflags.h uses the GFLAGS_NAMESPACE #define which
  204. # is defined in gflags_declare.h, we try each location in turn.
  205. set(GFLAGS_HEADER_FILE ${GFLAGS_INCLUDE_DIR}/gflags/gflags.h)
  206. if (NOT EXISTS ${GFLAGS_HEADER_FILE})
  207. gflags_report_not_found(
  208. "Could not find file: ${GFLAGS_HEADER_FILE} "
  209. "containing namespace information in gflags install located at: "
  210. "${GFLAGS_INCLUDE_DIR}.")
  211. endif()
  212. file(READ ${GFLAGS_HEADER_FILE} GFLAGS_HEADER_FILE_CONTENTS)
  213. string(REGEX MATCH "namespace [A-Za-z]+"
  214. GFLAGS_NAMESPACE "${GFLAGS_HEADER_FILE_CONTENTS}")
  215. string(REGEX REPLACE "namespace ([A-Za-z]+)" "\\1"
  216. GFLAGS_NAMESPACE "${GFLAGS_NAMESPACE}")
  217. if (NOT GFLAGS_NAMESPACE)
  218. gflags_report_not_found(
  219. "Failed to extract gflags namespace from header file: "
  220. "${GFLAGS_HEADER_FILE}.")
  221. endif (NOT GFLAGS_NAMESPACE)
  222. if (GFLAGS_NAMESPACE STREQUAL "google" OR
  223. GFLAGS_NAMESPACE STREQUAL "gflags")
  224. # Found valid gflags namespace from gflags.h.
  225. set(GFLAGS_NAMESPACE "${GFLAGS_NAMESPACE}" PARENT_SCOPE)
  226. return()
  227. endif()
  228. # Failed to find gflags namespace from gflags.h, gflags is likely a new
  229. # version, check gflags_declare.h, which in newer versions (>= 2.1.2) contains
  230. # the GFLAGS_NAMESPACE #define, which is then referenced in gflags.h.
  231. set(GFLAGS_DECLARE_FILE ${GFLAGS_INCLUDE_DIR}/gflags/gflags_declare.h)
  232. if (NOT EXISTS ${GFLAGS_DECLARE_FILE})
  233. gflags_report_not_found(
  234. "Could not find file: ${GFLAGS_DECLARE_FILE} "
  235. "containing namespace information in gflags install located at: "
  236. "${GFLAGS_INCLUDE_DIR}.")
  237. endif()
  238. file(READ ${GFLAGS_DECLARE_FILE} GFLAGS_DECLARE_FILE_CONTENTS)
  239. string(REGEX MATCH "#define GFLAGS_NAMESPACE [A-Za-z]+"
  240. GFLAGS_NAMESPACE "${GFLAGS_DECLARE_FILE_CONTENTS}")
  241. string(REGEX REPLACE "#define GFLAGS_NAMESPACE ([A-Za-z]+)" "\\1"
  242. GFLAGS_NAMESPACE "${GFLAGS_NAMESPACE}")
  243. if (NOT GFLAGS_NAMESPACE)
  244. gflags_report_not_found(
  245. "Failed to extract gflags namespace from declare file: "
  246. "${GFLAGS_DECLARE_FILE}.")
  247. endif (NOT GFLAGS_NAMESPACE)
  248. if (GFLAGS_NAMESPACE STREQUAL "google" OR
  249. GFLAGS_NAMESPACE STREQUAL "gflags")
  250. # Found valid gflags namespace from gflags.h.
  251. set(GFLAGS_NAMESPACE "${GFLAGS_NAMESPACE}" PARENT_SCOPE)
  252. return()
  253. endif()
  254. endfunction(GFLAGS_CHECK_GFLAGS_NAMESPACE_USING_REGEX)
  255. # -----------------------------------------------------------------
  256. # By default, if the user has expressed no preference for using an exported
  257. # gflags CMake configuration over performing a search for the installed
  258. # components, and has not specified any hints for the search locations, then
  259. # prefer a gflags exported configuration if available.
  260. if (NOT DEFINED GFLAGS_PREFER_EXPORTED_GFLAGS_CMAKE_CONFIGURATION
  261. AND NOT GFLAGS_INCLUDE_DIR_HINTS
  262. AND NOT GFLAGS_LIBRARY_DIR_HINTS)
  263. message(STATUS "No preference for use of exported gflags CMake configuration "
  264. "set, and no hints for include/library directories provided. "
  265. "Defaulting to preferring an installed/exported gflags CMake configuration "
  266. "if available.")
  267. set(GFLAGS_PREFER_EXPORTED_GFLAGS_CMAKE_CONFIGURATION TRUE)
  268. endif()
  269. # Wrap into function because gflags_report_not_found() uses "return" statement
  270. function(__find_exported_gflags)
  271. # Try to find an exported CMake configuration for gflags, as generated by
  272. # gflags versions >= 2.1.
  273. #
  274. # We search twice, s/t we can invert the ordering of precedence used by
  275. # find_package() for exported package build directories, and installed
  276. # packages (found via CMAKE_SYSTEM_PREFIX_PATH), listed as items 6) and 7)
  277. # respectively in [1].
  278. #
  279. # By default, exported build directories are (in theory) detected first, and
  280. # this is usually the case on Windows. However, on OS X & Linux, the install
  281. # path (/usr/local) is typically present in the PATH environment variable
  282. # which is checked in item 4) in [1] (i.e. before both of the above, unless
  283. # NO_SYSTEM_ENVIRONMENT_PATH is passed). As such on those OSs installed
  284. # packages are usually detected in preference to exported package build
  285. # directories.
  286. #
  287. # To ensure a more consistent response across all OSs, and as users usually
  288. # want to prefer an installed version of a package over a locally built one
  289. # where both exist (esp. as the exported build directory might be removed
  290. # after installation), we first search with NO_CMAKE_PACKAGE_REGISTRY which
  291. # means any build directories exported by the user are ignored, and thus
  292. # installed directories are preferred. If this fails to find the package
  293. # we then research again, but without NO_CMAKE_PACKAGE_REGISTRY, so any
  294. # exported build directories will now be detected.
  295. #
  296. # To prevent confusion on Windows, we also pass NO_CMAKE_BUILDS_PATH (which
  297. # is item 5) in [1]), to not preferentially use projects that were built
  298. # recently with the CMake GUI to ensure that we always prefer an installed
  299. # version if available.
  300. #
  301. # [1] http://www.cmake.org/cmake/help/v2.8.11/cmake.html#command:find_package
  302. find_package(gflags QUIET
  303. NO_MODULE
  304. NO_CMAKE_PACKAGE_REGISTRY
  305. NO_CMAKE_BUILDS_PATH)
  306. if (gflags_FOUND)
  307. message(STATUS "Found installed version of gflags: ${gflags_DIR}")
  308. else(gflags_FOUND)
  309. # Failed to find an installed version of gflags, repeat search allowing
  310. # exported build directories.
  311. message(STATUS "Failed to find installed gflags CMake configuration, "
  312. "searching for gflags build directories exported with CMake.")
  313. # Again pass NO_CMAKE_BUILDS_PATH, as we know that gflags is exported and
  314. # do not want to treat projects built with the CMake GUI preferentially.
  315. find_package(gflags QUIET
  316. NO_MODULE
  317. NO_CMAKE_BUILDS_PATH)
  318. if (gflags_FOUND)
  319. message(STATUS "Found exported gflags build directory: ${gflags_DIR}")
  320. endif(gflags_FOUND)
  321. endif(gflags_FOUND)
  322. set(FOUND_INSTALLED_GFLAGS_CMAKE_CONFIGURATION ${gflags_FOUND})
  323. # gflags v2.1 - 2.1.2 shipped with a bug in their gflags-config.cmake [1]
  324. # whereby gflags_LIBRARIES = "gflags", but there was no imported target
  325. # called "gflags", they were called: gflags[_nothreads]-[static/shared].
  326. # As this causes linker errors when gflags is not installed in a location
  327. # on the current library paths, detect if this problem is present and
  328. # fix it.
  329. #
  330. # [1] https://github.com/gflags/gflags/issues/110
  331. if (gflags_FOUND)
  332. # NOTE: This is not written as additional conditions in the outer
  333. # if (gflags_FOUND) as the NOT TARGET "${gflags_LIBRARIES}"
  334. # condition causes problems if gflags is not found.
  335. if (${gflags_VERSION} VERSION_LESS 2.1.3 AND
  336. NOT TARGET "${gflags_LIBRARIES}")
  337. message(STATUS "Detected broken gflags install in: ${gflags_DIR}, "
  338. "version: ${gflags_VERSION} <= 2.1.2 which defines gflags_LIBRARIES = "
  339. "${gflags_LIBRARIES} which is not an imported CMake target, see: "
  340. "https://github.com/gflags/gflags/issues/110. Attempting to fix by "
  341. "detecting correct gflags target.")
  342. # Ordering here expresses preference for detection, specifically we do not
  343. # want to use the _nothreads variants if the full library is available.
  344. list(APPEND CHECK_GFLAGS_IMPORTED_TARGET_NAMES
  345. gflags-shared gflags-static
  346. gflags_nothreads-shared gflags_nothreads-static)
  347. foreach(CHECK_GFLAGS_TARGET ${CHECK_GFLAGS_IMPORTED_TARGET_NAMES})
  348. if (TARGET ${CHECK_GFLAGS_TARGET})
  349. message(STATUS "Found valid gflags target: ${CHECK_GFLAGS_TARGET}, "
  350. "updating gflags_LIBRARIES.")
  351. set(gflags_LIBRARIES ${CHECK_GFLAGS_TARGET})
  352. break()
  353. endif()
  354. endforeach()
  355. if (NOT TARGET ${gflags_LIBRARIES})
  356. message(STATUS "Failed to fix detected broken gflags install in: "
  357. "${gflags_DIR}, version: ${gflags_VERSION} <= 2.1.2, none of the "
  358. "imported targets for gflags: ${CHECK_GFLAGS_IMPORTED_TARGET_NAMES} "
  359. "are defined. Will continue with a manual search for gflags "
  360. "components. We recommend you build/install a version of gflags > "
  361. "2.1.2 (or master).")
  362. set(FOUND_INSTALLED_GFLAGS_CMAKE_CONFIGURATION FALSE)
  363. endif()
  364. endif()
  365. endif()
  366. if (FOUND_INSTALLED_GFLAGS_CMAKE_CONFIGURATION)
  367. message(STATUS "Detected gflags version: ${gflags_VERSION}")
  368. if(NOT DEFINED GFLAGS_FOUND AND DEFINED gflags_FOUND)
  369. set(GFLAGS_FOUND ${gflags_FOUND})
  370. endif()
  371. if(NOT DEFINED GFLAGS_INCLUDE_DIR AND DEFINED gflags_INCLUDE_DIR)
  372. set(GFLAGS_INCLUDE_DIR ${gflags_INCLUDE_DIR})
  373. endif()
  374. if(NOT DEFINED GFLAGS_LIBRARY AND DEFINED gflags_LIBRARIES)
  375. set(GFLAGS_LIBRARY ${gflags_LIBRARIES})
  376. endif()
  377. if(NOT DEFINED GFLAGS_INCLUDE_DIR)
  378. gflags_report_not_found("GFLAGS_INCLUDE_DIR is missing")
  379. endif()
  380. # gflags does not export the namespace in their CMake configuration, so
  381. # use our function to determine what it should be, as it can be either
  382. # gflags or google dependent upon version & configuration.
  383. #
  384. # NOTE: We use the regex method to determine the namespace here, as
  385. # check_cxx_source_compiles() will not use imported targets, which
  386. # is what gflags will be in this case.
  387. gflags_check_gflags_namespace_using_regex()
  388. if (NOT GFLAGS_NAMESPACE)
  389. gflags_report_not_found(
  390. "Failed to determine gflags namespace using regex for gflags "
  391. "version: ${gflags_VERSION} exported here: ${gflags_DIR} using CMake.")
  392. endif (NOT GFLAGS_NAMESPACE)
  393. else (FOUND_INSTALLED_GFLAGS_CMAKE_CONFIGURATION)
  394. gflags_report_not_found("Failed to find an installed/exported CMake configuration "
  395. "for gflags, will perform search for installed gflags components.")
  396. endif (FOUND_INSTALLED_GFLAGS_CMAKE_CONFIGURATION)
  397. set(GFLAGS_FOUND "${GFLAGS_FOUND}" PARENT_SCOPE)
  398. set(GFLAGS_INCLUDE_DIR "${GFLAGS_INCLUDE_DIR}" PARENT_SCOPE)
  399. set(GFLAGS_LIBRARY "${GFLAGS_LIBRARY}" PARENT_SCOPE)
  400. set(GFLAGS_NAMESPACE "${GFLAGS_NAMESPACE}" PARENT_SCOPE)
  401. endfunction()
  402. if (GFLAGS_PREFER_EXPORTED_GFLAGS_CMAKE_CONFIGURATION)
  403. __find_exported_gflags()
  404. endif(GFLAGS_PREFER_EXPORTED_GFLAGS_CMAKE_CONFIGURATION)
  405. if (NOT GFLAGS_FOUND)
  406. # Either failed to find an exported gflags CMake configuration, or user
  407. # told us not to use one. Perform a manual search for all gflags components.
  408. # Handle possible presence of lib prefix for libraries on MSVC, see
  409. # also GFLAGS_RESET_FIND_LIBRARY_PREFIX().
  410. if (MSVC)
  411. # Preserve the caller's original values for CMAKE_FIND_LIBRARY_PREFIXES
  412. # s/t we can set it back before returning.
  413. set(CALLERS_CMAKE_FIND_LIBRARY_PREFIXES "${CMAKE_FIND_LIBRARY_PREFIXES}")
  414. # The empty string in this list is important, it represents the case when
  415. # the libraries have no prefix (shared libraries / DLLs).
  416. set(CMAKE_FIND_LIBRARY_PREFIXES "lib" "" "${CMAKE_FIND_LIBRARY_PREFIXES}")
  417. endif (MSVC)
  418. # Search user-installed locations first, so that we prefer user installs
  419. # to system installs where both exist.
  420. list(APPEND GFLAGS_CHECK_INCLUDE_DIRS
  421. /usr/local/include
  422. /usr/local/homebrew/include # Mac OS X
  423. /opt/local/var/macports/software # Mac OS X.
  424. /opt/local/include
  425. /usr/include)
  426. list(APPEND GFLAGS_CHECK_PATH_SUFFIXES
  427. gflags/include # Windows (for C:/Program Files prefix).
  428. gflags/Include ) # Windows (for C:/Program Files prefix).
  429. list(APPEND GFLAGS_CHECK_LIBRARY_DIRS
  430. /usr/local/lib
  431. /usr/local/homebrew/lib # Mac OS X.
  432. /opt/local/lib
  433. /usr/lib)
  434. list(APPEND GFLAGS_CHECK_LIBRARY_SUFFIXES
  435. gflags/lib # Windows (for C:/Program Files prefix).
  436. gflags/Lib ) # Windows (for C:/Program Files prefix).
  437. # Search supplied hint directories first if supplied.
  438. find_path(GFLAGS_INCLUDE_DIR
  439. NAMES gflags/gflags.h
  440. PATHS ${GFLAGS_INCLUDE_DIR_HINTS}
  441. ${GFLAGS_CHECK_INCLUDE_DIRS}
  442. PATH_SUFFIXES ${GFLAGS_CHECK_PATH_SUFFIXES})
  443. if (NOT GFLAGS_INCLUDE_DIR OR
  444. NOT EXISTS ${GFLAGS_INCLUDE_DIR})
  445. gflags_report_not_found(
  446. "Could not find gflags include directory, set GFLAGS_INCLUDE_DIR "
  447. "to directory containing gflags/gflags.h")
  448. endif (NOT GFLAGS_INCLUDE_DIR OR
  449. NOT EXISTS ${GFLAGS_INCLUDE_DIR})
  450. find_library(GFLAGS_LIBRARY NAMES gflags gflags_debug gflags_nothreads gflags_nothreads_debug
  451. PATHS ${GFLAGS_LIBRARY_DIR_HINTS}
  452. ${GFLAGS_CHECK_LIBRARY_DIRS}
  453. PATH_SUFFIXES ${GFLAGS_CHECK_LIBRARY_SUFFIXES})
  454. if (NOT GFLAGS_LIBRARY OR
  455. NOT EXISTS ${GFLAGS_LIBRARY})
  456. gflags_report_not_found(
  457. "Could not find gflags library, set GFLAGS_LIBRARY "
  458. "to full path to libgflags.")
  459. endif (NOT GFLAGS_LIBRARY OR
  460. NOT EXISTS ${GFLAGS_LIBRARY})
  461. # gflags typically requires a threading library (which is OS dependent), note
  462. # that this defines the CMAKE_THREAD_LIBS_INIT variable. If we are able to
  463. # detect threads, we assume that gflags requires it.
  464. find_package(Threads QUIET)
  465. set(GFLAGS_LINK_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
  466. # On Windows (including MinGW), the Shlwapi library is used by gflags if
  467. # available.
  468. if (WIN32)
  469. include(CheckIncludeFileCXX)
  470. check_include_file_cxx("shlwapi.h" HAVE_SHLWAPI)
  471. if (HAVE_SHLWAPI)
  472. list(APPEND GFLAGS_LINK_LIBRARIES shlwapi.lib)
  473. endif(HAVE_SHLWAPI)
  474. endif (WIN32)
  475. # Mark internally as found, then verify. GFLAGS_REPORT_NOT_FOUND() unsets
  476. # if called.
  477. set(GFLAGS_FOUND TRUE)
  478. # Identify what namespace gflags was built with.
  479. if (GFLAGS_INCLUDE_DIR AND NOT GFLAGS_NAMESPACE)
  480. # To handle Windows peculiarities / CMake bugs on MSVC we try two approaches
  481. # to detect the gflags namespace:
  482. #
  483. # 1) Try to use check_cxx_source_compiles() to compile a trivial program
  484. # with the two choices for the gflags namespace.
  485. #
  486. # 2) [In the event 1) fails] Use regex on the gflags headers to try to
  487. # determine the gflags namespace. Whilst this is less robust than 1),
  488. # it does avoid any interaction with msbuild.
  489. gflags_check_gflags_namespace_using_try_compile()
  490. if (NOT GFLAGS_NAMESPACE)
  491. # Failed to determine gflags namespace using check_cxx_source_compiles()
  492. # method, try and obtain it using regex on the gflags headers instead.
  493. message(STATUS "Failed to find gflags namespace using using "
  494. "check_cxx_source_compiles(), trying namespace regex instead, "
  495. "this is expected on Windows.")
  496. gflags_check_gflags_namespace_using_regex()
  497. if (NOT GFLAGS_NAMESPACE)
  498. gflags_report_not_found(
  499. "Failed to determine gflags namespace either by "
  500. "check_cxx_source_compiles(), or namespace regex.")
  501. endif (NOT GFLAGS_NAMESPACE)
  502. endif (NOT GFLAGS_NAMESPACE)
  503. endif (GFLAGS_INCLUDE_DIR AND NOT GFLAGS_NAMESPACE)
  504. # Make the GFLAGS_NAMESPACE a cache variable s/t the user can view it, and could
  505. # overwrite it in the CMake GUI.
  506. set(GFLAGS_NAMESPACE "${GFLAGS_NAMESPACE}" CACHE STRING
  507. "gflags namespace (google or gflags)" FORCE)
  508. # gflags does not seem to provide any record of the version in its
  509. # source tree, thus cannot extract version.
  510. # Catch case when caller has set GFLAGS_NAMESPACE in the cache / GUI
  511. # with an invalid value.
  512. if (GFLAGS_NAMESPACE AND
  513. NOT GFLAGS_NAMESPACE STREQUAL "google" AND
  514. NOT GFLAGS_NAMESPACE STREQUAL "gflags")
  515. gflags_report_not_found(
  516. "Caller defined GFLAGS_NAMESPACE:"
  517. " ${GFLAGS_NAMESPACE} is not valid, not google or gflags.")
  518. endif ()
  519. # Catch case when caller has set GFLAGS_INCLUDE_DIR in the cache / GUI and
  520. # thus FIND_[PATH/LIBRARY] are not called, but specified locations are
  521. # invalid, otherwise we would report the library as found.
  522. if (GFLAGS_INCLUDE_DIR AND
  523. NOT EXISTS ${GFLAGS_INCLUDE_DIR}/gflags/gflags.h)
  524. gflags_report_not_found(
  525. "Caller defined GFLAGS_INCLUDE_DIR:"
  526. " ${GFLAGS_INCLUDE_DIR} does not contain gflags/gflags.h header.")
  527. endif (GFLAGS_INCLUDE_DIR AND
  528. NOT EXISTS ${GFLAGS_INCLUDE_DIR}/gflags/gflags.h)
  529. # TODO: This regex for gflags library is pretty primitive, we use lowercase
  530. # for comparison to handle Windows using CamelCase library names, could
  531. # this check be better?
  532. string(TOLOWER "${GFLAGS_LIBRARY}" LOWERCASE_GFLAGS_LIBRARY)
  533. if (GFLAGS_LIBRARY AND
  534. NOT "${LOWERCASE_GFLAGS_LIBRARY}" MATCHES ".*gflags[^/]*")
  535. gflags_report_not_found(
  536. "Caller defined GFLAGS_LIBRARY: "
  537. "${GFLAGS_LIBRARY} does not match gflags.")
  538. endif (GFLAGS_LIBRARY AND
  539. NOT "${LOWERCASE_GFLAGS_LIBRARY}" MATCHES ".*gflags[^/]*")
  540. gflags_reset_find_library_prefix()
  541. endif(NOT GFLAGS_FOUND)
  542. # Set standard CMake FindPackage variables if found.
  543. if (GFLAGS_FOUND)
  544. set(GFLAGS_INCLUDE_DIRS ${GFLAGS_INCLUDE_DIR})
  545. set(GFLAGS_LIBRARIES ${GFLAGS_LIBRARY} ${GFLAGS_LINK_LIBRARIES})
  546. endif (GFLAGS_FOUND)
  547. # Handle REQUIRED / QUIET optional arguments.
  548. include(FindPackageHandleStandardArgs)
  549. find_package_handle_standard_args(Gflags DEFAULT_MSG
  550. GFLAGS_INCLUDE_DIRS GFLAGS_LIBRARIES GFLAGS_NAMESPACE)
  551. # Only mark internal variables as advanced if we found gflags, otherwise
  552. # leave them visible in the standard GUI for the user to set manually.
  553. if (GFLAGS_FOUND)
  554. mark_as_advanced(FORCE GFLAGS_INCLUDE_DIR
  555. GFLAGS_LIBRARY
  556. GFLAGS_NAMESPACE
  557. gflags_DIR) # Autogenerated by find_package(gflags)
  558. endif (GFLAGS_FOUND)