aboutsummaryrefslogtreecommitdiffstats
path: root/admin/check-style.sh
blob: 696f9247a74a105c284664d79f789f36df645ad5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#! /bin/sh
# Copyright 2011 The Kyua Authors.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
#   notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in the
#   documentation and/or other materials provided with the distribution.
# * Neither the name of Google Inc. nor the names of its contributors
#   may be used to endorse or promote products derived from this software
#   without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# \file admin/check-style.sh
#
# Sanity checks the coding style of all source files in the project tree.

ProgName="${0##*/}"


# Prints an error message and exits.
#
# \param ... Parts of the error message; concatenated using a space as the
#     separator.
err() {
    echo "${ProgName}:" "${@}" 1>&2
    exit 1
}


# Locates all source files within the project directory.
#
# We require the project to have been configured in a directory that is separate
# from the source tree.   This is to allow us to easily filter out build
# artifacts from our search.
#
# \param srcdir Absolute path to the source directory.
# \param builddir Absolute path to the build directory.
# \param tarname Basename of the project's tar file, to skip possible distfile
#     directories.
find_sources() {
    local srcdir="${1}"; shift
    local builddir="${1}"; shift
    local tarname="${1}"; shift

    (
        cd "${srcdir}"
        find . -type f -a \
                  \! -path "*/.git/*" \
                  \! -path "*/.deps/*" \
                  \! -path "*/autom4te.cache/*" \
                  \! -path "*/${tarname}-[0-9]*/*" \
                  \! -path "*/${builddir##*/}/*" \
                  \! -name "Makefile.in" \
                  \! -name "aclocal.m4" \
                  \! -name "config.h.in" \
                  \! -name "configure" \
                  \! -name "testsuite"
    )
}


# Prints the style rules applicable to a given file.
#
# \param file Path to the source file.
guess_rules() {
    local file="${1}"; shift

    case "${file}" in
        */ax_cxx_compile_stdcxx.m4) ;;
        */ltmain.sh) ;;
        *Makefile*) echo common make ;;
        *.[0-9]) echo common man ;;
        *.cpp|*.hpp) echo common cpp ;;
        *.sh) echo common shell ;;
        *) echo common ;;
    esac
}


# Validates a given file against the rules that apply to it.
#
# \param srcdir Absolute path to the source directory.
# \param file Name of the file to validate relative to srcdir.
#
# \return 0 if the file is valid; 1 otherwise, in which case the style
# violations are printed to the output.
check_file() {
    local srcdir="${1}"; shift
    local file="${1}"; shift

    local err=0
    for rule in $(guess_rules "${file}"); do
        awk -f "${srcdir}/admin/check-style-${rule}.awk" \
            "${srcdir}/${file}" || err=1
    done

    return ${err}
}


# Entry point.
main() {
    local builddir=.
    local srcdir=.
    local tarname=UNKNOWN

    local arg
    while getopts :b:s:t: arg; do
        case "${arg}" in
            b)
                builddir="${OPTARG}"
                ;;

            s)
                srcdir="${OPTARG}"
                ;;

            t)
                tarname="${OPTARG}"
                ;;

            \?)
                err "Unknown option -${OPTARG}"
                ;;
        esac
    done
    shift $(expr ${OPTIND} - 1)

    srcdir="$(cd "${srcdir}" && pwd -P)"
    builddir="$(cd "${builddir}" && pwd -P)"
    [ "${srcdir}" != "${builddir}" ] || \
        err "srcdir and builddir cannot match; reconfigure the package" \
            "in a separate directory"

    local sources
    if [ ${#} -gt 0 ]; then
        sources="${@}"
    else
        sources="$(find_sources "${srcdir}" "${builddir}" "${tarname}")"
    fi

    local ok=0
    for file in ${sources}; do
        local file="$(echo ${file} | sed -e "s,\\./,,")"

        check_file "${srcdir}" "${file}" || ok=1
    done

    return "${ok}"
}


main "${@}"