summaryrefslogtreecommitdiff
path: root/scripts/deplist
blob: fc7ba9db469e755e824dc80209e7716de08a8ae8 (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
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
#!/bin/sh -e

## e.g.
#
#   $ ./scripts/setup
#   $ ./scripts/deplist system | ./scripts/depsort
#

HERE="$(dirname $(readlink -f ${0}))";
BASE="${HERE}/..";

##
# Usage
#
if test ${#} = 0; then
    cat <<EOF
Usage: ${0} REPO [REPO ...]
EOF
fi

##
# Sanity check. The specified repositories exist.
#
for repo in ${@}; do
    if ! test -d "${BASE}/${repo}"; then
        printf "E: '%s' is not a valid repository!\n" "${repo}" 1>&2;
        exit 1;
    fi
done

##
# Find a package by name or find the parent package of a subpackage.
# Note that the index is padded by a space on either side for easy grep.
#
# Usage: parent PACKAGE
#
parent ()
{
    a=$("${HERE}"/sgrep " ${1} " "${HERE}"/.index | cut -d' ' -f2 | xargs);
    if ! test -z "${a}"; then
        r=${a};
    else
        b=$(grep " ${1} " "${HERE}"/.index | cut -d' ' -f2 | xargs);
        if ! test -z "${b}"; then
            r=${b};
        else
            r=${1};
        fi
    fi
    printf "%s" "${r}";
}

##
# Prints a fatal error and exits.
#
nxdepf ()
{
    printf "E: dependency '%s' not found or excepted (needed by '%s')\n" "${1}" "${2}" 1>&2;
    exit 1;
}

##
# Do everything at once.
#
for repo in ${@}; do
    find "${BASE}/${repo}" -mindepth 1 -maxdepth 1 -type d | while read k; do
    (
        ##
        # Source APKBUILD in a subshell to avoid contamination.
        #
        . "${k}/APKBUILD";

        ##
        # Special-case some options. For example, '!check' means
        # that the 'checkdepends=' variable is not used. Ignore.
        #
        for m in ${options}; do
            case ${m} in
                !check) checkdepends=; ;;
            esac
        done

        ##
        # Aggregate all possible depends. Obviously if something
        # is not specified, it will be empty anyway.
        #
        # FIXME: Clean this up somehow.
        #
        cats=$(tr ' ' '\n' <<EOF | sort | uniq
${checkdepends}
${depends}
${depends_dev}
${makedepends}
${makedepends_build}
${makedepends_host}
${_core_depends}
EOF
);

        ##
        # Construct canonical repository/package name.
        #
        p="${repo}/${k##*/}";

        ##
        # Column 1 output.
        #
        printf "%s " "${p}";
        test -z "${cats}" && printf "\n" && continue; # skip when empty; we are done

        for d in ${cats}; do

            ##
            # Trim out most of the qualifiers.
            #
            _d=${d};            # formatting

            _d=${_d%=*};        # remove =version
            _d=${_d%>*};        # remove >version
            _d=${_d%<*};        # remove <version
            _d=${_d%~*};        # remove ~version

            ##
            # Handle cases where a dependency is provided by another package.
            #
            case "${_d}" in
                !*) continue; ;;

                # override: system
                /bin/sh)        _d=dash; ;;
                /sbin/init)     _d=s6-linux-init; ;;
                cmd:sendmail)   _d=ssmtp; ;;
                cmd:which)      _d=debianutils; ;;
                libc-utils)     _d=musl; ;;
                musl-dbg)       _d=musl; ;;

                # override: user (FIXME: incomplete)
                cmd:byacc)      _d=$(parent ${d#cmd:*}); ;;
                cmd:cpio)       _d=$(parent libarchive-tools); ;;
                cmd:gzip)       _d=$(parent ${d#cmd:*}); ;;
                cmd:yacc)       _d=bison; ;;
                cmd:lex)        _d=flex; ;;
                cmd:locale)     _d=musl-locales; ;;
                cmd:mcookie)    _d=$(parent ${d#cmd:*}); ;;
                cmd:pkg-config) _d=pkgconf; ;;
                cmd:unix2dos)   _d=dos2unix; ;;
                cmd:xsltproc)   _d=libxslt; ;;
                cargo-*)        _d=rust; ;;
                guile-dev)      _d=guile; ;;
                llvm-dev)       _d=$(parent llvm14-dev); ;;
                llvm-static)    _d=$(parent llvm14-static); ;;
                llvm-test-*)    _d=$(parent llvm14-test-utils); ;;

                # automatic search
                *)              _d=$(parent $_d); ;;
            esac

            ##
            # Determine relative path to dependency. It might be
            # in the wrong repository, in which case must fix.
            #
            # TODO: Clean up to accommodate any number of repos
            # and their policies. For example, 'kernel' if we do
            # end up creating that someday.
            #
            case "${repo}" in
                system) # nothing in system may depend on anything outside of system
                    if ! test -f "${BASE}/${repo}/${_d}/APKBUILD"; then
                        nxdepf "${_d}" "${p}";
                    fi
                    path="${repo}/${_d}";
                    ;;
                user) # packages in user might depend on something in system
                    if ! test -f "${BASE}/${repo}/${_d}/APKBUILD"; then
                        if test -f "${BASE}/system/${_d}/APKBUILD"; then
                            path="system/${_d}";
                        else
                            nxdepf "${_d}" "${p}";
                        fi
                    else
                        path="${repo}/${_d}";
                    fi
                    ;;
            esac

            ##
            # Print remaining columns of output if not parent.
            #
            printf " %s" "${path}";
        done | tr ' ' '\n' | sed -e "\@${p}\$@d" | sort -u | xargs; # sort and remove duplicates
    )
    done | sort; # sort only within a repo
done