Skip to content

Commit 11e5a76

Browse files
committed
Configure Scala and C++ code formatting
Based on the scripts and configuration used by Scala Native.
1 parent a2b6cb5 commit 11e5a76

File tree

8 files changed

+133
-2
lines changed

8 files changed

+133
-2
lines changed

.clang-format

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
# Documentation: https://clang.llvm.org/docs/ClangFormatStyleOptions.html
3+
BasedOnStyle: LLVM
4+
IndentWidth: 4
5+
---
6+
Language: Cpp
7+
...

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
target/
2+
scripts/.coursier
3+
scripts/.scalafmt-*

.scalafmt.conf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
style = defaultWithAlign
2+
docstrings = JavaDoc
3+
assumeStandardLibraryStripMargin = true
4+
project.excludeFilters = [
5+
tests/samples
6+
]
7+
project.git = true
8+
runner.dialect = Scala211

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ compiler: gcc
55
script:
66
- docker-compose build $TEST_ENV
77
- docker-compose run $TEST_ENV
8+
- if [[ "$TEST_ENV" = *llvm-5.0 ]]; then
9+
docker-compose run --rm $TEST_ENV script/scalafmt --test;
10+
docker-compose run --rm $TEST_ENV script/clangfmt --test;
11+
fi
812

913
matrix:
1014
include:

CONTRIBUTING.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,22 @@ sbt ~test
3232

3333
## Coding Guidelines
3434

35+
Code should be formatted with `./scripts/scalafmt` and `./scripts/clangfmt`.
36+
Make sure that all of your contributions are properly formatted before
37+
suggesting any changes. You can check the formatting using either:
38+
39+
```sh
40+
script/scalafmt --test
41+
script/clangfmt --test
42+
```
43+
44+
or:
45+
46+
```sh
47+
docker-compose run --rm ubuntu-18.04-llvm-6.0 script/scalafmt --test
48+
docker-compose run --rm ubuntu-18.04-llvm-6.0 script/clangfmt --test
49+
```
50+
3551
The C++ tool is built on Clang and Libtooling and should respect the conventions of
3652
LLVM and Clang tools. The code itself should adhere to the [LLVM Coding Standards],
3753
specifically:
@@ -40,4 +56,4 @@ specifically:
4056
- [Use `\n` instead of `std::endl`](https://llvm.org/docs/CodingStandards.html#avoid-std-endl)
4157
and remember to flush when reporting errors.
4258

43-
[LLVM Coding Standards]: https://llvm.org/docs/CodingStandards.html
59+
[LLVM Coding Standards]: https://llvm.org/docs/CodingStandards.html

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ RUN set -x \
77
&& echo "deb https://dl.bintray.com/sbt/debian /" > /etc/apt/sources.list.d/sbt.list \
88
&& apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2EE0EA64E40A89B84B2DF73499E82A75642AC823 \
99
&& apt update \
10-
&& apt install -y curl build-essential openjdk-8-jdk-headless sbt cmake make \
10+
&& apt install -y curl build-essential openjdk-8-jdk-headless sbt cmake make clang-format-5.0 \
1111
&& rm -rf /var/lib/apt/lists/*
1212

1313
ARG LLVM_VERSION=6.0

scripts/clangfmt

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/bin/bash
2+
#
3+
# Format C/C++ code using clang-format.
4+
#
5+
# To ensure reproducible formatting the script checks that clang-format
6+
# is from the most recent version of LLVM supported by Scala Native.
7+
#
8+
# Usage: $0 [--test]
9+
#
10+
# Set CLANG_FORMAT_PATH to configure path to clang-format.
11+
12+
set -euo pipefail
13+
IFS=$'\n\t'
14+
15+
# The required version of clang-format.
16+
CLANG_FORMAT_VERSION=5.0
17+
CLANG_FORMAT_VERSION_STRING="clang-format version $CLANG_FORMAT_VERSION"
18+
19+
die() {
20+
while [ "$#" -gt 0 ]; do
21+
echo >&2 "$1"; shift
22+
done
23+
exit 1
24+
}
25+
26+
check_clang_format_version() {
27+
cmd="$1"
28+
[ -e "$(type -P "$cmd")" ] && \
29+
"$cmd" --version 2> /dev/null | grep -q "$CLANG_FORMAT_VERSION_STRING"
30+
}
31+
32+
clang_format=
33+
34+
if [ -n "${CLANG_FORMAT_PATH:-}" ]; then
35+
check_clang_format_version "$CLANG_FORMAT_PATH" || \
36+
die "CLANG_FORMAT_PATH does not have required version $CLANG_FORMAT_VERSION" \
37+
"CLANG_FORMAT_PATH points to $CLANG_FORMAT_PATH"
38+
clang_format="$CLANG_FORMAT_PATH"
39+
else
40+
for cmd in "clang-format-$CLANG_FORMAT_VERSION" clang-format; do
41+
if check_clang_format_version "$cmd"; then
42+
clang_format="$cmd"
43+
break
44+
fi
45+
done
46+
fi
47+
48+
if [ -z "$clang_format" ]; then
49+
die "clang-format version $CLANG_FORMAT_VERSION not found" \
50+
"Install LLVM version $CLANG_FORMAT_VERSION and rerun."
51+
fi
52+
53+
test_mode=
54+
55+
while [ "$#" -gt 0 ]; do
56+
arg="$1"
57+
case "$arg" in
58+
--test) test_mode=true; shift ;;
59+
--*) die "Unknown argument: $arg" "Usage: $0 [--test]" ;;
60+
*) break ;;
61+
esac
62+
done
63+
64+
if [ "$#" -gt 0 ]; then
65+
"$clang_format" --style=file -i "$@"
66+
else
67+
find . -name "*.[ch]" -or -name "*.cpp" | xargs "$clang_format" --style=file -i
68+
fi
69+
70+
if [ "$test_mode" = true ]; then
71+
git diff --quiet --exit-code || \
72+
die "C/C++ code formatting changes detected" \
73+
"Run \`$0\` to reformat."
74+
fi

scripts/scalafmt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env bash
2+
3+
# set -x
4+
5+
HERE="`dirname $0`"
6+
VERSION="1.2.0"
7+
COURSIER="$HERE/.coursier"
8+
SCALAFMT="$HERE/.scalafmt-$VERSION"
9+
10+
if [ ! -f $COURSIER ]; then
11+
curl -L -o $COURSIER https://git.io/vgvpD
12+
chmod +x $COURSIER
13+
fi
14+
15+
if [ ! -f $SCALAFMT ]; then
16+
$COURSIER bootstrap com.geirsson:scalafmt-cli_2.11:$VERSION --main org.scalafmt.cli.Cli -o $SCALAFMT
17+
chmod +x $SCALAFMT
18+
fi
19+
20+
$SCALAFMT "$@"

0 commit comments

Comments
 (0)