-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbuild.js
More file actions
171 lines (153 loc) · 5.29 KB
/
build.js
File metadata and controls
171 lines (153 loc) · 5.29 KB
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
const fs = require("fs");
const os = require("os");
const path = require("path");
const { execFileSync, execSync } = require("child_process");
const THREADS = os.cpus().length;
const DEFAULT_LBUG_SOURCE_DIR = path.resolve(__dirname, "../ladybug");
function resolveExistingPath(candidate) {
if (!candidate) {
return null;
}
const resolved = path.resolve(candidate);
return fs.existsSync(resolved) ? resolved : null;
}
function getDefaultBuildDir(lbugSourceDir) {
return lbugSourceDir ? path.join(lbugSourceDir, "build", "release") : null;
}
function getDefaultPrecompiledLibPath(lbugBuildDir) {
if (!lbugBuildDir) {
return null;
}
const candidates = process.platform === "win32"
? [
path.join(lbugBuildDir, "src", "Release", "lbug.lib"),
path.join(lbugBuildDir, "src", "lbug.lib"),
path.join(lbugBuildDir, "src", "Release", "liblbug.lib"),
path.join(lbugBuildDir, "src", "liblbug.lib"),
]
: [path.join(lbugBuildDir, "src", "liblbug.a")];
return candidates.find((candidate) => fs.existsSync(candidate)) || null;
}
function getDefaultExtraLinkLibs(lbugBuildDir, precompiledLibPath) {
if (!lbugBuildDir || !precompiledLibPath) {
return [];
}
const linkTxtCandidates = [
path.join(lbugBuildDir, "tools", "python_api", "CMakeFiles", "_lbug.dir", "link.txt"),
path.join(lbugBuildDir, "src", "CMakeFiles", "lbug_shared.dir", "link.txt"),
];
const tokenPattern = /"[^"]+"|\S+/g;
for (const linkTxtPath of linkTxtCandidates) {
if (!fs.existsSync(linkTxtPath)) {
continue;
}
const linkTxtDir = path.dirname(linkTxtPath);
const linkBaseDir = linkTxtPath.includes(`${path.sep}tools${path.sep}python_api${path.sep}`)
? path.join(lbugBuildDir, "tools", "python_api")
: linkTxtPath.includes(`${path.sep}src${path.sep}CMakeFiles${path.sep}`)
? path.join(lbugBuildDir, "src")
: linkTxtDir;
const tokens = fs.readFileSync(linkTxtPath, "utf8").match(tokenPattern) || [];
const libs = [];
let sawPrecompiledLib = false;
const linkLineHasPrecompiledLib = tokens.some((rawToken) => {
const token = rawToken.replace(/^"(.*)"$/, "$1");
if (token.startsWith("-")) {
return false;
}
const resolvedToken = fs.existsSync(path.resolve(linkBaseDir, token))
? path.resolve(linkBaseDir, token)
: path.resolve(linkTxtDir, token);
return resolvedToken === precompiledLibPath;
});
let outputPath = null;
for (let i = 0; i < tokens.length; i++) {
const rawToken = tokens[i];
const token = rawToken.replace(/^"(.*)"$/, "$1");
if (token === "-o") {
const outputToken = tokens[++i]?.replace(/^"(.*)"$/, "$1");
outputPath = outputToken ? path.resolve(linkBaseDir, outputToken) : null;
continue;
}
if (token === "-install_name" || token === "-current_version" || token === "-compatibility_version") {
i++;
continue;
}
if (token.startsWith("@")) {
continue;
}
const resolvedToken = token.startsWith("-")
? token
: fs.existsSync(path.resolve(linkBaseDir, token))
? path.resolve(linkBaseDir, token)
: path.resolve(linkTxtDir, token);
if (!sawPrecompiledLib && linkLineHasPrecompiledLib) {
if (resolvedToken === precompiledLibPath) {
sawPrecompiledLib = true;
}
continue;
}
if (token.startsWith("-l")) {
libs.push(token);
continue;
}
if (!/\.(a|lib|dylib|so|tbd)$/.test(token)) {
continue;
}
if (resolvedToken === precompiledLibPath || resolvedToken === outputPath) {
continue;
}
libs.push(resolvedToken);
}
if (libs.length > 0) {
return [...new Set(libs)];
}
}
return [];
}
console.log(`Using ${THREADS} threads to build Lbug.`);
const env = { ...process.env };
const lbugSourceDir = resolveExistingPath(env.LBUG_SOURCE_DIR || DEFAULT_LBUG_SOURCE_DIR);
const lbugBuildDir = resolveExistingPath(env.LBUG_BUILD_DIR || getDefaultBuildDir(lbugSourceDir));
const precompiledLibPath = resolveExistingPath(
env.LBUG_NODEJS_PRECOMPILED_LIB_PATH || getDefaultPrecompiledLibPath(lbugBuildDir)
);
const extraLinkLibs = getDefaultExtraLinkLibs(lbugBuildDir, precompiledLibPath);
if (lbugSourceDir) {
env.LBUG_SOURCE_DIR = lbugSourceDir;
}
if (lbugBuildDir) {
env.LBUG_BUILD_DIR = lbugBuildDir;
}
const cmakeArgs = env.EXTRA_CMAKE_FLAGS
? env.EXTRA_CMAKE_FLAGS.trim().split(/\s+/).filter(Boolean)
: [];
if (lbugSourceDir) {
cmakeArgs.push(`-DLBUG_SOURCE_DIR=${lbugSourceDir}`);
}
if (lbugBuildDir) {
cmakeArgs.push(`-DLBUG_BUILD_DIR=${lbugBuildDir}`);
}
if (precompiledLibPath) {
cmakeArgs.push(
"-DBUILD_LBUG=FALSE",
"-DBUILD_SHELL=FALSE",
"-DLBUG_NODEJS_USE_PRECOMPILED_LIB=TRUE",
`-DLBUG_NODEJS_PRECOMPILED_LIB_PATH=${precompiledLibPath}`
);
console.log(`Using precompiled liblbug from ${precompiledLibPath}.`);
}
if (extraLinkLibs.length > 0) {
cmakeArgs.push(`-DLBUG_NODEJS_EXTRA_LINK_LIBS=${extraLinkLibs.join(";")}`);
}
execSync("npm run clean", { stdio: "inherit" });
execFileSync("cmake", ["-S", ".", "-B", "build", ...cmakeArgs], {
cwd: __dirname,
env,
stdio: "inherit",
});
execFileSync("cmake", ["--build", "build", "-j", `${THREADS}`], {
cwd: __dirname,
env,
stdio: "inherit",
});