Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mkDummySrc: fix handling of build scripts #122

Merged
merged 1 commit into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
* `buildPackage` now has more robust checks to ensure that all references to
vendored sources are removed after installation (which avoids consumers of the
final binaries having to download the sources as well)
* `mkDummySrc` how handles build scripts in a manner which ensures cargo runs
the real script later (instead of thinking it has not changed)

## [0.6.0] - 2022-09-07

Expand Down
12 changes: 10 additions & 2 deletions checks/compilesFresh.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@
, jq
}:

expected: mkDrv: args:
expectedArg: mkDrv: args:
let
runCargoAndCheckFreshness = cmd: extra: ''
runCargoAndCheckFreshness = cmd: extra:
let
expected = if builtins.isAttrs expectedArg then
expectedArg.${cmd} or ""
else
expectedArg;
in
''
cargo ${cmd} \
--release \
--message-format json-diagnostic-short \
Expand All @@ -17,6 +24,7 @@ let

# Make sure only the crate needed building
if [[ "${expected}" != "$builtTargets" ]]; then
echo for command ${cmd}
echo expected \""${expected}"\"
echo but got \""$builtTargets"\"
false
Expand Down
41 changes: 41 additions & 0 deletions checks/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,38 @@ myPkgs // {
myLib.cargoBuild {
src = ./overlapping-targets;
};
compilesFreshWithBuildScript = self.compilesFresh
{
check = (builtins.concatStringsSep "\n" [
"build-script-build"
"with-build-script"
]);
build = (builtins.concatStringsSep "\n" [
"with-build-script"
]);
test = (builtins.concatStringsSep "\n" [
"with-build-script"
]);
}
myLib.cargoBuild {
src = ./with-build-script;
};
compilesFreshWithBuildScriptCustom = self.compilesFresh
{
check = (builtins.concatStringsSep "\n" [
"build-script-mycustomscript"
"with-build-script-custom"
]);
build = (builtins.concatStringsSep "\n" [
"with-build-script-custom"
]);
test = (builtins.concatStringsSep "\n" [
"with-build-script-custom"
]);
}
myLib.cargoBuild {
src = ./with-build-script-custom;
};

customCargoTargetDirectory =
let
Expand Down Expand Up @@ -251,6 +283,15 @@ myPkgs // {

vendorGitSubset = callPackage ./vendorGitSubset.nix { };

# https://github.com/ipetkov/crane/issues/117
withBuildScript = myLib.buildPackage {
src = ./with-build-script;
};
# https://github.com/ipetkov/crane/issues/117
withBuildScriptCustom = myLib.buildPackage {
src = ./with-build-script-custom;
};

workspace = myLib.buildPackage {
src = myLib.cleanCargoSource ./workspace;
pname = "workspace";
Expand Down
1 change: 1 addition & 0 deletions checks/mkDummySrcTests/customized/expected/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ path = "tests/custom.rs"
name = "foo"

[package]
build = "cranespecific-dummy.rs"
edition = "2021"
name = "mkDummySrcSimple"
version = "0.1.0"
2 changes: 0 additions & 2 deletions checks/mkDummySrcTests/customized/expected/build.rs

This file was deleted.

8 changes: 7 additions & 1 deletion checks/mkDummySrcTests/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
}:

let
doCompare = name: expected: actual:
doCompare = name: expected: orig_actual:
let
actual = runCommand "trim-actual-${name}" { } ''
cp --recursive ${orig_actual} --no-target-directory $out --no-preserve=mode,ownership
find $out -name Cargo.toml | xargs sed -i"" 's!/nix/store/[^-]\+-dummy.rs!cranespecific-dummy.rs!'
'';
in
runCommand "compare-${name}" { } ''
echo ${expected} ${actual}
diff -r ${expected} ${actual}
Expand Down
1 change: 1 addition & 0 deletions checks/mkDummySrcTests/single-alt/expected/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name = "foo"
path = "src/custom.rs"

[package]
build = "cranespecific-dummy.rs"
edition = "2021"
name = "mkDummySrcSimple"
version = "0.1.0"
2 changes: 0 additions & 2 deletions checks/mkDummySrcTests/single-alt/expected/build.rs

This file was deleted.

