Skip to content

Commit

Permalink
buildRustPackage: format
Browse files Browse the repository at this point in the history
  • Loading branch information
TomaSajt committed Nov 10, 2024
1 parent e3893e5 commit 6cebfc1
Showing 1 changed file with 172 additions and 138 deletions.
310 changes: 172 additions & 138 deletions pkgs/build-support/rust/build-rust-package/default.nix
Original file line number Diff line number Diff line change
@@ -1,81 +1,102 @@
{ lib
, importCargoLock
, fetchCargoTarball
, stdenv
, callPackage
, cargoBuildHook
, cargoCheckHook
, cargoInstallHook
, cargoNextestHook
, cargoSetupHook
, cargo
, cargo-auditable
, buildPackages
, rustc
, libiconv
, windows
{
lib,
importCargoLock,
fetchCargoTarball,
stdenv,
callPackage,
cargoBuildHook,
cargoCheckHook,
cargoInstallHook,
cargoNextestHook,
cargoSetupHook,
cargo,
cargo-auditable,
buildPackages,
rustc,
libiconv,
windows,
}:

{ name ? "${args.pname}-${args.version}"
{
name ? "${args.pname}-${args.version}",

# Name for the vendored dependencies tarball
, cargoDepsName ? name

, src ? null
, srcs ? null
, preUnpack ? null
, unpackPhase ? null
, postUnpack ? null
, cargoPatches ? []
, patches ? []
, sourceRoot ? null
, logLevel ? ""
, buildInputs ? []
, nativeBuildInputs ? []
, cargoUpdateHook ? ""
, cargoDepsHook ? ""
, buildType ? "release"
, meta ? {}
, cargoLock ? null
, cargoVendorDir ? null
, checkType ? buildType
, buildNoDefaultFeatures ? false
, checkNoDefaultFeatures ? buildNoDefaultFeatures
, buildFeatures ? [ ]
, checkFeatures ? buildFeatures
, useNextest ? false
, auditable ? !cargo-auditable.meta.broken

, depsExtraArgs ? {}

# Toggles whether a custom sysroot is created when the target is a .json file.
, __internal_dontAddSysroot ? false

# Needed to `pushd`/`popd` into a subdir of a tarball if this subdir
# contains a Cargo.toml, but isn't part of a workspace (which is e.g. the
# case for `rustfmt`/etc from the `rust-sources).
# Otherwise, everything from the tarball would've been built/tested.
, buildAndTestSubdir ? null
, ... } @ args:

assert cargoVendorDir == null && cargoLock == null
-> !(args ? cargoSha256 && args.cargoSha256 != null) && !(args ? cargoHash && args.cargoHash != null)
-> throw "cargoHash, cargoVendorDir, or cargoLock must be set";
cargoDepsName ? name,

src ? null,
srcs ? null,
preUnpack ? null,
unpackPhase ? null,
postUnpack ? null,
cargoPatches ? [ ],
patches ? [ ],
sourceRoot ? null,
logLevel ? "",
buildInputs ? [ ],
nativeBuildInputs ? [ ],
cargoUpdateHook ? "",
cargoDepsHook ? "",
buildType ? "release",
meta ? { },
cargoLock ? null,
cargoVendorDir ? null,
checkType ? buildType,
buildNoDefaultFeatures ? false,
checkNoDefaultFeatures ? buildNoDefaultFeatures,
buildFeatures ? [ ],
checkFeatures ? buildFeatures,
useNextest ? false,
auditable ? !cargo-auditable.meta.broken,

depsExtraArgs ? { },

# Toggles whether a custom sysroot is created when the target is a .json file.
__internal_dontAddSysroot ? false,

# Needed to `pushd`/`popd` into a subdir of a tarball if this subdir
# contains a Cargo.toml, but isn't part of a workspace (which is e.g. the
# case for `rustfmt`/etc from the `rust-sources).
# Otherwise, everything from the tarball would've been built/tested.
buildAndTestSubdir ? null,
...
}@args:

assert
cargoVendorDir == null && cargoLock == null
->
!(args ? cargoSha256 && args.cargoSha256 != null) && !(args ? cargoHash && args.cargoHash != null)
-> throw "cargoHash, cargoVendorDir, or cargoLock must be set";

let

cargoDeps =
if cargoVendorDir != null then null
else if cargoLock != null then importCargoLock cargoLock
else fetchCargoTarball ({
inherit src srcs sourceRoot preUnpack unpackPhase postUnpack cargoUpdateHook;
name = cargoDepsName;
patches = cargoPatches;
} // lib.optionalAttrs (args ? cargoHash) {
hash = args.cargoHash;
} // lib.optionalAttrs (args ? cargoSha256) {
sha256 = lib.warn "cargoSha256 is deprecated. Please use cargoHash with SRI hash instead" args.cargoSha256;
} // depsExtraArgs);
if cargoVendorDir != null then
null
else if cargoLock != null then
importCargoLock cargoLock
else
fetchCargoTarball (
{
inherit
src
srcs
sourceRoot
preUnpack
unpackPhase
postUnpack
cargoUpdateHook
;
name = cargoDepsName;
patches = cargoPatches;
}
// lib.optionalAttrs (args ? cargoHash) {
hash = args.cargoHash;
}
// lib.optionalAttrs (args ? cargoSha256) {
sha256 = lib.warn "cargoSha256 is deprecated. Please use cargoHash with SRI hash instead" args.cargoSha256;
}
// depsExtraArgs
);

