summary refs log tree commit diff
path: root/pkgs/os-specific/darwin/apple-sdk/framework-setup-hook.sh
blob: 04d8e878f4f768e0e104b7372fbb536644532887 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# On macOS, frameworks are linked to the system CoreFoundation but
# dynamic libraries built with nix use a pure version of CF this
# causes segfaults for binaries that depend on it at runtime.  This
# can be solved in two ways.
# 1. Rewrite references to the pure CF using this setup hook, this
# works for the simple case but this can still cause problems if other
# dependencies (eg. python) use the pure CF.
# 2. Create a wrapper for the binary that sets DYLD_FRAMEWORK_PATH to
# /System/Library/Frameworks.  This will make everything load the
# system's CoreFoundation framework while still keeping the
# dependencies pure for other packages.

fixupOutputHooks+=('fixDarwinFrameworksIn $prefix')

fixDarwinFrameworks() {
    local systemPrefix='/System/Library/Frameworks'

    for fn in "$@"; do
        if [ -L "$fn" ]; then continue; fi
        echo "$fn: fixing dylib"

        for framework in $(otool -L "$fn" | awk '/CoreFoundation\.framework/ {print $1}'); do
          install_name_tool -change "$framework" "$systemPrefix/CoreFoundation.framework/Versions/A/CoreFoundation" "$fn" >&2
        done
    done
}

fixDarwinFrameworksIn() {
    local dir="$1"
    fixDarwinFrameworks $(find "$dir" -name "*.dylib")
}


# This configures the stdenv to use /System/Library/Frameworks/CoreFoundation.framework
# instead of the nix version by including the system frameworks path
# as an rpath entry when creating binaries.

useSystemCoreFoundationFramework () {
  export NIX_COREFOUNDATION_RPATH=/System/Library/Frameworks
}

envHooks+=(useSystemCoreFoundationFramework)