2020-02-14 00:52:16 +01:00
|
|
|
/*
|
2024-09-06 18:56:18 +02:00
|
|
|
Copyright 2024 New Vector Ltd.
|
|
|
|
Copyright 2020, 2021 The Matrix.org Foundation C.I.C.
|
2020-02-14 00:52:16 +01:00
|
|
|
|
2024-09-06 18:56:18 +02:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2020-02-14 00:52:16 +01:00
|
|
|
*/
|
|
|
|
|
2024-10-30 19:06:03 +01:00
|
|
|
import path, { dirname } from "node:path";
|
|
|
|
import { fileURLToPath } from "node:url";
|
2020-02-14 00:52:16 +01:00
|
|
|
|
2024-10-30 19:06:03 +01:00
|
|
|
import HakEnv from "./hakEnv.js";
|
|
|
|
import type { TargetId } from "./target.js";
|
|
|
|
import type { DependencyInfo } from "./dep.js";
|
|
|
|
import { loadJsonFile } from "../../src/utils.js";
|
|
|
|
import packageJson from "../../package.json";
|
2020-02-14 00:52:16 +01:00
|
|
|
|
2022-12-15 12:00:58 +01:00
|
|
|
const GENERALCOMMANDS = ["target"];
|
2020-02-14 00:52:16 +01:00
|
|
|
|
|
|
|
// These can only be run on specific modules
|
2023-04-24 14:19:10 +02:00
|
|
|
const MODULECOMMANDS = ["check", "fetch", "link", "build", "copy", "clean"];
|
2020-02-14 00:52:16 +01:00
|
|
|
|
2021-07-07 00:52:19 +02:00
|
|
|
// Shortcuts for multiple commands at once (useful for building universal binaries
|
|
|
|
// because you can run the fetch/fetchDeps/build for each arch and then copy/link once)
|
2022-12-05 12:50:49 +01:00
|
|
|
const METACOMMANDS: Record<string, string[]> = {
|
2023-04-24 14:19:10 +02:00
|
|
|
fetchandbuild: ["check", "fetch", "build"],
|
2022-12-15 12:00:58 +01:00
|
|
|
copyandlink: ["copy", "link"],
|
2021-07-07 00:52:19 +02:00
|
|
|
};
|
|
|
|
|
2020-02-17 16:44:06 +01:00
|
|
|
// Scripts valid in a hak.json 'scripts' section
|
2023-04-24 14:19:10 +02:00
|
|
|
const HAKSCRIPTS = ["check", "fetch", "build"];
|
2020-02-14 00:52:16 +01:00
|
|
|
|
2024-10-30 19:06:03 +01:00
|
|
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
|
|
|
2022-12-01 08:20:55 +01:00
|
|
|
async function main(): Promise<void> {
|
2024-06-19 11:20:17 +02:00
|
|
|
const prefix = path.join(__dirname, "..", "..");
|
2020-02-14 00:52:16 +01:00
|
|
|
|
2022-05-27 00:05:59 +02:00
|
|
|
const targetIds: TargetId[] = [];
|
2021-06-18 18:38:22 +02:00
|
|
|
// Apply `--target <target>` option if specified
|
2021-07-07 00:52:19 +02:00
|
|
|
// Can be specified multiple times for the copy command to bundle
|
2024-10-30 19:06:03 +01:00
|
|
|
// multiple arches into a single universal output module)
|
2022-12-15 12:00:58 +01:00
|
|
|
for (;;) {
|
|
|
|
// eslint-disable-line no-constant-condition
|
|
|
|
const targetIndex = process.argv.indexOf("--target");
|
2021-07-07 00:52:19 +02:00
|
|
|
if (targetIndex === -1) break;
|
|
|
|
|
2022-12-15 12:00:58 +01:00
|
|
|
if (targetIndex + 1 >= process.argv.length) {
|
2021-06-18 18:38:22 +02:00
|
|
|
console.error("--target option specified without a target");
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
// Extract target ID and remove from args
|
2021-12-14 15:32:27 +01:00
|
|
|
targetIds.push(process.argv.splice(targetIndex, 2)[1] as TargetId);
|
2021-06-18 18:38:22 +02:00
|
|
|
}
|
|
|
|
|
2022-12-15 12:00:58 +01:00
|
|
|
const hakEnvs = targetIds.map((tid) => new HakEnv(prefix, tid));
|
2021-12-13 22:15:17 +01:00
|
|
|
if (hakEnvs.length == 0) hakEnvs.push(new HakEnv(prefix, null));
|
|
|
|
for (const h of hakEnvs) {
|
|
|
|
await h.init();
|
|
|
|
}
|
2021-07-07 00:52:19 +02:00
|
|
|
const hakEnv = hakEnvs[0];
|
2020-02-14 00:52:16 +01:00
|
|
|
|
2022-05-27 00:05:59 +02:00
|
|
|
const deps: Record<string, DependencyInfo> = {};
|
2020-02-14 00:52:16 +01:00
|
|
|
|
|
|
|
const hakDepsCfg = packageJson.hakDependencies || {};
|
|
|
|
|
2024-10-30 19:06:03 +01:00
|
|
|
for (const dep in hakDepsCfg) {
|
2022-12-15 12:00:58 +01:00
|
|
|
const hakJsonPath = path.join(prefix, "hak", dep, "hak.json");
|
2022-05-27 00:05:59 +02:00
|
|
|
let hakJson: Record<string, any>;
|
2020-02-14 00:52:16 +01:00
|
|
|
try {
|
2024-10-30 19:06:03 +01:00
|
|
|
hakJson = loadJsonFile(hakJsonPath);
|
2024-10-16 16:59:12 +02:00
|
|
|
} catch {
|
2020-02-14 00:52:16 +01:00
|
|
|
console.error("No hak.json found for " + dep + ".");
|
|
|
|
console.log("Expecting " + hakJsonPath);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
deps[dep] = {
|
|
|
|
name: dep,
|
2024-10-30 19:06:03 +01:00
|
|
|
version: hakDepsCfg[dep as keyof typeof hakDepsCfg],
|
2020-02-14 00:52:16 +01:00
|
|
|
cfg: hakJson,
|
2022-12-15 12:00:58 +01:00
|
|
|
moduleHakDir: path.join(prefix, "hak", dep),
|
2020-02-15 17:52:41 +01:00
|
|
|
moduleDotHakDir: path.join(hakEnv.dotHakDir, dep),
|
2021-07-07 00:52:19 +02:00
|
|
|
moduleTargetDotHakDir: path.join(hakEnv.dotHakDir, dep, hakEnv.getTargetId()),
|
2022-12-15 12:00:58 +01:00
|
|
|
moduleBuildDir: path.join(hakEnv.dotHakDir, dep, hakEnv.getTargetId(), "build"),
|
|
|
|
moduleBuildDirs: hakEnvs.map((h) => path.join(h.dotHakDir, dep, h.getTargetId(), "build")),
|
|
|
|
moduleOutDir: path.join(hakEnv.dotHakDir, "hakModules", dep),
|
|
|
|
nodeModuleBinDir: path.join(hakEnv.dotHakDir, dep, hakEnv.getTargetId(), "build", "node_modules", ".bin"),
|
|
|
|
depPrefix: path.join(hakEnv.dotHakDir, dep, hakEnv.getTargetId(), "opt"),
|
2020-02-14 00:52:16 +01:00
|
|
|
scripts: {},
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const s of HAKSCRIPTS) {
|
2024-10-30 19:06:03 +01:00
|
|
|
if (hakJson.scripts?.[s]) {
|
|
|
|
const scriptModule = await import(path.join("file://", prefix, "hak", dep, hakJson.scripts[s]));
|
|
|
|
if (scriptModule.default) {
|
2021-12-14 15:32:27 +01:00
|
|
|
deps[dep].scripts[s] = scriptModule.default;
|
|
|
|
} else {
|
|
|
|
deps[dep].scripts[s] = scriptModule;
|
|
|
|
}
|
2020-02-14 00:52:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-27 00:05:59 +02:00
|
|
|
let cmds: string[];
|
2020-02-14 00:52:16 +01:00
|
|
|
if (process.argv.length < 3) {
|
2023-04-24 14:19:10 +02:00
|
|
|
cmds = ["check", "fetch", "build", "copy", "link"];
|
2021-07-07 00:52:19 +02:00
|
|
|
} else if (METACOMMANDS[process.argv[2]]) {
|
|
|
|
cmds = METACOMMANDS[process.argv[2]];
|
2020-02-17 21:10:58 +01:00
|
|
|
} else {
|
|
|
|
cmds = [process.argv[2]];
|
2020-02-14 00:52:16 +01:00
|
|
|
}
|
|
|
|
|
2022-12-15 12:00:58 +01:00
|
|
|
if (hakEnvs.length > 1 && cmds.some((c) => !["copy", "link"].includes(c))) {
|
2021-07-07 00:52:19 +02:00
|
|
|
// We allow link here too for convenience because it's completely arch independent
|
|
|
|
console.error("Multiple targets only supported with the copy command");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-17 21:10:58 +01:00
|
|
|
let modules = process.argv.slice(3);
|
|
|
|
if (modules.length === 0) modules = Object.keys(deps);
|
2020-02-14 00:52:16 +01:00
|
|
|
|
2020-02-17 21:10:58 +01:00
|
|
|
for (const cmd of cmds) {
|
|
|
|
if (GENERALCOMMANDS.includes(cmd)) {
|
2022-12-15 12:00:58 +01:00
|
|
|
if (cmd === "target") {
|
2020-02-17 21:10:58 +01:00
|
|
|
console.log(hakEnv.getNodeTriple());
|
|
|
|
}
|
|
|
|
return;
|
2020-02-14 00:52:16 +01:00
|
|
|
}
|
|
|
|
|
2020-02-17 21:10:58 +01:00
|
|
|
if (!MODULECOMMANDS.includes(cmd)) {
|
|
|
|
console.error("Unknown command: " + cmd);
|
|
|
|
console.log("Commands I know about:");
|
|
|
|
for (const cmd of MODULECOMMANDS) {
|
|
|
|
console.log("\t" + cmd);
|
|
|
|
}
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2020-02-14 00:52:16 +01:00
|
|
|
|
2022-12-15 12:00:58 +01:00
|
|
|
const cmdFunc = (await import("./" + cmd)).default;
|
2020-02-14 00:52:16 +01:00
|
|
|
|
2020-02-17 21:10:58 +01:00
|
|
|
for (const mod of modules) {
|
|
|
|
const depInfo = deps[mod];
|
|
|
|
if (depInfo === undefined) {
|
2022-12-15 12:00:58 +01:00
|
|
|
console.log("Module " + mod + " not found - is it in hakDependencies " + "in your package.json?");
|
2020-02-17 21:10:58 +01:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
console.log("hak " + cmd + ": " + mod);
|
|
|
|
await cmdFunc(hakEnv, depInfo);
|
2020-02-14 00:52:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-15 12:00:58 +01:00
|
|
|
main().catch((err) => {
|
2021-06-22 14:35:39 +02:00
|
|
|
console.error(err);
|
|
|
|
process.exit(1);
|
|
|
|
});
|