mirror of
https://github.com/github/codeql.git
synced 2026-04-25 08:45:14 +02:00
Introduce environment variable to specify framework assembly locations
This commit is contained in:
@@ -75,7 +75,7 @@ namespace Semmle.Autobuild.Shared
|
||||
return defaultValue;
|
||||
|
||||
return value.
|
||||
Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).
|
||||
Split(FileUtils.NewLineCharacters, StringSplitOptions.RemoveEmptyEntries).
|
||||
Select(s => AsStringWithExpandedEnvVars(s, actions)).ToArray();
|
||||
}
|
||||
|
||||
|
||||
@@ -158,6 +158,49 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
|
||||
{
|
||||
var frameworkLocations = new HashSet<string>();
|
||||
|
||||
var frameworkReferences = Environment.GetEnvironmentVariable(EnvironmentVariableNames.DotnetFrameworkReferences);
|
||||
var frameworkReferencesUseSubfolders = Environment.GetEnvironmentVariable(EnvironmentVariableNames.DotnetFrameworkReferencesUseSubfolders);
|
||||
_ = bool.TryParse(frameworkReferencesUseSubfolders, out var useSubfolders);
|
||||
if (!string.IsNullOrWhiteSpace(frameworkReferences))
|
||||
{
|
||||
var frameworkPaths = frameworkReferences.Split(FileUtils.NewLineCharacters, StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
foreach (var path in frameworkPaths)
|
||||
{
|
||||
if (!Directory.Exists(path))
|
||||
{
|
||||
logger.LogError($"Specified framework reference path '{path}' does not exist.");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (useSubfolders)
|
||||
{
|
||||
dllPaths.Add(path);
|
||||
frameworkLocations.Add(path);
|
||||
continue;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var dlls = Directory.GetFiles(path, "*.dll", new EnumerationOptions { RecurseSubdirectories = false, MatchCasing = MatchCasing.CaseInsensitive });
|
||||
if (dlls.Length == 0)
|
||||
{
|
||||
logger.LogError($"No DLLs found in specified framework reference path '{path}'.");
|
||||
continue;
|
||||
}
|
||||
|
||||
dllPaths.UnionWith(dlls);
|
||||
frameworkLocations.UnionWith(dlls);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logger.LogError($"Error while searching for DLLs in '{path}': {e.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
return frameworkLocations;
|
||||
}
|
||||
|
||||
AddNetFrameworkDlls(dllPaths, frameworkLocations);
|
||||
AddAspNetCoreFrameworkDlls(dllPaths, frameworkLocations);
|
||||
AddMicrosoftWindowsDesktopDlls(dllPaths, frameworkLocations);
|
||||
|
||||
@@ -2,7 +2,19 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
|
||||
{
|
||||
internal class EnvironmentVariableNames
|
||||
{
|
||||
/// <summary>
|
||||
/// Controls whether to generate source files from Asp.Net Core views (`.cshtml`, `.razor`).
|
||||
/// </summary>
|
||||
public const string WebViewGeneration = "CODEQL_EXTRACTOR_CSHARP_BUILDLESS_EXTRACT_WEB_VIEWS";
|
||||
public const string MonoPath = "CODEQL_EXTRACTOR_CSHARP_BUILDLESS_MONO_PATH";
|
||||
|
||||
/// <summary>
|
||||
/// Specifies the location of .Net framework references added to the compilation.
|
||||
/// </summary>
|
||||
public const string DotnetFrameworkReferences = "CODEQL_EXTRACTOR_CSHARP_BUILDLESS_DOTNET_FRAMEWORK_REFERENCES";
|
||||
|
||||
/// <summary>
|
||||
/// Controls whether to use framework dependencies from subfolders.
|
||||
/// </summary>
|
||||
public const string DotnetFrameworkReferencesUseSubfolders = "CODEQL_EXTRACTOR_CSHARP_BUILDLESS_DOTNET_FRAMEWORK_REFERENCES_USE_SUBFOLDERS";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
|
||||
|
||||
public IEnumerable<FileInfo> Filter(IEnumerable<FileInfo> files)
|
||||
{
|
||||
var filters = (Environment.GetEnvironmentVariable("LGTM_INDEX_FILTERS") ?? string.Empty).Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
var filters = (Environment.GetEnvironmentVariable("LGTM_INDEX_FILTERS") ?? string.Empty).Split(FileUtils.NewLineCharacters, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (filters.Length == 0)
|
||||
{
|
||||
return files;
|
||||
|
||||
@@ -71,7 +71,7 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
|
||||
var args = new StringBuilder();
|
||||
args.Append($"/target:exe /generatedfilesout:\"{outputFolder}\" /out:\"{dllPath}\" /analyzerconfig:\"{analyzerConfig}\" ");
|
||||
|
||||
foreach (var f in Directory.GetFiles(sourceGeneratorFolder, "*.dll"))
|
||||
foreach (var f in Directory.GetFiles(sourceGeneratorFolder, "*.dll", new EnumerationOptions { RecurseSubdirectories = false, MatchCasing = MatchCasing.CaseInsensitive }))
|
||||
{
|
||||
args.Append($"/analyzer:\"{f}\" ");
|
||||
}
|
||||
|
||||
@@ -78,30 +78,12 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
|
||||
.OrderByDescending(Path.GetFileName);
|
||||
}
|
||||
|
||||
string? monoDir = null;
|
||||
|
||||
var monoPathEnv = Environment.GetEnvironmentVariable(EnvironmentVariableNames.MonoPath);
|
||||
if (monoPathEnv is not null)
|
||||
{
|
||||
if (Directory.Exists(monoPathEnv))
|
||||
{
|
||||
monoDir = monoPathEnv;
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.LogError($"The directory specified in {EnvironmentVariableNames.MonoPath} does not exist: {monoPathEnv}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var monoPath = FileUtils.FindProgramOnPath(Win32.IsWindows() ? "mono.exe" : "mono");
|
||||
string[] monoDirs = monoPath is not null
|
||||
? [Path.GetFullPath(Path.Combine(monoPath, "..", "lib", "mono")), monoPath]
|
||||
: ["/usr/lib/mono", "/usr/local/mono", "/usr/local/bin/mono", @"C:\Program Files\Mono\lib\mono"];
|
||||
|
||||
monoDir = monoDirs.FirstOrDefault(Directory.Exists);
|
||||
}
|
||||
var monoPath = FileUtils.FindProgramOnPath(Win32.IsWindows() ? "mono.exe" : "mono");
|
||||
string[] monoDirs = monoPath is not null
|
||||
? [Path.GetFullPath(Path.Combine(monoPath, "..", "lib", "mono")), monoPath]
|
||||
: ["/usr/lib/mono", "/usr/local/mono", "/usr/local/bin/mono", @"C:\Program Files\Mono\lib\mono"];
|
||||
|
||||
var monoDir = monoDirs.FirstOrDefault(Directory.Exists);
|
||||
if (monoDir is not null)
|
||||
{
|
||||
return Directory.EnumerateDirectories(monoDir)
|
||||
|
||||
@@ -386,7 +386,7 @@ namespace Semmle.Extraction.CSharp
|
||||
|
||||
if (compilerArguments.GeneratedFilesOutputDirectory is not null)
|
||||
{
|
||||
paths.AddRange(Directory.GetFiles(compilerArguments.GeneratedFilesOutputDirectory, "*.cs", SearchOption.AllDirectories));
|
||||
paths.AddRange(Directory.GetFiles(compilerArguments.GeneratedFilesOutputDirectory, "*.cs", new EnumerationOptions { RecurseSubdirectories = true, MatchCasing = MatchCasing.CaseInsensitive }));
|
||||
}
|
||||
|
||||
return ReadSyntaxTrees(
|
||||
|
||||
@@ -112,7 +112,7 @@ namespace Semmle.Extraction
|
||||
.Where(s => s is not null)
|
||||
?? Enumerable.Empty<string>();
|
||||
|
||||
var additionalCsFiles = System.IO.Directory.GetFiles(directoryName, "*.cs", SearchOption.AllDirectories);
|
||||
var additionalCsFiles = System.IO.Directory.GetFiles(directoryName, "*.cs", new EnumerationOptions { RecurseSubdirectories = true, MatchCasing = MatchCasing.CaseInsensitive });
|
||||
|
||||
var projectReferences = root
|
||||
.SelectNodes("/Project/ItemGroup/ProjectReference/@Include", mgr)
|
||||
|
||||
@@ -13,6 +13,8 @@ namespace Semmle.Util
|
||||
{
|
||||
public const string NugetExeUrl = "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe";
|
||||
|
||||
public static readonly char[] NewLineCharacters = ['\r', '\n'];
|
||||
|
||||
public static string ConvertToWindows(string path)
|
||||
{
|
||||
return path.Replace('/', '\\');
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
from create_database_utils import *
|
||||
import os
|
||||
|
||||
os.environ["CODEQL_EXTRACTOR_CSHARP_BUILDLESS_MONO_PATH"] = "/non-existent-path"
|
||||
os.environ["CODEQL_EXTRACTOR_CSHARP_BUILDLESS_DOTNET_FRAMEWORK_REFERENCES"] = "/non-existent-path"
|
||||
run_codeql_database_create([], lang="csharp", extra_args=["--extractor-option=buildless=true"])
|
||||
|
||||
Reference in New Issue
Block a user