gcc: support avr

- respect libc’s incdir and libdir
- make non-unix systems single threaded
- set LIMITS_H_TEST to false for avr
- misc updates to support new libc’s
- use multilib with avr

For threads we want to use:
- posix on unix systems
- win32 on windows
- single on everything else

For avr:
- add library directories for avrlibc
- to disable relro and bind
- avr5 should have precedence over avr3 - otherwise gcc uses the wrong one
This commit is contained in:
Matthew Bauer 2018-10-15 16:11:20 -05:00
parent 72e3b2a662
commit 412093994b
11 changed files with 171 additions and 95 deletions

View file

@ -99,9 +99,23 @@ rec {
riscv64 = riscv "64";
riscv32 = riscv "32";
arduino-uno = {
avr = {
config = "avr";
platform = { name = "avr5"; };
};
arm-embedded = {
config = "arm-none-eabi";
libc = "newlib";
};
aarch64-embedded = {
config = "aarch64-none-elf";
libc = "newlib";
};
ppc-embedded = {
config = "powerpc-none-eabi";
libc = "newlib";
};
#

View file

@ -210,7 +210,7 @@ stdenv.mkDerivation {
## General libc support
##
echo "-L${libc_lib}/lib" > $out/nix-support/libc-ldflags
echo "-L${libc_lib}${libc.libdir or "/lib"}" > $out/nix-support/libc-ldflags
echo "${libc_lib}" > $out/nix-support/orig-libc
echo "${libc_dev}" > $out/nix-support/orig-libc-dev
@ -293,6 +293,16 @@ stdenv.mkDerivation {
hardening_unsupported_flags+=" pic"
''
+ optionalString targetPlatform.isAvr ''
hardening_unsupported_flags+=" relro bindnow"
''
+ optionalString (libc != null && targetPlatform.isAvr) ''
for isa in avr5 avr3 avr4 avr6 avr25 avr31 avr35 avr51 avrxmega2 avrxmega4 avrxmega5 avrxmega6 avrxmega7 tiny-stack; do
echo "-L${getLib libc}/avr/lib/$isa" >> $out/nix-support/libc-cflags
done
''
+ ''
set +u
substituteAll ${./add-flags.sh} $out/nix-support/add-flags.sh

View file

@ -232,7 +232,7 @@ stdenv.mkDerivation {
# 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.
echo "-B${libc_lib}/lib/ -idirafter ${libc_dev}/include ${optionalString isGNU "-idirafter ${cc}/lib/gcc/*/*/include-fixed"}" > $out/nix-support/libc-cflags
echo "-B${libc_lib}${libc.libdir or "/lib/"} -idirafter ${libc_dev}${libc.incdir or "/include"} ${optionalString isGNU "-idirafter ${cc}/lib/gcc/*/*/include-fixed"}" > $out/nix-support/libc-cflags
echo "${libc_lib}" > $out/nix-support/orig-libc
echo "${libc_dev}" > $out/nix-support/orig-libc-dev
@ -284,6 +284,20 @@ stdenv.mkDerivation {
hardening_unsupported_flags+=" stackprotector"
''
+ optionalString targetPlatform.isAvr ''
hardening_unsupported_flags+=" stackprotector pic"
''
+ optionalString (targetPlatform.libc == "newlib") ''
hardening_unsupported_flags+=" stackprotector fortify pie pic"
''
+ optionalString (libc != null && targetPlatform.isAvr) ''
for isa in avr5 avr3 avr4 avr6 avr25 avr31 avr35 avr51 avrxmega2 avrxmega4 avrxmega5 avrxmega6 avrxmega7 tiny-stack; do
echo "-B${getLib libc}/avr/lib/$isa" >> $out/nix-support/libc-cflags
done
''
+ ''
substituteAll ${./add-flags.sh} $out/nix-support/add-flags.sh
substituteAll ${./add-hardening.sh} $out/nix-support/add-hardening.sh

View file

@ -130,7 +130,7 @@ let version = "4.8.5";
"--disable-libmpx" # requires libc
] else [
(if crossDarwin then "--with-sysroot=${getLib libcCross}/share/sysroot"
else "--with-headers=${getDev libcCross}/include")
else "--with-headers=${getDev libcCross}${libcCross.incdir or "/include"}")
"--enable-__cxa_atexit"
"--enable-long-long"
] ++
@ -148,10 +148,15 @@ let version = "4.8.5";
# In uclibc cases, libgomp needs an additional '-ldl'
# and as I don't know how to pass it, I disable libgomp.
"--disable-libgomp"
] ++ [
"--enable-threads=posix"
"--enable-nls"
"--disable-decimal-float" # No final libdecnumber (it may work only in 386)
]
++ optional (targetPlatform.libc == "newlib") "--with-newlib"
++ optional (targetPlatform.libc == "avrlibc") "--with-avrlibc"
++ [
"--enable-threads=${if targetPlatform.isUnix then "posix"
else if targetPlatform.isWindows then "win32"
else "single"}"
"--enable-nls"
"--disable-decimal-float" # No final libdecnumber (it may work only in 386)
]));
stageNameAddon = if crossStageStatic then "-stage-static" else "-stage-final";
crossNameAddon = if targetPlatform != hostPlatform then "${targetPlatform.config}${stageNameAddon}-" else "";
@ -270,7 +275,7 @@ stdenv.mkDerivation ({
}"
] ++
(if enableMultilib
(if (enableMultilib || targetPlatform.isAvr)
then ["--enable-multilib" "--disable-libquadmath"]
else ["--disable-multilib"]) ++
optional (!enableShared) "--disable-shared" ++
@ -360,20 +365,20 @@ stdenv.mkDerivation ({
EXTRA_TARGET_FLAGS = optionals
(targetPlatform != hostPlatform && libcCross != null)
([
"-idirafter ${libcCross.dev}/include"
"-idirafter ${getDev libcCross}${libcCross.incdir or "/include"}"
] ++ optionals (! crossStageStatic) [
"-B${libcCross.out}/lib"
"-B${libcCross.out}${libcCross.libdir or "/lib"}"
]);
EXTRA_TARGET_LDFLAGS = optionals
(targetPlatform != hostPlatform && libcCross != null)
([
"-Wl,-L${libcCross.out}/lib"
"-Wl,-L${libcCross.out}${libcCross.libdir or "/lib"}"
] ++ (if crossStageStatic then [
"-B${libcCross.out}/lib"
"-B${libcCross.out}${libcCross.libdir or "/lib"}"
] else [
"-Wl,-rpath,${libcCross.out}/lib"
"-Wl,-rpath-link,${libcCross.out}/lib"
"-Wl,-rpath,${libcCross.out}${libcCross.libdir or "/lib"}"
"-Wl,-rpath-link,${libcCross.out}${libcCross.libdir or "/lib"}"
]));
passthru = {

View file

@ -135,7 +135,7 @@ let version = "4.9.4";
"--disable-libmpx" # requires libc
] else [
(if crossDarwin then "--with-sysroot=${getLib libcCross}/share/sysroot"
else "--with-headers=${getDev libcCross}/include")
else "--with-headers=${getDev libcCross}${libcCross.incdir or "/include"}")
"--enable-__cxa_atexit"
"--enable-long-long"
] ++
@ -156,10 +156,15 @@ let version = "4.9.4";
# In uclibc cases, libgomp needs an additional '-ldl'
# and as I don't know how to pass it, I disable libgomp.
"--disable-libgomp"
] ++ [
"--enable-threads=posix"
"--enable-nls"
"--disable-decimal-float" # No final libdecnumber (it may work only in 386)
]
++ optional (targetPlatform.libc == "newlib") "--with-newlib"
++ optional (targetPlatform.libc == "avrlibc") "--with-avrlibc"
++ [
"--enable-threads=${if targetPlatform.isUnix then "posix"
else if targetPlatform.isWindows then "win32"
else "single"}"
"--enable-nls"
"--disable-decimal-float" # No final libdecnumber (it may work only in 386)
]));
stageNameAddon = if crossStageStatic then "-stage-static" else "-stage-final";
crossNameAddon = if targetPlatform != hostPlatform then "${targetPlatform.config}${stageNameAddon}-" else "";
@ -292,7 +297,7 @@ stdenv.mkDerivation ({
}"
] ++
(if enableMultilib
(if (enableMultilib || targetPlatform.isAvr)
then ["--enable-multilib" "--disable-libquadmath"]
else ["--disable-multilib"]) ++
optional (!enableShared) "--disable-shared" ++
@ -381,20 +386,20 @@ stdenv.mkDerivation ({
EXTRA_TARGET_FLAGS = optionals
(targetPlatform != hostPlatform && libcCross != null)
([
"-idirafter ${getDev libcCross}/include"
"-idirafter ${getDev libcCross}${libcCross.incdir or "/include"}"
] ++ optionals (! crossStageStatic) [
"-B${libcCross.out}/lib"
"-B${libcCross.out}${libcCross.libdir or "/lib"}"
]);
EXTRA_TARGET_LDFLAGS = optionals
(targetPlatform != hostPlatform && libcCross != null)
([
"-Wl,-L${libcCross.out}/lib"
"-Wl,-L${libcCross.out}${libcCross.libdir or "/lib"}"
] ++ (if crossStageStatic then [
"-B${libcCross.out}/lib"
"-B${libcCross.out}${libcCross.libdir or "/lib"}"
] else [
"-Wl,-rpath,${libcCross.out}/lib"
"-Wl,-rpath-link,${libcCross.out}/lib"
"-Wl,-rpath,${libcCross.out}${libcCross.libdir or "/lib"}"
"-Wl,-rpath-link,${libcCross.out}${libcCross.libdir or "/lib"}"
]));
passthru =

View file

@ -122,7 +122,7 @@ let version = "5.5.0";
"--disable-libmpx" # requires libc
] else [
(if crossDarwin then "--with-sysroot=${getLib libcCross}/share/sysroot"
else "--with-headers=${getDev libcCross}/include")
else "--with-headers=${getDev libcCross}${libcCross.incdir or "/include"}")
"--enable-__cxa_atexit"
"--enable-long-long"
] ++
@ -143,10 +143,15 @@ let version = "5.5.0";
# In uclibc cases, libgomp needs an additional '-ldl'
# and as I don't know how to pass it, I disable libgomp.
"--disable-libgomp"
] ++ [
"--enable-threads=posix"
"--enable-nls"
"--disable-decimal-float" # No final libdecnumber (it may work only in 386)
]
++ optional (targetPlatform.libc == "newlib") "--with-newlib"
++ optional (targetPlatform.libc == "avrlibc") "--with-avrlibc"
++ [
"--enable-threads=${if targetPlatform.isUnix then "posix"
else if targetPlatform.isWindows then "win32"
else "single"}"
"--enable-nls"
"--disable-decimal-float" # No final libdecnumber (it may work only in 386)
]));
stageNameAddon = if crossStageStatic then "-stage-static" else "-stage-final";
crossNameAddon = if targetPlatform != hostPlatform then "${targetPlatform.config}${stageNameAddon}-" else "";
@ -296,7 +301,7 @@ stdenv.mkDerivation ({
}"
] ++
(if enableMultilib
(if (enableMultilib || targetPlatform.isAvr)
then ["--enable-multilib" "--disable-libquadmath"]
else ["--disable-multilib"]) ++
optional (!enableShared) "--disable-shared" ++
@ -387,20 +392,20 @@ stdenv.mkDerivation ({
EXTRA_TARGET_FLAGS = optionals
(targetPlatform != hostPlatform && libcCross != null)
([
"-idirafter ${getDev libcCross}/include"
"-idirafter ${getDev libcCross}${libcCross.incdir or "/include"}"
] ++ optionals (! crossStageStatic) [
"-B${libcCross.out}/lib"
"-B${libcCross.out}${libcCross.libdir or "/lib"}"
]);
EXTRA_TARGET_LDFLAGS = optionals
(targetPlatform != hostPlatform && libcCross != null)
([
"-Wl,-L${libcCross.out}/lib"
"-Wl,-L${libcCross.out}${libcCross.libdir or "/lib"}"
] ++ (if crossStageStatic then [
"-B${libcCross.out}/lib"
"-B${libcCross.out}${libcCross.libdir or "/lib"}"
] else [
"-Wl,-rpath,${libcCross.out}/lib"
"-Wl,-rpath-link,${libcCross.out}/lib"
"-Wl,-rpath,${libcCross.out}${libcCross.libdir or "/lib"}"
"-Wl,-rpath-link,${libcCross.out}${libcCross.libdir or "/lib"}"
]));
passthru =

View file

@ -120,7 +120,7 @@ let version = "6.4.0";
"--disable-libmpx" # requires libc
] else [
(if crossDarwin then "--with-sysroot=${getLib libcCross}/share/sysroot"
else "--with-headers=${getDev libcCross}/include")
else "--with-headers=${getDev libcCross}${libcCross.incdir or "/include"}")
"--enable-__cxa_atexit"
"--enable-long-long"
] ++
@ -143,10 +143,15 @@ let version = "6.4.0";
"--disable-libgomp"
# musl at least, disable: https://git.buildroot.net/buildroot/commit/?id=873d4019f7fb00f6a80592224236b3ba7d657865
"--disable-libmpx"
] ++ [
"--enable-threads=posix"
"--enable-nls"
"--disable-decimal-float" # No final libdecnumber (it may work only in 386)
]
++ optional (targetPlatform.libc == "newlib") "--with-newlib"
++ optional (targetPlatform.libc == "avrlibc") "--with-avrlibc"
++ [
"--enable-threads=${if targetPlatform.isUnix then "posix"
else if targetPlatform.isWindows then "win32"
else "single"}"
"--enable-nls"
"--disable-decimal-float" # No final libdecnumber (it may work only in 386)
]));
stageNameAddon = if crossStageStatic then "-stage-static" else "-stage-final";
crossNameAddon = if targetPlatform != hostPlatform then "${targetPlatform.config}${stageNameAddon}-" else "";
@ -301,7 +306,7 @@ stdenv.mkDerivation ({
}"
] ++
(if enableMultilib
(if (enableMultilib || targetPlatform.isAvr)
then ["--enable-multilib" "--disable-libquadmath"]
else ["--disable-multilib"]) ++
optional (!enableShared) "--disable-shared" ++
@ -391,20 +396,20 @@ stdenv.mkDerivation ({
EXTRA_TARGET_FLAGS = optionals
(targetPlatform != hostPlatform && libcCross != null)
([
"-idirafter ${getDev libcCross}/include"
"-idirafter ${getDev libcCross}${libcCross.incdir or "/include"}"
] ++ optionals (! crossStageStatic) [
"-B${libcCross.out}/lib"
"-B${libcCross.out}${libcCross.libdir or "/lib"}"
]);
EXTRA_TARGET_LDFLAGS = optionals
(targetPlatform != hostPlatform && libcCross != null)
([
"-Wl,-L${libcCross.out}/lib"
"-Wl,-L${libcCross.out}${libcCross.libdir or "/lib"}"
] ++ (if crossStageStatic then [
"-B${libcCross.out}/lib"
"-B${libcCross.out}${libcCross.libdir or "/lib"}"
] else [
"-Wl,-rpath,${libcCross.out}/lib"
"-Wl,-rpath-link,${libcCross.out}/lib"
"-Wl,-rpath,${libcCross.out}${libcCross.libdir or "/lib"}"
"-Wl,-rpath-link,${libcCross.out}${libcCross.libdir or "/lib"}"
]));
passthru =

View file

@ -67,7 +67,7 @@ let version = "7.3.0";
[ "--with-as=${targetPackages.stdenv.cc.bintools}/bin/${targetPlatform.config}-as"
"--with-ld=${targetPackages.stdenv.cc.bintools}/bin/${targetPlatform.config}-ld" ] ++
(if crossMingw && crossStageStatic then [
"--with-headers=${libcCross}/include"
"--with-headers=${getDev libcCross}${libcCross.incdir or "/include"}"
"--with-gcc"
"--with-gnu-as"
"--with-gnu-ld"
@ -92,7 +92,7 @@ let version = "7.3.0";
"--disable-libmpx" # requires libc
] else [
(if crossDarwin then "--with-sysroot=${getLib libcCross}/share/sysroot"
else "--with-headers=${getDev libcCross}/include")
else "--with-headers=${getDev libcCross}${libcCross.incdir or "/include"}")
"--enable-__cxa_atexit"
"--enable-long-long"
] ++
@ -115,11 +115,17 @@ let version = "7.3.0";
"--disable-libgomp"
# musl at least, disable: https://git.buildroot.net/buildroot/commit/?id=873d4019f7fb00f6a80592224236b3ba7d657865
"--disable-libmpx"
] ++ [
"--enable-threads=posix"
"--enable-nls"
"--disable-decimal-float" # No final libdecnumber (it may work only in 386)
]));
]
++ optional (targetPlatform.libc == "newlib") "--with-newlib"
++ optional (targetPlatform.libc == "avrlibc") "--with-avrlibc"
++ [
"--enable-threads=${if targetPlatform.isUnix then "posix"
else if targetPlatform.isWindows then "win32"
else "single"}"
"--enable-nls"
# No final libdecnumber (it may work only in 386)
"--disable-decimal-float"
]));
stageNameAddon = if crossStageStatic then "-stage-static" else "-stage-final";
crossNameAddon = if targetPlatform != hostPlatform then "${targetPlatform.config}${stageNameAddon}-" else "";
@ -188,7 +194,12 @@ stdenv.mkDerivation ({
sed -i gcc/config/linux.h -e '1i#undef LOCAL_INCLUDE_DIR'
''
)
else "");
else "")
+ stdenv.lib.optionalString targetPlatform.isAvr ''
makeFlagsArray+=(
'LIMITS_H_TEST=false'
)
'';
inherit noSysDirs staticCompiler crossStageStatic
libcCross crossMingw;
@ -267,7 +278,7 @@ stdenv.mkDerivation ({
}"
] ++
(if enableMultilib
(if (enableMultilib || targetPlatform.isAvr)
then ["--enable-multilib" "--disable-libquadmath"]
else ["--disable-multilib"]) ++
optional (!enableShared) "--disable-shared" ++
@ -334,20 +345,20 @@ stdenv.mkDerivation ({
EXTRA_TARGET_FLAGS = optionals
(targetPlatform != hostPlatform && libcCross != null)
([
"-idirafter ${getDev libcCross}/include"
"-idirafter ${getDev libcCross}${libcCross.incdir or "/include"}"
] ++ optionals (! crossStageStatic) [
"-B${libcCross.out}/lib"
"-B${libcCross.out}${libcCross.libdir or "/lib"}"
]);
EXTRA_TARGET_LDFLAGS = optionals
(targetPlatform != hostPlatform && libcCross != null)
([
"-Wl,-L${libcCross.out}/lib"
"-Wl,-L${libcCross.out}${libcCross.libdir or "/lib"}"
] ++ (if crossStageStatic then [
"-B${libcCross.out}/lib"
"-B${libcCross.out}${libcCross.libdir or "/lib"}"
] else [
"-Wl,-rpath,${libcCross.out}/lib"
"-Wl,-rpath-link,${libcCross.out}/lib"
"-Wl,-rpath,${libcCross.out}${libcCross.libdir or "/lib"}"
"-Wl,-rpath-link,${libcCross.out}${libcCross.libdir or "/lib"}"
]));
passthru =

View file

@ -87,7 +87,7 @@ let version = "8.2.0";
"--disable-libmpx" # requires libc
] else [
(if crossDarwin then "--with-sysroot=${getLib libcCross}/share/sysroot"
else "--with-headers=${getDev libcCross}/include")
else "--with-headers=${getDev libcCross}${libcCross.incdir or "/include"}")
"--enable-__cxa_atexit"
"--enable-long-long"
] ++
@ -110,10 +110,15 @@ let version = "8.2.0";
"--disable-libgomp"
# musl at least, disable: https://git.buildroot.net/buildroot/commit/?id=873d4019f7fb00f6a80592224236b3ba7d657865
"--disable-libmpx"
] ++ [
"--enable-threads=posix"
"--enable-nls"
"--disable-decimal-float" # No final libdecnumber (it may work only in 386)
]
++ optional (targetPlatform.libc == "newlib") "--with-newlib"
++ optional (targetPlatform.libc == "avrlibc") "--with-avrlibc"
++ [
"--enable-threads=${if targetPlatform.isUnix then "posix"
else if targetPlatform.isWindows then "win32"
else "single"}"
"--enable-nls"
"--disable-decimal-float" # No final libdecnumber (it may work only in 386)
]));
stageNameAddon = if crossStageStatic then "-stage-static" else "-stage-final";
crossNameAddon = if targetPlatform != hostPlatform then "-${targetPlatform.config}" + stageNameAddon else "";
@ -261,7 +266,7 @@ stdenv.mkDerivation ({
}"
] ++
(if enableMultilib
(if (enableMultilib || targetPlatform.isAvr)
then ["--enable-multilib" "--disable-libquadmath"]
else ["--disable-multilib"]) ++
optional (!enableShared) "--disable-shared" ++
@ -322,23 +327,16 @@ stdenv.mkDerivation ({
LIBRARY_PATH = optionals (targetPlatform == hostPlatform) (makeLibraryPath (optional (zlib != null) zlib));
EXTRA_TARGET_FLAGS = optionals
(targetPlatform != hostPlatform && libcCross != null)
([
"-idirafter ${getDev libcCross}/include"
] ++ optionals (! crossStageStatic) [
"-B${libcCross.out}/lib"
]);
EXTRA_TARGET_LDFLAGS = optionals
(targetPlatform != hostPlatform && libcCross != null)
([
"-Wl,-L${libcCross.out}/lib"
"-Wl,-L${libcCross.out}${libcCross.libdir or "/lib"}"
] ++ (if crossStageStatic then [
"-B${libcCross.out}/lib"
"-B${libcCross.out}${libcCross.libdir or "/lib"}"
] else [
"-Wl,-rpath,${libcCross.out}/lib"
"-Wl,-rpath-link,${libcCross.out}/lib"
"-Wl,-rpath,${libcCross.out}${libcCross.libdir or "/lib"}"
"-Wl,-rpath-link,${libcCross.out}${libcCross.libdir or "/lib"}"
]));
passthru =

View file

@ -104,10 +104,15 @@ let version = "7-20170409";
# In uclibc cases, libgomp needs an additional '-ldl'
# and as I don't know how to pass it, I disable libgomp.
"--disable-libgomp"
] ++ [
"--enable-threads=posix"
"--enable-nls"
"--disable-decimal-float" # No final libdecnumber (it may work only in 386)
]
++ optional (targetPlatform.libc == "newlib") "--with-newlib"
++ optional (targetPlatform.libc == "avrlibc") "--with-avrlibc"
++ [
"--enable-threads=${if targetPlatform.isUnix then "posix"
else if targetPlatform.isWindows then "win32"
else "single"}"
"--enable-nls"
"--disable-decimal-float" # No final libdecnumber (it may work only in 386)
]));
stageNameAddon = if crossStageStatic then "-stage-static" else "-stage-final";
crossNameAddon = if targetPlatform != hostPlatform then "-${targetPlatform.config}" + stageNameAddon else "";
@ -290,20 +295,20 @@ stdenv.mkDerivation ({
EXTRA_TARGET_FLAGS = optionals
(targetPlatform != hostPlatform && libcCross != null)
([
"-idirafter ${getDev libcCross}/include"
"-idirafter ${getDev libcCross}${libcCross.incdir or "/include"}"
] ++ optionals (! crossStageStatic) [
"-B${libcCross.out}/lib"
"-B${libcCross.out}${libcCross.libdir or "/lib"}"
]);
EXTRA_TARGET_LDFLAGS = optionals
(targetPlatform != hostPlatform && libcCross != null)
([
"-Wl,-L${libcCross.out}/lib"
"-Wl,-L${libcCross.out}${libcCross.libdir or "/lib"}"
] ++ (if crossStageStatic then [
"-B${libcCross.out}/lib"
"-B${libcCross.out}${libcCross.libdir or "/lib"}"
] else [
"-Wl,-rpath,${libcCross.out}/lib"
"-Wl,-rpath-link,${libcCross.out}/lib"
"-Wl,-rpath,${libcCross.out}${libcCross.libdir or "/lib"}"
"-Wl,-rpath-link,${libcCross.out}${libcCross.libdir or "/lib"}"
]));
passthru =

View file

@ -14,9 +14,13 @@ stdenv.mkDerivation {
nativeBuildInputs = [ automake autoconf ];
# Make sure we don't strip the libraries in lib/gcc/avr.
stripDebugList= "bin";
stripDebugList = "bin";
dontPatchELF = true;
passthru = {
incdir = "/avr/include";
};
meta = with stdenv.lib; {
description = "a C runtime library for AVR microcontrollers";
homepage = http://savannah.nongnu.org/projects/avr-libc/;