From 67aa63e5e2fe43f907b4bc3048727f19748af90c Mon Sep 17 00:00:00 2001 From: John Ericson Date: Mon, 1 Nov 2021 21:48:55 -0400 Subject: [PATCH] build-support/rust: Organize - `toRustTarget` and friends pulled out from rust tools into rust library. Since they don't depend on any packages they can be more widely useable. - `build-rust-package` gets its own directory - `fetch-cargo-tarball` gets its own directory (cherry picked from commit 18ed048c7b27e288a6c9ba894790a7e67ed5080d) --- .../rust/{ => build-rust-package}/default.nix | 0 .../patch-registry-deps/pkg-config | 0 .../sysroot/Cargo.lock | 0 .../{ => build-rust-package}/sysroot/cargo.py | 0 .../sysroot/default.nix | 0 .../sysroot/update-lockfile.sh | 0 .../cargo-vendor-normalise.py | 0 .../default.nix} | 4 +- pkgs/build-support/rust/lib/default.nix | 37 ++++++++++++++++++ pkgs/development/compilers/rust/default.nix | 39 +++++-------------- .../compilers/rust/make-rust-platform.nix | 4 +- 11 files changed, 50 insertions(+), 34 deletions(-) rename pkgs/build-support/rust/{ => build-rust-package}/default.nix (100%) rename pkgs/build-support/rust/{ => build-rust-package}/patch-registry-deps/pkg-config (100%) rename pkgs/build-support/rust/{ => build-rust-package}/sysroot/Cargo.lock (100%) rename pkgs/build-support/rust/{ => build-rust-package}/sysroot/cargo.py (100%) rename pkgs/build-support/rust/{ => build-rust-package}/sysroot/default.nix (100%) rename pkgs/build-support/rust/{ => build-rust-package}/sysroot/update-lockfile.sh (100%) rename pkgs/build-support/rust/{ => fetch-cargo-tarball}/cargo-vendor-normalise.py (100%) rename pkgs/build-support/rust/{fetchCargoTarball.nix => fetch-cargo-tarball/default.nix} (95%) create mode 100644 pkgs/build-support/rust/lib/default.nix diff --git a/pkgs/build-support/rust/default.nix b/pkgs/build-support/rust/build-rust-package/default.nix similarity index 100% rename from pkgs/build-support/rust/default.nix rename to pkgs/build-support/rust/build-rust-package/default.nix diff --git a/pkgs/build-support/rust/patch-registry-deps/pkg-config b/pkgs/build-support/rust/build-rust-package/patch-registry-deps/pkg-config similarity index 100% rename from pkgs/build-support/rust/patch-registry-deps/pkg-config rename to pkgs/build-support/rust/build-rust-package/patch-registry-deps/pkg-config diff --git a/pkgs/build-support/rust/sysroot/Cargo.lock b/pkgs/build-support/rust/build-rust-package/sysroot/Cargo.lock similarity index 100% rename from pkgs/build-support/rust/sysroot/Cargo.lock rename to pkgs/build-support/rust/build-rust-package/sysroot/Cargo.lock diff --git a/pkgs/build-support/rust/sysroot/cargo.py b/pkgs/build-support/rust/build-rust-package/sysroot/cargo.py similarity index 100% rename from pkgs/build-support/rust/sysroot/cargo.py rename to pkgs/build-support/rust/build-rust-package/sysroot/cargo.py diff --git a/pkgs/build-support/rust/sysroot/default.nix b/pkgs/build-support/rust/build-rust-package/sysroot/default.nix similarity index 100% rename from pkgs/build-support/rust/sysroot/default.nix rename to pkgs/build-support/rust/build-rust-package/sysroot/default.nix diff --git a/pkgs/build-support/rust/sysroot/update-lockfile.sh b/pkgs/build-support/rust/build-rust-package/sysroot/update-lockfile.sh similarity index 100% rename from pkgs/build-support/rust/sysroot/update-lockfile.sh rename to pkgs/build-support/rust/build-rust-package/sysroot/update-lockfile.sh diff --git a/pkgs/build-support/rust/cargo-vendor-normalise.py b/pkgs/build-support/rust/fetch-cargo-tarball/cargo-vendor-normalise.py similarity index 100% rename from pkgs/build-support/rust/cargo-vendor-normalise.py rename to pkgs/build-support/rust/fetch-cargo-tarball/cargo-vendor-normalise.py diff --git a/pkgs/build-support/rust/fetchCargoTarball.nix b/pkgs/build-support/rust/fetch-cargo-tarball/default.nix similarity index 95% rename from pkgs/build-support/rust/fetchCargoTarball.nix rename to pkgs/build-support/rust/fetch-cargo-tarball/default.nix index 3b36554e707..78b1f70fa2a 100644 --- a/pkgs/build-support/rust/fetchCargoTarball.nix +++ b/pkgs/build-support/rust/fetch-cargo-tarball/default.nix @@ -9,8 +9,8 @@ let cargo-vendor-normalise = stdenv.mkDerivation { postFixup = "wrapPythonPrograms"; doInstallCheck = true; installCheckPhase = '' - # check that ./fetchcargo-default-config.toml is a fix point - reference=${./fetchcargo-default-config.toml} + # check that ../fetchcargo-default-config.toml is a fix point + reference=${../fetchcargo-default-config.toml} < $reference $out/bin/cargo-vendor-normalise > test; cmp test $reference ''; diff --git a/pkgs/build-support/rust/lib/default.nix b/pkgs/build-support/rust/lib/default.nix new file mode 100644 index 00000000000..24adcf2cb4e --- /dev/null +++ b/pkgs/build-support/rust/lib/default.nix @@ -0,0 +1,37 @@ +{ lib }: + +rec { + # https://doc.rust-lang.org/reference/conditional-compilation.html#target_arch + toTargetArch = platform: + if platform.isAarch32 then "arm" + else platform.parsed.cpu.name; + + # https://doc.rust-lang.org/reference/conditional-compilation.html#target_os + toTargetOs = platform: + if platform.isDarwin then "macos" + else platform.parsed.kernel.name; + + # Returns the name of the rust target, even if it is custom. Adjustments are + # because rust has slightly different naming conventions than we do. + toRustTarget = platform: let + inherit (platform.parsed) cpu vendor kernel abi; + cpu_ = platform.rustc.platform.arch or { + "armv7a" = "armv7"; + "armv7l" = "armv7"; + "armv6l" = "arm"; + "armv5tel" = "armv5te"; + "riscv64" = "riscv64gc"; + }.${cpu.name} or cpu.name; + vendor_ = platform.rustc.platform.vendor or { + "w64" = "pc"; + }.${vendor.name} or vendor.name; + in platform.rustc.config + or "${cpu_}-${vendor_}-${kernel.name}${lib.optionalString (abi.name != "unknown") "-${abi.name}"}"; + + # Returns the name of the rust target if it is standard, or the json file + # containing the custom target spec. + toRustTargetSpec = platform: + if (platform.rustc or {}) ? platform + then builtins.toFile (toRustTarget platform + ".json") (builtins.toJSON platform.rustc.platform) + else toRustTarget platform; +} diff --git a/pkgs/development/compilers/rust/default.nix b/pkgs/development/compilers/rust/default.nix index b48791f11b7..819513e25e8 100644 --- a/pkgs/development/compilers/rust/default.nix +++ b/pkgs/development/compilers/rust/default.nix @@ -17,38 +17,17 @@ , CoreFoundation, Security , pkgsBuildTarget, pkgsBuildBuild , makeRustPlatform -}: rec { - # https://doc.rust-lang.org/reference/conditional-compilation.html#target_arch - toTargetArch = platform: - if platform.isAarch32 then "arm" - else platform.parsed.cpu.name; +}: - # https://doc.rust-lang.org/reference/conditional-compilation.html#target_os - toTargetOs = platform: - if platform.isDarwin then "macos" - else platform.parsed.kernel.name; +let + # Use `import` to make sure no packages sneak in here. + lib' = import ../../../build-support/rust/lib { inherit lib; }; +in +{ + lib = lib'; - # Returns the name of the rust target, even if it is custom. Adjustments are - # because rust has slightly different naming conventions than we do. - toRustTarget = platform: with platform.parsed; let - cpu_ = platform.rustc.platform.arch or { - "armv7a" = "armv7"; - "armv7l" = "armv7"; - "armv6l" = "arm"; - "armv5tel" = "armv5te"; - }.${cpu.name} or cpu.name; - vendor_ = platform.rustc.platform.vendor or { - "w64" = "pc"; - }.${vendor.name} or vendor.name; - in platform.rustc.config - or "${cpu_}-${vendor_}-${kernel.name}${lib.optionalString (abi.name != "unknown") "-${abi.name}"}"; - - # Returns the name of the rust target if it is standard, or the json file - # containing the custom target spec. - toRustTargetSpec = platform: - if (platform.rustc or {}) ? platform - then builtins.toFile (toRustTarget platform + ".json") (builtins.toJSON platform.rustc.platform) - else toRustTarget platform; + # Backwards compat before `lib` was factored out. + inherit (lib') toTargetArch toTargetOs toRustTarget toRustTargetSpec; # This just contains tools for now. But it would conceivably contain # libraries too, say if we picked some default/recommended versions from diff --git a/pkgs/development/compilers/rust/make-rust-platform.nix b/pkgs/development/compilers/rust/make-rust-platform.nix index 584b1fdbe43..158641290d5 100644 --- a/pkgs/development/compilers/rust/make-rust-platform.nix +++ b/pkgs/development/compilers/rust/make-rust-platform.nix @@ -7,11 +7,11 @@ rec { inherit rustc cargo; }; - fetchCargoTarball = buildPackages.callPackage ../../../build-support/rust/fetchCargoTarball.nix { + fetchCargoTarball = buildPackages.callPackage ../../../build-support/rust/fetch-cargo-tarball { inherit cargo; }; - buildRustPackage = callPackage ../../../build-support/rust { + buildRustPackage = callPackage ../../../build-support/rust/build-rust-package { inherit cargoBuildHook cargoCheckHook cargoInstallHook cargoSetupHook fetchCargoTarball rustc; };