Make llvmdo and llvmgrep invulnerable to where they are run from by getting
[oota-llvm.git] / utils / llvmdo
1 #!/bin/sh
2 ##===- utils/llvmdo - Counts Lines Of Code -------------------*- Script -*-===##
3
4 #                     The LLVM Compiler Infrastructure
5 #
6 # This file was developed by Reid Spencer and is distributed under the 
7 # University of Illinois Open Source License. See LICENSE.TXT for details.
8
9 ##===----------------------------------------------------------------------===##
10 #
11 # This script is a general purpose "apply" function for the source files in LLVM
12 # It uses "find" to locate all the source files and then applies the user's 
13 # command to them. As such, this command is often not used by itself much but
14 # the other find related tools (countloc.sh,llvmgrep,getsrcs.sh) are all based 
15 # on the implementation. This script defines "what is a source file" in LLVM and
16 # so should be maintained if new directories, new file extensions, etc. are 
17 # used in LLVM as it progresses.
18 #
19 # Usage:
20 #  llvmdo [-dirs "DIRNAMES..."] PROGRAM ARGS...
21 #
22 # The -dirs argument allows you to specify the set of directories that are 
23 # searched. By default, everything is searched. Note that you must use quotes
24 # around the list of directory names. After that you simply specify whatever
25 # program you want to run against each file and the arguments to give it. The
26 # PROGRAM will be given the file name as its last argument.
27 ##===----------------------------------------------------------------------===##
28
29 if test $# -lt 1 ; then
30   echo "Usage: llvmdo [-dirs "DIRNAMES..."] PROGRAM ARGS..."
31   exit 1
32 fi
33
34 if test "$1" = "-dirs" ; then
35   LLVMDO_DIRS="$2";
36   shift ; shift
37 elif test -z "$LLVMDO_DIRS" ; then
38   LLVMDO_DIRS="include lib tools utils runtime autoconf docs test examples projects"
39 fi
40 if test "$1" = "" ; then
41   echo "Missing program name to run"
42   exit 1
43 fi
44
45 PROGRAM=`which $1`
46 if test ! -x "$PROGRAM" ; then
47   echo "Can't execute $1"
48   exit 1
49 fi
50 shift;
51
52 TOPDIR=`llvm-config --src-root`
53
54 if test -d "$TOPDIR" ; then
55   cd $TOPDIR
56   case `uname -s` in
57     SunOS) find_prog=gfind ;;
58     *) find_prog=find ;;
59   esac
60   $find_prog $LLVMDO_DIRS -type f \
61     \( \
62       -path 'docs/doxygen/*' -o \
63       -path 'docs/CommandGuide/html/*' -o \
64       -path 'docs/CommandGuide/man/*' -o \
65       -path 'docs/CommandGuide/ps/*' -o \
66       -path 'docs/CommandGuide/man/*' -o \
67       -path 'docs/HistoricalNotes/*' -o \
68       -path 'docs/img/*' -o \
69       -path '*/.libs/*' \
70     \) -prune  -o \( \
71       \( \
72          -name '*.cpp' -o \
73          -name '*.h' -o \
74          -name '*.def' -o \
75          -name '*.c' -o \
76          -name '*.l' -o \
77          -name '*.y' -o \
78          -name '*.td' -o \
79          -name '*.py' -o \
80          -name '*.pl' -o \
81          -name '*.sh' -o \
82          -name '*.lst' -o \
83          -name '*.pod' -o \
84          -name '*.html' -o \
85          -name '*.css' -o \
86          -name '*.cfg' -o \
87          -name '*.cc' -o \
88          -name '*.txt' -o \
89          -name '*.TXT' -o \
90          -name '*.el' -o \
91          -name '*.m4' -o \
92          -name '*.in' -o \
93          -name '*.ac' -o \
94          -name '*.tr' -o \
95          -name '*.vim' -o \
96          -name '*.gnuplot' -o \
97          -name 'Make*' -o \
98          -path 'test/*.ll' -o \
99          -path 'runtime/*.ll' \
100       \) \
101       \! -name '.*' \
102       \! -name '*~' \
103       \! -name '#*' \
104       \! -name 'Lexer.cpp' \
105       \! -name 'llvmAsmParser.cpp' \
106       \! -name 'llvmAsmParser.h' \
107       \! -name 'FileLexer.cpp' \
108       \! -name 'FileParser.cpp' \
109       \! -name 'FileParser.h' \
110       \! -name 'StackerParser.h' \
111       \! -name 'StackerParser.cpp' \
112       \! -name 'ConfigLexer.cpp' \
113       \! -name 'PPCPerfectShuffle.h' \
114      -exec $PROGRAM "$@" {} \; \
115     \)
116 else
117   echo "Can't find LLVM top directory in $TOPDIR"
118 fi
119