Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bazel-build for C# language pack #16519

Merged
merged 14 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions csharp/extractor/Semmle.Extraction/Extractor/Extractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,14 @@ public static string Version
{
get
{
// the resources for git information are always attached to the entry` assembly by our build system
// the attribute for the git information are always attached to the entry assembly by our build system
var assembly = Assembly.GetEntryAssembly();
var describeAllStream = assembly.GetManifestResourceStream("git-ql-describe-all.log");
var headSHAStream = assembly.GetManifestResourceStream("git-ql-rev-parse.log");
if (describeAllStream == null || headSHAStream == null)
var versionString = assembly!.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
criemen marked this conversation as resolved.
Show resolved Hide resolved
if (versionString == null)
{
return "unknown (not built from internal bazel workspace)";
}
var describeAll = new StreamReader(describeAllStream).ReadToEnd().Trim('\n');
var headSHA = new StreamReader(headSHAStream).ReadToEnd().Trim('\n');
return $"{describeAll} ({headSHA})";
return versionString.InformationalVersion;
}
}

Expand Down
17 changes: 17 additions & 0 deletions csharp/scripts/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
py_binary(
name = "gen-assembly-info",
srcs = ["gen-assembly-info.py"],
deps = ["@rules_python//python/runfiles"],
)

# this is an instance of the dbscheme kept in the bazel build tree
# this allows everything that bazel builds to be up-to-date,
# independently from whether //go:gen was already run to update the checked in files
genrule(
name = "assembly-info-src",
srcs = ["@semmle_code//:git_info"],
outs = ["AssemblyInfo.cs"],
cmd = "$(execpath :gen-assembly-info) $@ $(SRCS)",
tools = [":gen-assembly-info"],
visibility = ["//csharp:__subpackages__"],
)
34 changes: 34 additions & 0 deletions csharp/scripts/gen-assembly-info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
Generates an `AssemblyInfo.cs` file that specifies the `AssemblyInformationalVersion` attribute.

This attribute is set to the git version string of the repository."""

import pathlib
import argparse


def options():
p = argparse.ArgumentParser(
description="Generate the assembly info file that contains the git SHA and branch name"
)
p.add_argument("output", help="The path to the output file")
p.add_argument("gitinfo_files", nargs="+", help="The path to the gitinfo files")
return p.parse_args()


opts = options()

gitfiles = dict()
for file in map(pathlib.Path, opts.gitinfo_files):
gitfiles[file.name] = file

version_string = gitfiles["git-ql-describe-all.log"].read_text().strip()
version_string += f" ({gitfiles['git-ql-rev-parse.log'].read_text().strip()})"

output_file = pathlib.Path(opts.output)
output_file_contents = f"""
using System.Reflection;

[assembly: AssemblyInformationalVersion("{version_string}")]
"""
output_file.write_text(output_file_contents)
7 changes: 4 additions & 3 deletions misc/bazel/csharp.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@ def codeql_csharp_binary(name, **kwargs):
nullable = kwargs.pop("nullable", "enable")
visibility = kwargs.pop("visibility", ["//visibility:public"])
resources = kwargs.pop("resources", [])
srcs = kwargs.pop("srcs", [])

# the entry assembly always has the git info embedded
resources.append("@semmle_code//:git_info")
# always add the assembly info file that sets the AssemblyInformationalVersion attribute to the extractor version
srcs.append("//csharp/scripts:assembly-info-src")

target_frameworks = kwargs.pop("target_frameworks", [TARGET_FRAMEWORK])
csharp_binary_target = "bin/" + name
publish_binary_target = "publish/" + name
csharp_binary(name = csharp_binary_target, nullable = nullable, target_frameworks = target_frameworks, resources = resources, visibility = visibility, **kwargs)
csharp_binary(name = csharp_binary_target, srcs = srcs, nullable = nullable, target_frameworks = target_frameworks, resources = resources, visibility = visibility, **kwargs)
publish_binary(
name = publish_binary_target,
binary = csharp_binary_target,
Expand Down