Beispiel #1
0
        private static BuildScript GetInstalledSdksScript(IBuildActions actions)
        {
            var listSdks = new CommandBuilder(actions, silent: true).
                           RunCommand("dotnet").
                           Argument("--list-sdks");

            return(listSdks.Script);
        }
Beispiel #2
0
        CommandBuilder GetRestoreCommand(IBuildActions actions, string dotNetPath, IDictionary <string, string> environment)
        {
            var restore = new CommandBuilder(actions, null, environment).
                          RunCommand(DotNetCommand(actions, dotNetPath)).
                          Argument("restore");

            return(restore);
        }
Beispiel #3
0
        static BuildScript GetInstalledRuntimesScript(IBuildActions actions, string dotNetPath, IDictionary <string, string> environment)
        {
            var listSdks = new CommandBuilder(actions, environment: environment, silent: true).
                           RunCommand(DotNetCommand(actions, dotNetPath)).
                           Argument("--list-runtimes");

            return(listSdks.Script);
        }
Beispiel #4
0
        BuildScript GetInfoCommand(IBuildActions actions, string dotNetPath, IDictionary <string, string> environment)
        {
            var info = new CommandBuilder(actions, null, environment).
                       RunCommand(DotNetCommand(actions, dotNetPath)).
                       Argument("--info");

            return(info.Script);
        }
Beispiel #5
0
        CommandBuilder GetCleanCommand(IBuildActions actions, string dotNetPath, IDictionary <string, string> environment)
        {
            var clean = new CommandBuilder(actions, null, environment).
                        RunCommand(DotNetCommand(actions, dotNetPath)).
                        Argument("clean");

            return(clean);
        }
        public static IEnumerable <VcVarsBatFile> GetCandidateVcVarsFiles(IBuildActions actions)
        {
            var programFilesx86 = actions.GetEnvironmentVariable("ProgramFiles(x86)");

            if (programFilesx86 is null)
            {
                yield break;
            }

            // Attempt to use vswhere to find installations of Visual Studio
            var vswhere = actions.PathCombine(programFilesx86, "Microsoft Visual Studio", "Installer", "vswhere.exe");

            if (actions.FileExists(vswhere))
            {
                var exitCode1 = actions.RunProcess(vswhere, "-prerelease -legacy -property installationPath", null, null, out var installationList);
                var exitCode2 = actions.RunProcess(vswhere, "-prerelease -legacy -property installationVersion", null, null, out var versionList);

                if (exitCode1 == 0 && exitCode2 == 0 && versionList.Count == installationList.Count)
                {
                    // vswhere ran successfully and produced the expected output
                    foreach (var vsInstallation in versionList.Zip(installationList, (v, i) => (Version: v, InstallationPath: i)))
                    {
                        var dot = vsInstallation.Version.IndexOf('.');
                        var majorVersionString = dot == -1 ? vsInstallation.Version : vsInstallation.Version.Substring(0, dot);
                        if (int.TryParse(majorVersionString, out var majorVersion))
                        {
                            if (majorVersion < 15)
                            {
                                // Visual Studio 2015 and below
                                yield return(new VcVarsBatFile(actions.PathCombine(vsInstallation.InstallationPath, @"VC\vcvarsall.bat"), majorVersion));
                            }
                            else
                            {
                                // Visual Studio 2017 and above
                                yield return(new VcVarsBatFile(actions.PathCombine(vsInstallation.InstallationPath, @"VC\Auxiliary\Build\vcvars32.bat"), majorVersion));

                                yield return(new VcVarsBatFile(actions.PathCombine(vsInstallation.InstallationPath, @"VC\Auxiliary\Build\vcvars64.bat"), majorVersion));

                                yield return(new VcVarsBatFile(actions.PathCombine(vsInstallation.InstallationPath, @"Common7\Tools\VsDevCmd.bat"), majorVersion));
                            }
                        }
                        // else: Skip installation without a version
                    }
                    yield break;
                }
            }

            // vswhere not installed or didn't run correctly - return legacy Visual Studio versions
            yield return(new VcVarsBatFile(actions.PathCombine(programFilesx86, @"Microsoft Visual Studio 14.0\VC\vcvarsall.bat"), 14));

            yield return(new VcVarsBatFile(actions.PathCombine(programFilesx86, @"Microsoft Visual Studio 12.0\VC\vcvarsall.bat"), 12));

            yield return(new VcVarsBatFile(actions.PathCombine(programFilesx86, @"Microsoft Visual Studio 11.0\VC\vcvarsall.bat"), 11));

            yield return(new VcVarsBatFile(actions.PathCombine(programFilesx86, @"Microsoft Visual Studio 10.0\VC\vcvarsall.bat"), 10));
        }
