mozilla-nss: fix dependencies for library/nspr/32
[oi-userland.git] / tools / perl-meta-deps
bloba8c9d70df5664beeb11808ff3a72f3aa4246c16e
1 #! /usr/bin/ksh
4 # This file and its contents are supplied under the terms of the
5 # Common Development and Distribution License ("CDDL"), version 1.0.
6 # You may only use this file in accordance with the terms of version
7 # 1.0 of the CDDL.
9 # A full copy of the text of the CDDL should have accompanied this
10 # source. A copy of the CDDL is also available via the Internet at
11 # http://www.illumos.org/license/CDDL.
15 # Copyright 2022 Marcel Telka
18 function usage
20 [[ -n "$1" ]] && printf "ERROR: %s\n\n" "$1"
21 printf "Usage: perl-meta-deps WS_MACH BUILD_DIR [configure|build|test|runtime]... [suggests] PERL_VERSION\n" >&2
22 [[ -n "$1" ]] && exit 1
23 exit 0
27 (($# == 0)) && usage
29 WS_MACH="$1"
30 [[ -d "$WS_MACH" ]] && shift || usage "WS_MACH does not exist"
32 BUILD_DIR="$1"
33 [[ -d "$BUILD_DIR" ]] && shift || usage "BUILD_DIR does not exist"
35 PHASES=
36 while true ; do
37 case "$1" in
38 configure|build|test|runtime)
39 PHASES="$PHASES,.$1" && shift ;;
40 *) break ;;
41 esac
42 done
43 PHASES=${PHASES/#,}
44 [[ -z "$PHASES" ]] && PHASES=".configure,.build,.test,.runtime"
46 RELATION=".requires,.recommends"
47 [[ "$1" == "suggests" ]] && RELATION=".$1" && shift
49 (($# == 0)) && usage "PERL_VERSION missing"
50 PERLVER=$1 && shift
51 PLV=${PERLVER//.}
52 PERL=/usr/perl5/$PERLVER/bin/perl
53 [[ -x "$PERL" ]] || usage "Perl $PERLVER not found"
54 PERL_ARCH=$($PERL -e 'use Config; print $Config{archname}')
56 (($# != 0)) && usage "Too many arguments"
58 PERL_VERSION_CONVERT="$(dirname $0)/perl-version-convert"
59 [[ ! -x "$PERL_VERSION_CONVERT" ]] && printf "ERROR: perl-version-convert not found" && exit 1
61 CACHEDIR="$WS_MACH/perl-meta-deps-cache"
62 mkdir -p "$CACHEDIR"
63 # Remove old entries from the cache
64 find "$CACHEDIR" -type f -mtime +7 -exec rm -f {} \;
67 /usr/bin/jq -r '.prereqs|'"$PHASES"'|'"$RELATION"'|to_entries?|.[]|.key+" "+(.value|tostring)' | while read PREREQ VERSION ; do
68 # Convert perl version number to pkg(5) compatible form
69 VER=$("$PERL_VERSION_CONVERT" "$PREREQ" "$VERSION")
70 [[ "$VER" != "0" ]] && VER="@$VER" || VER=
72 CACHEFILE="$CACHEDIR/$PREREQ-$PERLVER"
74 # Resolve the dependency if not in cache already
75 if [[ ! -e "$CACHEFILE" || ("$RELATION" != ".suggests" && ! -s "$CACHEFILE") ]] ; then
76 # We must replace '::' by '-' otherwise 'pkgdepend resolve' fails
77 MANIFEST="$BUILD_DIR/META-${PREREQ//::/-}-$PERLVER"
79 # Prepare manifest
80 if [[ "$PREREQ" == "perl" ]] ; then
81 # "perl" is special case
82 cat <<#EOF > "$MANIFEST"
83 depend fmri=__TBD type=require pkg.debug.depend.file=$PREREQ \\
84 pkg.debug.depend.path=usr/perl5/$PERLVER/bin
85 EOF
86 else
87 cat <<#EOF > "$MANIFEST"
88 depend fmri=__TBD type=require pkg.debug.depend.file=${PREREQ//:://}.pm \\
89 pkg.debug.depend.path=usr/perl5/$PERLVER/lib \\
90 pkg.debug.depend.path=usr/perl5/$PERLVER/lib/$PERL_ARCH \\
91 pkg.debug.depend.path=usr/perl5/vendor_perl/$PERLVER \\
92 pkg.debug.depend.path=usr/perl5/vendor_perl/$PERLVER/$PERL_ARCH
93 EOF
96 # Resolve dependency
97 if /usr/bin/pkgdepend resolve "$MANIFEST" ; then
98 # Remove version number and cache the result
99 cat "$MANIFEST.res" | sed -e 's/@[^ ]*//g' -e 's/-'$PLV'/-$(PLV)/g' > "$CACHEFILE"
100 else
101 if [[ "$RELATION" != ".suggests" ]] ; then
102 printf "ERROR: Prerequisite %s not found\n" "$PREREQ" >&2
103 cat "$MANIFEST"
105 # Cache negative result
106 [[ ! -e "$CACHEFILE" ]] && > "$CACHEFILE"
110 # Use cached result, add required version number and drop runtime/perl
111 cat "$CACHEFILE" | sed -e 's/\(fmri=[^ ]*\)/\1'$VER'/g' -e '/fmri=pkg:\/runtime\/perl-\$(PLV)/d'
112 done | /usr/bin/pkgfmt -u | uniq | (
113 PREV_LINE_H=
114 PREV_FMRI=
115 PREV_VER=
116 PREV_LINE_T=
117 while read LINE ; do
118 # Copy verbatim lines without "fmri=pkg:" (e.g. unresolved lines)
119 [[ "${LINE//*fmri=pkg:*/FMRI}" != "FMRI" ]] && printf "%s\n" "$LINE" && continue
121 LINE_H=${LINE%%fmri=*}
122 LINE_T=${LINE#*fmri=}
123 FMRI=${LINE_T%% *}
124 LINE_T=${LINE_T#* }
125 [[ "$FMRI" == "$LINE_T" ]] && LINE_T=
126 VER=${FMRI##*@}
127 FMRI=${FMRI%%@*}
128 [[ "$FMRI" == "$VER" ]] && VER=
129 [[ -n "$VER" ]] && VER="@$VER"
130 FMRI="fmri=$FMRI"
132 if [[ "$PREV_LINE_H" != "$LINE_H" || "$PREV_FMRI" != "$FMRI" || "$PREV_LINE_T" != "$LINE_T" ]] ; then
133 [[ -n "$PREV_LINE_H$PREV_FMRI$PREV_VER$PREV_LINE_T" ]] && printf "%s%s%s%s\n" "$PREV_LINE_H" "$PREV_FMRI" "$PREV_VER" "$PREV_LINE_T"
134 PREV_LINE_H="$LINE_H"
135 PREV_FMRI="$FMRI"
136 PREV_VER="$VER"
137 PREV_LINE_T="$LINE_T"
138 continue
141 # Select higher version
142 VER1=${PREV_VER#@}
143 VER2=${VER#@}
144 while true ; do
145 [[ -z "$VER1" ]] && PREV_VER="$VER" && break
146 [[ -z "$VER2" ]] && break
148 V1=${VER1%%.*} ; [[ "$V1" == "$VER1" ]] && VER1="$VER1."
149 V2=${VER2%%.*} ; [[ "$V2" == "$VER2" ]] && VER2="$VER2."
151 ((V1 < V2)) && PREV_VER="$VER" && break
152 ((V1 > V2)) && break
154 VER1=${VER1#*.}
155 VER2=${VER2#*.}
156 done
157 done
158 [[ -n "$PREV_LINE_H$PREV_FMRI$PREV_VER$PREV_LINE_T" ]] && printf "%s%s%s%s\n" "$PREV_LINE_H" "$PREV_FMRI" "$PREV_VER" "$PREV_LINE_T"
161 exit 0