aboutsummaryrefslogtreecommitdiff
path: root/paludis/repositories/e/ebuild/exheres-0/exlib_functions.bash
blob: fc690e3a9e4d07287411b17a0e54afc6ac579d84 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/usr/bin/env bash
# vim: set sw=4 sts=4 et :

# Copyright (c) 2007, 2008, 2009 Ciaran McCreesh
# Copyright (c) 2009 Bo ├śrsted Andresen
#
# This file is part of the Paludis package manager. Paludis is free software;
# you can redistribute it and/or modify it under the terms of the GNU General
# Public License, version 2, as published by the Free Software Foundation.
#
# Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place, Suite 330, Boston, MA  02111-1307  USA

export_exlib_phases()
{
    [[ -z "${CURRENT_EXLIB}" ]] && die "export_exlib_phases called but CURRENT_EXLIB undefined"

    local e
    for e in "$@" ; do
        if [[ "${e}" == builtin_* ]] || ! has "${e}" ${PALUDIS_EBUILD_FUNCTIONS}; then
            die "$e should not be in export_exlib_phases for ${CURRENT_EXLIB}"
        fi
        eval "${e}() { ${CURRENT_EXLIB}_${e} \"\$@\" ; }"
    done
}

exparam()
{
    die "exparam is banned outside exlibs"
}

exparam_var_name()
{
    echo EXPARAMVAR_${1//-/__dash__}
}

exparam_print()
{
    case "${1}" in
        -a) eval "${2}=( \"\${${3}}\" )" ;;
        -v) eval "${2}=\"\${${3}}\""      ;;
        *)  eval "echo \"\${${1}}\""     ;;
    esac
}

