Andreas Wacknitz
2024-04-05 f09dca6e20c67020d6f337fc95eec480de4d24ef
tools/perl-meta-deps
@@ -18,7 +18,7 @@
function usage
{
   [[ -n "$1" ]] && printf "ERROR: %s\n\n" "$1"
   printf "Usage: perl-meta-deps WS_MACH BUILD_DIR [build|test|runtime] PERL_VERSION\n" >&2
   printf "Usage: perl-meta-deps WS_MACH BUILD_DIR [configure|build|test|runtime]... [suggests] PERL_VERSION\n" >&2
   [[ -n "$1" ]] && exit 1
   exit 0
}
@@ -32,18 +32,25 @@
BUILD_DIR="$1"
[[ -d "$BUILD_DIR" ]] && shift || usage "BUILD_DIR does not exist"
PHASES=".configure,.build,.test,.runtime"
case "$1" in
   "build")   PHASES=".configure,.build" && shift ;;
   "test")      PHASES=".test" && shift ;;
   "runtime")   PHASES=".runtime" && shift ;;
esac
PHASES=
while true ; do
   case "$1" in
      configure|build|test|runtime)
         PHASES="$PHASES,.$1" && shift ;;
      *)   break ;;
   esac
done
PHASES=${PHASES/#,}
[[ -z "$PHASES" ]] && PHASES=".configure,.build,.test,.runtime"
RELATION=".requires,.recommends"
[[ "$1" == "suggests" ]] && RELATION=".$1" && shift
(($# == 0)) && usage "PERL_VERSION missing"
PERLVER=$1 && shift
PLV=${PERLVER//.}
PERL=/usr/perl5/$PERLVER/bin/perl
[[ -x "$PERL" ]] || usage "perl $PERLVER not found"
[[ -x "$PERL" ]] || usage "Perl $PERLVER not found"
PERL_ARCH=$($PERL -e 'use Config; print $Config{archname}')
(($# != 0)) && usage "Too many arguments"
@@ -57,17 +64,17 @@
find "$CACHEDIR" -type f -mtime +7 -exec rm -f {} \;
printf "\n# Automatically generated dependencies based on distribution metadata\n"
/usr/bin/jq -r '.prereqs|'"$PHASES"'|.requires|to_entries?|.[]|.key+" "+(.value|tostring)' | while read PREREQ VERSION ; do
/usr/bin/jq -r '.prereqs|'"$PHASES"'|'"$RELATION"'|to_entries?|.[]|.key+" "+(.value|tostring)' | while read PREREQ VERSION ; do
   # Convert perl version number to pkg(5) compatible form
   VER=$("$PERL_VERSION_CONVERT" "$PREREQ" "$VERSION")
   [[ "$VER" != "0" ]] && VER="@$VER" || VER=
   CACHEFILE="$CACHEDIR/$PREREQ-$PERLVER"
   # Resolve the dependency if not in cache already
   if [[ ! -s "$CACHEDIR/$PREREQ" ]] ; then
   if [[ ! -e "$CACHEFILE" || ("$RELATION" != ".suggests" && ! -s "$CACHEFILE") ]] ; then
      # We must replace '::' by '-' otherwise 'pkgdepend resolve' fails
      MANIFEST="$BUILD_DIR/META-${PREREQ//::/-}"
      MANIFEST="$BUILD_DIR/META-${PREREQ//::/-}-$PERLVER"
      # Prepare manifest
      if [[ "$PREREQ" == "perl" ]] ; then
@@ -89,23 +96,27 @@
      # Resolve dependency
      if /usr/bin/pkgdepend resolve "$MANIFEST" ; then
         # Remove version number and cache the result
         cat "$MANIFEST.res" | sed -e 's/@[^ ]*//g' -e 's/-'$PLV'/-$(PLV)/g' > "$CACHEDIR/$PREREQ"
         cat "$MANIFEST.res" | sed -e 's/@[^ ]*//g' -e 's/-'$PLV'/-$(PLV)/g' > "$CACHEFILE"
      else
         printf "ERROR: Prerequisite %s not found\n" "$PREREQ" >&2
         cat "$MANIFEST"
         if [[ "$RELATION" != ".suggests" ]] ; then
            printf "ERROR: Prerequisite %s not found\n" "$PREREQ" >&2
            cat "$MANIFEST"
         fi
         # Cache negative result
         [[ ! -e "$CACHEFILE" ]] && > "$CACHEFILE"
      fi
   fi
   # Use cached result (if any) and add required version number
   [[ -f "$CACHEDIR/$PREREQ" ]] && cat "$CACHEDIR/$PREREQ" | sed  -e 's/\(fmri=[^ ]*\)/\1'$VER'/g'
   # Use cached result, add required version number and drop runtime/perl
   cat "$CACHEFILE" | sed -e 's/\(fmri=[^ ]*\)/\1'$VER'/g' -e '/fmri=pkg:\/runtime\/perl-\$(PLV)/d'
done | /usr/bin/pkgfmt -u | uniq | (
   PREV_LINE_H=
   PREV_FMRI=
   PREV_VER=
   PREV_LINE_T=
   while read LINE ; do
      # Copy verbatim lines without "fmri="
      [[ "${LINE//*fmri=*/FMRI}" != "FMRI" ]] && printf "%s\n" "$LINE" && continue
      # Copy verbatim lines without "fmri=pkg:" (e.g. unresolved lines)
      [[ "${LINE//*fmri=pkg:*/FMRI}" != "FMRI" ]] && printf "%s\n" "$LINE" && continue
      LINE_H=${LINE%%fmri=*}
      LINE_T=${LINE#*fmri=}
@@ -117,10 +128,6 @@
      [[ "$FMRI" == "$VER" ]] && VER=
      [[ -n "$VER" ]] && VER="@$VER"
      FMRI="fmri=$FMRI"
      # There is no connection between module version and
      # runtime/perl version for modules bundled with runtime/perl
      [[ "$FMRI" == 'fmri=pkg:/runtime/perl-$(PLV)' ]] && VER=
      if [[ "$PREV_LINE_H" != "$LINE_H" || "$PREV_FMRI" != "$FMRI" || "$PREV_LINE_T" != "$LINE_T" ]] ; then
         [[ -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"
@@ -149,4 +156,6 @@
      done
   done
   [[ -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"
)| /usr/bin/pkgfmt
)
exit 0