2017-08-28 18:56:08 +00:00
|
|
|
# The Nixpkgs CC is not directly usable, since it doesn't know where
|
|
|
|
# the C library and standard header files are. Therefore the compiler
|
|
|
|
# produced by that package cannot be installed directly in a user
|
|
|
|
# environment and used from the command line. So we use a wrapper
|
|
|
|
# script that sets up the right environment variables so that the
|
|
|
|
# compiler and the linker just "work".
|
|
|
|
|
2018-02-01 00:00:00 +00:00
|
|
|
{ name ? ""
|
2018-08-09 10:57:38 +00:00
|
|
|
, stdenvNoCC, nativeTools, propagateDoc ? !nativeTools, noLibc ? false, nativeLibc, nativePrefix ? ""
|
|
|
|
, bintools ? null, libc ? null
|
|
|
|
, coreutils ? null, shell ? stdenvNoCC.shell, gnugrep ? null
|
2017-08-28 18:56:08 +00:00
|
|
|
, extraPackages ? [], extraBuildCommands ? ""
|
|
|
|
, buildPackages ? {}
|
|
|
|
, useMacosReexportHack ? false
|
|
|
|
}:
|
|
|
|
|
|
|
|
with stdenvNoCC.lib;
|
|
|
|
|
2018-02-01 00:00:00 +00:00
|
|
|
assert nativeTools -> !propagateDoc && nativePrefix != "";
|
2017-08-28 18:56:08 +00:00
|
|
|
assert !nativeTools ->
|
|
|
|
bintools != null && coreutils != null && gnugrep != null;
|
|
|
|
assert !(nativeLibc && noLibc);
|
|
|
|
assert (noLibc || nativeLibc) == (libc == null);
|
|
|
|
|
|
|
|
let
|
|
|
|
stdenv = stdenvNoCC;
|
|
|
|
inherit (stdenv) hostPlatform targetPlatform;
|
|
|
|
|
|
|
|
# Prefix for binaries. Customarily ends with a dash separator.
|
|
|
|
#
|
|
|
|
# TODO(@Ericson2314) Make unconditional, or optional but always true by
|
|
|
|
# default.
|
|
|
|
targetPrefix = stdenv.lib.optionalString (targetPlatform != hostPlatform)
|
|
|
|
(targetPlatform.config + "-");
|
|
|
|
|
|
|
|
bintoolsVersion = (builtins.parseDrvName bintools.name).version;
|
|
|
|
bintoolsName = (builtins.parseDrvName bintools.name).name;
|
|
|
|
|
|
|
|
libc_bin = if libc == null then null else getBin libc;
|
|
|
|
libc_dev = if libc == null then null else getDev libc;
|
|
|
|
libc_lib = if libc == null then null else getLib libc;
|
|
|
|
bintools_bin = if nativeTools then "" else getBin bintools;
|
|
|
|
# The wrapper scripts use 'cat' and 'grep', so we may need coreutils.
|
|
|
|
coreutils_bin = if nativeTools then "" else getBin coreutils;
|
|
|
|
|
|
|
|
# See description in cc-wrapper.
|
2018-07-27 00:16:28 +00:00
|
|
|
infixSalt = replaceStrings ["-" "."] ["_" "_"] targetPlatform.config;
|
2017-08-28 18:56:08 +00:00
|
|
|
|
|
|
|
# The dynamic linker has different names on different platforms. This is a
|
|
|
|
# shell glob that ought to match it.
|
|
|
|
dynamicLinker =
|
|
|
|
/**/ if libc == null then null
|
2018-01-05 18:36:58 +00:00
|
|
|
else if targetPlatform.libc == "musl" then "${libc_lib}/lib/ld-musl-*"
|
2018-03-28 01:24:27 +00:00
|
|
|
else if targetPlatform.libc == "bionic" then "/system/bin/linker"
|
2017-08-28 18:56:08 +00:00
|
|
|
else if targetPlatform.system == "i686-linux" then "${libc_lib}/lib/ld-linux.so.2"
|
|
|
|
else if targetPlatform.system == "x86_64-linux" then "${libc_lib}/lib/ld-linux-x86-64.so.2"
|
|
|
|
# ARM with a wildcard, which can be "" or "-armhf".
|
treewide: isArm -> isAarch32
Following legacy packing conventions, `isArm` was defined just for
32-bit ARM instruction set. This is confusing to non packagers though,
because Aarch64 is an ARM instruction set.
The official ARM overview for ARMv8[1] is surprisingly not confusing,
given the overall state of affairs for ARM naming conventions, and
offers us a solution. It divides the nomenclature into three levels:
```
ISA: ARMv8 {-A, -R, -M}
/ \
Mode: Aarch32 Aarch64
| / \
Encoding: A64 A32 T32
```
At the top is the overall v8 instruction set archicture. Second are the
two modes, defined by bitwidth but differing in other semantics too, and
buttom are the encodings, (hopefully?) isomorphic if they encode the
same mode.
The 32 bit encodings are mostly backwards compatible with previous
non-Thumb and Thumb encodings, and if so we can pun the mode names to
instead mean "sets of compatable or isomorphic encodings", and then
voilà we have nice names for 32-bit and 64-bit arm instruction sets
which do not use the word ARM so as to not confused either laymen or
experienced ARM packages.
[1]: https://developer.arm.com/products/architecture/a-profile
(cherry picked from commit ba52ae50488de85a9cf60a3a04f1c9ca7122ec74)
2018-03-20 02:41:06 +00:00
|
|
|
else if (with targetPlatform; isAarch32 && isLinux) then "${libc_lib}/lib/ld-linux*.so.3"
|
2017-08-28 18:56:08 +00:00
|
|
|
else if targetPlatform.system == "aarch64-linux" then "${libc_lib}/lib/ld-linux-aarch64.so.1"
|
|
|
|
else if targetPlatform.system == "powerpc-linux" then "${libc_lib}/lib/ld.so.1"
|
2017-12-05 10:27:45 +00:00
|
|
|
else if targetPlatform.isMips then "${libc_lib}/lib/ld.so.1"
|
2017-08-28 18:56:08 +00:00
|
|
|
else if targetPlatform.isDarwin then "/usr/lib/dyld"
|
|
|
|
else if stdenv.lib.hasSuffix "pc-gnu" targetPlatform.config then "ld.so.1"
|
|
|
|
else null;
|
|
|
|
|
|
|
|
expand-response-params =
|
|
|
|
if buildPackages.stdenv.cc or null != null && buildPackages.stdenv.cc != "/dev/null"
|
|
|
|
then import ../expand-response-params { inherit (buildPackages) stdenv; }
|
|
|
|
else "";
|
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
stdenv.mkDerivation {
|
|
|
|
name = targetPrefix
|
2018-04-15 00:00:01 +00:00
|
|
|
+ (if name != "" then name else stdenv.lib.removePrefix targetPrefix "${bintoolsName}-wrapper")
|
2017-08-28 18:56:08 +00:00
|
|
|
+ (stdenv.lib.optionalString (bintools != null && bintoolsVersion != "") "-${bintoolsVersion}");
|
|
|
|
|
|
|
|
preferLocalBuild = true;
|
|
|
|
|
|
|
|
inherit bintools_bin libc_bin libc_dev libc_lib coreutils_bin;
|
|
|
|
shell = getBin shell + shell.shellPath or "";
|
|
|
|
gnugrep_bin = if nativeTools then "" else gnugrep;
|
|
|
|
|
|
|
|
inherit targetPrefix infixSalt;
|
|
|
|
|
2018-02-01 00:00:00 +00:00
|
|
|
outputs = [ "out" ] ++ optionals propagateDoc [ "man" "info" ];
|
2017-08-28 18:56:08 +00:00
|
|
|
|
|
|
|
passthru = {
|
|
|
|
inherit bintools libc nativeTools nativeLibc nativePrefix;
|
|
|
|
|
|
|
|
emacsBufferSetup = pkgs: ''
|
|
|
|
; We should handle propagation here too
|
|
|
|
(mapc
|
|
|
|
(lambda (arg)
|
|
|
|
(when (file-directory-p (concat arg "/lib"))
|
|
|
|
(setenv "NIX_${infixSalt}_LDFLAGS" (concat (getenv "NIX_${infixSalt}_LDFLAGS") " -L" arg "/lib")))
|
|
|
|
(when (file-directory-p (concat arg "/lib64"))
|
|
|
|
(setenv "NIX_${infixSalt}_LDFLAGS" (concat (getenv "NIX_${infixSalt}_LDFLAGS") " -L" arg "/lib64"))))
|
|
|
|
'(${concatStringsSep " " (map (pkg: "\"${pkg}\"") pkgs)}))
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
dontBuild = true;
|
|
|
|
dontConfigure = true;
|
|
|
|
|
|
|
|
unpackPhase = ''
|
|
|
|
src=$PWD
|
|
|
|
'';
|
|
|
|
|
|
|
|
installPhase =
|
|
|
|
''
|
|
|
|
set -u
|
|
|
|
|
2018-02-01 00:00:00 +00:00
|
|
|
mkdir -p $out/bin $out/nix-support
|
2017-08-28 18:56:08 +00:00
|
|
|
|
|
|
|
wrap() {
|
|
|
|
local dst="$1"
|
|
|
|
local wrapper="$2"
|
|
|
|
export prog="$3"
|
|
|
|
set +u
|
|
|
|
substituteAll "$wrapper" "$out/bin/$dst"
|
|
|
|
set -u
|
|
|
|
chmod +x "$out/bin/$dst"
|
|
|
|
}
|
|
|
|
''
|
|
|
|
|
|
|
|
+ (if nativeTools then ''
|
|
|
|
echo ${nativePrefix} > $out/nix-support/orig-bintools
|
|
|
|
|
|
|
|
ldPath="${nativePrefix}/bin"
|
|
|
|
'' else ''
|
|
|
|
echo $bintools_bin > $out/nix-support/orig-bintools
|
|
|
|
|
|
|
|
ldPath="${bintools_bin}/bin"
|
|
|
|
''
|
|
|
|
|
|
|
|
+ optionalString (targetPlatform.isSunOS && nativePrefix != "") ''
|
|
|
|
# Solaris needs an additional ld wrapper.
|
|
|
|
ldPath="${nativePrefix}/bin"
|
|
|
|
exec="$ldPath/${targetPrefix}ld"
|
|
|
|
wrap ld-solaris ${./ld-solaris-wrapper.sh}
|
|
|
|
'')
|
|
|
|
|
|
|
|
+ ''
|
|
|
|
# Create a symlink to as (the assembler).
|
|
|
|
if [ -e $ldPath/${targetPrefix}as ]; then
|
|
|
|
ln -s $ldPath/${targetPrefix}as $out/bin/${targetPrefix}as
|
|
|
|
fi
|
|
|
|
|
|
|
|
'' + (if !useMacosReexportHack then ''
|
|
|
|
wrap ${targetPrefix}ld ${./ld-wrapper.sh} ''${ld:-$ldPath/${targetPrefix}ld}
|
|
|
|
'' else ''
|
|
|
|
ldInner="${targetPrefix}ld-reexport-delegate"
|
|
|
|
wrap "$ldInner" ${./macos-sierra-reexport-hack.bash} ''${ld:-$ldPath/${targetPrefix}ld}
|
|
|
|
wrap "${targetPrefix}ld" ${./ld-wrapper.sh} "$out/bin/$ldInner"
|
|
|
|
unset ldInner
|
|
|
|
'') + ''
|
|
|
|
|
2017-11-25 19:04:43 +00:00
|
|
|
for variant in ld.gold ld.bfd ld.lld; do
|
|
|
|
local underlying=$ldPath/${targetPrefix}$variant
|
|
|
|
[[ -e "$underlying" ]] || continue
|
|
|
|
wrap ${targetPrefix}$variant ${./ld-wrapper.sh} $underlying
|
|
|
|
done
|
2017-08-28 18:56:08 +00:00
|
|
|
|
|
|
|
set +u
|
|
|
|
'';
|
|
|
|
|
2017-11-27 05:12:13 +00:00
|
|
|
emulation = let
|
|
|
|
fmt =
|
|
|
|
/**/ if targetPlatform.isDarwin then "mach-o"
|
|
|
|
else if targetPlatform.isWindows then "pe"
|
|
|
|
else "elf" + toString targetPlatform.parsed.cpu.bits;
|
|
|
|
endianPrefix = if targetPlatform.isBigEndian then "big" else "little";
|
2018-08-21 19:31:34 +00:00
|
|
|
sep = optionalString (!targetPlatform.isMips && !targetPlatform.isPower) "-";
|
2017-11-27 05:12:13 +00:00
|
|
|
arch =
|
|
|
|
/**/ if targetPlatform.isAarch64 then endianPrefix + "aarch64"
|
treewide: isArm -> isAarch32
Following legacy packing conventions, `isArm` was defined just for
32-bit ARM instruction set. This is confusing to non packagers though,
because Aarch64 is an ARM instruction set.
The official ARM overview for ARMv8[1] is surprisingly not confusing,
given the overall state of affairs for ARM naming conventions, and
offers us a solution. It divides the nomenclature into three levels:
```
ISA: ARMv8 {-A, -R, -M}
/ \
Mode: Aarch32 Aarch64
| / \
Encoding: A64 A32 T32
```
At the top is the overall v8 instruction set archicture. Second are the
two modes, defined by bitwidth but differing in other semantics too, and
buttom are the encodings, (hopefully?) isomorphic if they encode the
same mode.
The 32 bit encodings are mostly backwards compatible with previous
non-Thumb and Thumb encodings, and if so we can pun the mode names to
instead mean "sets of compatable or isomorphic encodings", and then
voilà we have nice names for 32-bit and 64-bit arm instruction sets
which do not use the word ARM so as to not confused either laymen or
experienced ARM packages.
[1]: https://developer.arm.com/products/architecture/a-profile
(cherry picked from commit ba52ae50488de85a9cf60a3a04f1c9ca7122ec74)
2018-03-20 02:41:06 +00:00
|
|
|
else if targetPlatform.isAarch32 then endianPrefix + "arm"
|
2017-11-27 05:12:13 +00:00
|
|
|
else if targetPlatform.isx86_64 then "x86-64"
|
|
|
|
else if targetPlatform.isi686 then "i386"
|
2017-12-05 10:27:45 +00:00
|
|
|
else if targetPlatform.isMips then {
|
|
|
|
"mips" = "btsmipn32"; # n32 variant
|
|
|
|
"mipsel" = "ltsmipn32"; # n32 variant
|
|
|
|
"mips64" = "btsmip";
|
|
|
|
"mips64el" = "ltsmip";
|
|
|
|
}.${targetPlatform.parsed.cpu.name}
|
2018-08-21 19:31:34 +00:00
|
|
|
else if targetPlatform.isPower then if targetPlatform.isBigEndian then "ppc" else "lppc"
|
2018-07-26 13:33:36 +00:00
|
|
|
else if targetPlatform.isSparc then "sparc"
|
2017-11-27 05:12:13 +00:00
|
|
|
else throw "unknown emulation for platform: " + targetPlatform.config;
|
2017-12-05 10:27:45 +00:00
|
|
|
in targetPlatform.platform.bfdEmulation or (fmt + sep + arch);
|
2017-11-27 05:12:13 +00:00
|
|
|
|
2018-05-13 15:31:24 +00:00
|
|
|
strictDeps = true;
|
2017-07-07 19:57:01 +00:00
|
|
|
depsTargetTargetPropagated = extraPackages;
|
2017-08-28 18:56:08 +00:00
|
|
|
|
2018-05-07 17:07:19 +00:00
|
|
|
wrapperName = "BINTOOLS_WRAPPER";
|
|
|
|
|
|
|
|
setupHooks = [
|
|
|
|
../setup-hooks/role.bash
|
|
|
|
./setup-hook.sh
|
|
|
|
];
|
2017-08-28 18:56:08 +00:00
|
|
|
|
|
|
|
postFixup =
|
|
|
|
''
|
|
|
|
set -u
|
|
|
|
''
|
|
|
|
|
|
|
|
+ optionalString (libc != null) (''
|
|
|
|
##
|
|
|
|
## General libc support
|
|
|
|
##
|
|
|
|
|
|
|
|
echo "-L${libc_lib}/lib" > $out/nix-support/libc-ldflags
|
|
|
|
|
|
|
|
echo "${libc_lib}" > $out/nix-support/orig-libc
|
|
|
|
echo "${libc_dev}" > $out/nix-support/orig-libc-dev
|
|
|
|
|
|
|
|
##
|
|
|
|
## Dynamic linker support
|
|
|
|
##
|
|
|
|
|
|
|
|
if [[ -z ''${dynamicLinker+x} ]]; then
|
|
|
|
echo "Don't know the name of the dynamic linker for platform '${targetPlatform.config}', so guessing instead." >&2
|
|
|
|
local dynamicLinker="${libc_lib}/lib/ld*.so.?"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Expand globs to fill array of options
|
|
|
|
dynamicLinker=($dynamicLinker)
|
|
|
|
|
|
|
|
case ''${#dynamicLinker[@]} in
|
|
|
|
0) echo "No dynamic linker found for platform '${targetPlatform.config}'." >&2;;
|
|
|
|
1) echo "Using dynamic linker: '$dynamicLinker'" >&2;;
|
|
|
|
*) echo "Multiple dynamic linkers found for platform '${targetPlatform.config}'." >&2;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
if [ -n "''${dynamicLinker:-}" ]; then
|
|
|
|
echo $dynamicLinker > $out/nix-support/dynamic-linker
|
|
|
|
|
|
|
|
'' + (if targetPlatform.isDarwin then ''
|
|
|
|
printf "export LD_DYLD_PATH=%q\n" "$dynamicLinker" >> $out/nix-support/setup-hook
|
|
|
|
'' else ''
|
|
|
|
if [ -e ${libc_lib}/lib/32/ld-linux.so.2 ]; then
|
|
|
|
echo ${libc_lib}/lib/32/ld-linux.so.2 > $out/nix-support/dynamic-linker-m32
|
|
|
|
fi
|
|
|
|
|
|
|
|
local ldflagsBefore=(-dynamic-linker "$dynamicLinker")
|
|
|
|
'') + ''
|
|
|
|
fi
|
|
|
|
|
|
|
|
# The dynamic linker is passed in `ldflagsBefore' to allow
|
|
|
|
# explicit overrides of the dynamic linker by callers to ld
|
|
|
|
# (the *last* value counts, so ours should come first).
|
|
|
|
printWords "''${ldflagsBefore[@]}" > $out/nix-support/libc-ldflags-before
|
|
|
|
'')
|
|
|
|
|
|
|
|
+ optionalString (!nativeTools) ''
|
|
|
|
##
|
|
|
|
## User env support
|
|
|
|
##
|
|
|
|
|
|
|
|
# Propagate the underling unwrapped bintools so that if you
|
2018-02-01 00:00:00 +00:00
|
|
|
# install the wrapper, you get tools like objdump (same for any
|
|
|
|
# binaries of libc).
|
2017-08-28 18:56:08 +00:00
|
|
|
printWords ${bintools_bin} ${if libc == null then "" else libc_bin} > $out/nix-support/propagated-user-env-packages
|
2018-02-01 00:00:00 +00:00
|
|
|
''
|
2017-11-28 22:16:09 +00:00
|
|
|
|
2018-02-01 00:00:00 +00:00
|
|
|
+ optionalString propagateDoc ''
|
2017-11-28 22:16:09 +00:00
|
|
|
##
|
|
|
|
## Man page and info support
|
|
|
|
##
|
|
|
|
|
2018-08-09 10:57:38 +00:00
|
|
|
mkdir -p $man/nix-support $info/nix-support
|
2018-08-09 11:07:14 +00:00
|
|
|
echo ${bintools.man or ""} >> $man/nix-support/propagated-user-env-packages
|
|
|
|
echo ${bintools.info or ""} >> $info/nix-support/propagated-user-env-packages
|
2017-08-28 18:56:08 +00:00
|
|
|
''
|
|
|
|
|
|
|
|
+ ''
|
|
|
|
##
|
|
|
|
## Hardening support
|
|
|
|
##
|
|
|
|
|
|
|
|
# some linkers on some platforms don't support specific -z flags
|
|
|
|
export hardening_unsupported_flags=""
|
|
|
|
if [[ "$($ldPath/${targetPrefix}ld -z now 2>&1 || true)" =~ un(recognized|known)\ option ]]; then
|
|
|
|
hardening_unsupported_flags+=" bindnow"
|
|
|
|
fi
|
|
|
|
if [[ "$($ldPath/${targetPrefix}ld -z relro 2>&1 || true)" =~ un(recognized|known)\ option ]]; then
|
|
|
|
hardening_unsupported_flags+=" relro"
|
|
|
|
fi
|
|
|
|
''
|
|
|
|
|
|
|
|
+ optionalString hostPlatform.isCygwin ''
|
|
|
|
hardening_unsupported_flags+=" pic"
|
|
|
|
''
|
|
|
|
|
|
|
|
+ ''
|
|
|
|
set +u
|
|
|
|
substituteAll ${./add-flags.sh} $out/nix-support/add-flags.sh
|
|
|
|
substituteAll ${./add-hardening.sh} $out/nix-support/add-hardening.sh
|
2018-05-07 17:15:34 +00:00
|
|
|
substituteAll ${../wrapper-common/utils.bash} $out/nix-support/utils.bash
|
2017-08-28 18:56:08 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
## Extra custom steps
|
|
|
|
##
|
|
|
|
''
|
2018-02-01 00:00:00 +00:00
|
|
|
|
2017-08-28 18:56:08 +00:00
|
|
|
+ extraBuildCommands;
|
|
|
|
|
|
|
|
inherit dynamicLinker expand-response-params;
|
|
|
|
|
2018-05-07 17:15:34 +00:00
|
|
|
# for substitution in utils.bash
|
2017-08-28 18:56:08 +00:00
|
|
|
expandResponseParams = "${expand-response-params}/bin/expand-response-params";
|
|
|
|
|
|
|
|
meta =
|
|
|
|
let bintools_ = if bintools != null then bintools else {}; in
|
|
|
|
(if bintools_ ? meta then removeAttrs bintools.meta ["priority"] else {}) //
|
|
|
|
{ description =
|
|
|
|
stdenv.lib.attrByPath ["meta" "description"] "System binary utilities" bintools_
|
|
|
|
+ " (wrapper script)";
|
|
|
|
} // optionalAttrs useMacosReexportHack {
|
|
|
|
platforms = stdenv.lib.platforms.darwin;
|
|
|
|
};
|
|
|
|
}
|