Skip to content

Commit 173aebd

Browse files
kiukchungmeta-codesync[bot]
authored andcommitted
Enable checked archive download recovery
Summary: **Diffstack**: Enables builds to use archive copies when original hosts fail, while preserving checksums and extraction, so one download outage need not stop the build. **This Diff** [1/2]: Enables both library-download paths to use checked archive copies. The next diff lets Python package builds pass copy addresses to these paths: ```lang=shell # Recorded tiny CMake project, same manifest and mirror, with an origin returning 404. /tmp/forgewheel-unit-venv/bin/cmake -S . -B build \ -DFB_FETCH_ARCHIVE_MIRRORS=http://localhost:33203/mirror # After: exit 0; one mirror request; extracted payload matches. ``` ***Problem***: Configuration fails when the original archive host returns an error. The available copy cannot help because separate library-download paths do not consistently use it: ```lang=shell # Same recorded project, manifest, mirror setting and failed origin. /tmp/forgewheel-unit-venv/bin/cmake -S . -B build \ -DFB_FETCH_ARCHIVE_MIRRORS=http://localhost:33203/mirror # Before: origin request returned 404; exit 1. ``` ***Approach***: * `fb_manifest_download` is the common URL, checksum and filename argument builder for normal archive downloads and OpenSSL. `FB_FETCH_ARCHIVE_MIRRORS` is its CMake list of copy addresses. * Normalize prefixes and reject more than five before transfer. Keep the original address last and git retrieval unchanged. * OpenSSL, a cryptography library, uses these arguments and reads its version from the manifest, the file listing the archive's URL and checksum. Reviewed By: vitaut Differential Revision: D130529026 fbshipit-source-id: f8ce5d9c09560dace6c057b8d684c589dd53df91
1 parent d723c52 commit 173aebd

2 files changed

Lines changed: 307 additions & 6 deletions

File tree

‎build/fbcode_builder/CMake/FBFetchFromManifest.cmake‎

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,21 @@ set(
2121
"Fetch git dependencies' branches as they were at this time, such as git log -1 --format=%cI or now, ignoring deps/github_hashes"
2222
)
2323

24+
# For a build behind a mirror of the archives the manifests pin. CMake tries
25+
# each prefix, followed by /<sha256> of the archive, before the manifest's own
26+
# URL. It tries the URLs in turn and skips one that answers an HTTP error, so a
27+
# mirror that lacks an archive costs one request. CMake's download script stops
28+
# after six URLs that answer an HTTP error (CMake 4.4), so a sixth prefix would
29+
# leave the manifest's own URL untried: fb_manifest_download() stops the
30+
# configure instead of building against a mirror alone. It reads each prefix
31+
# without its surrounding spaces and without a trailing /, and skips an empty
32+
# one, so a list that a shell variable carries needs no cleaning by its caller.
33+
set(
34+
FB_FETCH_ARCHIVE_MIRRORS ""
35+
CACHE STRING
36+
"At most five URL prefixes tried before a manifest's [download] url, each as <prefix>/<sha256>"
37+
)
38+
2439
# Sets <out> to <key> in the first [<section>] or [<section>.<condition>] of
2540
# getdeps' <manifest> that has it, or to an empty string. The condition isn't
2641
# evaluated: what getdeps takes from the system on this platform, such as
@@ -45,6 +60,54 @@ function(fb_manifest_value manifest section key out)
4560
set(${out} "${value}" PARENT_SCOPE)
4661
endfunction()
4762

