Skip to content

Commit e4156cb

Browse files
authored
Fix SonarCloud config [BUILD-688] (#82)
1 parent 7758136 commit e4156cb

File tree

2 files changed

+58
-24
lines changed

2 files changed

+58
-24
lines changed

cc_files/get_cc_files.bzl

+9-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ load("//cc:defs.bzl", "BINARY", "LIBRARY", "TEST_LIBRARY", "TEST_SRCS")
33
FilesInfo = provider(
44
fields = {
55
"files": "files of the target",
6+
"test_files": "test files of the target",
67
},
78
)
89

@@ -34,13 +35,17 @@ def get_cc_files(ctx):
3435

3536
def _get_cc_target_files_impl(target, ctx):
3637
if not CcInfo in target:
37-
return [FilesInfo(files = [])]
38+
return [FilesInfo(files = [], test_files = [])]
3839

3940
tags = getattr(ctx.rule.attr, "tags", [])
40-
if not LIBRARY in tags and not TEST_LIBRARY in tags and not BINARY in tags:
41-
return [FilesInfo(files = [])]
4241

43-
return [FilesInfo(files = get_cc_files(ctx))]
42+
if LIBRARY in tags or BINARY in tags:
43+
return [FilesInfo(files = get_cc_files(ctx), test_files = [])]
44+
45+
if TEST_LIBRARY in tags:
46+
return [FilesInfo(files = [], test_files = get_cc_files(ctx))]
47+
48+
return [FilesInfo(files = [], test_files = [])]
4449

4550
get_cc_target_files = aspect(
4651
implementation = _get_cc_target_files_impl,

tools/gen_sonar_cfg.bzl

+49-20
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,65 @@ load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
22
load("//cc_files:get_cc_files.bzl", "FilesInfo", "get_cc_target_files")
33

44
def _gen_sonar_cfg_impl(ctx):
5-
all_files_bash = ""
6-
all_files = []
5+
files_bash = ""
6+
files = []
77

88
for target in ctx.attr.targets:
9-
for file in target[FilesInfo].files + ctx.files.test_srcs:
10-
if file.path not in all_files:
11-
all_files_bash += file.path
12-
all_files_bash += " "
13-
all_files.append(file.path)
9+
for file in target[FilesInfo].files:
10+
if file.path not in files:
11+
files_bash += file.path
12+
files_bash += " "
13+
files.append(file.path)
14+
15+
test_files_bash = ""
16+
test_files = []
17+
18+
for target in ctx.attr.targets:
19+
for file in target[FilesInfo].test_files:
20+
if file.path not in test_files:
21+
test_files_bash += file.path
22+
test_files_bash += " "
23+
test_files.append(file.path)
24+
25+
for file in ctx.files.test_srcs:
26+
if file.path not in test_files:
27+
test_files_bash += file.path
28+
test_files_bash += " "
29+
test_files.append(file.path)
1430

1531
out = ctx.actions.declare_file("sonar-project.properties")
1632
ctx.actions.run_shell(
1733
outputs = [out],
1834
command = """
35+
add_files () {{
36+
files=$1
37+
first=1
38+
for file in $files; do
39+
if [[ $first -eq 1 ]]
40+
then
41+
first=0
42+
else
43+
echo ',\\' >> {out_file}
44+
fi
45+
echo -n " {root_dir}/$file" >> {out_file}
46+
done
47+
echo '' >> {out_file}
48+
}}
49+
1950
echo 'sonar.sourceEncoding=UTF-8' >> {out_file}
20-
echo 'sonar.sources=\\' >> {out_file}
21-
first=1
22-
for file in {all_files}; do
23-
if [[ $first -eq 1 ]]
24-
then
25-
first=0
26-
else
27-
echo ',\\' >> {out_file}
28-
fi
29-
echo -n " {root_dir}/$file" >> {out_file}
30-
done
31-
echo '' >> {out_file}
51+
52+
echo 'sonar.inclusions=\\' >> {out_file}
53+
add_files "{inclusions}"
54+
55+
echo 'sonar.coverage.exclusions=\\' >> {out_file}
56+
add_files "{exclusions}"
57+
58+
echo 'sonar.cpd.exclusions=\\' >> {out_file}
59+
add_files "{exclusions}"
3260
""".format(
3361
out_file = out.path,
34-
all_files = all_files_bash,
62+
inclusions = files_bash + " " + test_files_bash,
63+
exclusions = test_files_bash,
3564
root_dir = ctx.attr.root_dir[BuildSettingInfo].value,
3665
),
3766
)

0 commit comments

Comments
 (0)