summaryrefslogtreecommitdiff
path: root/pkgtools/verifypc/files/verifypc.sh
blob: a380b8621b1b78abd2b0d3af20a1911781aec41e (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
#!@SH@
#
# $NetBSD: verifypc.sh,v 1.1.1.1 2005/10/01 12:56:51 jmmv Exp $
#
# verifypc - Sanity check package dependencies according to pkg-config
# Copyright (c) 2005 Julio M. Merino Vidal <jmmv@NetBSD.org>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
# 3. Neither the name of author nor the names of its contributors may
#    be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#

: ${MAKE:=@MAKE@}

: ${VERIFYPC_IGNORE:=}

SORTED_DEPS=

# -------------------------------------------------------------------------

#
# Show an error message on stderr and exit.
#
err() {
    echo "verifypc: ${@}" 1>&2
    exit 1
}

# -------------------------------------------------------------------------

#
# Show a warning message on stderr.
#
warn() {
    echo "verifypc: ${@}" 1>&2
}

# -------------------------------------------------------------------------

#
# Checks if the file passed as an argument belongs to any of our direct
# dependencies.
#
search_file_in_depends() {
    local file="${1}"
    local dep dir file

    for dep in ${SORTED_DEPS}; do
        dir=$(echo ${dep} | cut -d : -f 2)
        if [ -d ${dir} ] && grep ${file} ${dir}/PLIST* >/dev/null; then
            echo ${dep}
            return
        fi
    done
}

# -------------------------------------------------------------------------

#
# Checks if the pkg-config dependency specification is satisfied by
# the 'dep' package dependency.
#
check_match() {
    local dep="${1}" pcname="${2}" pcop="${3}" pcver="${4}"
    local pkgdep pkgname

    if [ ${pcop} != ">" -a ${pcop} != ">=" -a ${pcop} != "-" ]; then
        warn "unsupported operator ${pcop} in ${pcname} dependency"
        return 1
    fi

    pkgdep=$(echo ${dep} | cut -d : -f 1 | sed 's|>=|-|;s|>|-|')
    pkgname=$(echo ${pkgdep} | cut -d - -f 1)

    if ! pkg_admin pmatch "${pkgname}${pcop}${pcver}" "${pkgdep}"; then
        warn "${pcname} not correct; '${pcop} ${pcver}' needed"
        return 1
    fi
    return 0
}

# -------------------------------------------------------------------------

#
# Main program.
#
main() {
    local dep error lines log pcname pcop pcver

    [ -f Makefile -a -f ../../mk/bsd.pkg.mk ] ||
        err "must be run within a package directory"

    log=$(${MAKE} show-var VARNAME=_PKG_CONFIG_LOG)

    [ -f ${log} ] ||
        err "pkg-config log not found; must run '${MAKE} configure' first"

    # Construct a list of dependency specifications for the current package.
    SORTED_DEPS=$(${MAKE} show-vars VARNAMES="BUILD_DEPENDS DEPENDS" | tr ' ' '
' | sort -r | uniq)

    error=0
    lines=$(cat ${log} | sort | uniq | tr ' ' '¬')
    for l in ${lines}; do
        pcname=$(echo ${l} | cut -d '¬' -f 1)
        pcop=$(echo ${l} | cut -d '¬' -f 2)
        pcver=$(echo ${l} | cut -d '¬' -f 3)

        dep=$(search_file_in_depends pkgconfig/${pcname}.pc)
        if [ -n "${dep}" -a "${pcop}" != "NOT-FOUND" ]; then
            # The package passed to pkg-config is correct, as we have it
            # among our dependencies.  If we are requiring an specific
            # version of it, let's check if it is correct.
            if [ ${pcop} != "(any)" ]; then
                check_match ${dep} ${pcname} ${pcop} ${pcver}
                [ $? -eq 0 ] || error=1
            fi
        else
            # The package passed to pkg-config cannot be found among our
            # dependencies or it does not exist; tell the user.
            if echo ${VERIFYPC_IGNORE} | \
                egrep "^${pcname}$|^${pcname} | ${pcname} | ${pcname}$" \
                >/dev/null
            then
                : # Package explicitly ignored by the user, so shut up.
            else
                warn "${pcname} not a direct dependency"
                error=1
            fi
        fi
    done

    return ${error}
}

main "${@}"