-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·354 lines (334 loc) · 10.7 KB
/
index.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
global.gypcc = { // NOTABUG: old school anti-soydev export
main,
Target,
argv_from_string,
};
const T = Target.prototype;
T.exec_sync = exec_sync;
T.add_env = add_env;
T.add_argv_string = add_argv_string;
T.add_argv = add_argv;
T.get_exec_args = get_exec_args;
T.get_binding = get_binding;
T._debug = _debug;
T._silent = _silent;
T._verbose = _verbose;
T._silly = _silly;
T._static = _static;
T._shared = _shared;
T._source = _source;
T._o = _o;
T._I = _I;
T._D = _D;
T._cflag = _cflag;
// T._ldflag = _ldflag;
T._l = _l;
T._L = _L;
T._f = _f;
T._F = _F;
const fs = require('fs');
const p = require('path');
const chproc = require("child_process");
const log = console.log;
const CWD = process.cwd();
const ENV = process.env;
function main(argc, argv)
{
let r = 0;
if (argc == 1)
return help();
argv.shift();
const cmd = argv[0];
switch (cmd) {
case "version": case "--version": case "-v":
return version();
case "help": case "--help": case "-h":
return help();
}
const tgt = new Target();
tgt.add_env(ENV);
tgt.add_argv(argv);
r = tgt.exec_sync();
return r;
// TODO: ...
function version()
{
log("node-gypcc@");
return 0;
}
function help(argv)
{
log("node-gypcc@");
return 0;
}
}
function Target()
{
this.verbose = ""; // silent verbose silly
this.debug = 0;
this.out = "";
this.target_name = "";
this.type = "exectuable"; // shared_library static_library
this.sources = [];
this.defines = [];
this.include_dirs = [];
this.libraries = [];
this.cflags = [];
// this.ldflags = [];
this.HOME = "";
this.PATH = ENV.PATH;
// PYTHON: "/usr/local/bin/python3.9",
// NODE_GYP_FORCE_PYTHON: "/usr/local/bin/python3.9",
this.MAKE = "";
// TODO: this.msvs_guid = ""
this.MACOSX_DEPLOYMENT_TARGET = "";
// npm
this.npm_config_runtime = "";
this.npm_config_target = "";
this.npm_config_arch = "";
this.npm_config_target_arch = "";
this.npm_config_build_from_source = "";
this.npm_config_dist_url = ""; // NOTE: https://github.com/nodejs/node-gyp/issues/2250
}
function exec_sync()
{
const binding = this.get_binding();
const exec_args = this.get_exec_args()
const binding_path = P`./binding.gyp`;
if (this.verbose !== "silent")
console.dir(binding)
mkjson(binding_path, {targets: [ binding ]});
let code = 0;
try {
chproc.execFileSync.apply(null, exec_args);
} catch(e) {
code = 1;
log("gypcc: ERROR: exec_sync");
console.error(e);
}
fs.rmSync(binding_path,{force:true});
if (code == 0)
fs.renameSync(
`./build/${this.debug?"Debug":"Release"}/${this.target_name}.node`,
this.out
);
return code;
}
function add_env(obj)
{
const tgt = this;
const {CFLAGS, LDFLAGS} = obj;
tgt.add_argv_string(CFLAGS);
tgt.add_argv_string(LDFLAGS);
if (_use("npm_config_runtime") === "electron") {
tgt.npm_config_dist_url = tgt.npm_config_dist_url || "https://electronjs.org/headers";
tgt.npm_config_build_from_source = tgt.npm_config_build_from_source || "true";
tgt.HOME = tgt.HOME || "~/.electron-gyp";
}
_use("npm_config_target");
_use("npm_config_arch");
_use("npm_config_target_arch");
_use("npm_config_build_from_source");
_use("npm_config_dist_url"); // NOTE: https://github.com/nodejs/node-gyp/issues/2250
_use("MAKE");
_use("HOME");
_use("PATH");
_use("MACOSX_DEPLOYMENT_TARGET");
function _use(key) {
if (obj[key])
return tgt[key] = obj[key];
return '';
}
}
function add_argv_string(string)
{
if (string)
this.add_argv(argv_from_string(string));
}
function add_argv(argv)
{
// https://gcc.gnu.org/onlinedocs/gcc/Overall-Options.html#Overall-Options
// TODO: ? -pthread, -x, -e entry --entry=entry, -c (compile w/o link)
// TODO: 'cflags!': ['-stdlib=libc++'],
// TODO: ? -Wl, link_settings https://github.com/nodejs/node-gyp/issues/682
const tgt = this;
const argc = argv.length;
let argi = 0;
function next() { return argv[argi++]; }
while (argi < argc) {
let arg = next();
switch (arg) {
case "-o" : tgt._o(next()); continue;
case "-framework": tgt._f(next()); continue;
case "-static" : tgt._static(); continue;
case "-shared" : tgt._shared(); continue;
case "--debug" : tgt._debug(); continue;
case "--silent" : tgt._silent(); continue;
case "--verbose" : tgt._verbose(); continue;
case "--silly" : tgt._silly(); continue;
}
let short = arg.match(/^-([a-zA-Z])(.*)/);
if (short) {
let [,ch,val] = short;
switch (ch) {
case "I": tgt._I(val); continue;
case "L": tgt._L(val); continue;
case "l": tgt._l(val); continue;
case "D": tgt._D(val); continue;
case "F": tgt._F(val); continue;
}
tgt._cflag(arg); continue;
}
let long = arg.match(/^--([a-zA-Z][a-zA-Z0-9]*)(=?)(.*)/);
if (long) {
let [,key,,val] = long;
switch (key) {
case "make" : tgt.MAKE = val; continue;
case "runtime" : tgt.npm_config_runtime = val; continue;
case "target" : tgt.npm_config_target = val; continue;
case "arch" : tgt.npm_config_arch = val; continue;
case "target-arch" : tgt.npm_config_target_arch = val; continue;
case "build-from-source": tgt.npm_config_build_from_source = val; continue;
case "dist-url" : tgt.npm_config_dist_url = val; continue;
}
tgt._cflag(arg); continue;
}
tgt._source(arg);
}
}
function get_binding()
{
const {
target_name, sources, libraries, include_dirs, defines, cflags,
MACOSX_DEPLOYMENT_TARGET
} = this;
const xcode_settings = {
OTHER_CFLAGS: cflags, // NOTE: https://github.com/nickdesaulniers/node-nanomsg/pull/144
};
if (MACOSX_DEPLOYMENT_TARGET)
xcode_settings.MACOSX_DEPLOYMENT_TARGET = MACOSX_DEPLOYMENT_TARGET;
const binding = {
target_name,
defines,
sources,
libraries,
include_dirs,
cflags,
xcode_settings,
};
return binding;
}
function get_exec_args()
{
const file = p.resolve(__dirname,'node_modules','.bin','node-gyp');
const args = [file, "rebuild"];
const env = {};
const {
debug, verbose,
PATH, HOME, MAKE,
npm_config_runtime, npm_config_target, npm_config_arch,
npm_config_target_arch, npm_config_build_from_source,
npm_config_dist_url,
} = this;
if (PATH) env.PATH = P`${PATH}`;
if (HOME) env.HOME = P`${HOME}`;
if (MAKE) args.push(`--make=${ENV.MAKE}`);
if (debug) args.push(`--debug`);
if (verbose) args.push(`--${verbose}`);
if (npm_config_runtime) args.push(`--runtime=${
env.npm_config_runtime = npm_config_runtime
}`);
if (npm_config_target) args.push(`--target=${
env.npm_config_runtime = npm_config_target
}`);
if (npm_config_arch) args.push(`--arch=${
env.npm_config_arch = npm_config_arch
}`);
if (npm_config_target_arch) args.push(`--target-arch=${
env.npm_config_target_arch = npm_config_target_arch
}`);
if (npm_config_dist_url) args.push(`--dist-url=${npm_config_dist_url}`);
// NOTE: https://github.com/nodejs/node-gyp/issues/2250
if (npm_config_build_from_source)
env.npm_config_build_from_source = npm_config_build_from_source;
// TODO: cmd += ` --directory=`+P`${CWD}`
return [process.execPath, args, {
// cwd:
env,
}];
}
function _debug() { this.debug = 1; }
function _silent() { this.verbose = "silent"; }
function _verbose() { this.verbose = "verbose"; }
function _silly() { this.verbose = "silly"; }
function _static() { this.type = "static_library"; }
function _shared() { this.type = "shared_library"; }
function _source(src) { this.sources.push(P`${src}`); }
function _I(dir) { this.include_dirs.push(P`${dir}`); }
function _D(def) { this.defines.push(def); }
function _cflag(f) { this.cflags.push(f); }
// function _ldflag(f) { this.ldflags.push(f); }
function _l(lib) { this.libraries.push('-l'+lib); }
function _L(dir) { this.libraries.push('-L'+P`${dir}`); }
function _f(lib) { this.libraries.push(`-framework ${lib}`); }
function _F(dir) { this.libraries.push(`-F${P`${dir}`}`); }
function _o(file)
{
if (!file.endsWith(".node"))
file += ".node";
this.out = P`${file}`;
this.target_name = p.basename(file).replace(/\..*$/,"");
}
// utils ----------------------------------------------------------------------
const RX_ARGV = /([^\s'"]([^\s'"]*(['"])([^\3]*?)\3)+[^\s'"]*)|[^\s'"]+|(['"])([^\5]*?)\5/gi; // https://github.com/mccormicka/string-argv/blob/master/index.ts
function argv_from_string(str)
{
const argv = [];
// TODO: dehack escaped whitespace fix...
const string = str.replace(/\\ /g,"~@@~");
function push(a) {
if (typeof a !== "string")
return 0;
if (!a)
return -1;
return argv.push(_arg(a));
}
if (string.length !== str.length)
function _arg(a) { return a.replace(/~@@~/g,"\\ "); }
else
function _arg(a) { return a; }
let match;
while (true) {
match = RX_ARGV.exec(string); // Each call returns next match
if (match === null) break;
if (push(match[1])) continue; // Index 1 is captured group if it exists
if (push(match[6])) continue;
if (push(match[0])) continue; // Index 0 is matched text if no captured group exists
};
return argv;
}
function P(strings, ...keys)
{
let i = 0;
let r = strings[i++];
for (const k of keys)
r += `${k}${strings[i++]}`;
return _path_fix(r);
}
const _path_fix = (p.sep === '/') ? function (path) { return path; } : function (path)
{
path = path.replace(/^~\//g, ENV.HOME || ENV.USERPROFILE ); // TODO: verify
path = path.replace(/\//g, p.sep);
}
function mkdirp(path)
{
return fs.mkdirSync(P`${path}`, {recursive:true});
}
function mkfile(path, content) // => num of bytes written
{
return fs.writeFileSync(P`${path}`, content);
}
function mkjson(path, content)
{
return mkfile(path, JSON.stringify(content))
}