Skip to content

Commit 4e5b6a3

Browse files
committed
Try to produce SARIF from linux clang
This is just horrible.
1 parent 607cd81 commit 4e5b6a3

File tree

4 files changed

+306
-186
lines changed

4 files changed

+306
-186
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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
OUT="$(cat "$LOGNAME")"
24+
# Drop last line, which is: "$N (warning|error)[s] generated."
25+
echo "$OUT" | head -n 2 | tail -n 1 > "$LOGNAME"
26+
exit $RES
27+
else
28+
/bin/false # ???
29+
fi

.github/workflows/CI-linux.yml

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ jobs:
4646
linux:
4747
runs-on: ubuntu-latest
4848
env:
49+
JOB_NAME: linux.${{ inputs.distro }}.${{ inputs.compiler-family }}${{ inputs.compiler-version }}.${{ inputs.flavor }}
4950
SONAR_SCANNER_VERSION: 5.0.1.3006
5051
name: ${{ inputs.distro }}.${{ inputs.compiler-family }}${{ inputs.compiler-version }}.${{ inputs.flavor }}
5152
container:
@@ -137,7 +138,8 @@ jobs:
137138
fi
138139
if [ "$COMPILER_FAMILY" = "LLVM" ]; then
139140
eatmydata apt install clang-${{ inputs.compiler-version }} \
140-
libomp-${{ inputs.compiler-version }}-dev
141+
libomp-${{ inputs.compiler-version }}-dev \
142+
python3
141143
fi
142144
if [ "$ENABLE_SAMPLE_BASED_TESTING" = "true" ]; then
143145
eatmydata apt install zstd
@@ -228,6 +230,7 @@ jobs:
228230
uses: github/codeql-action/init@v3
229231
with:
230232
languages: cpp
233+
source-root: ${{ github.workspace }}/rawspeed
231234
- name: Set up JDK 11 (for SonarCloud static analysis)
232235
timeout-minutes: 1
233236
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 +267,7 @@ jobs:
264267
env:
265268
CC: ${{ inputs.compiler-CC }}
266269
CXX: ${{ inputs.compiler-CXX }}
270+
FAMILY: ${{ inputs.compiler-family }}
267271
CLANG_TIDY: ${{ inputs.compiler-CLANG_TIDY }}
268272
GCOV: ${{ inputs.compiler-GCOV }}
269273
SRC_DIR: ${{ github.workspace }}/rawspeed
@@ -279,14 +283,15 @@ jobs:
279283
cmake -E make_directory "${INSTALL_PREFIX}"
280284
export ECO="${ECO} -DRAWSPEED_REFERENCE_SAMPLE_ARCHIVE=${RPUU_DST}"
281285
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
283-
export ECO="${ECO} -DRAWSPEED_ENABLE_WERROR=OFF"
286+
if [ "$FAMILY" = "LLVM" ]; then
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,23 @@ jobs:
295300
run: |
296301
set -xe
297302
"${SRC_DIR}/.ci/ci-script.sh"
303+
- name: Merge clang SARIF reports
304+
if: inputs.compiler-family == 'LLVM' && !cancelled() && steps.build.conclusion != 'skipped'
305+
timeout-minutes: 1
306+
env:
307+
SRC_DIR: ${{ github.workspace }}/rawspeed
308+
run: |
309+
set -xe
310+
"${SRC_DIR}/.ci/clang-sarif-merge.py" --output="${GITHUB_WORKSPACE}/clang_report.json" "${GITHUB_WORKSPACE}/clang_report/"
311+
cat ""${GITHUB_WORKSPACE}/clang_report.json"
312+
- name: Upload results of clang compile [SARIF]
313+
timeout-minutes: 1
314+
if: inputs.compiler-family == 'LLVM' && !cancelled() && steps.build.conclusion != 'skipped'
315+
uses: github/codeql-action/upload-sarif@v3
316+
with:
317+
sarif_file: "${{ github.workspace }}/clang_report.json"
318+
checkout_path: "${{ github.workspace }}/rawspeed"
319+
category: ${{ env.JOB_NAME }}
298320
- name: Test (unit tests)
299321
timeout-minutes: 1
300322
env:

0 commit comments

Comments
 (0)