|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +const { execSync } = require('child_process'); |
| 7 | +const { tmpdir } = require('os'); |
| 8 | +const fs = require('fs'); |
| 9 | +const path = require('path'); |
| 10 | +const { createHash } = require('crypto'); |
| 11 | + |
| 12 | +const REPO_ROOT = path.join(__dirname, '..', '..'); |
| 13 | + |
| 14 | +const ghApiHeaders = { |
| 15 | + Accept: 'application/vnd.github.v3+json', |
| 16 | + 'User-Agent': 'node-pty Build', |
| 17 | +}; |
| 18 | + |
| 19 | +if (process.env.GITHUB_TOKEN) { |
| 20 | + ghApiHeaders.Authorization = 'Basic ' + Buffer.from(process.env.GITHUB_TOKEN).toString('base64'); |
| 21 | +} |
| 22 | + |
| 23 | +const ghDownloadHeaders = { |
| 24 | + ...ghApiHeaders, |
| 25 | + Accept: 'application/octet-stream', |
| 26 | +}; |
| 27 | + |
| 28 | +function getSysrootChecksum(expectedName) { |
| 29 | + const checksumPath = path.join(REPO_ROOT, 'scripts', 'linux', 'checksums.txt'); |
| 30 | + const checksums = fs.readFileSync(checksumPath, 'utf8'); |
| 31 | + for (const line of checksums.split('\n')) { |
| 32 | + const [checksum, name] = line.split(/\s+/); |
| 33 | + if (name === expectedName) { |
| 34 | + return checksum; |
| 35 | + } |
| 36 | + } |
| 37 | + return undefined; |
| 38 | +} |
| 39 | + |
| 40 | +async function fetchUrl(options, retries = 10, retryDelay = 1000) { |
| 41 | + try { |
| 42 | + const controller = new AbortController(); |
| 43 | + const timeout = setTimeout(() => controller.abort(), 30 * 1000); |
| 44 | + const version = '20250407-330404'; |
| 45 | + try { |
| 46 | + const response = await fetch(`https://api.github.com/repos/Microsoft/vscode-linux-build-agent/releases/tags/v${version}`, { |
| 47 | + headers: ghApiHeaders, |
| 48 | + signal: controller.signal |
| 49 | + }); |
| 50 | + if (response.ok && (response.status >= 200 && response.status < 300)) { |
| 51 | + console.log(`Fetch completed: Status ${response.status}.`); |
| 52 | + const contents = Buffer.from(await response.arrayBuffer()); |
| 53 | + const asset = JSON.parse(contents.toString()).assets.find((a) => a.name === options.assetName); |
| 54 | + if (!asset) { |
| 55 | + throw new Error(`Could not find asset in release of Microsoft/vscode-linux-build-agent @ ${version}`); |
| 56 | + } |
| 57 | + console.log(`Found asset ${options.assetName} @ ${asset.url}.`); |
| 58 | + const assetResponse = await fetch(asset.url, { |
| 59 | + headers: ghDownloadHeaders |
| 60 | + }); |
| 61 | + if (assetResponse.ok && (assetResponse.status >= 200 && assetResponse.status < 300)) { |
| 62 | + const assetContents = Buffer.from(await assetResponse.arrayBuffer()); |
| 63 | + console.log(`Fetched response body buffer: ${assetContents.byteLength} bytes`); |
| 64 | + if (options.checksumSha256) { |
| 65 | + const actualSHA256Checksum = createHash('sha256').update(assetContents).digest('hex'); |
| 66 | + if (actualSHA256Checksum !== options.checksumSha256) { |
| 67 | + throw new Error(`Checksum mismatch for ${asset.url} (expected ${options.checksumSha256}, actual ${actualSHA256Checksum})`); |
| 68 | + } |
| 69 | + } |
| 70 | + console.log(`Verified SHA256 checksums match for ${asset.url}`); |
| 71 | + const tarCommand = `tar -xz -C ${options.dest}`; |
| 72 | + execSync(tarCommand, { input: assetContents }); |
| 73 | + console.log(`Fetch complete!`); |
| 74 | + return; |
| 75 | + } |
| 76 | + throw new Error(`Request ${asset.url} failed with status code: ${assetResponse.status}`); |
| 77 | + } |
| 78 | + throw new Error(`Request https://api.github.com failed with status code: ${response.status}`); |
| 79 | + } finally { |
| 80 | + clearTimeout(timeout); |
| 81 | + } |
| 82 | + } catch (e) { |
| 83 | + if (retries > 0) { |
| 84 | + console.log(`Fetching failed: ${e}`); |
| 85 | + await new Promise(resolve => setTimeout(resolve, retryDelay)); |
| 86 | + return fetchUrl(options, retries - 1, retryDelay); |
| 87 | + } |
| 88 | + throw e; |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +async function getSysroot(arch) { |
| 93 | + let expectedName; |
| 94 | + let triple; |
| 95 | + const prefix = '-glibc-2.28-gcc-10.5.0'; |
| 96 | + |
| 97 | + switch (arch) { |
| 98 | + case 'x64': |
| 99 | + expectedName = `x86_64-linux-gnu${prefix}.tar.gz`; |
| 100 | + triple = 'x86_64-linux-gnu'; |
| 101 | + break; |
| 102 | + case 'arm64': |
| 103 | + expectedName = `aarch64-linux-gnu${prefix}.tar.gz`; |
| 104 | + triple = 'aarch64-linux-gnu'; |
| 105 | + break; |
| 106 | + default: |
| 107 | + throw new Error(`Unsupported architecture: ${arch}`); |
| 108 | + } |
| 109 | + |
| 110 | + console.log(`Fetching ${expectedName} for ${triple}`); |
| 111 | + const checksumSha256 = getSysrootChecksum(expectedName); |
| 112 | + if (!checksumSha256) { |
| 113 | + throw new Error(`Could not find checksum for ${expectedName}`); |
| 114 | + } |
| 115 | + |
| 116 | + const sysroot = path.join(tmpdir(), `vscode-${arch}-sysroot`); |
| 117 | + const stamp = path.join(sysroot, '.stamp'); |
| 118 | + const result = `${sysroot}/${triple}/${triple}/sysroot`; |
| 119 | + |
| 120 | + if (fs.existsSync(stamp) && fs.readFileSync(stamp).toString() === expectedName) { |
| 121 | + console.log(`Sysroot already installed: ${result}`); |
| 122 | + return result; |
| 123 | + } |
| 124 | + |
| 125 | + console.log(`Installing ${arch} root image: ${sysroot}`); |
| 126 | + fs.rmSync(sysroot, { recursive: true, force: true }); |
| 127 | + fs.mkdirSync(sysroot, { recursive: true }); |
| 128 | + |
| 129 | + await fetchUrl({ |
| 130 | + checksumSha256, |
| 131 | + assetName: expectedName, |
| 132 | + dest: sysroot |
| 133 | + }); |
| 134 | + |
| 135 | + fs.writeFileSync(stamp, expectedName); |
| 136 | + console.log(`Sysroot installed: ${result}`); |
| 137 | + return result; |
| 138 | +} |
| 139 | + |
| 140 | +async function main() { |
| 141 | + const arch = process.argv[2] || process.env.ARCH || 'x64'; |
| 142 | + console.log(`Installing sysroot for architecture: ${arch}`); |
| 143 | + |
| 144 | + try { |
| 145 | + const sysrootPath = await getSysroot(arch); |
| 146 | + console.log(`SYSROOT_PATH=${sysrootPath}`); |
| 147 | + } catch (error) { |
| 148 | + console.error('Error installing sysroot:', error); |
| 149 | + process.exit(1); |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +if (require.main === module) { |
| 154 | + main(); |
| 155 | +} |
| 156 | + |
| 157 | +module.exports = { getSysroot }; |
0 commit comments