-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_makescript_.js
More file actions
185 lines (135 loc) · 5.05 KB
/
Copy path_makescript_.js
File metadata and controls
185 lines (135 loc) · 5.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
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
const fs = require("fs");
const path = require("path");
const child_process = require('child_process')
var project = process.argv[2];
if (!project) {
console.log("First param must be build project.");
process.exit();
}
var target = process.argv[3];
if (!target) {
console.log("Second param must be build target.");
process.exit();
}
var command = "";
var gccBin = "";
if (process.argv.length == 5) {
command = process.argv[4];
} else if (process.argv.length == 6) {
command = process.argv[5];
gccBin = process.argv[4];
}
if (!command) {
command = "all";
}
var walk = function(dir) {
var results = [];
var list = fs.readdirSync(dir);
list.forEach(function(file) {
var fileAll = dir + '/' + file;
var stat = fs.statSync(fileAll);
if (stat && stat.isDirectory()) {
var w = walk(fileAll);
w.forEach(function(fileW) {
results.push(file+'/'+fileW);
});
} else {
results.push(file);
}
});
return results
};
var getIncludes = function(code) {
var regExp = /^\s*#include "([^.]+)\.h"/gm;
var ret = [];
var includesOne;
while ((includesOne = regExp.exec(code)) !== null) {
ret.push(includesOne[1]);
}
return ret;
};
if (command == "all") {
var fPath = __dirname+"/"+project;
fPath = path.resolve(fPath);
var dir = walk(fPath);
fs.writeFileSync(fPath+"/main.cpp", fs.readFileSync(fPath+"/main.cpp", "utf8"), {encoding: "utf8"});
var main = fs.readFileSync(fPath+"/main.cpp", "utf8");
var includes = [];
var includesNames = getIncludes(main);
dir.forEach(function (file) {
if (file == "main.cpp") return;
if (file.indexOf(".cpp", file.length - ".cpp".length) !== -1) {
includes.push(file.substr(0, file.length - ".cpp".length));
}
if (file.indexOf(".c", file.length - ".c".length) !== -1) {
includes.push(file.substr(0, file.length - ".c".length));
}
if ((file.indexOf(".cpp", file.length - ".cpp".length) !== -1) || (file.indexOf(".c", file.length - ".c".length) !== -1) || (file.indexOf(".h", file.length - ".h".length) !== -1)) {
Array.prototype.push.apply(includesNames, getIncludes(fs.readFileSync(fPath+"/"+file, "utf8")));
}
});
//unique
includesNames = Array.prototype.filter.call(includesNames, function (value, index, self) {
return self.indexOf(value) === index;
});
var includesNamesFinal = [];
includesNames.forEach(function (include) {
if (/^([A-Za-z0-9_-]+)$/.test(include)) {
includesNamesFinal.push("INCLUDE_"+include.toUpperCase()+"=1");
}
});
includesNamesFinal = includesNamesFinal.join(" ");
includes = includes.join(" ");
var makeCommandIncludes = "";
if (includes) {
makeCommandIncludes = " BUILD_INCLUDES=\""+includes+"\"";
}
var makeCommand = "make -f _makefile_ BUILD_ID="+project+" BUILD_TARGET="+target+" GCC_BIN=\""+gccBin+"\" USING_FOLDER_MAIN=1 CUSTOM_BUILD_ID=RND_BUILD_ID_"+Math.round(Math.random()*1000)+""+makeCommandIncludes+" "+includesNamesFinal;
console.log("MAKE COMMAND: "+makeCommand);
try {
//var out = child_process.execSync(makeCommand, {cwd: __dirname});
var parts = makeCommand.split(" ");
var returncode = 0;
const ls = child_process.spawn(parts[0], parts.slice(1), {cwd: __dirname, shell: true});
ls.stdout.on('data', (data) => {
process.stdout.write(data.toString("utf8"));
});
ls.stderr.on('data', (data) => {
process.stdout.write(data.toString("utf8"));
});
ls.on('close', (code) => {
console.log("");
console.log(`Compile process exited with code ${code}`);
// EXIT CODE
process.exit(code);
});
} catch (e) {
//console.log(e.toString("utf8"));
}
}
if (command == "clean") {
var fPath = __dirname+"/"+project;
fPath = path.resolve(fPath);
var dir = walk(fPath);
dir.forEach(function (file) {
if ((file.indexOf(".d", file.length - ".d".length) !== -1) ||
(file.indexOf(".o", file.length - ".o".length) !== -1) ||
(file.indexOf(".bin", file.length - ".bin".length) !== -1) ||
(file.indexOf(".elf", file.length - ".elf".length) !== -1) ||
(file.indexOf(".map", file.length - ".map".length) !== -1) ||
(file.indexOf(".hex", file.length - ".hex".length) !== -1)) {
fs.unlinkSync(fPath+"/"+file);
console.log("Removed file "+project+"/"+file);
}
});
var fPathLibs = __dirname+"/_libs_";
fPathLibs = path.resolve(fPathLibs);
var dirLibs = walk(fPathLibs);
dirLibs.forEach(function (file) {
if ((file.indexOf(target+".d", file.length - target+".d".length) !== -1) ||
(file.indexOf(target+".o", file.length - target+".o".length) !== -1)) {
fs.unlinkSync(fPathLibs+"/"+file);
console.log("Removed file _libs_/"+file);
}
});
}