1#!/bin/sh
2##===- utils/llvmdo - Counts Lines Of Code -------------------*- Script -*-===##
3#
4# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5# See https://llvm.org/LICENSE.txt for license information.
6# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7#
8##===----------------------------------------------------------------------===##
9#
10# This script is a general purpose "apply" function for the source files in LLVM
11# It uses "find" to locate all the source files and then applies the user's
12# command to them. As such, this command is often not used by itself much but
13# the other find related tools (countloc.sh,llvmgrep,getsrcs.sh,userloc.sh) are
14# all based on this script.  This script defines "what is a source file" in
15# LLVM and so should be maintained if new directories, new file extensions,
16# etc. are used in LLVM as it progresses.
17#
18# Usage:
19#  llvmdo [-topdir DIR] [-dirs "DIRNAMES..."] [-code-only] PROGRAM ARGS...
20#
21# The -topdir option allows you to specify the llvm source root directly. If it
22# is not specified then it will be obtained with llvm-config which must be built
23# and in your path.
24#
25# The -dirs argument allows you to specify the set of directories that are
26# searched. The default list of directories searched is:
27#   include lib tools utils runtime autoconf docs test examples projects
28# Note that you must use quotes around the list of directory names.
29#
30# The -code-only option specifies that only those files that are considered
31# "code" should be visited. HTML documentation is considered code, but things
32# like README files, etc. are not.
33#
34# Finally, you simply specify whatever program you want to run against each
35# file and the arguments to give it. The PROGRAM will be given the file name
36# as its last argument.
37##===----------------------------------------------------------------------===##
38
39if test $# -lt 1 ; then
40  echo "Usage: llvmdo [-topdir DIR] [-dirs "DIRNAMES..."] [-code-only] PROGRAM ARGS..."
41  exit 1
42fi
43
44if test "$1" = "-topdir" ; then
45  TOPDIR="$2"
46  shift; shift;
47else
48  TOPDIR=`llvm-config --src-root`
49fi
50
51if test "$1" = "-dirs" ; then
52  LLVMDO_DIRS="$2"
53  shift ; shift
54elif test -z "$LLVMDO_DIRS" ; then
55  LLVMDO_DIRS="include lib tools utils runtime autoconf docs test examples projects cmake"
56fi
57
58if test "$1" = "-code-only" ; then
59  CODE_ONLY="set"
60  shift
61else
62  CODE_ONLY=""
63fi
64
65if test "$1" = "" ; then
66  echo "Missing program name to run"
67  exit 1
68fi
69
70PROGRAM=`which $1`
71if test ! -x "$PROGRAM" ; then
72  echo "Can't execute $1"
73  exit 1
74fi
75shift;
76
77paths_to_ignore="\
78  -path */.svn/ -o \
79  -path */.svn/* -o \
80  -path docs/doxygen/* -o \
81  -path docs/CommandGuide/html/* -o \
82  -path docs/CommandGuide/man/* -o \
83  -path docs/CommandGuide/ps/* -o \
84  -path docs/CommandGuide/man/* -o \
85  -path docs/HistoricalNotes/* -o \
86  -path docs/img/* -o \
87  -path */.libs/* -o \
88  -path lib/Support/bzip2/* -o \
89  -path projects/llvm-test/* \
90"
91files_to_match="\
92     -name *.ac \
93  -o -name *.b \
94  -o -name *.c \
95  -o -name *.cc \
96  -o -name *.cfg \
97  -o -name *.cpp \
98  -o -name *.css \
99  -o -name *.def \
100  -o -name *.el \
101  -o -name *.exp \
102  -o -name *.footer \
103  -o -name *.gnuplot' \
104  -o -name *.h \
105  -o -name *.header \
106  -o -name *.html \
107  -o -name *.in \
108  -o -name *.inc \
109  -o -name *.intro \
110  -o -name *.l \
111  -o -name *.ll \
112  -o -name *.lst \
113  -o -name *.m4 \
114  -o -name *.pod \
115  -o -name *.pl \
116  -o -name *.py \
117  -o -name *.sh \
118  -o -name *.schema \
119  -o -name *.st \
120  -o -name *.tcl \
121  -o -name *.td \
122  -o -name *.tr \
123  -o -name *.y \
124  -o -name Make* \
125  -o -name *.cmake \
126  -o -name llvmdo \
127  -o -name llvmgrep \
128  -o -name check-each-file \
129  -o -name codgen-diff \
130  -o -name llvm-native-gcc \
131  -o -name llvm-native-gxx \
132  -o -name makellvm \
133  -o -path include/llvm/ADT/ilist \
134  -o -path test/\*.ll \
135  -o -path test/Scripts/not \
136  -o -path runtime/\*.ll \
137"
138if test -z "$CODE_ONLY" ; then
139  files_to_match="$files_to_match \
140    -o -name *.txt \
141    -o -name *.TXT \
142    -o -name *.vim \
143    -o -name vimrc \
144    -o -name README \
145    -o -name COPYING.LIB \
146    -o -name LICENSE* "
147fi
148files_to_ignore="\
149     -name \.* \
150  -o -name *~ \
151  -o -name #* \
152  -o -name configure \
153  -o -name slow.ll \
154  -o -name *libtool* \
155  -o -name ltdl* \
156  -o -name ltdl.m4 \
157  -o -name ltmain.m4 \
158  -o -name ltmain.sh \
159  -o -name aclocal.m4 \
160  -o -name acinclude.m4 \
161  -o -name *LoopSimplifyCrash.ll \
162  -o -name *AST-Remove.ll \
163  -o -name PPCPerfectShuffle.h \
164"
165
166if test -d "$TOPDIR" ; then
167  cd $TOPDIR
168  # Have to use the right "find" on a per-platform basis. Most platforms have
169  # Gnu find as "find", but Solaris does not.
170  case `uname -s` in
171    SunOS) find_prog=gfind ;;
172    *) find_prog=find ;;
173  esac
174  # Turn off file name generation (globbing) so that substitution of the
175  # variables doesn't cause the shell to create lists of file names
176  set -f
177  $find_prog $LLVMDO_DIRS -type f \
178    \( $paths_to_ignore \) -prune \
179    -o \( \( $files_to_match \) \! \( $files_to_ignore \) \
180    -exec $PROGRAM "$@" {} \; \)
181else
182  echo "Can't find LLVM top directory in $TOPDIR"
183fi
184