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

C#: Add binlog support to buildless #16672

Closed
Closed
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
1 change: 1 addition & 0 deletions csharp/autobuilder/Semmle.Autobuild.CSharp/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ codeql_csharp_binary(
"//csharp/autobuilder/Semmle.Autobuild.Shared",
"//csharp/extractor/Semmle.Extraction.CSharp",
"//csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching",
"//csharp/extractor/Semmle.Extraction.CSharp.Driver:bin/Semmle.Extraction.CSharp.Driver",
"//csharp/extractor/Semmle.Extraction.CSharp.Standalone:bin/Semmle.Extraction.CSharp.Standalone",
"//csharp/extractor/Semmle.Util",
"@paket.main//microsoft.build",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
private const string extractorOptionPrefix = "CODEQL_EXTRACTOR_CSHARP_OPTION_";

public bool Buildless { get; }
public string? Binlog { get; }

public override Language Language => Language.CSharp;

Expand All @@ -29,7 +30,7 @@
actions.GetEnvironmentVariable(extractorOptionPrefix + "BUILDLESS").AsBool("buildless", false) ||
actions.GetEnvironmentVariable(buildModeEnvironmentVariable)?.ToLower() == "none";


Binlog = actions.GetEnvironmentVariable(extractorOptionPrefix + "BINLOG");

Check failure on line 33 in csharp/autobuilder/Semmle.Autobuild.CSharp/CSharpAutobuilder.cs

View workflow job for this annotation

GitHub Actions / CodeQL-Build

The name 'extractorOptionPrefix' does not exist in the current context

Check failure on line 33 in csharp/autobuilder/Semmle.Autobuild.CSharp/CSharpAutobuilder.cs

View workflow job for this annotation

GitHub Actions / CodeQL-Build

The name 'extractorOptionPrefix' does not exist in the current context

Check failure on line 33 in csharp/autobuilder/Semmle.Autobuild.CSharp/CSharpAutobuilder.cs

View workflow job for this annotation

GitHub Actions / qltest (1/2)

The name 'extractorOptionPrefix' does not exist in the current context

Check failure on line 33 in csharp/autobuilder/Semmle.Autobuild.CSharp/CSharpAutobuilder.cs

View workflow job for this annotation

GitHub Actions / qltest (2/2)

The name 'extractorOptionPrefix' does not exist in the current context

Check failure on line 33 in csharp/autobuilder/Semmle.Autobuild.CSharp/CSharpAutobuilder.cs

View workflow job for this annotation

GitHub Actions / stubgentest

The name 'extractorOptionPrefix' does not exist in the current context

Check failure on line 33 in csharp/autobuilder/Semmle.Autobuild.CSharp/CSharpAutobuilder.cs

View workflow job for this annotation

GitHub Actions / unit-tests (ubuntu-latest)

The name 'extractorOptionPrefix' does not exist in the current context
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<ProjectReference Include="..\..\extractor\Semmle.Util\Semmle.Util.csproj" />
<ProjectReference Include="..\..\extractor\Semmle.Extraction.CSharp\Semmle.Extraction.CSharp.csproj" />
<ProjectReference Include="..\..\extractor\Semmle.Extraction.CSharp.Standalone\Semmle.Extraction.CSharp.Standalone.csproj" />
<ProjectReference Include="..\..\extractor\Semmle.Extraction.CSharp.Driver\Semmle.Extraction.CSharp.Driver.csproj" />
<ProjectReference Include="..\..\extractor\Semmle.Extraction.CSharp.DependencyFetching\Semmle.Extraction.CSharp.DependencyFetching.csproj" />
<ProjectReference Include="..\Semmle.Autobuild.Shared\Semmle.Autobuild.Shared.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ internal class StandaloneBuildRule : IBuildRule<CSharpAutobuildOptions>
{
public BuildScript Analyse(IAutobuilder<CSharpAutobuildOptions> builder, bool auto)
{
return BuildScript.Create(_ => Semmle.Extraction.CSharp.Standalone.Program.Main([]));
return builder.Options.Binlog is string binlog
? BuildScript.Create(_ => Semmle.Extraction.CSharp.Driver.Main(["--binlog", binlog]))
: BuildScript.Create(_ => Semmle.Extraction.CSharp.Standalone.Program.Main([]));
}
}
}
6 changes: 6 additions & 0 deletions csharp/codeql-extractor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,9 @@ options:
- progress+++
type: string
pattern: "^(off|errors|warnings|(info|progress)|(debug|progress\\+)|(trace|progress\\+\\+)|progress\\+\\+\\+)$"
binlog:
title: Binlog
description: >
[EXPERIMENTAL] The value is a path to the MsBuild binary log file that should be extracted.
This option only works when `--build-mode none` is also specified.
type: string
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ codeql_csharp_binary(
"*.cs",
"Properties/*.cs",
]),
visibility = ["//csharp:__pkg__"],
visibility = ["//csharp:__subpackages__"],
deps = [
"//csharp/extractor/Semmle.Extraction.CSharp",
],
Expand Down
1 change: 1 addition & 0 deletions csharp/extractor/Semmle.Extraction.CSharp/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ codeql_csharp_library(
"//csharp/extractor/Semmle.Extraction",
"//csharp/extractor/Semmle.Extraction.CSharp.Util",
"//csharp/extractor/Semmle.Util",
"@paket.main//basic.compilerlog.util",
"@paket.main//microsoft.build",
"@paket.main//microsoft.codeanalysis.csharp",
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
using Microsoft.CodeAnalysis.Text;
using Semmle.Util;
using Semmle.Util.Logging;
using Basic.CompilerLog;
using Basic.CompilerLog.Util;

namespace Semmle.Extraction.CSharp
{
Expand Down Expand Up @@ -92,16 +94,63 @@ public static ILogger MakeLogger(Verbosity verbosity, bool includeConsole)
/// <param name="args">Command line arguments as passed to csc.exe</param>
/// <returns><see cref="ExitCode"/></returns>
public static ExitCode Run(string[] args)
{
var options = Options.CreateWithEnvironment(args);
tamasvajk marked this conversation as resolved.
Show resolved Hide resolved
using var logger = MakeLogger(options.Verbosity, options.Console);
if (options.BinaryLogPath is string binlogPath)
{
logger.LogInfo($"Reading compiler calls from binary log {binlogPath}");
using var fileStream = new FileStream(binlogPath, FileMode.Open, FileAccess.Read, FileShare.Read);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We should expect this to fail, and log it nicely.


// Filter out compiler calls that aren't interesting for examination
var predicate = bool (CompilerCall compilerCall) =>
{
if (!compilerCall.IsCSharp)
{
return false;
}

return compilerCall.Kind switch
{
CompilerCallKind.XamlPreCompile => false,
CompilerCallKind.Satellite => false,
CompilerCallKind.WpfTemporaryCompile => false,
_ => true
};
};

var compilerCalls = BinaryLogUtil.ReadAllCompilerCalls(fileStream, predicate);
var allFailed = true;
foreach (var compilerCall in compilerCalls)
{
var diagnosticName = compilerCall.GetDiagnosticName();
Console.WriteLine($"Processing {diagnosticName}");
var compilerCallOptions = Options.CreateWithEnvironment([]);
compilerCallOptions.CompilerName = compilerCall.CompilerFilePath;
compilerCallOptions.CompilerArguments.AddRange(compilerCall.GetArguments());
logger.LogInfo($"Running extractor on arguments from binary log. Processing {diagnosticName}.");
var ec = Run(compilerCallOptions, logger);
if (ec != ExitCode.Failed)
{
allFailed &= false;
}
}

return allFailed ? ExitCode.Failed : ExitCode.Ok;
}
else
{
return Run(options, logger);
}
}

public static ExitCode Run(Options options, ILogger logger)
{
var stopwatch = new Stopwatch();
stopwatch.Start();

var options = Options.CreateWithEnvironment(args);
var workingDirectory = Directory.GetCurrentDirectory();
var compilerArgs = options.CompilerArguments.ToArray();

using var logger = MakeLogger(options.Verbosity, options.Console);

var canonicalPathCache = CanonicalPathCache.Create(logger, 1000);
var pathTransformer = new PathTransformer(canonicalPathCache);

Expand Down Expand Up @@ -393,10 +442,10 @@ private static ExitCode AnalyseTracing(
.Select(src => src.Path)
.ToList();

if (compilerArguments.GeneratedFilesOutputDirectory is not null)
{
paths.AddRange(Directory.GetFiles(compilerArguments.GeneratedFilesOutputDirectory, "*.cs", new EnumerationOptions { RecurseSubdirectories = true, MatchCasing = MatchCasing.CaseInsensitive }));
}
// if (compilerArguments.GeneratedFilesOutputDirectory is not null)
// {
// paths.AddRange(Directory.GetFiles(compilerArguments.GeneratedFilesOutputDirectory, "*.cs", new EnumerationOptions { RecurseSubdirectories = true, MatchCasing = MatchCasing.CaseInsensitive }));
// }

return ReadSyntaxTrees(
paths.Select(canonicalPathCache.GetCanonicalPath),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ public sealed class Options : CommonOptions
/// <summary>
/// All other arguments passed to the compilation.
/// </summary>
public IList<string> CompilerArguments { get; } = new List<string>();
public List<string> CompilerArguments { get; } = new List<string>();

/// <summary>
/// Holds if assembly information should be prefixed to TRAP labels.
/// </summary>
public bool AssemblySensitiveTrap { get; private set; } = false;

public string? BinaryLogPath { get; set; }

public static Options CreateWithEnvironment(string[] arguments)
{
var options = new Options();
Expand Down Expand Up @@ -65,6 +67,9 @@ public override bool HandleOption(string key, string value)
case "load-sources-from-project":
ProjectsToLoad.Add(value);
return true;
case "binlog":
BinaryLogPath = value;
return true;
default:
return base.HandleOption(key, value);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Microsoft.Build
Microsoft.CodeAnalysis.CSharp

Basic.CompilerLog.Util
1 change: 1 addition & 0 deletions csharp/paket.dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ source https://api.nuget.org/v3/index.json
# behave like nuget in choosing transitive dependency versions
strategy: min

nuget Basic.CompilerLog.Util
nuget Mono.Posix.NETStandard
nuget Newtonsoft.Json
nuget xunit
Expand Down
21 changes: 21 additions & 0 deletions csharp/paket.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading