1*9152e1c5SLionel Sambuc#!/bin/sh 2*9152e1c5SLionel Sambuc# 3*9152e1c5SLionel Sambuc# syspkgdeps [-a arch] [-m machine] [-s setsdir] [-p prefix] sets 4*9152e1c5SLionel Sambuc# 5*9152e1c5SLionel Sambuc# Compute naive package dependencies based on file & directory 6*9152e1c5SLionel Sambuc# nesting. E.g., if pkg P contains /foo/bar and Q contains /foo, 7*9152e1c5SLionel Sambuc# then Q is considered a dependency of P. 8*9152e1c5SLionel Sambuc# 9*9152e1c5SLionel Sambuc# Each line of output contains two syspkg names, 10*9152e1c5SLionel Sambuc# where the first syspkg depends on the second syspkg. 11*9152e1c5SLionel Sambuc# 12*9152e1c5SLionel Sambuc 13*9152e1c5SLionel Sambuc#set -u 14*9152e1c5SLionel Sambuc 15*9152e1c5SLionel Sambucprog="${0##*/}" 16*9152e1c5SLionel Sambucrundir="$(dirname "$0")" # ${0%/*} isn't good enough when there's no "/" 17*9152e1c5SLionel Sambuc. "${rundir}/sets.subr" 18*9152e1c5SLionel Sambuc 19*9152e1c5SLionel Sambuc# 20*9152e1c5SLionel Sambuc# set defaults 21*9152e1c5SLionel Sambuc# 22*9152e1c5SLionel Sambucprefix=/ 23*9152e1c5SLionel Sambuc 24*9152e1c5SLionel Sambucusage() 25*9152e1c5SLionel Sambuc{ 26*9152e1c5SLionel Sambuc cat 1>&2 <<USAGE 27*9152e1c5SLionel SambucUsage: ${0##*/} [-a arch] [-m machine] [-s setsdir] [-p prefix] setname [...] 28*9152e1c5SLionel Sambuc -a arch set arch (e.g, m68k, mips, powerpc) [${MACHINE_ARCH}] 29*9152e1c5SLionel Sambuc -m machine set machine (e.g, amiga, i386, macppc) [${MACHINE}] 30*9152e1c5SLionel Sambuc -s setsdir directory to find sets [${setsdir}] 31*9152e1c5SLionel Sambuc -p prefix prefix for created plist [${prefix}] 32*9152e1c5SLionel Sambuc setname [...] sets to find dependencies for 33*9152e1c5SLionel SambucUSAGE 34*9152e1c5SLionel Sambuc exit 1 35*9152e1c5SLionel Sambuc} 36*9152e1c5SLionel Sambuc 37*9152e1c5SLionel Sambuc# parse arguments 38*9152e1c5SLionel Sambucwhile getopts a:m:p:s: ch; do 39*9152e1c5SLionel Sambuc case ${ch} in 40*9152e1c5SLionel Sambuc a) 41*9152e1c5SLionel Sambuc MACHINE_ARCH="${OPTARG}" 42*9152e1c5SLionel Sambuc MACHINE_CPU="$(arch_to_cpu "${OPTARG}")" 43*9152e1c5SLionel Sambuc ;; 44*9152e1c5SLionel Sambuc m) 45*9152e1c5SLionel Sambuc MACHINE="${OPTARG}" 46*9152e1c5SLionel Sambuc ;; 47*9152e1c5SLionel Sambuc p) 48*9152e1c5SLionel Sambuc prefix="${OPTARG}" 49*9152e1c5SLionel Sambuc ;; 50*9152e1c5SLionel Sambuc s) 51*9152e1c5SLionel Sambuc setsdir="${OPTARG}" 52*9152e1c5SLionel Sambuc ;; 53*9152e1c5SLionel Sambuc *) 54*9152e1c5SLionel Sambuc usage 55*9152e1c5SLionel Sambuc ;; 56*9152e1c5SLionel Sambuc esac 57*9152e1c5SLionel Sambucdone 58*9152e1c5SLionel Sambucshift $((${OPTIND} - 1)) 59*9152e1c5SLionel Sambucif [ $# -lt 1 ]; then 60*9152e1c5SLionel Sambuc usage 61*9152e1c5SLionel Sambucfi 62*9152e1c5SLionel Sambuc 63*9152e1c5SLionel Sambucsets="$*" 64*9152e1c5SLionel Sambuccase "${sets}" in 65*9152e1c5SLionel Sambucall) sets="${nlists}" ;; 66*9152e1c5SLionel Sambucesac 67*9152e1c5SLionel Sambuc 68*9152e1c5SLionel Sambuc# TBD clean up 69*9152e1c5SLionel SambucSCRATCH="$(${MKTEMP} -d "/var/tmp/${prog}.XXXXXX")" 70*9152e1c5SLionel Sambuc 71*9152e1c5SLionel Sambucif [ $? -ne 0 ]; then 72*9152e1c5SLionel Sambuc echo >&2 "${prog}: Could not create scratch directory." 73*9152e1c5SLionel Sambuc exit 1 74*9152e1c5SLionel Sambucfi 75*9152e1c5SLionel Sambuc 76*9152e1c5SLionel SambucPATH_MEMBERSHIP="${SCRATCH}/path-membership" 77*9152e1c5SLionel SambucPATH_TO_PKGNAME="${SCRATCH}/pathpkg.db" 78*9152e1c5SLionel SambucPARENT_PKGNAMES="${SCRATCH}/parent-pkgnames" 79*9152e1c5SLionel SambucPARENT_PATHNAMES="${SCRATCH}/parent-pathnames" 80*9152e1c5SLionel Sambuc 81*9152e1c5SLionel Sambucecho >&2 "${prog}: indexing packages by pathnames" 82*9152e1c5SLionel Sambuc 83*9152e1c5SLionel Sambuclist_set_files ${sets} | ${SED} 's/^\.\///' | \ 84*9152e1c5SLionel Sambuc${ENV_CMD} PREFIX="${prefix}" ${AWK} '{ 85*9152e1c5SLionel Sambuc if ($1 == ".") { 86*9152e1c5SLionel Sambuc print ENVIRON["PREFIX"] " " $2; 87*9152e1c5SLionel Sambuc } else { 88*9152e1c5SLionel Sambuc print ENVIRON["PREFIX"] $1 " " $2; 89*9152e1c5SLionel Sambuc } 90*9152e1c5SLionel Sambuc}' | ${SORT} -k 1 -u > "${PATH_MEMBERSHIP}" 91*9152e1c5SLionel Sambuc 92*9152e1c5SLionel Sambuc${DB} -q -w -f - btree "${PATH_TO_PKGNAME}" < "${PATH_MEMBERSHIP}" 93*9152e1c5SLionel Sambuc 94*9152e1c5SLionel Sambucif [ $? -ne 0 ]; then 95*9152e1c5SLionel Sambuc echo >&2 "${prog}: error creating database, aborting" 96*9152e1c5SLionel Sambuc exit 1 97*9152e1c5SLionel Sambucfi 98*9152e1c5SLionel Sambuc 99*9152e1c5SLionel Sambucecho >&2 "${prog}: computing parent pathnames" 100*9152e1c5SLionel Sambuc 101*9152e1c5SLionel Sambucwhile read pathname pkgname; do 102*9152e1c5SLionel Sambuc # print parent pathname. 103*9152e1c5SLionel Sambuc # (This uses a cheap implementation of dirname from sets.subr.) 104*9152e1c5SLionel Sambuc dirname "${pathname}" 105*9152e1c5SLionel Sambucdone < "${PATH_MEMBERSHIP}" > "${PARENT_PATHNAMES}" 106*9152e1c5SLionel Sambuc 107*9152e1c5SLionel Sambucecho >&2 "${prog}: selecting parent packages using parent pathnames" 108*9152e1c5SLionel Sambuc 109*9152e1c5SLionel Sambuc${DB} -q -f - btree "${PATH_TO_PKGNAME}" < "${PARENT_PATHNAMES}" | \ 110*9152e1c5SLionel Sambuc ${PASTE} "${PATH_MEMBERSHIP}" - | \ 111*9152e1c5SLionel Sambuc ${AWK} '{ if ($2 != $4) print $2 " " $4; }' | \ 112*9152e1c5SLionel Sambuc ${SORT} -u > "${SCRATCH}/alldeps" 113*9152e1c5SLionel Sambuc 114*9152e1c5SLionel Sambucif [ $? -ne 0 ]; then 115*9152e1c5SLionel Sambuc echo >&2 "${prog}: error in parent-directory lookup, aborting" 116*9152e1c5SLionel Sambuc exit 1 117*9152e1c5SLionel Sambucfi 118*9152e1c5SLionel Sambuc 119*9152e1c5SLionel Sambucecho >&2 "${prog}: checking for cyclic dependencies" 120*9152e1c5SLionel Sambuc 121*9152e1c5SLionel Sambuctsort_errors="$(${TSORT} < "${SCRATCH}/alldeps" 2>&1 >/dev/null)" 122*9152e1c5SLionel Sambuc 123*9152e1c5SLionel Sambucif [ -n "${tsort_errors}" ]; then 124*9152e1c5SLionel Sambuc # Errors from tsort are usually to do with cyclic dependencies. 125*9152e1c5SLionel Sambuc # The most likely underlying cause is that /foo and /foo/bar/baz 126*9152e1c5SLionel Sambuc # are in syspkg A, but /foo/bar is in syspkg B. 127*9152e1c5SLionel Sambuc echo >&2 "${tsort_errors}" # this is likely to be multiple lines 128*9152e1c5SLionel Sambuc echo >&2 "${prog}: Above messages probably indicate an error in the lists" 129*9152e1c5SLionel Sambuc exit 1 130*9152e1c5SLionel Sambucfi 131*9152e1c5SLionel Sambuc 132*9152e1c5SLionel Sambucecho >&2 "${prog}: removing redundant dependencies" 133*9152e1c5SLionel Sambuc 134*9152e1c5SLionel Sambuc${HOST_SH} "${rundir}/culldeps" < "${SCRATCH}/alldeps" 135*9152e1c5SLionel Sambuc 136*9152e1c5SLionel Sambucif [ $? -ne 0 ]; then 137*9152e1c5SLionel Sambuc echo >&2 "${prog}: error in culldeps, aborting" 138*9152e1c5SLionel Sambuc exit 1 139*9152e1c5SLionel Sambucfi 140