#!/bin/bash
function updatedirs()
{
# initialise variables
local devhome=${DEVHOME-$HOME}
local srchome=${SRCHOME-$devhome/src}
local objhome=${OBJHOME-$devhome/obj}
# the other ones
export srcdir=$PWD
export objdir=$PWD
# find out if we're in src or obj
case "$PWD" in
$srchome*)
# we're in a srcdir - try to find the objdir
objdir=${PWD/#$srchome/$objhome}
;;
$objhome*)
# we're in an objdir - try to find the srcdir
srcdir=${PWD/#$objhome/$srchome}
;;
esac
if [ \! -d "$srcdir" ]; then
srcdir=$PWD
fi
if [ \! -d "$objdir" ]; then
objdir=$PWD
fi
}
function findMakefile() {
file="$2"
command cd "$1"
while ! test -f "./$file"; do
command cd .. 2>/dev/null
test "$PWD" == "/" && return
done
echo "$PWD"
}
function run() {
# Check if it's cmake; if so, don't try to colorise the output
test -d "$objdir/CMakeFiles" && progress=false
INSIDE_SPECIAL_MAKE=true
export INSIDE_SPECIAL_MAKE
if $progress; then
if type -p pmake >/dev/null; then
exec pmake "$@"
else
echo >&2 "Warning: cannot locate pmake!"
fi
fi
exec $MAKE "$@"
}
[ "$MAKE" == "$0" ] && unset MAKE
MAKE=${MAKE-/usr/bin/make}
export MAKE
# Don't recurse
test -n "$INSIDE_SPECIAL_MAKE" && \
exec $MAKE "$@"
# safety check
test -f $HOME/.bash_functions && . $HOME/.bash_functions
test "`type -t updatedirs`" == function || $MAKE "$@"
progress=true
file=Makefile
dir=.
args=()
# parse args
while test $# -gt 0 ; do
case "${1}" in
-f*)
file="${1#-f}"
shift
test -z "$file" && {
file="${1}"
shift
}
args=("${args[@]}" -f "$file")
;;
-C*)
dir="${1#-C}"
shift
test -z "$dir" && {
dir="${1}"
shift
}
;;
VERBOSE=1)
progress=false
args=("${args[@]}" "$1")
shift
;;
*)
args=("${args[@]}" "$1")
shift
;;
esac
done
objdir=$(findMakefile "$dir" "$file")
if [ -z "$objdir" ]; then
# See if updatedirs can do something...
objdir=$(command cd "$dir"; updatedirs; findMakefile "$objdir" "$file")
fi
run -C "$objdir" "${args[@]}"
exit 255
# Old code:
# Check if $dir exists
if ! test -d "$dir"; then
# Doesn't exist. Let make complain
run -C "$dir" "${args[@]}"
fi
# Check if $dir is a source or object dir
if test -f "$dir/file"; then
# It's an object dir, since there's a Makefile there
run -C "$dir" "${args[@]}"
fi