1 change: 1 addition & 0 deletions checks/mkDummySrcTests/single/expected/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ path = "tests/custom.rs"
name = "foo"

[package]
build = "cranespecific-dummy.rs"
edition = "2021"
name = "mkDummySrcSimple"
version = "0.1.0"
2 changes: 0 additions & 2 deletions checks/mkDummySrcTests/single/expected/build.rs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ path = "tests/custom.rs"
name = "foo"

[package]
build = "cranespecific-dummy.rs"
edition = "2021"
name = "bar"
version = "0.1.0"

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ path = "../foo/bar/baz"
name = "foo"

[package]
build = "cranespecific-dummy.rs"
edition = "2021"
name = "qux"
version = "0.1.0"
2 changes: 0 additions & 2 deletions checks/mkDummySrcTests/workspace/expected/member/qux/build.rs

This file was deleted.

16 changes: 16 additions & 0 deletions checks/with-build-script-custom/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions checks/with-build-script-custom/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "with-build-script-custom"
version = "0.1.0"
edition = "2021"
build = "mycustomscript.rs"

[build-dependencies]
byteorder = "*"
10 changes: 10 additions & 0 deletions checks/with-build-script-custom/mycustomscript.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use std::{
fs,
io::{self, Write},
};

fn main() -> io::Result<()> {
fs::create_dir_all("./target")?;
fs::File::create("./target/mydata")?.write_all(b"hello world!\n")?;
Ok(())
}
4 changes: 4 additions & 0 deletions checks/with-build-script-custom/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const DATA: &str = include_str!("../target/mydata");
fn main() {
println!("{DATA}");
}
16 changes: 16 additions & 0 deletions checks/with-build-script/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions checks/with-build-script/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "with-build-script"
version = "0.1.0"
edition = "2021"

[build-dependencies]
byteorder = "*"
10 changes: 10 additions & 0 deletions checks/with-build-script/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use std::{
fs,
io::{self, Write},
};

fn main() -> io::Result<()> {
fs::create_dir_all("./target")?;
fs::File::create("./target/mydata")?.write_all(b"hello world!\n")?;
Ok(())
}
4 changes: 4 additions & 0 deletions checks/with-build-script/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const DATA: &str = include_str!("../target/mydata");
fn main() {
println!("{DATA}");
}
30 changes: 25 additions & 5 deletions lib/mkDummySrc.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ let

inherit (lib)
optionalString
recursiveUpdate
removePrefix;

inherit (lib.strings) concatStrings;
Expand Down Expand Up @@ -139,9 +140,30 @@ let
cargoTomlDest = builtins.unsafeDiscardStringContext (removePrefix cargoTomlsBase (toString p));
parentDir = "$out/${dirOf cargoTomlDest}";

trimmedCargoToml = cleanCargoToml {
cargoToml = p;
};
# Override the cleaned Cargo.toml with a build script which points to our dummy
# source. We need a build script present to cache build-dependencies, which can be
# achieved by dropping a build.rs file in the source directory. Except that is the most
# common format to use, and cargo appears to use file timestamps to check for changes
# to the build script, yet nix will strip all timestamps when putting the sources in the
# store. This results in cargo not realizing that our dummy script and the project's
# _real_ script are, in fact, different. So we work around this by having the Cargo.toml
# file point directly to our dummy source in the store.
# https://github.com/ipetkov/crane/issues/117
trimmedCargoToml =
let
cleanedCargoToml = cleanCargoToml {
cargoToml = p;
};
in
# Only update if we have a `package` definition, workspaces Cargo.tomls don't need updating
if cleanedCargoToml ? package then
recursiveUpdate
cleanedCargoToml
{
package.build = dummyrs;
}
else
cleanedCargoToml;

safeStubLib =
if hasAttr "lib" trimmedCargoToml
Expand All @@ -160,8 +182,6 @@ let
mkdir -p ${parentDir}
cp ${writeTOML "Cargo.toml" trimmedCargoToml} $out/${cargoTomlDest}
'' + optionalString (trimmedCargoToml ? package) ''
# To build build-dependencies
${cpDummy parentDir "build.rs"}
# To build regular and dev dependencies (cargo build + cargo test)
${cpDummy parentDir "src/lib.rs"}

Expand Down