Skip to content

Commit ded4901

Browse files
authored
Merge pull request github#13970 from michaelnebel/csharp/usereferenceassemlblies
C#: Compile against the reference assemblies in the standalone extractor (if possible)
2 parents cd289f8 + 3afa4aa commit ded4901

File tree

7 files changed

+53
-35
lines changed

7 files changed

+53
-35
lines changed

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DependencyManager.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,6 @@ public DependencyManager(string srcDir, IDependencyOptions options, ILogger logg
8585
}
8686
}
8787

88-
if (options.UseMscorlib)
89-
{
90-
UseReference(typeof(object).Assembly.Location);
91-
}
92-
9388
if (options.UseNuGet)
9489
{
9590
dllDirNames.Add(packageDirectory.DirInfo.FullName);

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DependencyOptions.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,6 @@ public interface IDependencyOptions
4040
/// </summary>
4141
bool ScanNetFrameworkDlls { get; }
4242

43-
/// <summary>
44-
/// Whether to use mscorlib as a reference.
45-
/// </summary>
46-
bool UseMscorlib { get; }
47-
4843
/// <summary>
4944
/// Determine whether the given path should be excluded.
5045
/// </summary>
@@ -74,8 +69,6 @@ public class DependencyOptions : IDependencyOptions
7469

7570
public bool ScanNetFrameworkDlls { get; set; } = true;
7671

77-
public bool UseMscorlib { get; set; } = true;
78-
7972
public bool ExcludesFile(string path) =>
8073
Excludes.Any(path.Contains);
8174

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotnetVersion.cs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,44 @@ internal record DotnetVersion : IComparable<DotnetVersion>
1010
private readonly Version? preReleaseVersion;
1111
private readonly string? preReleaseVersionType;
1212
private bool IsPreRelease => preReleaseVersionType is not null && preReleaseVersion is not null;
13-
public string FullPath
13+
14+
private string FullVersion
1415
{
1516
get
1617
{
1718
var preRelease = IsPreRelease ? $"-{preReleaseVersionType}.{preReleaseVersion}" : "";
18-
var version = this.version + preRelease;
19-
return Path.Combine(dir, version);
19+
return this.version + preRelease;
2020
}
2121
}
2222

23+
public string FullPath => Path.Combine(dir, FullVersion);
24+
25+
/**
26+
* The full path to the reference assemblies for this runtime.
27+
* This is the same as FullPath, except that we assume that the
28+
* reference assemblies are in a directory called "packs" and
29+
* the reference assemblies themselves are in a directory called
30+
* "<Framework>.Ref/ref".
31+
* Example:
32+
* FullPath: /usr/share/dotnet/shared/Microsoft.NETCore.App/7.0.2
33+
* FullPathReferenceAssemblies: /usr/share/dotnet/packs/Microsoft.NETCore.App.Ref/7.0.2/ref
34+
*/
35+
public string? FullPathReferenceAssemblies
36+
{
37+
get
38+
{
39+
var directories = dir.Split(Path.DirectorySeparatorChar);
40+
if (directories.Length >= 2)
41+
{
42+
directories[^2] = "packs";
43+
directories[^1] = $"{directories[^1]}.Ref";
44+
return Path.Combine(string.Join(Path.DirectorySeparatorChar, directories), FullVersion, "ref");
45+
}
46+
return null;
47+
}
48+
}
49+
50+
2351
public DotnetVersion(string dir, string version, string preReleaseVersionType, string preReleaseVersion)
2452
{
2553
this.dir = dir;

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/Runtime.cs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,18 @@ private static IEnumerable<string> DesktopRuntimes
9494
}
9595
}
9696

97+
private string? GetVersion(string framework)
98+
{
99+
if (NewestRuntimes.TryGetValue(framework, out var version))
100+
{
101+
var refAssemblies = version.FullPathReferenceAssemblies;
102+
return Directory.Exists(refAssemblies)
103+
? refAssemblies
104+
: version.FullPath;
105+
}
106+
return null;
107+
}
108+
97109
/// <summary>
98110
/// Gets the .NET runtime location to use for extraction.
99111
/// </summary>
@@ -105,9 +117,9 @@ public string GetRuntime(bool useSelfContained)
105117
}
106118

107119
// Location of the newest .NET Core Runtime.
108-
if (NewestRuntimes.TryGetValue(netCoreApp, out var netCoreVersion))
120+
if (GetVersion(netCoreApp) is string path)
109121
{
110-
return netCoreVersion.FullPath;
122+
return path;
111123
}
112124

