UNPKG

16.4 kBJavaScriptView Raw
1"use strict";
2var __assign = (this && this.__assign) || function () {
3 __assign = Object.assign || function(t) {
4 for (var s, i = 1, n = arguments.length; i < n; i++) {
5 s = arguments[i];
6 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7 t[p] = s[p];
8 }
9 return t;
10 };
11 return __assign.apply(this, arguments);
12};
13var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15 return new (P || (P = Promise))(function (resolve, reject) {
16 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19 step((generator = generator.apply(thisArg, _arguments || [])).next());
20 });
21};
22var __generator = (this && this.__generator) || function (thisArg, body) {
23 var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24 return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25 function verb(n) { return function (v) { return step([n, v]); }; }
26 function step(op) {
27 if (f) throw new TypeError("Generator is already executing.");
28 while (_) try {
29 if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30 if (y = 0, t) op = [op[0] & 2, t.value];
31 switch (op[0]) {
32 case 0: case 1: t = op; break;
33 case 4: _.label++; return { value: op[1], done: false };
34 case 5: _.label++; y = op[1]; op = [0]; continue;
35 case 7: op = _.ops.pop(); _.trys.pop(); continue;
36 default:
37 if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38 if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39 if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40 if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41 if (t[2]) _.ops.pop();
42 _.trys.pop(); continue;
43 }
44 op = body.call(thisArg, _);
45 } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46 if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47 }
48};
49var __spreadArray = (this && this.__spreadArray) || function (to, from) {
50 for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
51 to[j] = from[i];
52 return to;
53};
54var __importDefault = (this && this.__importDefault) || function (mod) {
55 return (mod && mod.__esModule) ? mod : { "default": mod };
56};
57Object.defineProperty(exports, "__esModule", { value: true });
58var crypto_1 = __importDefault(require("crypto"));
59var fs_extra_1 = __importDefault(require("fs-extra"));
60var os_1 = __importDefault(require("os"));
61var path_1 = __importDefault(require("path"));
62var utils_1 = require("./utils");
63var system_1 = require("./system");
64var log_1 = require("./log");
65var patches_json_1 = __importDefault(require("../patches/patches.json"));
66var buildPath = path_1.default.resolve(process.env.PKG_BUILD_PATH ||
67 path_1.default.join(os_1.default.tmpdir(), "pkg." + crypto_1.default.randomBytes(12).toString('hex')));
68var nodePath = path_1.default.join(buildPath, 'node');
69var patchesPath = path_1.default.resolve(__dirname, '../patches');
70var nodeRepo = 'https://github.com/nodejs/node';
71function getMajor(nodeVersion) {
72 var _a = nodeVersion.match(/^v?(\d+)/) || ['', 0], version = _a[1];
73 return Number(version) | 0;
74}
75function getConfigureArgs(major, targetPlatform) {
76 var args = [];
77 // first of all v8_inspector introduces the use
78 // of `prime_rehash_policy` symbol that requires
79 // GLIBCXX_3.4.18 on some systems
80 // also we don't support any kind of debugging
81 // against packaged apps, hence v8_inspector is useless
82 args.push('--without-inspector');
83 if (system_1.hostPlatform === 'alpine') {
84 // Statically Link against libgcc and libstdc++ libraries. See vercel/pkg#555.
85 // libgcc and libstdc++ grant GCC Runtime Library Exception of GPL
86 args.push('--partly-static');
87 }
88 if (targetPlatform === 'linuxstatic') {
89 args.push('--fully-static');
90 }
91 // Link Time Optimization
92 if (major >= 12) {
93 if (system_1.hostPlatform !== 'win') {
94 args.push('--enable-lto');
95 }
96 }
97 // DTrace
98 args.push('--without-dtrace');
99 // bundled npm package manager
100 args.push('--without-npm');
101 // Small ICU
102 args.push('--with-intl=small-icu');
103 // Workaround for nodejs/node#39313
104 // All supported macOS versions have zlib as a system library
105 if (targetPlatform === 'macos') {
106 args.push('--shared-zlib');
107 }
108 return args;
109}
110function gitClone(nodeVersion) {
111 return __awaiter(this, void 0, void 0, function () {
112 var args;
113 return __generator(this, function (_a) {
114 switch (_a.label) {
115 case 0:
116 log_1.log.info('Cloning Node.js repository from GitHub...');
117 args = [
118 'clone',
119 '-b',
120 nodeVersion,
121 '--depth',
122 '1',
123 '--single-branch',
124 '--bare',
125 '--progress',
126 nodeRepo,
127 'node/.git',
128 ];
129 return [4 /*yield*/, utils_1.spawn('git', args, { cwd: buildPath, stdio: 'inherit' })];
130 case 1:
131 _a.sent();
132 return [2 /*return*/];
133 }
134 });
135 });
136}
137function gitResetHard(nodeVersion) {
138 return __awaiter(this, void 0, void 0, function () {
139 var patches, commit, args;
140 return __generator(this, function (_a) {
141 switch (_a.label) {
142 case 0:
143 log_1.log.info("Checking out " + nodeVersion);
144 patches = patches_json_1.default[nodeVersion];
145 commit = 'commit' in patches && patches.commit ? patches.commit : nodeVersion;
146 args = ['--work-tree', '.', 'reset', '--hard', commit];
147 return [4 /*yield*/, utils_1.spawn('git', args, { cwd: nodePath, stdio: 'inherit' })];
148 case 1:
149 _a.sent();
150 return [2 /*return*/];
151 }
152 });
153 });
154}
155function applyPatches(nodeVersion) {
156 return __awaiter(this, void 0, void 0, function () {
157 var storedPatches, storedPatch, patches, _i, patches_1, patch, patchPath, args;
158 return __generator(this, function (_a) {
159 switch (_a.label) {
160 case 0:
161 log_1.log.info('Applying patches');
162 storedPatches = patches_json_1.default[nodeVersion];
163 storedPatch = 'patches' in storedPatches ? storedPatches.patches : storedPatches;
164 patches = 'sameAs' in storedPatch
165 ? patches_json_1.default[storedPatch.sameAs]
166 : storedPatch;
167 _i = 0, patches_1 = patches;
168 _a.label = 1;
169 case 1:
170 if (!(_i < patches_1.length)) return [3 /*break*/, 4];
171 patch = patches_1[_i];
172 patchPath = path_1.default.join(patchesPath, patch);
173 args = ['-p1', '-i', patchPath];
174 return [4 /*yield*/, utils_1.spawn('patch', args, { cwd: nodePath, stdio: 'inherit' })];
175 case 2:
176 _a.sent();
177 _a.label = 3;
178 case 3:
179 _i++;
180 return [3 /*break*/, 1];
181 case 4: return [2 /*return*/];
182 }
183 });
184 });
185}
186function compileOnWindows(nodeVersion, targetArch, targetPlatform) {
187 return __awaiter(this, void 0, void 0, function () {
188 var args, major, config_flags;
189 return __generator(this, function (_a) {
190 switch (_a.label) {
191 case 0:
192 args = ['/c', 'vcbuild.bat', targetArch];
193 major = getMajor(nodeVersion);
194 config_flags = getConfigureArgs(major, targetPlatform);
195 // Event Tracing for Windows
196 args.push('noetw');
197 // Performance counters on Windows
198 if (major <= 10) {
199 args.push('noperfctr');
200 }
201 // Link Time Code Generation
202 if (major >= 12) {
203 args.push('ltcg');
204 }
205 // Can't cross compile for arm64 with small-icu
206 if (system_1.hostArch !== targetArch &&
207 !config_flags.includes('--with-intl=full-icu')) {
208 config_flags.push('--without-intl');
209 }
210 return [4 /*yield*/, utils_1.spawn('cmd', args, {
211 cwd: nodePath,
212 env: __assign(__assign({}, process.env), { config_flags: config_flags.join(' ') }),
213 stdio: 'inherit',
214 })];
215 case 1:
216 _a.sent();
217 if (major <= 10) {
218 return [2 /*return*/, path_1.default.join(nodePath, 'Release/node.exe')];
219 }
220 return [2 /*return*/, path_1.default.join(nodePath, 'out/Release/node.exe')];
221 }
222 });
223 });
224}
225var _a = process.env.MAKE_JOB_COUNT, MAKE_JOB_COUNT = _a === void 0 ? os_1.default.cpus().length : _a;
226function compileOnUnix(nodeVersion, targetArch, targetPlatform) {
227 return __awaiter(this, void 0, void 0, function () {
228 var args, cpu, _a, _b, CFLAGS, _c, CXXFLAGS, output;
229 return __generator(this, function (_d) {
230 switch (_d.label) {
231 case 0:
232 args = [];
233 cpu = {
234 x86: 'ia32',
235 x64: 'x64',
236 armv6: 'arm',
237 armv7: 'arm',
238 arm64: 'arm64',
239 ppc64: 'ppc64',
240 s390x: 's390x',
241 }[targetArch];
242 if (cpu) {
243 args.push('--dest-cpu', cpu);
244 }
245 if (targetArch === 'armv7') {
246 _a = process.env, _b = _a.CFLAGS, CFLAGS = _b === void 0 ? '' : _b, _c = _a.CXXFLAGS, CXXFLAGS = _c === void 0 ? '' : _c;
247 process.env.CFLAGS = CFLAGS + " -marm -mcpu=cortex-a7";
248 process.env.CXXFLAGS = CXXFLAGS + " -marm -mcpu=cortex-a7";
249 args.push('--with-arm-float-abi=hard');
250 args.push('--with-arm-fpu=vfpv3');
251 }
252 if (system_1.hostArch !== targetArch) {
253 log_1.log.warn('Cross compiling!');
254 log_1.log.warn('You are responsible for appropriate env like CC, CC_host, etc.');
255 args.push('--cross-compiling');
256 }
257 args.push.apply(args, getConfigureArgs(getMajor(nodeVersion), targetPlatform));
258 // TODO same for windows?
259 return [4 /*yield*/, utils_1.spawn('./configure', args, { cwd: nodePath, stdio: 'inherit' })];
260 case 1:
261 // TODO same for windows?
262 _d.sent();
263 return [4 /*yield*/, utils_1.spawn(system_1.hostPlatform === 'freebsd' ? 'gmake' : 'make', ['-j', String(MAKE_JOB_COUNT)], {
264 cwd: nodePath,
265 stdio: 'inherit',
266 })];
267 case 2:
268 _d.sent();
269 output = path_1.default.join(nodePath, 'out/Release/node');
270 return [4 /*yield*/, utils_1.spawn(process.env.STRIP || 'strip', __spreadArray(__spreadArray([], (targetPlatform === 'macos' ? ['-x'] : [])), [output]), {
271 stdio: 'inherit',
272 })];
273 case 3:
274 _d.sent();
275 if (!(targetPlatform === 'macos')) return [3 /*break*/, 5];
276 // Newer versions of Apple Clang automatically ad-hoc sign the compiled executable.
277 // However, for final executable to be signable, base binary MUST NOT have an existing signature.
278 return [4 /*yield*/, utils_1.spawn('codesign', ['--remove-signature', output], {
279 stdio: 'inherit',
280 })];
281 case 4:
282 // Newer versions of Apple Clang automatically ad-hoc sign the compiled executable.
283 // However, for final executable to be signable, base binary MUST NOT have an existing signature.
284 _d.sent();
285 _d.label = 5;
286 case 5: return [2 /*return*/, output];
287 }
288 });
289 });
290}
291function compile(nodeVersion, targetArch, targetPlatform) {
292 return __awaiter(this, void 0, void 0, function () {
293 var win;
294 return __generator(this, function (_a) {
295 log_1.log.info('Compiling Node.js from sources...');
296 win = system_1.hostPlatform === 'win';
297 if (win) {
298 return [2 /*return*/, compileOnWindows(nodeVersion, targetArch, targetPlatform)];
299 }
300 return [2 /*return*/, compileOnUnix(nodeVersion, targetArch, targetPlatform)];
301 });
302 });
303}
304function build(nodeVersion, targetArch, targetPlatform, local) {
305 return __awaiter(this, void 0, void 0, function () {
306 var output, outputHash;
307 return __generator(this, function (_a) {
308 switch (_a.label) {
309 case 0: return [4 /*yield*/, fs_extra_1.default.remove(buildPath)];
310 case 1:
311 _a.sent();
312 return [4 /*yield*/, fs_extra_1.default.mkdirp(buildPath)];
313 case 2:
314 _a.sent();
315 return [4 /*yield*/, gitClone(nodeVersion)];
316 case 3:
317 _a.sent();
318 return [4 /*yield*/, gitResetHard(nodeVersion)];
319 case 4:
320 _a.sent();
321 return [4 /*yield*/, applyPatches(nodeVersion)];
322 case 5:
323 _a.sent();
324 return [4 /*yield*/, compile(nodeVersion, targetArch, targetPlatform)];
325 case 6:
326 output = _a.sent();
327 return [4 /*yield*/, utils_1.hash(output)];
328 case 7:
329 outputHash = _a.sent();
330 return [4 /*yield*/, fs_extra_1.default.mkdirp(path_1.default.dirname(local))];
331 case 8:
332 _a.sent();
333 return [4 /*yield*/, fs_extra_1.default.copy(output, local)];
334 case 9:
335 _a.sent();
336 return [4 /*yield*/, fs_extra_1.default.promises.writeFile(local + ".sha256sum", outputHash + " " + path_1.default.basename(local) + "\n")];
337 case 10:
338 _a.sent();
339 return [4 /*yield*/, fs_extra_1.default.remove(buildPath)];
340 case 11:
341 _a.sent();
342 return [2 /*return*/];
343 }
344 });
345 });
346}
347exports.default = build;
348//# sourceMappingURL=build.js.map
\No newline at end of file