FindGflags.cmake 27 KB

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