'use strict';
import { spawnSync } from 'child_process';
import path from 'path';
import { getChromiumSysroot, getVSCodeSysroot } from './debian/install-sysroot';
import { generatePackageDeps as generatePackageDepsDebian } from './debian/calculate-deps';
import { generatePackageDeps as generatePackageDepsRpm } from './rpm/calculate-deps';
import { referenceGeneratedDepsByArch as debianGeneratedDeps } from './debian/dep-lists';
import { referenceGeneratedDepsByArch as rpmGeneratedDeps } from './rpm/dep-lists';
import { DebianArchString, isDebianArchString } from './debian/types';
import { isRpmArchString, RpmArchString } from './rpm/types';
import product = require('../../product.json');
const FAIL_BUILD_FOR_NEW_DEPENDENCIES: boolean = true;
const bundledDeps = [
'libEGL.so',
'libGLESv2.so',
'libvulkan.so.1',
'libvk_swiftshader.so',
'libffmpeg.so'
];
export async function getDependencies(packageType: 'deb' | 'rpm', buildDir: string, applicationName: string, arch: string): Promise<string[]> {
if (packageType === 'deb') {
if (!isDebianArchString(arch)) {
throw new Error('Invalid Debian arch string ' + arch);
}
}
if (packageType === 'rpm' && !isRpmArchString(arch)) {
throw new Error('Invalid RPM arch string ' + arch);
}
const canAsar = false;
const nativeModulesPath = path.join(buildDir, 'resources', 'app', canAsar ? 'node_modules.asar.unpacked' : 'node_modules');
const findResult = spawnSync('find', [nativeModulesPath, '-name', '*.node']);
if (findResult.status) {
console.error('Error finding files:');
console.error(findResult.stderr.toString());
return [];
}
const appPath = path.join(buildDir, applicationName);
const files = findResult.stdout.toString().trimEnd().split('\n');
files.push(path.join(buildDir, 'bin', product.tunnelApplicationName));
files.push(appPath);
files.push(path.join(buildDir, 'chrome-sandbox'));
files.push(path.join(buildDir, 'chrome_crashpad_handler'));
let dependencies: Set<string>[];
if (packageType === 'deb') {
const chromiumSysroot = await getChromiumSysroot(arch as DebianArchString);
const vscodeSysroot = await getVSCodeSysroot(arch as DebianArchString);
dependencies = generatePackageDepsDebian(files, arch as DebianArchString, chromiumSysroot, vscodeSysroot);
} else {
dependencies = generatePackageDepsRpm(files);
}
const mergedDependencies = mergePackageDeps(dependencies);
const sortedDependencies: string[] = Array.from(mergedDependencies).filter(dependency => {
return !bundledDeps.some(bundledDep => dependency.startsWith(bundledDep));
}).sort();
const referenceGeneratedDeps = packageType === 'deb' ?
debianGeneratedDeps[arch as DebianArchString] :
rpmGeneratedDeps[arch as RpmArchString];
if (JSON.stringify(sortedDependencies) !== JSON.stringify(referenceGeneratedDeps)) {
const failMessage = 'The dependencies list has changed.'
+ '\nOld:\n' + referenceGeneratedDeps.join('\n')
+ '\nNew:\n' + sortedDependencies.join('\n');
if (FAIL_BUILD_FOR_NEW_DEPENDENCIES) {
throw new Error(failMessage);
} else {
console.warn(failMessage);
}
}
return sortedDependencies;
}
function mergePackageDeps(inputDeps: Set<string>[]): Set<string> {
const requires = new Set<string>();
for (const depSet of inputDeps) {
for (const dep of depSet) {
const trimmedDependency = dep.trim();
if (trimmedDependency.length && !trimmedDependency.startsWith('#')) {
requires.add(trimmedDependency);
}
}
}
return requires;
}