exparam_internal()
{
    local i e=${1} a to_var v a_v=$(exparam_var_name ${1})__ALLDECLS__
    shift
    if [[ ${1} == -v ]]; then
        [[ ${#} -eq 3 ]] || die "exparam ${1} requires exactly three arguments"
        a=${1}
        to_var=${2}
        shift 2
    else
        [[ ${#} -eq 1 ]] || die "exparam requires exactly one argument"
    fi
    v=$(exparam_var_name ${e})_${1%\[*}
    if [[ ${1} == *\[*\] ]]; then
        has "${1%\[*}[]" ${!a_v} || die "${e}.exlib has no ${1%\[*} array"
        i=${1#*\[}
        i=${i%\]}
        case "${i}" in
            "#")            exparam_print ${to_var:+-v "${to_var}"} "#${v}[*]"   ;;
            "*"|"@")        exparam_print ${to_var:+-a "${to_var}"} "${v}[${i}]"    ;;
            +([[:digit:]])) exparam_print ${to_var:+-v "${to_var}"} "${v}[${i}]" ;;
            *)              die "Invalid index in exparam ${1}"          ;;
        esac
    else
        has "${1}" ${!a_v%\[\]} || die "${e}.exlib has no ${1} parameter"
        exparam_print ${to_var:+-v "${to_var}"} "${v}"
    fi
}

myexparam()
{
    [[ -z "${CURRENT_EXLIB}" ]] && die "myexparam called but CURRENT_EXLIB undefined"

    local v=${1%%=*} a_v="$(exparam_var_name ${CURRENT_EXLIB})__ALLDECLS__"
    [[ ${1} == *=\[ && ${#} -gt 1 ]] && v+="[]"
    printf -v "${a_v}" "%s %s" "${!a_v}" "${v}"

    v=$(exparam_var_name ${CURRENT_EXLIB})_${v%\[\]}
    if [[ -z ${!v+set} && ${1} == *=* ]]; then
        if [[ ${1} == *=\[ && ${#} -gt 1 ]]; then
            shift
            local i a=()
            while [[ ${#} -gt 1 ]]; do
                a+=( "${1}" )
                shift
            done
            [[ ${1} == \] ]] || die "Array encountered with no closing ]"
            eval "${v}=( \"\${a[@]}\" )"
        else
            printf -v "${v}" "%s" "${1#*=}"
        fi
    fi
}

require()
{
    ebuild_notice "debug" "Command 'require ${@}', using EXLIBSDIRS '${EXLIBSDIRS}'"
    local exlibs e ee p a=() location v a_v v_qa
    # parse exlib parameters
    while [[ -n $@ ]]; do
        if [[ ${1} == +(\[) ]]; then
            [[ -z ${e} ]] && die "\"${1}\" encountered with no preceding exlib"
            p=${1}
            shift
            while [[ -n ${1} && ${1} != ${p//\[/\]} ]]; do
                v="${1%%=*}"
                if [[ ${1#*=} == ${p} ]]; then
                    v+="[]"
                    a=()
                    shift
                    while [[ -n ${1} && ${1} != ${p//\[/\]} ]]; do
                        a+=( "${1}" )
                        shift
                    done
                    [[ ${1} == ${p//\[/\]} ]] || die "\"${p}\" encountered with no closing \"${p//[/]}\" for array ${v}"
                    eval "$(exparam_var_name ${e})_${v%\[\]}=( \"\${a[@]}\" )"
                else
                    printf -v "$(exparam_var_name ${e})_${1%%=*}" "%s" "${1#*=}"
                fi
                a_v="$(exparam_var_name ${e})__ALL__"
                printf -v "${a_v}" "%s %s" "${!a_v}" "${v}"
                shift
            done
            [[ ${1} == ${p//\[/\]} ]] || die "\"${p}\" encountered with no closing \"${p//[/]}\""
        else
            e=${1}
            exlibs+=" ${e}"
        fi
        shift
    done
    # source exlibs
    for e in ${exlibs}; do
        location=
        for ee in ${EXLIBSDIRS} ; do
            [[ -f "${ee}/${e}.exlib" ]] && location="${ee}/${e}.exlib"
        done
        local old_CURRENT_EXLIB="${CURRENT_EXLIB}"
        export CURRENT_EXLIB="${e}"
        alias exparam="exparam_internal ${CURRENT_EXLIB}"

        for v in ${PALUDIS_SOURCE_MERGED_VARIABLES} ${PALUDIS_BRACKET_MERGED_VARIABLES} ; do
            local c_v="current_${v}" u_v="unset_${v}"
            local ${c_v}="${!v}"
            local ${u_v}="${!v-unset}"
            unset ${v}
        done

        for v_qa in ${PALUDIS_ECLASS_MUST_NOT_SET_VARIABLES} ; do
            local v=${v_qa#qa:}
            local c_v="current_${v}" u_v="unset_${v}"
            export -n ${c_v}="${!v}"
            export -n ${u_v}="${!v-unset}"
        done

        [[ -z "${location}" ]] && die "Error finding exlib ${e} in ${EXLIBSDIRS}"
        source "${location}" || die "Error sourcing exlib ${e}"
        hasq "${CURRENT_EXLIB}" ${INHERITED} || export INHERITED="${INHERITED} ${CURRENT_EXLIB}"

        for v in ${PALUDIS_SOURCE_MERGED_VARIABLES} ; do
            local e_v="E_${v}"
            export -n ${e_v}="${!e_v} ${!v}"
        done

        for v in ${PALUDIS_BRACKET_MERGED_VARIABLES} ; do
            local e_v="E_${v}"
            export -n ${e_v}="${!e_v} ( ${!v} )"
        done

        for v in ${PALUDIS_SOURCE_MERGED_VARIABLES} ${PALUDIS_BRACKET_MERGED_VARIABLES} ; do
            local c_v="current_${v}" u_v="unset_${v}"
            [[ "unset" == ${!u_v} ]] && unset ${v} || export ${v}="${!c_v}"
        done

        for v_qa in ${PALUDIS_ECLASS_MUST_NOT_SET_VARIABLES} ; do
            local v=${v_qa#qa:}
            local c_v="current_${v}" u_v="unset_${v}"
            if [[ ${!c_v} != ${!v} || ${!u_v} != ${!v-unset} ]]; then
                if [[ ${v} == ${v_qa} ]] ; then
                    die "Variable '${v}' illegally set by ${location}"
                else
                    ebuild_notice "qa" "Variable '${v}' should not be set by ${location}"
                    export -n ${c_v}="${!v}"
                    export -n ${u_v}="${!v-unset}"
                fi
            fi
        done

        # die on required exlib parameters that haven't been supplied
        local c_v v
        a_v=$(exparam_var_name ${CURRENT_EXLIB})__ALLDECLS__
        for v in ${!a_v}; do
            c_v=$(exparam_var_name ${CURRENT_EXLIB})_${v%\[\]}
            if [[ $(eval "declare -p ${c_v}") == declare\ -a\ ${c_v}=* ]]; then
                [[ ${v} == *\[\] ]] || die "${CURRENT_EXLIB}.exlib requires a scalar ${v} parameter but got an array"
            elif [[ -n ${!c_v+set} ]]; then
                [[ ${v} != *\[\] ]] || die "${CURRENT_EXLIB}.exlib requires an array ${v} but got a scalar"
            else
                die "${CURRENT_EXLIB}.exlib requires a ${v} parameter"
            fi
        done

        # die on supplied exlib parameters which haven't been declared
        v=$(exparam_var_name ${CURRENT_EXLIB})__ALL__
        for v in ${!v}; do
            has ${v} ${!a_v} || die "${CURRENT_EXLIB}.exlib takes no ${v} parameter"
        done

        export CURRENT_EXLIB="${old_CURRENT_EXLIB}"
        if [[ -n ${CURRENT_EXLIB} ]]; then
            alias exparam="exparam_internal ${CURRENT_EXLIB}"
        else
            unalias exparam
        fi
    done
}

default()
{
    [[ $(type -t "default_$(paludis_phase_to_function_name "${!PALUDIS_EBUILD_PHASE_VAR}")" ) == "function" ]] || \
        die "default_$(paludis_phase_to_function_name "${!PALUDIS_EBUILD_PHASE_VAR}") is not a function"
    default_$(paludis_phase_to_function_name "${!PALUDIS_EBUILD_PHASE_VAR}") "$@"
}

illegal_in_global_scope()
{
    [[ "${!PALUDIS_EBUILD_PHASE_VAR}" == "metadata" ]] \
        && die "Exheres bug: ${FUNCNAME[1]} must not be called in global scope"
}