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