]>
Commit | Line | Data |
---|---|---|
f1e6f933 | 1 | #!/usr/bin/env bash |
186b9041 | 2 | # SPDX-License-Identifier: LGPL-2.1-or-later |
f1e6f933 | 3 | set -e |
2d0bc684 | 4 | |
c988ef4c FS |
5 | # FIXME: |
6 | # - Coccinelle doesn't like our TEST() macros, which then causes name conflicts; i.e. Cocci can't process | |
7 | # that TEST(xsetxattr) yields test_xsetxattr() and uses just xsetxattr() in this case, which then conflicts | |
8 | # with the tested xsetxattr() function, leading up to the whole test case getting skipped due to | |
9 | # conflicting typedefs | |
c988ef4c FS |
10 | # - Coccinelle has issues with some of our more complex macros |
11 | ||
4a4eaade FS |
12 | # Exclude following paths from the Coccinelle transformations |
13 | EXCLUDED_PATHS=( | |
14 | "src/boot/efi/*" | |
15 | "src/shared/linux/*" | |
16 | "src/basic/linux/*" | |
17 | # Symlinked to test-bus-vtable-cc.cc, which causes issues with the IN_SET macro | |
18 | "src/libsystemd/sd-bus/test-bus-vtable.c" | |
ca21d59a | 19 | "src/libsystemd/sd-journal/lookup3.c" |
c988ef4c FS |
20 | # Ignore man examples, as they redefine some macros we use internally, which makes Coccinelle complain |
21 | # and ignore code that tries to use the redefined stuff | |
22 | "man/*" | |
4a4eaade FS |
23 | ) |
24 | ||
8370da9e | 25 | TOP_DIR="$(git rev-parse --show-toplevel)" |
c988ef4c | 26 | CACHE_DIR="$(dirname "$0")/.coccinelle-cache" |
8370da9e | 27 | ARGS=() |
00bfe67f | 28 | |
4a4eaade | 29 | # Create an array from files tracked by git... |
c988ef4c | 30 | mapfile -t FILES < <(git ls-files ':/*.c') |
4a4eaade FS |
31 | # ...and filter everything that matches patterns from EXCLUDED_PATHS |
32 | for excl in "${EXCLUDED_PATHS[@]}"; do | |
8370da9e FS |
33 | # shellcheck disable=SC2206 |
34 | FILES=(${FILES[@]//$excl}) | |
4a4eaade FS |
35 | done |
36 | ||
00bfe67f | 37 | case "$1" in |
cc5549ca | 38 | -i) |
8370da9e | 39 | ARGS+=(--in-place) |
cc5549ca ZJS |
40 | shift |
41 | ;; | |
00bfe67f | 42 | esac |
31d31f20 | 43 | |
3b253ad6 | 44 | if ! parallel -h >/dev/null; then |
cc5549ca ZJS |
45 | echo 'Please install GNU parallel (package "parallel")' |
46 | exit 1 | |
3b253ad6 ZJS |
47 | fi |
48 | ||
8370da9e FS |
49 | [[ ${#@} -ne 0 ]] && SCRIPTS=("$@") || SCRIPTS=("$TOP_DIR"/coccinelle/*.cocci) |
50 | ||
c988ef4c FS |
51 | mkdir -p "$CACHE_DIR" |
52 | echo "--x-- Using Coccinelle cache directory: $CACHE_DIR" | |
53 | echo "--x--" | |
54 | echo "--x-- Note: running spatch for the first time without populated cache takes" | |
55 | echo "--x-- a _long_ time (15-30 minutes). Also, the cache is quite large" | |
56 | echo "--x-- (~15 GiB), so make sure you have enough free space." | |
57 | echo | |
58 | ||
8370da9e FS |
59 | for script in "${SCRIPTS[@]}"; do |
60 | echo "--x-- Processing $script --x--" | |
61 | TMPFILE="$(mktemp)" | |
62 | echo "+ spatch --sp-file $script ${ARGS[*]} ..." | |
c988ef4c FS |
63 | # A couple of notes: |
64 | # | |
65 | # 1) Limit this to 10 files at once, as processing the ASTs is _very_ memory hungry - e.g. with 20 files | |
66 | # at once one spatch process can take around 2.5 GiB of RAM, which can easily eat up all available RAM | |
67 | # when paired together with parallel | |
68 | # | |
69 | # 2) Make sure spatch can find our includes via -I <dir>, similarly as we do when compiling stuff | |
70 | # | |
71 | # 3) Make sure to include includes from includes (--recursive-includes), but use them only to get type | |
72 | # definitions (--include-headers-for-types) - otherwise we'd start formating them as well, which might be | |
73 | # unwanted, especially for includes we fetch verbatim from third-parties | |
74 | # | |
4d3510d0 FS |
75 | # 4) Explicitly undefine the SD_BOOT symbol, so Coccinelle ignores includes guarded by #if SD_BOOT |
76 | # | |
77 | # 5) Use cache, since generating the full AST is _very_ expensive, i.e. the uncached run takes 15 - 30 | |
c988ef4c FS |
78 | # minutes (for one rule(!)), vs 30 - 90 seconds when the cache is populated. One major downside of the |
79 | # cache is that it's quite big - ATTOW the cache takes around 15 GiB, but the performance boost is | |
80 | # definitely worth it | |
81 | parallel --halt now,fail=1 --keep-order --noswap --max-args=10 \ | |
82 | spatch --cache-prefix "$CACHE_DIR" \ | |
83 | -I src \ | |
84 | --recursive-includes \ | |
85 | --include-headers-for-types \ | |
4d3510d0 | 86 | --undefined SD_BOOT \ |
c988ef4c FS |
87 | --smpl-spacing \ |
88 | --sp-file "$script" \ | |
89 | "${ARGS[@]}" ::: "${FILES[@]}" \ | |
90 | 2>"$TMPFILE" || cat "$TMPFILE" | |
91 | rm -f "$TMPFILE" | |
8370da9e | 92 | echo -e "--x-- Processed $script --x--\n" |
2d0bc684 | 93 | done |