-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
buildRustPackage: factor out setting up .cargo/config to cargoSetupHook #112500
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
Changes from all commits
d083f41
4a4da22
da73f94
198dd77
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| cargoSetupPostUnpackHook() { | ||
| echo "Executing cargoSetupPostUnpackHook" | ||
|
|
||
| # Some cargo builds include build hooks that modify their own vendor | ||
| # dependencies. This copies the vendor directory into the build tree and makes | ||
| # it writable. If we're using a tarball, the unpackFile hook already handles | ||
| # this for us automatically. | ||
| if [ -z $cargoVendorDir ]; then | ||
| unpackFile "$cargoDeps" | ||
| export cargoDepsCopy=$(stripHash $cargoDeps) | ||
| else | ||
| cargoDepsCopy="$sourceRoot/${cargoRoot:+$cargoRoot/}${cargoVendorDir}" | ||
| fi | ||
|
|
||
| if [ ! -d .cargo ]; then | ||
| mkdir .cargo | ||
| fi | ||
|
|
||
| config="$(pwd)/$cargoDepsCopy/.cargo/config"; | ||
| if [[ ! -e $config ]]; then | ||
| config=@defaultConfig@ | ||
| fi; | ||
|
|
||
| tmp_config=$(mktemp) | ||
| substitute $config $tmp_config \ | ||
| --subst-var-by vendor "$(pwd)/$cargoDepsCopy" | ||
| cat ${tmp_config} >> .cargo/config | ||
|
|
||
| cat >> .cargo/config <<'EOF' | ||
| @rustTarget@ | ||
| EOF | ||
|
|
||
| echo "Finished cargoSetupPostUnpackHook" | ||
| } | ||
|
|
||
| # After unpacking and applying patches, check that the Cargo.lock matches our | ||
| # src package. Note that we do this after the patchPhase, because the | ||
| # patchPhase may create the Cargo.lock if upstream has not shipped one. | ||
| cargoSetupPostPatchHook() { | ||
| echo "Executing cargoSetupPostPatchHook" | ||
|
|
||
| cargoDepsLockfile="$NIX_BUILD_TOP/$cargoDepsCopy/Cargo.lock" | ||
| srcLockfile="$NIX_BUILD_TOP/$sourceRoot/${cargoRoot:+$cargoRoot/}/Cargo.lock" | ||
|
|
||
| echo "Validating consistency between $srcLockfile and $cargoDepsLockfile" | ||
| if ! @diff@ $srcLockfile $cargoDepsLockfile; then | ||
|
|
||
| # If the diff failed, first double-check that the file exists, so we can | ||
| # give a friendlier error msg. | ||
| if ! [ -e $srcLockfile ]; then | ||
| echo "ERROR: Missing Cargo.lock from src. Expected to find it at: $srcLockfile" | ||
| echo "Hint: You can use the cargoPatches attribute to add a Cargo.lock manually to the build." | ||
| exit 1 | ||
| fi | ||
|
|
||
| if ! [ -e $cargoDepsLockfile ]; then | ||
| echo "ERROR: Missing lockfile from cargo vendor. Expected to find it at: $cargoDepsLockfile" | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo | ||
| echo "ERROR: cargoSha256 is out of date" | ||
| echo | ||
| echo "Cargo.lock is not the same in $cargoDepsCopy" | ||
| echo | ||
| echo "To fix the issue:" | ||
| echo '1. Use "0000000000000000000000000000000000000000000000000000" as the cargoSha256 value' | ||
| echo "2. Build the derivation and wait for it to fail with a hash mismatch" | ||
| echo "3. Copy the 'got: sha256:' value back into the cargoSha256 field" | ||
|
||
| echo | ||
|
|
||
| exit 1 | ||
| fi | ||
|
|
||
| unset cargoDepsCopy | ||
|
|
||
| echo "Finished cargoSetupPostPatchHook" | ||
| } | ||
|
|
||
| postUnpackHooks+=(cargoSetupPostUnpackHook) | ||
|
|
||
| if [ -z ${cargoVendorDir-} ]; then | ||
| postPatchHooks+=(cargoSetupPostPatchHook) | ||
| fi | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| { buildPackages | ||
| , callPackage | ||
| , diffutils | ||
| , lib | ||
| , makeSetupHook | ||
| , rust | ||
| , stdenv | ||
| , target ? rust.toRustTargetSpec stdenv.hostPlatform | ||
| }: | ||
|
|
||
| let | ||
| targetIsJSON = lib.hasSuffix ".json" target; | ||
|
|
||
| # see https://github.com/rust-lang/cargo/blob/964a16a28e234a3d397b2a7031d4ab4a428b1391/src/cargo/core/compiler/compile_kind.rs#L151-L168 | ||
| # the "${}" is needed to transform the path into a /nix/store path before baseNameOf | ||
| shortTarget = if targetIsJSON then | ||
| (lib.removeSuffix ".json" (builtins.baseNameOf "${target}")) | ||
| else target; | ||
| ccForBuild="${buildPackages.stdenv.cc}/bin/${buildPackages.stdenv.cc.targetPrefix}cc"; | ||
| ccForHost="${stdenv.cc}/bin/${stdenv.cc.targetPrefix}cc"; | ||
| in { | ||
| cargoSetupHook = callPackage ({ }: | ||
| makeSetupHook { | ||
| name = "cargo-setup-hook.sh"; | ||
| deps = [ ]; | ||
| substitutions = { | ||
| defaultConfig = ../fetchcargo-default-config.toml; | ||
|
|
||
| # Specify the stdenv's `diff` by abspath to ensure that the user's build | ||
| # inputs do not cause us to find the wrong `diff`. | ||
| # The `.nativeDrv` stanza works like nativeBuildInputs and ensures cross-compiling has the right version available. | ||
| diff = "${diffutils.nativeDrv or diffutils}/bin/diff"; | ||
|
|
||
| # Target platform | ||
| rustTarget = '' | ||
| [target."${rust.toRustTarget stdenv.buildPlatform}"] | ||
| "linker" = "${ccForBuild}" | ||
| ${lib.optionalString (stdenv.buildPlatform.config != stdenv.hostPlatform.config) '' | ||
| [target."${shortTarget}"] | ||
| "linker" = "${ccForHost}" | ||
| ${# https://github.com/rust-lang/rust/issues/46651#issuecomment-433611633 | ||
| lib.optionalString (stdenv.hostPlatform.isMusl && stdenv.hostPlatform.isAarch64) '' | ||
| "rustflags" = [ "-C", "target-feature=+crt-static", "-C", "link-arg=-lgcc" ] | ||
| ''} | ||
| ''} | ||
| ''; | ||
| }; | ||
| } ./cargo-setup-hook.sh) {}; | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.