Skip to content

Commit 98645e9

Browse files
committed
Feature(CLI): Do llc & cc step automatically
1 parent 6efface commit 98645e9

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

src/cli.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,20 @@ import * as cli from "commander";
77
import {initializeLLVM, generateModuleFromProgram} from './backend/llvm';
88
import DiagnosticHostInstance from "./diagnostic.host";
99
import UnsupportedError from "./backend/error/unsupported.error";
10+
import {existsSync, mkdirSync, unlinkSync} from "fs";
11+
import {execFileSync} from "child_process";
1012

1113
interface CommandLineArguments {
1214
args: string[];
1315
printIR?: boolean;
16+
outputFile?: boolean;
1417
}
1518

1619
function parseCommandLine(): CommandLineArguments {
1720
cli
1821
.version('next')
1922
.option('-ir, --printIR', 'Print IR')
23+
.option('-o, --outputFile', 'Name of the executable file')
2024
.parse(process.argv);
2125

2226
return cli as any as CommandLineArguments;
@@ -55,6 +59,36 @@ try {
5559
if (cliOptions.printIR) {
5660
console.log(llvmModule.print());
5761
}
62+
63+
const outputPath = path.join(process.cwd(), 'output');
64+
65+
if (!existsSync(outputPath)) {
66+
mkdirSync(outputPath);
67+
}
68+
69+
try {
70+
llvm.writeBitcodeToFile(llvmModule, path.join(outputPath, 'main.ll'));
71+
72+
const optimizationLevel = "-O3";
73+
74+
execFileSync('llc', [
75+
optimizationLevel,
76+
'-filetype=obj', path.join(outputPath, 'main.ll'),
77+
'-o', path.join(outputPath, 'main.o')
78+
]);
79+
execFileSync("cc", [
80+
optimizationLevel,
81+
path.join(outputPath, 'main.o'),
82+
path.join(path.dirname(__filename), '..', 'packages', 'runtime', 'libhlvm-runtime.a'),
83+
'-o', path.join(outputPath, 'main'),
84+
'-lstdc++',
85+
'-std=c++11',
86+
'-Werror',
87+
'-v',
88+
]);
89+
} finally {
90+
// unlinkSync(outputPath);
91+
}
5892
} catch (e) {
5993
if (e instanceof UnsupportedError) {
6094
console.log(ts.formatDiagnostic(e.toDiagnostic(), DiagnosticHostInstance));
@@ -64,3 +98,4 @@ try {
6498

6599
throw e;
66100
}
101+

0 commit comments

Comments
 (0)