Skip to content

Commit 1cf5e89

Browse files
authored
Merge pull request github#16747 from tamasvajk/buildless/binary-log-extractor-2
C#: Add binlog support to buildless with source generator support
2 parents fd3089e + 4db586f commit 1cf5e89

File tree

33 files changed

+489
-97
lines changed

33 files changed

+489
-97
lines changed

csharp/autobuilder/Semmle.Autobuild.CSharp/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ codeql_csharp_binary(
1313
"//csharp/autobuilder/Semmle.Autobuild.Shared",
1414
"//csharp/extractor/Semmle.Extraction.CSharp",
1515
"//csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching",
16+
"//csharp/extractor/Semmle.Extraction.CSharp.Driver:bin/Semmle.Extraction.CSharp.Driver",
1617
"//csharp/extractor/Semmle.Extraction.CSharp.Standalone:bin/Semmle.Extraction.CSharp.Standalone",
1718
"//csharp/extractor/Semmle.Util",
1819
"@paket.main//microsoft.build",

csharp/autobuilder/Semmle.Autobuild.CSharp/CSharpAutobuilder.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ public class CSharpAutobuildOptions : AutobuildOptionsShared
1313
{
1414
private const string buildModeEnvironmentVariable = "CODEQL_EXTRACTOR_CSHARP_BUILD_MODE";
1515
internal const string ExtractorOptionBuildless = "CODEQL_EXTRACTOR_CSHARP_OPTION_BUILDLESS";
16+
internal const string ExtractorOptionBinlog = "CODEQL_EXTRACTOR_CSHARP_OPTION_BINLOG";
1617

1718
public bool Buildless { get; }
19+
public string? Binlog { get; }
1820

1921
public override Language Language => Language.CSharp;
2022

@@ -29,7 +31,7 @@ public CSharpAutobuildOptions(IBuildActions actions) : base(actions)
2931
actions.GetEnvironmentVariable(ExtractorOptionBuildless).AsBool("buildless", false) ||
3032
actions.GetEnvironmentVariable(buildModeEnvironmentVariable)?.ToLower() == "none";
3133

32-
34+
Binlog = actions.GetEnvironmentVariable(ExtractorOptionBinlog);
3335
}
3436
}
3537

@@ -114,6 +116,20 @@ private BuildScript AddBuildlessStartedDiagnostic()
114116
markdownMessage: "C# was extracted with build-mode set to 'none'. This means that all C# source in the working directory will be scanned, with build tools, such as Nuget and Dotnet CLIs, only contributing information about external dependencies.",
115117
severity: DiagnosticMessage.TspSeverity.Note
116118
));
119+
120+
// For the time being we are adding an additional message regarding the binlog usage. In the future, we might want to remove the buildless messages altogether when the binlog option is specified.
121+
if (actions.GetEnvironmentVariable(CSharpAutobuildOptions.ExtractorOptionBinlog) is not null)
122+
{
123+
AddDiagnostic(new DiagnosticMessage(
124+
Options.Language,
125+
"buildless/binlog",
126+
"C# was extracted with the experimental 'binlog' option",
127+
visibility: new DiagnosticMessage.TspVisibility(statusPage: true, cliSummaryTable: true, telemetry: true),
128+
markdownMessage: "C# was extracted with the experimental 'binlog' option.",
129+
severity: DiagnosticMessage.TspSeverity.Note
130+
));
131+
}
132+
117133
return 0;
118134
});
119135
}

csharp/autobuilder/Semmle.Autobuild.CSharp/Semmle.Autobuild.CSharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<ProjectReference Include="..\..\extractor\Semmle.Util\Semmle.Util.csproj" />
77
<ProjectReference Include="..\..\extractor\Semmle.Extraction.CSharp\Semmle.Extraction.CSharp.csproj" />
88
<ProjectReference Include="..\..\extractor\Semmle.Extraction.CSharp.Standalone\Semmle.Extraction.CSharp.Standalone.csproj" />
9+
<ProjectReference Include="..\..\extractor\Semmle.Extraction.CSharp.Driver\Semmle.Extraction.CSharp.Driver.csproj" />
910
<ProjectReference Include="..\..\extractor\Semmle.Extraction.CSharp.DependencyFetching\Semmle.Extraction.CSharp.DependencyFetching.csproj" />
1011
<ProjectReference Include="..\Semmle.Autobuild.Shared\Semmle.Autobuild.Shared.csproj" />
1112
</ItemGroup>

