summaryrefslogtreecommitdiff
path: root/pkgs/build-support/dotnet/auto-patchcil-hook/auto-patchcil.sh
blob: 898ad68cb969b68353e631bf9b7b4aeae6d076fa (plain)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!@shell@
# shellcheck shell=bash

declare -a autoPatchcilLibs
declare -a extraAutoPatchcilLibs

gatherLibraries() {
    if [ -d "$1/lib" ]; then
        autoPatchcilLibs+=("$1/lib")
    fi
}

addEnvHooks "${targetOffset:?}" gatherLibraries

# Can be used to manually add additional directories with shared object files
# to be included for the next autoPatchcil invocation.
addAutoPatchcilSearchPath() {
    local -a findOpts=()

    while [ $# -gt 0 ]; do
        case "$1" in
        --)
            shift
            break
            ;;
        --no-recurse)
            shift
            findOpts+=("-maxdepth" 1)
            ;;
        --*)
            echo "addAutoPatchcilSearchPath: ERROR: Invalid command line" \
                "argument: $1" >&2
            return 1
            ;;
        *) break ;;
        esac
    done

    local dir=
    while IFS= read -r -d '' dir; do
        extraAutoPatchcilLibs+=("$dir")
    done < <(
        find "$@" "${findOpts[@]}" \! -type d \
            \( -name '*.so' -o -name '*.so.*' \) -print0 |
            sed -z 's#/[^/]*$##' |
            uniq -z
    )
}

autoPatchcil() {
    local rid=
    local norecurse=
    while [ $# -gt 0 ]; do
        case "$1" in
        --)
            shift
            break
            ;;
        --rid)
            rid="$2"
            shift 2
            ;;
        --no-recurse)
            shift
            norecurse=1
            ;;
        --*)
            echo "autoPatchcil: ERROR: Invalid command line" \
                "argument: $1" >&2
            return 1
            ;;
        *) break ;;
        esac
    done

    if [ -z "$rid" ]; then
        echo "autoPatchcil: ERROR: No RID (Runtime ID) provided." >&2
        return 1
    fi

    local ignoreMissingDepsArray=("--ignore-missing")
    concatTo ignoreMissingDepsArray autoPatchcilIgnoreMissingDeps

    if [ ${#ignoreMissingDepsArray[@]} -lt 2 ]; then
        ignoreMissingDepsArray=()
    fi

    local autoPatchcilFlags=(
        ${norecurse:+--no-recurse}
        --rid "$rid"
        "${ignoreMissingDepsArray[@]}"
        --paths "$@"
        --libs "${autoPatchcilLibs[@]}"
    )

    # shellcheck disable=SC2016
    echoCmd 'patchcil auto flags' "${autoPatchcilFlags[@]}"
    @patchcil@ auto "${autoPatchcilFlags[@]}"
}

autoPatchcilFixupOutput() {
    if [[ -z "${dontAutoPatchcil-}" ]]; then
        if [ -n "${dotnetRuntimeIds+x}" ]; then
            if [[ -n $__structuredAttrs ]]; then
                local dotnetRuntimeIdsArray=("${dotnetRuntimeIds[@]}")
            else
                # shellcheck disable=SC2206 # Intentionally expanding it to preserve old behavior
                local dotnetRuntimeIdsArray=($dotnetRuntimeIds)
            fi
        else
            local dotnetRuntimeIdsArray=("")
        fi

        autoPatchcil --rid "${autoPatchcilRuntimeId:-${dotnetRuntimeIdsArray[0]}}" -- "${prefix:?}"
    fi
}

fixupOutputHooks+=(autoPatchcilFixupOutput)