-
-
Notifications
You must be signed in to change notification settings - Fork 213
Expand file tree
/
Copy pathcompiler.ts
More file actions
413 lines (385 loc) · 12.9 KB
/
compiler.ts
File metadata and controls
413 lines (385 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import { getLanguage, ocHide, ocShow, ocWrite } from './utils';
import { Language } from './types';
import { spawn, SpawnOptionsWithoutStdio } from 'child_process';
import { platform } from 'os';
import path from 'path';
import {
getCOutputArgPref,
getCppOutputArgPref,
getSaveLocationPref,
getHideStderrorWhenCompiledOK,
} from './preferences';
import * as vscode from 'vscode';
import { getJudgeViewProvider } from './extension';
import { toAsciiFilename } from './utilsPure';
export let onlineJudgeEnv = false;
export const setOnlineJudgeEnv = (value: boolean) => {
onlineJudgeEnv = value;
globalThis.logger.log('online judge env:', onlineJudgeEnv);
};
/**
* Get the location to save the generated binary in. If save location is
* available in preferences, returns that, otherwise returns the directory of
* active file.
*
* If it is a interpteted language, simply returns the path to the source code.
*
* @param srcPath location of the source code
*/
export const getBinSaveLocation = (srcPath: string): string => {
const language = getLanguage(srcPath);
if (language.skipCompile) {
return srcPath;
}
let ext: string;
switch (language.name) {
case 'java': {
ext = '*.class';
break;
}
case 'csharp': {
ext = language.compiler.includes('dotnet') ? '_bin' : '.bin';
break;
}
default: {
ext = '.bin';
}
}
const savePreference = getSaveLocationPref();
const srcFileName = path.parse(srcPath).name;
const binFileName = toAsciiFilename(srcFileName) + ext;
const binDir = path.dirname(srcPath);
if (
savePreference &&
savePreference !== '' &&
!savePreference.includes('${')
) {
return path.join(savePreference, binFileName);
}
return path.join(binDir, binFileName);
};
/**
* Get the complete list of required arguments to be passed to the compiler.
* Loads additional args from preferences if available.
*
* @param language The Language object for the source code
* @param srcPath location of the source code
*/
const getFlags = (language: Language, srcPath: string): string[] => {
// The language.args are fetched from user saved preferences, if any.
let args = language.args;
if (args[0] === '') args = [];
let ret: string[];
switch (language.name) {
case 'cpp':
case 'cc':
case 'cxx': {
ret = [
srcPath,
getCppOutputArgPref(),
getBinSaveLocation(srcPath),
...args,
'-D',
'DEBUG',
'-D',
'CPH',
];
if (onlineJudgeEnv) {
ret.push('-D');
ret.push('ONLINE_JUDGE');
}
break;
}
case 'c': {
{
ret = [
srcPath,
getCOutputArgPref(),
getBinSaveLocation(srcPath),
...args,
];
if (onlineJudgeEnv) {
ret.push('-D');
ret.push('ONLINE_JUDGE');
}
break;
}
}
case 'rust': {
ret = [srcPath, '-o', getBinSaveLocation(srcPath), ...args];
break;
}
case 'go': {
ret = [
'build',
'-o',
getBinSaveLocation(srcPath),
srcPath,
...args,
];
break;
}
case 'java': {
const binDir = path.dirname(getBinSaveLocation(srcPath));
ret = [srcPath, '-d', binDir, ...args];
break;
}
case 'hs': {
ret = [
srcPath,
'-o',
getBinSaveLocation(srcPath),
'-no-keep-hi-files',
'-no-keep-o-files',
...args,
];
break;
}
case 'csharp': {
const projDir = getDotnetProjectLocation(language, srcPath);
const binPath = getBinSaveLocation(srcPath);
if (language.compiler.includes('dotnet')) {
ret = [
'build',
projDir,
'-c',
'Release',
'-o',
binPath,
'--force',
...args,
];
if (onlineJudgeEnv) {
ret.push('/p:DefineConstants="TRACE;ONLINE_JUDGE"');
}
} else {
// mcs will run on shell, need to wrap paths with quotes.
// otherwise it will raise error when the paths contain whitespace.
let wrpSrcPath = srcPath;
let wrpBinPath = binPath;
if (platform() === 'win32') {
wrpSrcPath = '"' + srcPath + '"';
wrpBinPath = '"' + binPath + '"';
}
ret = [wrpSrcPath, '-out:' + wrpBinPath, ...args];
if (onlineJudgeEnv) {
ret.push('-define:ONLINE_JUDGE');
}
}
break;
}
default: {
ret = [];
break;
}
}
return ret;
};
/**
* Get the path of the .NET project file (*.csproj) from the location of the source code.
* If the compiler is not dotnet, simply returns the path to the source code.
*
* @param language The Language object for the source code
* @returns location of the .NET project file (*.csproj)
*/
const getDotnetProjectLocation = (
language: Language,
srcPath: string,
): string => {
if (!language.compiler.includes('dotnet')) {
return srcPath;
}
const projName = '.cphcsrun';
const srcDir = path.dirname(srcPath);
const projDir = path.join(srcDir, projName);
return path.join(projDir, projName + '.csproj');
};
/**
* Create a new .NET project to compile the source code.
* It would be created under the same directory as the code.
*
* @param language The Language object for the source code
* @param srcPath location of the source code
*/
const createDotnetProject = async (
language: Language,
srcPath: string,
): Promise<boolean> => {
const result = new Promise<boolean>((resolve) => {
const projDir = path.dirname(
getDotnetProjectLocation(language, srcPath),
);
globalThis.logger.log('Creating new .NET project');
const args = ['new', 'console', '--force', '-o', projDir];
const newProj = spawn(language.compiler, args);
let error = '';
newProj.stderr.on('data', (data) => {
error += data;
});
newProj.on('exit', (exitcode) => {
const exitCode = exitcode || 0;
const hideWarningsWhenCompiledOK = getHideStderrorWhenCompiledOK();
if (exitCode !== 0) {
ocWrite(
`Exit code: ${exitCode} Errors while creating new .NET project:\n` +
error,
);
ocShow();
resolve(false);
return;
}
if (!hideWarningsWhenCompiledOK && error.trim() !== '') {
ocWrite(
`Exit code: ${exitCode} Warnings while creating new .NET project:\n ` +
error,
);
ocShow();
}
const destPath = path.join(projDir, 'Program.cs');
globalThis.logger.log(
'Copying source code to the project',
srcPath,
destPath,
);
try {
const isLinux = platform() == 'linux';
if (isLinux) {
spawn('cp', ['-f', srcPath, destPath]);
} else {
const wrpSrcPath = '"' + srcPath + '"';
const wrpDestPath = '"' + destPath + '"';
spawn(
'cmd.exe',
['/c', 'copy', '/y', wrpSrcPath, wrpDestPath],
{ windowsVerbatimArguments: true },
);
}
resolve(true);
} catch (err) {
globalThis.logger.error('Error while copying source code', err);
ocWrite('Errors while creating new .NET project:\n' + err);
ocShow();
resolve(false);
}
});
newProj.on('error', (err) => {
globalThis.logger.log(err);
ocWrite('Errors while creating new .NET project:\n' + err);
ocShow();
resolve(false);
});
});
return result;
};
/**
* Compile a source file, storing the output binary in a location based on user
* preferences. If `skipCompile` is true for a language, skips the compilation
* and resolves true. If there is no preference, stores in the current
* directory. Resolves true if it succeeds, false otherwise.
*
* Saves the file before compilation starts.
*
* @param srcPath location of the source code
*/
export const compileFile = async (srcPath: string): Promise<boolean> => {
globalThis.logger.log('Compilation Started');
await vscode.workspace.openTextDocument(srcPath).then((doc) => doc.save());
ocHide();
const language: Language = getLanguage(srcPath);
if (language.skipCompile) {
return Promise.resolve(true);
}
const spawnOpts: SpawnOptionsWithoutStdio = {
cwd: undefined,
env: process.env,
};
if (language.name === 'csharp') {
if (language.compiler.includes('dotnet')) {
const projResult = await createDotnetProject(language, srcPath);
if (!projResult) {
getJudgeViewProvider().extensionToJudgeViewMessage({
command: 'compiling-stop',
});
getJudgeViewProvider().extensionToJudgeViewMessage({
command: 'not-running',
});
return Promise.resolve(false);
}
} else {
// HACK: Mono only provides mcs.bat for Windows?
// spawn cannot run mcs.bat :(
if (platform() === 'win32') {
spawnOpts.shell = true;
}
}
}
getJudgeViewProvider().extensionToJudgeViewMessage({
command: 'compiling-start',
});
const flags: string[] = getFlags(language, srcPath);
globalThis.logger.log('Compiling with flags', flags);
const result = new Promise<boolean>((resolve) => {
let compiler;
try {
compiler = spawn(language.compiler, flags, spawnOpts);
} catch (err) {
vscode.window.showErrorMessage(
`Could not launch the compiler ${language.compiler}. Is it installed?`,
);
throw err;
}
let error = '';
compiler.stderr.on('data', (data) => {
error += data;
});
compiler.on('error', (err) => {
globalThis.logger.error(err);
ocWrite(
'Errors while compiling:\n' +
err.message +
`\n\nHint: Is the compiler ${language.compiler} installed? Check the compiler command in cph settings for the current language.`,
);
getJudgeViewProvider().extensionToJudgeViewMessage({
command: 'compiling-stop',
});
getJudgeViewProvider().extensionToJudgeViewMessage({
command: 'not-running',
});
ocShow();
resolve(false);
});
compiler.on('exit', (exitcode) => {
const exitCode = exitcode || 0;
const hideWarningsWhenCompiledOK = getHideStderrorWhenCompiledOK();
if (exitCode !== 0) {
ocWrite(
`Exit code: ${exitCode} Errors while compiling:\n` + error,
);
ocShow();
globalThis.logger.error('Compilation failed');
getJudgeViewProvider().extensionToJudgeViewMessage({
command: 'compiling-stop',
});
getJudgeViewProvider().extensionToJudgeViewMessage({
command: 'not-running',
});
resolve(false);
return;
}
if (!hideWarningsWhenCompiledOK && error.trim() !== '') {
ocWrite(
`Exit code: ${exitCode} Warnings while compiling:\n ` +
error,
);
ocShow();
}
globalThis.logger.log('Compilation passed');
getJudgeViewProvider().extensionToJudgeViewMessage({
command: 'compiling-stop',
});
resolve(true);
return;
});
});
return result;
};