- set EXEC = NO_SUCH_TOOL
- set TOOLS = `grep DIRS ${LLVMDIR}/tools/Makefile | sed 's/DIRS[ ]*=[ ]*//'`
- foreach T ( $TOOLS )
- if ($argv[1] == "$T") then
- set EXEC = $T
- shift argv
+ set EXEC = $argv[1]
+endif
+if ($DEBUG) then
+ echo "DEBUG: EXEC = $EXEC"
+endif
+
+## Compute LLVMDIR: the root of the current LLVM tree.
+## It is recorded in the variable LEVEL in Makefile, to compute it
+##
+if (! $?MFILE) then
+ if (-f GNUmakefile) then
+ set MFILE = GNUmakefile
+ else if (-f makefile) then
+ set MFILE = makefile
+ else
+ set MFILE = Makefile
+ endif
+endif
+if ($DEBUG) then
+ echo "DEBUG: MFILE = $MFILE"
+endif
+if (! -f $MFILE) then
+ echo "Missing or invalid makefile: $MFILE"
+ exit 1
+endif
+
+set LLVMDIR = `awk '/LEVEL[ ]*=/ {print $NF}' $MFILE`
+if ($DEBUG) then
+ echo "DEBUG: LLVMDIR = $LLVMDIR"
+endif
+
+if ($#LLVMDIR == 0 || ! -d "$LLVMDIR") then
+ echo "Unable to find LLVM src-root directory or directory is invalid."
+ echo "Are you within a valid LLVM directory for running gmake?"
+ exit 1
+endif
+
+## Try to determine the obj-root directory automatically if not specified
+##
+set OBJROOTDIRLIST = ( ${LLVMDIR} $OBJROOTDIRLIST ) ## add src dir
+if ($?OBJROOT == 0) then
+ ## Try to determine object root directory by looking for Makefile.config
+ foreach objdir ( $OBJROOTDIRLIST )
+ if (-f "${objdir}/Makefile.config") then
+ set OBJROOT = ${objdir}