Debian Codegraph @

buildtools/classes/Packager/Vcpkg.php

raw symbols SCIP
1 <?php
2
3 namespace Dpp\Packager;
4
5 use \RuntimeException;
6
7 const GREEN = "\033[32m";
8 const RED = "\033[31m";
9 const WHITE = "\033[0m";
10
11 class Vcpkg
12 {
13 /**
14 * @var string Git Tag
15 */
16 private string $latestTag;
17
18 /**
19 * @var string Semver version
20 */
21 private string $version;
22
23 /**
24 * @var string Path to GIT
25 */
26 private string $git;
27
28 /**
29 * @var string Path to SUDO
30 */
31 private string $sudo;
32
33 /**
34 * @var bool True when we have done the first build to get the SHA512 sum
35 */
36 private bool $firstBuildComplete = false;
37
38 /**
39 * Constructor
40 *
41 * Examines current diretory's git repository to get latest tag and version.
42 */
43 public function __construct()
44 {
45 global $argv;
46 if (count($argv) < 2) {
47 throw new RuntimeException(RED . "Missing github repository owner and access token\n" . WHITE);
48 }
49 echo GREEN . "Starting vcpkg updater...\n" . WHITE;
50
51 /* Get the latest tag from the version of the repository checked out by default into the action */
52 if (count($argv) > 2 && !empty($argv[3])) {
53 $this->latestTag = $argv[3];
54 } else {
55 $this->latestTag = preg_replace("/\n/", "", shell_exec("git describe --tags `git rev-list --tags --max-count=1`"));
56 }
57 $this->version = preg_replace('/^v/', '', $this->getTag());
58 echo GREEN . "Latest tag: " . $this->getTag() . " version: " . $this->getVersion() . "\n" . WHITE;
59
60 $this->git = trim(`which git`);
61 $this->sudo = trim(`which sudo`);
62 }
63
64 /**
65 * Get semver version
66 *
67 * @return string
68 */
69 public function getVersion(): string
70 {
71 return $this->version;
72 }
73
74 /**
75 * Get the git tag we are building
76 *
77 * @return string
78 */
79 public function getTag(): string
80 {
81 return $this->latestTag;
82 }
83
84 private function git(string $parameters, bool $sudo = false): void
85 {
86 system(($sudo ? $this->sudo . ' ' : '') . $this->git . ' ' . $parameters);
87 }
88
89 private function sudo(string $command): void
90 {
91 system($this->sudo . ' ' . $command);
92 }
93
94 /**
95 * Check out a repository by tag or branch name to ~/dpp,
96 * using the personal access token and username passed in as command line parameters.
97 *
98 * @param string $tag Tag to clone
99 * @return bool false if the repository could not be cloned
100 */
101 function checkoutRepository(string $tag = ""): bool
102 {
103 global $argv;
104
105 if (empty($tag)) {
106 /* Empty tag means use the main branch */
107 $tag = `{$this->git} config --get init.defaultBranch || echo master`;
108 }
109 $repositoryUrl = 'https://' . urlencode($argv[1]) . ':' . urlencode($argv[2]) . '@github.com/brainboxdotcc/DPP';
110
111 echo GREEN . "Check out repository: $tag (user: ". $argv[1] . " default branch: " . $tag . ")\n" . WHITE;
112
113 chdir(getenv('HOME'));
114 system('rm -rf ./dpp');
115 $this->git('config --global user.email "[email protected]"');
116 $this->git('config --global user.name "DPP VCPKG Bot"');
117 $this->git('clone ' . escapeshellarg($repositoryUrl) . ' ./dpp --depth=1');
118
119 /* This is noisy, silence it */
120 $status = chdir(getenv("HOME") . '/dpp');
121 $this->git('fetch -at 2>/dev/null');
122 $this->git('checkout master');
123 $this->git('pull');
124 $this->git('checkout ' . escapeshellarg($tag));
125 $this->git('pull');
126 $this->git("remote set-url origin $repositoryUrl");
127 echo GREEN . "Repository remote: " . WHITE;
128 $this->git('remote get-url origin');
129 echo "\n";
130
131 return $status;
132 }
133
134 /**
135 * Create ./vcpkg/ports/dpp/vcpkg.json and return the portfile contents to
136 * build the branch that is cloned at ~/dpp
137 *
138 * @param string $sha512 The SHA512 sum of the tagged download, or initially
139 * zero, which means that the vcpkg install command should obtain it the
140 * second time around.
141 * @return string The portfile content
142 */
143 function constructPortAndVersionFile(string $sha512 = "0"): string
144 {
145 echo GREEN . "Construct portfile for " . $this->getVersion() . ", sha512: $sha512\n" . WHITE;
146 chdir(getenv("HOME") . '/dpp');
147
148 $portFileContent = 'vcpkg_from_github(
149 OUT_SOURCE_PATH SOURCE_PATH
150 REPO brainboxdotcc/DPP
151 REF "v${VERSION}"
152 SHA512 ' . $sha512 . '
153 )
154
155 vcpkg_cmake_configure(
156 SOURCE_PATH "${SOURCE_PATH}"
157 DISABLE_PARALLEL_CONFIGURE
158 )
159
160 vcpkg_cmake_install()
161
162 vcpkg_cmake_config_fixup(NO_PREFIX_CORRECTION)
163
164 file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/share/dpp")
165 file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/include")
166
167 if(VCPKG_LIBRARY_LINKAGE STREQUAL "static")
168 file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/bin" "${CURRENT_PACKAGES_DIR}/debug/bin")
169 endif()
170
171 vcpkg_install_copyright(FILE_LIST "${SOURCE_PATH}/LICENSE")
172
173 file(COPY "${CMAKE_CURRENT_LIST_DIR}/usage" DESTINATION "${CURRENT_PACKAGES_DIR}/share/${PORT}")
174 ';
175 // ./vcpkg/ports/dpp/vcpkg.json
176 $versionFileContent = '{
177 "name": "dpp",
178 "version": ' . json_encode($this->getVersion()) . ',
179 "description": "D++ Extremely Lightweight C++ Discord Library.",
180 "homepage": "https://dpp.dev/",
181 "license": "Apache-2.0",
182 "supports": "((windows & !static & !uwp) | linux | osx)",
183 "dependencies": [
184 "nlohmann-json",
185 "openssl",
186 "opus",
187 "zlib",
188 {
189 "name": "vcpkg-cmake",
190 "host": true
191 },
192 {
193 "name": "vcpkg-cmake-config",
194 "host": true
195 }
196 ]
197 }';
198 echo GREEN . "Writing portfile...\n" . WHITE;
199 file_put_contents('./vcpkg/ports/dpp/vcpkg.json', $versionFileContent);
200 return $portFileContent;
201 }
202
203 /**
204 * Attempt the first build of the vcpkg port. This will always fail, as it is given
205 * an SHA512 sum of 0. When it fails the output contains the SHA512 sum, which is then
206 * extracted from the error output using a regular expression, and saved for a second
207 * attempt.
208 * @param string $portFileContent Portfile content from constructPortAndVersionFile()
209 * with an SHA512 sum of 0 passed in.
210 * @return string SHA512 sum of build output
211 */
212 function firstBuild(string $portFileContent): string
213 {
214 echo GREEN . "Starting first build\n" . WHITE;
215
216 chdir(getenv("HOME") . '/dpp');
217 echo GREEN . "Create /usr/local/share/vcpkg/ports/dpp/\n" . WHITE;
218 $this->sudo('mkdir -p /usr/local/share/vcpkg/ports/dpp/');
219 echo GREEN . "Copy vcpkg.json to /usr/local/share/vcpkg/ports/dpp/vcpkg.json\n" . WHITE;
220 $this->sudo('cp -v -R ./vcpkg/ports/dpp/vcpkg.json /usr/local/share/vcpkg/ports/dpp/vcpkg.json');
221 file_put_contents('/tmp/portfile', $portFileContent);
222 $this->sudo('cp -v -R /tmp/portfile /usr/local/share/vcpkg/ports/dpp/portfile.cmake');
223 unlink('/tmp/portfile');
224 $buildResults = shell_exec($this->sudo . ' /usr/local/share/vcpkg/vcpkg install dpp:x64-linux');
225 $matches = [];
226 if (preg_match('/please change the expected SHA512 to:\s+([0-9a-fA-F]+)/', $buildResults, $matches)) {
227 echo GREEN . "Obtained SHA512 for first build: " . $matches[1] . "\n" . WHITE;
228 $this->firstBuildComplete = true;
229 return $matches[1];
230 }
231 echo RED . "No SHA512 found during first build :(\n" . WHITE;
232 echo "\n\n" . RED . "Build Results:" . WHITE . "\n\n" . $buildResults . "\n\n";
233 return '';
234 }
235
236 /**
237 * Second build using a valid SHA512 sum. This attempt should succeed, allowing us to push
238 * the changed vcpkg portfiles into the master branch, where they can be used in a PR to
239 * microsoft/vcpkg repository later.
240 *
241 * @param string $portFileContent the contents of the portfile, containing a valid SHA512
242 * sum from the first build attempt.
243 * @return bool False if the build failed
244 */
245 function secondBuild(string $portFileContent): bool
246 {
247
248 if (!$this->firstBuildComplete) {
249 throw new RuntimeException("No SHA512 sum is available, first build has not been run!");
250 }
251
252 echo GREEN . "Executing second build\n" . WHITE;
253 echo GREEN . "Copy local port files to /usr/local/share...\n" . WHITE;
254 chdir(getenv("HOME") . '/dpp');
255 file_put_contents('./vcpkg/ports/dpp/portfile.cmake', $portFileContent);
256 $this->sudo('cp -v -R ./vcpkg/ports/dpp/vcpkg.json /usr/local/share/vcpkg/ports/dpp/vcpkg.json');
257 $this->sudo('cp -v -R ./vcpkg/ports/dpp/portfile.cmake /usr/local/share/vcpkg/ports/dpp/portfile.cmake');
258 $this->sudo('cp -v -R ./vcpkg/ports/* /usr/local/share/vcpkg/ports/');
259
260 echo GREEN . "vcpkg x-add-version...\n" . WHITE;
261 chdir('/usr/local/share/vcpkg');
262 $this->sudo('./vcpkg format-manifest ./ports/dpp/vcpkg.json');
263 /* Note: We commit this in /usr/local, but we never push it (we can't) */
264 $this->git('add .', true);
265 $this->git('-c user.name="DPP VCPKG Bot" -c [email protected] commit -m "[bot] VCPKG info update"', true);
266 $this->sudo('/usr/local/share/vcpkg/vcpkg x-add-version dpp');
267
268 echo GREEN . "Copy back port files from /usr/local/share...\n" . WHITE;
269 chdir(getenv('HOME') . '/dpp');
270 $this->git('checkout master');
271 system('cp -v -R /usr/local/share/vcpkg/ports/dpp/vcpkg.json ./vcpkg/ports/dpp/vcpkg.json');
272 system('cp -v -R /usr/local/share/vcpkg/versions/baseline.json ./vcpkg/versions/baseline.json');
273 system('cp -v -R /usr/local/share/vcpkg/versions/d-/dpp.json ./vcpkg/versions/d-/dpp.json');
274
275 echo GREEN . "Commit and push changes to master branch\n" . WHITE;
276 $this->git('config --global user.email "[email protected]"');
277 $this->git('config --global user.name "DPP VCPKG Bot"');
278 $this->git('add .');
279 $this->git('commit -m "[bot] VCPKG info update [skip ci]"');
280 $this->git('config pull.rebase false');
281 $this->git('pull');
282 $this->git('push origin master');
283
284 echo GREEN . "vcpkg install...\n" . WHITE;
285 $resultCode = 0;
286 $output = [];
287 exec($this->sudo . ' /usr/local/share/vcpkg/vcpkg install dpp:x64-linux', $output, $resultCode);
288
289 if ($resultCode != 0) {
290 echo RED . "There were build errors!\n\nBuild log:\n" . WHITE;
291 readfile("/usr/local/share/vcpkg/buildtrees/dpp/install-x64-linux-dbg-out.log");
292 }
293
294 return $resultCode == 0;
295 }
296 };