Skip to content

Commit

Permalink
lib.systems: add various has* flags useful for embedded systems
Browse files Browse the repository at this point in the history
  • Loading branch information
kuruczgy committed Oct 31, 2024
1 parent 0f002f4 commit 4729f93
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 42 deletions.
20 changes: 20 additions & 0 deletions lib/systems/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,26 @@ let
|| isWasm # WASM
) && !isStatic;

# Controls whether libunwind is enabled.
hasStackUnwinding = !(final.isWasm || final.isFreeBSD || final.isDarwin || final.isNone);

# You often don't want excepetions in an embedded setting, disable them by default.
hasExceptions = !(final.isNone || final.isWasm);

# If there is no kernel (`isNone`) we assume no threads. WASM can
# supposedly support threads in certain cases, so this might change in
# the future.
hasThreads = !(final.isNone || final.isWasm);

hasFilesystem = !(final.isNone || final.isWasm);

# Wanting localization in embedded settings is probably a rare edge-case,
# so disable it by default.
hasLocalization = !final.isNone;

# Without a kernel there is no cross-platform way for libc to obtain a clock.
hasMonotonicClock = !final.isNone;

# The difference between `isStatic` and `hasSharedLibraries` is mainly the
# addition of the `staticMarker` (see make-derivation.nix). Some
# platforms, like embedded machines without a libc (e.g. arm-none-eabi)
Expand Down
58 changes: 22 additions & 36 deletions pkgs/development/compilers/llvm/common/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ let
bintools = bintools';
extraPackages =
[ targetLlvmLibraries.compiler-rt ]
++ lib.optionals (!stdenv.targetPlatform.isWasm && !stdenv.targetPlatform.isFreeBSD) [
++ lib.optionals stdenv.targetPlatform.hasStackUnwinding [
targetLlvmLibraries.libunwind
];
extraBuildCommands =
Expand All @@ -745,14 +745,14 @@ let
echo "-rtlib=compiler-rt -Wno-unused-command-line-argument" >> $out/nix-support/cc-cflags
echo "-B${targetLlvmLibraries.compiler-rt}/lib" >> $out/nix-support/cc-cflags
''
+ lib.optionalString (!stdenv.targetPlatform.isWasm) ''
+ lib.optionalString stdenv.targetPlatform.hasStackUnwinding ''
echo "--unwindlib=libunwind" >> $out/nix-support/cc-cflags
echo "-L${targetLlvmLibraries.libunwind}/lib" >> $out/nix-support/cc-ldflags
''
+ lib.optionalString (!stdenv.targetPlatform.isWasm && stdenv.targetPlatform.useLLVM or false) ''
+ lib.optionalString (stdenv.targetPlatform.hasStackUnwinding && stdenv.targetPlatform.useLLVM or false) ''
echo "-lunwind" >> $out/nix-support/cc-ldflags
''
+ lib.optionalString stdenv.targetPlatform.isWasm ''
+ lib.optionalString (!stdenv.targetPlatform.hasExceptions) ''
echo "-fno-exceptions" >> $out/nix-support/cc-cflags
''
)
Expand All @@ -765,18 +765,13 @@ let
"-Wno-unused-command-line-argument"
"-B${targetLlvmLibraries.compiler-rt}/lib"
]
++ lib.optional stdenv.targetPlatform.hasStackUnwinding "--unwindlib=libunwind"
++ lib.optional (
!stdenv.targetPlatform.isWasm && !stdenv.targetPlatform.isFreeBSD
) "--unwindlib=libunwind"
++ lib.optional (
!stdenv.targetPlatform.isWasm
&& !stdenv.targetPlatform.isFreeBSD
stdenv.targetPlatform.hasStackUnwinding
&& stdenv.targetPlatform.useLLVM or false
) "-lunwind"
++ lib.optional stdenv.targetPlatform.isWasm "-fno-exceptions";
nixSupport.cc-ldflags = lib.optionals (
!stdenv.targetPlatform.isWasm && !stdenv.targetPlatform.isFreeBSD
) [ "-L${targetLlvmLibraries.libunwind}/lib" ];
++ lib.optional (!stdenv.targetPlatform.hasExceptions) "-fno-exceptions";
nixSupport.cc-ldflags = lib.optionals stdenv.targetPlatform.hasStackUnwinding [ "-L${targetLlvmLibraries.libunwind}/lib" ];
}
);

