2020-04-24 23:36:52 +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".
|
|
|
|
|
|
|
|
|
|
{ name ? ""
|
2021-02-05 17:12:51 +00:00
|
|
|
|
, lib
|
2020-04-24 23:36:52 +00:00
|
|
|
|
, stdenvNoCC
|
|
|
|
|
, cc ? null, libc ? null, bintools, coreutils ? null, shell ? stdenvNoCC.shell
|
2020-08-20 17:08:02 +00:00
|
|
|
|
, gccForLibs ? null
|
2020-05-29 06:06:01 +00:00
|
|
|
|
, zlib ? null
|
2020-04-24 23:36:52 +00:00
|
|
|
|
, nativeTools, noLibc ? false, nativeLibc, nativePrefix ? ""
|
|
|
|
|
, propagateDoc ? cc != null && cc ? man
|
|
|
|
|
, extraTools ? [], extraPackages ? [], extraBuildCommands ? ""
|
2022-06-16 17:23:12 +00:00
|
|
|
|
, nixSupport ? {}
|
2020-04-24 23:36:52 +00:00
|
|
|
|
, isGNU ? false, isClang ? cc.isClang or false, gnugrep ? null
|
|
|
|
|
, buildPackages ? {}
|
|
|
|
|
, libcxx ? null
|
|
|
|
|
}:
|
|
|
|
|
|
2021-02-05 17:12:51 +00:00
|
|
|
|
with lib;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
|
assert nativeTools -> !propagateDoc && nativePrefix != "";
|
|
|
|
|
assert !nativeTools ->
|
|
|
|
|
cc != 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.
|
2021-02-05 17:12:51 +00:00
|
|
|
|
targetPrefix = lib.optionalString (targetPlatform != hostPlatform)
|
2020-04-24 23:36:52 +00:00
|
|
|
|
(targetPlatform.config + "-");
|
|
|
|
|
|
2021-02-05 17:12:51 +00:00
|
|
|
|
ccVersion = lib.getVersion cc;
|
|
|
|
|
ccName = lib.removePrefix targetPrefix (lib.getName cc);
|
2020-04-24 23:36:52 +00: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;
|
|
|
|
|
cc_solib = getLib cc
|
|
|
|
|
+ optionalString (targetPlatform != hostPlatform) "/${targetPlatform.config}";
|
|
|
|
|
|
|
|
|
|
# The wrapper scripts use 'cat' and 'grep', so we may need coreutils.
|
|
|
|
|
coreutils_bin = if nativeTools then "" else getBin coreutils;
|
|
|
|
|
|
2020-06-18 07:06:33 +00:00
|
|
|
|
# The "suffix salt" is a arbitrary string added in the end of env vars
|
2020-04-24 23:36:52 +00:00
|
|
|
|
# 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.
|
2020-06-18 07:06:33 +00:00
|
|
|
|
suffixSalt = replaceStrings ["-" "."] ["_" "_"] targetPlatform.config;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
|
expand-response-params =
|
2020-11-03 02:18:15 +00:00
|
|
|
|
if (buildPackages.stdenv.hasCC or false) && buildPackages.stdenv.cc != "/dev/null"
|
2020-04-24 23:36:52 +00:00
|
|
|
|
then import ../expand-response-params { inherit (buildPackages) stdenv; }
|
|
|
|
|
else "";
|
|
|
|
|
|
2020-09-25 04:45:31 +00:00
|
|
|
|
useGccForLibs = isClang
|
|
|
|
|
&& libcxx == null
|
2021-05-28 09:39:13 +00:00
|
|
|
|
&& !stdenv.targetPlatform.isDarwin
|
2020-09-25 04:45:31 +00:00
|
|
|
|
&& !(stdenv.targetPlatform.useLLVM or false)
|
|
|
|
|
&& !(stdenv.targetPlatform.useAndroidPrebuilt or false)
|
2021-02-05 17:12:51 +00:00
|
|
|
|
&& !(stdenv.targetPlatform.isiOS or false)
|
2020-09-25 04:45:31 +00:00
|
|
|
|
&& gccForLibs != null;
|
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
|
# older compilers (for example bootstrap's GCC 5) fail with -march=too-modern-cpu
|
|
|
|
|
isGccArchSupported = arch:
|
2020-07-18 16:06:22 +00:00
|
|
|
|
if isGNU then
|
2020-09-25 04:45:31 +00:00
|
|
|
|
{ # Intel
|
|
|
|
|
skylake = versionAtLeast ccVersion "6.0";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
skylake-avx512 = versionAtLeast ccVersion "6.0";
|
|
|
|
|
cannonlake = versionAtLeast ccVersion "8.0";
|
|
|
|
|
icelake-client = versionAtLeast ccVersion "8.0";
|
|
|
|
|
icelake-server = versionAtLeast ccVersion "8.0";
|
2020-12-25 13:55:36 +00:00
|
|
|
|
cascadelake = versionAtLeast ccVersion "9.0";
|
|
|
|
|
cooperlake = versionAtLeast ccVersion "10.0";
|
|
|
|
|
tigerlake = versionAtLeast ccVersion "10.0";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
knm = versionAtLeast ccVersion "8.0";
|
2020-09-25 04:45:31 +00:00
|
|
|
|
# AMD
|
|
|
|
|
znver1 = versionAtLeast ccVersion "6.0";
|
|
|
|
|
znver2 = versionAtLeast ccVersion "9.0";
|
2020-12-25 13:55:36 +00:00
|
|
|
|
znver3 = versionAtLeast ccVersion "11.0";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
}.${arch} or true
|
2020-07-18 16:06:22 +00:00
|
|
|
|
else if isClang then
|
2020-09-25 04:45:31 +00:00
|
|
|
|
{ # Intel
|
|
|
|
|
cannonlake = versionAtLeast ccVersion "5.0";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
icelake-client = versionAtLeast ccVersion "7.0";
|
|
|
|
|
icelake-server = versionAtLeast ccVersion "7.0";
|
|
|
|
|
knm = versionAtLeast ccVersion "7.0";
|
2020-09-25 04:45:31 +00:00
|
|
|
|
# AMD
|
|
|
|
|
znver1 = versionAtLeast ccVersion "4.0";
|
|
|
|
|
znver2 = versionAtLeast ccVersion "9.0";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
}.${arch} or true
|
|
|
|
|
else
|
|
|
|
|
false;
|
|
|
|
|
|
2021-04-26 19:14:03 +00:00
|
|
|
|
|
|
|
|
|
darwinPlatformForCC = optionalString stdenv.targetPlatform.isDarwin (
|
|
|
|
|
if (targetPlatform.darwinPlatform == "macos" && isGNU) then "macosx"
|
|
|
|
|
else targetPlatform.darwinPlatform
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
darwinMinVersion = optionalString stdenv.targetPlatform.isDarwin (
|
|
|
|
|
stdenv.targetPlatform.darwinMinVersion
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
darwinMinVersionVariable = optionalString stdenv.targetPlatform.isDarwin
|
|
|
|
|
stdenv.targetPlatform.darwinMinVersionVariable;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
in
|
|
|
|
|
|
|
|
|
|
# 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;
|
|
|
|
|
|
|
|
|
|
stdenv.mkDerivation {
|
|
|
|
|
pname = targetPrefix
|
|
|
|
|
+ (if name != "" then name else "${ccName}-wrapper");
|
|
|
|
|
version = if cc == null then null else ccVersion;
|
|
|
|
|
|
|
|
|
|
preferLocalBuild = true;
|
|
|
|
|
|
|
|
|
|
inherit cc libc_bin libc_dev libc_lib bintools coreutils_bin;
|
|
|
|
|
shell = getBin shell + shell.shellPath or "";
|
|
|
|
|
gnugrep_bin = if nativeTools then "" else gnugrep;
|
|
|
|
|
|
2020-06-18 07:06:33 +00:00
|
|
|
|
inherit targetPrefix suffixSalt;
|
2021-04-26 19:14:03 +00:00
|
|
|
|
inherit darwinPlatformForCC darwinMinVersion darwinMinVersionVariable;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
|
outputs = [ "out" ] ++ optionals propagateDoc [ "man" "info" ];
|
|
|
|
|
|
|
|
|
|
passthru = {
|
|
|
|
|
# "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
|
|
|
|
|
# Binutils, and Apple's "cctools"; "bintools" as an attempt to find an
|
|
|
|
|
# unused middle-ground name that evokes both.
|
|
|
|
|
inherit bintools;
|
2020-07-18 16:06:22 +00:00
|
|
|
|
inherit libc nativeTools nativeLibc nativePrefix isGNU isClang;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
|
emacsBufferSetup = pkgs: ''
|
|
|
|
|
; We should handle propagation here too
|
|
|
|
|
(mapc
|
|
|
|
|
(lambda (arg)
|
|
|
|
|
(when (file-directory-p (concat arg "/include"))
|
2020-06-18 07:06:33 +00:00
|
|
|
|
(setenv "NIX_CFLAGS_COMPILE_${suffixSalt}" (concat (getenv "NIX_CFLAGS_COMPILE_${suffixSalt}") " -isystem " arg "/include"))))
|
2020-04-24 23:36:52 +00:00
|
|
|
|
'(${concatStringsSep " " (map (pkg: "\"${pkg}\"") pkgs)}))
|
|
|
|
|
'';
|
2022-06-16 17:23:12 +00:00
|
|
|
|
|
2022-09-09 14:08:57 +00:00
|
|
|
|
inherit expand-response-params;
|
|
|
|
|
|
2022-06-16 17:23:12 +00:00
|
|
|
|
inherit nixSupport;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
dontBuild = true;
|
|
|
|
|
dontConfigure = true;
|
2022-06-16 17:23:12 +00:00
|
|
|
|
enableParallelBuilding = true;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
|
unpackPhase = ''
|
|
|
|
|
src=$PWD
|
|
|
|
|
'';
|
|
|
|
|
|
|
|
|
|
wrapper = ./cc-wrapper.sh;
|
|
|
|
|
|
|
|
|
|
installPhase =
|
|
|
|
|
''
|
|
|
|
|
mkdir -p $out/bin $out/nix-support
|
|
|
|
|
|
|
|
|
|
wrap() {
|
|
|
|
|
local dst="$1"
|
|
|
|
|
local wrapper="$2"
|
|
|
|
|
export prog="$3"
|
2021-03-19 17:17:44 +00:00
|
|
|
|
export use_response_file_by_default=${if isClang then "1" else "0"}
|
2020-04-24 23:36:52 +00:00
|
|
|
|
substituteAll "$wrapper" "$out/bin/$dst"
|
|
|
|
|
chmod +x "$out/bin/$dst"
|
|
|
|
|
}
|
|
|
|
|
''
|
|
|
|
|
|
|
|
|
|
+ (if nativeTools then ''
|
|
|
|
|
echo ${if targetPlatform.isDarwin then cc else nativePrefix} > $out/nix-support/orig-cc
|
|
|
|
|
|
|
|
|
|
ccPath="${if targetPlatform.isDarwin then cc else nativePrefix}/bin"
|
|
|
|
|
'' else ''
|
|
|
|
|
echo $cc > $out/nix-support/orig-cc
|
|
|
|
|
|
|
|
|
|
ccPath="${cc}/bin"
|
|
|
|
|
'')
|
|
|
|
|
|
2020-07-18 16:06:22 +00:00
|
|
|
|
# Create symlinks to everything in the bintools wrapper.
|
2020-04-24 23:36:52 +00:00
|
|
|
|
+ ''
|
|
|
|
|
for bbin in $bintools/bin/*; do
|
|
|
|
|
mkdir -p "$out/bin"
|
|
|
|
|
ln -s "$bbin" "$out/bin/$(basename $bbin)"
|
|
|
|
|
done
|
2020-07-18 16:06:22 +00:00
|
|
|
|
''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
2020-07-18 16:06:22 +00:00
|
|
|
|
# We export environment variables pointing to the wrapped nonstandard
|
|
|
|
|
# cmds, lest some lousy configure script use those to guess compiler
|
|
|
|
|
# version.
|
|
|
|
|
+ ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
export named_cc=${targetPrefix}cc
|
|
|
|
|
export named_cxx=${targetPrefix}c++
|
|
|
|
|
|
|
|
|
|
if [ -e $ccPath/${targetPrefix}gcc ]; then
|
|
|
|
|
wrap ${targetPrefix}gcc $wrapper $ccPath/${targetPrefix}gcc
|
|
|
|
|
ln -s ${targetPrefix}gcc $out/bin/${targetPrefix}cc
|
|
|
|
|
export named_cc=${targetPrefix}gcc
|
|
|
|
|
export named_cxx=${targetPrefix}g++
|
|
|
|
|
elif [ -e $ccPath/clang ]; then
|
|
|
|
|
wrap ${targetPrefix}clang $wrapper $ccPath/clang
|
|
|
|
|
ln -s ${targetPrefix}clang $out/bin/${targetPrefix}cc
|
|
|
|
|
export named_cc=${targetPrefix}clang
|
|
|
|
|
export named_cxx=${targetPrefix}clang++
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [ -e $ccPath/${targetPrefix}g++ ]; then
|
|
|
|
|
wrap ${targetPrefix}g++ $wrapper $ccPath/${targetPrefix}g++
|
|
|
|
|
ln -s ${targetPrefix}g++ $out/bin/${targetPrefix}c++
|
|
|
|
|
elif [ -e $ccPath/clang++ ]; then
|
|
|
|
|
wrap ${targetPrefix}clang++ $wrapper $ccPath/clang++
|
|
|
|
|
ln -s ${targetPrefix}clang++ $out/bin/${targetPrefix}c++
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [ -e $ccPath/cpp ]; then
|
|
|
|
|
wrap ${targetPrefix}cpp $wrapper $ccPath/cpp
|
|
|
|
|
fi
|
|
|
|
|
''
|
|
|
|
|
|
|
|
|
|
+ optionalString cc.langAda or false ''
|
|
|
|
|
wrap ${targetPrefix}gnatmake ${./gnat-wrapper.sh} $ccPath/${targetPrefix}gnatmake
|
|
|
|
|
wrap ${targetPrefix}gnatbind ${./gnat-wrapper.sh} $ccPath/${targetPrefix}gnatbind
|
|
|
|
|
wrap ${targetPrefix}gnatlink ${./gnat-wrapper.sh} $ccPath/${targetPrefix}gnatlink
|
2021-08-05 21:33:18 +00:00
|
|
|
|
|
|
|
|
|
# this symlink points to the unwrapped gnat's output "out". It is used by
|
|
|
|
|
# our custom gprconfig compiler description to find GNAT's ada runtime. See
|
|
|
|
|
# ../../development/tools/build-managers/gprbuild/{boot.nix, nixpkgs-gnat.xml}
|
|
|
|
|
ln -sf ${cc} $out/nix-support/gprconfig-gnat-unwrapped
|
2020-04-24 23:36:52 +00:00
|
|
|
|
''
|
|
|
|
|
|
2020-05-29 06:06:01 +00:00
|
|
|
|
+ optionalString cc.langD or false ''
|
|
|
|
|
wrap ${targetPrefix}gdc $wrapper $ccPath/${targetPrefix}gdc
|
|
|
|
|
''
|
|
|
|
|
|
2022-09-30 11:47:45 +00:00
|
|
|
|
+ optionalString cc.langFortran or false ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
wrap ${targetPrefix}gfortran $wrapper $ccPath/${targetPrefix}gfortran
|
|
|
|
|
ln -sv ${targetPrefix}gfortran $out/bin/${targetPrefix}g77
|
|
|
|
|
ln -sv ${targetPrefix}gfortran $out/bin/${targetPrefix}f77
|
2020-09-25 04:45:31 +00:00
|
|
|
|
export named_fc=${targetPrefix}gfortran
|
2020-04-24 23:36:52 +00:00
|
|
|
|
''
|
|
|
|
|
|
|
|
|
|
+ optionalString cc.langJava or false ''
|
|
|
|
|
wrap ${targetPrefix}gcj $wrapper $ccPath/${targetPrefix}gcj
|
|
|
|
|
''
|
|
|
|
|
|
|
|
|
|
+ optionalString cc.langGo or false ''
|
|
|
|
|
wrap ${targetPrefix}gccgo $wrapper $ccPath/${targetPrefix}gccgo
|
|
|
|
|
'';
|
|
|
|
|
|
|
|
|
|
strictDeps = true;
|
2020-05-29 06:06:01 +00:00
|
|
|
|
propagatedBuildInputs = [ bintools ] ++ extraTools ++ optionals cc.langD or false [ zlib ];
|
2020-07-18 16:06:22 +00:00
|
|
|
|
depsTargetTargetPropagated = optional (libcxx != null) libcxx ++ extraPackages;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
|
wrapperName = "CC_WRAPPER";
|
|
|
|
|
|
|
|
|
|
setupHooks = [
|
|
|
|
|
../setup-hooks/role.bash
|
2022-11-02 22:02:43 +00:00
|
|
|
|
] ++ lib.optional (cc.langC or true) ./setup-hook.sh
|
2021-02-05 17:12:51 +00:00
|
|
|
|
++ lib.optional (cc.langFortran or false) ./fortran-hook.sh;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
|
postFixup =
|
2020-07-18 16:06:22 +00:00
|
|
|
|
# Ensure flags files exists, as some other programs cat them. (That these
|
|
|
|
|
# are considered an exposed interface is a bit dubious, but fine for now.)
|
|
|
|
|
''
|
|
|
|
|
touch "$out/nix-support/cc-cflags"
|
|
|
|
|
touch "$out/nix-support/cc-ldflags"
|
2020-04-24 23:36:52 +00:00
|
|
|
|
''
|
2020-07-18 16:06:22 +00: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.
|
|
|
|
|
+ ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
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
|
|
|
|
|
''
|
|
|
|
|
|
2020-07-18 16:06:22 +00:00
|
|
|
|
##
|
|
|
|
|
## GCC libs for non-GCC support
|
|
|
|
|
##
|
2020-09-25 04:45:31 +00:00
|
|
|
|
+ optionalString useGccForLibs ''
|
2020-07-18 16:06:22 +00:00
|
|
|
|
|
2020-08-20 17:08:02 +00:00
|
|
|
|
echo "-B${gccForLibs}/lib/gcc/${targetPlatform.config}/${gccForLibs.version}" >> $out/nix-support/cc-cflags
|
|
|
|
|
echo "-L${gccForLibs}/lib/gcc/${targetPlatform.config}/${gccForLibs.version}" >> $out/nix-support/cc-ldflags
|
|
|
|
|
echo "-L${gccForLibs.lib}/${targetPlatform.config}/lib" >> $out/nix-support/cc-ldflags
|
2020-07-18 16:06:22 +00:00
|
|
|
|
''
|
|
|
|
|
|
2021-02-05 17:12:51 +00:00
|
|
|
|
# TODO We would like to connect this to `useGccForLibs`, but we cannot yet
|
|
|
|
|
# because `libcxxStdenv` on linux still needs this. Maybe someday we'll
|
|
|
|
|
# always set `useLLVM` on Darwin, and maybe also break down `useLLVM` into
|
|
|
|
|
# fine-grained use flags (libgcc vs compiler-rt, ld.lld vs legacy, libc++
|
|
|
|
|
# vs libstdc++, etc.) since Darwin isn't `useLLVM` on all counts. (See
|
|
|
|
|
# https://clang.llvm.org/docs/Toolchain.html for all the axes one might
|
|
|
|
|
# break `useLLVM` into.)
|
2021-04-12 18:23:04 +00:00
|
|
|
|
+ optionalString (isClang
|
2021-03-09 03:18:52 +00:00
|
|
|
|
&& targetPlatform.isLinux
|
|
|
|
|
&& !(stdenv.targetPlatform.useAndroidPrebuilt or false)
|
2021-04-12 18:23:04 +00:00
|
|
|
|
&& !(stdenv.targetPlatform.useLLVM or false)
|
|
|
|
|
&& gccForLibs != null) ''
|
2021-02-05 17:12:51 +00:00
|
|
|
|
echo "--gcc-toolchain=${gccForLibs}" >> $out/nix-support/cc-cflags
|
|
|
|
|
''
|
|
|
|
|
|
2020-07-18 16:06:22 +00: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.
|
2020-04-24 23:36:52 +00:00
|
|
|
|
+ optionalString (libc != null) (''
|
2020-07-18 16:06:22 +00:00
|
|
|
|
touch "$out/nix-support/libc-cflags"
|
|
|
|
|
touch "$out/nix-support/libc-ldflags"
|
2020-08-20 17:08:02 +00:00
|
|
|
|
echo "-B${libc_lib}${libc.libdir or "/lib/"}" >> $out/nix-support/libc-crt1-cflags
|
2020-05-29 06:06:01 +00:00
|
|
|
|
'' + optionalString (!(cc.langD or false)) ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
echo "-idirafter ${libc_dev}${libc.incdir or "/include"}" >> $out/nix-support/libc-cflags
|
2020-05-29 06:06:01 +00:00
|
|
|
|
'' + optionalString (isGNU && (!(cc.langD or false))) ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
for dir in "${cc}"/lib/gcc/*/*/include-fixed; do
|
|
|
|
|
echo '-idirafter' ''${dir} >> $out/nix-support/libc-cflags
|
|
|
|
|
done
|
|
|
|
|
'' + ''
|
|
|
|
|
|
|
|
|
|
echo "${libc_lib}" > $out/nix-support/orig-libc
|
|
|
|
|
echo "${libc_dev}" > $out/nix-support/orig-libc-dev
|
|
|
|
|
'')
|
|
|
|
|
|
2020-07-18 16:06:22 +00:00
|
|
|
|
##
|
|
|
|
|
## General libc++ support
|
|
|
|
|
##
|
|
|
|
|
|
|
|
|
|
# We have a libc++ directly, we have one via "smuggled" GCC, or we have one
|
|
|
|
|
# bundled with the C compiler because it is GCC
|
2020-09-25 04:45:31 +00:00
|
|
|
|
+ optionalString (libcxx != null || (useGccForLibs && gccForLibs.langCC or false) || (isGNU && cc.langCC or false)) ''
|
2020-07-18 16:06:22 +00:00
|
|
|
|
touch "$out/nix-support/libcxx-cxxflags"
|
|
|
|
|
touch "$out/nix-support/libcxx-ldflags"
|
2020-08-20 17:08:02 +00:00
|
|
|
|
''
|
2020-09-25 04:45:31 +00:00
|
|
|
|
+ optionalString (libcxx == null && (useGccForLibs && gccForLibs.langCC or false)) ''
|
2022-12-02 08:20:57 +00:00
|
|
|
|
for dir in ${gccForLibs}${lib.optionalString (hostPlatform != targetPlatform) "/${targetPlatform.config}"}/include/c++/*; do
|
2020-07-18 16:06:22 +00:00
|
|
|
|
echo "-isystem $dir" >> $out/nix-support/libcxx-cxxflags
|
|
|
|
|
done
|
2022-12-02 08:20:57 +00:00
|
|
|
|
for dir in ${gccForLibs}${lib.optionalString (hostPlatform != targetPlatform) "/${targetPlatform.config}"}/include/c++/*/${targetPlatform.config}; do
|
2020-07-18 16:06:22 +00:00
|
|
|
|
echo "-isystem $dir" >> $out/nix-support/libcxx-cxxflags
|
|
|
|
|
done
|
|
|
|
|
''
|
|
|
|
|
+ optionalString (libcxx.isLLVM or false) (''
|
2021-05-20 23:08:51 +00:00
|
|
|
|
echo "-isystem ${lib.getDev libcxx}/include/c++/v1" >> $out/nix-support/libcxx-cxxflags
|
2020-07-18 16:06:22 +00:00
|
|
|
|
echo "-stdlib=libc++" >> $out/nix-support/libcxx-ldflags
|
|
|
|
|
echo "-lc++abi" >> $out/nix-support/libcxx-ldflags
|
|
|
|
|
'')
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
|
## 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.
|
2020-04-24 23:36:52 +00:00
|
|
|
|
+ optionalString (!nativeTools) ''
|
|
|
|
|
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"
|
|
|
|
|
|
|
|
|
|
'' + optionalString cc.langAda or false ''
|
2020-07-18 16:06:22 +00:00
|
|
|
|
touch "$out/nix-support/gnat-cflags"
|
|
|
|
|
touch "$out/nix-support/gnat-ldflags"
|
2020-04-24 23:36:52 +00:00
|
|
|
|
basePath=$(echo $cc/lib/*/*/*)
|
|
|
|
|
ccCFlags+=" -B$basePath -I$basePath/adainclude"
|
|
|
|
|
gnatCFlags="-I$basePath/adainclude -I$basePath/adalib"
|
|
|
|
|
|
2020-07-18 16:06:22 +00:00
|
|
|
|
echo "$gnatCFlags" >> $out/nix-support/gnat-cflags
|
2020-04-24 23:36:52 +00:00
|
|
|
|
'' + ''
|
2020-07-18 16:06:22 +00:00
|
|
|
|
echo "$ccLDFlags" >> $out/nix-support/cc-ldflags
|
|
|
|
|
echo "$ccCFlags" >> $out/nix-support/cc-cflags
|
2020-04-24 23:36:52 +00:00
|
|
|
|
'' + optionalString (targetPlatform.isDarwin && (libcxx != null) && (cc.isClang or false)) ''
|
2021-05-20 23:08:51 +00:00
|
|
|
|
echo " -L${lib.getLib libcxx}/lib" >> $out/nix-support/cc-ldflags
|
2020-07-18 16:06:22 +00:00
|
|
|
|
''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
2020-07-18 16:06:22 +00:00
|
|
|
|
##
|
|
|
|
|
## Man page and info support
|
|
|
|
|
##
|
|
|
|
|
+ optionalString propagateDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
ln -s ${cc.man} $man
|
|
|
|
|
ln -s ${cc.info} $info
|
2020-05-29 06:06:01 +00:00
|
|
|
|
'' + optionalString (cc.langD or false) ''
|
|
|
|
|
echo "-B${zlib}${zlib.libdir or "/lib/"}" >> $out/nix-support/libc-cflags
|
2020-04-24 23:36:52 +00:00
|
|
|
|
''
|
|
|
|
|
|
2020-07-18 16:06:22 +00:00
|
|
|
|
##
|
|
|
|
|
## Hardening support
|
|
|
|
|
##
|
2020-04-24 23:36:52 +00:00
|
|
|
|
+ ''
|
|
|
|
|
export hardening_unsupported_flags="${builtins.concatStringsSep " " (cc.hardeningUnsupportedFlags or [])}"
|
|
|
|
|
''
|
|
|
|
|
|
|
|
|
|
# Machine flags. These are necessary to support
|
|
|
|
|
|
|
|
|
|
# TODO: We should make a way to support miscellaneous machine
|
|
|
|
|
# flags and other gcc flags as well.
|
|
|
|
|
|
|
|
|
|
# Always add -march based on cpu in triple. Sometimes there is a
|
|
|
|
|
# discrepency (x86_64 vs. x86-64), so we provide an "arch" arg in
|
|
|
|
|
# that case.
|
2021-05-28 09:39:13 +00:00
|
|
|
|
# TODO: aarch64-darwin has mcpu incompatible with gcc
|
|
|
|
|
+ optionalString ((targetPlatform ? gcc.arch) && (isClang || !(stdenv.isDarwin && stdenv.isAarch64)) &&
|
2021-02-05 17:12:51 +00:00
|
|
|
|
isGccArchSupported targetPlatform.gcc.arch) ''
|
|
|
|
|
echo "-march=${targetPlatform.gcc.arch}" >> $out/nix-support/cc-cflags-before
|
2020-04-24 23:36:52 +00:00
|
|
|
|
''
|
|
|
|
|
|
|
|
|
|
# -mcpu is not very useful. You should use mtune and march
|
|
|
|
|
# instead. It’s provided here for backwards compatibility.
|
2021-05-28 09:39:13 +00:00
|
|
|
|
# TODO: aarch64-darwin has mcpu incompatible with gcc
|
|
|
|
|
+ optionalString ((targetPlatform ? gcc.cpu) && (isClang || !(stdenv.isDarwin && stdenv.isAarch64))) ''
|
2021-02-05 17:12:51 +00:00
|
|
|
|
echo "-mcpu=${targetPlatform.gcc.cpu}" >> $out/nix-support/cc-cflags-before
|
2020-04-24 23:36:52 +00:00
|
|
|
|
''
|
|
|
|
|
|
|
|
|
|
# -mfloat-abi only matters on arm32 but we set it here
|
|
|
|
|
# unconditionally just in case. If the abi specifically sets hard
|
|
|
|
|
# vs. soft floats we use it here.
|
2021-02-05 17:12:51 +00:00
|
|
|
|
+ optionalString (targetPlatform ? gcc.float-abi) ''
|
|
|
|
|
echo "-mfloat-abi=${targetPlatform.gcc.float-abi}" >> $out/nix-support/cc-cflags-before
|
2020-04-24 23:36:52 +00:00
|
|
|
|
''
|
2021-02-05 17:12:51 +00:00
|
|
|
|
+ optionalString (targetPlatform ? gcc.fpu) ''
|
|
|
|
|
echo "-mfpu=${targetPlatform.gcc.fpu}" >> $out/nix-support/cc-cflags-before
|
2020-04-24 23:36:52 +00:00
|
|
|
|
''
|
2021-02-05 17:12:51 +00:00
|
|
|
|
+ optionalString (targetPlatform ? gcc.mode) ''
|
|
|
|
|
echo "-mmode=${targetPlatform.gcc.mode}" >> $out/nix-support/cc-cflags-before
|
2020-04-24 23:36:52 +00:00
|
|
|
|
''
|
2021-09-18 10:52:07 +00:00
|
|
|
|
+ optionalString (targetPlatform ? gcc.thumb) ''
|
|
|
|
|
echo "-m${if targetPlatform.gcc.thumb then "thumb" else "arm"}" >> $out/nix-support/cc-cflags-before
|
|
|
|
|
''
|
2021-02-05 17:12:51 +00:00
|
|
|
|
+ optionalString (targetPlatform ? gcc.tune &&
|
|
|
|
|
isGccArchSupported targetPlatform.gcc.tune) ''
|
|
|
|
|
echo "-mtune=${targetPlatform.gcc.tune}" >> $out/nix-support/cc-cflags-before
|
2020-04-24 23:36:52 +00:00
|
|
|
|
''
|
|
|
|
|
|
|
|
|
|
# TODO: categorize these and figure out a better place for them
|
|
|
|
|
+ optionalString hostPlatform.isCygwin ''
|
|
|
|
|
hardening_unsupported_flags+=" pic"
|
|
|
|
|
'' + optionalString targetPlatform.isMinGW ''
|
2021-08-12 14:41:47 +00:00
|
|
|
|
hardening_unsupported_flags+=" stackprotector fortify"
|
2020-04-24 23:36:52 +00:00
|
|
|
|
'' + optionalString targetPlatform.isAvr ''
|
|
|
|
|
hardening_unsupported_flags+=" stackprotector pic"
|
2022-08-12 12:06:08 +00:00
|
|
|
|
'' + optionalString (targetPlatform.libc == "newlib" || targetPlatform.libc == "newlib-nano") ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
hardening_unsupported_flags+=" stackprotector fortify pie pic"
|
2021-03-09 03:18:52 +00:00
|
|
|
|
'' + optionalString (targetPlatform.libc == "musl" && targetPlatform.isx86_32) ''
|
|
|
|
|
hardening_unsupported_flags+=" stackprotector"
|
2020-04-24 23:36:52 +00:00
|
|
|
|
'' + optionalString targetPlatform.isNetBSD ''
|
|
|
|
|
hardening_unsupported_flags+=" stackprotector fortify"
|
|
|
|
|
'' + optionalString cc.langAda or false ''
|
2020-12-25 13:55:36 +00:00
|
|
|
|
hardening_unsupported_flags+=" format stackprotector strictoverflow"
|
2020-05-29 06:06:01 +00:00
|
|
|
|
'' + optionalString cc.langD or false ''
|
|
|
|
|
hardening_unsupported_flags+=" format"
|
2022-06-26 10:26:21 +00:00
|
|
|
|
'' + optionalString cc.langFortran or false ''
|
|
|
|
|
hardening_unsupported_flags+=" format"
|
2020-05-29 06:06:01 +00:00
|
|
|
|
'' + optionalString targetPlatform.isWasm ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
hardening_unsupported_flags+=" stackprotector fortify pie pic"
|
2022-09-09 14:08:57 +00:00
|
|
|
|
'' + optionalString targetPlatform.isMicroBlaze ''
|
|
|
|
|
hardening_unsupported_flags+=" stackprotector"
|
2020-04-24 23:36:52 +00:00
|
|
|
|
''
|
|
|
|
|
|
|
|
|
|
+ optionalString (libc != null && targetPlatform.isAvr) ''
|
|
|
|
|
for isa in avr5 avr3 avr4 avr6 avr25 avr31 avr35 avr51 avrxmega2 avrxmega4 avrxmega5 avrxmega6 avrxmega7 tiny-stack; do
|
2020-08-20 17:08:02 +00:00
|
|
|
|
echo "-B${getLib libc}/avr/lib/$isa" >> $out/nix-support/libc-crt1-cflags
|
2020-04-24 23:36:52 +00:00
|
|
|
|
done
|
|
|
|
|
''
|
|
|
|
|
|
2021-04-26 19:14:03 +00:00
|
|
|
|
+ optionalString stdenv.targetPlatform.isDarwin ''
|
|
|
|
|
echo "-arch ${targetPlatform.darwinArch}" >> $out/nix-support/cc-cflags
|
2021-05-28 09:39:13 +00:00
|
|
|
|
''
|
|
|
|
|
|
|
|
|
|
+ optionalString targetPlatform.isAndroid ''
|
|
|
|
|
echo "-D__ANDROID_API__=${targetPlatform.sdkVer}" >> $out/nix-support/cc-cflags
|
2021-04-26 19:14:03 +00:00
|
|
|
|
''
|
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
|
# There are a few tools (to name one libstdcxx5) which do not work
|
|
|
|
|
# well with multi line flags, so make the flags single line again
|
|
|
|
|
+ ''
|
2020-07-18 16:06:22 +00:00
|
|
|
|
for flags in "$out/nix-support"/*flags*; do
|
|
|
|
|
substituteInPlace "$flags" --replace $'\n' ' '
|
|
|
|
|
done
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
|
substituteAll ${./add-flags.sh} $out/nix-support/add-flags.sh
|
|
|
|
|
substituteAll ${./add-hardening.sh} $out/nix-support/add-hardening.sh
|
|
|
|
|
substituteAll ${../wrapper-common/utils.bash} $out/nix-support/utils.bash
|
|
|
|
|
''
|
|
|
|
|
|
2022-07-14 12:49:19 +00:00
|
|
|
|
##
|
|
|
|
|
## General Clang support
|
|
|
|
|
## Needs to go after ^ because the for loop eats \n and makes this file an invalid script
|
|
|
|
|
##
|
|
|
|
|
+ optionalString isClang ''
|
|
|
|
|
export defaultTarget=${targetPlatform.config}
|
|
|
|
|
substituteAll ${./add-clang-cc-cflags-before.sh} $out/nix-support/add-local-cc-cflags-before.sh
|
|
|
|
|
''
|
|
|
|
|
|
2020-07-18 16:06:22 +00:00
|
|
|
|
##
|
|
|
|
|
## Extra custom steps
|
|
|
|
|
##
|
2022-06-16 17:23:12 +00:00
|
|
|
|
+ extraBuildCommands
|
|
|
|
|
+ lib.strings.concatStringsSep "; "
|
|
|
|
|
(lib.attrsets.mapAttrsToList
|
|
|
|
|
(name: value: "echo ${toString value} >> $out/nix-support/${name}")
|
|
|
|
|
nixSupport);
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# for substitution in utils.bash
|
|
|
|
|
expandResponseParams = "${expand-response-params}/bin/expand-response-params";
|
|
|
|
|
|
|
|
|
|
meta =
|
|
|
|
|
let cc_ = if cc != null then cc else {}; in
|
|
|
|
|
(if cc_ ? meta then removeAttrs cc.meta ["priority"] else {}) //
|
|
|
|
|
{ description =
|
2021-02-05 17:12:51 +00:00
|
|
|
|
lib.attrByPath ["meta" "description"] "System C compiler" cc_
|
2020-04-24 23:36:52 +00:00
|
|
|
|
+ " (wrapper script)";
|
|
|
|
|
priority = 10;
|
|
|
|
|
};
|
|
|
|
|
}
|