Skip to content

C#: Add assembly attributes to assemblies built with Bazel #16705

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

Merged
merged 4 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 3 additions & 2 deletions csharp/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>

<Company>GitHub</Company>
<Product>CodeQL</Product>
<Copyright>Copyright © $([System.DateTime]::Now.Year) $(Company)</Copyright>
<Version>1.0.0.0</Version>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<FileVersion>1.0.0.0</FileVersion>
<AssemblyVersion>$(Version)</AssemblyVersion>
<FileVersion>$(Version)</FileVersion>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ codeql_xunit_test(
name = "Semmle.Autobuild.CSharp.Tests",
srcs = glob([
"*.cs",
"Properties/*.cs",
]),
deps = [
"//csharp/autobuilder/Semmle.Autobuild.CSharp:bin/Semmle.Autobuild.CSharp",
Expand Down
1 change: 0 additions & 1 deletion csharp/autobuilder/Semmle.Autobuild.CSharp/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ codeql_csharp_binary(
name = "Semmle.Autobuild.CSharp",
srcs = glob([
"*.cs",
"Properties/*.cs",
]),
visibility = ["//csharp:__subpackages__"],
deps = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ codeql_xunit_test(
name = "Semmle.Autobuild.Cpp.Tests",
srcs = glob([
"*.cs",
"Properties/*.cs",
]),
deps = [
"//csharp/autobuilder/Semmle.Autobuild.Cpp:bin/Semmle.Autobuild.Cpp",
Expand Down
1 change: 0 additions & 1 deletion csharp/autobuilder/Semmle.Autobuild.Cpp/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ codeql_csharp_binary(
name = "Semmle.Autobuild.Cpp",
srcs = glob([
"*.cs",
"Properties/*.cs",
]),
visibility = ["//visibility:public"],
deps = [
Expand Down
1 change: 0 additions & 1 deletion csharp/autobuilder/Semmle.Autobuild.Shared/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ codeql_csharp_library(
name = "Semmle.Autobuild.Shared",
srcs = glob([
"*.cs",
"Properties/*.cs",
]),
visibility = ["//visibility:public"],
deps = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ codeql_csharp_library(
name = "Semmle.Extraction.CSharp.DependencyFetching",
srcs = glob([
"*.cs",
"Properties/*.cs",
"SourceGenerators/**/*.cs",
]),
allow_unsafe_blocks = True,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<ItemGroup>
<ProjectReference Include="..\Semmle.Util\Semmle.Util.csproj" />
<ProjectReference Include="..\Semmle.Extraction\Semmle.Extraction.csproj" />

<InternalsVisibleTo Include="Semmle.Extraction.Tests" />
</ItemGroup>
<Import Project="..\..\.paket\Paket.Restore.targets" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ codeql_csharp_binary(
name = "Semmle.Extraction.CSharp.DependencyStubGenerator",
srcs = glob([
"*.cs",
"Properties/*.cs",
]),
visibility = ["//csharp:__pkg__"],
deps = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ codeql_csharp_binary(
name = "Semmle.Extraction.CSharp.Driver",
srcs = glob([
"*.cs",
"Properties/*.cs",
]),
visibility = ["//csharp:__pkg__"],
deps = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ codeql_csharp_binary(
name = "Semmle.Extraction.CSharp.Standalone",
srcs = glob([
"*.cs",
"Properties/*.cs",
]),
visibility = ["//csharp:__subpackages__"],
deps = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ codeql_csharp_library(
name = "Semmle.Extraction.CSharp.StubGenerator",
srcs = glob([
"*.cs",
"Properties/*.cs",
]),
internals_visible_to = ["Semmle.Extraction.Tests"],
visibility = ["//csharp:__subpackages__"],
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<ProjectReference Include="..\Semmle.Util\Semmle.Util.csproj" />
<ProjectReference Include="..\Semmle.Extraction.CSharp.DependencyFetching\Semmle.Extraction.CSharp.DependencyFetching.csproj" />
<ProjectReference Include="..\Semmle.Extraction.CSharp.Util\Semmle.Extraction.CSharp.Util.csproj" />

<InternalsVisibleTo Include="Semmle.Extraction.Tests" />
</ItemGroup>
<Import Project="..\..\.paket\Paket.Restore.targets" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ load(
codeql_csharp_library(
name = "Semmle.Extraction.CSharp.Util",
srcs = glob([
"Properties/*.cs",
"*.cs",
]),
visibility = ["//csharp:__subpackages__"],
Expand Down
1 change: 0 additions & 1 deletion csharp/extractor/Semmle.Extraction.Tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ codeql_xunit_test(
name = "Semmle.Extraction.Tests",
srcs = glob([
"*.cs",
"Properties/*.cs",
]),
deps = [
"//csharp/extractor/Semmle.Extraction",
Expand Down
1 change: 0 additions & 1 deletion csharp/extractor/Semmle.Extraction/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ codeql_csharp_library(
srcs = glob([
"Entities/**/*.cs",
"Extractor/**/*.cs",
"Properties/*.cs",
"*.cs",
]),
# enable via -c dbg on the bazel command line/in .bazelrc.local
Expand Down
1 change: 0 additions & 1 deletion csharp/extractor/Semmle.Util.Tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ codeql_xunit_test(
name = "Semmle.Util.Tests",
srcs = glob([
"*.cs",
"Properties/*.cs",
]),
deps = [
"//csharp/extractor/Semmle.Util",
Expand Down
1 change: 0 additions & 1 deletion csharp/extractor/Semmle.Util/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ codeql_csharp_library(
"Logging/**/*.cs",
"ToolStatusPage/**/*.cs",
"*.cs",
"Properties/*.cs",
]),
visibility = ["//visibility:public"],
deps = [
Expand Down
18 changes: 11 additions & 7 deletions csharp/scripts/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
py_binary(
name = "gen-git-assembly-info",
srcs = ["gen-git-assembly-info.py"],
deps = ["@rules_python//python/runfiles"],
)

py_binary(
name = "gen-assembly-info",
srcs = ["gen-assembly-info.py"],
visibility = ["//csharp:__subpackages__"],
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",
name = "git-assembly-info-src",
srcs = ["@semmle_code//:git_info"],
outs = ["AssemblyInfo.cs"],
cmd = "$(execpath :gen-assembly-info) $@ $(SRCS)",
tools = [":gen-assembly-info"],
outs = ["GitAssemblyInfo.cs"],
cmd = "$(execpath :gen-git-assembly-info) $@ $(SRCS)",
tools = [":gen-git-assembly-info"],
visibility = ["//csharp:__subpackages__"],
)
26 changes: 13 additions & 13 deletions csharp/scripts/gen-assembly-info.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
"""
Generates an `AssemblyInfo.cs` file that specifies the `AssemblyInformationalVersion` attribute.

This attribute is set to the git version string of the repository."""
Generates an `AssemblyInfo.cs` file that specifies a bunch of useful attributes
that we want to set on our assemblies."""

import pathlib
import argparse


def options():
p = argparse.ArgumentParser(
description="Generate the assembly info file that contains the git SHA and branch name"
description="Generate an assembly info file."
)
p.add_argument("output", help="The path to the output file")
p.add_argument("gitinfo_files", nargs="+", help="The path to the gitinfo files")
p.add_argument("name", help="The name of the assembly")
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}")]
[assembly: AssemblyTitle("{opts.name}")]
[assembly: AssemblyProduct("CodeQL")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyCompany("GitHub")]
[assembly: AssemblyCopyright("Copyright © 2024 GitHub")]

[assembly: System.Runtime.Versioning.TargetFramework(".NETCoreApp,Version=v8.0", FrameworkDisplayName = ".NET 8.0")]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we record in the C# wiki that this requires updating when we upgrade .NET?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I'm doing it.


"""
output_file.write_text(output_file_contents)
34 changes: 34 additions & 0 deletions csharp/scripts/gen-git-assembly-info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
Generates an `GitAssemblyInfo.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 git 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)
22 changes: 20 additions & 2 deletions misc/bazel/csharp.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,25 @@ load("//misc/bazel:pkg.bzl", "codeql_pkg_files")

TARGET_FRAMEWORK = "net8.0"

def _gen_assembly_info(name):
assembly_info_gen = name + "-assembly-info"

native.genrule(
name = assembly_info_gen,
outs = [name + "AssemblyInfo.cs"],
cmd = "$(execpath //csharp/scripts:gen-assembly-info) $@ " + name,
tools = ["//csharp/scripts:gen-assembly-info"],
)
return ":" + assembly_info_gen

def codeql_csharp_library(name, **kwargs):
assembly_info_gen = _gen_assembly_info(name)
srcs = kwargs.pop("srcs", [])
srcs.append(assembly_info_gen)

kwargs.setdefault("nullable", "enable")
kwargs.setdefault("target_frameworks", [TARGET_FRAMEWORK])
csharp_library(name = name, **kwargs)
csharp_library(name = name, srcs = srcs, **kwargs)

def codeql_xunit_test(name, **kwargs):
kwargs.setdefault("nullable", "enable")
Expand Down Expand Up @@ -40,8 +55,11 @@ def codeql_csharp_binary(name, **kwargs):
resources = kwargs.pop("resources", [])
srcs = kwargs.pop("srcs", [])

assembly_info_gen = _gen_assembly_info(name)
srcs.append(assembly_info_gen)

# always add the assembly info file that sets the AssemblyInformationalVersion attribute to the extractor version
srcs.append("//csharp/scripts:assembly-info-src")
srcs.append("//csharp/scripts:git-assembly-info-src")

csharp_binary_target = "bin/" + name
publish_binary_target = "publish/" + name
Expand Down
Loading