target = stdenv.hostPlatform.rust.rustcTargetSpec;
targetIsJSON = lib.hasSuffix ".json" target;
Expand All @@ -94,71 +115,84 @@ in
# See https://os.phil-opp.com/testing/ for more information.
assert useSysroot -> !(args.doCheck or true);

stdenv.mkDerivation ((removeAttrs args [ "depsExtraArgs" "cargoUpdateHook" "cargoLock" ]) // lib.optionalAttrs useSysroot {
RUSTFLAGS = "--sysroot ${sysroot} " + (args.RUSTFLAGS or "");
} // lib.optionalAttrs (stdenv.isDarwin && buildType == "debug") {
RUSTFLAGS =
"-C split-debuginfo=packed "
+ lib.optionalString useSysroot "--sysroot ${sysroot} "
+ (args.RUSTFLAGS or "");
} // {
inherit buildAndTestSubdir cargoDeps;

cargoBuildType = buildType;

cargoCheckType = checkType;

cargoBuildNoDefaultFeatures = buildNoDefaultFeatures;

cargoCheckNoDefaultFeatures = checkNoDefaultFeatures;

cargoBuildFeatures = buildFeatures;

cargoCheckFeatures = checkFeatures;

patchRegistryDeps = ./patch-registry-deps;

nativeBuildInputs = nativeBuildInputs ++ lib.optionals auditable [
(buildPackages.cargo-auditable-cargo-wrapper.override {
inherit cargo cargo-auditable;
})
] ++ [
cargoBuildHook
(if useNextest then cargoNextestHook else cargoCheckHook)
cargoInstallHook
cargoSetupHook
rustc
];

buildInputs = buildInputs
++ lib.optionals stdenv.hostPlatform.isDarwin [ libiconv ]
++ lib.optionals stdenv.hostPlatform.isMinGW [ windows.pthreads ];

patches = cargoPatches ++ patches;

PKG_CONFIG_ALLOW_CROSS =
if stdenv.buildPlatform != stdenv.hostPlatform then 1 else 0;

postUnpack = ''
eval "$cargoDepsHook"
export RUST_LOG=${logLevel}
'' + (args.postUnpack or "");

configurePhase = args.configurePhase or ''
runHook preConfigure
runHook postConfigure
'';

doCheck = args.doCheck or true;

strictDeps = true;

meta = meta // {
badPlatforms = meta.badPlatforms or [] ++ rustc.badTargetPlatforms;
# default to Rust's platforms
platforms = lib.intersectLists
meta.platforms or lib.platforms.all
rustc.targetPlatforms;
};
})
stdenv.mkDerivation (
(removeAttrs args [
"depsExtraArgs"
"cargoUpdateHook"
"cargoLock"
])
// lib.optionalAttrs useSysroot {
RUSTFLAGS = "--sysroot ${sysroot} " + (args.RUSTFLAGS or "");
}
// lib.optionalAttrs (stdenv.isDarwin && buildType == "debug") {
RUSTFLAGS =
"-C split-debuginfo=packed "
+ lib.optionalString useSysroot "--sysroot ${sysroot} "
+ (args.RUSTFLAGS or "");
}
// {
inherit buildAndTestSubdir cargoDeps;

cargoBuildType = buildType;

cargoCheckType = checkType;

cargoBuildNoDefaultFeatures = buildNoDefaultFeatures;

cargoCheckNoDefaultFeatures = checkNoDefaultFeatures;

cargoBuildFeatures = buildFeatures;

cargoCheckFeatures = checkFeatures;

patchRegistryDeps = ./patch-registry-deps;

nativeBuildInputs =
nativeBuildInputs
++ lib.optionals auditable [
(buildPackages.cargo-auditable-cargo-wrapper.override {
inherit cargo cargo-auditable;
})
]
++ [
cargoBuildHook
(if useNextest then cargoNextestHook else cargoCheckHook)
cargoInstallHook
cargoSetupHook
rustc
];

buildInputs =
buildInputs
++ lib.optionals stdenv.hostPlatform.isDarwin [ libiconv ]
++ lib.optionals stdenv.hostPlatform.isMinGW [ windows.pthreads ];

patches = cargoPatches ++ patches;

PKG_CONFIG_ALLOW_CROSS = if stdenv.buildPlatform != stdenv.hostPlatform then 1 else 0;

postUnpack =
''
eval "$cargoDepsHook"
export RUST_LOG=${logLevel}
''
+ (args.postUnpack or "");

configurePhase =
args.configurePhase or ''
runHook preConfigure
runHook postConfigure
'';

doCheck = args.doCheck or true;

strictDeps = true;

meta = meta // {
badPlatforms = meta.badPlatforms or [ ] ++ rustc.badTargetPlatforms;
# default to Rust's platforms
platforms = lib.intersectLists meta.platforms or lib.platforms.all rustc.targetPlatforms;
};
}
)

0 comments on commit 6cebfc1

Please sign in to comment.