aboutsummaryrefslogtreecommitdiff
path: root/pkgs/build-support
diff options
context:
space:
mode:
authorJohn Ericson <John.Ericson@Obsidian.Systems>2020-11-28 21:33:03 -0500
committerJohn Ericson <John.Ericson@Obsidian.Systems>2020-11-28 21:33:03 -0500
commit73425f6c3b1761d0331aa31d8c025729dbf4c566 (patch)
tree0a25f78736864f15d8371637b22f4fffaddfa340 /pkgs/build-support
parente91a1e91a60ce26b5c90bf0620a564534d823762 (diff)
parentaa5dd7ef5e838e7915c3a9694db22c464857a82b (diff)
Merge remote-tracking branch 'upstream/master' into staging
Diffstat (limited to 'pkgs/build-support')
-rw-r--r--pkgs/build-support/fetchzip/default.nix9
-rw-r--r--pkgs/build-support/rust/build-rust-crate/build-crate.nix2
-rw-r--r--pkgs/build-support/rust/build-rust-crate/configure-crate.nix4
-rw-r--r--pkgs/build-support/rust/default.nix40
-rw-r--r--pkgs/build-support/rust/sysroot/Cargo.lock29
-rw-r--r--pkgs/build-support/rust/sysroot/cargo.py45
-rw-r--r--pkgs/build-support/rust/sysroot/default.nix41
-rwxr-xr-xpkgs/build-support/rust/sysroot/update-lockfile.sh21
8 files changed, 179 insertions, 12 deletions
diff --git a/pkgs/build-support/fetchzip/default.nix b/pkgs/build-support/fetchzip/default.nix
index c61df8ceb001..44748f231bc2 100644
--- a/pkgs/build-support/fetchzip/default.nix
+++ b/pkgs/build-support/fetchzip/default.nix
@@ -44,8 +44,13 @@
mv "$unpackDir/$fn" "$out"
'' else ''
mv "$unpackDir" "$out"
- '') #*/
- + extraPostFetch;
+ '')
+ + extraPostFetch
+ # Remove write permissions for files unpacked with write bits set
+ # Fixes https://github.com/NixOS/nixpkgs/issues/38649
+ + ''
+ chmod -R a-w "$out"
+ '';
} // removeAttrs args [ "stripRoot" "extraPostFetch" ])).overrideAttrs (x: {
# Hackety-hack: we actually need unzip hooks, too
nativeBuildInputs = x.nativeBuildInputs ++ [ unzip ];
diff --git a/pkgs/build-support/rust/build-rust-crate/build-crate.nix b/pkgs/build-support/rust/build-rust-crate/build-crate.nix
index 142109cef49f..84d1b2300f14 100644
--- a/pkgs/build-support/rust/build-rust-crate/build-crate.nix
+++ b/pkgs/build-support/rust/build-rust-crate/build-crate.nix
@@ -15,7 +15,7 @@
++ [(mkRustcDepArgs dependencies crateRenames)]
++ [(mkRustcFeatureArgs crateFeatures)]
++ extraRustcOpts
- ++ lib.optional (stdenv.hostPlatform != stdenv.buildPlatform) "--target ${rust.toRustTarget stdenv.hostPlatform} -C linker=${stdenv.hostPlatform.config}-gcc"
+ ++ lib.optional (stdenv.hostPlatform != stdenv.buildPlatform) "--target ${rust.toRustTargetSpec stdenv.hostPlatform} -C linker=${stdenv.hostPlatform.config}-gcc"
# since rustc 1.42 the "proc_macro" crate is part of the default crate prelude
# https://github.com/rust-lang/cargo/commit/4d64eb99a4#diff-7f98585dbf9d30aa100c8318e2c77e79R1021-R1022
++ lib.optional (lib.elem "proc-macro" crateType) "--extern proc_macro"
diff --git a/pkgs/build-support/rust/build-rust-crate/configure-crate.nix b/pkgs/build-support/rust/build-rust-crate/configure-crate.nix
index 18587f7047c4..5ada40b3b9bb 100644
--- a/pkgs/build-support/rust/build-rust-crate/configure-crate.nix
+++ b/pkgs/build-support/rust/build-rust-crate/configure-crate.nix
@@ -135,8 +135,8 @@ in ''
export CARGO_MANIFEST_DIR=$(pwd)
export DEBUG="${toString (!release)}"
export OPT_LEVEL="${toString optLevel}"
- export TARGET="${rust.toRustTarget stdenv.hostPlatform}"
- export HOST="${rust.toRustTarget stdenv.buildPlatform}"
+ export TARGET="${rust.toRustTargetSpec stdenv.hostPlatform}"
+ export HOST="${rust.toRustTargetSpec stdenv.buildPlatform}"
export PROFILE=${if release then "release" else "debug"}
export OUT_DIR=$(pwd)/target/build/${crateName}.out
export CARGO_PKG_VERSION_MAJOR=${lib.elemAt version 0}
diff --git a/pkgs/build-support/rust/default.nix b/pkgs/build-support/rust/default.nix
index f6177ce198da..8e47a2b0bf25 100644
--- a/pkgs/build-support/rust/default.nix
+++ b/pkgs/build-support/rust/default.nix
@@ -4,6 +4,10 @@
, cargo
, diffutils
, fetchCargoTarball
+, runCommandNoCC
+, rustPlatform
+, callPackage
+, remarshal
, git
, rust
, rustc
@@ -26,12 +30,15 @@
, cargoBuildFlags ? []
, buildType ? "release"
, meta ? {}
-, target ? null
+, target ? rust.toRustTargetSpec stdenv.hostPlatform
, cargoVendorDir ? null
, checkType ? buildType
, depsExtraArgs ? {}
, cargoParallelTestThreads ? true
+# 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).
@@ -69,13 +76,26 @@ let
cargoDepsCopy="$sourceRoot/${cargoVendorDir}"
'';
- rustTarget = if target == null then rust.toRustTarget stdenv.hostPlatform else target;
+ targetIsJSON = stdenv.lib.hasSuffix ".json" target;
+ useSysroot = targetIsJSON && !__internal_dontAddSysroot;
+
+ # 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
+ (stdenv.lib.removeSuffix ".json" (builtins.baseNameOf "${target}"))
+ else target;
+
+ sysroot = (callPackage ./sysroot {}) {
+ inherit target shortTarget;
+ RUSTFLAGS = args.RUSTFLAGS or "";
+ originalCargoToml = src + /Cargo.toml; # profile info is later extracted
+ };
ccForBuild="${buildPackages.stdenv.cc}/bin/${buildPackages.stdenv.cc.targetPrefix}cc";
cxxForBuild="${buildPackages.stdenv.cc}/bin/${buildPackages.stdenv.cc.targetPrefix}c++";
ccForHost="${stdenv.cc}/bin/${stdenv.cc.targetPrefix}cc";
cxxForHost="${stdenv.cc}/bin/${stdenv.cc.targetPrefix}c++";
- releaseDir = "target/${rustTarget}/${buildType}";
+ releaseDir = "target/${shortTarget}/${buildType}";
tmpDir = "${releaseDir}-tmp";
# Specify the stdenv's `diff` by abspath to ensure that the user's build
@@ -85,7 +105,13 @@ let
in
-stdenv.mkDerivation ((removeAttrs args ["depsExtraArgs"]) // {
+# Tests don't currently work for `no_std`, and all custom sysroots are currently built without `std`.
+# See https://os.phil-opp.com/testing/ for more information.
+assert useSysroot -> !(args.doCheck or true);
+
+stdenv.mkDerivation ((removeAttrs args ["depsExtraArgs"]) // stdenv.lib.optionalAttrs useSysroot {
+ RUSTFLAGS = "--sysroot ${sysroot} " + (args.RUSTFLAGS or "");
+} // {
inherit cargoDeps;
patchRegistryDeps = ./patch-registry-deps;
@@ -115,7 +141,7 @@ stdenv.mkDerivation ((removeAttrs args ["depsExtraArgs"]) // {
[target."${rust.toRustTarget stdenv.buildPlatform}"]
"linker" = "${ccForBuild}"
${stdenv.lib.optionalString (stdenv.buildPlatform.config != stdenv.hostPlatform.config) ''
- [target."${rustTarget}"]
+ [target."${shortTarget}"]
"linker" = "${ccForHost}"
${# https://github.com/rust-lang/rust/issues/46651#issuecomment-433611633
stdenv.lib.optionalString (stdenv.hostPlatform.isMusl && stdenv.hostPlatform.isAarch64) ''
@@ -185,7 +211,7 @@ stdenv.mkDerivation ((removeAttrs args ["depsExtraArgs"]) // {
"CXX_${rust.toRustTarget stdenv.hostPlatform}"="${cxxForHost}" \
cargo build -j $NIX_BUILD_CORES \
${stdenv.lib.optionalString (buildType == "release") "--release"} \
- --target ${rustTarget} \
+ --target ${target} \
--frozen ${concatStringsSep " " cargoBuildFlags}
)
@@ -205,7 +231,7 @@ stdenv.mkDerivation ((removeAttrs args ["depsExtraArgs"]) // {
'';
checkPhase = args.checkPhase or (let
- argstr = "${stdenv.lib.optionalString (checkType == "release") "--release"} --target ${rustTarget} --frozen";
+ argstr = "${stdenv.lib.optionalString (checkType == "release") "--release"} --target ${target} --frozen";
threads = if cargoParallelTestThreads then "$NIX_BUILD_CORES" else "1";
in ''
${stdenv.lib.optionalString (buildAndTestSubdir != null) "pushd ${buildAndTestSubdir}"}
diff --git a/pkgs/build-support/rust/sysroot/Cargo.lock b/pkgs/build-support/rust/sysroot/Cargo.lock
new file mode 100644
index 000000000000..61fcef61744e
--- /dev/null
+++ b/pkgs/build-support/rust/sysroot/Cargo.lock
@@ -0,0 +1,29 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+[[package]]
+name = "alloc"
+version = "0.0.0"
+dependencies = [
+ "compiler_builtins",
+ "core",
+]
+
+[[package]]
+name = "compiler_builtins"
+version = "0.1.36"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7cd0782e0a7da7598164153173e5a5d4d9b1da094473c98dce0ff91406112369"
+dependencies = [
+ "rustc-std-workspace-core",
+]
+
+[[package]]
+name = "core"
+version = "0.0.0"
+
+[[package]]
+name = "rustc-std-workspace-core"
+version = "1.99.0"
+dependencies = [
+ "core",
+]
diff --git a/pkgs/build-support/rust/sysroot/cargo.py b/pkgs/build-support/rust/sysroot/cargo.py
new file mode 100644
index 000000000000..09f6fba6d1c8
--- /dev/null
+++ b/pkgs/build-support/rust/sysroot/cargo.py
@@ -0,0 +1,45 @@
+import os
+import toml
+
+rust_src = os.environ['RUSTC_SRC']
+orig_cargo = os.environ['ORIG_CARGO'] if 'ORIG_CARGO' in os.environ else None
+
+base = {
+ 'package': {
+ 'name': 'alloc',
+ 'version': '0.0.0',
+ 'authors': ['The Rust Project Developers'],
+ 'edition': '2018',
+ },
+ 'dependencies': {
+ 'compiler_builtins': {
+ 'version': '0.1.0',
+ 'features': ['rustc-dep-of-std', 'mem'],
+ },
+ 'core': {
+ 'path': os.path.join(rust_src, 'libcore'),
+ },
+ },
+ 'lib': {
+ 'name': 'alloc',
+ 'path': os.path.join(rust_src, 'liballoc/lib.rs'),
+ },
+ 'patch': {
+ 'crates-io': {
+ 'rustc-std-workspace-core': {
+ 'path': os.path.join(rust_src, 'tools/rustc-std-workspace-core'),
+ },
+ },
+ },
+}
+
+if orig_cargo is not None:
+ with open(orig_cargo, 'r') as f:
+ src = toml.loads(f.read())
+ if 'profile' in src:
+ base['profile'] = src['profile']
+
+out = toml.dumps(base)
+
+with open('Cargo.toml', 'x') as f:
+ f.write(out)
diff --git a/pkgs/build-support/rust/sysroot/default.nix b/pkgs/build-support/rust/sysroot/default.nix
new file mode 100644
index 000000000000..4db7cf0dc392
--- /dev/null
+++ b/pkgs/build-support/rust/sysroot/default.nix
@@ -0,0 +1,41 @@
+{ stdenv, rust, rustPlatform, buildPackages }:
+
+{ shortTarget, originalCargoToml, target, RUSTFLAGS }:
+
+let
+ cargoSrc = stdenv.mkDerivation {
+ name = "cargo-src";
+ preferLocalBuild = true;
+ phases = [ "installPhase" ];
+ installPhase = ''
+ RUSTC_SRC=${rustPlatform.rustcSrc.override { minimalContent = false; }} ORIG_CARGO=${originalCargoToml} \
+ ${buildPackages.python3.withPackages (ps: with ps; [ toml ])}/bin/python3 ${./cargo.py}
+ mkdir -p $out
+ cp Cargo.toml $out/Cargo.toml
+ cp ${./Cargo.lock} $out/Cargo.lock
+ '';
+ };
+in rustPlatform.buildRustPackage {
+ inherit target RUSTFLAGS;
+
+ name = "custom-sysroot";
+ src = cargoSrc;
+
+ RUSTC_BOOTSTRAP = 1;
+ __internal_dontAddSysroot = true;
+ cargoSha256 = "0y6dqfhsgk00y3fv5bnjzk0s7i30nwqc1rp0xlrk83hkh80x81mw";
+
+ doCheck = false;
+
+ installPhase = ''
+ export LIBS_DIR=$out/lib/rustlib/${shortTarget}/lib
+ mkdir -p $LIBS_DIR
+ for f in target/${shortTarget}/release/deps/*.{rlib,rmeta}; do
+ cp $f $LIBS_DIR
+ done
+
+ export RUST_SYSROOT=$(rustc --print=sysroot)
+ host=${rust.toRustTarget stdenv.buildPlatform}
+ cp -r $RUST_SYSROOT/lib/rustlib/$host $out
+ '';
+}
diff --git a/pkgs/build-support/rust/sysroot/update-lockfile.sh b/pkgs/build-support/rust/sysroot/update-lockfile.sh
new file mode 100755
index 000000000000..83d29832384f
--- /dev/null
+++ b/pkgs/build-support/rust/sysroot/update-lockfile.sh
@@ -0,0 +1,21 @@
+#!/usr/bin/env nix-shell
+#!nix-shell -i bash -p python3 python3.pkgs.toml cargo
+
+set -e
+
+HERE=$(dirname "${BASH_SOURCE[0]}")
+NIXPKGS_ROOT="$HERE/../../../.."
+
+# https://unix.stackexchange.com/a/84980/390173
+tempdir=$(mktemp -d 2>/dev/null || mktemp -d -t 'update-lockfile')
+
+cd "$tempdir"
+nix-build -E "with import (/. + \"${NIXPKGS_ROOT}\") {}; pkgs.rustPlatform.rustcSrc.override { minimalContent = false; }"
+RUSTC_SRC="$(pwd)/result" python3 "$HERE/cargo.py"
+RUSTC_BOOTSTRAP=1 cargo build || echo "Build failure is expected. All that's needed is the lockfile."
+
+cp Cargo.lock "$HERE"
+
+rm -rf "$tempdir"
+
+