about summary refs log tree commit diff
path: root/pkgs/build-support/dotnet/build-dotnet-module/hooks/dotnet-fixup-hook.sh
blob: a9701f2898b2749813d8fd74eb8c950702ba9651 (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# For compatibility, convert makeWrapperArgs to an array unless we are using
# structured attributes. That is, we ensure that makeWrapperArgs is always an
# array.
# See https://github.com/NixOS/nixpkgs/blob/858f4db3048c5be3527e183470e93c1a72c5727c/pkgs/build-support/dotnet/build-dotnet-module/hooks/dotnet-fixup-hook.sh#L1-L3
# and https://github.com/NixOS/nixpkgs/pull/313005#issuecomment-2175482920
if [[ -z $__structuredAttrs ]]; then
    makeWrapperArgs=( ${makeWrapperArgs-} )
fi

# First argument is the executable you want to wrap,
# the second is the destination for the wrapper.
wrapDotnetProgram() {
    local -r dotnetRuntime=@dotnetRuntime@
    local -r wrapperPath=@wrapperPath@

    local -r dotnetFromEnvScript='dotnetFromEnv() {
    local dotnetPath
    if command -v dotnet 2>&1 >/dev/null; then
        dotnetPath=$(which dotnet) && \
            dotnetPath=$(realpath "$dotnetPath") && \
            dotnetPath=$(dirname "$dotnetPath") && \
            export DOTNET_ROOT="$dotnetPath"
    fi
}
dotnetFromEnv'

    if [[ -n $__structuredAttrs ]]; then
        local -r dotnetRuntimeDepsArray=( "${dotnetRuntimeDeps[@]}" )
    else
        local -r dotnetRuntimeDepsArray=($dotnetRuntimeDeps)
    fi

    local dotnetRuntimeDepsFlags=()
    if (( ${#dotnetRuntimeDepsArray[@]} > 0 )); then
        local libraryPathArray=("${dotnetRuntimeDepsArray[@]/%//lib}")
        local OLDIFS="$IFS" IFS=':'
        dotnetRuntimeDepsFlags+=("--suffix" "LD_LIBRARY_PATH" ":" "${libraryPathArray[*]}")
        IFS="$OLDIFS"
    fi

    local dotnetRootFlagsArray=()
    if [[ -z ${dotnetSelfContainedBuild-} ]]; then
        if [[ -n ${useDotnetFromEnv-} ]]; then
            # if dotnet CLI is available, set DOTNET_ROOT based on it. Otherwise set to default .NET runtime
            dotnetRootFlagsArray+=("--suffix" "PATH" ":" "$wrapperPath")
            dotnetRootFlagsArray+=("--run" "$dotnetFromEnvScript")
            dotnetRootFlagsArray+=("--set-default" "DOTNET_ROOT" "$dotnetRuntime")
            dotnetRootFlagsArray+=("--suffix" "PATH" ":" "$dotnetRuntime/bin")
        else
            dotnetRootFlagsArray+=("--set" "DOTNET_ROOT" "$dotnetRuntime")
            dotnetRootFlagsArray+=("--prefix" "PATH" ":" "$dotnetRuntime/bin")
        fi
    fi

    makeWrapper "$1" "$2" \
        "${dotnetRuntimeDepsFlags[@]}" \
        "${dotnetRootFlagsArray[@]}" \
        "${gappsWrapperArgs[@]}" \
        "${makeWrapperArgs[@]}"

    echo "installed wrapper to "$2""
}

dotnetFixupHook() {
    echo "Executing dotnetFixupPhase"

    local -r dotnetInstallPath="${dotnetInstallPath-$out/lib/$pname}"

    local executable executableBasename

    # check if dotnetExecutables is declared (including empty values, in which case we generate no executables)
    if declare -p dotnetExecutables &>/dev/null; then
        if [[ -n $__structuredAttrs ]]; then
            local dotnetExecutablesArray=( "${dotnetExecutables[@]}" )
        else
            local dotnetExecutablesArray=($dotnetExecutables)
        fi
        for executable in "${dotnetExecutablesArray[@]}"; do
            executableBasename=$(basename "$executable")

            local path="$dotnetInstallPath/$executable"

            if test -x "$path"; then
                wrapDotnetProgram "$path" "$out/bin/$executableBasename"
            else
                echo "Specified binary \"$executable\" is either not an executable or does not exist!"
                echo "Looked in $path"
                exit 1
            fi
        done
    else
        while IFS= read -d '' executable; do
            executableBasename=$(basename "$executable")
            wrapDotnetProgram "$executable" "$out/bin/$executableBasename" \;
        done < <(find "$dotnetInstallPath" ! -name "*.dll" -executable -type f -print0)
    fi

    echo "Finished dotnetFixupPhase"
}

if [[ -z "${dontDotnetFixup-}" ]]; then
    preFixupPhases+=" dotnetFixupHook"
fi