63+
# Sets <args> to the arguments that give <manifest>'s [download] archive to
64+
# FetchContent_Declare() or ExternalProject_Add(): URL with the URLs to try,
65+
# the FB_FETCH_ARCHIVE_MIRRORS prefixes first, URL_HASH with the manifest's
66+
# sha256, and DOWNLOAD_NAME with the archive's name, since a mirror URL ends in
67+
# the hash. A prefix is read without its surrounding spaces and without a
68+
# trailing /, an empty one is skipped, and more than five of them stop the
69+
# configure. The name is what follows the last / of the url, without a query or
70+
# a fragment. Sets <urls> to those URLs alone, for a message. Both are empty
71+
# when the manifest has no [download] url, and no mirror is tried when it gives
72+
# no sha256.
73+
function(fb_manifest_download manifest args urls)
74+
fb_manifest_value(${manifest} download url url)
75+
fb_manifest_value(${manifest} download sha256 hash)
76+
set(list "")
77+
set(download "")
78+
if(url)
79+
if(hash)
80+
set(prefixes "")
81+
foreach(mirror IN LISTS FB_FETCH_ARCHIVE_MIRRORS)
82+
string(STRIP "${mirror}" mirror)
83+
string(REGEX REPLACE "/+$" "" mirror "${mirror}")
84+
if(NOT mirror STREQUAL "")
85+
list(APPEND prefixes "${mirror}")
86+
endif()
87+
endforeach()
88+
list(LENGTH prefixes count)
89+
if(count GREATER 5)
90+
message(
91+
FATAL_ERROR
92+
"FB_FETCH_ARCHIVE_MIRRORS names ${count} prefixes, and five is the "
93+
"most: CMake stops after six URLs that answer an HTTP error, so a "
94+
"sixth prefix leaves a manifest's own url untried. The prefixes: "
95+
"${prefixes}"
96+
)
97+
endif()
98+
foreach(mirror IN LISTS prefixes)
99+
list(APPEND list "${mirror}/${hash}")
100+
endforeach()
101+
endif()
102+
list(APPEND list "${url}")
103+
string(REGEX REPLACE "[?#].*" "" name "${url}")
104+
string(REGEX REPLACE ".*/" "" name "${name}")
105+
set(download URL ${list} URL_HASH "SHA256=${hash}" DOWNLOAD_NAME "${name}")
106+
endif()
107+
set(${args} "${download}" PARENT_SCOPE)
108+
set(${urls} "${list}" PARENT_SCOPE)
109+
endfunction()
110+
48111
# Sets <out> to the last commit on <branch> of <repo_url> at or before
49112
# FB_FETCH_AS_OF, which a clone of the branch's commits alone can tell. A later
50113
# configure with the same time reuses the answer.
@@ -98,7 +161,8 @@ endfunction()
98161
# deps/github_hashes or project_hashes next to fbcode_builder records, else the
99162
# manifest's branch, else main, with a warning. FB_FETCH_AS_OF skips the
100163
# recorded revision and the warning, and takes the branch's commit at that
101-
# time. Further arguments go to FetchContent_Declare().
164+
# time. An archive is tried from each FB_FETCH_ARCHIVE_MIRRORS prefix before
165+
# its own URL. Further arguments go to FetchContent_Declare().
102166
function(fb_fetch_from_manifest name manifest)
103167
fb_manifest_value(${manifest} git repo_url repo_url)
104168
fb_manifest_value(${manifest} build subdir subdir)
@@ -144,17 +208,16 @@ function(fb_fetch_from_manifest name manifest)
144208
message(STATUS "${name} not found, fetching ${repo_url} ${rev}")
145209
set(source GIT_REPOSITORY "${repo_url}" GIT_TAG "${rev}")
146210
else()
147-
fb_manifest_value(${manifest} download url url)
148-
fb_manifest_value(${manifest} download sha256 sha256)
149-
if(NOT url)
211+
fb_manifest_download(${manifest} source urls)
212+
if(NOT urls)
150213
message(
151214
FATAL_ERROR
152215
"Can't fetch ${name}: getdeps' ${manifest} manifest has neither a "
153216
"[git] repo_url nor a [download] url."
154217
)
155218
endif()
156-
message(STATUS "${name} not found, fetching ${url}")
157-
set(source URL "${url}" URL_HASH "SHA256=${sha256}")
219+
list(JOIN urls " or " where)
220+
message(STATUS "${name} not found, fetching ${where}")
158221
# FetchContent strips the archive's top-level directory, where subdir
159222
# starts.
160223
if(subdir MATCHES "^[^/]+/(.+)")
Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
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

Comments
 (0)