nixpkgs/pkgs/development/web/deno/update/librusty_v8.ts
Alyssa Ross e3e57b8f18 lib.systems: elaborate Rust metadata
We need this stuff to be available in lib so make-derivation.nix can
access it to construct the Meson cross file.

This has a couple of other advantages:

 - It makes Rust less special.  Now figuring out what Rust calls a
   platform is the same as figuring out what Linux or QEMU call it.

 - We can unify the schema used to define Rust targets, and the schema
   used to access those values later.  Just like you can set "config"
   or "system" in a platform definition, and then access those same
   keys on the elaborated platform, you can now set "rustcTarget" in
   your crossSystem, and then access "stdenv.hostPlatform.rustcTarget"
   in your code.

"rustcTarget", "rustcTargetSpec", "cargoShortTarget", and
"cargoEnvVarTarget" have the "rustc" and "cargo" prefixes because
these are not exposed to code by the compiler, and are not
standardized.  The arch/os/etc. variables are all named to match the
forms in the Rust target spec JSON.

The new rust.target-family only takes a list, since we don't need to
worry about backwards compatibility when that name is used.

The old APIs are all still functional with no warning for now, so that
it's possible for external code to use a single API on both 23.05 and
23.11.  We can introduce the warnings once 23.05 is EOL, and make them
hard errors when 23.11 is EOL.
2023-11-09 10:02:24 +01:00

87 lines
2.4 KiB
TypeScript

import * as toml from "https://deno.land/std@0.202.0/toml/mod.ts";
import { getExistingVersion, logger, run, write } from "./common.ts";
const log = logger("librusty_v8");
export interface Architecture {
nix: string;
rust: string;
}
interface PrefetchResult {
arch: Architecture;
sha256: string;
}
const getCargoLock = async (
owner: string,
repo: string,
version: string,
) =>
fetch(`https://github.com/${owner}/${repo}/raw/${version}/Cargo.lock`)
.then((res) => res.text())
.then((txt) => toml.parse(txt));
const fetchArchShaTasks = (version: string, arches: Architecture[]) =>
arches.map(
async (arch: Architecture): Promise<PrefetchResult> => {
log("Fetching:", arch.nix);
const sha256 = await run("nix-prefetch", [
`
{ fetchurl }:
fetchurl {
url = "https://github.com/denoland/rusty_v8/releases/download/v${version}/librusty_v8_release_${arch.rust}.a";
}
`,
]);
log("Done: ", arch.nix);
return { arch, sha256 };
},
);
const templateDeps = (version: string, deps: PrefetchResult[]) =>
`# auto-generated file -- DO NOT EDIT!
{ stdenv, fetchurl }:
let
fetch_librusty_v8 = args: fetchurl {
name = "librusty_v8-\${args.version}";
url = "https://github.com/denoland/rusty_v8/releases/download/v\${args.version}/librusty_v8_release_\${stdenv.hostPlatform.rust.rustcTarget}.a";
sha256 = args.shas.\${stdenv.hostPlatform.system};
meta = { inherit (args) version; };
};
in
fetch_librusty_v8 {
version = "${version}";
shas = {
${deps.map(({ arch, sha256 }) => ` ${arch.nix} = "${sha256}";`).join("\n")}
};
}
`;
export async function updateLibrustyV8(
filePath: string,
owner: string,
repo: string,
denoVersion: string,
arches: Architecture[],
) {
log("Starting librusty_v8 update");
// 0.0.0
const cargoLockData = await getCargoLock(owner, repo, denoVersion);
console.log(cargoLockData);
const packageItem = cargoLockData.package.find(({ name }) => name === "v8");
const version = packageItem.version;
if (typeof version !== "string") {
throw "no librusty_v8 version";
}
log("librusty_v8 version:", version);
const existingVersion = await getExistingVersion(filePath);
if (version === existingVersion) {
log("Version already matches latest, skipping...");
return;
}
const archShaResults = await Promise.all(fetchArchShaTasks(version, arches));
await write(filePath, templateDeps(version, archShaResults));
log("Finished deps update");
}