openexr: make architecture independent
[oi-userland.git] / tools / perl-meta-deps
blobc8c6a7f85056ea6e6ee824f18cc8ad283678734e
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 [runtime] 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=".configure,.build,.test,.runtime"
36 [[ "$1" == "runtime" ]] && PHASES=".runtime" && shift
38 (($# == 0)) && usage "PERL_VERSION missing"
39 PERLVER=$1 && shift
40 PLV=${PERLVER//.}
41 PERL=/usr/perl5/$PERLVER/bin/perl
42 [[ -x "$PERL" ]] || usage "perl $PERLVER not found"
43 PERL_ARCH=$($PERL -e 'use Config; print $Config{archname}')
45 (($# != 0)) && usage "Too many arguments"
47 PERL_VERSION_CONVERT="$(dirname $0)/perl-version-convert"
48 [[ ! -x "$PERL_VERSION_CONVERT" ]] && printf "ERROR: perl-version-convert not found" && exit 1
50 CACHEDIR="$WS_MACH/perl-meta-deps-cache"
51 mkdir -p "$CACHEDIR"
52 # Remove old entries from the cache
53 find "$CACHEDIR" -type f -mtime +7 -exec rm -f {} \;
56 printf "\n# Automatically generated dependencies based on distribution metadata\n"
58 /usr/bin/jq -r '.prereqs|'"$PHASES"'|.requires|to_entries?|.[]|.key+" "+(.value|tostring)' | while read PREREQ VERSION ; do
59 # Convert perl version number to pkg(5) compatible form
60 VER=$("$PERL_VERSION_CONVERT" "$PREREQ" "$VERSION")
61 [[ "$VER" != "0" ]] && VER="@$VER" || VER=
63 # Resolve the dependency if not in cache already
64 if [[ ! -s "$CACHEDIR/$PREREQ" ]] ; then
65 # We must replace '::' by '-' otherwise 'pkgdepend resolve' fails
66 MANIFEST="$BUILD_DIR/META-${PREREQ//::/-}"
68 # Prepare manifest
69 if [[ "$PREREQ" == "perl" ]] ; then
70 # "perl" is special case
71 cat <<#EOF > "$MANIFEST"
72 depend fmri=__TBD type=require pkg.debug.depend.file=$PREREQ \\
73 pkg.debug.depend.path=usr/perl5/$PERLVER/bin
74 EOF
75 else
76 cat <<#EOF > "$MANIFEST"
77 depend fmri=__TBD type=require pkg.debug.depend.file=${PREREQ//:://}.pm \\
78 pkg.debug.depend.path=usr/perl5/$PERLVER/lib \\
79 pkg.debug.depend.path=usr/perl5/$PERLVER/lib/$PERL_ARCH \\
80 pkg.debug.depend.path=usr/perl5/vendor_perl/$PERLVER \\
81 pkg.debug.depend.path=usr/perl5/vendor_perl/$PERLVER/$PERL_ARCH
82 EOF
85 # Resolve dependency
86 if /usr/bin/pkgdepend resolve "$MANIFEST" ; then
87 # Remove version number and cache the result
88 cat "$MANIFEST.res" | sed -e 's/@[^ ]*//g' -e 's/-'$PLV'/-$(PLV)/g' > "$CACHEDIR/$PREREQ"
89 else
90 printf "ERROR: Prerequisite %s not found\n" "$PREREQ" >&2
91 cat "$MANIFEST"
95 # Use cached result (if any) and add required version number
96 [[ -f "$CACHEDIR/$PREREQ" ]] && cat "$CACHEDIR/$PREREQ" | sed -e 's/\(fmri=[^ ]*\)/\1'$VER'/g'
97 done | /usr/bin/pkgfmt -u | uniq | (
98 PREV_LINE_H=
99 PREV_FMRI=
100 PREV_VER=
101 PREV_LINE_T=
102 while read LINE ; do
103 # Copy verbatim lines without "fmri="
104 [[ "${LINE//*fmri=*/FMRI}" != "FMRI" ]] && printf "%s\n" "$LINE" && continue
106 LINE_H=${LINE%%fmri=*}
107 LINE_T=${LINE#*fmri=}
108 FMRI=${LINE_T%% *}
109 LINE_T=${LINE_T#* }
110 [[ "$FMRI" == "$LINE_T" ]] && LINE_T=
111 VER=${FMRI##*@}
112 FMRI=${FMRI%%@*}
113 [[ "$FMRI" == "$VER" ]] && VER=
114 [[ -n "$VER" ]] && VER="@$VER"
115 FMRI="fmri=$FMRI"
117 # There is no connection between module version and
118 # runtime/perl version for modules bundled with runtime/perl
119 [[ "$FMRI" == 'fmri=pkg:/runtime/perl-$(PLV)' ]] && VER=
121 if [[ "$PREV_LINE_H" != "$LINE_H" || "$PREV_FMRI" != "$FMRI" || "$PREV_LINE_T" != "$LINE_T" ]] ; then
122 [[ -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"
123 PREV_LINE_H="$LINE_H"
124 PREV_FMRI="$FMRI"
125 PREV_VER="$VER"
126 PREV_LINE_T="$LINE_T"
127 continue
130 # Select higher version
131 VER1=${PREV_VER#@}
132 VER2=${VER#@}
133 while true ; do
134 [[ -z "$VER1" ]] && PREV_VER="$VER" && break
135 [[ -z "$VER2" ]] && break
137 V1=${VER1%%.*} ; [[ "$V1" == "$VER1" ]] && VER1="$VER1."
138 V2=${VER2%%.*} ; [[ "$V2" == "$VER2" ]] && VER2="$VER2."
140 ((V1 < V2)) && PREV_VER="$VER" && break
141 ((V1 > V2)) && break
143 VER1=${VER1#*.}
144 VER2=${VER2#*.}
145 done
146 done
147 [[ -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"
148 )| /usr/bin/pkgfmt