Create a gist now

Instantly share code, notes, and snippets.

@msharov /uptreemake
Last active May 18, 2016

What would you like to do?
A bash function for working with nonrecursive make projects. Allows building from any subdirectory by automatically walking up the tree to the project makefile and prepending the path to the command.
#! /bin/sh
#
# This make alias helps when working with nonrecursive make projects
# containing subdirectories. Will walk up the tree and invoke the
# first make it finds to build targets in the current subdir.
function make() {
local MAKEDIR TGTDIR MAKECD MAKEARGS
MAKEDIR=$PWD
while [ ! -r $MAKEDIR/Makefile -a $MAKEDIR != "/" ]; do
MAKEDIR=`dirname $MAKEDIR`
done
if [ ! -r $MAKEDIR/Makefile ]; then
echo "No Makefile found"
return
fi
if [ "$PWD" != "$MAKEDIR" ]; then
[ -r Module.mk ] && TGTDIR=`expr $PWD : "$MAKEDIR/\(.*\)"`
MAKECD=" --no-print-directory -C $MAKEDIR"
fi
MAKEARGS=$@
[ -z "$MAKEARGS" ] && MAKEARGS="all"
[ -z "$TGTDIR" ] || MAKEARGS="$TGTDIR/$MAKEARGS"
/usr/bin/make -j 8 $MAKECD $MAKEARGS
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment