2 # Copyright 2011 The Kyua Authors.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
9 # * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # * Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
14 # * Neither the name of Google Inc. nor the names of its contributors
15 # may be used to endorse or promote products derived from this software
16 # without specific prior written permission.
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 # \file admin/check-style.sh
32 # Sanity checks the coding style of all source files in the project tree.
37 # Prints an error message and exits.
39 # \param ... Parts of the error message; concatenated using a space as the
42 echo "${ProgName}:" "${@}" 1>&2
47 # Locates all source files within the project directory.
49 # We require the project to have been configured in a directory that is separate
50 # from the source tree. This is to allow us to easily filter out build
51 # artifacts from our search.
53 # \param srcdir Absolute path to the source directory.
54 # \param builddir Absolute path to the build directory.
55 # \param tarname Basename of the project's tar file, to skip possible distfile
58 local srcdir="${1}"; shift
59 local builddir="${1}"; shift
60 local tarname="${1}"; shift
66 \! -path "*/.deps/*" \
67 \! -path "*/autom4te.cache/*" \
68 \! -path "*/${tarname}-[0-9]*/*" \
69 \! -path "*/${builddir##*/}/*" \
70 \! -name "Makefile.in" \
71 \! -name "aclocal.m4" \
72 \! -name "config.h.in" \
73 \! -name "configure" \
79 # Prints the style rules applicable to a given file.
81 # \param file Path to the source file.
83 local file="${1}"; shift
86 */ax_cxx_compile_stdcxx.m4) ;;
88 *Makefile*) echo common make ;;
89 *.[0-9]) echo common man ;;
90 *.cpp|*.hpp) echo common cpp ;;
91 *.sh) echo common shell ;;
97 # Validates a given file against the rules that apply to it.
99 # \param srcdir Absolute path to the source directory.
100 # \param file Name of the file to validate relative to srcdir.
102 # \return 0 if the file is valid; 1 otherwise, in which case the style
103 # violations are printed to the output.
105 local srcdir="${1}"; shift
106 local file="${1}"; shift
109 for rule in $(guess_rules "${file}"); do
110 awk -f "${srcdir}/admin/check-style-${rule}.awk" \
111 "${srcdir}/${file}" || err=1
122 local tarname=UNKNOWN
125 while getopts :b:s:t: arg; do
140 err "Unknown option -${OPTARG}"
144 shift $(expr ${OPTIND} - 1)
146 srcdir="$(cd "${srcdir}" && pwd -P)"
147 builddir="$(cd "${builddir}" && pwd -P)"
148 [ "${srcdir}" != "${builddir}" ] || \
149 err "srcdir and builddir cannot match; reconfigure the package" \
150 "in a separate directory"
153 if [ ${#} -gt 0 ]; then
156 sources="$(find_sources "${srcdir}" "${builddir}" "${tarname}")"
160 for file in ${sources}; do
161 local file="$(echo ${file} | sed -e "s,\\./,,")"
163 check_file "${srcdir}" "${file}" || ok=1