|
| 1 | +# Author: sum01 <[email protected]> |
| 2 | +# Git: https://github.com/sum01/FindBerkeleyDB |
| 3 | +# Read the README.md for the full info. |
| 4 | + |
| 5 | +# Allow user to pass a path instead of guessing |
| 6 | +if(BerkeleyDB_ROOT_DIR) |
| 7 | + set(_BERKELEYDB_PATHS "${BerkeleyDB_ROOT_DIR}") |
| 8 | +elseif(CMAKE_SYSTEM_NAME MATCHES ".*[wW]indows.*") |
| 9 | + # MATCHES is used to work on any devies with windows in the name |
| 10 | + # Shameless copy-paste from FindOpenSSL.cmake v3.8 |
| 11 | + file(TO_CMAKE_PATH "$ENV{PROGRAMFILES}" _programfiles) |
| 12 | + list(APPEND _BERKELEYDB_HINTS "${_programfiles}") |
| 13 | + |
| 14 | + # There's actually production release and version numbers in the file path. |
| 15 | + # For example, if they're on v6.2.32: C:/Program Files/Oracle/Berkeley DB 12cR1 6.2.32/ |
| 16 | + # But this still works to find it, so I'm guessing it can accept partial path matches. |
| 17 | + |
| 18 | + foreach(_TARGET_BERKELEYDB_PATH "Oracle/Berkeley DB" "Berkeley DB") |
| 19 | + list(APPEND _BERKELEYDB_PATHS |
| 20 | + "${_programfiles}/${_TARGET_BERKELEYDB_PATH}" |
| 21 | + "C:/Program Files (x86)/${_TARGET_BERKELEYDB_PATH}" |
| 22 | + "C:/Program Files/${_TARGET_BERKELEYDB_PATH}" |
| 23 | + "C:/${_TARGET_BERKELEYDB_PATH}" |
| 24 | + ) |
| 25 | + endforeach() |
| 26 | +else() |
| 27 | + # Paths for anything other than Windows |
| 28 | + # Cellar/berkeley-db is for macOS from homebrew installation |
| 29 | + list(APPEND _BERKELEYDB_PATHS |
| 30 | + "/usr/local/Cellar/berkeley-db@4" |
| 31 | + "/usr/local/Cellar/berkeley-db" |
| 32 | + "/opt" |
| 33 | + "/opt/local" |
| 34 | + "/usr/local" |
| 35 | + ) |
| 36 | +endif() |
| 37 | + |
| 38 | +# Find includes path |
| 39 | +find_path(BerkeleyDB_INCLUDE_DIRS |
| 40 | + db_cxx.h |
| 41 | + PATHS "${_BERKELEYDB_PATHS}" |
| 42 | + PATH_SUFFIXES "include" "includes" |
| 43 | + ) |
| 44 | + |
| 45 | +# Checks if the version file exists, save the version file to a var, and fail if there's no version file |
| 46 | +if(BerkeleyDB_INCLUDE_DIRS) |
| 47 | + # Read the version file db.h into a variable |
| 48 | + file(READ "${BerkeleyDB_INCLUDE_DIRS}/db.h" _BERKELEYDB_DB_HEADER) |
| 49 | + # Parse the DB version into variables to be used in the lib names |
| 50 | + string(REGEX REPLACE ".*DB_VERSION_MAJOR ([0-9]+).*" "\\1" BerkeleyDB_VERSION_MAJOR "${_BERKELEYDB_DB_HEADER}") |
| 51 | + string(REGEX REPLACE ".*DB_VERSION_MINOR ([0-9]+).*" "\\1" BerkeleyDB_VERSION_MINOR "${_BERKELEYDB_DB_HEADER}") |
| 52 | + # Patch version example on non-crypto installs: x.x.xNC |
| 53 | + string(REGEX REPLACE ".*DB_VERSION_PATCH ([0-9]+(NC)?).*" "\\1" BerkeleyDB_VERSION_PATCH "${_BERKELEYDB_DB_HEADER}") |
| 54 | +else() |
| 55 | + if(BerkeleyDB_FIND_REQUIRED) |
| 56 | + # If the find_package(BerkeleyDB REQUIRED) was used, fail since we couldn't find the header |
| 57 | + message(FATAL_ERROR "Failed to find Berkeley DB's header file \"db.h\"! Try setting \"BerkeleyDB_ROOT_DIR\" when initiating Cmake.") |
| 58 | + elseif(NOT BerkeleyDB_FIND_QUIETLY) |
| 59 | + message(WARNING "Failed to find Berkeley DB's header file \"db.h\"! Try setting \"BerkeleyDB_ROOT_DIR\" when initiating Cmake.") |
| 60 | + endif() |
| 61 | + # Set some garbage values to the versions since we didn't find a file to read |
| 62 | + set(BerkeleyDB_VERSION_MAJOR "0") |
| 63 | + set(BerkeleyDB_VERSION_MINOR "0") |
| 64 | + set(BerkeleyDB_VERSION_PATCH "0") |
| 65 | +endif() |
| 66 | + |
| 67 | +# The actual returned/output version variable (the others can be used if needed) |
| 68 | +set(BerkeleyDB_VERSION "${BerkeleyDB_VERSION_MAJOR}.${BerkeleyDB_VERSION_MINOR}.${BerkeleyDB_VERSION_PATCH}") |
| 69 | + |
| 70 | +# Finds the target library for berkeley db, since they all follow the same naming conventions |
| 71 | +macro(findpackage_berkeleydb_get_lib _BERKELEYDB_OUTPUT_VARNAME _TARGET_BERKELEYDB_LIB) |
| 72 | + # Different systems sometimes have a version in the lib name... |
| 73 | + # and some have a dash or underscore before the versions. |
| 74 | + # CMake recommends to put unversioned names before versioned names |
| 75 | + find_library(${_BERKELEYDB_OUTPUT_VARNAME} |
| 76 | + NAMES |
| 77 | + "${_TARGET_BERKELEYDB_LIB}" |
| 78 | + "lib${_TARGET_BERKELEYDB_LIB}" |
| 79 | + "lib${_TARGET_BERKELEYDB_LIB}${BerkeleyDB_VERSION_MAJOR}.${BerkeleyDB_VERSION_MINOR}" |
| 80 | + "lib${_TARGET_BERKELEYDB_LIB}-${BerkeleyDB_VERSION_MAJOR}.${BerkeleyDB_VERSION_MINOR}" |
| 81 | + "lib${_TARGET_BERKELEYDB_LIB}_${BerkeleyDB_VERSION_MAJOR}.${BerkeleyDB_VERSION_MINOR}" |
| 82 | + "lib${_TARGET_BERKELEYDB_LIB}${BerkeleyDB_VERSION_MAJOR}${BerkeleyDB_VERSION_MINOR}" |
| 83 | + "lib${_TARGET_BERKELEYDB_LIB}-${BerkeleyDB_VERSION_MAJOR}${BerkeleyDB_VERSION_MINOR}" |
| 84 | + "lib${_TARGET_BERKELEYDB_LIB}_${BerkeleyDB_VERSION_MAJOR}${BerkeleyDB_VERSION_MINOR}" |
| 85 | + "lib${_TARGET_BERKELEYDB_LIB}${BerkeleyDB_VERSION_MAJOR}" |
| 86 | + "lib${_TARGET_BERKELEYDB_LIB}-${BerkeleyDB_VERSION_MAJOR}" |
| 87 | + "lib${_TARGET_BERKELEYDB_LIB}_${BerkeleyDB_VERSION_MAJOR}" |
| 88 | + HINTS ${_BERKELEYDB_HINTS} |
| 89 | + PATH_SUFFIXES "lib" "lib64" "libs" "libs64" |
| 90 | + PATHS ${_BERKELEYDB_PATHS} |
| 91 | + ) |
| 92 | + # If the library was found, add it to our list of libraries |
| 93 | + if(${_BERKELEYDB_OUTPUT_VARNAME}) |
| 94 | + # If found, append to our libraries variable |
| 95 | + # The ${{}} is because the first expands to target the real variable, the second expands the variable's contents... |
| 96 | + # and the real variable's contents is the path to the lib. Thus, it appends the path of the lib to BerkeleyDB_LIBRARIES. |
| 97 | + list(APPEND BerkeleyDB_LIBRARIES "${${_BERKELEYDB_OUTPUT_VARNAME}}") |
| 98 | + endif() |
| 99 | +endmacro() |
| 100 | + |
| 101 | +# Find and set the paths of the specific library to the variable |
| 102 | +findpackage_berkeleydb_get_lib(BerkeleyDB_LIBRARY "db") |
| 103 | +# NOTE: Windows doesn't have a db_cxx lib, but instead compiles the cxx code into the "db" lib |
| 104 | +findpackage_berkeleydb_get_lib(BerkeleyDB_Cxx_LIBRARY "db_cxx") |
| 105 | +# NOTE: I don't think Linux/Unix gets an SQL lib |
| 106 | +findpackage_berkeleydb_get_lib(BerkeleyDB_Sql_LIBRARY "db_sql") |
| 107 | +findpackage_berkeleydb_get_lib(BerkeleyDB_Stl_LIBRARY "db_stl") |
| 108 | + |
| 109 | +# Needed for find_package_handle_standard_args() |
| 110 | +include(FindPackageHandleStandardArgs) |
| 111 | +# Fails if required vars aren't found, or if the version doesn't meet specifications. |
| 112 | +find_package_handle_standard_args(BerkeleyDB |
| 113 | + FOUND_VAR BerkeleyDB_FOUND |
| 114 | + REQUIRED_VARS |
| 115 | + BerkeleyDB_INCLUDE_DIRS |
| 116 | + BerkeleyDB_LIBRARY |
| 117 | + VERSION_VAR BerkeleyDB_VERSION |
| 118 | + ) |
| 119 | + |
| 120 | +# Create an imported lib for easy linking by external projects |
| 121 | +if(BerkeleyDB_FOUND AND BerkeleyDB_LIBRARIES AND NOT TARGET Oracle::BerkeleyDB) |
| 122 | + add_library(Oracle::BerkeleyDB UNKNOWN IMPORTED) |
| 123 | + set_target_properties(Oracle::BerkeleyDB PROPERTIES |
| 124 | + INTERFACE_INCLUDE_DIRECTORIES "${BerkeleyDB_INCLUDE_DIRS}" |
| 125 | + IMPORTED_LOCATION "${BerkeleyDB_LIBRARY}" |
| 126 | + INTERFACE_LINK_LIBRARIES "${BerkeleyDB_LIBRARIES}" |
| 127 | + ) |
| 128 | +endif() |
| 129 | + |
| 130 | +# Only show the includes path and libraries in the GUI if they click "advanced". |
| 131 | +# Does nothing when using the CLI |
| 132 | +mark_as_advanced(FORCE |
| 133 | + BerkeleyDB_INCLUDE_DIRS |
| 134 | + BerkeleyDB_LIBRARIES |
| 135 | + BerkeleyDB_LIBRARY |
| 136 | + BerkeleyDB_Cxx_LIBRARY |
| 137 | + BerkeleyDB_Sql_LIBRARY |
| 138 | + BerkeleyDB_Stl_LIBRARY |
| 139 | + ) |
| 140 | + |
| 141 | +include(FindPackageMessage) |
| 142 | +# A message that tells the user what includes/libs were found, and obeys the QUIET command. |
| 143 | +find_package_message(BerkeleyDB |
| 144 | + "Found BerkeleyDB libraries: ${BerkeleyDB_LIBRARIES}" |
| 145 | + "[${BerkeleyDB_LIBRARIES}[${BerkeleyDB_INCLUDE_DIRS}]]" |
| 146 | + ) |
0 commit comments