2015-01-09 20:22:12 +01:00
|
|
|
# The Nixpkgs CC is not directly usable, since it doesn't know where
|
2014-10-10 13:49:26 +02:00
|
|
|
# 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".
|
2008-06-26 13:07:46 +02:00
|
|
|
|
2018-02-01 01:00:00 +01:00
|
|
|
{ name ? ""
|
|
|
|
, stdenvNoCC, nativeTools, propagateDoc ? !nativeTools, noLibc ? false, nativeLibc, nativePrefix ? ""
|
2017-08-26 17:43:30 +02:00
|
|
|
, cc ? null, libc ? null, bintools, coreutils ? null, shell ? stdenvNoCC.shell
|
2018-03-08 19:56:36 +01:00
|
|
|
, extraPackages ? [], extraBuildCommands ? ""
|
2015-12-17 23:02:40 +01:00
|
|
|
, isGNU ? false, isClang ? cc.isClang or false, gnugrep ? null
|
2017-08-22 00:26:08 +02:00
|
|
|
, buildPackages ? {}
|
2008-06-26 13:07:46 +02:00
|
|
|
}:
|
|
|
|
|
2017-09-20 17:07:52 +02:00
|
|
|
with stdenvNoCC.lib;
|
2014-10-10 13:49:26 +02:00
|
|
|
|
2018-02-01 01:00:00 +01:00
|
|
|
assert nativeTools -> !propagateDoc && nativePrefix != "";
|
2015-12-17 23:02:40 +01:00
|
|
|
assert !nativeTools ->
|
2017-08-26 17:43:30 +02:00
|
|
|
cc != null && coreutils != null && gnugrep != null;
|
2017-05-23 02:59:39 +02:00
|
|
|
assert !(nativeLibc && noLibc);
|
|
|
|
assert (noLibc || nativeLibc) == (libc == null);
|
2008-06-26 13:07:46 +02:00
|
|
|
|
2009-04-09 17:24:33 +02:00
|
|
|
let
|
2017-09-20 17:07:52 +02:00
|
|
|
stdenv = stdenvNoCC;
|
2017-07-06 03:47:48 +02:00
|
|
|
inherit (stdenv) hostPlatform targetPlatform;
|
|
|
|
|
2017-02-14 00:01:04 +01:00
|
|
|
# Prefix for binaries. Customarily ends with a dash separator.
|
|
|
|
#
|
|
|
|
# TODO(@Ericson2314) Make unconditional, or optional but always true by
|
|
|
|
# default.
|
2017-11-25 19:43:57 +01:00
|
|
|
targetPrefix = stdenv.lib.optionalString (targetPlatform != hostPlatform)
|
|
|
|
(targetPlatform.config + "-");
|
2009-04-09 17:24:33 +02:00
|
|
|
|
2015-01-09 20:22:12 +01:00
|
|
|
ccVersion = (builtins.parseDrvName cc.name).version;
|
|
|
|
ccName = (builtins.parseDrvName cc.name).name;
|
2012-08-03 19:23:51 +02:00
|
|
|
|
2017-05-23 02:59:39 +02:00
|
|
|
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;
|
2016-04-14 18:00:39 +02:00
|
|
|
cc_solib = getLib cc;
|
2016-02-03 16:54:03 +01:00
|
|
|
# The wrapper scripts use 'cat' and 'grep', so we may need coreutils.
|
2016-04-14 18:00:39 +02:00
|
|
|
coreutils_bin = if nativeTools then "" else getBin coreutils;
|
2017-06-08 20:00:40 +02:00
|
|
|
|
2017-12-09 07:01:39 +01:00
|
|
|
default_cxx_stdlib_compile = optionalString (targetPlatform.isLinux && !(cc.isGNU or false) && !nativeTools)
|
2017-06-08 20:00:40 +02:00
|
|
|
"-isystem $(echo -n ${cc.gcc}/include/c++/*) -isystem $(echo -n ${cc.gcc}/include/c++/*)/$(${cc.gcc}/bin/gcc -dumpmachine)";
|
2017-05-23 02:59:39 +02:00
|
|
|
|
|
|
|
dashlessTarget = stdenv.lib.replaceStrings ["-"] ["_"] targetPlatform.config;
|
2017-06-26 06:43:06 +02:00
|
|
|
|
|
|
|
# The "infix salt" is a arbitrary string added in the middle of env vars
|
|
|
|
# defined by cc-wrapper's hooks so that multiple cc-wrappers can be used
|
|
|
|
# without interfering. For the moment, it is defined as the target triple,
|
|
|
|
# adjusted to be a valid bash identifier. This should be considered an
|
|
|
|
# unstable implementation detail, however.
|
|
|
|
infixSalt = dashlessTarget;
|
2017-06-19 17:10:27 +02:00
|
|
|
|
2017-08-23 22:23:30 +02:00
|
|
|
expand-response-params =
|
|
|
|
if buildPackages.stdenv.cc or null != null && buildPackages.stdenv.cc != "/dev/null"
|
|
|
|
then import ../expand-response-params { inherit (buildPackages) stdenv; }
|
|
|
|
else "";
|
2017-06-29 18:06:55 +02:00
|
|
|
|
2009-04-09 17:24:33 +02:00
|
|
|
in
|
2009-03-25 18:34:38 +01:00
|
|
|
|
2017-08-28 20:56:08 +02:00
|
|
|
# Ensure bintools matches
|
|
|
|
assert libc_bin == bintools.libc_bin;
|
|
|
|
assert libc_dev == bintools.libc_dev;
|
|
|
|
assert libc_lib == bintools.libc_lib;
|
|
|
|
assert nativeTools == bintools.nativeTools;
|
|
|
|
assert nativeLibc == bintools.nativeLibc;
|
|
|
|
assert nativePrefix == bintools.nativePrefix;
|
|
|
|
|
2012-12-29 15:11:32 +01:00
|
|
|
stdenv.mkDerivation {
|
2017-11-25 19:43:57 +01:00
|
|
|
name = targetPrefix
|
2017-02-14 00:01:04 +01:00
|
|
|
+ (if name != "" then name else "${ccName}-wrapper")
|
|
|
|
+ (stdenv.lib.optionalString (cc != null && ccVersion != "") "-${ccVersion}");
|
2014-02-04 16:58:12 +01:00
|
|
|
|
2014-10-10 13:49:26 +02:00
|
|
|
preferLocalBuild = true;
|
|
|
|
|
2017-08-28 20:56:08 +02:00
|
|
|
inherit cc libc_bin libc_dev libc_lib bintools coreutils_bin;
|
|
|
|
shell = getBin shell + stdenv.lib.optionalString (stdenv ? shellPath) stdenv.shellPath;
|
2016-02-01 14:42:33 +01:00
|
|
|
gnugrep_bin = if nativeTools then "" else gnugrep;
|
2014-02-04 16:58:12 +01:00
|
|
|
|
2017-11-25 19:43:57 +01:00
|
|
|
inherit targetPrefix infixSalt;
|
2017-06-08 20:00:40 +02:00
|
|
|
|
2018-02-01 01:00:00 +01:00
|
|
|
outputs = [ "out" ] ++ optionals propagateDoc [ "man" "info" ];
|
2017-06-08 20:00:40 +02:00
|
|
|
|
2016-10-07 16:31:37 +02:00
|
|
|
passthru = {
|
2017-10-11 01:01:42 +02:00
|
|
|
# "cc" is the generic name for a C compiler, but there is no one for package
|
|
|
|
# providing the linker and related tools. The two we use now are GNU
|
2017-08-28 20:56:08 +02:00
|
|
|
# Binutils, and Apple's "cctools"; "bintools" as an attempt to find an
|
2017-10-11 01:01:42 +02:00
|
|
|
# unused middle-ground name that evokes both.
|
2017-08-28 20:56:08 +02:00
|
|
|
inherit bintools;
|
2017-11-25 19:43:57 +01:00
|
|
|
inherit libc nativeTools nativeLibc nativePrefix isGNU isClang default_cxx_stdlib_compile;
|
2016-10-07 16:31:37 +02:00
|
|
|
|
|
|
|
emacsBufferSetup = pkgs: ''
|
|
|
|
; We should handle propagation here too
|
2017-08-28 20:56:08 +02:00
|
|
|
(mapc
|
|
|
|
(lambda (arg)
|
|
|
|
(when (file-directory-p (concat arg "/include"))
|
|
|
|
(setenv "NIX_${infixSalt}_CFLAGS_COMPILE" (concat (getenv "NIX_${infixSalt}_CFLAGS_COMPILE") " -isystem " arg "/include"))))
|
|
|
|
'(${concatStringsSep " " (map (pkg: "\"${pkg}\"") pkgs)}))
|
2016-10-07 16:31:37 +02:00
|
|
|
'';
|
|
|
|
};
|
2014-10-10 13:49:26 +02:00
|
|
|
|
2017-08-23 22:55:55 +02:00
|
|
|
dontBuild = true;
|
|
|
|
dontConfigure = true;
|
|
|
|
|
|
|
|
unpackPhase = ''
|
|
|
|
src=$PWD
|
|
|
|
'';
|
|
|
|
|
|
|
|
installPhase =
|
2014-10-10 13:49:26 +02:00
|
|
|
''
|
2017-08-23 23:30:56 +02:00
|
|
|
set -u
|
|
|
|
|
2018-02-01 01:00:00 +01:00
|
|
|
mkdir -p $out/bin $out/nix-support
|
2014-10-10 13:49:26 +02:00
|
|
|
|
|
|
|
wrap() {
|
|
|
|
local dst="$1"
|
|
|
|
local wrapper="$2"
|
|
|
|
export prog="$3"
|
|
|
|
substituteAll "$wrapper" "$out/bin/$dst"
|
|
|
|
chmod +x "$out/bin/$dst"
|
|
|
|
}
|
|
|
|
''
|
|
|
|
|
|
|
|
+ (if nativeTools then ''
|
2017-08-23 22:55:55 +02:00
|
|
|
echo ${if targetPlatform.isDarwin then cc else nativePrefix} > $out/nix-support/orig-cc
|
|
|
|
|
2017-05-21 20:51:02 +02:00
|
|
|
ccPath="${if targetPlatform.isDarwin then cc else nativePrefix}/bin"
|
2014-10-10 13:49:26 +02:00
|
|
|
'' else ''
|
2015-01-09 20:22:12 +01:00
|
|
|
echo $cc > $out/nix-support/orig-cc
|
2014-10-10 13:49:26 +02:00
|
|
|
|
2016-01-02 11:14:09 +01:00
|
|
|
ccPath="${cc}/bin"
|
2014-10-10 13:49:26 +02:00
|
|
|
'')
|
|
|
|
|
|
|
|
+ ''
|
2017-08-28 20:56:08 +02:00
|
|
|
# Create symlinks to everything in the bintools wrapper.
|
|
|
|
for bbin in $bintools/bin/*; do
|
|
|
|
mkdir -p "$out/bin"
|
|
|
|
ln -s "$bbin" "$out/bin/$(basename $bbin)"
|
|
|
|
done
|
2014-10-10 14:01:38 +02:00
|
|
|
|
2017-06-26 07:17:09 +02:00
|
|
|
# We export environment variables pointing to the wrapped nonstandard
|
|
|
|
# cmds, lest some lousy configure script use those to guess compiler
|
|
|
|
# version.
|
2017-11-25 19:43:57 +01:00
|
|
|
export named_cc=${targetPrefix}cc
|
|
|
|
export named_cxx=${targetPrefix}c++
|
2017-06-26 07:17:09 +02:00
|
|
|
|
2017-06-08 20:00:40 +02:00
|
|
|
export default_cxx_stdlib_compile="${default_cxx_stdlib_compile}"
|
2015-03-06 20:04:43 +01:00
|
|
|
|
2017-11-25 19:43:57 +01:00
|
|
|
if [ -e $ccPath/${targetPrefix}gcc ]; then
|
|
|
|
wrap ${targetPrefix}gcc ${./cc-wrapper.sh} $ccPath/${targetPrefix}gcc
|
|
|
|
ln -s ${targetPrefix}gcc $out/bin/${targetPrefix}cc
|
|
|
|
export named_cc=${targetPrefix}gcc
|
|
|
|
export named_cxx=${targetPrefix}g++
|
2015-01-09 20:22:12 +01:00
|
|
|
elif [ -e $ccPath/clang ]; then
|
2017-11-25 19:43:57 +01:00
|
|
|
wrap ${targetPrefix}clang ${./cc-wrapper.sh} $ccPath/clang
|
|
|
|
ln -s ${targetPrefix}clang $out/bin/${targetPrefix}cc
|
|
|
|
export named_cc=${targetPrefix}clang
|
|
|
|
export named_cxx=${targetPrefix}clang++
|
2014-10-10 13:49:26 +02:00
|
|
|
fi
|
|
|
|
|
2017-11-25 19:43:57 +01:00
|
|
|
if [ -e $ccPath/${targetPrefix}g++ ]; then
|
|
|
|
wrap ${targetPrefix}g++ ${./cc-wrapper.sh} $ccPath/${targetPrefix}g++
|
|
|
|
ln -s ${targetPrefix}g++ $out/bin/${targetPrefix}c++
|
2015-01-09 20:22:12 +01:00
|
|
|
elif [ -e $ccPath/clang++ ]; then
|
2017-11-25 19:43:57 +01:00
|
|
|
wrap ${targetPrefix}clang++ ${./cc-wrapper.sh} $ccPath/clang++
|
|
|
|
ln -s ${targetPrefix}clang++ $out/bin/${targetPrefix}c++
|
2014-10-10 15:48:34 +02:00
|
|
|
fi
|
|
|
|
|
2015-01-09 20:22:12 +01:00
|
|
|
if [ -e $ccPath/cpp ]; then
|
2017-11-25 19:43:57 +01:00
|
|
|
wrap ${targetPrefix}cpp ${./cc-wrapper.sh} $ccPath/cpp
|
2014-10-10 13:49:26 +02:00
|
|
|
fi
|
|
|
|
''
|
|
|
|
|
2015-01-09 20:22:12 +01:00
|
|
|
+ optionalString cc.langFortran or false ''
|
2017-11-25 19:43:57 +01:00
|
|
|
wrap ${targetPrefix}gfortran ${./cc-wrapper.sh} $ccPath/${targetPrefix}gfortran
|
|
|
|
ln -sv ${targetPrefix}gfortran $out/bin/${targetPrefix}g77
|
|
|
|
ln -sv ${targetPrefix}gfortran $out/bin/${targetPrefix}f77
|
2014-10-10 13:49:26 +02:00
|
|
|
''
|
|
|
|
|
2015-01-09 20:22:12 +01:00
|
|
|
+ optionalString cc.langJava or false ''
|
2017-11-25 19:43:57 +01:00
|
|
|
wrap ${targetPrefix}gcj ${./cc-wrapper.sh} $ccPath/${targetPrefix}gcj
|
2014-10-10 13:49:26 +02:00
|
|
|
''
|
|
|
|
|
2015-01-09 20:22:12 +01:00
|
|
|
+ optionalString cc.langGo or false ''
|
2017-11-25 19:43:57 +01:00
|
|
|
wrap ${targetPrefix}gccgo ${./cc-wrapper.sh} $ccPath/${targetPrefix}gccgo
|
2017-08-23 22:55:55 +02:00
|
|
|
'';
|
|
|
|
|
2017-07-07 21:57:01 +02:00
|
|
|
propagatedBuildInputs = [ bintools ];
|
|
|
|
depsTargetTargetPropagated = extraPackages;
|
2017-08-23 22:55:55 +02:00
|
|
|
|
|
|
|
setupHook = ./setup-hook.sh;
|
|
|
|
|
|
|
|
postFixup =
|
|
|
|
''
|
|
|
|
set -u
|
2017-08-28 20:56:08 +02:00
|
|
|
|
|
|
|
# Backwards compatability for packages expecting this file, e.g. with
|
|
|
|
# `$NIX_CC/nix-support/dynamic-linker`.
|
|
|
|
#
|
|
|
|
# TODO(@Ericson2314): Remove this after stable release and force
|
|
|
|
# everyone to refer to bintools-wrapper directly.
|
|
|
|
if [[ -f "$bintools/nix-support/dynamic-linker" ]]; then
|
|
|
|
ln -s "$bintools/nix-support/dynamic-linker" "$out/nix-support"
|
|
|
|
fi
|
|
|
|
if [[ -f "$bintools/nix-support/dynamic-linker-m32" ]]; then
|
|
|
|
ln -s "$bintools/nix-support/dynamic-linker-m32" "$out/nix-support"
|
|
|
|
fi
|
2017-08-23 22:55:55 +02:00
|
|
|
''
|
|
|
|
|
2017-08-28 20:56:08 +02:00
|
|
|
+ optionalString (libc != null) ''
|
2017-08-23 22:55:55 +02:00
|
|
|
##
|
|
|
|
## General libc support
|
|
|
|
##
|
|
|
|
|
|
|
|
# The "-B${libc_lib}/lib/" flag is a quick hack to force gcc to link
|
|
|
|
# against the crt1.o from our own glibc, rather than the one in
|
|
|
|
# /usr/lib. (This is only an issue when using an `impure'
|
|
|
|
# compiler/linker, i.e., one that searches /usr/lib and so on.)
|
|
|
|
#
|
|
|
|
# Unfortunately, setting -B appears to override the default search
|
|
|
|
# path. Thus, the gcc-specific "../includes-fixed" directory is
|
|
|
|
# now longer searched and glibc's <limits.h> header fails to
|
|
|
|
# compile, because it uses "#include_next <limits.h>" to find the
|
|
|
|
# limits.h file in ../includes-fixed. To remedy the problem,
|
|
|
|
# another -idirafter is necessary to add that directory again.
|
2017-11-30 20:43:38 +01:00
|
|
|
echo "-B${libc_lib}/lib/ -idirafter ${libc_dev}/include ${optionalString isGNU "-idirafter ${cc}/lib/gcc/*/*/include-fixed"}" > $out/nix-support/libc-cflags
|
2017-08-23 22:55:55 +02:00
|
|
|
|
|
|
|
echo "${libc_lib}" > $out/nix-support/orig-libc
|
|
|
|
echo "${libc_dev}" > $out/nix-support/orig-libc-dev
|
2017-08-28 20:56:08 +02:00
|
|
|
''
|
2017-08-23 22:55:55 +02:00
|
|
|
|
|
|
|
+ optionalString (!nativeTools) ''
|
|
|
|
##
|
|
|
|
## Initial CFLAGS
|
|
|
|
##
|
|
|
|
|
|
|
|
# GCC shows ${cc_solib}/lib in `gcc -print-search-dirs', but not
|
|
|
|
# ${cc_solib}/lib64 (even though it does actually search there...)..
|
|
|
|
# This confuses libtool. So add it to the compiler tool search
|
|
|
|
# path explicitly.
|
|
|
|
if [ -e "${cc_solib}/lib64" -a ! -L "${cc_solib}/lib64" ]; then
|
|
|
|
ccLDFlags+=" -L${cc_solib}/lib64"
|
|
|
|
ccCFlags+=" -B${cc_solib}/lib64"
|
|
|
|
fi
|
|
|
|
ccLDFlags+=" -L${cc_solib}/lib"
|
|
|
|
ccCFlags+=" -B${cc_solib}/lib"
|
|
|
|
|
|
|
|
echo "$ccLDFlags" > $out/nix-support/cc-ldflags
|
|
|
|
echo "$ccCFlags" > $out/nix-support/cc-cflags
|
2018-02-01 01:00:00 +01:00
|
|
|
''
|
2017-08-23 22:55:55 +02:00
|
|
|
|
2018-02-01 01:00:00 +01:00
|
|
|
+ optionalString propagateDoc ''
|
2017-08-23 22:55:55 +02:00
|
|
|
##
|
2018-02-01 01:00:00 +01:00
|
|
|
## Man page and info support
|
2017-08-23 22:55:55 +02:00
|
|
|
##
|
|
|
|
|
2018-02-01 01:00:00 +01:00
|
|
|
mkdir -p $man/nix-support $info/nix-support
|
2017-08-23 22:55:55 +02:00
|
|
|
printWords ${cc.man or ""} > $man/nix-support/propagated-user-env-packages
|
2018-02-01 01:00:00 +01:00
|
|
|
printWords ${cc.info or ""} > $info/nix-support/propagated-user-env-packages
|
2014-10-10 13:49:26 +02:00
|
|
|
''
|
|
|
|
|
|
|
|
+ ''
|
2017-08-23 22:55:55 +02:00
|
|
|
##
|
|
|
|
## Hardening support
|
|
|
|
##
|
2015-02-08 19:36:13 +01:00
|
|
|
|
2017-09-16 23:54:17 +02:00
|
|
|
export hardening_unsupported_flags="${builtins.concatStringsSep " " (cc.hardeningUnsupportedFlags or [])}"
|
2017-06-20 20:04:24 +02:00
|
|
|
''
|
|
|
|
|
|
|
|
+ optionalString hostPlatform.isCygwin ''
|
|
|
|
hardening_unsupported_flags+=" pic"
|
|
|
|
''
|
2016-08-23 18:13:31 +02:00
|
|
|
|
2017-06-20 20:04:24 +02:00
|
|
|
+ ''
|
2017-06-26 06:43:06 +02:00
|
|
|
substituteAll ${./add-flags.sh} $out/nix-support/add-flags.sh
|
|
|
|
substituteAll ${./add-hardening.sh} $out/nix-support/add-hardening.sh
|
|
|
|
substituteAll ${./utils.sh} $out/nix-support/utils.sh
|
2017-08-23 22:55:55 +02:00
|
|
|
|
|
|
|
##
|
|
|
|
## Extra custom steps
|
|
|
|
##
|
2015-07-20 13:42:30 +02:00
|
|
|
''
|
2018-02-01 01:00:00 +01:00
|
|
|
|
2015-07-20 13:42:30 +02:00
|
|
|
+ extraBuildCommands;
|
2014-10-10 13:49:26 +02:00
|
|
|
|
2017-08-28 20:56:08 +02:00
|
|
|
inherit expand-response-params;
|
2017-07-01 02:27:48 +02:00
|
|
|
|
2017-08-23 22:39:15 +02:00
|
|
|
# for substitution in utils.sh
|
|
|
|
expandResponseParams = "${expand-response-params}/bin/expand-response-params";
|
2010-12-04 22:45:37 +01:00
|
|
|
|
2009-03-25 19:34:27 +01:00
|
|
|
meta =
|
2015-01-09 20:22:12 +01:00
|
|
|
let cc_ = if cc != null then cc else {}; in
|
|
|
|
(if cc_ ? meta then removeAttrs cc.meta ["priority"] else {}) //
|
2009-04-19 18:00:46 +02:00
|
|
|
{ description =
|
2015-01-09 20:22:12 +01:00
|
|
|
stdenv.lib.attrByPath ["meta" "description"] "System C compiler" cc_
|
2009-04-19 18:00:46 +02:00
|
|
|
+ " (wrapper script)";
|
2017-07-18 05:36:17 +02:00
|
|
|
};
|
2008-06-26 13:07:46 +02:00
|
|
|
}
|