forked from ovr/StaticScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.ts
More file actions
110 lines (87 loc) · 3.05 KB
/
cli.ts
File metadata and controls
110 lines (87 loc) · 3.05 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
import * as ts from 'typescript';
import * as path from 'path';
import * as llvm from 'llvm-node';
import * as cli from "commander";
import {RUNTIME_ARCHIVE_FILE, RUNTIME_DEFINITION_FILE} from "@static-script/runtime";
import {initializeLLVM, generateModuleFromProgram} from './backend/llvm';
import DiagnosticHostInstance from "./diagnostic.host";
import UnsupportedError from "./backend/error/unsupported.error";
import {existsSync, mkdirSync} from "fs";
import {execFileSync} from "child_process";
import {executeLLCSync, executeOptSync} from "./utils";
interface CommandLineArguments {
args: string[];
printIR?: boolean;
outputFile?: string;
optimizationLevel?: string;
}
function parseCommandLine(): CommandLineArguments {
cli
.version('next')
.option('-ir, --printIR', 'Print IR', false)
.option('-f, --outputFile <n>', 'Name of the executable file', 'main')
.option('-o, --optimizationLevel <n>', 'Optimization level', 3)
.parse(process.argv);
return cli as any as CommandLineArguments;
}
const cliOptions = parseCommandLine();
const options: ts.CompilerOptions = {
target: ts.ScriptTarget.ES2018,
jsx: ts.JsxEmit.None,
lib: [
path.join(__dirname, '..', 'staticscript.d.ts'),
RUNTIME_DEFINITION_FILE,
],
types: []
};
const files = cliOptions.args;
const host = ts.createCompilerHost(options);
const program = ts.createProgram(files, options, host);
const diagnostics = ts.getPreEmitDiagnostics(program);
if (diagnostics.length) {
ts.sys.write(ts.formatDiagnosticsWithColorAndContext(diagnostics, DiagnosticHostInstance));
ts.sys.exit(ts.ExitStatus.DiagnosticsPresent_OutputsSkipped);
}
initializeLLVM();
try {
const llvmModule = generateModuleFromProgram(program);
llvm.verifyModule(llvmModule);
if (cliOptions.printIR) {
console.log(llvmModule.print());
}
const outputPath = path.join(process.cwd(), 'output');
if (!existsSync(outputPath)) {
mkdirSync(outputPath);
}
llvm.writeBitcodeToFile(llvmModule, path.join(outputPath, 'main.bc'));
const optimizationLevel = `-O${cliOptions.optimizationLevel}`;
executeOptSync([
optimizationLevel,
path.join(outputPath, 'main.bc'),
'-o', path.join(outputPath, 'main.bc')
]);
executeLLCSync([
optimizationLevel,
// Fully relocatable, position independent code
'-relocation-model=pic',
'-filetype=obj', path.join(outputPath, 'main.bc'),
'-o', path.join(outputPath, 'main.o'),
]);
execFileSync("c++", [
optimizationLevel,
path.join(outputPath, 'main.o'),
RUNTIME_ARCHIVE_FILE,
'-o', path.join(outputPath, cliOptions.outputFile),
'-lstdc++',
'-std=c++11',
'-Werror',
'-pthread',
'-v',
]);
} catch (e) {
if (e instanceof UnsupportedError) {
ts.sys.write(ts.formatDiagnosticsWithColorAndContext([e.toDiagnostic()], DiagnosticHostInstance));
ts.sys.exit(ts.ExitStatus.DiagnosticsPresent_OutputsSkipped);
}
throw e;
}