Skip to content

Commit c717526

Browse files
committed
Make bs-platform commit installable
To decide the version number: - If has prebuilt bsc compiler, use it (windows always fall into this category) - if it does not check ocamlversion using ocaml/version (if git submodule initialized), otherwise vendor/ocaml.tar.gz, and build it if no ocaml compiler
1 parent 568211c commit c717526

File tree

1 file changed

+46
-90
lines changed

1 file changed

+46
-90
lines changed

scripts/install.js

Lines changed: 46 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,6 @@ var sys_extension = config.sys_extension;
2828

2929
process.env.BS_RELEASE_BUILD = "true";
3030

31-
/**
32-
* @type {string}
33-
*/
34-
var ocamlVersion;
35-
36-
/**
37-
*
38-
* @param {string} p
39-
*/
40-
function addPath(p) {
41-
process.env.PATH = p + path.delimiter + process.env.PATH;
42-
}
43-
4431
var ninja_bin_output = path.join(root_dir, "lib", "ninja.exe");
4532

4633
/**
@@ -189,7 +176,11 @@ function ensureExists(dir) {
189176
fs.mkdirSync(dir);
190177
}
191178
}
192-
function install() {
179+
180+
/**
181+
* @param {string} stdlib
182+
*/
183+
function install(stdlib) {
193184
installDirBy(runtime_dir, ocaml_dir, function(file) {
194185
var y = path.parse(file);
195186
return y.name === "js" || y.ext.includes("cm");
@@ -198,62 +189,13 @@ function install() {
198189
var y = path.parse(file);
199190
return y.ext === ".ml" || y.ext === ".mli" || y.ext.includes("cm");
200191
});
201-
var stdlib_dir = path.join(
202-
jscomp_dir,
203-
ocamlVersion.includes("4.02") ? "stdlib-402" : "stdlib-406"
204-
);
192+
var stdlib_dir = path.join(jscomp_dir, stdlib);
205193
installDirBy(stdlib_dir, ocaml_dir, function(file) {
206194
var y = path.parse(file);
207195
return y.ext === ".ml" || y.ext === ".mli" || y.ext.includes("cm");
208196
});
209197
}
210198

211-
/**
212-
* raise an exception if not matched
213-
*/
214-
function matchedCompilerExn() {
215-
var output = cp.execSync("ocamlc.opt -v", { encoding: "ascii" });
216-
217-
if (output.indexOf(ocamlVersion) >= 0) {
218-
console.log(output);
219-
console.log("Use the compiler above");
220-
} else {
221-
console.log(
222-
"version",
223-
output,
224-
"needed version",
225-
ocamlVersion,
226-
"No matched compiler found, may re-try"
227-
);
228-
throw "";
229-
}
230-
}
231-
function tryToProvideOCamlCompiler() {
232-
try {
233-
if (process.env.BS_ALWAYS_BUILD_YOUR_COMPILER) {
234-
throw "FORCED TO REBUILD";
235-
}
236-
matchedCompilerExn();
237-
} catch (e) {
238-
console.log(
239-
"Build a local version of OCaml compiler, it may take a couple of minutes"
240-
);
241-
try {
242-
require("./buildocaml.js").build(true);
243-
} catch (e) {
244-
console.log(e.stdout.toString());
245-
console.log(e.stderr.toString());
246-
console.log(
247-
"Building a local version of the OCaml compiler failed, check the output above for more information. A possible problem is that you don't have a compiler installed."
248-
);
249-
throw e;
250-
}
251-
console.log("configure again with local ocaml installed");
252-
matchedCompilerExn();
253-
console.log("config finished");
254-
}
255-
}
256-
257199
/**
258200
*
259201
* @param {string} sys_extension
@@ -295,36 +237,42 @@ function copyPrebuiltCompilers() {
295237
}
296238

297239
/**
298-
* @returns {boolean}
240+
* @returns {string|undefined}
299241
*/
300242
function checkPrebuiltBscCompiler() {
301243
try {
302244
var version = String(
303245
cp.execFileSync(path.join(lib_dir, "bsc" + sys_extension), ["-v"])
304246
);
305247

306-
ocamlVersion = version.substr(
248+
var myOCamlVersion = version.substr(
307249
version.indexOf(":") + 1,
308250
version.lastIndexOf(" ") - version.indexOf(":") - 1
309251
);
310-
console.log("checkoutput:", version, "ocaml version", ocamlVersion);
252+
console.log("checkoutput:", version, "ocaml version", myOCamlVersion);
311253
console.log("Prebuilt compiler works good");
312254

313-
return true;
255+
return myOCamlVersion;
314256
} catch (e) {
315257
console.log("No working prebuilt buckleScript compiler");
316-
return false;
258+
if (is_windows) {
259+
throw new Error("no prebuilt bsc compiler on windows");
260+
}
261+
return;
317262
}
318263
}
319-
320-
function buildLibs() {
264+
/**
265+
*
266+
* @param {string} stdlib
267+
*/
268+
function buildLibs(stdlib) {
321269
ensureExists(lib_dir);
322270
ensureExists(ocaml_dir);
323271
ensureExists(path.join(lib_dir, "js"));
324272
ensureExists(path.join(lib_dir, "es6"));
325273
process.env.NINJA_IGNORE_GENERATOR = "true";
326274
var releaseNinja = `
327-
stdlib = ${ocamlVersion.includes("4.06") ? "stdlib-406" : "stdlib-402"}
275+
stdlib = ${stdlib}
328276
subninja runtime/release.ninja
329277
subninja others/release.ninja
330278
subninja $stdlib/release.ninja
@@ -352,25 +300,35 @@ build all: phony runtime others $stdlib
352300
console.log("Build finished");
353301
}
354302

303+
/**
304+
* @returns {string}
305+
*/
355306
function provideCompiler() {
356-
// FIXME: weird logic
357-
// if (fs.existsSync(path.join(lib_dir,'ocaml','pervasives.cmi'))) {
358-
// console.log('Found pervasives.cmi, assume it was already built')
359-
// return true // already built before
360-
// }
361-
if (checkPrebuiltBscCompiler()) {
307+
var myVersion = checkPrebuiltBscCompiler();
308+
if (myVersion !== undefined) {
362309
copyPrebuiltCompilers();
310+
return myVersion;
363311
} else {
364-
ocamlVersion = require("./buildocaml.js").getVersionPrefix();
365-
addPath(path.join(__dirname, "..", "native", ocamlVersion, "bin"));
366-
// when not having bsc.exe
367-
tryToProvideOCamlCompiler();
312+
myVersion = require("./buildocaml.js").getVersionPrefix();
313+
var ocamlcPath = path.join(
314+
__dirname,
315+
"..",
316+
"native",
317+
myVersion,
318+
"bin",
319+
"ocamlc.opt"
320+
);
321+
if (!fs.existsSync(ocamlcPath)) {
322+
require("./buildocaml.js").build(true);
323+
} else {
324+
console.log(ocamlcPath, "is already there");
325+
}
368326
// Note this ninja file only works under *nix due to the suffix
369327
// under windows require '.exe'
370328
var releaseNinja = require("./ninjaFactory.js").libNinja({
371329
ocamlopt: "ocamlopt.opt",
372330
ext: ".exe",
373-
INCL: ocamlVersion,
331+
INCL: myVersion,
374332
isWin: is_windows
375333
});
376334

@@ -381,17 +339,15 @@ function provideCompiler() {
381339
stdio: [0, 1, 2]
382340
});
383341
fs.unlinkSync(filePath);
342+
return myVersion;
384343
}
385344
}
386345

387346
provideNinja();
388347

389-
if (is_windows) {
390-
copyPrebuiltCompilers();
391-
} else {
392-
provideCompiler();
393-
}
348+
var ocamlVersion = provideCompiler();
394349

395-
buildLibs();
350+
var stdlib = ocamlVersion.includes("4.02") ? "stdlib-402" : "stdlib-406";
396351

397-
install();
352+
buildLibs(stdlib);
353+
install(stdlib);

0 commit comments

Comments
 (0)