Skip to content

Commit f08fe57

Browse files
committed
Try to produce SARIF from linux clang
This is just horrible.
1 parent 2a2cae5 commit f08fe57

File tree

6 files changed

+314
-185
lines changed

6 files changed

+314
-185
lines changed

.ci/clang-sarif-merge.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/env python3
2+
3+
import argparse
4+
import functools
5+
import glob
6+
import json
7+
import os
8+
9+
10+
def create_parser():
11+
parser = argparse.ArgumentParser(description="merge many clang sarif's")
12+
13+
parser.add_argument(
14+
"-o",
15+
"--output",
16+
dest="output",
17+
required=True,
18+
help="the merged SARIF file",
19+
)
20+
21+
parser.add_argument(
22+
"dir",
23+
help="Directory containing individual sarif's",
24+
)
25+
26+
return parser
27+
28+
29+
def merge_two_sarifs(x, y):
30+
assert x.keys() == y.keys()
31+
res = dict()
32+
for key in x.keys():
33+
a = x[key]
34+
b = y[key]
35+
assert type(a) == type(b)
36+
if isinstance(a, str):
37+
assert a == b
38+
res[key] = a
39+
elif isinstance(a, list):
40+
res[key] = a + b
41+
elif isinstance(a, dict):
42+
res[key] = merge_two_sarifs(a, b)
43+
else:
44+
assert False
45+
return res
46+
47+
48+
def main():
49+
# Parse the command line flags
50+
parser = create_parser()
51+
args, unknown_args = parser.parse_known_args()
52+
assert not unknown_args
53+
files = glob.glob(os.path.join(args.dir, "*.json"), recursive=True)
54+
datas = []
55+
for f in files:
56+
with open(f, "r") as f:
57+
datas.append(json.load(f))
58+
result = dict()
59+
if len(datas) > 0:
60+
result = functools.reduce(merge_two_sarifs, datas)
61+
if "runs" in result:
62+
result["runs"] = [functools.reduce(merge_two_sarifs, result["runs"])]
63+
with open(args.output, "w") as f:
64+
json.dump(result, f)
65+
66+
67+
if __name__ == "__main__":
68+
main()

.ci/clang-sarif-wrapper.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/sh
2+
3+
# set -x
4+
set +e
5+
6+
SARIFDIR="$1"
7+
mkdir -p "$SARIFDIR"
8+
9+
shift
10+
11+
INVOCATIONHASH=$(echo "$*" | sha1sum --text - | cut -d' ' -f 1)
12+
13+
LOGNAME="$SARIFDIR/$INVOCATIONHASH.json"
14+
15+
$@ -fdiagnostics-format=sarif -Wno-sarif-format-unstable 2>"$LOGNAME"
16+
RES=$?
17+
18+
LC=$(wc -l "$LOGNAME" | cut -d' ' -f 1)
19+
if [ $LC -eq 3 ]; then
20+
# Got no warnings.
21+
exit $RES
22+
elif [ $LC -eq 4 ]; then
23+
touch "$SARIFDIR/.warnings"
24+
OUT="$(cat "$LOGNAME")"
25+
# Drop last line, which is: "$N (warning|error)[s] generated."
26+
echo "$OUT" | head -n 2 | tail -n 1 > "$LOGNAME"
27+
exit $RES
28+
else
29+
/bin/false # ???
30+
fi