Beispiel #7
0
            public override int Run(IBuildActions actions, Action <string, bool> startCallback, Action <int, string, bool> exitCallBack, out IList <string> stdout)
            {
                var ret1 = s1.Run(actions, startCallback, exitCallBack, out var stdout1);
                var ret2 = (s2a != null ? s2a(stdout1, ret1) : s2b !(ret1)).Run(actions, startCallback, exitCallBack, out var stdout2);
                var @out = new List <string>();

                @out.AddRange(stdout1);
                @out.AddRange(stdout2);
                stdout = @out;
                return(ret2);
            }
Beispiel #8
0
        public static string[] AsListWithExpandedEnvVars(this string?value, IBuildActions actions, string[] defaultValue)
        {
            if (value == null)
            {
                return(defaultValue);
            }

            return(value.
                   Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).
                   Select(s => AsStringWithExpandedEnvVars(s, actions)).ToArray());
        }
Beispiel #9
0
            public override int Run(IBuildActions actions, Action <string> startCallback, Action <int, string> exitCallBack)
            {
                int ret1;

                if (s2a != null)
                {
                    ret1 = s1.Run(actions, startCallback, exitCallBack, out var stdout1);
                    return(s2a(stdout1, ret1).Run(actions, startCallback, exitCallBack));
                }

                ret1 = s1.Run(actions, startCallback, exitCallBack);
                return(s2b(ret1).Run(actions, startCallback, exitCallBack));
            }
Beispiel #10
0
        public static string AsStringWithExpandedEnvVars(this string value, IBuildActions actions)
        {
            if (string.IsNullOrEmpty(value))
            {
                return(value);
            }

            // `Environment.ExpandEnvironmentVariables` only works with Windows-style
            // environment variables
            var windowsStyle = actions.IsWindows()
                ? value
                : linuxEnvRegEx.Replace(value, m => $"%{m.Groups[1].Value}%");

            return(actions.EnvironmentExpandEnvironmentVariables(windowsStyle));
        }
Beispiel #11
0
            public override int Run(IBuildActions actions, Action <string, bool> startCallback, Action <int, string, bool> exitCallBack)
            {
                int ret1;

                if (s2a != null)
                {
                    ret1 = s1.Run(actions, startCallback, exitCallBack, out var stdout1);
                    return(s2a(stdout1, ret1).Run(actions, startCallback, exitCallBack));
                }

                if (s2b != null)
                {
                    ret1 = s1.Run(actions, startCallback, exitCallBack);
                    return(s2b(ret1).Run(actions, startCallback, exitCallBack));
                }

                throw new InvalidOperationException("Unexpected error");
            }
Beispiel #12
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:Semmle.Autobuild.CommandBuilder"/> class.
        /// </summary>
        /// <param name="workingDirectory">The working directory (<code>null</code> for current directory).</param>
        /// <param name="environment">Additional environment variables.</param>
        public CommandBuilder(IBuildActions actions, string workingDirectory = null, IDictionary <string, string> environment = null)
        {
            arguments = new StringBuilder();
            if (actions.IsWindows())
            {
                executable = "cmd.exe";
                arguments.Append("/C");
                escapingMode = EscapeMode.Cmd;
            }
            else
            {
                escapingMode = EscapeMode.Process;
            }

            firstCommand          = true;
            this.workingDirectory = workingDirectory;
            this.environment      = environment;
        }
Beispiel #13
0
            public override int Run(IBuildActions actions, Action <string, bool> startCallback, Action <int, string, bool> exitCallBack, out IList <string> stdout)
            {
                startCallback(this.ToString(), silent);
                var ret        = 1;
                var retMessage = "";

                try
                {
                    ret = actions.RunProcess(exe, arguments, workingDirectory, environment, out stdout);
                }
                catch (Exception ex)
                    when(ex is System.ComponentModel.Win32Exception || ex is FileNotFoundException)
                    {
                        retMessage = ex.Message;
                        stdout     = Array.Empty <string>();
                    }
                exitCallBack(ret, retMessage, silent);
                return(ret);
            }
Beispiel #14
0
        /// <summary>
        /// Reads options from environment variables.
        /// Throws ArgumentOutOfRangeException for invalid arguments.
        /// </summary>
        public void ReadEnvironment(IBuildActions actions)
        {
            RootDirectory        = actions.GetCurrentDirectory();
            VsToolsVersion       = actions.GetEnvironmentVariable(prefix + "VSTOOLS_VERSION");
            MsBuildArguments     = actions.GetEnvironmentVariable(prefix + "MSBUILD_ARGUMENTS");
            MsBuildPlatform      = actions.GetEnvironmentVariable(prefix + "MSBUILD_PLATFORM");
            MsBuildConfiguration = actions.GetEnvironmentVariable(prefix + "MSBUILD_CONFIGURATION");
            MsBuildTarget        = actions.GetEnvironmentVariable(prefix + "MSBUILD_TARGET");
            DotNetArguments      = actions.GetEnvironmentVariable(prefix + "DOTNET_ARGUMENTS");
            DotNetVersion        = actions.GetEnvironmentVariable(prefix + "DOTNET_VERSION");
            BuildCommand         = actions.GetEnvironmentVariable(prefix + "BUILD_COMMAND");
            Solution             = actions.GetEnvironmentVariable(prefix + "SOLUTION").AsList(new string[0]);

            IgnoreErrors = actions.GetEnvironmentVariable(prefix + "IGNORE_ERRORS").AsBool("ignore_errors", false);
            Buildless    = actions.GetEnvironmentVariable(prefix + "BUILDLESS").AsBool("buildless", false);
            AllSolutions = actions.GetEnvironmentVariable(prefix + "ALL_SOLUTIONS").AsBool("all_solutions", false);
            NugetRestore = actions.GetEnvironmentVariable(prefix + "NUGET_RESTORE").AsBool("nuget_restore", true);

            Language = actions.GetEnvironmentVariable("LGTM_PROJECT_LANGUAGE").AsLanguage();
        }
