forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.js
241 lines (218 loc) · 6.83 KB
/
install.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
//@ts-check
// For Windows, we distribute a prebuilt bsc.exe
// To build on windows, we still need figure out constructing config.ml
// from existing compiler
// For other OSes, we detect
// if there is other compiler installed and the version matches,
// we get the config.ml from existing OCaml compiler and build whole_compiler
// Otherwise, we build the compiler shipped with Buckle and use the
// old compiler.ml
// This will be run in npm postinstall, don't use too fancy features here
var child_process = require("child_process");
var fs = require("fs");
var path = require("path");
var root_dir = path.join(__dirname, "..");
var lib_dir = path.join(root_dir, "lib");
var jscomp_dir = path.join(root_dir, "jscomp");
var supported_os = ["darwin", "freebsd", "linux", "openbsd", "win32"];
if (supported_os.indexOf(process.platform) < 0) {
throw new Error("Not supported platform" + process.platform);
}
var is_windows = process.platform === "win32";
var bsc_exe = require("./bin_path").bsc_exe;
var ninja_bin_filename = process.platform === "win32" ? "ninja.exe" : "ninja";
var ninja_bin_output = require("./bin_path").ninja_exe;
var force_compiler_rebuild = process.argv.includes("-force-compiler-rebuild");
var force_lib_rebuild = process.argv.includes("-force-lib-rebuild");
/**
* Make sure `ninja_bin_output` exists
* The installation of `ninja.exe` is re-entrant, since we always pre-check if it is already installed
* This is less problematic since `ninja.exe` is very stable
*/
function provideNinja() {
var vendor_ninja_version = "1.9.0.git";
var ninja_source_dir = path.join(root_dir, "vendor", "ninja");
function build_ninja() {
console.log(`building ninja`);
ensureExists(ninja_source_dir);
if (fs.existsSync(path.join(root_dir, "vendor", "ninja.tar.gz"))) {
// Build from source on installation of the npm package
// for platforms where we don't provide a pre-built binary.
console.log("Extracting ninja sources...");
child_process.execSync(`tar xzf ../ninja.tar.gz`, {
cwd: ninja_source_dir,
stdio: [0, 1, 2],
});
console.log("No prebuilt Ninja, building Ninja now");
var build_ninja_command = "python3 ./configure.py --bootstrap";
child_process.execSync(build_ninja_command, {
cwd: ninja_source_dir,
stdio: [0, 1, 2],
});
fs.copyFileSync(
path.join(ninja_source_dir, ninja_bin_filename),
ninja_bin_output
);
} else {
// Build from source for "npm install" in local dev.
require("../scripts/buildNinjaBinary");
fs.copyFileSync(
path.join(root_dir, "ninja", ninja_bin_filename),
ninja_bin_output
);
}
console.log("ninja binary is ready: ", ninja_bin_output);
}
// sanity check to make sure the binary actually runs. Used for Linux. Too many variants
/**
*
* @param {string} binary_path
*/
function test_ninja_compatible(binary_path) {
var version;
try {
version = child_process
.execSync(JSON.stringify(binary_path) + " --version", {
encoding: "utf8",
stdio: ["pipe", "pipe", "ignore"], // execSync outputs to stdout even if we catch the error. Silent it here
})
.trim();
} catch (e) {
console.log("ninja not compatible?", String(e));
return false;
}
return version === vendor_ninja_version;
}
if (
process.env.NINJA_FORCE_REBUILD === undefined &&
fs.existsSync(ninja_bin_output) &&
test_ninja_compatible(ninja_bin_output)
) {
console.log(
"ninja binary is already cached and installed: ",
ninja_bin_output
);
return;
}
build_ninja();
}
/**
*
* @param {string} dir
* TODO: `mkdirSync` may fail
*/
function ensureExists(dir) {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
}
/**
* @returns {string|undefined}
*/
function checkPrebuiltBscCompiler() {
if (process.env.RESCRIPT_FORCE_REBUILD || force_compiler_rebuild) {
return;
}
try {
var version = String(child_process.execFileSync(bsc_exe, ["-v"]));
var myOCamlVersion = version.substr(
version.indexOf(":") + 1,
version.lastIndexOf(" ") - version.indexOf(":") - 1
);
console.log("checkoutput:", version, "ocaml version", myOCamlVersion);
console.log("Prebuilt compiler works good");
return myOCamlVersion;
} catch (e) {
console.log("No working prebuilt ReScript compiler");
if (is_windows) {
throw new Error("no prebuilt bsc compiler on windows");
}
return;
}
}
/**
*
* @param {string} stdlib
*/
function buildLibs(stdlib) {
ensureExists(lib_dir);
ensureExists(path.join(lib_dir, "js"));
ensureExists(path.join(lib_dir, "es6"));
process.env.NINJA_IGNORE_GENERATOR = "true";
var releaseNinja = `
bsc = ${bsc_exe}
stdlib = ${stdlib}
subninja runtime/release.ninja
subninja others/release.ninja
subninja $stdlib/release.ninja
${process.env.RESCRIPT_FORCE_REBUILD ? "subninja test/build.ninja\n" : "\n"}
o all: phony runtime others $stdlib
`;
var filePath = path.join(jscomp_dir, "release.ninja");
fs.writeFileSync(filePath, releaseNinja, "ascii");
var cleanArgs = ["-f", "release.ninja", "-t", "clean"];
child_process.execFileSync(ninja_bin_output, cleanArgs, {
cwd: jscomp_dir,
stdio: [0, 1, 2],
shell: false,
});
var buildArgs = ["-f", "release.ninja", "--verbose", "-k", "1"];
child_process.execFileSync(ninja_bin_output, buildArgs, {
cwd: jscomp_dir,
stdio: [0, 1, 2],
shell: false,
});
fs.unlinkSync(filePath);
console.log("Build finished");
}
/**
* @returns {string}
*/
function provideCompiler() {
var myVersion = checkPrebuiltBscCompiler();
if (myVersion !== undefined) {
return myVersion;
} else {
var ocamlopt = "ocamlopt.opt";
myVersion = "4.06.1";
if (!require("./buildocaml.js").checkEnvCompiler()) {
// no compiler available
var prefix = require("./buildocaml.js").build(true);
ocamlopt = `${prefix}/bin/${ocamlopt}`;
}
// Note this ninja file only works under *nix due to the suffix
// under windows require '.exe'
var releaseNinja = require("./ninjaFactory.js").libNinja({
ocamlopt: ocamlopt,
INCL: myVersion,
isWin: is_windows,
});
var filePath = path.join(lib_dir, "release.ninja");
fs.writeFileSync(filePath, releaseNinja, "ascii");
child_process.execFileSync(
ninja_bin_output,
["-f", "release.ninja", "-t", "clean"],
{
cwd: lib_dir,
stdio: [0, 1, 2],
}
);
child_process.execFileSync(
ninja_bin_output,
["-f", "release.ninja", "-v"],
{
cwd: lib_dir,
stdio: [0, 1, 2],
}
);
fs.unlinkSync(filePath);
return myVersion;
}
}
provideNinja();
provideCompiler();
var stdlib = "stdlib-406";
if (process.env.RESCRIPT_FORCE_REBUILD || force_lib_rebuild) {
buildLibs(stdlib);
require("./installUtils.js").install();
}