Expand All @@ -787,27 +782,23 @@ let
bintools = bintools';
extraPackages =
[ targetLlvmLibraries.compiler-rt-no-libc ]
++ lib.optionals
(
!stdenv.targetPlatform.isWasm && !stdenv.targetPlatform.isFreeBSD && !stdenv.targetPlatform.isDarwin
)
[
targetLlvmLibraries.libunwind
];
++ lib.optionals stdenv.targetPlatform.hasStackUnwinding [
targetLlvmLibraries.libunwind
];
extraBuildCommands =
lib.optionalString (lib.versions.major metadata.release_version == "13") (
''
echo "-rtlib=compiler-rt -Wno-unused-command-line-argument" >> $out/nix-support/cc-cflags
echo "-B${targetLlvmLibraries.compiler-rt-no-libc}/lib" >> $out/nix-support/cc-cflags
''
+ lib.optionalString (!stdenv.targetPlatform.isWasm && !stdenv.targetPlatform.isDarwin) ''
+ lib.optionalString stdenv.targetPlatform.hasStackUnwinding ''
echo "--unwindlib=libunwind" >> $out/nix-support/cc-cflags
echo "-L${targetLlvmLibraries.libunwind}/lib" >> $out/nix-support/cc-ldflags
''
+ lib.optionalString (!stdenv.targetPlatform.isWasm && stdenv.targetPlatform.useLLVM or false) ''
+ lib.optionalString (stdenv.targetPlatform.hasStackUnwinding && stdenv.targetPlatform.useLLVM or false) ''
echo "-lunwind" >> $out/nix-support/cc-ldflags
''
+ lib.optionalString stdenv.targetPlatform.isWasm ''
+ lib.optionalString (!stdenv.targetPlatform.hasExceptions) ''
echo "-fno-exceptions" >> $out/nix-support/cc-cflags
''
)
Expand All @@ -820,18 +811,13 @@ let
"-Wno-unused-command-line-argument"
"-B${targetLlvmLibraries.compiler-rt-no-libc}/lib"
]
++ lib.optional stdenv.targetPlatform.hasStackUnwinding "--unwindlib=libunwind"
++ lib.optional (
!stdenv.targetPlatform.isWasm && !stdenv.targetPlatform.isFreeBSD && !stdenv.targetPlatform.isDarwin
) "--unwindlib=libunwind"
++ lib.optional (
!stdenv.targetPlatform.isWasm
&& !stdenv.targetPlatform.isFreeBSD
stdenv.targetPlatform.hasStackUnwinding
&& stdenv.targetPlatform.useLLVM or false
) "-lunwind"
++ lib.optional stdenv.targetPlatform.isWasm "-fno-exceptions";
nixSupport.cc-ldflags = lib.optionals (
!stdenv.targetPlatform.isWasm && !stdenv.targetPlatform.isFreeBSD && !stdenv.targetPlatform.isDarwin
) [ "-L${targetLlvmLibraries.libunwind}/lib" ];
++ lib.optional (!stdenv.targetPlatform.hasExceptions) "-fno-exceptions";
nixSupport.cc-ldflags = lib.optionals stdenv.targetPlatform.hasStackUnwinding [ "-L${targetLlvmLibraries.libunwind}/lib" ];
}
);

Expand All @@ -857,7 +843,7 @@ let
"-nostdlib++"
]
++ lib.optional (
lib.versionAtLeast metadata.release_version "15" && stdenv.targetPlatform.isWasm
lib.versionAtLeast metadata.release_version "15" && !stdenv.targetPlatform.hasExceptions
) "-fno-exceptions";
}
);
Expand All @@ -882,7 +868,7 @@ let
"-B${targetLlvmLibraries.compiler-rt-no-libc}/lib"
]
++ lib.optional (
lib.versionAtLeast metadata.release_version "15" && stdenv.targetPlatform.isWasm
lib.versionAtLeast metadata.release_version "15" && !stdenv.targetPlatform.hasExceptions
) "-fno-exceptions";
}
);
Expand All @@ -903,7 +889,7 @@ let
nixSupport.cc-cflags =
[ "-nostartfiles" ]
++ lib.optional (
lib.versionAtLeast metadata.release_version "15" && stdenv.targetPlatform.isWasm
lib.versionAtLeast metadata.release_version "15" && !stdenv.targetPlatform.hasExceptions
) "-fno-exceptions";
}
);
Expand All @@ -919,7 +905,7 @@ let
extraBuildCommands = mkExtraBuildCommands0 cc;
}
// lib.optionalAttrs (
lib.versionAtLeast metadata.release_version "15" && stdenv.targetPlatform.isWasm
lib.versionAtLeast metadata.release_version "15" && !stdenv.targetPlatform.hasExceptions
) { nixSupport.cc-cflags = [ "-fno-exceptions" ]; };

