#! /bin/sh1# Copyright 2011 The Kyua Authors.2# All rights reserved.3#4# Redistribution and use in source and binary forms, with or without5# modification, are permitted provided that the following conditions are6# met:7#8# * Redistributions of source code must retain the above copyright9# notice, this list of conditions and the following disclaimer.10# * Redistributions in binary form must reproduce the above copyright11# notice, this list of conditions and the following disclaimer in the12# documentation and/or other materials provided with the distribution.13# * Neither the name of Google Inc. nor the names of its contributors14# may be used to endorse or promote products derived from this software15# without specific prior written permission.16#17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS18# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT19# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR20# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT21# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,22# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT23# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2829# \file admin/check-style.sh30#31# Sanity checks the coding style of all source files in the project tree.3233ProgName="${0##*/}"343536# Prints an error message and exits.37#38# \param ... Parts of the error message; concatenated using a space as the39# separator.40err() {41echo "${ProgName}:" "${@}" 1>&242exit 143}444546# Locates all source files within the project directory.47#48# We require the project to have been configured in a directory that is separate49# from the source tree. This is to allow us to easily filter out build50# artifacts from our search.51#52# \param srcdir Absolute path to the source directory.53# \param builddir Absolute path to the build directory.54# \param tarname Basename of the project's tar file, to skip possible distfile55# directories.56find_sources() {57local srcdir="${1}"; shift58local builddir="${1}"; shift59local tarname="${1}"; shift6061(62cd "${srcdir}"63find . -type f -a \64\! -path "*/.git/*" \65\! -path "*/.deps/*" \66\! -path "*/autom4te.cache/*" \67\! -path "*/${tarname}-[0-9]*/*" \68\! -path "*/${builddir##*/}/*" \69\! -name "Makefile.in" \70\! -name "aclocal.m4" \71\! -name "config.h.in" \72\! -name "configure" \73\! -name "testsuite"74)75}767778# Prints the style rules applicable to a given file.79#80# \param file Path to the source file.81guess_rules() {82local file="${1}"; shift8384case "${file}" in85*/ax_cxx_compile_stdcxx.m4) ;;86*/ltmain.sh) ;;87*Makefile*) echo common make ;;88*.[0-9]) echo common man ;;89*.cpp|*.hpp) echo common cpp ;;90*.sh) echo common shell ;;91*) echo common ;;92esac93}949596# Validates a given file against the rules that apply to it.97#98# \param srcdir Absolute path to the source directory.99# \param file Name of the file to validate relative to srcdir.100#101# \return 0 if the file is valid; 1 otherwise, in which case the style102# violations are printed to the output.103check_file() {104local srcdir="${1}"; shift105local file="${1}"; shift106107local err=0108for rule in $(guess_rules "${file}"); do109awk -f "${srcdir}/admin/check-style-${rule}.awk" \110"${srcdir}/${file}" || err=1111done112113return ${err}114}115116117# Entry point.118main() {119local builddir=.120local srcdir=.121local tarname=UNKNOWN122123local arg124while getopts :b:s:t: arg; do125case "${arg}" in126b)127builddir="${OPTARG}"128;;129130s)131srcdir="${OPTARG}"132;;133134t)135tarname="${OPTARG}"136;;137138\?)139err "Unknown option -${OPTARG}"140;;141esac142done143shift $(expr ${OPTIND} - 1)144145srcdir="$(cd "${srcdir}" && pwd -P)"146builddir="$(cd "${builddir}" && pwd -P)"147[ "${srcdir}" != "${builddir}" ] || \148err "srcdir and builddir cannot match; reconfigure the package" \149"in a separate directory"150151local sources152if [ ${#} -gt 0 ]; then153sources="${@}"154else155sources="$(find_sources "${srcdir}" "${builddir}" "${tarname}")"156fi157158local ok=0159for file in ${sources}; do160local file="$(echo ${file} | sed -e "s,\\./,,")"161162check_file "${srcdir}" "${file}" || ok=1163done164165return "${ok}"166}167168169main "${@}"170171172