haskell-builder.nix: work around useSystemCoreFoundationFramework hook

In 23.11, Darwin was changes to use the system CoreFoundation both aarch64-darwin and x86_64-darwin. The ability to change between implementations was removed, but the hook was left in place because at the time it was harmless. Unfortunately, that is no longer the case.

After the upgrade of ld64 to 951.9, it is no longer as permissive about when `-rpath` may be used. It no longer allows the flag to be used when targeting macOS 10.5, and it no longer allows it when merging objects. The former was an issue for certain versions of GCC (now fixed). The latter is an issue for Haskell.

When a Haskell project uses Darwin frameworks and Cabal to build, they will fail to build without this workaround, giving the following error.

    ld: -rpath can only be used when creating a dynamic final linked image

This is due to an optimization Cabal [performs][1]. If the linker supports creating relocatable objects or merging them, it will do so eagerly. This causes packages such as OpenGLRaw to fail to build due using Cabal and frameworks (OpenGL), which pull in the hook.

This workaround is ugly, but it will be reverted as soon as it hits staging and replaced with the real solution, which is removing the hook. The hook is only used with the 10.12 SDK. The 11.0 and 12.3 SDKs do not support the hook, and being able to switch CoreFoundation implementations is not anticipated to be restored in the future (due to problems it causes).

[1]: 705b6ebcae/Cabal/src/Distribution/Simple/Program/Builtin.hs (L341-L380)
This commit is contained in:
Randy Eckenrode 2024-07-23 18:30:26 -04:00
parent 09a27d5bc1
commit e14e9647d7
No known key found for this signature in database
GPG Key ID: 64C1CD4EC2A600D9

View File

@ -150,6 +150,35 @@ assert stdenv.hostPlatform.isWasm -> enableStaticLibraries == false;
let
# This is a workaround for the 2024-07-20 staging-next cycle to avoid causing mass rebuilds.
# todo(@reckenrode) Remove this workaround and remove `NIX_COREFOUNDATION_RPATH`, the related hooks, and ld-wrapper support.
nixCoreFoundationRpathWorkaround = stdenv.mkDerivation {
name = "nix-corefoundation-rpath-workaround";
buildCommand = ''
mkdir -p "$out/nix-support"
cat <<-EOF > "$out/nix-support/setup-hook"
removeUseSystemCoreFoundationFrameworkHook() {
unset NIX_COREFOUNDATION_RPATH
local _hook
for _hook in envBuildBuildHooks envBuildHostHooks envBuildTargetHooks envHostHostHooks envHostTargetHooks envTargetTargetHooks; do
local _index=0
local _var="\$_hook[@]"
for _var in "\''${!_var}"; do
if [ "\$_var" = "useSystemCoreFoundationFramework" ]; then
unset "\$_hook[\$_index]"
fi
((++_index))
done
unset _index
unset _var
done
unset _hook
}
addEnvHooks "\$hostOffset" removeUseSystemCoreFoundationFrameworkHook
EOF
'';
};
inherit (lib) optional optionals optionalString versionAtLeast
concatStringsSep enableFeature optionalAttrs;
@ -430,7 +459,8 @@ stdenv.mkDerivation ({
inherit depsBuildBuild nativeBuildInputs;
buildInputs = otherBuildInputs ++ optionals (!isLibrary) propagatedBuildInputs
# For patchShebangsAuto in fixupPhase
++ optionals stdenv.hostPlatform.isGhcjs [ nodejs ];
++ optionals stdenv.hostPlatform.isGhcjs [ nodejs ]
++ optionals (stdenv.isDarwin && stdenv.isx86_64) [ nixCoreFoundationRpathWorkaround ];
propagatedBuildInputs = optionals isLibrary propagatedBuildInputs;
LANG = "en_US.UTF-8"; # GHC needs the locale configured during the Haddock phase.