summaryrefslogtreecommitdiff
path: root/other/astyle/format-source
blob: 7de28d06f02d65abe05470758fad0349eef3cd9e (plain)
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
#!/bin/bash

set -ex

SOURCE_DIR="$1"
ASTYLE="$2"
APIDSL="$3"

# Go to the source root.
if [ -z "$SOURCE_DIR" ]; then
  SOURCE_DIR=.
fi
cd "$SOURCE_DIR"

if [ -z "$ASTYLE" ] || ! which "$ASTYLE"; then
  ASTYLE=astyle
fi

if ! which "$ASTYLE"; then
  # If we couldn't find or install an astyle binary, don't do anything.
  echo "Could not find an astyle binary; please install astyle."
  exit 1
fi

if ! which "$APIDSL"; then
  if [ -f ../apidsl/apigen.native ]; then
    APIDSL=../apidsl/apigen.native
  else
    APIDSL=apidsl_curl
  fi
fi

TO_JSON='s/\\/\\\\/g;s/\n/\\n/g;s/"/\\"/g;s/^(.*)$/"$1"/'
FROM_JSON='s/\\"/"/g;s/^"(.*)"$/$1/;s/\\\\/\\/g;s/\\n/\n/g'

apidsl_request() {
  TMPFILE=$(mktemp /tmp/apidsl.XXXXXX)
  curl -s -o "$TMPFILE" -X POST --data @<(
    echo '["Request",'
    cat "$2"
    echo ']'
  ) "https://apidsl.herokuapp.com/$1"
  if grep '\[1,"' "$TMPFILE" >/dev/null; then
    echo "Error: $(grep -o '".*"' /tmp/apidsl-$$ | perl -0777 -pe "$FROM_JSON")" >&2
    rm "$TMPFILE"
    exit 1
  fi
  perl -0777 -pe 's/^\[0,(.*)\]$/$1/' "$TMPFILE"
  rm "$TMPFILE"
}

apidsl_curl() {
  echo "apidsl_curl $*" >&2
  apidsl_request "c" <(
    apidsl_request "parse" <(
      perl -0777 -pe "$TO_JSON" "$1"
    )
  ) | perl -0777 -pe "$FROM_JSON"
}

# Check if apidsl generated sources are up to date.
set +x
"$APIDSL" toxcore/LAN_discovery.api.h >toxcore/LAN_discovery.h &
"$APIDSL" toxcore/crypto_core.api.h >toxcore/crypto_core.h &
"$APIDSL" toxcore/ping.api.h >toxcore/ping.h &
"$APIDSL" toxcore/ping_array.api.h >toxcore/ping_array.h &
"$APIDSL" toxcore/tox.api.h >toxcore/tox.h &
"$APIDSL" toxav/toxav.api.h >toxav/toxav.h &
"$APIDSL" toxencryptsave/toxencryptsave.api.h >toxencryptsave/toxencryptsave.h &
set -x

wait
wait
wait
wait
wait
wait
wait

if grep '<unresolved>' ./*/*.h; then
  echo "error: some apidsl references were unresolved"
  exit 1
fi

readarray -t CC_SOURCES <<<"$(find . '(' -name '*.cc' ')')"
CC_SOURCES+=(toxcore/crypto_core.c)
CC_SOURCES+=(toxcore/ping_array.c)

for bin in clang-format-6.0 clang-format-5.0 clang-format; do
  if which "$bin"; then
    "$bin" -i -style='{BasedOnStyle: Google, ColumnLimit: 100}' "${CC_SOURCES[@]}"
    break
  fi
done

FIND="find ."
FIND="$FIND '(' -name '*.[ch]' ')'"
FIND="$FIND -and -not -name '*.api.h'"
FIND="$FIND -and -not -wholename './super_donators/*'"
FIND="$FIND -and -not -wholename './third_party/*'"
FIND="$FIND -and -not -wholename './toxencryptsave/crypto_pwhash*'"

readarray -t C_SOURCES <<<"$(eval "$FIND")"

"$ASTYLE" -n --options=other/astyle/astylerc "${C_SOURCES[@]}"

git diff --color=always --exit-code