|
| 1 | +cmake_minimum_required(VERSION 3.24) |
| 2 | + |
| 3 | +if(NOT BEMAN_OPTIONAL_LOCKFILE) |
| 4 | + set(BEMAN_OPTIONAL_LOCKFILE |
| 5 | + "lockfile.json" |
| 6 | + CACHE FILEPATH |
| 7 | + "Path to the dependency lockfile for the Beman Optional." |
| 8 | + ) |
| 9 | +endif() |
| 10 | + |
| 11 | +set(BemanOptional_projectDir "${CMAKE_CURRENT_LIST_DIR}/..") |
| 12 | +message(TRACE "BemanOptional_projectDir=\"${BemanOptional_projectDir}\"") |
| 13 | + |
| 14 | +message(TRACE "BEMAN_OPTIONAL_LOCKFILE=\"${BEMAN_OPTIONAL_LOCKFILE}\"") |
| 15 | +file( |
| 16 | + REAL_PATH |
| 17 | + "${BEMAN_OPTIONAL_LOCKFILE}" |
| 18 | + BemanOptional_lockfile |
| 19 | + BASE_DIRECTORY "${BemanOptional_projectDir}" |
| 20 | + EXPAND_TILDE |
| 21 | +) |
| 22 | +message(DEBUG "Using lockfile: \"${BemanOptional_lockfile}\"") |
| 23 | + |
| 24 | +# Force CMake to reconfigure the project if the lockfile changes |
| 25 | +set_property( |
| 26 | + DIRECTORY "${BemanOptional_projectDir}" |
| 27 | + APPEND |
| 28 | + PROPERTY CMAKE_CONFIGURE_DEPENDS "${BemanOptional_lockfile}" |
| 29 | +) |
| 30 | + |
| 31 | +# For more on the protocol for this function, see: |
| 32 | +# https://cmake.org/cmake/help/latest/command/cmake_language.html#provider-commands |
| 33 | +function(BemanOptional_provideDependency method package_name) |
| 34 | + # Read the lockfile |
| 35 | + file(READ "${BemanOptional_lockfile}" BemanOptional_rootObj) |
| 36 | + |
| 37 | + # Get the "dependencies" field and store it in BemanOptional_dependenciesObj |
| 38 | + string( |
| 39 | + JSON |
| 40 | + BemanOptional_dependenciesObj |
| 41 | + ERROR_VARIABLE BemanOptional_error |
| 42 | + GET "${BemanOptional_rootObj}" |
| 43 | + "dependencies" |
| 44 | + ) |
| 45 | + if(BemanOptional_error) |
| 46 | + message(FATAL_ERROR "${BemanOptional_lockfile}: ${BemanOptional_error}") |
| 47 | + endif() |
| 48 | + |
| 49 | + # Get the length of the libraries array and store it in BemanOptional_dependenciesObj |
| 50 | + string( |
| 51 | + JSON |
| 52 | + BemanOptional_numDependencies |
| 53 | + ERROR_VARIABLE BemanOptional_error |
| 54 | + LENGTH "${BemanOptional_dependenciesObj}" |
| 55 | + ) |
| 56 | + if(BemanOptional_error) |
| 57 | + message(FATAL_ERROR "${BemanOptional_lockfile}: ${BemanOptional_error}") |
| 58 | + endif() |
| 59 | + |
| 60 | + # Loop over each dependency object |
| 61 | + math(EXPR BemanOptional_maxIndex "${BemanOptional_numDependencies} - 1") |
| 62 | + foreach(BemanOptional_index RANGE "${BemanOptional_maxIndex}") |
| 63 | + set(BemanOptional_errorPrefix |
| 64 | + "${BemanOptional_lockfile}, dependency ${BemanOptional_index}" |
| 65 | + ) |
| 66 | + |
| 67 | + # Get the dependency object at BemanOptional_index |
| 68 | + # and store it in BemanOptional_depObj |
| 69 | + string( |
| 70 | + JSON |
| 71 | + BemanOptional_depObj |
| 72 | + ERROR_VARIABLE BemanOptional_error |
| 73 | + GET "${BemanOptional_dependenciesObj}" |
| 74 | + "${BemanOptional_index}" |
| 75 | + ) |
| 76 | + if(BemanOptional_error) |
| 77 | + message( |
| 78 | + FATAL_ERROR |
| 79 | + "${BemanOptional_errorPrefix}: ${BemanOptional_error}" |
| 80 | + ) |
| 81 | + endif() |
| 82 | + |
| 83 | + # Get the "name" field and store it in BemanOptional_name |
| 84 | + string( |
| 85 | + JSON |
| 86 | + BemanOptional_name |
| 87 | + ERROR_VARIABLE BemanOptional_error |
| 88 | + GET "${BemanOptional_depObj}" |
| 89 | + "name" |
| 90 | + ) |
| 91 | + if(BemanOptional_error) |
| 92 | + message( |
| 93 | + FATAL_ERROR |
| 94 | + "${BemanOptional_errorPrefix}: ${BemanOptional_error}" |
| 95 | + ) |
| 96 | + endif() |
| 97 | + |
| 98 | + # Get the "package_name" field and store it in BemanOptional_pkgName |
| 99 | + string( |
| 100 | + JSON |
| 101 | + BemanOptional_pkgName |
| 102 | + ERROR_VARIABLE BemanOptional_error |
| 103 | + GET "${BemanOptional_depObj}" |
| 104 | + "package_name" |
| 105 | + ) |
| 106 | + if(BemanOptional_error) |
| 107 | + message( |
| 108 | + FATAL_ERROR |
| 109 | + "${BemanOptional_errorPrefix}: ${BemanOptional_error}" |
| 110 | + ) |
| 111 | + endif() |
| 112 | + |
| 113 | + # Get the "git_repository" field and store it in BemanOptional_repo |
| 114 | + string( |
| 115 | + JSON |
| 116 | + BemanOptional_repo |
| 117 | + ERROR_VARIABLE BemanOptional_error |
| 118 | + GET "${BemanOptional_depObj}" |
| 119 | + "git_repository" |
| 120 | + ) |
| 121 | + if(BemanOptional_error) |
| 122 | + message( |
| 123 | + FATAL_ERROR |
| 124 | + "${BemanOptional_errorPrefix}: ${BemanOptional_error}" |
| 125 | + ) |
| 126 | + endif() |
| 127 | + |
| 128 | + # Get the "git_tag" field and store it in BemanOptional_tag |
| 129 | + string( |
| 130 | + JSON |
| 131 | + BemanOptional_tag |
| 132 | + ERROR_VARIABLE BemanOptional_error |
| 133 | + GET "${BemanOptional_depObj}" |
| 134 | + "git_tag" |
| 135 | + ) |
| 136 | + if(BemanOptional_error) |
| 137 | + message( |
| 138 | + FATAL_ERROR |
| 139 | + "${BemanOptional_errorPrefix}: ${BemanOptional_error}" |
| 140 | + ) |
| 141 | + endif() |
| 142 | + |
| 143 | + if(method STREQUAL "FIND_PACKAGE") |
| 144 | + if(package_name STREQUAL BemanOptional_pkgName) |
| 145 | + string( |
| 146 | + APPEND |
| 147 | + BemanOptional_debug |
| 148 | + "Redirecting find_package calls for ${BemanOptional_pkgName} " |
| 149 | + "to FetchContent logic fetching ${BemanOptional_repo} at " |
| 150 | + "${BemanOptional_tag} according to ${BemanOptional_lockfile}." |
| 151 | + ) |
| 152 | + message(DEBUG "${BemanOptional_debug}") |
| 153 | + FetchContent_Declare( |
| 154 | + "${BemanOptional_name}" |
| 155 | + GIT_REPOSITORY "${BemanOptional_repo}" |
| 156 | + GIT_TAG "${BemanOptional_tag}" |
| 157 | + EXCLUDE_FROM_ALL |
| 158 | + ) |
| 159 | + set(INSTALL_GTEST OFF) # Disable GoogleTest installation |
| 160 | + FetchContent_MakeAvailable("${BemanOptional_name}") |
| 161 | + |
| 162 | + # Important! <PackageName>_FOUND tells CMake that `find_package` is |
| 163 | + # not needed for this package anymore |
| 164 | + set("${BemanOptional_pkgName}_FOUND" TRUE PARENT_SCOPE) |
| 165 | + endif() |
| 166 | + endif() |
| 167 | + endforeach() |
| 168 | + |
| 169 | + set(GTest_FOUND TRUE PARENT_SCOPE) |
| 170 | +endfunction() |
| 171 | + |
| 172 | +cmake_language( |
| 173 | + SET_DEPENDENCY_PROVIDER BemanOptional_provideDependency |
| 174 | + SUPPORTED_METHODS FIND_PACKAGE |
| 175 | +) |
0 commit comments