Skip to content

Commit 71372bc

Browse files
committed
C#: Add Bazel-based build system.
This commits a bazel-based build system for C# using `rules_dotnet`. External dependencies are managed via `paket`, and updates to the generated bazel files are done via `./update-deps.sh`. We're providing our own (minimal) test runner for `xunit` tests.
1 parent 9d21e2c commit 71372bc

File tree

36 files changed

+708
-5
lines changed

36 files changed

+708
-5
lines changed

.bazelrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,6 @@ common --enable_runfiles
2121
common --registry=file:///%workspace%/misc/bazel/registry
2222
common --registry=https://bcr.bazel.build
2323

24+
common --@rules_dotnet//dotnet/settings:strict_deps=false
25+
2426
try-import %workspace%/local.bazelrc

.bazelrc.internal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22

33
common --registry=file:///%workspace%/ql/misc/bazel/registry
44
common --registry=https://bcr.bazel.build
5+
6+
common --@rules_dotnet//dotnet/settings:strict_deps=false

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,5 @@ ruby/extractor/cargo-bazel-lock.json -merge
7878
csharp/paket.lock linguist-generated=true
7979
# needs eol=crlf, as `paket` touches this file and saves it als crlf
8080
csharp/.paket/Paket.Restore.targets linguist-generated=true eol=crlf
81+
csharp/paket.main.bzl linguist-generated=true
82+
csharp/paket.main_extension.bzl linguist-generated=true

BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports_files(["LICENSE"])

MODULE.bazel

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,19 @@ bazel_dep(name = "abseil-cpp", version = "20240116.0", repo_name = "absl")
2323
bazel_dep(name = "nlohmann_json", version = "3.11.3", repo_name = "json")
2424
bazel_dep(name = "fmt", version = "10.0.0")
2525
bazel_dep(name = "gazelle", version = "0.36.0")
26+
bazel_dep(name = "rules_dotnet", version = "0.15.1")
2627

2728
bazel_dep(name = "buildifier_prebuilt", version = "6.4.0", dev_dependency = True)
2829

