private static CSharpCompilationOptions GetCompilationOptions(CommonCompilerOptions compilerOptions, string projectDirectory) { var outputKind = compilerOptions.EmitEntryPoint.GetValueOrDefault() ? OutputKind.ConsoleApplication : OutputKind.DynamicallyLinkedLibrary; var options = new CSharpCompilationOptions(outputKind); string platformValue = compilerOptions.Platform; bool allowUnsafe = compilerOptions.AllowUnsafe ?? false; bool optimize = compilerOptions.Optimize ?? false; bool warningsAsErrors = compilerOptions.WarningsAsErrors ?? false; Platform platform; if (!Enum.TryParse(value: platformValue, ignoreCase: true, result: out platform)) { platform = Platform.AnyCpu; } options = options .WithAllowUnsafe(allowUnsafe) .WithPlatform(platform) .WithGeneralDiagnosticOption(warningsAsErrors ? ReportDiagnostic.Error : ReportDiagnostic.Default) .WithOptimizationLevel(optimize ? OptimizationLevel.Release : OptimizationLevel.Debug); return AddSigningOptions(options, compilerOptions, projectDirectory); }
private static CSharpCompilationOptions AddSigningOptions(CSharpCompilationOptions options, CommonCompilerOptions compilerOptions, string projectDirectory) { var useOssSigning = compilerOptions.UseOssSigning == true; var keyFile = compilerOptions.KeyFile; if (!string.IsNullOrEmpty(keyFile)) { keyFile = Path.GetFullPath(Path.Combine(projectDirectory, compilerOptions.KeyFile)); if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) || useOssSigning) { return options.WithCryptoPublicKey( SnkUtils.ExtractPublicKey(File.ReadAllBytes(keyFile))); } options = options.WithCryptoKeyFile(keyFile); return options.WithDelaySign(compilerOptions.DelaySign); } return options; }
public static CommonCompilerOptions Combine(params CommonCompilerOptions[] options) { var result = new CommonCompilerOptions(); foreach (var option in options) { // Skip null options if (option == null) { continue; } // Defines are always combined if (option.Defines != null) { var existing = result.Defines ?? Enumerable.Empty<string>(); result.Defines = existing.Concat(option.Defines).Distinct(); } if (option.LanguageVersion != null) { result.LanguageVersion = option.LanguageVersion; } if (option.Platform != null) { result.Platform = option.Platform; } if (option.AllowUnsafe != null) { result.AllowUnsafe = option.AllowUnsafe; } if (option.WarningsAsErrors != null) { result.WarningsAsErrors = option.WarningsAsErrors; } if (option.Optimize != null) { result.Optimize = option.Optimize; } if (option.KeyFile != null) { result.KeyFile = option.KeyFile; } if (option.DelaySign != null) { result.DelaySign = option.DelaySign; } if (option.PublicSign != null) { result.PublicSign = option.PublicSign; } if (option.EmitEntryPoint != null) { result.EmitEntryPoint = option.EmitEntryPoint; } } return result; }
private static CompilationSettings ToCompilationSettings(CommonCompilerOptions compilerOptions, NuGetFramework targetFramework, string projectDirectory) { var options = GetCompilationOptions(compilerOptions, projectDirectory); // Disable 1702 until roslyn turns this off by default options = options.WithSpecificDiagnosticOptions(new Dictionary<string, ReportDiagnostic> { { "CS1701", ReportDiagnostic.Suppress }, // Binding redirects { "CS1702", ReportDiagnostic.Suppress }, { "CS1705", ReportDiagnostic.Suppress } }); AssemblyIdentityComparer assemblyIdentityComparer = targetFramework.IsDesktop() ? DesktopAssemblyIdentityComparer.Default : null; options = options.WithAssemblyIdentityComparer(assemblyIdentityComparer); LanguageVersion languageVersion; if (!Enum.TryParse<LanguageVersion>(value: compilerOptions.LanguageVersion, ignoreCase: true, result: out languageVersion)) { languageVersion = LanguageVersion.CSharp6; } var settings = new CompilationSettings { LanguageVersion = languageVersion, Defines = compilerOptions.Defines ?? Enumerable.Empty<string>(), CompilationOptions = options }; return settings; }
private static IEnumerable<string> TranslateCommonOptions(CommonCompilerOptions options) { List<string> commonArgs = new List<string>(); if (options.Defines != null) { commonArgs.AddRange(options.Defines.Select(def => $"-d:{def}")); } if (options.LanguageVersion != null) { commonArgs.Add($"-langversion:{GetLanguageVersion(options.LanguageVersion)}"); } if (options.Platform != null) { commonArgs.Add($"-platform:{options.Platform}"); } if (options.AllowUnsafe == true) { commonArgs.Add("-unsafe"); } if (options.WarningsAsErrors == true) { commonArgs.Add("-warnaserror"); } if (options.Optimize == true) { commonArgs.Add("-optimize"); } if (options.KeyFile != null) { commonArgs.Add($"-keyfile:\"{options.KeyFile}\""); } if (options.DelaySign == true) { commonArgs.Add("-delaysign"); } // TODO: What is this? What does it mean to sign without a key? // Is this "OSS" signing? // if (options.StrongName) if (options.EmitEntryPoint != true) { commonArgs.Add("-t:library"); } return commonArgs; }
private static CSharpCompilationOptions AddSigningOptions(CSharpCompilationOptions options, CommonCompilerOptions compilerOptions, string projectDirectory) { var useOssSigning = compilerOptions.PublicSign == true; var keyFile = compilerOptions.KeyFile; if (!string.IsNullOrEmpty(keyFile)) { keyFile = Path.GetFullPath(Path.Combine(projectDirectory, compilerOptions.KeyFile)); if (Environment.OSVersion.Platform != PlatformID.Win32NT || useOssSigning) { return options.WithCryptoPublicKey( SnkUtils.ExtractPublicKey(File.ReadAllBytes(keyFile))); } options = options.WithCryptoKeyFile(keyFile); return options.WithDelaySign(compilerOptions.DelaySign); } return options; }
private static IEnumerable<string> TranslateCommonOptions(CommonCompilerOptions options) { List<string> commonArgs = new List<string>(); if (options.Defines != null) { commonArgs.AddRange(options.Defines.Select(def => $"-d:{def}")); } if (options.LanguageVersion != null) { commonArgs.Add($"-langversion:{GetLanguageVersion(options.LanguageVersion)}"); } if (options.Platform != null) { commonArgs.Add($"-platform:{options.Platform}"); } if (options.AllowUnsafe == true) { commonArgs.Add("-unsafe"); } if (options.WarningsAsErrors == true) { commonArgs.Add("-warnaserror"); } if (options.Optimize == true) { commonArgs.Add("-optimize"); } if (options.KeyFile != null) { commonArgs.Add($"-keyfile:\"{options.KeyFile}\""); // If we're not on Windows, full signing isn't supported, so we'll // public sign, unless the public sign switch has explicitly been // set to false if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && options.PublicSign == null) { commonArgs.Add("-publicsign"); } } if (options.DelaySign == true) { commonArgs.Add("-delaysign"); } if (options.PublicSign == true) { commonArgs.Add("-publicsign"); } if (options.EmitEntryPoint != true) { commonArgs.Add("-t:library"); } return commonArgs; }