Skip to content

Commit 3914de1

Browse files
authored
Merge branch 'main' into szg251/rust-testsuites
2 parents cf14784 + f496625 commit 3914de1

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

extras/flake-rust.nix

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
pkgs:
22

3-
{ crane, src, crateName, extraSources ? [ ], extraSourcesDir ? ".extras", data ? [ ], dataDir ? "data", devShellHook ? "", devShellTools ? [ ], testTools ? [ ] }:
3+
{ crane, src, crateName, rustVersion ? "latest", extraSources ? [ ], extraSourcesDir ? ".extras", data ? [ ], dataDir ? "data", devShellHook ? "", devShellTools ? [ ], testTools ? [ ] }:
44
let
5-
rustWithTools = pkgs.rust-bin.stable.latest.default.override {
5+
rustWithTools = pkgs.rust-bin.stable.${rustVersion}.default.override {
66
extensions = [ "rustfmt" "rust-analyzer" "clippy" "rust-src" ];
77
};
88
craneLib = crane.lib.${pkgs.system}.overrideToolchain rustWithTools;
99

1010
cleanSrc = craneLib.cleanCargoSource (craneLib.path src);
1111

12-
# Library source code with extra dependencies attached
13-
fullSrc =
12+
# Library source code with extra dependencies copied
13+
buildEnv =
1414
pkgs.stdenv.mkDerivation
1515
{
1616
src = cleanSrc;
@@ -24,6 +24,9 @@ let
2424
'';
2525
};
2626

27+
# Library source code, intended to be in extra-sources (inside the `.extras` directory)
28+
# The main difference is that dependencies are not copied, to `.extras`
29+
# but they are referenced from the parent directory (`../`).
2730
vendoredSrc =
2831
pkgs.stdenv.mkDerivation
2932
{
@@ -38,7 +41,7 @@ let
3841
};
3942

4043
commonArgs = {
41-
src = fullSrc;
44+
src = buildEnv;
4245
pname = crateName;
4346
strictDeps = true;
4447
};
@@ -89,6 +92,8 @@ in
8992

9093
packages."${crateName}-rust-src" = vendoredSrc;
9194

95+
packages."${crateName}-rust-build-env" = buildEnv;
96+
9297
checks."${crateName}-rust-test" = craneLib.cargoNextest (commonArgs // {
9398
inherit cargoArtifacts;
9499
nativeBuildInputs = testTools;

extras/lbf-nix/lbf-rust.nix

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ let
2020
files
2121
# Classes for which to generate implementations for (default lbf-prelude classes).
2222
, classes ? [ ]
23-
, # Dependencies to include in the Cabal's `build-depends` stanza.
23+
, # Dependencies to include in the Cargo's `dependencies` section.
2424
# examples: dependencies = [ "lbf-prelude" ]
2525
dependencies ? [ ]
2626
, configs ? [ ]
27-
, # Name of the package and also the name of the Cabal package.
27+
, # Name of the package and also the name of the Cargo crate.
2828
# Examples: name = "lbf-myproject"
2929
name
30-
, # Version of the package and also the version of the Cabal package.
30+
, # Version of the package and also the version of the Cargo crate.
3131
# Examples: version = "0.1.0.0"
3232
version ? "0.1.0"
3333
}: { inherit src imports files classes dependencies configs name version; };
@@ -67,7 +67,7 @@ let
6767

6868
cargoTemplate = opts: with (lbfRustOpts opts);
6969
pkgs.writeTextFile {
70-
name = "lambda-buffers-cabal-template";
70+
name = "lambda-buffers-cargo-template";
7171
text = ''
7272
[package]
7373
name = "${name}"
@@ -78,6 +78,9 @@ let
7878
'';
7979
};
8080

81+
# This is a lookup table of default crate versions used by lamba-buffers modules
82+
# Based on the contents of `build.json` a subset of these will be attached to the
83+
# Cargo.toml file
8184
crateVersions = pkgs.writeTextFile {
8285
name = "lambda-buffers-crate-versions";
8386
text = ''
@@ -107,6 +110,10 @@ let
107110
DEPS=$(echo ${builtins.concatStringsSep " " dependencies} $(cat build.json | jq -r ".[]" | sort -u));
108111
echo "Gathered Cargo deps $DEPS";
109112
cat ${cargoTemplate opts} > Cargo.toml;
113+
# Using the lookup table `crateVersions`, filling in the library version.
114+
# If no version is found, we default to a local path dependency, pointing to
115+
# a sibling directory (directory in extra-sources or .extras)
116+
# e.g.: for `lbr-prelude` we print `lbr-prelude = { path = "../lbr-prelude" }
110117
for DEP in $DEPS; do
111118
if [ $DEP != "std" ]; then
112119
echo "$(cat ${crateVersions} | grep "$DEP" || echo "$DEP = { path = \"../$DEP\" }")" >> Cargo.toml
@@ -127,6 +134,10 @@ let
127134
chmod -R u+w $out/src
128135
pushd $out/src
129136
137+
# Collecting modules of the library and attaching a module declaration
138+
# to parent modules. Any directory in the path must also
139+
# be considered as a module (e.g. for `foo/bar/baz.rs` we have to create
140+
# `lib.rs`, `foo.rs`and `foo/bar.rs`)
130141
MODS=$(find . -type f -name "*.rs")
131142
MODS+=" "
132143
MODS+=$(find . -type d)

0 commit comments

Comments
 (0)