30+
dotnet = use_extension("@rules_dotnet//dotnet:extensions.bzl", "dotnet")
31+
dotnet.toolchain(dotnet_version = "8.0.101")
32+
use_repo(dotnet, "dotnet_toolchains")
33+
34+
register_toolchains("@dotnet_toolchains//:all")
35+
36+
csharp_main_extension = use_extension("//csharp:paket.main_extension.bzl", "main_extension")
37+
use_repo(csharp_main_extension, "paket.main")
38+
2939
pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")
3040
pip.parse(
3141
hub_name = "codegen_deps",

csharp/BUILD.bazel

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
load("@rules_pkg//pkg:mappings.bzl", "pkg_filegroup", "pkg_files")
2+
load("@semmle_code//:common.bzl", "zipmerge")
3+
load("@semmle_code//:dist.bzl", "dist")
4+
15
package(default_visibility = ["//visibility:public"])
26

37
alias(
@@ -9,3 +13,73 @@ alias(
913
name = "dbscheme-stats",
1014
actual = "//csharp/ql/lib:dbscheme-stats",
1115
)
16+
17+
pkg_files(
18+
name = "dbscheme-group",
19+
srcs = [
20+
":dbscheme",
21+
":dbscheme-stats",
22+
],
23+
strip_prefix = None,
24+
)
25+
26+
pkg_files(
27+
name = "extractor-asp",
28+
srcs = [
29+
"@semmle_code//extractor-asp:extractor-asp-fat",
30+
],
31+
prefix = "tools",
32+
renames = {
33+
"@semmle_code//extractor-asp:extractor-asp-fat": "extractor-asp.jar",
34+
},
35+
)
36+
37+
pkg_filegroup(
38+
name = "db-files",
39+
srcs = [
40+
":dbscheme-group",
41+
"//csharp/downgrades",
42+
],
43+
)
44+
45+
pkg_files(
46+
name = "extra-files",
47+
srcs = [
48+
":codeql-extractor.yml",
49+
"//:LICENSE",
50+
],
51+
)
52+
53+
# See `csharp.bzl` for an explanation of why we need zipmerge here
54+
zipmerge(
55+
name = "extractor-arch",
56+
srcs = [
57+
"//csharp/autobuilder/Semmle.Autobuild.CSharp:Semmle.Autobuild.CSharp.zip",
58+
"//csharp/extractor/Semmle.Extraction.CSharp.Driver:Semmle.Extraction.CSharp.Driver.zip",
59+
"//csharp/extractor/Semmle.Extraction.CSharp.Standalone:Semmle.Extraction.CSharp.Standalone.zip",
60+
],
61+
out = "extractor-arch.zip",
62+
)
63+
64+
dist(
65+
name = "extractor-generic",
66+
srcs = [
67+
":dbscheme-group",
68+
":extra-files",
69+
":extractor-asp",
70+
"//csharp/downgrades",
71+
"//csharp/tools",
72+
],
73+
prefix = "csharp",
74+
visibility = ["//visibility:public"],
75+
)
76+
77+
test_suite(
78+
name = "unit-tests",
79+
tags = ["csharp"],
80+
tests = [
81+
"//csharp/autobuilder/Semmle.Autobuild.CSharp.Tests",
82+
"//csharp/autobuilder/Semmle.Autobuild.Cpp.Tests",
83+
"//csharp/extractor/Semmle.Extraction.Tests",
84+
],
85+
)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
load(
2+
"//misc/bazel:csharp.bzl",
3+
"codeql_xunit_test",
4+
)
5+
6+
codeql_xunit_test(
7+
name = "Semmle.Autobuild.CSharp.Tests",
8+
srcs = glob([
9+
"*.cs",
10+
"Properties/*.cs",
11+
]),
12+
deps = [
13+
"//csharp/autobuilder/Semmle.Autobuild.CSharp:bin/Semmle.Autobuild.CSharp",
14+
"//csharp/autobuilder/Semmle.Autobuild.Shared",
15+
"@paket.main//microsoft.net.test.sdk",
16+
"@paket.main//system.io.filesystem",
17+
],
18+
)

csharp/autobuilder/Semmle.Autobuild.CSharp.Tests/BuildScripts.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,9 @@ public void DownloadFile(string address, string fileName)
215215

216216
internal class TestDiagnosticWriter : IDiagnosticsWriter
217217
{
218-
public IList<DiagnosticMessage> Diagnostics { get; } = new List<DiagnosticMessage>();
218+
public IList<Semmle.Util.DiagnosticMessage> Diagnostics { get; } = new List<Semmle.Util.DiagnosticMessage>();
219219

220-
public void AddEntry(DiagnosticMessage message) => this.Diagnostics.Add(message);
220+
public void AddEntry(Semmle.Util.DiagnosticMessage message) => this.Diagnostics.Add(message);
221221

222222
public void Dispose() { }
223223
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
load(
2+
"//misc/bazel:csharp.bzl",
3+
"codeql_csharp_binary",
4+
)
5+
6+
codeql_csharp_binary(
7+
name = "Semmle.Autobuild.CSharp",
8+
srcs = glob([
9+
"*.cs",
10+
"Properties/*.cs",
11+
]),
12+
visibility = ["//csharp:__subpackages__"],
13+
deps = [
14+
"//csharp/autobuilder/Semmle.Autobuild.Shared",
15+
"//csharp/extractor/Semmle.Extraction.CSharp",
16+
"//csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching",
17+
"//csharp/extractor/Semmle.Util",
18+
"@paket.main//microsoft.build",
19+
"@paket.main//newtonsoft.json",
20+
],
21+
)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
load(
2+
"//misc/bazel:csharp.bzl",
3+
"codeql_xunit_test",
4+
)
5+
6+
codeql_xunit_test(
7+
name = "Semmle.Autobuild.Cpp.Tests",
8+
srcs = glob([
9+
"*.cs",
10+
"Properties/*.cs",
11+
]),
12+
deps = [
13+
"//csharp/autobuilder/Semmle.Autobuild.Cpp:bin/Semmle.Autobuild.Cpp",
14+
"//csharp/autobuilder/Semmle.Autobuild.Shared",
15+
"@paket.main//microsoft.net.test.sdk",
16+
"@paket.main//system.io.filesystem",
17+
],
18+
)

0 commit comments

Comments
 (0)