csharp/autobuilder/Semmle.Autobuild.CSharp/StandaloneBuildRule.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ internal class StandaloneBuildRule : IBuildRule<CSharpAutobuildOptions>
1010
{
1111
public BuildScript Analyse(IAutobuilder<CSharpAutobuildOptions> builder, bool auto)
1212
{
13-
return BuildScript.Create(_ => Semmle.Extraction.CSharp.Standalone.Program.Main([]));
13+
return builder.Options.Binlog is string binlog
14+
? BuildScript.Create(_ => Semmle.Extraction.CSharp.Driver.Main(["--binlog", binlog]))
15+
: BuildScript.Create(_ => Semmle.Extraction.CSharp.Standalone.Program.Main([]));
1416
}
1517
}
1618
}

csharp/codeql-extractor.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,9 @@ options:
6565
- progress+++
6666
type: string
6767
pattern: "^(off|errors|warnings|(info|progress)|(debug|progress\\+)|(trace|progress\\+\\+)|progress\\+\\+\\+)$"
68+
binlog:
69+
title: Binlog
70+
description: >
71+
[EXPERIMENTAL] The value is a path to the MsBuild binary log file that should be extracted.
72+
This option only works when `--build-mode none` is also specified.
73+
type: string

csharp/extractor/Semmle.Extraction.CSharp.Driver/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ codeql_csharp_binary(
88
srcs = glob([
99
"*.cs",
1010
]),
11-
visibility = ["//csharp:__pkg__"],
11+
visibility = ["//csharp:__subpackages__"],
1212
deps = [
1313
"//csharp/extractor/Semmle.Extraction.CSharp",
1414
],

csharp/extractor/Semmle.Extraction.CSharp/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ codeql_csharp_library(
1919
"//csharp/extractor/Semmle.Extraction",
2020
"//csharp/extractor/Semmle.Extraction.CSharp.Util",
2121
"//csharp/extractor/Semmle.Util",
22+
"@paket.main//basic.compilerlog.util",
2223
"@paket.main//microsoft.build",
2324
"@paket.main//microsoft.codeanalysis.csharp",
2425
],

csharp/extractor/Semmle.Extraction.CSharp/Entities/File.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,14 @@ public override void Populate(TextWriter trapFile)
3434
lineCounts.Total++;
3535

3636
trapFile.numlines(this, lineCounts);
37-
Context.TrapWriter.Archive(originalPath, TransformedPath, text.Encoding ?? System.Text.Encoding.Default);
37+
if (BinaryLogExtractionContext.GetAdjustedPath(Context.ExtractionContext, originalPath) is not null)
38+
{
39+
Context.TrapWriter.ArchiveContent(rawText, TransformedPath);
40+
}
41+
else
42+
{
43+
Context.TrapWriter.Archive(originalPath, TransformedPath, text.Encoding ?? System.Text.Encoding.Default);
44+
}
3845
}
3946
}
4047
else if (IsPossiblyTextFile())

csharp/extractor/Semmle.Extraction.CSharp/Extractor/Analyser.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ private void DoExtractTree(SyntaxTree tree)
185185
{
186186
var stopwatch = new Stopwatch();
187187
stopwatch.Start();
188-
var sourcePath = tree.FilePath;
188+
var sourcePath = BinaryLogExtractionContext.GetAdjustedPath(ExtractionContext, tree.FilePath) ?? tree.FilePath;
189+
189190
var transformedSourcePath = PathTransformer.Transform(sourcePath);
190191

191192
var trapPath = transformedSourcePath.GetTrapPath(Logger, options.TrapCompression);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.Collections.Generic;
2+
using Microsoft.CodeAnalysis.CSharp;
3+
using Semmle.Util;
4+
using Semmle.Util.Logging;
5+
6+
namespace Semmle.Extraction.CSharp
7+
{
8+
public class BinaryLogAnalyser : Analyser
9+
{
10+
public BinaryLogAnalyser(IProgressMonitor pm, ILogger logger, PathTransformer pathTransformer, IPathCache pathCache, bool addAssemblyTrapPrefix)
11+
: base(pm, logger, pathTransformer, pathCache, addAssemblyTrapPrefix)
12+
{
13+
}
14+
15+
public void Initialize(
16+
string cwd, string[] args, string outputPath, CSharpCompilation compilation,
17+
IEnumerable<Microsoft.CodeAnalysis.SyntaxTree> generatedSyntaxTrees,
18+
string compilationIdentifier, CommonOptions options)
19+
{
20+
base.compilation = compilation;
21+
ExtractionContext = new BinaryLogExtractionContext(
22+
cwd, args, outputPath, generatedSyntaxTrees, compilationIdentifier,
23+
Logger, PathTransformer, options.QlTest);
24+
this.options = options;
25+
LogExtractorInfo();
26+
SetReferencePaths();
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)