|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# |
| 3 | +# Checks of FBFetchFromManifest.cmake that need no project: |
| 4 | +# |
| 5 | +# cmake -P FBFetchFromManifestTest.cmake |
| 6 | +# |
| 7 | +# Each check reads a manifest this script writes to a scratch directory, so |
| 8 | +# the real manifests can change without breaking it. The last check reads the |
| 9 | +# real boost and openssl manifests when they sit next to this module. |
| 10 | + |
| 11 | +cmake_minimum_required(VERSION 3.14) |
| 12 | +include("${CMAKE_CURRENT_LIST_DIR}/FBFetchFromManifest.cmake") |
| 13 | + |
| 14 | +set(checks 0) |
| 15 | +function(check what actual expected) |
| 16 | + if(NOT "${actual}" STREQUAL "${expected}") |
| 17 | + file(REMOVE_RECURSE "${scratch}") |
| 18 | + message(FATAL_ERROR "${what}:\n got '${actual}'\n expected '${expected}'") |
| 19 | + endif() |
| 20 | + math(EXPR checks "${checks} + 1") |
| 21 | + set(checks "${checks}" PARENT_SCOPE) |
| 22 | +endfunction() |
| 23 | + |
| 24 | +# A scratch fbcode_builder layout: the module reads <dir>/manifests/<name>. |
| 25 | +set(tmp "$ENV{TMPDIR}") |
| 26 | +if(NOT tmp) |
| 27 | + set(tmp "$ENV{TEMP}") |
| 28 | +endif() |
| 29 | +if(NOT tmp) |
| 30 | + set(tmp "/tmp") |
| 31 | +endif() |
| 32 | +string(RANDOM LENGTH 8 token) |
| 33 | +set(scratch "${tmp}/fb_fetch_from_manifest_test_${token}") |
| 34 | +file(MAKE_DIRECTORY "${scratch}/manifests") |
| 35 | +set(_FB_FETCH_FBCODE_BUILDER_DIR "${scratch}") |
| 36 | + |
| 37 | +set(hash "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef") |
| 38 | +file(WRITE "${scratch}/manifests/archived" "[manifest] |
| 39 | +name = archived |
| 40 | +
|
| 41 | +[download] |
| 42 | +url = https://example.com/releases/archived-1.2.3.tar.gz |
| 43 | +sha256 = ${hash} |
| 44 | +
|
| 45 | +[build] |
| 46 | +builder = cmake |
| 47 | +subdir = archived-1.2.3 |
| 48 | +") |
| 49 | +file(WRITE "${scratch}/manifests/by_os" "[manifest] |
| 50 | +name = by_os |
| 51 | +
|
| 52 | +[download.not(os=windows)] |
| 53 | +url = https://example.com/releases/by_os-2.0.tar.gz |
| 54 | +sha256 = ${hash} |
| 55 | +
|
| 56 | +[download.os=windows] |
| 57 | +url = https://example.com/releases/by_os-2.0.zip |
| 58 | +sha256 = fedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210 |
| 59 | +") |
| 60 | +file(WRITE "${scratch}/manifests/unhashed" "[manifest] |
| 61 | +name = unhashed |
| 62 | +
|
| 63 | +[download] |
| 64 | +url = https://example.com/releases/unhashed-0.1.tar.gz?token=abc#top |
| 65 | +") |
| 66 | +file(WRITE "${scratch}/manifests/cloned" "[manifest] |
| 67 | +name = cloned |
| 68 | +
|
| 69 | +[git] |
| 70 | +repo_url = https://example.com/cloned.git |
| 71 | +rev = 0123456789abcdef0123456789abcdef01234567 |
| 72 | +") |
| 73 | + |
| 74 | +# With no mirror, the manifest's URL alone. |
| 75 | +set(FB_FETCH_ARCHIVE_MIRRORS "") |
| 76 | +fb_manifest_download(archived args urls) |
| 77 | +check("archived, no mirror: urls" "${urls}" |
| 78 | + "https://example.com/releases/archived-1.2.3.tar.gz") |
| 79 | +check("archived, no mirror: args" "${args}" |
| 80 | + "URL;https://example.com/releases/archived-1.2.3.tar.gz;URL_HASH;SHA256=${hash};DOWNLOAD_NAME;archived-1.2.3.tar.gz") |
| 81 | + |
| 82 | +# With two mirrors, each prefix with the hash, in order, then the URL. |
| 83 | +set(FB_FETCH_ARCHIVE_MIRRORS |
| 84 | + "https://mirror-a.example/flat;https://mirror-b.example/tree") |
| 85 | +set(mirrored "https://mirror-a.example/flat/${hash};https://mirror-b.example/tree/${hash}") |
| 86 | +fb_manifest_download(archived args urls) |
| 87 | +check("archived, two mirrors: urls" "${urls}" |
| 88 | + "${mirrored};https://example.com/releases/archived-1.2.3.tar.gz") |
| 89 | +check("archived, two mirrors: args" "${args}" |
| 90 | + "URL;${urls};URL_HASH;SHA256=${hash};DOWNLOAD_NAME;archived-1.2.3.tar.gz") |
| 91 | + |
| 92 | +# The arguments parse as FetchContent_Declare() and ExternalProject_Add() read |
| 93 | +# them: every URL under URL, one hash, one name. |
| 94 | +cmake_parse_arguments(parsed "" "URL_HASH;DOWNLOAD_NAME" "URL" ${args}) |
| 95 | +check("archived, two mirrors: parsed URL" "${parsed_URL}" "${urls}") |
| 96 | +check("archived, two mirrors: parsed URL_HASH" "${parsed_URL_HASH}" "SHA256=${hash}") |
| 97 | +check("archived, two mirrors: parsed DOWNLOAD_NAME" "${parsed_DOWNLOAD_NAME}" |
| 98 | + "archived-1.2.3.tar.gz") |
| 99 | +check("archived, two mirrors: nothing else" "${parsed_UNPARSED_ARGUMENTS}" "") |
| 100 | + |
| 101 | +# A conditional [download.<condition>] section counts as [download]; the first |
| 102 | +# one wins, as in fb_manifest_value. |
| 103 | +fb_manifest_download(by_os args urls) |
| 104 | +check("by_os: urls" "${urls}" "${mirrored};https://example.com/releases/by_os-2.0.tar.gz") |
| 105 | +check("by_os: args" "${args}" |
| 106 | + "URL;${urls};URL_HASH;SHA256=${hash};DOWNLOAD_NAME;by_os-2.0.tar.gz") |
| 107 | + |
| 108 | +# No sha256: no mirror can name the archive, so the URL alone. The file name |
| 109 | +# stops at a query or a fragment. |
| 110 | +fb_manifest_download(unhashed args urls) |
| 111 | +check("unhashed: urls" "${urls}" "https://example.com/releases/unhashed-0.1.tar.gz?token=abc#top") |
| 112 | +check("unhashed: args" "${args}" |
| 113 | + "URL;${urls};URL_HASH;SHA256=;DOWNLOAD_NAME;unhashed-0.1.tar.gz") |
| 114 | + |
| 115 | +# A [git] manifest has no archive. |
| 116 | +fb_manifest_download(cloned args urls) |
| 117 | +check("cloned: urls" "${urls}" "") |
| 118 | +check("cloned: args" "${args}" "") |
| 119 | + |
| 120 | +# A url whose query holds a / : the archive's name is in the path, and the |
| 121 | +# last / of the whole url is in the query. |
| 122 | +file(WRITE "${scratch}/manifests/queried" "[manifest] |
| 123 | +name = queried |
| 124 | +
|
| 125 | +[download] |
| 126 | +url = https://example.com/releases/queried-3.4.tar.gz?path=/pub/x |
| 127 | +sha256 = ${hash} |
| 128 | +") |
| 129 | + |
| 130 | +# Each prefix is read without its surrounding spaces and without a trailing /, |
| 131 | +# and an empty one is skipped, so one / stands between the prefix and the hash. |
| 132 | +set(FB_FETCH_ARCHIVE_MIRRORS |
| 133 | + " https://mirror-a.example/flat/ ;; ;https://mirror-b.example/tree// ") |
| 134 | +fb_manifest_download(archived args urls) |
| 135 | +check("untidy prefixes: urls" "${urls}" |
| 136 | + "${mirrored};https://example.com/releases/archived-1.2.3.tar.gz") |
| 137 | +check("untidy prefixes: args" "${args}" |
| 138 | + "URL;${urls};URL_HASH;SHA256=${hash};DOWNLOAD_NAME;archived-1.2.3.tar.gz") |
| 139 | + |
| 140 | +# Five prefixes are the most that keep the manifest's own url reachable: CMake |
| 141 | +# stops after six URLs that answer an HTTP error. |
| 142 | +set(five "") |
| 143 | +foreach(n RANGE 1 5) |
| 144 | + list(APPEND five "https://mirror-${n}.example/flat") |
| 145 | +endforeach() |
| 146 | +set(FB_FETCH_ARCHIVE_MIRRORS "${five}") |
| 147 | +fb_manifest_download(archived args urls) |
| 148 | +list(LENGTH urls count) |
| 149 | +check("five prefixes: six URLs" "${count}" "6") |
| 150 | +list(GET urls 5 last) |
| 151 | +check("five prefixes: the manifest's url is last" "${last}" |
| 152 | + "https://example.com/releases/archived-1.2.3.tar.gz") |
| 153 | + |
| 154 | +# The name stops at the query: a / inside the query names no file. |
| 155 | +set(FB_FETCH_ARCHIVE_MIRRORS "https://mirror-a.example/flat") |
| 156 | +fb_manifest_download(queried args urls) |
| 157 | +check("queried: urls" "${urls}" |
| 158 | + "https://mirror-a.example/flat/${hash};https://example.com/releases/queried-3.4.tar.gz?path=/pub/x") |
| 159 | +check("queried: args" "${args}" |
| 160 | + "URL;${urls};URL_HASH;SHA256=${hash};DOWNLOAD_NAME;queried-3.4.tar.gz") |
| 161 | + |
| 162 | +# A case whose verdict is the configure's own stop runs in its own cmake -P: a |
| 163 | +# FATAL_ERROR ends the script that raises it, so this script cannot read it. |
| 164 | +set(here "${CMAKE_CURRENT_LIST_DIR}") |
| 165 | +function(check_child what body stops expected) |
| 166 | + set(file "${scratch}/child.cmake") |
| 167 | + file(WRITE "${file}" "${body}") |
| 168 | + execute_process( |
| 169 | + COMMAND "${CMAKE_COMMAND}" -P "${file}" |
| 170 | + RESULT_VARIABLE code |
| 171 | + OUTPUT_VARIABLE out |
| 172 | + ERROR_VARIABLE err |
| 173 | + ) |
| 174 | + if(stops AND code EQUAL 0) |
| 175 | + file(REMOVE_RECURSE "${scratch}") |
| 176 | + message(FATAL_ERROR "${what}: the child exited 0, and a stop was expected") |
| 177 | + endif() |
| 178 | + if(NOT stops AND NOT code EQUAL 0) |
| 179 | + file(REMOVE_RECURSE "${scratch}") |
| 180 | + message(FATAL_ERROR "${what}: the child exited ${code}:\n${err}") |
| 181 | + endif() |
| 182 | + if(NOT "${out}${err}" MATCHES "${expected}") |
| 183 | + file(REMOVE_RECURSE "${scratch}") |
| 184 | + message(FATAL_ERROR |
| 185 | + "${what}:\n got '${out}${err}'\n expected a match of '${expected}'") |
| 186 | + endif() |
| 187 | + math(EXPR checks "${checks} + 1") |
| 188 | + set(checks "${checks}" PARENT_SCOPE) |
| 189 | +endfunction() |
| 190 | + |
| 191 | +set(six "${five};https://mirror-6.example/flat") |
| 192 | +set(child_head "include(\"${here}/FBFetchFromManifest.cmake\") |
| 193 | +set(_FB_FETCH_FBCODE_BUILDER_DIR \"${scratch}\") |
| 194 | +set(FB_FETCH_ARCHIVE_MIRRORS \"${six}\") |
| 195 | +") |
| 196 | + |
| 197 | +# A sixth prefix stops the configure, and the message names the count and the |
| 198 | +# reason. Without the stop the build reads the mirrors alone. |
| 199 | +check_child("six prefixes: the configure stops" |
| 200 | + "${child_head}fb_manifest_download(archived args urls)\n" |
| 201 | + TRUE "names 6 prefixes, and five is the most") |
| 202 | + |
| 203 | +# A [git] manifest reads no prefix, so six of them change nothing for it. |
| 204 | +set(child_tail [[ |
| 205 | +fb_manifest_download(cloned args urls) |
| 206 | +if(NOT args STREQUAL "" OR NOT urls STREQUAL "") |
| 207 | + message(FATAL_ERROR "cloned returned '${args}' and '${urls}'") |
| 208 | +endif() |
| 209 | +message(STATUS "CLONED-EMPTY") |
| 210 | +]]) |
| 211 | +check_child("six prefixes: a [git] manifest is unchanged" |
| 212 | + "${child_head}${child_tail}" FALSE "CLONED-EMPTY") |
| 213 | + |
| 214 | +set(FB_FETCH_ARCHIVE_MIRRORS "") |
| 215 | + |
| 216 | +file(REMOVE_RECURSE "${scratch}") |
| 217 | + |
| 218 | +# The real boost and openssl manifests, when they sit next to this module: one |
| 219 | +# mirror gives two URLs, the mirror's first, with the manifest's hash and the |
| 220 | +# archive's name. fb_fetch_from_manifest gives boost's arguments to |
| 221 | +# FetchContent_Declare(), and folly gives openssl's to ExternalProject_Add(). |
| 222 | +get_filename_component(fbcode_builder "${CMAKE_CURRENT_LIST_DIR}" DIRECTORY) |
| 223 | +foreach(manifest boost openssl) |
| 224 | + if(EXISTS "${fbcode_builder}/manifests/${manifest}") |
| 225 | + set(_FB_FETCH_FBCODE_BUILDER_DIR "${fbcode_builder}") |
| 226 | + set(FB_FETCH_ARCHIVE_MIRRORS "https://mirror.example/flat") |
| 227 | + fb_manifest_value(${manifest} download url expected_url) |
| 228 | + fb_manifest_value(${manifest} download sha256 expected_hash) |
| 229 | + string(REGEX REPLACE ".*/" "" expected_name "${expected_url}") |
| 230 | + fb_manifest_download(${manifest} args urls) |
| 231 | + check("${manifest}: urls" "${urls}" |
| 232 | + "https://mirror.example/flat/${expected_hash};${expected_url}") |
| 233 | + check("${manifest}: args" "${args}" |
| 234 | + "URL;${urls};URL_HASH;SHA256=${expected_hash};DOWNLOAD_NAME;${expected_name}") |
| 235 | + endif() |
| 236 | +endforeach() |
| 237 | + |
| 238 | +message(STATUS "FBFetchFromManifestTest: ${checks} checks passed") |
0 commit comments