Skip to content

Commit

Permalink
Simplify Cargo manifest generation Nix tool
Browse files Browse the repository at this point in the history
Signed-off-by: Nick Spinale <[email protected]>
  • Loading branch information
nspin committed Oct 24, 2023
1 parent 16b02af commit 25cb174
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 30 deletions.
4 changes: 1 addition & 3 deletions hacking/cargo-manifest-management/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
# SPDX-License-Identifier: BSD-2-Clause
#

project_root := ../..

run_script := \
script=$$(nix-build -A script --no-out-link) && $$script --root $(project_root)
script=$$(nix-build -A script --no-out-link) && $$script

.PHONY: none
none:
Expand Down
9 changes: 4 additions & 5 deletions hacking/cargo-manifest-management/execute_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,18 @@
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--plan', type=Path)
parser.add_argument('--root', type=Path)
parser.add_argument('--just-check', default=False, action=argparse.BooleanOptionalAction)
args = parser.parse_args()

with args.plan.open() as f:
plan = json.load(f)

run(plan, args.root, args.just_check)
run(plan, args.just_check)


def run(plan, root, just_check):
for relative_manifest_path, v in plan.items():
manifest_path = root / relative_manifest_path
def run(plan, just_check):
for manifest_path, v in plan.items():
manifest_path = Path(manifest_path)
src = Path(v['src'])
just_check_equivalence = v['justEnsureEquivalence']

Expand Down
18 changes: 11 additions & 7 deletions hacking/cargo-manifest-management/manual-manifests.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
# SPDX-License-Identifier: BSD-2-Clause
#

{
# "virtio-drivers" = "tmp/virtio-drivers";
# "mbedtls" = "tmp/rust-mbedtls/mbedtls";
# "mbedtls-sys-auto" = "tmp/rust-mbedtls/mbedtls-sys";
# "mbedtls-platform-support" = "tmp/rust-mbedtls/mbedtls-platform-support";
# "embedded-fat" = "tmp/rust-embedded-fat";
# "volatile" = "tmp/volatile";
let

relativeToWorkspaceRoot = relativePath: "${toString ../..}/${relativePath}";

in {
# "virtio-drivers" = relativeToWorkspaceRoot "tmp/virtio-drivers";
# "mbedtls" = relativeToWorkspaceRoot "tmp/rust-mbedtls/mbedtls";
# "mbedtls-sys-auto" = relativeToWorkspaceRoot "tmp/rust-mbedtls/mbedtls-sys";
# "mbedtls-platform-support" = relativeToWorkspaceRoot "tmp/rust-mbedtls/mbedtls-platform-support";
# "embedded-fat" = relativeToWorkspaceRoot "tmp/rust-embedded-fat";
# "volatile" = relativeToWorkspaceRoot "tmp/volatile";
}
30 changes: 15 additions & 15 deletions hacking/cargo-manifest-management/workspace.nix
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,17 @@ let
in
scanDirForFilesWithName dirFilter "Cargo.nix" workspaceRoot;

genrateManifest = absolutePath:
genrateManifest = cargoNixAbsolutePath:
let
relativePath =
pathBetween
workspaceRoot
(builtins.dirOf absolutePath);
absolutePath = builtins.dirOf cargoNixAbsolutePath;
manifestExpr = callManifest {
inherit relativePath;
f = import absolutePath;
inherit absolutePath;
f = import cargoNixAbsolutePath;
};
parsed = parseManifestExpr manifestExpr;
inherit (parsed) manifestValue frontmatter justEnsureEquivalence;
in {
inherit relativePath manifestValue frontmatter justEnsureEquivalence;
inherit absolutePath manifestValue frontmatter justEnsureEquivalence;
packageName = manifestValue.package.name or null;
packageVersion = manifestValue.package.version or null;
manifestTOML = renderManifest {
Expand Down Expand Up @@ -65,19 +62,19 @@ let
inherit (elaboratedNix) frontmatter justEnsureEquivalence;
};

callManifest = { relativePath, f }:
callManifest = { absolutePath, f }:
let
bespokeScope = manifestScope // {
localCrates = mkDeps relativePath;
localCrates = mkDeps absolutePath;
};
args = builtins.intersectAttrs (lib.functionArgs f) bespokeScope;
in
f args;

mkDeps = relativePath:
mkDeps = absolutePath:
let
rel = pathBetween relativePath;
generated = lib.mapAttrs (_: manifest: { path = rel manifest.relativePath; }) generatedManifestsByPackageName;
rel = pathBetween absolutePath;
generated = lib.mapAttrs (_: manifest: { path = rel manifest.absolutePath; }) generatedManifestsByPackageName;
manual = lib.mapAttrs (_: otherPath: { path = rel otherPath; }) manualManifests;
in
generated // manual;
Expand All @@ -90,7 +87,7 @@ let
(manifest: lib.optional (manifest.packageName != null) (lib.nameValuePair manifest.packageName manifest)));

plan = lib.listToAttrs (lib.forEach generatedManifestsList (manifest: {
name = "${manifest.relativePath}${lib.optionalString (manifest.relativePath != "") "/"}Cargo.toml";
name = "${manifest.absolutePath}/Cargo.toml";
value = {
src = manifest.manifestTOML;
inherit (manifest) justEnsureEquivalence;
Expand All @@ -105,7 +102,10 @@ let
'';

# for manual inspection, useful for debugging this script
links = linkFarm "crates" (lib.mapAttrs (_: v: v.src) plan);
links = linkFarm "crates"
(lib.mapAttrs'
(absolutePath: v: lib.nameValuePair "root/${absolutePath}" v.src)
plan);

in {
inherit script;
Expand Down

0 comments on commit 25cb174

Please sign in to comment.