using Semmle.Util.Logging; using System; using System.Collections.Generic; using System.IO; using System.Linq; namespace Semmle.Autobuild.Shared { /// /// A build rule analyses the files in "builder" and outputs a build script. /// public interface IBuildRule { /// /// Analyse the files and produce a build script. /// /// The files and options relating to the build. /// Whether this build rule is being automatically applied. BuildScript Analyse(Autobuilder builder, bool auto); } /// /// A delegate used to wrap a build script in an environment where an appropriate /// version of .NET Core is automatically installed. /// public delegate BuildScript WithDotNet(Autobuilder builder, Func?, BuildScript> f); /// /// Exception indicating that environment variables are missing or invalid. /// public class InvalidEnvironmentException : Exception { public InvalidEnvironmentException(string m) : base(m) { } } /// /// Main application logic, containing all data /// gathered from the project and filesystem. /// /// The overall design is intended to be extensible so that in theory, /// it should be possible to add new build rules without touching this code. /// public abstract class Autobuilder { /// /// Full file paths of files found in the project directory, as well as /// their distance from the project root folder. The list is sorted /// by distance in ascending order. /// public IEnumerable<(string, int)> Paths => pathsLazy.Value; private readonly Lazy> pathsLazy; /// /// Gets a list of paths matching a set of extensions (including the "."), /// as well as their distance from the project root folder. /// The list is sorted by distance in ascending order. /// /// The extensions to find. /// The files matching the extension. public IEnumerable<(string, int)> GetExtensions(params string[] extensions) => Paths.Where(p => extensions.Contains(Path.GetExtension(p.Item1))); /// /// Gets all paths matching a particular filename, as well as /// their distance from the project root folder. The list is sorted /// by distance in ascending order. /// /// The filename to find. /// Possibly empty sequence of paths with the given filename. public IEnumerable<(string, int)> GetFilename(string name) => Paths.Where(p => Actions.GetFileName(p.Item1) == name); /// /// Holds if a given path, relative to the root of the source directory /// was found. /// /// The relative path. /// True iff the path was found. public bool HasRelativePath(string path) => HasPath(Actions.PathCombine(RootDirectory, path)); /// /// List of project/solution files to build. /// public IList ProjectsOrSolutionsToBuild => projectsOrSolutionsToBuildLazy.Value; private readonly Lazy> projectsOrSolutionsToBuildLazy; /// /// Holds if a given path was found. /// /// The path of the file. /// True iff the path was found. public bool HasPath(string path) => Paths.Any(p => path == p.Item1); private void FindFiles(string dir, int depth, int maxDepth, IList<(string, int)> results) { foreach (var f in Actions.EnumerateFiles(dir)) { results.Add((f, depth)); } if (depth < maxDepth) { foreach (var d in Actions.EnumerateDirectories(dir)) { FindFiles(d, depth + 1, maxDepth, results); } } } /// /// The root of the source directory. /// private string RootDirectory => Options.RootDirectory; /// /// Gets the supplied build configuration. /// public AutobuildOptions Options { get; } /// /// The set of build actions used during the autobuilder. /// Could be real system operations, or a stub for testing. /// public IBuildActions Actions { get; } private IEnumerable? FindFiles(string extension, Func create) { var matchingFiles = GetExtensions(extension) .Select(p => (ProjectOrSolution: create(p.Item1), DistanceFromRoot: p.Item2)) .Where(p => p.ProjectOrSolution.HasLanguage(this.Options.Language)) .ToArray(); if (matchingFiles.Length == 0) return null; if (Options.AllSolutions) return matchingFiles.Select(p => p.ProjectOrSolution); return matchingFiles .Where(f => f.DistanceFromRoot == matchingFiles[0].DistanceFromRoot) .Select(f => f.ProjectOrSolution); } /// /// Find all the relevant files and picks the best /// solution file and tools. /// /// The command line options. protected Autobuilder(IBuildActions actions, AutobuildOptions options) { Actions = actions; Options = options; pathsLazy = new Lazy>(() => { var files = new List<(string, int)>(); FindFiles(options.RootDirectory, 0, options.SearchDepth, files); return files.OrderBy(f => f.Item2).ToArray(); }); projectsOrSolutionsToBuildLazy = new Lazy>(() => { List? ret; if (options.Solution.Any()) { ret = new List(); foreach (var solution in options.Solution) { if (actions.FileExists(solution)) ret.Add(new Solution(this, solution, true)); else Log(Severity.Error, $"The specified project or solution file {solution} was not found"); } return ret; } // First look for `.proj` files ret = FindFiles(".proj", f => new Project(this, f))?.ToList(); if (ret is not null) return ret; // Then look for `.sln` files ret = FindFiles(".sln", f => new Solution(this, f, false))?.ToList(); if (ret is not null) return ret; // Finally look for language specific project files, e.g. `.csproj` files ret = FindFiles(this.Options.Language.ProjectExtension, f => new Project(this, f))?.ToList(); return ret ?? new List(); }); CodeQLExtractorLangRoot = Actions.GetEnvironmentVariable($"CODEQL_EXTRACTOR_{this.Options.Language.UpperCaseName}_ROOT"); CodeQlPlatform = Actions.GetEnvironmentVariable("CODEQL_PLATFORM"); TrapDir = Actions.GetEnvironmentVariable($"CODEQL_EXTRACTOR_{this.Options.Language.UpperCaseName}_TRAP_DIR") ?? throw new InvalidEnvironmentException($"The environment variable CODEQL_EXTRACTOR_{this.Options.Language.UpperCaseName}_TRAP_DIR has not been set."); SourceArchiveDir = Actions.GetEnvironmentVariable($"CODEQL_EXTRACTOR_{this.Options.Language.UpperCaseName}_SOURCE_ARCHIVE_DIR") ?? throw new InvalidEnvironmentException($"The environment variable CODEQL_EXTRACTOR_{this.Options.Language.UpperCaseName}_SOURCE_ARCHIVE_DIR has not been set."); } protected string TrapDir { get; } protected string SourceArchiveDir { get; } private readonly ILogger logger = new ConsoleLogger(Verbosity.Info); /// /// Log a given build event to the console. /// /// The format string. /// Inserts to the format string. public void Log(Severity severity, string format, params object[] args) { logger.Log(severity, format, args); } /// /// Attempt to build this project. /// /// The exit code, 0 for success and non-zero for failures. public int AttemptBuild() { Log(Severity.Info, $"Working directory: {Options.RootDirectory}"); var script = GetBuildScript(); if (Options.IgnoreErrors) script |= BuildScript.Success; void startCallback(string s, bool silent) { Log(silent ? Severity.Debug : Severity.Info, $"\nRunning {s}"); } void exitCallback(int ret, string msg, bool silent) { Log(silent ? Severity.Debug : Severity.Info, $"Exit code {ret}{(string.IsNullOrEmpty(msg) ? "" : $": {msg}")}"); } return script.Run(Actions, startCallback, exitCallback); } /// /// Returns the build script to use for this project. /// public abstract BuildScript GetBuildScript(); protected BuildScript AutobuildFailure() => BuildScript.Create(actions => { Log(Severity.Error, "Could not auto-detect a suitable build method"); return 1; }); /// /// Value of CODEQL_EXTRACTOR__ROOT environment variable. /// public string? CodeQLExtractorLangRoot { get; } /// /// Value of CODEQL_PLATFORM environment variable. /// public string? CodeQlPlatform { get; } } }