Beispiel #15
0
        /// <summary>
        /// Reads options from environment variables.
        /// Throws ArgumentOutOfRangeException for invalid arguments.
        /// </summary>
        public AutobuildOptions(IBuildActions actions, Language language)
        {
            RootDirectory        = actions.GetCurrentDirectory();
            VsToolsVersion       = actions.GetEnvironmentVariable(prefix + "VSTOOLS_VERSION");
            MsBuildArguments     = actions.GetEnvironmentVariable(prefix + "MSBUILD_ARGUMENTS")?.AsStringWithExpandedEnvVars(actions);
            MsBuildPlatform      = actions.GetEnvironmentVariable(prefix + "MSBUILD_PLATFORM");
            MsBuildConfiguration = actions.GetEnvironmentVariable(prefix + "MSBUILD_CONFIGURATION");
            MsBuildTarget        = actions.GetEnvironmentVariable(prefix + "MSBUILD_TARGET");
            DotNetArguments      = actions.GetEnvironmentVariable(prefix + "DOTNET_ARGUMENTS")?.AsStringWithExpandedEnvVars(actions);
            DotNetVersion        = actions.GetEnvironmentVariable(prefix + "DOTNET_VERSION");
            BuildCommand         = actions.GetEnvironmentVariable(prefix + "BUILD_COMMAND");
            Solution             = actions.GetEnvironmentVariable(prefix + "SOLUTION").AsListWithExpandedEnvVars(actions, Array.Empty <string>());

            IgnoreErrors = actions.GetEnvironmentVariable(prefix + "IGNORE_ERRORS").AsBool("ignore_errors", false);
            Buildless    = actions.GetEnvironmentVariable(prefix + "BUILDLESS").AsBool("buildless", false);
            AllSolutions = actions.GetEnvironmentVariable(prefix + "ALL_SOLUTIONS").AsBool("all_solutions", false);
            NugetRestore = actions.GetEnvironmentVariable(prefix + "NUGET_RESTORE").AsBool("nuget_restore", true);

            Language = language;
        }
        /// <summary>
        /// Reads options from environment variables.
        /// Throws ArgumentOutOfRangeException for invalid arguments.
        /// </summary>
        public AutobuildOptions(IBuildActions actions)
        {
            RootDirectory        = actions.GetCurrentDirectory();
            VsToolsVersion       = actions.GetEnvironmentVariable(prefix + "VSTOOLS_VERSION");
            MsBuildArguments     = actions.GetEnvironmentVariable(prefix + "MSBUILD_ARGUMENTS")?.AsStringWithExpandedEnvVars(actions);
            MsBuildPlatform      = actions.GetEnvironmentVariable(prefix + "MSBUILD_PLATFORM");
            MsBuildConfiguration = actions.GetEnvironmentVariable(prefix + "MSBUILD_CONFIGURATION");
            MsBuildTarget        = actions.GetEnvironmentVariable(prefix + "MSBUILD_TARGET");
            DotNetArguments      = actions.GetEnvironmentVariable(prefix + "DOTNET_ARGUMENTS")?.AsStringWithExpandedEnvVars(actions);
            DotNetVersion        = actions.GetEnvironmentVariable(prefix + "DOTNET_VERSION");
            BuildCommand         = actions.GetEnvironmentVariable(prefix + "BUILD_COMMAND");
            Solution             = actions.GetEnvironmentVariable(prefix + "SOLUTION").AsListWithExpandedEnvVars(actions, new string[0]);

            IgnoreErrors = actions.GetEnvironmentVariable(prefix + "IGNORE_ERRORS").AsBool("ignore_errors", false);
            Buildless    = actions.GetEnvironmentVariable(prefix + "BUILDLESS").AsBool("buildless", false);
            AllSolutions = actions.GetEnvironmentVariable(prefix + "ALL_SOLUTIONS").AsBool("all_solutions", false);
            NugetRestore = actions.GetEnvironmentVariable(prefix + "NUGET_RESTORE").AsBool("nuget_restore", true);

            Language = actions.GetEnvironmentVariable("LGTM_PROJECT_LANGUAGE").AsLanguage();
            Indexing = !actions.GetEnvironmentVariable("CODEQL_AUTOBUILDER_CSHARP_NO_INDEXING").AsBool("no_indexing", false);
        }
 /// <summary>
 /// Finds a VcVars file that provides a compatible environment for the given solution.
 /// </summary>
 /// <param name="sln">The solution file.</param>
 /// <returns>A compatible file, or throws an exception.</returns>
 public static VcVarsBatFile FindCompatibleVcVars(IBuildActions actions, ISolution sln) =>
 FindCompatibleVcVars(actions, sln.ToolsVersion.Major);
Beispiel #18
0
        /// <summary>
        /// Find all the relevant files and picks the best
        /// solution file and tools.
        /// </summary>
        /// <param name="options">The command line options.</param>
        public Autobuilder(IBuildActions actions, AutobuildOptions options)
        {
            Actions = actions;
            Options = options;

            pathsLazy = new Lazy <IEnumerable <string> >(() =>
            {
                var files = new List <string>();
                FindFiles(options.RootDirectory, options.SearchDepth, files);
                return(files.
                       OrderBy(s => s.Count(c => c == Path.DirectorySeparatorChar)).
                       ThenBy(s => Path.GetFileName(s).Length).
                       ToArray());
            });

            solutionsToBuildLazy = new Lazy <IList <ISolution> >(() =>
            {
                if (options.Solution.Any())
                {
                    var ret = new List <ISolution>();
                    foreach (var solution in options.Solution)
                    {
                        if (actions.FileExists(solution))
                        {
                            ret.Add(new Solution(this, solution));
                        }
                        else
                        {
                            Log(Severity.Error, "The specified solution file {0} was not found", solution);
                        }
                    }
                    return(ret);
                }

                var solutions = GetExtensions(".sln").
                                Select(s => new Solution(this, s)).
                                Where(s => s.ProjectCount > 0).
                                OrderByDescending(s => s.ProjectCount).
                                ThenBy(s => s.Path.Length).
                                ToArray();

                foreach (var sln in solutions)
                {
                    Log(Severity.Info, $"Found {sln.Path} with {sln.ProjectCount} {this.Options.Language} projects, version {sln.ToolsVersion}, config {string.Join(" ", sln.Configurations.Select(c => c.FullName))}");
                }

                return(new List <ISolution>(options.AllSolutions ?
                                            solutions :
                                            solutions.Take(1)));
            });

            SemmleDist = Actions.GetEnvironmentVariable("SEMMLE_DIST");

            SemmleJavaHome = Actions.GetEnvironmentVariable("SEMMLE_JAVA_HOME");

            SemmlePlatformTools = Actions.GetEnvironmentVariable("SEMMLE_PLATFORM_TOOLS");

            if (SemmleDist == null)
            {
                Log(Severity.Error, "The environment variable SEMMLE_DIST has not been set.");
            }
        }