.github/workflows/CI-linux.yml

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ jobs:
137137
fi
138138
if [ "$COMPILER_FAMILY" = "LLVM" ]; then
139139
eatmydata apt install clang-${{ inputs.compiler-version }} \
140-
libomp-${{ inputs.compiler-version }}-dev
140+
libomp-${{ inputs.compiler-version }}-dev \
141+
python3
141142
fi
142143
if [ "$ENABLE_SAMPLE_BASED_TESTING" = "true" ]; then
143144
eatmydata apt install zstd
@@ -228,6 +229,7 @@ jobs:
228229
uses: github/codeql-action/init@v3
229230
with:
230231
languages: cpp
232+
source-root: ${{ github.workspace }}/rawspeed
231233
- name: Set up JDK 11 (for SonarCloud static analysis)
232234
timeout-minutes: 1
233235
if: inputs.flavor == 'SonarCloudStaticAnalysis' && github.repository == 'darktable-org/rawspeed' && github.event_name != 'pull_request' && github.ref_type == 'branch' && (github.ref_name == 'develop' || github.ref_name == 'stable')
@@ -264,6 +266,7 @@ jobs:
264266
env:
265267
CC: ${{ inputs.compiler-CC }}
266268
CXX: ${{ inputs.compiler-CXX }}
269+
FAMILY: ${{ inputs.compiler-family }}
267270
CLANG_TIDY: ${{ inputs.compiler-CLANG_TIDY }}
268271
GCOV: ${{ inputs.compiler-GCOV }}
269272
SRC_DIR: ${{ github.workspace }}/rawspeed
@@ -279,14 +282,16 @@ jobs:
279282
cmake -E make_directory "${INSTALL_PREFIX}"
280283
export ECO="${ECO} -DRAWSPEED_REFERENCE_SAMPLE_ARCHIVE=${RPUU_DST}"
281284
export ECO="${ECO} -DCMAKE_CXX_CLANG_TIDY_EXPORT_FIXES_DIR=${BUILD_DIR}/clang-tidy/"
282-
if [ "$FLAVOR" = "ClangTidy" ] || [ "$FLAVOR" = "ClangStaticAnalysis" ] || [ "$FLAVOR" = "ClangCTUStaticAnalysis" ] || [ "$FLAVOR" = "CodeQLAnalysis" ]; then
285+
if [ "$FAMILY" = "LLVM" ]; then
283286
export ECO="${ECO} -DRAWSPEED_ENABLE_WERROR=OFF"
287+
export ECO="${ECO} -DCMAKE_CXX_COMPILER_LAUNCHER='${SRC_DIR}/.ci/clang-sarif-wrapper.sh;${GITHUB_WORKSPACE}/clang_report/'"
284288
fi
285289
"${SRC_DIR}/.ci/ci-script.sh"
286290
- name: Build
287291
id: build
288292
timeout-minutes: ${{ inputs.flavor != 'ClangTidy' && (inputs.flavor != 'CodeQLAnalysis' && 7 || 12) || 25 }}
289293
env:
294+
FAMILY: ${{ inputs.compiler-family }}
290295
SRC_DIR: ${{ github.workspace }}/rawspeed
291296
BUILD_DIR: ${{ github.workspace }}/rawspeed-build
292297
INSTALL_PREFIX: ${{ github.workspace }}/rawspeed-install
@@ -295,6 +300,28 @@ jobs:
295300
run: |
296301
set -xe
297302
"${SRC_DIR}/.ci/ci-script.sh"
303+
if [ "$FLAVOR" = "ClangTidy" ] || [ "$FLAVOR" = "ClangStaticAnalysis" ] || [ "$FLAVOR" = "ClangCTUStaticAnalysis" ] || [ "$FLAVOR" = "CodeQLAnalysis" ]; then
304+
exit 0
305+
else
306+
MARKER="${GITHUB_WORKSPACE}/clang_report/.warnings"
307+
[ -f "$MARKER" ] && exit 1 || exit 0
308+
fi
309+
- name: Merge clang SARIF reports
310+
if: inputs.compiler-family == 'LLVM' && !cancelled() && steps.build.conclusion != 'skipped'
311+
timeout-minutes: 1
312+
env:
313+
SRC_DIR: ${{ github.workspace }}/rawspeed
314+
run: |
315+
set -xe
316+
"${SRC_DIR}/.ci/clang-sarif-merge.py" --output="${GITHUB_WORKSPACE}/clang_report.json" "${GITHUB_WORKSPACE}/clang_report/"
317+
- name: Upload results of clang compile [SARIF]
318+
timeout-minutes: 1
319+
if: inputs.compiler-family == 'LLVM' && !cancelled() && steps.build.conclusion != 'skipped'
320+
uses: github/codeql-action/upload-sarif@v3
321+
with:
322+
sarif_file: "${{ github.workspace }}/clang_report.json"
323+
checkout_path: "${{ github.workspace }}/rawspeed"
324+
category: ${{ job.name }}
298325
- name: Test (unit tests)
299326
timeout-minutes: 1
300327
env:

0 commit comments

Comments
 (0)