blob: bcfc221c2b1b503af1face6804b96954853eb115 [file] [log] [blame]
Reid Spencera60ff2e2004-07-26 22:52:44 +00001#!/bin/sh
Reid Spencer33709e52004-09-20 08:00:09 +00002##===- utils/llvmdo - Counts Lines Of Code -------------------*- Script -*-===##
3#
4# The LLVM Compiler Infrastructure
5#
Chris Lattner30609102007-12-29 20:37:13 +00006# This file is distributed under the University of Illinois Open Source
7# License. See LICENSE.TXT for details.
Reid Spencer33709e52004-09-20 08:00:09 +00008#
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
Reid Spencerca7592a2006-08-14 18:49:05 +000014# the other find related tools (countloc.sh,llvmgrep,getsrcs.sh,userloc.sh) are
15# all based on this script. This script defines "what is a source file" in
16# LLVM and so should be maintained if new directories, new file extensions,
17# etc. are used in LLVM as it progresses.
Reid Spencer33709e52004-09-20 08:00:09 +000018#
19# Usage:
Reid Spencerca7592a2006-08-14 18:49:05 +000020# llvmdo [-topdir DIR] [-dirs "DIRNAMES..."] [-code-only] PROGRAM ARGS...
21#
22# The -topdir option allows you to specify the llvm source root directly. If it
23# is not specified then it will be obtained with llvm-config which must be built
24# and in your path.
Reid Spencer33709e52004-09-20 08:00:09 +000025#
26# The -dirs argument allows you to specify the set of directories that are
Reid Spencerca7592a2006-08-14 18:49:05 +000027# searched. The default list of directories searched is:
28# include lib tools utils runtime autoconf docs test examples projects
29# Note that you must use quotes around the list of directory names.
30#
31# The -code-only option specifies that only those files that are considered
32# "code" should be visited. HTML documentation is considered code, but things
33# like README files, etc. are not.
34#
35# Finally, you simply specify whatever program you want to run against each
36# file and the arguments to give it. The PROGRAM will be given the file name
37# as its last argument.
Reid Spencer9610fc92004-09-20 08:04:13 +000038##===----------------------------------------------------------------------===##
Reid Spencer8141e372004-09-20 07:21:19 +000039
40if test $# -lt 1 ; then
Reid Spencerca7592a2006-08-14 18:49:05 +000041 echo "Usage: llvmdo [-topdir DIR] [-dirs "DIRNAMES..."] [-code-only] PROGRAM ARGS..."
Reid Spencer1e75b7b2006-08-11 21:53:27 +000042 exit 1
Reid Spencer8141e372004-09-20 07:21:19 +000043fi
44
Reid Spencerca7592a2006-08-14 18:49:05 +000045if test "$1" = "-topdir" ; then
46 TOPDIR="$2"
47 shift; shift;
48else
49 TOPDIR=`llvm-config --src-root`
50fi
51
Reid Spencer8141e372004-09-20 07:21:19 +000052if test "$1" = "-dirs" ; then
Reid Spencerca7592a2006-08-14 18:49:05 +000053 LLVMDO_DIRS="$2"
Reid Spencer8141e372004-09-20 07:21:19 +000054 shift ; shift
55elif test -z "$LLVMDO_DIRS" ; then
Daniel Dunbar4f80d712009-07-16 00:06:36 +000056 LLVMDO_DIRS="include lib tools utils runtime autoconf docs test examples projects cmake"
Reid Spencer8141e372004-09-20 07:21:19 +000057fi
Reid Spencer956dae82006-08-13 18:28:27 +000058
59if test "$1" = "-code-only" ; then
60 CODE_ONLY="set"
61 shift
62else
63 CODE_ONLY=""
64fi
65
Reid Spencer1e75b7b2006-08-11 21:53:27 +000066if test "$1" = "" ; then
67 echo "Missing program name to run"
68 exit 1
69fi
70
Reid Spencera60ff2e2004-07-26 22:52:44 +000071PROGRAM=`which $1`
Reid Spencer8141e372004-09-20 07:21:19 +000072if test ! -x "$PROGRAM" ; then
Reid Spencera60ff2e2004-07-26 22:52:44 +000073 echo "Can't execute $1"
74 exit 1
75fi
76shift;
Reid Spencer1e75b7b2006-08-11 21:53:27 +000077
Reid Spencerca7592a2006-08-14 18:49:05 +000078paths_to_ignore="\
Reid Spencer75f75b92007-07-04 01:35:32 +000079 -path */.svn/ -o \
80 -path */.svn/* -o \
Reid Spencerca7592a2006-08-14 18:49:05 +000081 -path docs/doxygen/* -o \
82 -path docs/CommandGuide/html/* -o \
83 -path docs/CommandGuide/man/* -o \
84 -path docs/CommandGuide/ps/* -o \
85 -path docs/CommandGuide/man/* -o \
86 -path docs/HistoricalNotes/* -o \
87 -path docs/img/* -o \
88 -path */.libs/* -o \
89 -path lib/Support/bzip2/* -o \
90 -path projects/llvm-test/* \
91"
92files_to_match="\
93 -name *.ac \
94 -o -name *.b \
95 -o -name *.c \
96 -o -name *.cc \
97 -o -name *.cfg \
98 -o -name *.cpp \
99 -o -name *.css \
100 -o -name *.def \
101 -o -name *.el \
102 -o -name *.exp \
103 -o -name *.footer \
104 -o -name *.gnuplot' \
105 -o -name *.h \
106 -o -name *.header \
107 -o -name *.html \
108 -o -name *.in \
109 -o -name *.inc \
110 -o -name *.intro \
111 -o -name *.l \
112 -o -name *.ll \
Reid Spencerca7592a2006-08-14 18:49:05 +0000113 -o -name *.lst \
114 -o -name *.m4 \
115 -o -name *.pod \
116 -o -name *.pl \
117 -o -name *.py \
118 -o -name *.sh \
119 -o -name *.schema \
120 -o -name *.st \
121 -o -name *.tcl \
122 -o -name *.td \
123 -o -name *.tr \
124 -o -name *.y \
125 -o -name Make* \
Daniel Dunbar4f80d712009-07-16 00:06:36 +0000126 -o -name *.cmake \
Reid Spencerca7592a2006-08-14 18:49:05 +0000127 -o -name llvmdo \
128 -o -name llvmgrep \
129 -o -name check-each-file \
130 -o -name codgen-diff \
Reid Spencerca7592a2006-08-14 18:49:05 +0000131 -o -name llvm-native-gcc \
132 -o -name llvm-native-gxx \
133 -o -name makellvm \
134 -o -path include/llvm/ADT/ilist \
135 -o -path test/\*.ll \
136 -o -path test/Scripts/not \
137 -o -path runtime/\*.ll \
138"
139if test -z "$CODE_ONLY" ; then
140 files_to_match="$files_to_match \
141 -o -name *.txt \
142 -o -name *.TXT \
143 -o -name *.vim \
144 -o -name vimrc \
145 -o -name README \
146 -o -name COPYING.LIB \
147 -o -name LICENSE* "
148fi
149files_to_ignore="\
150 -name \.* \
151 -o -name *~ \
152 -o -name #* \
Reid Spencerca7592a2006-08-14 18:49:05 +0000153 -o -name configure \
154 -o -name slow.ll \
155 -o -name *libtool* \
156 -o -name ltdl* \
157 -o -name ltdl.m4 \
158 -o -name ltmain.m4 \
159 -o -name ltmain.sh \
160 -o -name aclocal.m4 \
161 -o -name acinclude.m4 \
Reid Spencerca7592a2006-08-14 18:49:05 +0000162 -o -name *LoopSimplifyCrash.ll \
163 -o -name *AST-Remove.ll \
Reid Spencerca7592a2006-08-14 18:49:05 +0000164 -o -name PPCPerfectShuffle.h \
165"
Reid Spencer1e75b7b2006-08-11 21:53:27 +0000166
Reid Spencera60ff2e2004-07-26 22:52:44 +0000167if test -d "$TOPDIR" ; then
168 cd $TOPDIR
Reid Spencerca7592a2006-08-14 18:49:05 +0000169 # Have to use the right "find" on a per-platform basis. Most platforms have
170 # Gnu find as "find", but Solaris does not.
Reid Spencer054defa2004-10-07 16:03:21 +0000171 case `uname -s` in
172 SunOS) find_prog=gfind ;;
173 *) find_prog=find ;;
174 esac
Reid Spencer956dae82006-08-13 18:28:27 +0000175 # Turn off file name generation (globbing) so that substitution of the
176 # variables doesn't cause the shell to create lists of file names
177 set -f
Reid Spencer054defa2004-10-07 16:03:21 +0000178 $find_prog $LLVMDO_DIRS -type f \
Reid Spencer956dae82006-08-13 18:28:27 +0000179 \( $paths_to_ignore \) -prune \
180 -o \( \( $files_to_match \) \! \( $files_to_ignore \) \
181 -exec $PROGRAM "$@" {} \; \)
Reid Spencera60ff2e2004-07-26 22:52:44 +0000182else
183 echo "Can't find LLVM top directory in $TOPDIR"
184fi