Beispiel #19
0
 static string DotNetCommand(IBuildActions actions, string dotNetPath) =>
 dotNetPath != null?actions.PathCombine(dotNetPath, "dotnet") : "dotnet";
Beispiel #20
0
 private static string DotNetCommand(IBuildActions actions, string?dotNetPath) =>
 dotNetPath is not null?actions.PathCombine(dotNetPath, "dotnet") : "dotnet";
Beispiel #21
0
 public override int Run(IBuildActions actions, Action <string> startCallback, Action <int, string> exitCallBack, out IList <string> stdout)
 {
     stdout = new string[0];
     return(func(actions));
 }
Beispiel #22
0
 /// <summary>
 /// Run this build script.
 /// </summary>
 /// <param name="actions">
 /// The interface used to implement the build actions.
 /// </param>
 /// <param name="startCallback">
 /// A call back that is called every time a new process is started. The
 /// argument to the call back is a textual representation of the process.
 /// </param>
 /// <param name="exitCallBack">
 /// A call back that is called every time a new process exits. The first
 /// argument to the call back is the exit code, and the second argument is
 /// an exit message.
 /// </param>
 /// <returns>The exit code from this build script.</returns>
 public abstract int Run(IBuildActions actions, Action <string> startCallback, Action <int, string> exitCallBack);
Beispiel #23
0
 public CppAutobuilder(IBuildActions actions, AutobuildOptions options) : base(actions, options)
 {
 }
Beispiel #24
0
 /// <summary>
 /// Run this build command.
 /// </summary>
 /// <param name="actions">
 /// The interface used to implement the build actions.
 /// </param>
 /// <param name="startCallback">
 /// A call back that is called every time a new process is started. The
 /// argument to the call back is a textual representation of the process.
 /// </param>
 /// <param name="exitCallBack">
 /// A call back that is called every time a new process exits. The first
 /// argument to the call back is the exit code, and the second argument is
 /// an exit message.
 /// </param>
 /// <param name="stdout">Contents of standard out.</param>
 /// <returns>The exit code from this build script.</returns>
 public abstract int Run(IBuildActions actions, Action <string, bool> startCallback, Action <int, string, bool> exitCallBack, out IList <string> stdout);
 /// <summary>
 /// Finds a VcVars that provides a compatible environment for the given tools version.
 /// </summary>
 /// <param name="targetVersion">The tools version.</param>
 /// <returns>A compatible file, or null.</returns>
 public static VcVarsBatFile FindCompatibleVcVars(IBuildActions actions, int targetVersion) =>
 targetVersion < 10 ?
 VcVarsAllBatFiles(actions).OrderByDescending(b => b.ToolsVersion).FirstOrDefault() :
 VcVarsAllBatFiles(actions).Where(b => b.ToolsVersion >= targetVersion).OrderBy(b => b.ToolsVersion).FirstOrDefault();
 /// <summary>
 /// Enumerates all available tools.
 /// </summary>
 public static IEnumerable <VcVarsBatFile> VcVarsAllBatFiles(IBuildActions actions) =>
 GetCandidateVcVarsFiles(actions).Where(v => actions.FileExists(v.Path));
Beispiel #27
0
 public override int Run(IBuildActions actions, Action <string, bool> startCallback, Action <int, string, bool> exitCallBack, out IList <string> stdout)
 {
     stdout = Array.Empty <string>();
     return(func(actions));
 }
Beispiel #28
0
 public override int Run(IBuildActions actions, Action <string, bool> startCallback, Action <int, string, bool> exitCallBack) => func(actions);