Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bintools-wrapper: support lld's -flavor argument #231960

Draft
wants to merge 4 commits into
base: staging
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions pkgs/build-support/bintools-wrapper/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
{ name ? ""
, lib
, stdenvNoCC
, testers ? {}
, bintools ? null, libc ? null, coreutils ? null, shell ? stdenvNoCC.shell, gnugrep ? null
, netbsd ? null, netbsdCross ? null
, sharedLibraryLoader ?
Expand Down Expand Up @@ -100,7 +101,7 @@ let

in

stdenv.mkDerivation {
stdenv.mkDerivation (finalAttrs: {
pname = targetPrefix
+ (if name != "" then name else "${bintoolsName}-wrapper");
version = if bintools == null then "" else bintoolsVersion;
Expand All @@ -113,6 +114,11 @@ stdenv.mkDerivation {
inherit targetPrefix suffixSalt;
inherit bintools libc nativeTools nativeLibc nativePrefix isGNU isLLVM;

tests = import ./tests.nix {
inherit lib stdenvNoCC testers;
inherit (finalAttrs) finalPackage;
};

emacsBufferSetup = pkgs: ''
; We should handle propagation here too
(mapc
Expand Down Expand Up @@ -397,4 +403,4 @@ stdenv.mkDerivation {
} // optionalAttrs useMacosReexportHack {
platforms = lib.platforms.darwin;
};
}
})
23 changes: 19 additions & 4 deletions pkgs/build-support/bintools-wrapper/ld-wrapper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ if [ -z "${NIX_BINTOOLS_WRAPPER_FLAGS_SET_@suffixSalt@:-}" ]; then
source @out@/nix-support/add-flags.sh
fi


# Optionally filter out paths not refering to the store.
extraAfter=()
extraBefore=()
expandResponseParams "$@"

# NIX_LINK_TYPE is set if ld has been called through our cc wrapper. We take
Expand All @@ -33,6 +33,22 @@ else
linkType=$(checkLinkType "${params[@]}")
fi

# `lld` supports specifying the linker flavor (i.e. gnu, link, ld64, wasm, etc)
# via the `-flavor` argument:
# https://github.com/llvm/llvm-project/blob/1ba9ec0d10c8c95216d8d7fc47f028344c1640bb/lld/tools/lld/lld.cpp#L125-L142
#
# However, this must be the *first* argument to `lld`. So, if `-flavor` is the
# first arg in `params`, we hoist it to the front of `extraBefore`:
if [[ ${#params[@]} -ge 2 ]] && [[ ${params[0]} == "-flavor" ]]; then
# As per the link above, only `-flavor <flavor name>` is supported. Not
# `--flavor`, `-flavor=...`, `/flavor:...`, etc.
linkFlavor="${params[1]}"
extraBefore=(-flavor "$linkFlavor" "${extraBefore[@]}")

params=("${params[@]:2}")
fi
Comment on lines +42 to +49
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See previous comment.


# Optionally filter out paths not refering to the store.
if [[ "${NIX_ENFORCE_PURITY:-}" = 1 && -n "${NIX_STORE:-}"
&& ( -z "$NIX_IGNORE_LD_THROUGH_GCC_@suffixSalt@" || -z "${NIX_LINK_TYPE_@suffixSalt@:-}" ) ]]; then
rest=()
Expand Down Expand Up @@ -76,8 +92,7 @@ fi

source @out@/nix-support/add-hardening.sh

extraAfter=()
extraBefore=(${hardeningLDFlags[@]+"${hardeningLDFlags[@]}"})
extraBefore+=(${hardeningLDFlags[@]+"${hardeningLDFlags[@]}"})

if [ -z "${NIX_LINK_TYPE_@suffixSalt@:-}" ]; then
extraAfter+=($(filterRpathFlags "$linkType" $NIX_LDFLAGS_@suffixSalt@))
Expand Down
52 changes: 52 additions & 0 deletions pkgs/build-support/bintools-wrapper/tests.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{ stdenvNoCC
, lib
, finalPackage
, testers
}: let
testVersion = { binaryName }: testers.testVersion {
package = finalPackage;
command = "${binaryName} --version";
};
in lib.optionalAttrs finalPackage.isLLVM {
# `lld` supports specifying the linker flavor with `-flavor` but requires that
# this be the _first_ argument.
flavorArgumentParsed = stdenvNoCC.mkDerivation {
name = "bintools-wrapper-lld-flavor-arg-test";
nativeBuildInputs = [ finalPackage ];
buildCommand = ''
# We expect `-flavor` to be moved before `--help`:
NIX_LDFLAGS_BEFORE="--help" ld -flavor gnu \
| grep "supported targets: elf"

# If `-flavor` isn't the first arg the wrapper should not move it; i.e.
# we expect the below to fail with an error about `-flavor`:
{ ld --gc-sections -flavor gnu || :; } \
|& grep "unknown argument '-flavor'"

# Note that we allow non-zero exit codes for the other linker flavors;
# `ld-wrapper` still injects flags that don't account for the linker
# flavor and so arg parsing fails on some hostPlatforms.

# We should be able to specify the `link` flavor:
{ NIX_LDFLAGS_BEFORE="/help" ld -flavor link || :; } \
| grep "Create a DLL"

# `ld64`:
{ NIX_LDFLAGS_BEFORE="--help" ld -flavor ld64 || :; } \
| grep "macos, ios"
'' + lib.optionalString (lib.versionAtLeast "6.0.0" finalPackage.version) ''
# wasm support was added to lld in v6.0:
# https://github.com/llvm/llvm-project/commit/c94d393ad52b6698b15400ee7a33a68b4bda274b
{ NIX_LDFLAGS_BEFORE="--help" ld -flavor wasm || :; } \
| grep "OVERVIEW: LLVM Linker"
'' + ''
'';
};

# The `lld` package exposes several linker symlinks for its various flavors
# (`wasm-ld`, `ld64.lld`, `ld.lld`, `lld-link`) as well as an `lld` binary but
# at the moment we only expose `ld.lld` (and `ld`, a symlink) so that's all
# we test for here:
ldVersion = testVersion { binaryName = "ld"; };
ldLldVersion = testVersion { binaryName = "ld.lld"; };
}