]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/opentelemetry-cpp/tools/vcpkg/scripts/cmake/vcpkg_from_github.cmake
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / jaegertracing / opentelemetry-cpp / tools / vcpkg / scripts / cmake / vcpkg_from_github.cmake
1 #[===[.md:
2 # vcpkg_from_github
3
4 Download and extract a project from GitHub. Enables support for `install --head`.
5
6 ## Usage:
7 ```cmake
8 vcpkg_from_github(
9 OUT_SOURCE_PATH <SOURCE_PATH>
10 REPO <Microsoft/cpprestsdk>
11 [REF <v2.0.0>]
12 [SHA512 <45d0d7f8cc350...>]
13 [HEAD_REF <master>]
14 [PATCHES <patch1.patch> <patch2.patch>...]
15 [GITHUB_HOST <https://github.com>]
16 [AUTHORIZATION_TOKEN <${SECRET_FROM_FILE}>]
17 [FILE_DISAMBIGUATOR <N>]
18 )
19 ```
20
21 ## Parameters:
22 ### OUT_SOURCE_PATH
23 Specifies the out-variable that will contain the extracted location.
24
25 This should be set to `SOURCE_PATH` by convention.
26
27 ### REPO
28 The organization or user and repository on GitHub.
29
30 ### REF
31 A stable git commit-ish (ideally a tag or commit) that will not change contents. **This should not be a branch.**
32
33 For repositories without official releases, this can be set to the full commit id of the current latest master.
34
35 If `REF` is specified, `SHA512` must also be specified.
36
37 ### SHA512
38 The SHA512 hash that should match the archive (https://github.com/${REPO}/archive/${REF}.tar.gz).
39
40 This is most easily determined by first setting it to `1`, then trying to build the port. The error message will contain the full hash, which can be copied back into the portfile.
41
42 ### HEAD_REF
43 The unstable git commit-ish (ideally a branch) to pull for `--head` builds.
44
45 For most projects, this should be `master`. The chosen branch should be one that is expected to be always buildable on all supported platforms.
46
47 ### PATCHES
48 A list of patches to be applied to the extracted sources.
49
50 Relative paths are based on the port directory.
51
52 ### GITHUB_HOST
53 A replacement host for enterprise GitHub instances.
54
55 This field should contain the scheme, host, and port of the desired URL without a trailing slash.
56
57 ### AUTHORIZATION_TOKEN
58 A token to be passed via the Authorization HTTP header as "token ${AUTHORIZATION_TOKEN}".
59
60 ### FILE_DISAMBIGUATOR
61 A token to uniquely identify the resulting filename if the SHA512 changes even though a git ref does not, to avoid stepping on the same file name.
62
63 ## Notes:
64 At least one of `REF` and `HEAD_REF` must be specified, however it is preferable for both to be present.
65
66 This exports the `VCPKG_HEAD_VERSION` variable during head builds.
67
68 ## Examples:
69
70 * [cpprestsdk](https://github.com/Microsoft/vcpkg/blob/master/ports/cpprestsdk/portfile.cmake)
71 * [ms-gsl](https://github.com/Microsoft/vcpkg/blob/master/ports/ms-gsl/portfile.cmake)
72 * [beast](https://github.com/Microsoft/vcpkg/blob/master/ports/beast/portfile.cmake)
73 #]===]
74
75 function(vcpkg_from_github)
76 set(oneValueArgs OUT_SOURCE_PATH REPO REF SHA512 HEAD_REF GITHUB_HOST AUTHORIZATION_TOKEN FILE_DISAMBIGUATOR)
77 set(multipleValuesArgs PATCHES)
78 # parse parameters such that semicolons in options arguments to COMMAND don't get erased
79 cmake_parse_arguments(PARSE_ARGV 0 _vdud "" "${oneValueArgs}" "${multipleValuesArgs}")
80
81 if(NOT DEFINED _vdud_OUT_SOURCE_PATH)
82 message(FATAL_ERROR "OUT_SOURCE_PATH must be specified.")
83 endif()
84
85 if((DEFINED _vdud_REF AND NOT DEFINED _vdud_SHA512) OR (NOT DEFINED _vdud_REF AND DEFINED _vdud_SHA512))
86 message(FATAL_ERROR "SHA512 must be specified if REF is specified.")
87 endif()
88
89 if(NOT DEFINED _vdud_REPO)
90 message(FATAL_ERROR "The GitHub repository must be specified.")
91 endif()
92
93 if(NOT DEFINED _vdud_REF AND NOT DEFINED _vdud_HEAD_REF)
94 message(FATAL_ERROR "At least one of REF and HEAD_REF must be specified.")
95 endif()
96
97 if(NOT DEFINED _vdud_GITHUB_HOST)
98 set(GITHUB_HOST https://github.com)
99 set(GITHUB_API_URL https://api.github.com)
100 else()
101 set(GITHUB_HOST "${_vdud_GITHUB_HOST}")
102 set(GITHUB_API_URL "${_vdud_GITHUB_HOST}/api/v3")
103 endif()
104
105 if(DEFINED _vdud_AUTHORIZATION_TOKEN)
106 set(HEADERS "HEADERS" "Authorization: token ${_vdud_AUTHORIZATION_TOKEN}")
107 else()
108 set(HEADERS)
109 endif()
110
111 string(REGEX REPLACE ".*/" "" REPO_NAME "${_vdud_REPO}")
112 string(REGEX REPLACE "/.*" "" ORG_NAME "${_vdud_REPO}")
113
114 macro(set_TEMP_SOURCE_PATH BASE BASEREF)
115 set(TEMP_SOURCE_PATH "${BASE}/${REPO_NAME}-${BASEREF}")
116 if(NOT EXISTS "${TEMP_SOURCE_PATH}")
117 # Sometimes GitHub strips a leading 'v' off the REF.
118 string(REGEX REPLACE "^v" "" REF "${BASEREF}")
119 string(REPLACE "/" "-" REF "${REF}")
120 set(TEMP_SOURCE_PATH "${BASE}/${REPO_NAME}-${REF}")
121 if(NOT EXISTS "${TEMP_SOURCE_PATH}")
122 message(FATAL_ERROR "Could not determine source path: '${BASE}/${REPO_NAME}-${BASEREF}' does not exist")
123 endif()
124 endif()
125 endmacro()
126
127 if(VCPKG_USE_HEAD_VERSION AND NOT DEFINED _vdud_HEAD_REF)
128 message(STATUS "Package does not specify HEAD_REF. Falling back to non-HEAD version.")
129 set(VCPKG_USE_HEAD_VERSION OFF)
130 endif()
131
132 # Handle --no-head scenarios
133 if(NOT VCPKG_USE_HEAD_VERSION)
134 if(NOT _vdud_REF)
135 message(FATAL_ERROR "Package does not specify REF. It must built using --head.")
136 endif()
137
138 string(REPLACE "/" "-" SANITIZED_REF "${_vdud_REF}")
139
140 set(downloaded_file_name "${ORG_NAME}-${REPO_NAME}-${SANITIZED_REF}")
141 if (_vdud_FILE_DISAMBIGUATOR)
142 set(downloaded_file_name "${downloaded_file_name}-${_vdud_FILE_DISAMBIGUATOR}")
143 endif()
144
145 set(downloaded_file_name "${downloaded_file_name}.tar.gz")
146
147 vcpkg_download_distfile(ARCHIVE
148 URLS "${GITHUB_HOST}/${ORG_NAME}/${REPO_NAME}/archive/${_vdud_REF}.tar.gz"
149 SHA512 "${_vdud_SHA512}"
150 FILENAME "${downloaded_file_name}"
151 ${HEADERS}
152 )
153
154 vcpkg_extract_source_archive_ex(
155 OUT_SOURCE_PATH SOURCE_PATH
156 ARCHIVE "${ARCHIVE}"
157 REF "${SANITIZED_REF}"
158 PATCHES ${_vdud_PATCHES}
159 )
160
161 set(${_vdud_OUT_SOURCE_PATH} "${SOURCE_PATH}" PARENT_SCOPE)
162 return()
163 endif()
164
165 # The following is for --head scenarios
166 set(URL "${GITHUB_HOST}/${ORG_NAME}/${REPO_NAME}/archive/${_vdud_HEAD_REF}.tar.gz")
167 string(REPLACE "/" "-" SANITIZED_HEAD_REF "${_vdud_HEAD_REF}")
168 set(downloaded_file_name "${ORG_NAME}-${REPO_NAME}-${SANITIZED_HEAD_REF}")
169 if (_vdud_FILE_DISAMBIGUATOR)
170 set(downloaded_file_name "${downloaded_file_name}-${_vdud_FILE_DISAMBIGUATOR}")
171 endif()
172
173 set(downloaded_file_name "${downloaded_file_name}.tar.gz")
174
175 set(downloaded_file_path "${DOWNLOADS}/${downloaded_file_name}")
176
177 if(_VCPKG_NO_DOWNLOADS)
178 if(NOT EXISTS "${downloaded_file_path}" OR NOT EXISTS "${downloaded_file_path}.version")
179 message(FATAL_ERROR "Downloads are disabled, but '${downloaded_file_path}' does not exist.")
180 endif()
181 message(STATUS "Using cached ${downloaded_file_path}")
182 else()
183 if(EXISTS "${downloaded_file_path}")
184 message(STATUS "Purging cached ${downloaded_file_path} to fetch latest (use --no-downloads to suppress)")
185 file(REMOVE "${downloaded_file_path}")
186 endif()
187 if(EXISTS "${downloaded_file_path}.version")
188 file(REMOVE "${downloaded_file_path}.version")
189 endif()
190 if(EXISTS "${CURRENT_BUILDTREES_DIR}/src/head")
191 file(REMOVE_RECURSE "${CURRENT_BUILDTREES_DIR}/src/head")
192 endif()
193
194 # Try to download the file and version information from github.
195 vcpkg_download_distfile(ARCHIVE_VERSION
196 URLS "${GITHUB_API_URL}/repos/${ORG_NAME}/${REPO_NAME}/git/refs/heads/${_vdud_HEAD_REF}"
197 FILENAME "${downloaded_file_name}.version"
198 SKIP_SHA512
199 ${HEADERS}
200 )
201
202 vcpkg_download_distfile(ARCHIVE
203 URLS ${URL}
204 FILENAME "${downloaded_file_name}"
205 SKIP_SHA512
206 ${HEADERS}
207 )
208 endif()
209
210 # Parse the github refs response with regex.
211 # TODO: use some JSON swiss-army-knife utility instead.
212 file(READ "${downloaded_file_path}.version" _contents)
213 string(REGEX MATCH "\"sha\": \"[a-f0-9]+\"" x "${_contents}")
214 string(REGEX REPLACE "\"sha\": \"([a-f0-9]+)\"" "\\1" _version ${x})
215
216 # exports VCPKG_HEAD_VERSION to the caller. This will get picked up by ports.cmake after the build.
217 # When multiple vcpkg_from_github's are used after each other, only use the version from the first (hopefully the primary one).
218 if(NOT DEFINED VCPKG_HEAD_VERSION)
219 set(VCPKG_HEAD_VERSION "${_version}" PARENT_SCOPE)
220 endif()
221
222 vcpkg_extract_source_archive_ex(
223 SKIP_PATCH_CHECK
224 OUT_SOURCE_PATH SOURCE_PATH
225 ARCHIVE "${downloaded_file_path}"
226 REF "${SANITIZED_HEAD_REF}"
227 WORKING_DIRECTORY "${CURRENT_BUILDTREES_DIR}/src/head"
228 PATCHES ${_vdud_PATCHES}
229 )
230 set(${_vdud_OUT_SOURCE_PATH} "${SOURCE_PATH}" PARENT_SCOPE)
231 endfunction()