cygwin-dll-link: improvements from testing on native cygwin (#476301)

This commit is contained in:
John Ericson 2026-02-19 15:59:06 +00:00 committed by GitHub
commit 5a2694bcf3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
24 changed files with 433 additions and 81 deletions

View file

@ -603,7 +603,7 @@ stdenvNoCC.mkDerivation {
installPhase =
if targetPlatform.isCygwin then
''
echo addToSearchPath "LINK_DLL_FOLDERS" "${cc_bin}/lib" >> $out
echo addToSearchPath "_linkDeps_inputPath" "${cc_solib}/bin" >> $out
# Work around build failure caused by the gnulib workaround for
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114870. remove after
# gnulib is updated in core packages (e.g. iconv, gnupatch, gnugrep)

View file

@ -1,77 +0,0 @@
# shellcheck shell=bash
addLinkDLLPaths() {
addToSearchPath "LINK_DLL_FOLDERS" "$1/lib"
addToSearchPath "LINK_DLL_FOLDERS" "$1/bin"
}
# shellcheck disable=SC2154
addEnvHooks "$targetOffset" addLinkDLLPaths
addOutputDLLPaths() {
for output in $(getAllOutputNames); do
addToSearchPath "LINK_DLL_FOLDERS" "${!output}/lib"
addToSearchPath "LINK_DLL_FOLDERS" "${!output}/bin"
done
}
postInstallHooks+=(addOutputDLLPaths)
_dllDeps() {
"$OBJDUMP" -p "$1" \
| sed -n 's/.*DLL Name: \(.*\)/\1/p' \
| sort -u
}
_linkDeps() {
local target="$1" dir="$2" check="$3"
echo 'target:' "$target"
local dll
_dllDeps "$target" | while read -r dll; do
echo ' dll:' "$dll"
if [[ -e "$dir/$dll" ]]; then continue; fi
# Locate the DLL - it should be an *executable* file on $LINK_DLL_FOLDERS.
local dllPath
if ! dllPath="$(PATH="$(dirname "$target"):$LINK_DLL_FOLDERS" type -P "$dll")"; then
if [[ -z "$check" || -n "${allowedImpureDLLsMap[$dll]}" ]]; then
continue
fi
echo unable to find "$dll" in "$LINK_DLL_FOLDERS" >&2
exit 1
fi
echo ' linking to:' "$dllPath"
CYGWIN+=\ winsymlinks:nativestrict ln -sr "$dllPath" "$dir"
# That DLL might have its own (transitive) dependencies,
# so add also all DLLs from its directory to be sure.
_linkDeps "$dllPath" "$dir" ""
done
}
linkDLLs() {
# shellcheck disable=SC2154
if [ ! -d "$prefix" ]; then return; fi
(
set -e
shopt -s globstar nullglob
local -a allowedImpureDLLsArray
concatTo allowedImpureDLLsArray allowedImpureDLLs
local -A allowedImpureDLLsMap;
for dll in "${allowedImpureDLLsArray[@]}"; do
allowedImpureDLLsMap[$dll]=1
done
cd "$prefix"
# Iterate over any DLL that we depend on.
local target
for target in {bin,libexec}/**/*.{exe,dll}; do
[[ ! -f "$target" || ! -x "$target" ]] ||
_linkDeps "$target" "$(dirname "$target")" "1"
done
)
}
fixupOutputHooks+=(linkDLLs)

View file

@ -363,6 +363,13 @@ originalAttrs:
fi
done
''
+ lib.optionalString stdenv.targetPlatform.isCygwin ''
targetBinDir="''${targetConfig+$targetConfig/}bin"
for i in "''${!outputBin}/$targetLibDir"/cyg*.dll; do
mkdir -p "''${!outputLib}/$targetBinDir"
mv "$i" "''${!outputLib}/$targetBinDir"/
done
''
# if cross-compiling, link from $lib/lib to $lib/${targetConfig}.
# since native-compiles have $lib/lib as a directory (not a
# symlink), this ensures that in every case we can assume that

View file

@ -0,0 +1,142 @@
# shellcheck shell=bash
_moveDLLsToLib() {
if [[ "${dontMoveDLLsToLib-}" ]]; then return; fi
# shellcheck disable=SC2154
moveToOutput "bin/*.dll" "${!outputLib}"
}
preFixupHooks+=(_moveDLLsToLib)
declare _linkDeps_inputPath _linkDeps_outputPath
_addOutputDLLPaths() {
for output in $(getAllOutputNames); do
addToSearchPath _linkDeps_outputPath "${!output}/bin"
done
}
preFixupHooks+=(_addOutputDLLPaths)
_dllDeps() {
@objdump@ -p "$1" \
| sed -n 's/.*DLL Name: \(.*\)/\1/p' \
| sort -u
}
declare -Ag _linkDeps_visited
_linkDeps() {
local target="$1" dir="$2"
# canonicalise these for the dictionary
target=$(realpath -s "$target")
dir=$(realpath -s "$dir")
if [[ -v _linkDeps_visited[$target] ]]; then
echo "_linkDeps: $target was already linked." 1>&2
return
fi
if [[ ! -f $target || ! -x $target ]]; then
echo "_linkDeps: $target is not an executable file, skipping." 1>&2
return
fi
local output inOutput
for output in $outputs ; do
if [[ $target == "${!output}"* && ! -L $target ]]; then
echo "_linkDeps: $target is in $output (${!output})." 1>&2
inOutput=1
break
fi
done
echo 'target:' "$target"
local dll
while read -r dll; do
echo ' dll:' "$dll"
local dllPath=$dir/$dll
if [[ -v _linkDeps_visited[$dllPath] ]]; then
echo "_linkDeps: $dll was already linked into $dir." 1>&2
continue
fi
if [[ -L $dllPath ]]; then
echo ' already linked'
dllPath=$(realpath -s "$(readlink "$dllPath")")
_linkDeps "$dllPath" "$dir"
elif [[ -e $dllPath ]]; then
echo ' already exits'
_linkDeps "$dllPath" "$dir"
else
if [[ $dll = cygwin1.dll ]]; then
dllPath=/bin/cygwin1.dll
else
# This intentionally doesn't use $dir because we want to prefer
# dependencies that are already linked next to the target.
local searchPath realTarget
searchPath=$_linkDeps_outputPath:$_linkDeps_inputPath:$HOST_PATH
if [[ -L $target ]]; then
searchPath=$searchPath:$(dirname "$(readlink "$target")")
fi
searchPath=$(dirname "$target"):$searchPath
if ! dllPath=$(PATH="$searchPath" type -P "$dll"); then
if [[ -z $inOutput || -n ${allowedImpureDLLsMap[$dll]} ]]; then
continue
fi
echo unable to find "$dll" in "$searchPath" >&2
exit 1
fi
fi
echo ' linking to:' "$dllPath"
CYGWIN+=' winsymlinks:nativestrict' ln -sr "$dllPath" "$dir"
_linkDeps "$dllPath" "$dir"
fi
_linkDeps_visited[$dir/$dll]=1
done < <(_dllDeps "$target")
wait $!
}
# linkDLLsDir can be used to override the destination path for links. This is
# useful if you're trying to link dependencies of libraries into the directory
# containing an executable.
#
# Arguments can be a file or directory path. Directories are searched
# recursively.
linkDLLs() {
# shellcheck disable=SC2154
(
set -e
shopt -s globstar nullglob dotglob
local -a allowedImpureDLLsArray
concatTo allowedImpureDLLsArray allowedImpureDLLs
local -A allowedImpureDLLsMap;
for dll in "${allowedImpureDLLsArray[@]}"; do
allowedImpureDLLsMap[$dll]=1
done
local target file
for target in "$@"; do
# Iterate over any DLL that we depend on.
if [[ -f "$target" ]]; then
_linkDeps "$target" "${linkDLLsDir-$(dirname "$target")}"
elif [[ -d "$target" ]]; then
for file in "${target%/}"/**/*.{exe,dll}; do
_linkDeps "$file" "${linkDLLsDir-$(dirname "$file")}"
done
else
echo "linkDLLs: $target is not a file or directory, skipping." 1>&2
fi
done
)
}
_linkDLLs() {
# shellcheck disable=SC2154
if [[ ! -d $prefix || ${dontLinkDLLs-} ]]; then return; fi
linkDLLs "$prefix"/{bin,lib,libexec}/
}
fixupOutputHooks+=(_linkDLLs)

View file

@ -0,0 +1,15 @@
{
lib,
makeSetupHook,
binutils-unwrapped,
stdenv,
callPackage,
}:
makeSetupHook {
name = "cygwin-dll-link-hook";
substitutions = {
objdump = "${lib.getBin binutils-unwrapped}/${stdenv.targetPlatform.config}/bin/objdump";
};
passthru.tests = callPackage ./tests { };
} ./cygwin-dll-link.sh

View file

@ -0,0 +1,130 @@
{
dieHook,
lib,
stdenv,
testers,
runCommand,
}:
rec {
dll = stdenv.mkDerivation {
name = "dll";
src = ./dll;
outputs = [
"out"
"dev"
];
buildInputs = [ dll2 ];
strictDeps = true;
};
dll2 = stdenv.mkDerivation {
name = "dll2";
src = ./dll2;
outputs = [
"out"
"dev"
];
};
exe = stdenv.mkDerivation {
name = "exe";
src = ./exe;
buildInputs = [ dll ];
nativeBuildInputs = [ dieHook ];
strictDeps = true;
doCheck = true;
postFixup = ''[[ -e "$out"/bin/cyghello2.dll ]] || die missing indirect dependency'';
};
link-dll = exe.overrideAttrs {
name = "link-dll";
preFixup = ''
ln -s ${lib.getLib dll}/bin/cyghello.dll "$out"/bin/
'';
};
user32 = stdenv.mkDerivation {
name = "user32";
src = ./user32;
allowedImpureDLLs = [ "USER32.dll" ];
};
impure-dll = testers.testBuildFailure (
user32.overrideAttrs {
name = "impure-dll";
allowedImpureDLLs = [ ];
}
);
user32-dll = stdenv.mkDerivation {
name = "user32-dll";
src = ./user32-dll;
outputs = [
"out"
"dev"
];
allowedImpureDLLs = [ "USER32.dll" ];
};
user32-exe = stdenv.mkDerivation {
name = "user32-exe";
src = ./user32-exe;
buildInputs = [ user32-dll ];
strictDeps = true;
doCheck = true;
};
link-dir-dll = exe.overrideAttrs {
name = "link-dir-dll";
preFixup = ''
mkdir "$out"/libexec
ln -s ${lib.getLib user32-dll}/bin/cygpeek.dll "$out"/libexec/
linkDLLsDir="$out"/bin linkDLLs "$out"/libexec/cygpeek.dll
'';
};
link-dir-exe = exe.overrideAttrs {
name = "link-dir-exe";
preFixup = ''
mkdir "$out"/libexec
ln -s ${lib.getLib user32-exe}/bin/{peek.exe,cygpeek.dll} "$out"/libexec/
linkDLLsDir="$out"/bin linkDLLs "$out"/libexec/peek.exe
'';
};
link-user32-dll = exe.overrideAttrs {
name = "link-user32-dll";
preFixup = ''
ln -s ${lib.getLib user32-dll}/bin/cygpeek.dll "$out"/bin/
'';
};
copy-dll-impure = testers.testBuildFailure (
user32-exe.overrideAttrs {
name = "copy-dll-impure";
preFixup = ''
cp ${lib.getLib user32-dll}/bin/cygpeek.dll "$out"/bin/
'';
}
);
copy-dll = user32-exe.overrideAttrs {
name = "copy-dll";
preFixup = ''
cp ${lib.getLib user32-dll}/bin/cygpeek.dll "$out"/bin/
linkDLLs "$out"/bin/cygpeek.dll
'';
allowedImpureDLLs = [ "USER32.dll" ];
};
double-link = user32-exe.overrideAttrs {
name = "double-link";
preFixup = ''linkDLLs "$out"'';
};
utf8-glob = runCommand "utf8-glob" { } ''
touch NetLock_Arany_=Class_Gold=_Főtanstvny:49412ce40010.crt
ls -l NetLock* > "$out"
'';
}

View file

@ -0,0 +1,16 @@
.PHONY: all install
all: cyghello.dll
install: $(out)/bin/cyghello.dll $(out)/lib/libhello.dll.a $(out)/include/hello.h
cyghello.dll libhello.dll.a: hello.c
$(CC) -o $@ $^ -shared -Wl,--out-implib,libhello.dll.a -Wl,--export-all-symbols -lhello2
$(out)/bin/cyghello.dll: cyghello.dll
install -m755 -D $< $@
$(out)/lib/libhello.dll.a: libhello.dll.a
install -m644 -D $< $@
$(out)/include/hello.h: hello.h
install -m644 -D $< $@

View file

@ -0,0 +1,7 @@
#include "hello.h"
#include <stdio.h>
#include <hello2.h>
void hello() {
hello2();
}

View file

@ -0,0 +1,2 @@
#pragma once
void hello();

View file

@ -0,0 +1,16 @@
.PHONY: all install
all: cyghello2.dll
install: $(out)/bin/cyghello2.dll $(out)/lib/libhello2.dll.a $(out)/include/hello2.h
cyghello2.dll libhello2.dll.a: hello2.c
$(CC) -o $@ $^ -shared -Wl,--out-implib,libhello2.dll.a -Wl,--export-all-symbols
$(out)/bin/cyghello2.dll: cyghello2.dll
install -m755 -D $< $@
$(out)/lib/libhello2.dll.a: libhello2.dll.a
install -m644 -D $< $@
$(out)/include/hello2.h: hello2.h
install -m644 -D $< $@

View file

@ -0,0 +1,6 @@
#include <stdio.h>
#include "hello2.h"
void hello2() {
printf("Hello, World!\n");
}

View file

@ -0,0 +1,2 @@
#pragma once
void hello2();

View file

@ -0,0 +1,13 @@
.PHONY: all check install
all: hello.exe
install: $(out)/bin/hello.exe
hello.exe: main.c
$(CC) -o $@ $^ -lhello
check: hello.exe
./hello.exe
$(out)/bin/hello.exe: hello.exe
install -m755 -D $< $@

View file

@ -0,0 +1,6 @@
#include <hello.h>
int main() {
hello();
return 0;
}

View file

@ -0,0 +1,16 @@
.PHONY: all install
all: cygpeek.dll
install: $(out)/bin/cygpeek.dll $(out)/lib/libpeek.dll.a $(out)/include/peek.h
cygpeek.dll libpeek.dll.a: peek.c
$(CC) -o $@ $^ -shared -Wl,--out-implib,libpeek.dll.a -Wl,--export-all-symbols
$(out)/bin/cygpeek.dll: cygpeek.dll
install -m755 -D $< $@
$(out)/lib/libpeek.dll.a: libpeek.dll.a
install -m644 -D $< $@
$(out)/include/peek.h: peek.h
install -m644 -D $< $@

View file

@ -0,0 +1,7 @@
#include <windows.h>
int peek()
{
MSG msg;
PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE);
}

View file

@ -0,0 +1,2 @@
#pragma once
void peek();

View file

@ -0,0 +1,13 @@
.PHONY: all check install
all: peek.exe
install: $(out)/bin/peek.exe
peek.exe: main.c
$(CC) -o $@ $^ -lpeek
check: peek.exe
./peek.exe
$(out)/bin/peek.exe: peek.exe
install -m755 -D $< $@

View file

@ -0,0 +1,6 @@
#include <peek.h>
int main() {
peek();
return 0;
}

View file

@ -0,0 +1,13 @@
.PHONY: all check install
all: hello.exe
install: $(out)/bin/hello.exe
hello.exe: main.c
$(CC) -o $@ $^
check: hello.exe
./hello.exe
$(out)/bin/hello.exe: hello.exe
install -m755 -D $< $@

View file

@ -0,0 +1,7 @@
#include <windows.h>
int main()
{
MSG msg;
PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE);
}

