Merge pull request #25976 from obsidiansystems/no-stdenv-is

Add `*Platform.is*` predicates and alias `stdenv.is*` to `hostPlatform`'s
This commit is contained in:
John Ericson 2017-05-22 13:30:20 -04:00 committed by GitHub
commit 0d88299019
13 changed files with 141 additions and 115 deletions

View File

@ -1,6 +1,9 @@
let inherit (import ../attrsets.nix) mapAttrs; in
rec { rec {
doubles = import ./doubles.nix; doubles = import ./doubles.nix;
parse = import ./parse.nix; parse = import ./parse.nix;
inspect = import ./inspect.nix;
platforms = import ./platforms.nix; platforms = import ./platforms.nix;
# Elaborate a `localSystem` or `crossSystem` so that it contains everything # Elaborate a `localSystem` or `crossSystem` so that it contains everything
@ -18,6 +21,13 @@ rec {
config = parse.tripleFromSystem final.parsed; config = parse.tripleFromSystem final.parsed;
# Just a guess, based on `system` # Just a guess, based on `system`
platform = platforms.selectBySystem final.system; platform = platforms.selectBySystem final.system;
} // args; libc =
/**/ if final.isDarwin then "libSystem"
else if final.isMinGW then "msvcrt"
else if final.isLinux then "glibc"
# TODO(@Ericson2314) think more about other operating systems
else "native/impure";
} // mapAttrs (n: v: v final.parsed) inspect.predicates
// args;
in final; in final;
} }

View File

