3
# Copyright (C) 2013 Canonical Ltd
5
# This program is free software: you can redistribute it and/or modify
6
# it under the terms of the GNU Lesser General Public License version 3 as
7
# published by the Free Software Foundation.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU Lesser General Public License for more details.
14
# You should have received a copy of the GNU Lesser General Public License
15
# along with this program. If not, see <http://www.gnu.org/licenses/>.
17
# Authored by: Michi Henning <michi.henning@canonical.com>
19
# Simple script to format files with astyle, followed by clang-format (which
20
# undoes some damage that's done by astyle, without wiping out astyle edits we want
23
# usage: formatcode [FILE]...
25
# If no arguments are provided, we format stdin and write to stdout.
27
astyle="@ASTYLE_COMMAND@"
28
format="@CLANG_FORMAT_COMMAND@"
29
dos2unix="@DOS2UNIX_COMMAND@"
30
style="@CMAKE_SOURCE_DIR@/_clang-format"
32
# Check that the format commands were found.
33
[ "$astyle" = "ASTYLE_COMMAND-NOTFOUND" -o \
34
"$dos2unix" = "DOS2UNIX_COMMAND-NOTFOUND" -o \
35
"$format" = "CLANG_FORMAT_COMMAND-NOTFOUND" ] && {
36
echo "formatcode: cmake did not find all formatting tools" >&2
40
# If no arguments were provided, read stdin and write stdout.
41
# Recent versions of astyle can't read stdin: http://sourceforge.net/p/astyle/bugs/63/
42
# astyle 2.03 writes DOS line endings: https://sourceforge.net/p/astyle/bugs/268/
45
tmp=`mktemp -p $tmpdir`
49
"$astyle" -q --options=@CMAKE_SOURCE_DIR@/astyle-config -n $tmp
51
"$format" -i -style=file $tmp
57
# Format files in place.
58
"$astyle" --options=@CMAKE_SOURCE_DIR@/astyle-config -n "$@"
59
[ $? -ne 0 ] && exit $?
63
"$format" -i -style=file $files "$@"