Skip to content

Commit de82fc9

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

File tree

6 files changed

+317
-185
lines changed

6 files changed

+317
-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: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ jobs:
5353
options: --tmpfs /tmp:exec --tmpfs /__w/${{ github.event.repository.name }}/${{ github.event.repository.name }}:exec
5454
steps:
5555
- name: Configure APT
56+
env:
57+
JOB_NAME: ${{ github.jobs[github.job].name }}
5658
timeout-minutes: 1
5759
run: |
5860
set -xe
@@ -137,7 +139,8 @@ jobs:
137139
fi
138140
if [ "$COMPILER_FAMILY" = "LLVM" ]; then
139141
eatmydata apt install clang-${{ inputs.compiler-version }} \
140-
libomp-${{ inputs.compiler-version }}-dev
142+
libomp-${{ inputs.compiler-version }}-dev \
143+
python3
141144
fi
142145
if [ "$ENABLE_SAMPLE_BASED_TESTING" = "true" ]; then
143146
eatmydata apt install zstd
@@ -228,6 +231,7 @@ jobs:
228231
uses: github/codeql-action/init@v3
229232
with:
230233
languages: cpp
234+
source-root: ${{ github.workspace }}/rawspeed
231235
- name: Set up JDK 11 (for SonarCloud static analysis)
232236
timeout-minutes: 1
233237
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 +268,7 @@ jobs:
264268
env:
265269
CC: ${{ inputs.compiler-CC }}
266270
CXX: ${{ inputs.compiler-CXX }}
271+
FAMILY: ${{ inputs.compiler-family }}
267272
CLANG_TIDY: ${{ inputs.compiler-CLANG_TIDY }}
268273
GCOV: ${{ inputs.compiler-GCOV }}
269274
SRC_DIR: ${{ github.workspace }}/rawspeed
@@ -279,14 +284,16 @@ jobs:
279284
cmake -E make_directory "${INSTALL_PREFIX}"
280285
export ECO="${ECO} -DRAWSPEED_REFERENCE_SAMPLE_ARCHIVE=${RPUU_DST}"
281286
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
287+
if [ "$FAMILY" = "LLVM" ]; then
283288
export ECO="${ECO} -DRAWSPEED_ENABLE_WERROR=OFF"
289+
export ECO="${ECO} -DCMAKE_CXX_COMPILER_LAUNCHER='${SRC_DIR}/.ci/clang-sarif-wrapper.sh;${GITHUB_WORKSPACE}/clang_report/'"
284290
fi
285291
"${SRC_DIR}/.ci/ci-script.sh"
286292
- name: Build
287293
id: build
288294
timeout-minutes: ${{ inputs.flavor != 'ClangTidy' && (inputs.flavor != 'CodeQLAnalysis' && 7 || 12) || 25 }}
289295
env:
296+
FAMILY: ${{ inputs.compiler-family }}
290297
SRC_DIR: ${{ github.workspace }}/rawspeed
291298
BUILD_DIR: ${{ github.workspace }}/rawspeed-build
292299
INSTALL_PREFIX: ${{ github.workspace }}/rawspeed-install
@@ -295,6 +302,29 @@ jobs:
295302
run: |
296303
set -xe
297304
"${SRC_DIR}/.ci/ci-script.sh"
305+
if [ "$FLAVOR" = "ClangTidy" ] || [ "$FLAVOR" = "ClangStaticAnalysis" ] || [ "$FLAVOR" = "ClangCTUStaticAnalysis" ] || [ "$FLAVOR" = "CodeQLAnalysis" ]; then
306+
exit 0
307+
else
308+
MARKER="${GITHUB_WORKSPACE}/clang_report/.warnings"
309+
[ -f "$MARKER" ] && exit 1 || exit 0
310+
fi
311+
- name: Merge clang SARIF reports
312+
if: inputs.compiler-family == 'LLVM' && !cancelled() && steps.build.conclusion != 'skipped'
313+
timeout-minutes: 1
314+
env:
315+
SRC_DIR: ${{ github.workspace }}/rawspeed
316+
JOB_NAME: ${{ job.name }}
317+
run: |
318+
set -xe
319+
"${SRC_DIR}/.ci/clang-sarif-merge.py" --output="${GITHUB_WORKSPACE}/clang_report.json" "${GITHUB_WORKSPACE}/clang_report/"
320+
- name: Upload results of clang compile [SARIF]
321+
timeout-minutes: 1
322+
if: inputs.compiler-family == 'LLVM' && !cancelled() && steps.build.conclusion != 'skipped'
323+
uses: github/codeql-action/upload-sarif@v3
324+
with:
325+
sarif_file: "${{ github.workspace }}/clang_report.json"
326+
checkout_path: "${{ github.workspace }}/rawspeed"
327+
category: ${{ job.name }}
298328
- name: Test (unit tests)
299329
timeout-minutes: 1
300330
env:

0 commit comments

Comments
 (0)