internal static int Main(string[] args) { try { // Note that AppContext.BaseDirectory isn't necessarily the directory containing csi.exe. // For example, when executed via corerun it's the directory containing corerun. string csiDirectory = Path.GetDirectoryName(typeof(Csi).GetTypeInfo().Assembly.ManifestModule.FullyQualifiedName); var buildPaths = new BuildPaths( clientDir: csiDirectory, workingDir: Directory.GetCurrentDirectory(), sdkDir: RuntimeMetadataReferenceResolver.GetDesktopFrameworkDirectory(), tempDir: Path.GetTempPath()); var compiler = new CSharpInteractiveCompiler( responseFile: Path.Combine(csiDirectory, InteractiveResponseFileName), buildPaths: buildPaths, args: args, analyzerLoader: new NotImplementedAnalyzerLoader()); var runner = new CommandLineRunner( ConsoleIO.Default, compiler, CSharpScriptCompiler.Instance, CSharpObjectFormatter.Instance); return runner.RunInteractive(); } catch (Exception ex) { Console.Error.WriteLine(ex.ToString()); return 1; } }
internal static int Main(string[] args) { try { var responseFile = Path.Combine(AppContext.BaseDirectory, InteractiveResponseFileName); var buildPaths = new BuildPaths( clientDir: AppContext.BaseDirectory, workingDir: Directory.GetCurrentDirectory(), sdkDir: CorLightup.Desktop.TryGetRuntimeDirectory(), tempDir: Path.GetTempPath()); var compiler = new CSharpInteractiveCompiler( responseFile: responseFile, buildPaths: buildPaths, args: args, analyzerLoader: new NotImplementedAnalyzerLoader()); var runner = new CommandLineRunner( ConsoleIO.Default, compiler, CSharpScriptCompiler.Instance, CSharpObjectFormatter.Instance); return runner.RunInteractive(); } catch (Exception ex) { Console.Error.WriteLine(ex.ToString()); return 1; } }
internal VisualBasicCompilerServer( Func <string, MetadataReferenceProperties, PortableExecutableReference> metadataProvider, string[] args, BuildPaths buildPaths, string?libDirectory, IAnalyzerAssemblyLoader analyzerLoader ) : this( metadataProvider, Path.Combine(buildPaths.ClientDirectory, ResponseFileName), args, buildPaths, libDirectory, analyzerLoader ) { }
internal CSharpInteractiveCompiler( string responseFile, BuildPaths buildPaths, string[] args, IAnalyzerAssemblyLoader analyzerLoader ) // Unlike C# compiler we do not use LIB environment variable. It's only supported for historical reasons. : base( CSharpCommandLineParser.Script, responseFile, args, buildPaths, null, analyzerLoader ) { }
internal VisualBasicCompilerServer( Func <string, MetadataReferenceProperties, PortableExecutableReference> metadataProvider, string?responseFile, string[] args, BuildPaths buildPaths, string?libDirectory, IAnalyzerAssemblyLoader analyzerLoader ) : base( VisualBasicCommandLineParser.Default, responseFile, args, buildPaths, libDirectory, analyzerLoader ) { _metadataProvider = metadataProvider; }
public MockCSharpCompiler( string responseFile, BuildPaths buildPaths, string[] args, ImmutableArray <DiagnosticAnalyzer> analyzers = default, ImmutableArray <ISourceGenerator> generators = default, AnalyzerAssemblyLoader loader = null ) : base( CSharpCommandLineParser.Default, responseFile, args, buildPaths, Environment.GetEnvironmentVariable("LIB"), loader ?? new DefaultAnalyzerAssemblyLoader() ) { _analyzers = analyzers.NullToEmpty(); _generators = generators.NullToEmpty(); }
internal static int Run(IEnumerable <string> arguments, RequestLanguage language, CompileFunc compileFunc, IAnalyzerAssemblyLoader analyzerAssemblyLoader) { var sdkDir = GetSystemSdkDirectory(); if (RuntimeHostInfo.IsCoreClrRuntime) { // Register encodings for console // https://github.com/dotnet/roslyn/issues/10785 System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); } var client = new DesktopBuildClient(language, compileFunc, analyzerAssemblyLoader); var clientDir = AppContext.BaseDirectory; var workingDir = Directory.GetCurrentDirectory(); var tempDir = BuildServerConnection.GetTempPath(workingDir); var buildPaths = new BuildPaths(clientDir: clientDir, workingDir: workingDir, sdkDir: sdkDir, tempDir: tempDir); var originalArguments = GetCommandLineArgs(arguments); return(client.RunCompilation(originalArguments, buildPaths).ExitCode); }
public bool TryCreateCompiler(RunRequest request, out CommonCompiler compiler) { var buildPaths = new BuildPaths(ClientDirectory, request.CurrentDirectory, SdkDirectory, request.TempDirectory); switch (request.Language) { case LanguageNames.Stark: compiler = new CSharpCompilerServer( AssemblyReferenceProvider, args: request.Arguments, buildPaths: buildPaths, libDirectory: request.LibDirectory, analyzerLoader: AnalyzerAssemblyLoader); return(true); default: compiler = null; return(false); } }
internal static int Run( string[] args, BuildPaths buildPaths, TextWriter textWriter, IAnalyzerAssemblyLoader analyzerLoader ) { FatalError.Handler = FailFast.OnFatalException; var responseFile = Path.Combine( buildPaths.ClientDirectory, VisualBasicCompiler.ResponseFileName ); var compiler = new Vbc(responseFile, buildPaths, args, analyzerLoader); return(ConsoleUtil.RunWithUtf8Output( compiler.Arguments.Utf8Output, textWriter, tw => compiler.Run(tw) )); }
private static CommandLineRunner CreateRunner( string[] args = null, string input = "", string responseFile = null, string workingDirectory = null) { var io = new TestConsoleIO(input); var buildPaths = new BuildPaths( clientDir: AppContext.BaseDirectory, workingDir: workingDirectory ?? AppContext.BaseDirectory, sdkDir: null, tempDir: Path.GetTempPath()); var compiler = new CSharpInteractiveCompiler( responseFile, buildPaths, args ?? s_defaultArgs, new NotImplementedAnalyzerLoader()); return new CommandLineRunner(io, compiler, CSharpScriptCompiler.Instance, CSharpObjectFormatter.Instance); }
private static CommandLineRunner CreateRunner( string[] args = null, string input = "", string responseFile = null, string workingDirectory = null) { var io = new TestConsoleIO(input); var buildPaths = new BuildPaths( clientDir: AppContext.BaseDirectory, workingDir: workingDirectory ?? AppContext.BaseDirectory, sdkDir: null, tempDir: Path.GetTempPath()); var compiler = new CSharpInteractiveCompiler( responseFile, buildPaths, args?.Where(a => a != null).ToArray() ?? s_defaultArgs, new NotImplementedAnalyzerLoader()); return(new CommandLineRunner(io, compiler, CSharpScriptCompiler.Instance, CSharpObjectFormatter.Instance)); }
private static CommandLineRunner CreateRunner( string[] args = null, string input = "", string responseFile = null, string workingDirectory = null) { var io = new TestConsoleIO(input); var clientDir = Path.GetDirectoryName(RuntimeUtilities.GetAssemblyLocation(typeof(CommandLineRunnerTests))); var buildPaths = new BuildPaths( clientDir: clientDir, workingDir: workingDirectory ?? clientDir, sdkDir: null, tempDir: Path.GetTempPath()); var compiler = new CSharpInteractiveCompiler( responseFile, buildPaths, args?.Where(a => a != null).ToArray() ?? s_defaultArgs, new NotImplementedAnalyzerLoader()); return(new CommandLineRunner(io, compiler, CSharpScriptCompiler.Instance, CSharpObjectFormatter.Instance)); }
internal static int Main(string[] args) { try { // Note that AppContext.BaseDirectory isn't necessarily the directory containing csi.exe. // For example, when executed via corerun it's the directory containing corerun. string csiDirectory = Path.GetDirectoryName( typeof(Csi).GetTypeInfo().Assembly.ManifestModule.FullyQualifiedName ); var buildPaths = new BuildPaths( clientDir: csiDirectory, workingDir: Directory.GetCurrentDirectory(), sdkDir: RuntimeMetadataReferenceResolver.GetDesktopFrameworkDirectory(), tempDir: Path.GetTempPath() ); var compiler = new CSharpInteractiveCompiler( responseFile: Path.Combine(csiDirectory, InteractiveResponseFileName), buildPaths: buildPaths, args: args, analyzerLoader: new NotImplementedAnalyzerLoader() ); var runner = new CommandLineRunner( ConsoleIO.Default, compiler, CSharpScriptCompiler.Instance, CSharpObjectFormatter.Instance ); return(runner.RunInteractive()); } catch (Exception ex) { Console.Error.WriteLine(ex.ToString()); return(1); } }
protected CSharpCompiler( CSharpCommandLineParser parser, string?responseFile, string[] args, BuildPaths buildPaths, string?additionalReferenceDirectories, IAnalyzerAssemblyLoader assemblyLoader ) : base( parser, responseFile, args, buildPaths, additionalReferenceDirectories, assemblyLoader ) { _diagnosticFormatter = new CommandLineDiagnosticFormatter( buildPaths.WorkingDirectory, Arguments.PrintFullPaths, Arguments.ShouldIncludeErrorEndLocation ); _tempDirectory = buildPaths.TempDirectory; }
internal CSharpCompilerServer(Func <string, MetadataReferenceProperties, PortableExecutableReference> metadataProvider, string[] args, BuildPaths buildPaths, string libDirectory, IAnalyzerAssemblyLoader analyzerLoader) : base(CSharpCommandLineParser.Default, buildPaths.ClientDirectory != null ? Path.Combine(buildPaths.ClientDirectory, ResponseFileName) : null, args, buildPaths, libDirectory, analyzerLoader) { _metadataProvider = metadataProvider; }
public PhpCompiler(CommandLineParser parser, string responseFile, string[] args, BuildPaths buildPaths, string additionalReferenceDirectories, IAnalyzerAssemblyLoader analyzerLoader) : base(parser, responseFile, args, buildPaths, additionalReferenceDirectories, analyzerLoader) { _tempDirectory = buildPaths.TempDirectory; }
internal Csc(string responseFile, BuildPaths buildPaths, string[] args, IAnalyzerAssemblyLoader analyzerLoader) : base(CSharpCommandLineParser.Default, responseFile, args, buildPaths, Environment.GetEnvironmentVariable("LIB"), analyzerLoader) { }
protected override int ExecuteTool(string pathToTool, string responseFileCommands, string commandLineCommands) { if (ProvideCommandLineArgs) { CommandLineArgs = GetArguments(commandLineCommands, responseFileCommands) .Select(arg => new TaskItem(arg)).ToArray(); } if (SkipCompilerExecution) { return(0); } if (!UseSharedCompilation || !string.IsNullOrEmpty(ToolPath)) { return(base.ExecuteTool(pathToTool, responseFileCommands, commandLineCommands)); } using (_sharedCompileCts = new CancellationTokenSource()) { try { CompilerServerLogger.Log($"CommandLine = '{commandLineCommands}'"); CompilerServerLogger.Log($"BuildResponseFile = '{responseFileCommands}'"); var buildPaths = new BuildPaths( clientDir: TryGetClientDir() ?? Path.GetDirectoryName(pathToTool), // MSBuild doesn't need the .NET SDK directory sdkDir: null, workingDir: CurrentDirectoryToUse()); var responseTask = BuildClientShim.RunServerCompilation( Language, GetArguments(commandLineCommands, responseFileCommands).ToList(), buildPaths, keepAlive: null, libEnvVariable: LibDirectoryToUse(), cancellationToken: _sharedCompileCts.Token); responseTask.Wait(_sharedCompileCts.Token); var response = responseTask.Result; if (response != null) { ExitCode = HandleResponse(response, pathToTool, responseFileCommands, commandLineCommands); } else { ExitCode = base.ExecuteTool(pathToTool, responseFileCommands, commandLineCommands); } } catch (OperationCanceledException) { ExitCode = 0; } catch (Exception e) { Log.LogErrorWithCodeFromResources("Compiler_UnexpectedException"); LogErrorOutput(e.ToString()); ExitCode = -1; } } return(ExitCode); }
protected CSharpCompiler(CSharpCommandLineParser parser, string responseFile, string[] args, BuildPaths buildPaths, string additionalReferenceDirectories, IAnalyzerAssemblyLoader assemblyLoader) : base(parser, responseFile, args, buildPaths, additionalReferenceDirectories, assemblyLoader) { _diagnosticFormatter = new CommandLineDiagnosticFormatter(buildPaths.WorkingDirectory, Arguments.PrintFullPaths, Arguments.ShouldIncludeErrorEndLocation); _tempDirectory = buildPaths.TempDirectory; }
protected override RunCompilationResult HandleResponse(BuildResponse response, string[] arguments, BuildPaths buildPaths, TextWriter textWriter) { // Override the base so we don't print the compilation output to Console.Out return(RunCompilationResult.Succeeded); }
internal Vbc(string responseFile, BuildPaths buildPaths, string[] args, IAnalyzerAssemblyLoader analyzerLoader) : base(VisualBasicCommandLineParser.Default, responseFile, args, buildPaths.ClientDirectory, buildPaths.WorkingDirectory, buildPaths.SdkDirectory, Environment.GetEnvironmentVariable("LIB"), analyzerLoader) { }
public ServerTests() { _buildPaths = ServerUtil.CreateBuildPaths( workingDir: Temp.CreateDirectory().Path, tempDir: Temp.CreateDirectory().Path); }
protected override int RunLocalCompilation(string[] arguments, BuildPaths buildPaths, TextWriter textWriter) { return(_compileFunc(arguments, buildPaths, textWriter, _analyzerAssemblyLoader)); }
internal CSharpCompilerServer(Func <string, MetadataReferenceProperties, PortableExecutableReference> metadataProvider, string?responseFile, string[] args, BuildPaths buildPaths, string?libDirectory, IAnalyzerAssemblyLoader analyzerLoader, GeneratorDriverCache driverCache) : base(CSharpCommandLineParser.Default, responseFile, args, buildPaths, libDirectory, analyzerLoader, driverCache) { _metadataProvider = metadataProvider; }
protected override int HandleResponse(BuildResponse response, List <string> arguments, BuildPaths buildPaths) { // Override the base so we don't print the compilation output to Console.Out return(0); }
public PhpCompiler(CommandLineParser parser, string responseFile, string[] args, BuildPaths buildPaths, string additionalReferenceDirectories, IAnalyzerAssemblyLoader analyzerLoader) : base(parser, responseFile, args, buildPaths, additionalReferenceDirectories, analyzerLoader) { _tempDirectory = buildPaths.TempDirectory; _diagnosticFormatter = new CommandLineDiagnosticFormatter(buildPaths.WorkingDirectory, Arguments.PrintFullPaths); }
public ServerTests() { _tempDirectory = Temp.CreateDirectory(); _buildPaths = ServerUtil.CreateBuildPaths(workingDir: _tempDirectory.Path); }
protected override string GetSessionKey(BuildPaths buildPaths) { return(_pipeName); }
internal VisualBasicCompilerServer(Func<string, MetadataReferenceProperties, PortableExecutableReference> metadataProvider, string[] args, BuildPaths buildPaths, string libDirectory, IAnalyzerAssemblyLoader analyzerLoader) : base(VisualBasicCommandLineParser.Default, buildPaths.ClientDirectory != null ? Path.Combine(buildPaths.ClientDirectory, ResponseFileName) : null, args, buildPaths, libDirectory, analyzerLoader) { _metadataProvider = metadataProvider; }
protected override Task <BuildResponse> RunServerCompilation(List <string> arguments, BuildPaths buildPaths, string sessionKey, string keepAlive, string libDirectory, CancellationToken cancellationToken) { if (_runServerCompilationFunc != null) { return(_runServerCompilationFunc()); } return(base.RunServerCompilation(arguments, buildPaths, sessionKey, keepAlive, libDirectory, cancellationToken)); }
public void BuildThenPublishWithAOT(BuildArgs buildArgs, RunHost host, string id) { string projectName = $"build_publish_{buildArgs.Config}"; buildArgs = buildArgs with { ProjectName = projectName }; buildArgs = ExpandBuildArgs(buildArgs, extraProperties: "<_WasmDevel>true</_WasmDevel>"); // no relinking for build bool relinked = false; (_, string output) = BuildProject(buildArgs, id, new BuildProjectOptions( InitProject: () => File.WriteAllText(Path.Combine(_projectDir !, "Program.cs"), s_mainReturns42), DotnetWasmFromRuntimePack: !relinked, CreateProject: true, Publish: false, Label: "first_build")); BuildPaths paths = GetBuildPaths(buildArgs); var pathsDict = GetFilesTable(buildArgs, paths, unchanged: false); string mainDll = $"{buildArgs.ProjectName}.dll"; var firstBuildStat = StatFiles(pathsDict.Select(kvp => kvp.Value.fullPath)); Assert.False(firstBuildStat["pinvoke.o"].Exists); Assert.False(firstBuildStat[$"{mainDll}.bc"].Exists); CheckOutputForNativeBuild(expectAOT: false, expectRelinking: relinked, buildArgs, output); Run(expectAOT: false); if (!_buildContext.TryGetBuildFor(buildArgs, out BuildProduct? product)) { throw new XunitException($"Test bug: could not get the build product in the cache"); } File.Move(product !.LogFile, Path.ChangeExtension(product.LogFile !, ".first.binlog")); _testOutput.WriteLine($"{Environment.NewLine}Publishing with no changes ..{Environment.NewLine}"); // relink by default for Release+publish (_, output) = BuildProject(buildArgs, id: id, new BuildProjectOptions( DotnetWasmFromRuntimePack: false, CreateProject: false, Publish: true, UseCache: false, Label: "first_publish")); var publishStat = StatFiles(pathsDict.Select(kvp => kvp.Value.fullPath)); Assert.True(publishStat["pinvoke.o"].Exists); Assert.True(publishStat[$"{mainDll}.bc"].Exists); CheckOutputForNativeBuild(expectAOT: true, expectRelinking: false, buildArgs, output); CompareStat(firstBuildStat, publishStat, pathsDict.Values); Run(expectAOT: true); // second build (_, output) = BuildProject(buildArgs, id: id, new BuildProjectOptions( InitProject: () => File.WriteAllText(Path.Combine(_projectDir !, "Program.cs"), s_mainReturns42), DotnetWasmFromRuntimePack: !relinked, CreateProject: true, Publish: false, Label: "second_build")); var secondBuildStat = StatFiles(pathsDict.Select(kvp => kvp.Value.fullPath)); // no relinking, or AOT CheckOutputForNativeBuild(expectAOT: false, expectRelinking: false, buildArgs, output); // no native files changed pathsDict.UpdateTo(unchanged: true); CompareStat(publishStat, secondBuildStat, pathsDict.Values); void Run(bool expectAOT) => RunAndTestWasmApp( buildArgs with { AOT = expectAOT }, buildDir: _projectDir, expectedExitCode: 42, host: host, id: id); } void CheckOutputForNativeBuild(bool expectAOT, bool expectRelinking, BuildArgs buildArgs, string buildOutput) { AssertSubstring($"{buildArgs.ProjectName}.dll -> {buildArgs.ProjectName}.dll.bc", buildOutput, expectAOT); AssertSubstring($"{buildArgs.ProjectName}.dll.bc -> {buildArgs.ProjectName}.dll.o", buildOutput, expectAOT); AssertSubstring("pinvoke.c -> pinvoke.o", buildOutput, expectRelinking || expectAOT); } }
/// <summary> /// Given the full path to the directory containing the compiler exes, /// retrieves the name of the pipe for client/server communication on /// that instance of the compiler. /// </summary> protected override string GetSessionKey(BuildPaths buildPaths) { return(BuildServerConnection.GetPipeNameForPathOpt(buildPaths.ClientDirectory)); }
public MockCSharpCompiler(string responseFile, BuildPaths buildPaths, string[] args, ImmutableArray <DiagnosticAnalyzer> analyzers = default, AnalyzerAssemblyLoader loader = null) : base(CSharpCommandLineParser.Default, responseFile, args, buildPaths, Environment.GetEnvironmentVariable("LIB"), loader ?? RuntimeUtilities.CreateAnalyzerAssemblyLoader()) { _analyzers = analyzers.NullToEmpty(); }
protected override int ExecuteTool(string pathToTool, string responseFileCommands, string commandLineCommands) { if (ProvideCommandLineArgs) { CommandLineArgs = GetArguments(commandLineCommands, responseFileCommands) .Select(arg => new TaskItem(arg)).ToArray(); } if (SkipCompilerExecution) { return(0); } if (!UseSharedCompilation || !string.IsNullOrEmpty(ToolPath) || !Utilities.IsCompilerServerSupported) { return(base.ExecuteTool(pathToTool, responseFileCommands, commandLineCommands)); } using (_sharedCompileCts = new CancellationTokenSource()) { try { CompilerServerLogger.Log($"CommandLine = '{commandLineCommands}'"); CompilerServerLogger.Log($"BuildResponseFile = '{responseFileCommands}'"); // Try to get the location of the user-provided build client and server, // which should be located next to the build task. If not, fall back to // "pathToTool", which is the compiler in the MSBuild default bin directory. var clientDir = TryGetClientDir() ?? Path.GetDirectoryName(pathToTool); pathToTool = Path.Combine(clientDir, ToolExe); // Note: we can't change the "tool path" printed to the console when we run // the Csc/Vbc task since MSBuild logs it for us before we get here. Instead, // we'll just print our own message that contains the real client location Log.LogMessage(ErrorString.UsingSharedCompilation, clientDir); var buildPaths = new BuildPaths( clientDir: clientDir, // MSBuild doesn't need the .NET SDK directory sdkDir: null, workingDir: CurrentDirectoryToUse()); var responseTask = DesktopBuildClient.RunServerCompilation( Language, GetArguments(commandLineCommands, responseFileCommands).ToList(), buildPaths, keepAlive: null, libEnvVariable: LibDirectoryToUse(), cancellationToken: _sharedCompileCts.Token); responseTask.Wait(_sharedCompileCts.Token); var response = responseTask.Result; if (response != null) { ExitCode = HandleResponse(response, pathToTool, responseFileCommands, commandLineCommands); } else { Log.LogMessage(ErrorString.SharedCompilationFallback, pathToTool); ExitCode = base.ExecuteTool(pathToTool, responseFileCommands, commandLineCommands); } } catch (OperationCanceledException) { ExitCode = 0; } catch (Exception e) { Log.LogErrorWithCodeFromResources("Compiler_UnexpectedException"); LogErrorOutput(e.ToString()); ExitCode = -1; } } return(ExitCode); }
internal TestableCompiler(CommonCompiler compiler, TestableFileSystem fileSystem, BuildPaths buildPaths) { if (!object.ReferenceEquals(compiler.FileSystem, fileSystem)) { throw new ArgumentException(null, nameof(fileSystem)); } Compiler = compiler; FileSystem = fileSystem; BuildPaths = buildPaths; }