Pablo Greco 7b2c62
#! /bin/bash
Pablo Greco 7b2c62
Pablo Greco 7b2c62
Dir=$1
Pablo Greco 7b2c62
List=$2
Pablo Greco 7b2c62
Dest="extra"
Pablo Greco 7b2c62
Pablo Greco 7b2c62
# Destination was specified on the command line
Pablo Greco 7b2c62
test -n "$3" && Dest="$3"
Pablo Greco 7b2c62
Pablo Greco 7b2c62
pushd $Dir
Pablo Greco 7b2c62
rm -rf modnames
Pablo Greco 7b2c62
find . -name "*.ko" -type f > modnames
Pablo Greco 7b2c62
# Look through all of the modules, and throw any that have a dependency in
Pablo Greco 7b2c62
# our list into the list as well.
Pablo Greco 7b2c62
rm -rf dep.list dep2.list
Pablo Greco 7b2c62
rm -rf req.list req2.list
Pablo Greco 7b2c62
touch dep.list req.list
Pablo Greco 7b2c62
cp "$List" .
Pablo Greco 7b2c62
Pablo Greco 7b2c62
# This variable needs to be exported because it is used in sub-script
Pablo Greco 7b2c62
# executed by xargs
Pablo Greco 7b2c62
export ListName=$(basename "$List")
Pablo Greco 7b2c62
Pablo Greco 7b2c62
# NB: this loop runs 2000+ iterations. Try to be fast.
Pablo Greco 7b2c62
NPROC=`nproc`
Pablo Greco 7b2c62
[ -z "$NPROC" ] && NPROC=1
Pablo Greco 7b2c62
cat modnames | xargs -r -n1 -P $NPROC sh -c '
Pablo Greco 7b2c62
  dep=$1
Pablo Greco 7b2c62
  depends=`modinfo $dep | sed -n -e "/^depends/ s/^depends:[ \t]*//p"`
Pablo Greco 7b2c62
  [ -z "$depends" ] && exit
Pablo Greco 7b2c62
  for mod in ${depends//,/ }
Pablo Greco 7b2c62
  do
Pablo Greco 7b2c62
    match=$(grep "^$mod.ko" "$ListName")
Pablo Greco 7b2c62
    [ -z "$match" ] && continue
Pablo Greco 7b2c62
    # check if the module we are looking at is in mod-extra too.
Pablo Greco 7b2c62
    # if so we do not need to mark the dep as required.
Pablo Greco 7b2c62
    mod2=${dep##*/}  # same as `basename $dep`, but faster
Pablo Greco 7b2c62
    match2=$(grep "^$mod2" "$ListName")
Pablo Greco 7b2c62
    if [ -n "$match2" ]
Pablo Greco 7b2c62
    then
Pablo Greco 7b2c62
      #echo $mod2 >> notreq.list
Pablo Greco 7b2c62
      continue
Pablo Greco 7b2c62
    fi
Pablo Greco 7b2c62
    echo $mod.ko >> req.list
Pablo Greco 7b2c62
  done
Pablo Greco 7b2c62
' DUMMYARG0   # xargs appends MODNAME, which becomes $dep in the script above
Pablo Greco 7b2c62
Pablo Greco 7b2c62
sort -u req.list > req2.list
Pablo Greco 7b2c62
sort -u "$ListName" > modules2.list
Pablo Greco 7b2c62
join -v 1 modules2.list req2.list > modules3.list
Pablo Greco 7b2c62
Pablo Greco 7b2c62
for mod in $(cat modules3.list)
Pablo Greco 7b2c62
do
Pablo Greco 7b2c62
  # get the path for the module
Pablo Greco 7b2c62
  modpath=`grep /$mod modnames`
Pablo Greco 7b2c62
  [ -z "$modpath" ] && continue
Pablo Greco 7b2c62
  echo $modpath >> dep.list
Pablo Greco 7b2c62
done
Pablo Greco 7b2c62
Pablo Greco 7b2c62
sort -u dep.list > dep2.list
Pablo Greco 7b2c62
Pablo Greco 7b2c62
# now move the modules into the extra/ directory
Pablo Greco 7b2c62
for mod in `cat dep2.list`
Pablo Greco 7b2c62
do
Pablo Greco 7b2c62
  newpath=`dirname $mod | sed -e "s/kernel\\//$Dest\//"`
Pablo Greco 7b2c62
  mkdir -p $newpath
Pablo Greco 7b2c62
  mv $mod $newpath
Pablo Greco 7b2c62
done
Pablo Greco 7b2c62
Pablo Greco 7b2c62
popd
Pablo Greco 7b2c62
Pablo Greco 7b2c62
# If we're signing modules, we can't leave the .mod files for the .ko files
Pablo Greco 7b2c62
# we've moved in .tmp_versions/.  Remove them so the Kbuild 'modules_sign'
Pablo Greco 7b2c62
# target doesn't try to sign a non-existent file.  This is kinda ugly, but
Pablo Greco 7b2c62
# so is modules-extra.
Pablo Greco 7b2c62
Pablo Greco 7b2c62
for mod in `cat ${Dir}/dep2.list`
Pablo Greco 7b2c62
do
Pablo Greco 7b2c62
  modfile=`basename $mod | sed -e 's/.ko/.mod/'`
Pablo Greco 7b2c62
  rm .tmp_versions/$modfile
Pablo Greco 7b2c62
done
Pablo Greco 7b2c62
Pablo Greco 7b2c62
pushd $Dir
Pablo Greco 7b2c62
rm modnames dep.list dep2.list req.list req2.list
Pablo Greco 7b2c62
rm "$ListName" modules2.list modules3.list
Pablo Greco 7b2c62
popd