@ -1,8 +1,9 @@
let lists = import ../lists.nix; in
let parse = import ./parse.nix; in
let inherit (import ../attrsets.nix) matchAttrs; in
let let
lists = import ../lists.nix;
parse = import ./parse.nix;
inherit (import ./inspect.nix) predicates;
inherit (import ../attrsets.nix) matchAttrs;
all = [ all = [
"aarch64-linux" "aarch64-linux"
"armv5tel-linux" "armv6l-linux" "armv7l-linux" "armv5tel-linux" "armv6l-linux" "armv7l-linux"
@ -25,20 +26,21 @@ in rec {
allBut = platforms: lists.filter (x: !(builtins.elem x platforms)) all; allBut = platforms: lists.filter (x: !(builtins.elem x platforms)) all;
none = []; none = [];
arm = filterDoubles (matchAttrs { cpu = { family = "arm"; bits = 32; }; }); arm = filterDoubles predicates.isArm32;
i686 = filterDoubles parse.isi686; i686 = filterDoubles predicates.isi686;
mips = filterDoubles (matchAttrs { cpu = { family = "mips"; }; }); mips = filterDoubles predicates.isMips;
x86_64 = filterDoubles parse.isx86_64; x86_64 = filterDoubles predicates.isx86_64;
cygwin = filterDoubles parse.isCygwin; cygwin = filterDoubles predicates.isCygwin;
darwin = filterDoubles parse.isDarwin; darwin = filterDoubles predicates.isDarwin;
freebsd = filterDoubles (matchAttrs { kernel = parse.kernels.freebsd; }); freebsd = filterDoubles predicates.isFreeBSD;
gnu = filterDoubles (matchAttrs { kernel = parse.kernels.linux; abi = parse.abis.gnu; }); # Should be better # Should be better, but MinGW is unclear, and HURD is bit-rotted.
illumos = filterDoubles (matchAttrs { kernel = parse.kernels.solaris; }); gnu = filterDoubles (matchAttrs { kernel = parse.kernels.linux; abi = parse.abis.gnu; });
linux = filterDoubles parse.isLinux; illumos = filterDoubles predicates.isSunOS;
netbsd = filterDoubles (matchAttrs { kernel = parse.kernels.netbsd; }); linux = filterDoubles predicates.isLinux;
openbsd = filterDoubles (matchAttrs { kernel = parse.kernels.openbsd; }); netbsd = filterDoubles predicates.isNetBSD;
unix = filterDoubles parse.isUnix; openbsd = filterDoubles predicates.isOpenBSD;
unix = filterDoubles predicates.isUnix;
mesaPlatforms = ["i686-linux" "x86_64-linux" "x86_64-darwin" "armv5tel-linux" "armv6l-linux" "armv7l-linux" "aarch64-linux"]; mesaPlatforms = ["i686-linux" "x86_64-linux" "x86_64-darwin" "armv5tel-linux" "armv6l-linux" "armv7l-linux" "aarch64-linux"];
} }

36
lib/systems/inspect.nix Normal file
View File

@ -0,0 +1,36 @@
with import ./parse.nix;
with import ../attrsets.nix;
rec {
patterns = {
"32bit" = { cpu = { bits = 32; }; };
"64bit" = { cpu = { bits = 64; }; };
i686 = { cpu = cpuTypes.i686; };
x86_64 = { cpu = cpuTypes.x86_64; };
Arm = { cpu = { family = "arm"; }; };
Mips = { cpu = { family = "mips"; }; };
BigEndian = { cpu = { significantByte = significantBytes.bigEndian; }; };
LittleEndian = { cpu = { significantByte = significantBytes.littleEndian; }; };
Unix = { kernel = { families = { inherit (kernelFamilies) unix; }; }; };
BSD = { kernel = { families = { inherit (kernelFamilies) bsd; }; }; };
Darwin = { kernel = kernels.darwin; };
Linux = { kernel = kernels.linux; };
SunOS = { kernel = kernels.solaris; };
FreeBSD = { kernel = kernels.freebsd; };
NetBSD = { kernel = kernels.netbsd; };
OpenBSD = { kernel = kernels.openbsd; };
Windows = { kernel = kernels.windows; };
Cygwin = { kernel = kernels.windows; abi = abis.cygnus; };
MinGW = { kernel = kernels.windows; abi = abis.gnu; };
Arm32 = recursiveUpdate patterns.Arm patterns."32bit";
Arm64 = recursiveUpdate patterns.Arm patterns."64bit";
};
predicates = mapAttrs'
(name: value: nameValuePair ("is" + name) (matchAttrs value))
patterns;
}

View File

@ -8,6 +8,7 @@
with import ../lists.nix; with import ../lists.nix;
with import ../types.nix; with import ../types.nix;
with import ../attrsets.nix; with import ../attrsets.nix;
with (import ./inspect.nix).predicates;
let let
lib = import ../default.nix; lib = import ../default.nix;
@ -109,19 +110,6 @@ rec {
inherit cpu vendor kernel abi; inherit cpu vendor kernel abi;
}; };
is64Bit = matchAttrs { cpu = { bits = 64; }; };
is32Bit = matchAttrs { cpu = { bits = 32; }; };
isi686 = matchAttrs { cpu = cpuTypes.i686; };
isx86_64 = matchAttrs { cpu = cpuTypes.x86_64; };
isDarwin = matchAttrs { kernel = kernels.darwin; };
isLinux = matchAttrs { kernel = kernels.linux; };
isUnix = matchAttrs { kernel = { families = { inherit (kernelFamilies) unix; }; }; };
isWindows = matchAttrs { kernel = kernels.windows; };
isCygwin = matchAttrs { kernel = kernels.windows; abi = abis.cygnus; };
isMinGW = matchAttrs { kernel = kernels.windows; abi = abis.gnu; };
mkSkeletonFromList = l: { mkSkeletonFromList = l: {
"2" = # We only do 2-part hacks for things Nix already supports "2" = # We only do 2-part hacks for things Nix already supports
if elemAt l 1 == "cygwin" if elemAt l 1 == "cygwin"
@ -153,22 +141,22 @@ rec {
getKernel = name: kernels.${name} or (throw "Unknown kernel: ${name}"); getKernel = name: kernels.${name} or (throw "Unknown kernel: ${name}");
getAbi = name: abis.${name} or (throw "Unknown ABI: ${name}"); getAbi = name: abis.${name} or (throw "Unknown ABI: ${name}");
system = rec { parsed = rec {
cpu = getCpu args.cpu; cpu = getCpu args.cpu;
vendor = vendor =
/**/ if args ? vendor then getVendor args.vendor /**/ if args ? vendor then getVendor args.vendor
else if isDarwin system then vendors.apple else if isDarwin parsed then vendors.apple
else if isWindows system then vendors.pc else if isWindows parsed then vendors.pc
else vendors.unknown; else vendors.unknown;
kernel = getKernel args.kernel; kernel = getKernel args.kernel;
abi = abi =
/**/ if args ? abi then getAbi args.abi /**/ if args ? abi then getAbi args.abi
else if isLinux system then abis.gnu else if isLinux parsed then abis.gnu
else if isWindows system then abis.gnu else if isWindows parsed then abis.gnu
else abis.unknown; else abis.unknown;
}; };
in mkSystem system; in mkSystem parsed;
mkSystemFromString = s: mkSystemFromSkeleton (mkSkeletonFromList (lib.splitString "-" s)); mkSystemFromString = s: mkSystemFromSkeleton (mkSkeletonFromList (lib.splitString "-" s));

View File

@ -1,4 +1,4 @@
{ pkgs ? import ((import ../../lib).cleanSource ../..) {} }: { pkgs ? import ((import ../.).cleanSource ../..) {} }:
pkgs.stdenv.mkDerivation { pkgs.stdenv.mkDerivation {
name = "nixpkgs-lib-tests"; name = "nixpkgs-lib-tests";

View File

@ -5,17 +5,15 @@
}: }:
let let
inherit (stdenv.lib.systems.parse) isDarwin;
prefix = stdenv.lib.optionalString prefix = stdenv.lib.optionalString
(targetPlatform != hostPlatform) (targetPlatform != hostPlatform)
"${targetPlatform.config}-"; "${targetPlatform.config}-";
in in
assert isDarwin targetPlatform.parsed; assert targetPlatform.isDarwin;
# Non-Darwin alternatives # Non-Darwin alternatives
assert (!isDarwin hostPlatform.parsed) -> (maloader != null && xctoolchain != null); assert (!hostPlatform.isDarwin) -> (maloader != null && xctoolchain != null);
let let
baseParams = rec { baseParams = rec {
@ -91,7 +89,7 @@ let
''; '';
postInstall = postInstall =
if isDarwin hostPlatform.parsed if hostPlatform.isDarwin
then '' then ''
cat >$out/bin/dsymutil << EOF cat >$out/bin/dsymutil << EOF
#!${stdenv.shell} #!${stdenv.shell}

View File

@ -61,7 +61,7 @@ in rec {
allowedRequisites ? null}: allowedRequisites ? null}:
let let
thisStdenv = import ../generic { thisStdenv = import ../generic {
inherit system config shell extraBuildInputs allowedRequisites; inherit config shell extraBuildInputs allowedRequisites;
name = "stdenv-darwin-boot-${toString step}"; name = "stdenv-darwin-boot-${toString step}";
@ -87,6 +87,10 @@ in rec {
${extraPreHook} ${extraPreHook}
''; '';
initialPath = [ bootstrapTools ]; initialPath = [ bootstrapTools ];
hostPlatform = localSystem;
targetPlatform = localSystem;
fetchurlBoot = import ../../build-support/fetchurl { fetchurlBoot = import ../../build-support/fetchurl {
stdenv = stage0.stdenv; stdenv = stage0.stdenv;
curl = bootstrapTools; curl = bootstrapTools;
@ -268,7 +272,7 @@ in rec {
}; };
stdenvDarwin = prevStage: let pkgs = prevStage; in import ../generic rec { stdenvDarwin = prevStage: let pkgs = prevStage; in import ../generic rec {
inherit system config; inherit config;
inherit (pkgs.stdenv) fetchurlBoot; inherit (pkgs.stdenv) fetchurlBoot;
name = "stdenv-darwin"; name = "stdenv-darwin";
@ -280,6 +284,9 @@ in rec {
stdenvSandboxProfile = binShClosure + libSystemProfile; stdenvSandboxProfile = binShClosure + libSystemProfile;
extraSandboxProfile = binShClosure + libSystemProfile; extraSandboxProfile = binShClosure + libSystemProfile;
hostPlatform = localSystem;
targetPlatform = localSystem;
initialPath = import ../common-path.nix { inherit pkgs; }; initialPath = import ../common-path.nix { inherit pkgs; };
shell = "${pkgs.bash}/bin/bash"; shell = "${pkgs.bash}/bin/bash";

View File

@ -35,8 +35,10 @@ let inherit (localSystem) system; in
stdenv = import ../generic { stdenv = import ../generic {
name = "stdenv-freebsd-boot-1"; name = "stdenv-freebsd-boot-1";
inherit system config; inherit config;
initialPath = [ "/" "/usr" ]; initialPath = [ "/" "/usr" ];
hostPlatform = localSystem;
targetPlatform = localSystem;
shell = "${bootstrapTools}/bin/bash"; shell = "${bootstrapTools}/bin/bash";
fetchurlBoot = null; fetchurlBoot = null;
cc = null; cc = null;
@ -50,9 +52,10 @@ let inherit (localSystem) system; in
stdenv = import ../generic { stdenv = import ../generic {
name = "stdenv-freebsd-boot-0"; name = "stdenv-freebsd-boot-0";
inherit system config; inherit config;
initialPath = [ prevStage.bootstrapTools ]; initialPath = [ prevStage.bootstrapTools ];
inherit (prevStage.stdenv) shell; inherit (prevStage.stdenv)
hostPlatform targetPlatform shell;
fetchurlBoot = prevStage.fetchurl; fetchurlBoot = prevStage.fetchurl;
cc = null; cc = null;
}; };
@ -65,10 +68,10 @@ let inherit (localSystem) system; in
inherit config overlays; inherit config overlays;
stdenv = import ../generic { stdenv = import ../generic {
name = "stdenv-freebsd-boot-3"; name = "stdenv-freebsd-boot-3";
inherit system config; inherit config;
inherit (prevStage.stdenv) inherit (prevStage.stdenv)
initialPath shell fetchurlBoot; hostPlatform targetPlatform initialPath shell fetchurlBoot;
cc = import ../../build-support/cc-wrapper { cc = import ../../build-support/cc-wrapper {
nativeTools = true; nativeTools = true;

View File

@ -1,6 +1,6 @@
let lib = import ../../../lib; in lib.makeOverridable ( let lib = import ../../../lib; in lib.makeOverridable (
{ system, name ? "stdenv", preHook ? "", initialPath, cc, shell { name ? "stdenv", preHook ? "", initialPath, cc, shell
, allowedRequisites ? null, extraAttrs ? {}, overrides ? (self: super: {}), config , allowedRequisites ? null, extraAttrs ? {}, overrides ? (self: super: {}), config
, # The `fetchurl' to use for downloading curl and its dependencies , # The `fetchurl' to use for downloading curl and its dependencies
@ -14,9 +14,18 @@ let lib = import ../../../lib; in lib.makeOverridable (
, __extraImpureHostDeps ? [] , __extraImpureHostDeps ? []
, stdenvSandboxProfile ? "" , stdenvSandboxProfile ? ""
, extraSandboxProfile ? "" , extraSandboxProfile ? ""
, # The platforms here do *not* correspond to the stage the stdenv is
# used in, but rather the previous one, in which it was built. We
# use the latter two platforms, like a cross compiler, because the
# stand environment is a build tool if you squint at it, and because
# neither of these are used when building stdenv so we know the
# build platform is irrelevant.
hostPlatform, targetPlatform
}: }:
let let
inherit (targetPlatform) system;
# See discussion at https://github.com/NixOS/nixpkgs/pull/25304#issuecomment-298385426 # See discussion at https://github.com/NixOS/nixpkgs/pull/25304#issuecomment-298385426
# for why this defaults to false, but I (@copumpkin) want to default it to true soon. # for why this defaults to false, but I (@copumpkin) want to default it to true soon.
@ -265,7 +274,7 @@ let
outputs' = outputs' =
outputs ++ outputs ++
(if separateDebugInfo then assert result.isLinux; [ "debug" ] else []); (if separateDebugInfo then assert targetPlatform.isLinux; [ "debug" ] else []);
buildInputs' = lib.chooseDevOutputs buildInputs ++ buildInputs' = lib.chooseDevOutputs buildInputs ++
(if separateDebugInfo then [ ../../build-support/setup-hooks/separate-debug-info.sh ] else []); (if separateDebugInfo then [ ../../build-support/setup-hooks/separate-debug-info.sh ] else []);
@ -311,7 +320,7 @@ let
# Inputs built by the usual native compiler. # Inputs built by the usual native compiler.
nativeBuildInputs = nativeBuildInputs' nativeBuildInputs = nativeBuildInputs'
++ lib.optional ++ lib.optional
(result.isCygwin (hostPlatform.isCygwin
|| (crossConfig != null && lib.hasSuffix "mingw32" crossConfig)) || (crossConfig != null && lib.hasSuffix "mingw32" crossConfig))
../../build-support/setup-hooks/win-dll-link.sh ../../build-support/setup-hooks/win-dll-link.sh
; ;
@ -392,54 +401,14 @@ let
}; };
# Utility flags to test the type of platform. # Utility flags to test the type of platform.
isDarwin = system == "x86_64-darwin"; inherit (hostPlatform)
isLinux = system == "i686-linux" isDarwin isLinux isSunOS isCygwin isFreeBSD isOpenBSD isi686 isx86_64
|| system == "x86_64-linux" is64bit isMips isBigEndian;
|| system == "powerpc-linux" isArm = hostPlatform.isArm32;
|| system == "armv5tel-linux" isAarch64 = hostPlatform.isArm64;
|| system == "armv6l-linux" # Other code instead checks for anything using GNU userland,
|| system == "armv7l-linux" # e.g. GNU/linux. This refers just to GNU Hurd.
|| system == "aarch64-linux" isGNU = system == "i686-gnu";
|| system == "mips64el-linux";
isGNU = system == "i686-gnu"; # GNU/Hurd
isGlibc = isGNU # useful for `stdenvNative'
|| isLinux
|| system == "x86_64-kfreebsd-gnu";
isSunOS = system == "i686-solaris"
|| system == "x86_64-solaris";
isCygwin = system == "i686-cygwin"
|| system == "x86_64-cygwin";
isFreeBSD = system == "i686-freebsd"
|| system == "x86_64-freebsd";
isOpenBSD = system == "i686-openbsd"
|| system == "x86_64-openbsd";
isi686 = system == "i686-linux"
|| system == "i686-gnu"
|| system == "i686-freebsd"
|| system == "i686-openbsd"
|| system == "i686-cygwin"
|| system == "i386-sunos";
isx86_64 = system == "x86_64-linux"
|| system == "x86_64-darwin"
|| system == "x86_64-freebsd"
|| system == "x86_64-openbsd"
|| system == "x86_64-cygwin"
|| system == "x86_64-solaris";
is64bit = system == "x86_64-linux"
|| system == "x86_64-darwin"
|| system == "x86_64-freebsd"
|| system == "x86_64-openbsd"
|| system == "x86_64-cygwin"
|| system == "x86_64-solaris"
|| system == "aarch64-linux"
|| system == "mips64el-linux";
isMips = system == "mips-linux"
|| system == "mips64el-linux";
isArm = system == "armv5tel-linux"
|| system == "armv6l-linux"
|| system == "armv7l-linux";
isAarch64 = system == "aarch64-linux";
isBigEndian = system == "powerpc-linux";
# Whether we should run paxctl to pax-mark binaries. # Whether we should run paxctl to pax-mark binaries.
needsPax = isLinux; needsPax = isLinux;

View File

@ -52,7 +52,7 @@ let
let let
thisStdenv = import ../generic { thisStdenv = import ../generic {
inherit system config extraBuildInputs; inherit config extraBuildInputs;
name = "stdenv-linux-boot"; name = "stdenv-linux-boot";
preHook = preHook =
'' ''
@ -64,6 +64,9 @@ let
shell = "${bootstrapTools}/bin/bash"; shell = "${bootstrapTools}/bin/bash";
initialPath = [bootstrapTools]; initialPath = [bootstrapTools];
hostPlatform = localSystem;
targetPlatform = localSystem;
fetchurlBoot = import ../../build-support/fetchurl/boot.nix { fetchurlBoot = import ../../build-support/fetchurl/boot.nix {
inherit system; inherit system;
}; };
@ -261,7 +264,7 @@ in
targetPlatform = localSystem; targetPlatform = localSystem;
inherit config overlays; inherit config overlays;
stdenv = import ../generic rec { stdenv = import ../generic rec {
inherit system config; inherit config;
preHook = '' preHook = ''
# Make "strip" produce deterministic output, by setting # Make "strip" produce deterministic output, by setting
@ -273,6 +276,9 @@ in
initialPath = initialPath =
((import ../common-path.nix) {pkgs = prevStage;}); ((import ../common-path.nix) {pkgs = prevStage;});
hostPlatform = localSystem;
targetPlatform = localSystem;
extraBuildInputs = [ prevStage.patchelf prevStage.paxctl ] ++ extraBuildInputs = [ prevStage.patchelf prevStage.paxctl ] ++
# Many tarballs come with obsolete config.sub/config.guess that don't recognize aarch64. # Many tarballs come with obsolete config.sub/config.guess that don't recognize aarch64.
lib.optional (system == "aarch64-linux") prevStage.updateAutotoolsGnuConfigScriptsHook; lib.optional (system == "aarch64-linux") prevStage.updateAutotoolsGnuConfigScriptsHook;

View File

@ -81,6 +81,9 @@ let
{ cc, fetchurl, extraPath ? [], overrides ? (self: super: { }) }: { cc, fetchurl, extraPath ? [], overrides ? (self: super: { }) }:
import ../generic { import ../generic {
hostPlatform = localSystem;
targetPlatform = localSystem;
preHook = preHook =
if system == "i686-freebsd" then prehookFreeBSD else if system == "i686-freebsd" then prehookFreeBSD else
if system == "x86_64-freebsd" then prehookFreeBSD else if system == "x86_64-freebsd" then prehookFreeBSD else

View File

@ -24,11 +24,11 @@ bootStages ++ [
initialPath = (import ../common-path.nix) { pkgs = prevStage; }; initialPath = (import ../common-path.nix) { pkgs = prevStage; };
system = stdenv.system; inherit (prevStage.stdenv) hostPlatform targetPlatform;
cc = import ../../build-support/cc-wrapper { cc = import ../../build-support/cc-wrapper {
nativeTools = false; nativeTools = false;
nativePrefix = stdenv.lib.optionalString stdenv.isSunOS "/usr"; nativePrefix = stdenv.lib.optionalString hostPlatform.isSunOS "/usr";
nativeLibc = true; nativeLibc = true;
inherit stdenv; inherit stdenv;
inherit (prevStage) binutils coreutils gnugrep; inherit (prevStage) binutils coreutils gnugrep;

View File

@ -6461,7 +6461,7 @@ with pkgs;
bin_replace_string = callPackage ../development/tools/misc/bin_replace_string { }; bin_replace_string = callPackage ../development/tools/misc/bin_replace_string { };
binutils = binutils =
if lib.systems.parse.isDarwin targetPlatform.parsed if targetPlatform.isDarwin
then darwin.binutils then darwin.binutils
else binutils-raw; else binutils-raw;
@ -8719,15 +8719,19 @@ with pkgs;
libgsf = callPackage ../development/libraries/libgsf { }; libgsf = callPackage ../development/libraries/libgsf { };
# glibc provides libiconv so systems with glibc don't need to build libiconv # GNU libc provides libiconv so systems with glibc don't need to build
# separately, but we also provide libiconvReal, which will always be a # libiconv separately. Additionally, Apple forked/repackaged libiconv so we
# standalone libiconv, just in case you want it # use that instead of the vanilla version on that OS.
libiconv = if stdenv ? cross then #
(if stdenv.cross.libc == "glibc" then libcCross # We also provide `libiconvReal`, which will always be a standalone libiconv,
else if stdenv.cross.libc == "libSystem" then darwin.libiconv # just in case you want it regardless of platform.
else libiconvReal) libiconv =
else if stdenv.isGlibc then glibcIconv stdenv.cc.libc if hostPlatform.libc == "glibc"
else if stdenv.isDarwin then darwin.libiconv then glibcIconv (if hostPlatform != buildPlatform
then libcCross
else stdenv.cc.libc)
else if hostPlatform.isDarwin
then darwin.libiconv
else libiconvReal; else libiconvReal;
glibcIconv = libc: let glibcIconv = libc: let