2024-02-17 15:01:29 -05:00
|
|
|
import { join } from 'path';
|
|
|
|
import { exec, spawn } from 'child_process';
|
|
|
|
import util from 'util';
|
2024-03-11 13:32:23 -04:00
|
|
|
import type { IRunner, IRunnerParams, IRunnerReturn, RunResult } from './types.cjs';
|
|
|
|
import { timeoutSeconds } from './settings.cjs';
|
2024-02-17 15:01:29 -05:00
|
|
|
import kill from 'tree-kill';
|
2024-02-17 15:12:54 -05:00
|
|
|
import os from 'os';
|
|
|
|
import fs from 'fs-extra';
|
2024-02-17 15:01:29 -05:00
|
|
|
|
|
|
|
const execPromise = util.promisify(exec);
|
|
|
|
|
|
|
|
export type CppPlatform = 'VisualStudio' | 'GCC';
|
|
|
|
|
|
|
|
interface IRunnerParamsCpp extends IRunnerParams {
|
|
|
|
srcDir: string;
|
|
|
|
problemName: string;
|
|
|
|
input: string;
|
|
|
|
cppPlatform: CppPlatform;
|
|
|
|
outputCallback?: (data: string) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const runCpp: IRunner<IRunnerParamsCpp> = async function (
|
|
|
|
params: IRunnerParamsCpp
|
2024-03-11 13:32:23 -04:00
|
|
|
): Promise<IRunnerReturn> {
|
2024-02-17 15:12:54 -05:00
|
|
|
const tmpDir = os.tmpdir();
|
|
|
|
const buildDir = join(tmpDir, 'bwcontest-cpp');
|
|
|
|
if (fs.existsSync(buildDir)) {
|
|
|
|
fs.removeSync(buildDir);
|
|
|
|
}
|
|
|
|
fs.mkdirSync(buildDir);
|
|
|
|
|
2024-02-17 15:01:29 -05:00
|
|
|
console.log(`- BUILD: ${params.problemName}`);
|
|
|
|
|
2024-02-17 15:12:54 -05:00
|
|
|
const configureCommand = `cmake -S ${params.srcDir} -B ${buildDir}`;
|
2024-02-17 15:01:29 -05:00
|
|
|
try {
|
|
|
|
await execPromise(configureCommand);
|
|
|
|
} catch (e) {
|
|
|
|
const buildErrorText = e?.toString() ?? 'Unknown build errors.';
|
|
|
|
console.log('Build errors: ' + buildErrorText);
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
runResult: { kind: 'CompileFailed', resultKindReason: buildErrorText }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-02-17 15:12:54 -05:00
|
|
|
const compileCommand = `cmake --build ${buildDir} --target ${params.problemName}`;
|
2024-02-17 15:01:29 -05:00
|
|
|
try {
|
|
|
|
await execPromise(compileCommand);
|
|
|
|
} catch (e) {
|
|
|
|
const buildErrorText = e?.toString() ?? 'Unknown build errors.';
|
|
|
|
console.log('Build errors: ' + buildErrorText);
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
runResult: { kind: 'CompileFailed', resultKindReason: buildErrorText }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(`- RUN: ${params.problemName}`);
|
|
|
|
|
|
|
|
let runCommand = '';
|
|
|
|
if (params.cppPlatform === 'VisualStudio') {
|
2024-02-17 15:12:54 -05:00
|
|
|
runCommand = `${join(buildDir, 'Debug', `${params.problemName}.exe`)}`;
|
2024-02-17 15:01:29 -05:00
|
|
|
} else {
|
2024-02-17 15:12:54 -05:00
|
|
|
runCommand = `${join(buildDir, params.problemName)}`;
|
2024-02-17 15:01:29 -05:00
|
|
|
}
|
|
|
|
try {
|
|
|
|
let outputBuffer = '';
|
|
|
|
const child = spawn(runCommand, { shell: true });
|
|
|
|
child.stdout.setEncoding('utf8');
|
|
|
|
child.stdout.on('data', (data) => {
|
|
|
|
outputBuffer += data.toString();
|
|
|
|
});
|
|
|
|
child.stderr.setEncoding('utf8');
|
|
|
|
child.stderr.on('data', (data) => {
|
|
|
|
outputBuffer += data.toString();
|
|
|
|
});
|
|
|
|
|
2024-02-17 15:49:30 -05:00
|
|
|
const runStartTime = performance.now();
|
2024-02-17 15:01:29 -05:00
|
|
|
child.stdin.write(params.input);
|
|
|
|
child.stdin.end();
|
|
|
|
|
|
|
|
let timeLimitExceeded = false;
|
|
|
|
let completedNormally = false;
|
|
|
|
|
|
|
|
return {
|
|
|
|
success: true,
|
2024-02-17 15:49:30 -05:00
|
|
|
runResult: new Promise<RunResult>((resolve) => {
|
2024-02-17 15:01:29 -05:00
|
|
|
child.on('close', () => {
|
|
|
|
completedNormally = !timeLimitExceeded;
|
|
|
|
|
2024-02-17 15:49:30 -05:00
|
|
|
const runEndTime = performance.now();
|
2024-02-17 15:01:29 -05:00
|
|
|
const runtimeMilliseconds = Math.floor(runEndTime - runStartTime);
|
|
|
|
|
|
|
|
if (completedNormally) {
|
|
|
|
clearTimeout(timeoutHandle);
|
|
|
|
resolve({
|
|
|
|
kind: 'Completed',
|
|
|
|
output: outputBuffer,
|
|
|
|
exitCode: child.exitCode!,
|
|
|
|
runtimeMilliseconds
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
console.log(`Process terminated, total sandbox time: ${runtimeMilliseconds}ms`);
|
|
|
|
resolve({
|
|
|
|
kind: 'TimeLimitExceeded',
|
|
|
|
output: outputBuffer,
|
|
|
|
resultKindReason: `Timeout after ${timeoutSeconds} seconds`
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-02-17 15:49:30 -05:00
|
|
|
const timeoutHandle = setTimeout(() => {
|
2024-02-17 15:01:29 -05:00
|
|
|
if (completedNormally) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(`Run timed out after ${timeoutSeconds} seconds, killing process...`);
|
|
|
|
timeLimitExceeded = true;
|
|
|
|
|
|
|
|
child.stdin.end();
|
|
|
|
child.stdin.destroy();
|
|
|
|
child.stdout.destroy();
|
|
|
|
child.stderr.destroy();
|
|
|
|
child.kill('SIGKILL');
|
|
|
|
}, timeoutSeconds * 1000);
|
|
|
|
}),
|
|
|
|
killFunc() {
|
|
|
|
if (child.pid !== undefined) {
|
|
|
|
if (!completedNormally && !timeLimitExceeded) {
|
|
|
|
kill(child.pid);
|
|
|
|
params.outputCallback?.('\n[Manually stopped]');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} catch (error) {
|
|
|
|
return { success: false, runResult: { kind: 'RunError' } };
|
|
|
|
}
|
|
|
|
};
|