113125
if (DesktopRuntimes.Any())
@@ -122,14 +134,6 @@ public string GetRuntime(bool useSelfContained)
122134
/// <summary>
123135
/// Gets the ASP.NET runtime location to use for extraction, if one exists.
124136
/// </summary>
125-
public string? GetAspRuntime()
126-
{
127-
// Location of the newest ASP.NET Core Runtime.
128-
if (NewestRuntimes.TryGetValue(aspNetCoreApp, out var aspNetCoreVersion))
129-
{
130-
return aspNetCoreVersion.FullPath;
131-
}
132-
return null;
133-
}
137+
public string? GetAspRuntime() => GetVersion(aspNetCoreApp);
134138
}
135139
}

csharp/extractor/Semmle.Extraction.CSharp.Standalone/Options.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ public override bool HandleFlag(string key, bool value)
2929
case "all-references":
3030
AnalyseCsProjFiles = !value;
3131
return true;
32-
case "stdlib":
33-
dependencies.UseMscorlib = value;
34-
return true;
3532
case "skip-dotnet":
3633
dependencies.ScanNetFrameworkDlls = !value;
3734
return true;
@@ -110,7 +107,7 @@ public override void InvalidArgument(string argument)
110107
/// <summary>
111108
/// Outputs the command line options to the console.
112109
/// </summary>
113-
public static void ShowHelp(System.IO.TextWriter output)
110+
public static void ShowHelp(TextWriter output)
114111
{
115112
output.WriteLine("C# standalone extractor\n\nExtracts a C# project in the current directory without performing a build.\n");
116113
output.WriteLine("Additional options:\n");
@@ -121,7 +118,6 @@ public static void ShowHelp(System.IO.TextWriter output)
121118
output.WriteLine(" --dry-run Stop before extraction");
122119
output.WriteLine(" --skip-nuget Do not download nuget packages");
123120
output.WriteLine(" --all-references Use all references (default is to only use references in .csproj files)");
124-
output.WriteLine(" --nostdlib Do not link mscorlib.dll (use only for extracting mscorlib itself)");
125121
output.WriteLine(" --threads:nnn Specify number of threads (default=CPU cores)");
126122
output.WriteLine(" --verbose Produce more output");
127123
output.WriteLine(" --pdb Cross-reference information from PDBs where available");

csharp/extractor/Semmle.Extraction.Tests/Options.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,8 @@ public void Framework()
135135
public void StandaloneDefaults()
136136
{
137137
standaloneOptions = CSharp.Standalone.Options.Create(Array.Empty<string>());
138-
Assert.Equal(0, standaloneOptions.Dependencies.DllDirs.Count);
138+
Assert.Empty(standaloneOptions.Dependencies.DllDirs);
139139
Assert.True(standaloneOptions.Dependencies.UseNuGet);
140-
Assert.True(standaloneOptions.Dependencies.UseMscorlib);
141140
Assert.False(standaloneOptions.SkipExtraction);
142141
Assert.Null(standaloneOptions.Dependencies.SolutionFile);
143142
Assert.True(standaloneOptions.Dependencies.ScanNetFrameworkDlls);
@@ -147,12 +146,11 @@ public void StandaloneDefaults()
147146
[Fact]
148147
public void StandaloneOptions()
149148
{
150-
standaloneOptions = CSharp.Standalone.Options.Create(new string[] { "--references:foo", "--silent", "--skip-nuget", "--skip-dotnet", "--exclude", "bar", "--nostdlib" });
149+
standaloneOptions = CSharp.Standalone.Options.Create(new string[] { "--references:foo", "--silent", "--skip-nuget", "--skip-dotnet", "--exclude", "bar" });
151150
Assert.Equal("foo", standaloneOptions.Dependencies.DllDirs[0]);
152151
Assert.Equal("bar", standaloneOptions.Dependencies.Excludes[0]);
153152
Assert.Equal(Verbosity.Off, standaloneOptions.Verbosity);
154153
Assert.False(standaloneOptions.Dependencies.UseNuGet);
155-
Assert.False(standaloneOptions.Dependencies.UseMscorlib);
156154
Assert.False(standaloneOptions.Dependencies.ScanNetFrameworkDlls);
157155
Assert.False(standaloneOptions.Errors);
158156
Assert.False(standaloneOptions.Help);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* The `--nostdlib` extractor option for the standalone extractor has been removed.

0 commit comments

Comments
 (0)