forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildocaml.js
executable file
·102 lines (90 loc) · 2.33 KB
/
buildocaml.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
#!/usr/bin/env node
//@ts-check
var cp = require("child_process");
var path = require("path");
var fs = require("fs");
var ocamlSrcDir = path.join(__dirname, "..", "native");
var ocamlVersionFilePath = path.join(ocamlSrcDir, "VERSION");
/**
* Checks whether an OCaml compiler is available.
* If it is, we do not need to build one from the vendored sources.
*/
function checkEnvCompiler() {
try {
var o = cp.execSync(`ocamlopt.opt -version`, {
encoding: "utf-8",
});
console.log("checking env compiler");
console.log(o);
return true;
} catch (e) {
console.log(e);
return false;
}
}
exports.checkEnvCompiler = checkEnvCompiler;
/**
* Ensures the ocaml folder exists at the root of the project, either from the submodule,
* or by extracting the vendor/ocaml.tar.gz there
*/
function ensureOCamlExistsSync() {
if (!fs.existsSync(ocamlVersionFilePath)) {
if (!fs.existsSync(ocamlSrcDir)) {
fs.mkdirSync(ocamlSrcDir);
}
console.log("Extracting OCaml sources...");
cp.execSync(`tar xzf ../vendor/ocaml.tar.gz`, {
cwd: ocamlSrcDir,
stdio: [0, 1, 2],
});
}
}
/**
* @type {string}
*/
var cached = undefined;
// FIXME: this works in CI, but for release build, submodule
// is carried, so it needs to be fixed
/**
* @returns{string}
*/
function getVersionPrefix() {
if (cached !== undefined) {
return cached;
}
ensureOCamlExistsSync();
console.log(`${ocamlVersionFilePath} is used in version detection`);
var version = fs.readFileSync(ocamlVersionFilePath, "ascii");
cached = version.substr(0, version.indexOf("+"));
return cached;
}
exports.getVersionPrefix = getVersionPrefix;
/**
*
* @param {boolean} config
* @returns {string}
*/
function build(config) {
ensureOCamlExistsSync();
var prefix = path.normalize(
path.join(__dirname, "..", "native", getVersionPrefix())
);
if (config) {
var { make } = require("./config.js");
cp.execSync(
"bash ./configure --disable-naked-pointers --enable-flambda -prefix " +
prefix +
` && ${make} clean`,
{ cwd: ocamlSrcDir, stdio: [0, 1, 2] }
);
}
cp.execSync(`${make} -j9 world.opt && ${make} install `, {
cwd: ocamlSrcDir,
stdio: [0, 1, 2],
});
return prefix;
}
exports.build = build;
if (require.main === module) {
build(!process.argv.includes("-noconfig"));
}