View file

@ -21,5 +21,7 @@ makeScopeWithSplicing' {
# this is here to avoid symlinks being made to cygwin1.dll in /nix/store
newlib-cygwin-nobin = callPackage ./newlib-cygwin/nobin.nix { };
newlib-cygwin-headers = callPackage ./newlib-cygwin { headersOnly = true; };
cygwinDllLinkHook = callPackage ./cygwin-dll-link-hook { };
};
}

View file

@ -83,7 +83,10 @@ lib.init bootStages
|| p.isGenode;
in
f hostPlatform && !(f buildPlatform)
) buildPackages.updateAutotoolsGnuConfigScriptsHook;
) buildPackages.updateAutotoolsGnuConfigScriptsHook
++ lib.optional (
hostPlatform.isCygwin && !buildPlatform.isCygwin
) buildPackages.cygwin.cygwinDllLinkHook;
})
);
in

View file

@ -476,7 +476,6 @@ let
nativeBuildInputs
++ optional separateDebugInfo' ../../build-support/setup-hooks/separate-debug-info.sh
++ optional isWindows ../../build-support/setup-hooks/win-dll-link.sh
++ optional isCygwin ../../build-support/setup-hooks/cygwin-dll-link.sh
++ optionals doCheck nativeCheckInputs
++ optionals doInstallCheck nativeInstallCheckInputs;
@ -706,7 +705,6 @@ let
allowedImpureDLLs
++ lib.optionals isCygwin [
"KERNEL32.dll"
"cygwin1.dll"
];
}
// (