-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathtestCompiler.js
167 lines (139 loc) · 4.69 KB
/
testCompiler.js
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
#!/usr/bin/env node
var fs = require('fs'),
path = require('path'),
child_process = require('child_process'),
crypto = require('crypto'),
jam = require('./lib/ohm/jamscript/jam');
var args = process.argv.slice(2);
var tmpDir = "/tmp/jam-" + randomValueHex(20);
var jsPath;
var cPath;
var preprocessDecls;
for (var i = 0; i < args.length; i++) {
var inputPath = args[i];
var extension = path.extname(inputPath);
if (extension === '.js') {
jsPath = inputPath;
} else if (extension === '.c') {
cPath = inputPath;
}
}
var inputError = false;
if (cPath === undefined) {
console.error("Error: C input file not specified");
inputError = true;
} else if (!fs.existsSync(cPath)) {
console.error("File not found: " + cPath);
inputError = true;
}
if (jsPath === undefined) {
console.error("Error: JavaScript input file not specified");
inputError = true;
} else if (!fs.existsSync(jsPath)) {
console.error("File not found: " + jsPath);
inputError = true;
}
if (inputError) {
process.exit(1);
}
try {
fs.mkdirSync(tmpDir);
try {
var preprocessedOutput = preprocess(cPath, false);
var preprocessed = preprocessedOutput.program;
var lineNumber = preprocessedOutput.lineNumber;
} catch (e) {
console.log(e);
console.log("Exiting with preprocessor error");
process.exit();
}
var results = jam.compile(preprocessed, fs.readFileSync(jsPath).toString(), lineNumber);
var tasks = [
compile(results.C, false)
];
Promise.all(tasks).then(
function(value) {
deleteFolderRecursive(tmpDir);
console.log(value);
},
function(error) {
console.log(error);
}
);
} catch(e) {
console.log("ERROR:");
console.log(e);
}
function compile(code, verbose) {
return new Promise(function(resolve, reject) {
// Set platform options
var options = "";
if (process.platform === "darwin") {
// Mac
options = "-framework CoreFoundation";
} else {
// Linux
options = "-lm -lbsd";
}
var includes = '#include "jam.h"\n';
includes = '#include "command.h"\n' + includes;
includes = '#include "jamdata.h"\n' + includes;
includes = '#include "jamdevices.h"\n' + includes;
includes = '#include <unistd.h>\n' + includes;
fs.writeFileSync("jamout.c", includes + preprocessDecls.join("\n") + "\n" + code);
fs.writeFileSync(`${tmpDir}/jamout.c`, includes + preprocessDecls.join("\n") + "\n" + code);
try {
var command = `clang -g ${tmpDir}/jamout.c -o ${tmpDir}/a.out -I/usr/local/include -I/usr/local/share/jam/lib/ ${options} -pthread -lcbor -lnanomsg /usr/local/lib/libjam.a -ltask -levent -lhiredis -lmujs -L/usr/local/lib -lpaho-mqtt3a`;
console.log("Compiling C code...");
// This prints any errors to stderr automatically, so no need to show the error again
child_process.execSync(command, {
stdio: [0, 1, 2]
});
} catch (e) {
reject("Compilation failed");
}
resolve("Compilation finished");
});
}
function preprocess(file, verbose) {
console.log("Preprocessing...");
var contents = fs.readFileSync(file).toString();
preprocessDecls = contents.match(/^[#;].*/gm);
if (preprocessDecls === null) {
preprocessDecls = [];
}
var includes = '#include "jam.h"\n';
var originalProgram = contents
contents = includes + "int main();\n" + contents;
fs.writeFileSync(`${tmpDir}/pre.c`, contents);
var command = `clang -E -P -I/usr/local/include -I/usr/local/share/jam/deps/fake_libc_include -I/usr/local/share/jam/lib ${tmpDir}/pre.c`;
if (verbose) {
console.log(command);
}
var preprocessedProg = child_process.execSync(command).toString();
var index = preprocessedProg.indexOf("int main();\n");
var tmp = preprocessedProg.substring(0, index);
var lineNumber = tmp.split('\n').length;
return {
program: preprocessedProg,
lineNumber: lineNumber
};
}
function randomValueHex(len) {
return crypto.randomBytes(Math.ceil(len / 2))
.toString('hex') // convert to hexadecimal format
.slice(0, len); // return required number of characters
}
function deleteFolderRecursive(path) {
if (fs.existsSync(path)) {
fs.readdirSync(path).forEach(function(file) {
var curPath = path + "/" + file;
if (fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
}