feat: use electrons sysroot when building with clang on linux (#443)

* feat: use electrons sysroot when building with clang on linux

* chore: fix up linting warnings

* chore: add SHA1 hash validation of the sysroot
This commit is contained in:
Samuel Attard 2020-10-02 13:16:21 -07:00 коммит произвёл GitHub
Родитель bfbcec1b15
Коммит dd22885668
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
6 изменённых файлов: 268 добавлений и 50 удалений

176
package-lock.json сгенерированный
Просмотреть файл

@ -1173,6 +1173,15 @@
"@types/node": "*"
}
},
"@types/lzma-native": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/@types/lzma-native/-/lzma-native-4.0.0.tgz",
"integrity": "sha512-9mBLyFWJe8/whIRjGwca5pbXv7ANzPj7KNgsgCU/l5z8xcyIe6LNae+iKdT5rjnXcXGOwyW/PKYt0N6+7N5QlA==",
"dev": true,
"requires": {
"@types/node": "*"
}
},
"@types/minimist": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/@types/minimist/-/minimist-1.2.0.tgz",
@ -2333,8 +2342,7 @@
"deep-extend": {
"version": "0.6.0",
"resolved": "https://registry.npmjs.org/deep-extend/-/deep-extend-0.6.0.tgz",
"integrity": "sha512-LOHxIOaPYdHlJRtCQfDIVZtfw/ufM8+rVj649RIHzcm/vGwQRXFt6OPqIFWsm2XEMrNIEtWR64sY1LEKD2vAOA==",
"dev": true
"integrity": "sha512-LOHxIOaPYdHlJRtCQfDIVZtfw/ufM8+rVj649RIHzcm/vGwQRXFt6OPqIFWsm2XEMrNIEtWR64sY1LEKD2vAOA=="
},
"deep-is": {
"version": "0.1.3",
@ -3721,6 +3729,14 @@
"integrity": "sha512-SEQu7vl8KjNL2eoGBLF3+wAjpsNfA9XMlXAYj/3EdaNfAlxKthD1xjEQfGOUhllCGGJVNY34bRr6lPINhNjyZw==",
"dev": true
},
"iconv-lite": {
"version": "0.4.24",
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz",
"integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==",
"requires": {
"safer-buffer": ">= 2.1.2 < 3"
}
},
"ignore": {
"version": "4.0.6",
"resolved": "https://registry.npmjs.org/ignore/-/ignore-4.0.6.tgz",
@ -3731,7 +3747,6 @@
"version": "3.0.3",
"resolved": "https://registry.npmjs.org/ignore-walk/-/ignore-walk-3.0.3.tgz",
"integrity": "sha512-m7o6xuOaT1aqheYHKf8W6J5pYH85ZI9w077erOzLje3JsB1gkafkAhHHY19dqjulgIZHFm32Cp5uNZgcQqdJKw==",
"dev": true,
"requires": {
"minimatch": "^3.0.4"
}
@ -3792,8 +3807,7 @@
"ini": {
"version": "1.3.5",
"resolved": "https://registry.npmjs.org/ini/-/ini-1.3.5.tgz",
"integrity": "sha512-RZY5huIKCMRWDUqZlEi72f/lmXKMvuszcMBduliQ3nnWbx9X/ZBQO7DijMEYS9EhHBb2qacRUMtC7svLwe0lcw==",
"dev": true
"integrity": "sha512-RZY5huIKCMRWDUqZlEi72f/lmXKMvuszcMBduliQ3nnWbx9X/ZBQO7DijMEYS9EhHBb2qacRUMtC7svLwe0lcw=="
},
"into-stream": {
"version": "5.1.1",
@ -4170,7 +4184,7 @@
"js-tokens": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz",
"integrity": "sha1-GSA/tZmR35jjoocFDUZHzerzJJk=",
"integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==",
"dev": true
},
"js-yaml": {
@ -4445,6 +4459,27 @@
"yallist": "^4.0.0"
}
},
"lzma-native": {
"version": "6.0.1",
"resolved": "https://registry.npmjs.org/lzma-native/-/lzma-native-6.0.1.tgz",
"integrity": "sha512-O6oWF0xe1AFvOCjU8uOZBZ/lhjaMNwHfVNaqVMqmoQXlRwBcFWpCAToiZOdXcKVMdo/5s/D0a2QgA5laMErxHQ==",
"requires": {
"node-addon-api": "^1.6.0",
"node-pre-gyp": "^0.11.0",
"readable-stream": "^2.3.5",
"rimraf": "^2.7.1"
},
"dependencies": {
"rimraf": {
"version": "2.7.1",
"resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.7.1.tgz",
"integrity": "sha512-uWjbaKIK3T1OSVptzX7Nl6PvQ3qAGtKEtVRjRuazjfL3Bx5eI409VZSqgND+4UNnmzLVdPj9FqFJNPqBZFve4w==",
"requires": {
"glob": "^7.1.3"
}
}
}
},
"macos-release": {
"version": "2.4.1",
"resolved": "https://registry.npmjs.org/macos-release/-/macos-release-2.4.1.tgz",
@ -4661,8 +4696,7 @@
"minimist": {
"version": "1.2.5",
"resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz",
"integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==",
"dev": true
"integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw=="
},
"minimist-options": {
"version": "4.1.0",
@ -4696,7 +4730,6 @@
"version": "0.5.5",
"resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.5.tgz",
"integrity": "sha512-NKmAlESf6jMGym1++R0Ra7wvhV+wFW63FaSOFPwRahvea0gMUcGUhVeAg/0BC0wiv9ih5NYPB1Wn1UEI1/L+xQ==",
"dev": true,
"requires": {
"minimist": "^1.2.5"
}
@ -4898,6 +4931,26 @@
"integrity": "sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=",
"dev": true
},
"needle": {
"version": "2.5.2",
"resolved": "https://registry.npmjs.org/needle/-/needle-2.5.2.tgz",
"integrity": "sha512-LbRIwS9BfkPvNwNHlsA41Q29kL2L/6VaOJ0qisM5lLWsTV3nP15abO5ITL6L81zqFhzjRKDAYjpcBcwM0AVvLQ==",
"requires": {
"debug": "^3.2.6",
"iconv-lite": "^0.4.4",
"sax": "^1.2.4"
},
"dependencies": {
"debug": {
"version": "3.2.6",
"resolved": "https://registry.npmjs.org/debug/-/debug-3.2.6.tgz",
"integrity": "sha512-mel+jf7nrtEl5Pn1Qx46zARXKDpBbvzezse7p7LqINmdoIk8PYP5SySaxEmYv6TZ0JyEKA1hsCId6DIhgITtWQ==",
"requires": {
"ms": "^2.1.1"
}
}
}
},
"neo-async": {
"version": "2.6.2",
"resolved": "https://registry.npmjs.org/neo-async/-/neo-async-2.6.2.tgz",
@ -4924,6 +4977,11 @@
"semver": "^5.4.1"
}
},
"node-addon-api": {
"version": "1.7.2",
"resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-1.7.2.tgz",
"integrity": "sha512-ibPK3iA+vaY1eEjESkQkM0BbCqFOaZMiXRTtdB0u7b4djtY6JnsjvPdUHVMg6xQt3B8fpTTWHI9A+ADjM9frzg=="
},
"node-emoji": {
"version": "1.10.0",
"resolved": "https://registry.npmjs.org/node-emoji/-/node-emoji-1.10.0.tgz",
@ -5025,6 +5083,74 @@
}
}
},
"node-pre-gyp": {
"version": "0.11.0",
"resolved": "https://registry.npmjs.org/node-pre-gyp/-/node-pre-gyp-0.11.0.tgz",
"integrity": "sha512-TwWAOZb0j7e9eGaf9esRx3ZcLaE5tQ2lvYy1pb5IAaG1a2e2Kv5Lms1Y4hpj+ciXJRofIxxlt5haeQ/2ANeE0Q==",
"requires": {
"detect-libc": "^1.0.2",
"mkdirp": "^0.5.1",
"needle": "^2.2.1",
"nopt": "^4.0.1",
"npm-packlist": "^1.1.6",
"npmlog": "^4.0.2",
"rc": "^1.2.7",
"rimraf": "^2.6.1",
"semver": "^5.3.0",
"tar": "^4"
},
"dependencies": {
"chownr": {
"version": "1.1.4",
"resolved": "https://registry.npmjs.org/chownr/-/chownr-1.1.4.tgz",
"integrity": "sha512-jJ0bqzaylmJtVnNgzTeSOs8DPavpbYgEr/b0YL8/2GO3xJEhInFmhKMUnEJQjZumK7KXGFhUy89PrsJWlakBVg=="
},
"fs-minipass": {
"version": "1.2.7",
"resolved": "https://registry.npmjs.org/fs-minipass/-/fs-minipass-1.2.7.tgz",
"integrity": "sha512-GWSSJGFy4e9GUeCcbIkED+bgAoFyj7XF1mV8rma3QW4NIqX9Kyx79N/PF61H5udOV3aY1IaMLs6pGbH71nlCTA==",
"requires": {
"minipass": "^2.6.0"
}
},
"minipass": {
"version": "2.9.0",
"resolved": "https://registry.npmjs.org/minipass/-/minipass-2.9.0.tgz",
"integrity": "sha512-wxfUjg9WebH+CUDX/CdbRlh5SmfZiy/hpkxaRI16Y9W56Pa75sWgd/rvFilSgrauD9NyFymP/+JFV3KwzIsJeg==",
"requires": {
"safe-buffer": "^5.1.2",
"yallist": "^3.0.0"
}
},
"minizlib": {
"version": "1.3.3",
"resolved": "https://registry.npmjs.org/minizlib/-/minizlib-1.3.3.tgz",
"integrity": "sha512-6ZYMOEnmVsdCeTJVE0W9ZD+pVnE8h9Hma/iOwwRDsdQoePpoX56/8B6z3P9VNwppJuBKNRuFDRNRqRWexT9G9Q==",
"requires": {
"minipass": "^2.9.0"
}
},
"tar": {
"version": "4.4.13",
"resolved": "https://registry.npmjs.org/tar/-/tar-4.4.13.tgz",
"integrity": "sha512-w2VwSrBoHa5BsSyH+KxEqeQBAllHhccyMFVHtGtdMpF4W7IRWfZjFiQceJPChOeTsSDVUpER2T8FA93pr0L+QA==",
"requires": {
"chownr": "^1.1.1",
"fs-minipass": "^1.2.5",
"minipass": "^2.8.6",
"minizlib": "^1.2.1",
"mkdirp": "^0.5.0",
"safe-buffer": "^5.1.2",
"yallist": "^3.0.3"
}
},
"yallist": {
"version": "3.1.1",
"resolved": "https://registry.npmjs.org/yallist/-/yallist-3.1.1.tgz",
"integrity": "sha512-a4UGQaWPH59mOXUYnAG2ewncQS4i4F43Tv3JoAM+s2VDAmS9NsK8GpDMLrCHPksFT7h3K6TOoUNn2pb7RoXx4g=="
}
}
},
"node-preload": {
"version": "0.2.1",
"resolved": "https://registry.npmjs.org/node-preload/-/node-preload-0.2.1.tgz",
@ -8583,6 +8709,14 @@
}
}
},
"npm-bundled": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/npm-bundled/-/npm-bundled-1.1.1.tgz",
"integrity": "sha512-gqkfgGePhTpAEgUsGEgcq1rqPXA+tv/aVBlgEzfXwA1yiUJF7xtEt3CtVwOjNYQOVknDk0F20w58Fnm3EtG0fA==",
"requires": {
"npm-normalize-package-bin": "^1.0.1"
}
},
"npm-conf": {
"version": "1.1.3",
"resolved": "https://registry.npmjs.org/npm-conf/-/npm-conf-1.1.3.tgz",
@ -8594,6 +8728,21 @@
"pify": "^3.0.0"
}
},
"npm-normalize-package-bin": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/npm-normalize-package-bin/-/npm-normalize-package-bin-1.0.1.tgz",
"integrity": "sha512-EPfafl6JL5/rU+ot6P3gRSCpPDW5VmIzX959Ob1+ySFUuuYHWHekXpwdUZcKP5C+DS4GEtdJluwBjnsNDl+fSA=="
},
"npm-packlist": {
"version": "1.4.8",
"resolved": "https://registry.npmjs.org/npm-packlist/-/npm-packlist-1.4.8.tgz",
"integrity": "sha512-5+AZgwru5IevF5ZdnFglB5wNlHG1AOOuw28WhUq8/8emhBmLv6jX5by4WJCh7lW0uSYZYS6DXqIsyZVIXRZU9A==",
"requires": {
"ignore-walk": "^3.0.1",
"npm-bundled": "^1.0.1",
"npm-normalize-package-bin": "^1.0.1"
}
},
"npm-run-path": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/npm-run-path/-/npm-run-path-2.0.2.tgz",
@ -9393,7 +9542,6 @@
"version": "1.2.8",
"resolved": "https://registry.npmjs.org/rc/-/rc-1.2.8.tgz",
"integrity": "sha512-y3bGgqKj3QBdxLbLkomlohkvsA8gdAiUQlSBJnBhfn+BPxg4bc62d8TcBW15wavDfgexCgccckhcZvywyQYPOw==",
"dev": true,
"requires": {
"deep-extend": "^0.6.0",
"ini": "~1.3.0",
@ -9652,6 +9800,11 @@
"truncate-utf8-bytes": "^1.0.0"
}
},
"sax": {
"version": "1.2.4",
"resolved": "https://registry.npmjs.org/sax/-/sax-1.2.4.tgz",
"integrity": "sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw=="
},
"semantic-release": {
"version": "17.1.2",
"resolved": "https://registry.npmjs.org/semantic-release/-/semantic-release-17.1.2.tgz",
@ -10321,8 +10474,7 @@
"strip-json-comments": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-2.0.1.tgz",
"integrity": "sha1-PFMZQukIwml8DsNEhYwobHygpgo=",
"dev": true
"integrity": "sha1-PFMZQukIwml8DsNEhYwobHygpgo="
},
"stubs": {
"version": "3.0.0",

Просмотреть файл

@ -43,6 +43,7 @@
"detect-libc": "^1.0.3",
"fs-extra": "^9.0.1",
"got": "^11.7.0",
"lzma-native": "^6.0.1",
"node-abi": "^2.19.1",
"node-gyp": "^7.1.0",
"ora": "^5.1.0",
@ -56,6 +57,7 @@
"@types/chai-as-promised": "^7.1.3",
"@types/debug": "^4.1.5",
"@types/fs-extra": "^9.0.1",
"@types/lzma-native": "^4.0.0",
"@types/mocha": "^8.0.3",
"@types/node": "^14.6.0",
"@types/tar": "^4.0.3",

Просмотреть файл

@ -1,39 +1,15 @@
import * as cp from 'child_process';
import * as debug from 'debug';
import * as fs from 'fs-extra';
import got from 'got';
import * as path from 'path';
import * as tar from 'tar';
import * as zlib from 'zlib';
import { ELECTRON_GYP_DIR } from './constants';
import { fetch } from './fetcher';
import { downloadLinuxSysroot } from './sysroot-fetcher';
const d = debug('electron-rebuild');
function sleep(n: number) {
return new Promise(r => setTimeout(r, n));
}
async function fetch(url: string, responseType: 'buffer' | 'text', retries = 3): Promise<string | Buffer> {
if (retries === 0) throw new Error('Failed to fetch a clang resource, run with DEBUG=electron-rebuild for more information');
d('downloading:', url);
try {
const response = await got.get(url, {
responseType,
});
if (response.statusCode !== 200) {
d('got bad status code:', response.statusCode);
await sleep(2000);
return fetch(url, responseType, retries - 1);
}
d('response came back OK');
return response.body as any;
} catch (err) {
d('request failed for some reason', err);
await sleep(2000);
return fetch(url, responseType, retries - 1);
}
}
const CDS_URL = 'https://commondatastorage.googleapis.com/chromium-browser-clang';
function getPlatformUrlPrefix(hostOS: string) {
@ -56,8 +32,10 @@ function getSDKRoot(): string {
return output.toString().trim();
}
export function getClangEnvironmentVars(electronVersion: string) {
const clangDir = path.resolve(ELECTRON_GYP_DIR, `${electronVersion}-clang`, 'bin');
export async function getClangEnvironmentVars(electronVersion: string, targetArch: string): Promise<{ env: Record<string, string>; args: string[] }> {
const clangDownloadDir = await downloadClangVersion(electronVersion);
const clangDir = path.resolve(clangDownloadDir, 'bin');
const clangArgs: string[] = [];
if (process.platform === 'darwin') {
clangArgs.push('-isysroot', getSDKRoot());
@ -69,6 +47,11 @@ export function getClangEnvironmentVars(electronVersion: string) {
gypArgs.push(`/p:CLToolExe=clang-cl.exe`, `/p:CLToolPath=${clangDir}`);
}
if (process.platform === 'linux') {
const sysrootPath = await downloadLinuxSysroot(electronVersion, targetArch);
clangArgs.push('--sysroot', sysrootPath);
}
return {
env: {
CC: `"${path.resolve(clangDir, 'clang')}" ${clangArgs.join(' ')}`,
@ -94,21 +77,21 @@ function clangVersionFromSVN(update: string): string | null {
return `${clangSvn}-${clangVersion.substr(0, 8)}-${clangSubRevision}`;
}
export async function downloadClangVersion(electronVersion: string) {
async function downloadClangVersion(electronVersion: string) {
d('fetching clang for Electron:', electronVersion);
const clangDirPath = path.resolve(ELECTRON_GYP_DIR, `${electronVersion}-clang`);
if (await fs.pathExists(path.resolve(clangDirPath, 'bin', 'clang'))) return;
if (await fs.pathExists(path.resolve(clangDirPath, 'bin', 'clang'))) return clangDirPath;
if (!await fs.pathExists(ELECTRON_GYP_DIR)) await fs.mkdirp(ELECTRON_GYP_DIR);
const electronDeps = await fetch(`https://raw.githubusercontent.com/electron/electron/v${electronVersion}/DEPS`, 'text');
const chromiumRevisionExtractor = /'chromium_version':\n\s+'([^']+)/g;
const chromiumRevisionMatch = chromiumRevisionExtractor.exec(electronDeps as string);
const chromiumRevisionMatch = chromiumRevisionExtractor.exec(electronDeps);
if (!chromiumRevisionMatch) throw new Error('Failed to determine Chromium revision for given Electron version');
const chromiumRevision = chromiumRevisionMatch[1];
d('fetching clang for Chromium:', chromiumRevision)
const base64ClangUpdate = await fetch(`https://chromium.googlesource.com/chromium/src.git/+/${chromiumRevision}/tools/clang/scripts/update.py?format=TEXT`, 'text');
const clangUpdate = Buffer.from(base64ClangUpdate as string, 'base64').toString('utf8');
const clangUpdate = Buffer.from(base64ClangUpdate, 'base64').toString('utf8');
const clangVersionString = clangVersionFromRevision(clangUpdate) || clangVersionFromSVN(clangUpdate);
if (!clangVersionString) throw new Error('Failed to determine Clang revision from Electron version');
@ -130,4 +113,5 @@ export async function downloadClangVersion(electronVersion: string) {
});
await fs.remove(tarPath);
d('cleaning up clang tar file');
return clangDirPath;
}

29
src/fetcher.ts Normal file
Просмотреть файл

@ -0,0 +1,29 @@
import * as debug from 'debug';
import got from 'got';
const d = debug('electron-rebuild');
function sleep(n: number) {
return new Promise(r => setTimeout(r, n));
}
export async function fetch<T extends 'buffer' | 'text', RT = T extends 'buffer' ? Buffer : string>(url: string, responseType: T, retries = 3): Promise<RT> {
if (retries === 0) throw new Error('Failed to fetch a clang resource, run with DEBUG=electron-rebuild for more information');
d('downloading:', url);
try {
const response = await got.get(url, {
responseType,
});
if (response.statusCode !== 200) {
d('got bad status code:', response.statusCode);
await sleep(2000);
return fetch(url, responseType, retries - 1);
}
d('response came back OK');
return response.body as RT;
} catch (err) {
d('request failed for some reason', err);
await sleep(2000);
return fetch(url, responseType, retries - 1);
}
}

Просмотреть файл

@ -9,7 +9,7 @@ import { readPackageJson } from './read-package-json';
import { Rebuilder } from './rebuild';
import { spawn } from '@malept/cross-spawn-promise';
import { ELECTRON_GYP_DIR } from './constants';
import { downloadClangVersion, getClangEnvironmentVars } from './clang-fetcher';
import { getClangEnvironmentVars } from './clang-fetcher';
const d = debug('electron-rebuild');
@ -165,7 +165,7 @@ export class ModuleRebuilder {
return fs.pathExists(path.resolve(this.modulePath, 'prebuilds', `${process.platform}-${this.rebuilder.arch}`, `electron-${this.rebuilder.ABI}.node`))
}
private restoreEnv(env: any) {
private restoreEnv(env: Record<string, string | undefined>): void {
const gotKeys = new Set<string>(Object.keys(process.env));
const expectedKeys = new Set<string>(Object.keys(env));
@ -191,13 +191,12 @@ export class ModuleRebuilder {
// throw new Error(`node-gyp does not support building modules with spaces in their path, tried to build: ${modulePath}`);
}
let env: any;
let env: Record<string, string | undefined>;
const extraNodeGypArgs: string[] = [];
if (this.rebuilder.useElectronClang) {
env = { ...process.env };
await downloadClangVersion(this.rebuilder.electronVersion);
const { env: clangEnv, args: clangArgs } = getClangEnvironmentVars(this.rebuilder.electronVersion);
const { env: clangEnv, args: clangArgs } = await getClangEnvironmentVars(this.rebuilder.electronVersion, this.rebuilder.arch);
Object.assign(process.env, clangEnv);
extraNodeGypArgs.push(...clangArgs);
}
@ -240,7 +239,7 @@ export class ModuleRebuilder {
await this.cacheModuleState(cacheKey);
if (this.rebuilder.useElectronClang) {
this.restoreEnv(env);
this.restoreEnv(env!);
}
}

52
src/sysroot-fetcher.ts Normal file
Просмотреть файл

@ -0,0 +1,52 @@
import * as crypto from 'crypto';
import * as debug from 'debug';
import * as fs from 'fs-extra';
import * as path from 'path';
import * as tar from 'tar';
import * as lzma from 'lzma-native';
import { ELECTRON_GYP_DIR } from './constants';
import { fetch } from './fetcher';
const d = debug('electron-rebuild');
const sysrootArchAliases = {
x64: 'amd64',
ia32: 'i386',
}
const SYSROOT_BASE_URL = 'https://s3.amazonaws.com/electronjs-sysroots/toolchain'
export async function downloadLinuxSysroot(electronVersion: string, targetArch: string): Promise<string> {
d('fetching sysroot for Electron:', electronVersion);
const sysrootDir = path.resolve(ELECTRON_GYP_DIR, `${electronVersion}-sysroot`);
if (await fs.pathExists(path.resolve(sysrootDir, 'lib'))) return sysrootDir;
if (!await fs.pathExists(sysrootDir)) await fs.mkdirp(sysrootDir);
const linuxArch = sysrootArchAliases[targetArch] || targetArch;
const electronSysroots = JSON.parse(await fetch(`https://raw.githubusercontent.com/electron/electron/v${electronVersion}/script/sysroots.json`, 'text'));
const { Sha1Sum: sha, Tarball: fileName } = electronSysroots[`sid_${linuxArch}`];
const sysrootURL = `${SYSROOT_BASE_URL}/${sha}/${fileName}`;
let sysrootBuffer = await fetch(sysrootURL, 'buffer');
const actualSha = crypto.createHash('SHA1').update(sysrootBuffer).digest('hex');
d('expected sha:', sha);
d('actual sha:', actualSha);
if (sha !== actualSha) throw new Error(`Attempted to download the linux sysroot for ${electronVersion} but the SHA checksum did not match`);
d('decompressing sysroot');
sysrootBuffer = await new Promise<Buffer>(resolve => lzma.decompress(sysrootBuffer, undefined, result => resolve(result)));
const tmpTarFile = path.resolve(ELECTRON_GYP_DIR, `${electronVersion}-${fileName}`);
if (await fs.pathExists(tmpTarFile)) await fs.remove(tmpTarFile);
await fs.writeFile(tmpTarFile, sysrootBuffer);
d('extracting sysroot');
await tar.x({
file: tmpTarFile,
cwd: sysrootDir,
});
return sysrootDir;
}