# Aliases
Expand Down
19 changes: 13 additions & 6 deletions pkgs/development/compilers/llvm/common/libcxx/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,22 @@ let

cxxabiCMakeFlags = lib.optionals (lib.versionAtLeast release_version "18") [
"-DLIBCXXABI_USE_LLVM_UNWINDER=OFF"
] ++ lib.optionals (useLLVM && !stdenv.hostPlatform.isWasm) (if lib.versionAtLeast release_version "18" then [
] ++ lib.optionals (useLLVM && stdenv.targetPlatform.hasStackUnwinding) (if lib.versionAtLeast release_version "18" then [
"-DLIBCXXABI_ADDITIONAL_LIBRARIES=unwind"
"-DLIBCXXABI_USE_COMPILER_RT=ON"
] else [
"-DLIBCXXABI_USE_COMPILER_RT=ON"
"-DLIBCXXABI_USE_LLVM_UNWINDER=ON"
]) ++ lib.optionals stdenv.hostPlatform.isWasm [
]) ++ lib.optionals (useLLVM && !stdenv.targetPlatform.isWasm) [
"-DLIBCXXABI_USE_COMPILER_RT=ON"
] ++ lib.optionals (!stdenv.targetPlatform.hasThreads) [
"-DLIBCXXABI_ENABLE_THREADS=OFF"
] ++ lib.optionals (!stdenv.targetPlatform.hasExceptions) [
"-DLIBCXXABI_ENABLE_EXCEPTIONS=OFF"
] ++ lib.optionals (!enableShared) [
"-DLIBCXXABI_ENABLE_SHARED=OFF"
] ++ lib.optionals (!stdenv.targetPlatform.hasLocalization) [
"-DLIBCXX_ENABLE_LOCALIZATION=OFF"
] ++ lib.optionals (!stdenv.targetPlatform.hasMonotonicClock) [
"-DLIBCXX_ENABLE_MONOTONIC_CLOCK=OFF"
];

cxxCMakeFlags = [
Expand All @@ -87,9 +92,11 @@ let
"-DLIBCXX_USE_COMPILER_RT=ON"
] ++ lib.optionals (useLLVM && !stdenv.hostPlatform.isFreeBSD && lib.versionAtLeast release_version "16") [
"-DLIBCXX_ADDITIONAL_LIBRARIES=unwind"
] ++ lib.optionals stdenv.hostPlatform.isWasm [
] ++ lib.optionals (!stdenv.targetPlatform.hasThreads) [
"-DLIBCXX_ENABLE_THREADS=OFF"
] ++ lib.optionals (!stdenv.targetPlatform.hasStackUnwinding) [
"-DLIBCXX_ENABLE_FILESYSTEM=OFF"
] ++ lib.optionals (!stdenv.targetPlatform.hasExceptions) [
"-DLIBCXX_ENABLE_EXCEPTIONS=OFF"
] ++ lib.optionals (!enableShared) [
"-DLIBCXX_ENABLE_SHARED=OFF"
Expand Down Expand Up @@ -125,7 +132,7 @@ stdenv.mkDerivation (rec {
++ lib.optional (cxxabi != null) lndir;

buildInputs = [ cxxabi ]
++ lib.optionals (useLLVM && !stdenv.hostPlatform.isWasm && !stdenv.hostPlatform.isFreeBSD) [ libunwind ];
++ lib.optionals (stdenv.targetPlatform.hasStackUnwinding && useLLVM) [ libunwind ];

# libc++.so is a linker script which expands to multiple libraries,
# libc++.so.1 and libc++abi.so or the external cxxabi. ld-wrapper doesn't
Expand Down

0 comments on commit 4729f93

Please sign in to comment.