/
cMake.js
332 lines (290 loc) · 9.37 KB
/
cMake.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
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
"use strict";
let splitargs = require("splitargs");
let which = require("which");
let fs = require("fs-extra");
let path = require("path");
let _ = require("lodash");
let environment = require("./environment");
let Dist = require("./dist");
let CMLog = require("./cmLog");
let vsDetect = require("./vsDetect");
let TargetOptions = require("./targetOptions");
let processHelpers = require("./processHelpers");
let locateNAN = require("./locateNAN");
let npmConfigData = require("rc")("npm");
let Toolset = require("./toolset");
function CMake(options) {
this.options = options || {};
this.log = new CMLog(this.options);
this.dist = new Dist(this.options);
this.projectRoot = path.resolve(this.options.directory || process.cwd());
this.workDir = path.resolve(this.options.out || path.join(this.projectRoot, "build"));
this.config = this.options.config || (this.options.debug ? "Debug" : "Release");
this.buildDir = path.join(this.workDir, this.config);
this._isAvailable = null;
this.targetOptions = new TargetOptions(this.options);
this.toolset = new Toolset(this.options);
this.cMakeOptions = this.options.cMakeOptions || {};
this.silent = !!options.silent;
}
Object.defineProperties(CMake.prototype, {
path: {
get: function () {
return this.options.cmakePath || "cmake";
}
},
isAvailable: {
get: function () {
if (this._isAvailable === null) {
this._isAvailable = CMake.isAvailable(this.options);
}
return this._isAvailable;
}
}
});
CMake.isAvailable = function (options) {
options = options || {};
try {
if (options.cmakePath) {
let stat = fs.lstatSync(options.cmakePath);
return !stat.isDirectory();
}
else {
which.sync("cmake");
return true;
}
}
catch (e) {
_.noop(e);
}
return false;
};
CMake.getGenerators = async function (options) {
let arch = " [arch]";
options = options || {};
let gens = [];
if (CMake.isAvailable(options)) {
// try parsing machine-readable capabilities (available since CMake 3.7)
try {
let stdout = await processHelpers.exec((options.cmakePath || "cmake") + " -E capabilities");
let capabilities = JSON.parse(stdout);
return capabilities.generators.map(x => x.name);
}
catch (error) {
this.log.verbose("TOOL", "Failed to query CMake capabilities (CMake is probably older than 3.7)");
}
// fall back to parsing help text
let stdout = await processHelpers.exec((options.cmakePath || "cmake") + " --help");
let hasCr = stdout.includes("\r\n");
let output = hasCr ? stdout.split("\r\n") : stdout.split("\n");
let on = false;
output.forEach(function (line, i) {
if (on) {
let parts = line.split("=");
if ((parts.length === 2 && parts[0].trim()) ||
(parts.length === 1 && i !== output.length - 1 && output[i + 1].trim()[0] === "=")) {
let gen = parts[0].trim();
if (_.endsWith(gen, arch)) {
gen = gen.substr(0, gen.length - arch.length);
}
gens.push(gen);
}
}
if (line.trim() === "Generators") {
on = true;
}
});
}
else {
throw new Error("CMake is not installed. Install CMake.");
}
return gens;
};
CMake.prototype.getGenerators = function () {
return CMake.getGenerators(this.options);
};
CMake.prototype.verifyIfAvailable = function () {
if (!this.isAvailable) {
throw new Error("CMake executable is not found. Please use your system's package manager to install it, or you can get installers from there: http://cmake.org.");
}
};
CMake.prototype.getConfigureCommand = async function () {
// Create command:
let command = this.path;
command += " \"" + this.projectRoot + "\" --no-warn-unused-cli";
let D = [];
// CMake.js watermark
D.push({"CMAKE_JS_VERSION": environment.moduleVersion});
// Build configuration:
D.push({"CMAKE_BUILD_TYPE": this.config});
if (environment.isWin) {
D.push({"CMAKE_RUNTIME_OUTPUT_DIRECTORY": this.workDir});
}
else {
D.push({"CMAKE_LIBRARY_OUTPUT_DIRECTORY": this.buildDir});
}
// Include and lib:
let incPaths;
if (this.dist.headerOnly) {
incPaths = [path.join(this.dist.internalPath, "/include/node")];
}
else {
let nodeH = path.join(this.dist.internalPath, "/src");
let v8H = path.join(this.dist.internalPath, "/deps/v8/include");
let uvH = path.join(this.dist.internalPath, "/deps/uv/include");
incPaths = [nodeH, v8H, uvH];
}
// NAN
let nanH = await locateNAN(this.projectRoot);
if (nanH) {
incPaths.push(nanH);
}
// Includes:
D.push({"CMAKE_JS_INC": incPaths.join(";")});
// Sources:
let srcPaths = [];
if (environment.isWin) {
let delayHook = path.normalize(path.join(__dirname, 'cpp', 'win_delay_load_hook.cc'));
srcPaths.push(delayHook.replace(/\\/gm, '/'));
}
D.push({"CMAKE_JS_SRC": srcPaths.join(";")});
// Runtime:
D.push({"NODE_RUNTIME": this.targetOptions.runtime});
D.push({"NODE_RUNTIMEVERSION": this.targetOptions.runtimeVersion});
D.push({"NODE_ARCH": this.targetOptions.arch});
if (environment.isWin) {
// Win
let libs = this.dist.winLibs;
if (libs.length) {
D.push({"CMAKE_JS_LIB": libs.join(";")});
}
}
// Custom options
for (let k of _.keys(this.cMakeOptions)) {
D.push({[k]: this.cMakeOptions[k]});
}
// Toolset:
await this.toolset.initialize(false);
if (this.toolset.generator) {
command += " -G\"" + this.toolset.generator + "\"";
}
if (this.toolset.platform) {
command += " -A\"" + this.toolset.platform + "\"";
}
if (this.toolset.toolset) {
command += " -T\"" + this.toolset.toolset + "\"";
}
if (this.toolset.cppCompilerPath) {
D.push({"CMAKE_CXX_COMPILER": this.toolset.cppCompilerPath});
}
if (this.toolset.cCompilerPath) {
D.push({"CMAKE_C_COMPILER": this.toolset.cCompilerPath});
}
if (this.toolset.compilerFlags.length) {
D.push({"CMAKE_CXX_FLAGS": this.toolset.compilerFlags.join(" ")});
}
if (this.toolset.linkerFlags.length) {
D.push({"CMAKE_SHARED_LINKER_FLAGS": this.toolset.linkerFlags.join(" ")});
}
if (this.toolset.makePath) {
D.push({"CMAKE_MAKE_PROGRAM": this.toolset.makePath});
}
// Load NPM config
for (let key of _.keys(npmConfigData)) {
if (_.startsWith(key, "cmake_")) {
let s = {};
let sk = key.substr(6);
if (sk) {
s[sk] = npmConfigData[key];
if (s[sk]) {
D.push(s);
}
}
}
}
command += " " +
D.map(function (p) {
return "-D" + _.keys(p)[0] + "=\"" + _.values(p)[0] + "\"";
}).join(" ");
return command;
};
CMake.prototype.configure = async function () {
this.verifyIfAvailable();
this.log.info("CMD", "CONFIGURE");
let listPath = path.join(this.projectRoot, "CMakeLists.txt");
let command = await this.getConfigureCommand();
try {
await fs.lstat(listPath);
}
catch (e) {
throw new Error("'" + listPath + "' not found.");
}
try {
await fs.ensureDir(this.workDir);
}
catch (e) {
_.noop(e);
}
let cwd = process.cwd();
process.chdir(this.workDir);
try {
await this._run(command);
}
finally {
process.chdir(cwd);
}
};
CMake.prototype.ensureConfigured = async function () {
try {
await fs.lstat(path.join(this.workDir, "CMakeCache.txt"));
}
catch (e) {
_.noop(e);
await this.configure();
}
};
CMake.prototype.getBuildCommand = function () {
var command = this.path + " --build \"" + this.workDir + "\" --config " + this.config;
if (this.options.target) {
command += " --target " + this.options.target;
}
return Promise.resolve(command);
};
CMake.prototype.build = async function () {
this.verifyIfAvailable();
await this.ensureConfigured();
let buildCommand = await this.getBuildCommand();
this.log.info("CMD", "BUILD");
await this._run(buildCommand);
};
CMake.prototype.getCleanCommand = function () {
return this.path + " -E remove_directory \"" + this.workDir + "\"";
};
CMake.prototype.clean = function () {
this.verifyIfAvailable();
this.log.info("CMD", "CLEAN");
return this._run(this.getCleanCommand());
};
CMake.prototype.reconfigure = async function () {
await this.clean();
await this.configure();
};
CMake.prototype.rebuild = async function () {
await this.clean();
await this.build();
};
CMake.prototype.compile = async function () {
try {
await this.build();
}
catch (e) {
_.noop(e);
this.log.info("REP", "Build has been failed, trying to do a full rebuild.");
await this.rebuild();
}
};
CMake.prototype._run = function (command) {
this.log.info("RUN", command);
return processHelpers.run(command, {silent: this.silent});
};
module.exports = CMake;