public static bool UseDotNetCore(ScriptAssembly scriptAssembly) { var metroCompilationOverrides = PlayerSettings.WSA.compilationOverrides; bool dotNetCoreEnabled = scriptAssembly.BuildTarget == BuildTarget.WSAPlayer && metroCompilationOverrides != PlayerSettings.WSACompilationOverrides.None; bool useDotNetCore = dotNetCoreEnabled && (IsCSharpAssembly(scriptAssembly) || (metroCompilationOverrides != PlayerSettings.WSACompilationOverrides.UseNetCorePartially && IsCSharpFirstPassAssembly(scriptAssembly))); return(useDotNetCore); }
public static bool IsCSharpFirstPassAssembly(ScriptAssembly scriptAssembly) { if (!scriptAssembly.Filename.ToLower().Contains("firstpass")) { return(false); } return(scriptAssembly.Language == ScriptCompilers.CSharpSupportedLanguage); }
public static void UpdateScriptAssemblyReference(ref ScriptAssembly scriptAssembly) { int referencesLength = scriptAssembly.References.Length; var newReferences = new string[referencesLength + 1]; Array.Copy(scriptAssembly.References, newReferences, referencesLength); newReferences[referencesLength] = AssetPath.Combine(EditorApplication.applicationContentsPath, "Managed", "Unity.CompilationPipeline.Common.dll"); scriptAssembly.References = newReferences; }
public PostProcessorTask(ScriptAssembly assembly, List <CompilerMessage> compilerMessages, string tempOutputDirectory, Func <ScriptAssembly, List <CompilerMessage>, string, List <CompilerMessage> > postProcessFunc) { Assembly = assembly; CompilerMessages = compilerMessages; this.tempOutputDirectory = tempOutputDirectory; this.postProcessFunc = postProcessFunc; }
public PostProcessorTask(ScriptAssembly assembly, List <CompilerMessage> compilerMessages, string tempOutputDirectory, IILPostProcessing ilPostProcessing) { Assembly = assembly; CompilerMessages = compilerMessages; this.tempOutputDirectory = tempOutputDirectory; this.ilPostProcessing = ilPostProcessing; }
bool AnyRunningCompilerHasReference(ScriptAssembly assembly) { foreach (var compilerTask in compilerTasks) { var compilerAssembly = compilerTask.Key; if (compilerAssembly.ScriptAssemblyReferences.Contains(assembly)) { return(true); } } return(false); }
bool IsAnyProcessUsingAssembly(ScriptAssembly assembly) { if (IsAnyRunningCompilerUsingAssembly(assembly)) { return(true); } if (IsAnyRunningPostProcessorUsingAssembly(assembly)) { return(true); } return(false); }
private void QueuePendingAssemblies() { if (this.pendingAssemblies.Count != 0) { List <ScriptAssembly> list = null; foreach (ScriptAssembly current in this.pendingAssemblies) { bool flag = true; ScriptAssembly[] scriptAssemblyReferences = current.ScriptAssemblyReferences; for (int i = 0; i < scriptAssemblyReferences.Length; i++) { ScriptAssembly key = scriptAssemblyReferences[i]; if (!this.processedAssemblies.ContainsKey(key)) { flag = false; break; } } if (flag) { if (list == null) { list = new List <ScriptAssembly>(); } list.Add(current); } } if (list != null) { bool buildingForEditor = (this.options & EditorScriptCompilationOptions.BuildingForEditor) == EditorScriptCompilationOptions.BuildingForEditor; foreach (ScriptAssembly current2 in list) { this.pendingAssemblies.Remove(current2); MonoIsland island = current2.ToMonoIsland(this.options, this.buildOutputDirectory); ScriptCompilerBase scriptCompilerBase = ScriptCompilers.CreateCompilerInstance(island, buildingForEditor, island._target, current2.RunUpdater); this.compilerTasks.Add(current2, scriptCompilerBase); scriptCompilerBase.BeginCompiling(); if (this.OnCompilationStarted != null) { this.OnCompilationStarted(current2, this.compilePhase); } if (this.compilerTasks.Count == this.maxConcurrentCompilers) { break; } } this.compilePhase++; } } }
public List <CompilerMessage> PostProcess(ScriptAssembly assembly, List <CompilerMessage> messages, string outputTempPath) { var hasCompileError = messages.Any(m => m.type == CompilerMessageType.Error); if (hasCompileError) { return(messages); } if (UnityCodeGenHelpers.IsCodeGen(assembly.Filename)) { return(messages); } if (!HasPostProcessors) { return(messages); } try { List <DiagnosticMessage> diagnostics = RunILPostProcessors(assembly, outputTempPath); foreach (var message in diagnostics) { if (message.DiagnosticType == DiagnosticType.Error) { hasCompileError = true; } messages.Add(new CompilerMessage { assemblyName = message.File, column = message.Column, line = message.Line, message = message.MessageData, type = message.DiagnosticType == DiagnosticType.Error ? CompilerMessageType.Error : CompilerMessageType.Warning, }); } } catch (Exception exception) { messages.Add(new CompilerMessage { assemblyName = assembly.Filename, message = $"Something went wrong while Post Processing the assembly ({assembly.Filename}) : {Environment.NewLine} {exception.Message} {Environment.NewLine}{exception.StackTrace}", type = CompilerMessageType.Error, }); } return(messages); }
internal static ScriptAssembly[] ToScriptAssemblies(IDictionary <EditorBuildRules.TargetAssembly, HashSet <string> > targetAssemblies, ScriptAssemblySettings settings, BuildFlags buildFlags, EditorBuildRules.CompilationAssemblies assemblies, HashSet <string> runUpdaterAssemblies) { ScriptAssembly[] array = new ScriptAssembly[targetAssemblies.Count]; Dictionary <EditorBuildRules.TargetAssembly, ScriptAssembly> dictionary = new Dictionary <EditorBuildRules.TargetAssembly, ScriptAssembly>(); int num = 0; bool flag = (buildFlags & BuildFlags.BuildingForEditor) == BuildFlags.BuildingForEditor; foreach (KeyValuePair <EditorBuildRules.TargetAssembly, HashSet <string> > current in targetAssemblies) { EditorBuildRules.TargetAssembly key = current.Key; HashSet <string> value = current.Value; ScriptAssembly scriptAssembly = new ScriptAssembly(); array[num] = scriptAssembly; dictionary[key] = array[num++]; scriptAssembly.BuildTarget = settings.BuildTarget; if (key.EditorOnly || (flag && settings.ApiCompatibilityLevel == ApiCompatibilityLevel.NET_4_6)) { scriptAssembly.ApiCompatibilityLevel = ((EditorApplication.scriptingRuntimeVersion != ScriptingRuntimeVersion.Latest) ? ApiCompatibilityLevel.NET_2_0 : ApiCompatibilityLevel.NET_4_6); } else { scriptAssembly.ApiCompatibilityLevel = settings.ApiCompatibilityLevel; } if (!string.IsNullOrEmpty(settings.FilenameSuffix)) { string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(key.Filename); string extension = Path.GetExtension(key.Filename); scriptAssembly.Filename = fileNameWithoutExtension + settings.FilenameSuffix + extension; } else { scriptAssembly.Filename = key.Filename; } if (runUpdaterAssemblies != null && runUpdaterAssemblies.Contains(scriptAssembly.Filename)) { scriptAssembly.RunUpdater = true; } scriptAssembly.OutputDirectory = settings.OutputDirectory; scriptAssembly.Defines = settings.Defines; scriptAssembly.Files = value.ToArray <string>(); Array.Sort <string>(scriptAssembly.Files); } num = 0; foreach (KeyValuePair <EditorBuildRules.TargetAssembly, HashSet <string> > current2 in targetAssemblies) { EditorBuildRules.AddScriptAssemblyReferences(ref array[num++], current2.Key, settings, buildFlags, assemblies, dictionary, settings.FilenameSuffix); } return(array); }
bool IsAnyProcessUsingAssembly(ScriptAssembly assembly) { if (AnyRunningCompilerHasReference(assembly)) { return(true); } if (ilPostProcessing != null && ilPostProcessing.IsAnyRunningPostProcessorUsingAssembly(assembly)) { return(true); } return(false); }
public static string[] GetSystemLibraryReferences(ApiCompatibilityLevel apiCompatibilityLevel, BuildTarget buildTarget, SupportedLanguage supportedLanguage, bool buildingForEditor, ScriptAssembly scriptAssembly) { if (WSAHelpers.BuildingForDotNet(buildTarget, buildingForEditor, scriptAssembly)) { return(new string[0]); } // The language may not be compatible with these additional references if (supportedLanguage != null && !supportedLanguage.CompilerRequiresAdditionalReferences()) { return(new string[0]); } return(GetCachedSystemLibraryReferences(apiCompatibilityLevel)); }
public static List <string> GetPrecompiledReferences(ScriptAssembly scriptAssembly, TargetAssemblyType targetAssemblyType, EditorScriptCompilationOptions options, EditorCompatibility editorCompatibility, IEnumerable <PrecompiledAssembly> implicitPrecompiledAssemblies, IEnumerable <PrecompiledAssembly> explicitPrecompiledAssemblies, ICompilationSetupWarningTracker warningSink) { var references = new List <string>(); bool buildingForEditor = (options & EditorScriptCompilationOptions.BuildingForEditor) == EditorScriptCompilationOptions.BuildingForEditor; bool assemblyEditorOnly = (scriptAssembly.Flags & AssemblyFlags.EditorOnly) == AssemblyFlags.EditorOnly; bool isCustomAssembly = (targetAssemblyType & TargetAssemblyType.Custom) == TargetAssemblyType.Custom; void AddReferenceIfMatchBuildTargetAndEditorFlag(PrecompiledAssembly precompiledAssembly, bool explicitReference) { bool compiledAssemblyEditorOnly = (precompiledAssembly.Flags & AssemblyFlags.EditorOnly) == AssemblyFlags.EditorOnly; // Add all pre-compiled runtime assemblies as references to all script assemblies. Don't add pre-compiled editor assemblies as dependencies to runtime assemblies. if (!compiledAssemblyEditorOnly || assemblyEditorOnly || (isCustomAssembly && buildingForEditor && editorCompatibility == EditorCompatibility.CompatibleWithEditor)) { if (IsPrecompiledAssemblyCompatibleWithBuildTarget(precompiledAssembly, scriptAssembly.BuildTarget)) { references.Add(precompiledAssembly.Path); } // we don't warn on build target mismatch, as this is actually a common pattern (an asmdef with multiple references to different "target-specific" assemblies with the same symbols - e.g. foo.XboxOne.dll, foo.PS5.dll, foo.WebGL.dll) } else if (explicitReference && !string.IsNullOrEmpty(scriptAssembly.AsmDefPath)) { warningSink?.AddAssetWarning(scriptAssembly.AsmDefPath, $"{scriptAssembly.Filename}: can't add reference to {precompiledAssembly.Path} as it is an editor-only assembly"); } } if (implicitPrecompiledAssemblies != null) { foreach (var precompiledAssembly in implicitPrecompiledAssemblies) { AddReferenceIfMatchBuildTargetAndEditorFlag(precompiledAssembly, false); } } if (explicitPrecompiledAssemblies != null) { foreach (var precompiledAssembly in explicitPrecompiledAssemblies) { AddReferenceIfMatchBuildTargetAndEditorFlag(precompiledAssembly, true); } } return(references); }
bool IsAnyRunningPostProcessorUsingAssembly(ScriptAssembly assembly) { foreach (var postProcessorTask in postProcessorTasks) { if (postProcessorTask.IsFinished) { continue; } var postProcessorAssembly = postProcessorTask.Assembly; if (postProcessorAssembly.ScriptAssemblyReferences.Contains(assembly)) { return(true); } } return(false); }
public static List <string> GetCompiledCustomAssembliesReferences(ScriptAssembly scriptAssembly, TargetAssembly[] customTargetAssemblies, string outputDirectory, string filenameSuffix) { var references = new List <string>(); if (customTargetAssemblies != null) { foreach (var customTargetAssembly in customTargetAssemblies) { var customTargetAssemblyPath = customTargetAssembly.FullPath(outputDirectory, filenameSuffix); // File might not exist if there are no scripts in the custom target assembly folder. if (File.Exists(customTargetAssemblyPath)) { references.Add(customTargetAssemblyPath); } } } return(references); }
public static List <string> GetCompiledCustomAssembliesReferences(ScriptAssembly scriptAssembly, IDictionary <string, TargetAssembly> customTargetAssemblies, string outputDirectory) { var references = new List <string>(); if (customTargetAssemblies != null) { foreach (var entry in customTargetAssemblies) { var customTargetAssembly = entry.Value; var customTargetAssemblyPath = customTargetAssembly.FullPath(outputDirectory); // File might not exist if there are no scripts in the custom target assembly folder. if (AssetPath.Exists(customTargetAssemblyPath)) { references.Add(customTargetAssemblyPath); } } } return(references); }
public static List <string> GetUnityReferences(ScriptAssembly scriptAssembly, PrecompiledAssembly[] unityAssemblies, EditorScriptCompilationOptions options) { var references = new List <string>(); bool assemblyEditorOnly = (scriptAssembly.Flags & AssemblyFlags.EditorOnly) == AssemblyFlags.EditorOnly; bool buildingForEditor = (options & EditorScriptCompilationOptions.BuildingForEditor) == EditorScriptCompilationOptions.BuildingForEditor; // Add Unity assemblies (UnityEngine.dll, UnityEditor.dll) referencees. if (unityAssemblies != null) { foreach (var unityAssembly in unityAssemblies) { var moduleExcludedForRuntimeCode = (unityAssembly.Flags & AssemblyFlags.ExcludedForRuntimeCode) == AssemblyFlags.ExcludedForRuntimeCode; // Add Unity editor assemblies (UnityEditor.dll) to all assemblies when building inside the editor if ((buildingForEditor && !moduleExcludedForRuntimeCode) || assemblyEditorOnly) { if ((unityAssembly.Flags & AssemblyFlags.UseForMono) != 0) { references.Add(unityAssembly.Path); } } else { bool unityAssemblyEditorOnly = (unityAssembly.Flags & AssemblyFlags.EditorOnly) == AssemblyFlags.EditorOnly; // Add Unity runtime assemblies (UnityEngine.dll) to all assemblies if (!unityAssemblyEditorOnly && !moduleExcludedForRuntimeCode) { if (IsPrecompiledAssemblyCompatibleWithScriptAssembly(unityAssembly, scriptAssembly)) { references.Add(unityAssembly.Path); } } } } } return(references); }
static bool ShouldUseAnalyzerForScriptAssembly(ScriptAssembly sa, TargetAssembly targetAssemblyOwningAnalyzer) { if (targetAssemblyOwningAnalyzer == null) { return(true); } if (sa.Filename == targetAssemblyOwningAnalyzer.Filename) { return(true); } foreach (var sar in sa.ScriptAssemblyReferences) { if (ShouldUseAnalyzerForScriptAssembly(sar, targetAssemblyOwningAnalyzer)) { return(true); } } return(false); }
public static void UpdateCodeGenScriptAssembly(ref ScriptAssembly scriptAssembly) { scriptAssembly.ScriptAssemblyReferences = new ScriptAssembly[0]; int newReferenceCount = 0; var references = new string[scriptAssembly.References.Length]; foreach (var reference in scriptAssembly.References) { var name = AssetPath.GetFileName(reference); if (!Utility.FastStartsWith(name, k_UnityEngineModules, k_UnityEngineModulesLower) && !Utility.FastStartsWith(name, k_UnityEditorModules, k_UnityEditorModulesLower)) { references[newReferenceCount] = reference; newReferenceCount++; } } var result = new string[newReferenceCount + 1]; Array.Copy(references, result, newReferenceCount); result[newReferenceCount] = AssetPath.Combine(EditorApplication.applicationContentsPath, "Managed", "Unity.CompilationPipeline.Common.dll"); scriptAssembly.References = result; }
public static bool BuildingForDotNet(BuildTarget buildTarget, bool buildingForEditor, ScriptAssembly scriptAssembly) { if (buildingForEditor) { return(false); } if (buildTarget != BuildTarget.WSAPlayer) { return(false); } if (CSharpLanguage.GetCSharpCompiler(buildTarget, buildingForEditor, scriptAssembly) != CSharpCompiler.Microsoft) { return(false); } if (PlayerSettings.GetScriptingBackend(BuildPipeline.GetBuildTargetGroup(buildTarget)) != ScriptingImplementation.WinRTDotNET) { return(false); } return(true); }
public static void AddAdditionalPlatformSupportData(ICompilationExtension compilationExtension, ref ScriptAssembly scriptAssembly) { if (compilationExtension == null) { return; } scriptAssembly.Defines = AddAdditionalToArray(scriptAssembly.Defines, compilationExtension.GetAdditionalDefines().ToArray()); scriptAssembly.References = AddAdditionalToArray(scriptAssembly.References, compilationExtension.GetAdditionalAssemblyReferences() .Concat(compilationExtension.GetWindowsMetadataReferences()).ToArray()); scriptAssembly.Files = AddAdditionalToArray(scriptAssembly.Files, compilationExtension.GetAdditionalSourceFiles().ToArray()); }
internal static ScriptAssembly[] ToScriptAssemblies(IDictionary <TargetAssembly, HashSet <string> > targetAssemblies, ScriptAssemblySettings settings, CompilationAssemblies assemblies, HashSet <string> runUpdaterAssemblies) { var scriptAssemblies = new ScriptAssembly[targetAssemblies.Count]; var targetToScriptAssembly = new Dictionary <TargetAssembly, ScriptAssembly>(); int index = 0; bool buildingForEditor = settings.BuildingForEditor; foreach (var entry in targetAssemblies) { var targetAssembly = entry.Key; var sourceFiles = entry.Value; var scriptAssembly = new ScriptAssembly(); // Setup TargetAssembly -> ScriptAssembly mapping for converting references scriptAssemblies[index] = scriptAssembly; targetToScriptAssembly[targetAssembly] = scriptAssemblies[index++]; // Setup ScriptAssembly scriptAssembly.Flags = targetAssembly.Flags; scriptAssembly.BuildTarget = settings.BuildTarget; scriptAssembly.Language = targetAssembly.Language; var editorOnlyTargetAssembly = (targetAssembly.Flags & AssemblyFlags.EditorOnly) == AssemblyFlags.EditorOnly; if (editorOnlyTargetAssembly || (buildingForEditor && settings.ApiCompatibilityLevel == ApiCompatibilityLevel.NET_4_6)) { scriptAssembly.ApiCompatibilityLevel = (EditorApplication.scriptingRuntimeVersion == ScriptingRuntimeVersion.Latest) ? ApiCompatibilityLevel.NET_4_6 : ApiCompatibilityLevel.NET_2_0; } else { scriptAssembly.ApiCompatibilityLevel = settings.ApiCompatibilityLevel; } if (!string.IsNullOrEmpty(settings.FilenameSuffix)) { var basename = AssetPath.GetAssemblyNameWithoutExtension(targetAssembly.Filename); scriptAssembly.Filename = string.Concat(basename, settings.FilenameSuffix, ".dll"); } else { scriptAssembly.Filename = targetAssembly.Filename; } if (runUpdaterAssemblies != null && runUpdaterAssemblies.Contains(scriptAssembly.Filename)) { scriptAssembly.RunUpdater = true; } scriptAssembly.OutputDirectory = settings.OutputDirectory; scriptAssembly.Defines = settings.Defines; scriptAssembly.Files = sourceFiles.ToArray(); if (targetAssembly.Type == TargetAssemblyType.Predefined) { scriptAssembly.CompilerOptions = settings.PredefinedAssembliesCompilerOptions; } else { scriptAssembly.CompilerOptions = targetAssembly.CompilerOptions; } // Script files must always be passed in the same order to the compiler. // Otherwise player builds might fail for partial classes. Array.Sort(scriptAssembly.Files); } // Setup ScriptAssembly references index = 0; foreach (var entry in targetAssemblies) { AddScriptAssemblyReferences(ref scriptAssemblies[index++], entry.Key, settings, assemblies, targetToScriptAssembly, settings.FilenameSuffix); } return(scriptAssemblies); }
static bool IsPrecompiledAssemblyCompatibleWithScriptAssembly(PrecompiledAssembly compiledAssembly, ScriptAssembly scriptAssembly) { bool useDotNet = WSAHelpers.UseDotNetCore(scriptAssembly); if (useDotNet) { bool compiledAssemblyCompatibleWithDotNet = (compiledAssembly.Flags & AssemblyFlags.UseForDotNet) == AssemblyFlags.UseForDotNet; return(compiledAssemblyCompatibleWithDotNet); } bool compiledAssemblyCompatibleWithMono = (compiledAssembly.Flags & AssemblyFlags.UseForMono) == AssemblyFlags.UseForMono; return(compiledAssemblyCompatibleWithMono); }
public static Dictionary <string, string> GetUnityReferences(ScriptAssembly scriptAssembly, TargetAssembly targetAssembly, PrecompiledAssembly[] unityAssemblies, HashSet <string> predefinedCustomTargetReferences, EditorScriptCompilationOptions options, UnityReferencesOptions unityReferencesOptions) { var references = new Dictionary <string, string>(); bool assemblyEditorOnly = (scriptAssembly.Flags & AssemblyFlags.EditorOnly) == AssemblyFlags.EditorOnly; bool buildingForEditor = (options & EditorScriptCompilationOptions.BuildingForEditor) == EditorScriptCompilationOptions.BuildingForEditor; bool excludeUnityModules = unityReferencesOptions == UnityReferencesOptions.ExcludeModules; bool isOverridingUnityAssembly = false; // Add Unity assemblies (UnityEngine.dll, UnityEditor.dll) referencees. if (unityAssemblies == null) { return(references); } foreach (var unityAssembly in unityAssemblies) { if ((unityAssembly.Flags & (AssemblyFlags.UserOverride | AssemblyFlags.UserOverrideCandidate)) != AssemblyFlags.None) { var unityAssemblyFileName = AssetPath.GetFileName(unityAssembly.Path); // This scriptAssembly is overriding this unityAssembly so it should probably not depend on itself. if (unityAssemblyFileName == scriptAssembly.Filename) { isOverridingUnityAssembly = true; continue; } // Custom targets may override Unity references, do not add them to avoid duplicated references. if (predefinedCustomTargetReferences != null && predefinedCustomTargetReferences.Contains(unityAssemblyFileName)) { continue; } // If this scriptAssembly/targetAssembly explicitly references another // scriptAssembly that has actually overridden this unityAssembly, we should // not add the unityAssembly to the references as well. It's possible // that this scriptAssembly is using new APIs that don't exist in the shipped // copy of the unityAssembly. if (targetAssembly != null && targetAssembly.References.Any(ta => ta.Filename == unityAssemblyFileName)) { continue; } } var isUnityModule = (unityAssembly.Flags & AssemblyFlags.UnityModule) == AssemblyFlags.UnityModule; if (isUnityModule && excludeUnityModules) { continue; } var moduleExcludedForRuntimeCode = (unityAssembly.Flags & AssemblyFlags.ExcludedForRuntimeCode) == AssemblyFlags.ExcludedForRuntimeCode; // Add Unity editor assemblies (UnityEditor.dll) to all assemblies when building inside the editor if ((buildingForEditor && !moduleExcludedForRuntimeCode) || assemblyEditorOnly) { if ((unityAssembly.Flags & AssemblyFlags.UseForMono) != 0) { references[Path.GetFileName(unityAssembly.Path)] = unityAssembly.Path; } } else { bool unityAssemblyEditorOnly = (unityAssembly.Flags & AssemblyFlags.EditorOnly) == AssemblyFlags.EditorOnly; // Add Unity runtime assemblies (UnityEngine.dll) to all assemblies if (!unityAssemblyEditorOnly && !moduleExcludedForRuntimeCode) { if (IsPrecompiledAssemblyCompatibleWithBuildTarget(unityAssembly, scriptAssembly.BuildTarget)) { references[Path.GetFileName(unityAssembly.Path)] = unityAssembly.Path; } } } } // UserOverride assemblies should not have a dependency on Editor assemblies. if (isOverridingUnityAssembly && !assemblyEditorOnly) { references = references.Where(kvp => !kvp.Key.Contains("UnityEditor")).ToDictionary(kvp => kvp.Key, kvp => kvp.Value); } return(references); }
public static Dictionary <string, string> GetUnityReferences(ScriptAssembly scriptAssembly, TargetAssembly targetAssembly, PrecompiledAssembly[] unityAssemblies, EditorScriptCompilationOptions options, UnityReferencesOptions unityReferencesOptions) { return(GetUnityReferences(scriptAssembly, targetAssembly, unityAssemblies, null, options, unityReferencesOptions)); }
internal static void AddScriptAssemblyReferences(ref ScriptAssembly scriptAssembly, TargetAssembly targetAssembly, ScriptAssemblySettings settings, CompilationAssemblies assemblies, IDictionary <TargetAssembly, ScriptAssembly> targetToScriptAssembly, ICompilationSetupWarningTracker warningSink) { var scriptAssemblyReferences = new List <ScriptAssembly>(targetAssembly.References.Count); var references = new List <string>(); bool buildingForEditor = settings.BuildingForEditor; bool noEngineReferences = (targetAssembly.Flags & AssemblyFlags.NoEngineReferences) == AssemblyFlags.NoEngineReferences; bool shouldProcessPredefinedCustomTargets = assemblies.CustomTargetAssemblies != null && (targetAssembly.Type & TargetAssemblyType.Predefined) == TargetAssemblyType.Predefined; var predefinedCustomTargetReferences = Enumerable.Empty <TargetAssembly>(); if (shouldProcessPredefinedCustomTargets && assemblies.PredefinedAssembliesCustomTargetReferences != null) { predefinedCustomTargetReferences = assemblies.PredefinedAssembliesCustomTargetReferences; } var unityReferences = new Dictionary <string, string>(); // Add Unity assemblies (UnityEngine.dll, UnityEditor.dll) references, as long as the target // doesn't specify that it doesn't want them. if (!noEngineReferences) { // Add predefined custom target references in a hash-set for fast lookup var predefinedCustomTargetRefs = new HashSet <string>(predefinedCustomTargetReferences.Select(x => x.Filename)); unityReferences = GetUnityReferences(scriptAssembly, targetAssembly, assemblies.UnityAssemblies, predefinedCustomTargetRefs, settings.CompilationOptions, UnityReferencesOptions.None); references.AddRange(unityReferences.Values); } AddTestRunnerCustomReferences(ref targetAssembly, assemblies.CustomTargetAssemblies); // Setup target assembly references foreach (var reference in targetAssembly.References) { ScriptAssembly scriptAssemblyReference; // If the assembly already showed up in the unity references, don't reference it here. // This can happen when an assembly is configured as a unity assembly override, but // overrides are disabled. The Unity assembly should take precedence in that case. if (unityReferences.ContainsKey(reference.Filename)) { continue; } // Add ScriptAssembly references to other dirty script assemblies that also need to be rebuilt. if (targetToScriptAssembly.TryGetValue(reference, out scriptAssemblyReference)) { System.Diagnostics.Debug.Assert(scriptAssemblyReference != null); scriptAssemblyReferences.Add(scriptAssemblyReference); } } // For predefined target assembly add references to custom target assemblies if (shouldProcessPredefinedCustomTargets) { foreach (var customTargetAssembly in predefinedCustomTargetReferences) { ScriptAssembly scriptAssemblyReference; // Only add ScriptAssembly reference if the custom target assembly is dirty, e.g. is in targetToScriptAssembly dictionary // Otherwise just add already compiled custom target assembly as precompiled reference. if (targetToScriptAssembly.TryGetValue(customTargetAssembly, out scriptAssemblyReference)) { scriptAssemblyReferences.Add(scriptAssemblyReference); } } } // Add pre-compiled assemblies as references var allPrecompiledAssemblies = assemblies.PrecompiledAssemblies ?? new Dictionary <string, PrecompiledAssembly>(0); List <PrecompiledAssembly> precompiledReferences = new List <PrecompiledAssembly>(allPrecompiledAssemblies.Count); var explicitPrecompiledReferences = new List <PrecompiledAssembly>(targetAssembly.ExplicitPrecompiledReferences.Count); if ((targetAssembly.Flags & AssemblyFlags.ExplicitReferences) == AssemblyFlags.ExplicitReferences) { if (!noEngineReferences) { precompiledReferences.AddRange(allPrecompiledAssemblies .Where(x => (x.Value.Flags & AssemblyFlags.UserAssembly) != AssemblyFlags.UserAssembly) .Select(x => x.Value)); } foreach (var explicitPrecompiledReference in targetAssembly.ExplicitPrecompiledReferences) { PrecompiledAssembly assembly; if (allPrecompiledAssemblies.TryGetValue(explicitPrecompiledReference, out assembly)) { explicitPrecompiledReferences.Add(assembly); } } } else { var precompiledAssemblies = allPrecompiledAssemblies.Values.Where(x => (x.Flags & AssemblyFlags.ExplicitlyReferenced) != AssemblyFlags.ExplicitlyReferenced).ToList(); // if noEngineReferences, add just the non-explicitly-referenced user assemblies if (noEngineReferences) { precompiledReferences.AddRange(precompiledAssemblies.Where(x => (x.Flags & AssemblyFlags.UserAssembly) == AssemblyFlags.UserAssembly)); } else { precompiledReferences.AddRange(precompiledAssemblies); } } AddTestRunnerPrecompiledReferences(targetAssembly, allPrecompiledAssemblies, ref precompiledReferences); var precompiledReferenceNames = GetPrecompiledReferences(scriptAssembly, targetAssembly.Type, settings.CompilationOptions, targetAssembly.editorCompatibility, precompiledReferences, explicitPrecompiledReferences, warningSink); references.AddRange(precompiledReferenceNames); if (buildingForEditor && assemblies.EditorAssemblyReferences != null) { references.AddRange(assemblies.EditorAssemblyReferences); } references.AddRange(MonoLibraryHelpers.GetSystemLibraryReferences(scriptAssembly.CompilerOptions.ApiCompatibilityLevel)); scriptAssembly.ScriptAssemblyReferences = scriptAssemblyReferences.ToArray(); scriptAssembly.References = references.ToArray(); }
public bool Poll() { bool result; if (this.Stopped) { result = true; } else { Dictionary <ScriptAssembly, ScriptCompilerBase> dictionary = null; foreach (KeyValuePair <ScriptAssembly, ScriptCompilerBase> current in this.compilerTasks) { ScriptCompilerBase value = current.Value; if (value.Poll()) { if (dictionary == null) { dictionary = new Dictionary <ScriptAssembly, ScriptCompilerBase>(); } ScriptAssembly key = current.Key; dictionary.Add(key, value); } } if (dictionary != null) { foreach (KeyValuePair <ScriptAssembly, ScriptCompilerBase> current2 in dictionary) { ScriptAssembly key2 = current2.Key; ScriptCompilerBase value2 = current2.Value; CompilerMessage[] compilerMessages = value2.GetCompilerMessages(); List <CompilerMessage> list = compilerMessages.ToList <CompilerMessage>(); if (this.OnCompilationFinished != null) { this.OnCompilationFinished(key2, list); } this.processedAssemblies.Add(key2, list.ToArray()); if (!this.CompileErrors) { this.CompileErrors = compilerMessages.Any((CompilerMessage m) => m.type == CompilerMessageType.Error); } this.compilerTasks.Remove(key2); value2.Dispose(); } } if (this.CompileErrors) { if (this.pendingAssemblies.Count > 0) { foreach (ScriptAssembly current3 in this.pendingAssemblies) { this.processedAssemblies.Add(current3, new CompilerMessage[0]); } this.pendingAssemblies.Clear(); } result = (this.compilerTasks.Count == 0); } else { if (this.compilerTasks.Count == 0 || (dictionary != null && dictionary.Count > 0)) { this.QueuePendingAssemblies(); } result = (this.pendingAssemblies.Count == 0 && this.compilerTasks.Count == 0); } } return(result); }
public static PrecompiledAssembly[] GetUnityReferences(ScriptAssembly scriptAssembly, PrecompiledAssembly[] unityAssemblies, EditorScriptCompilationOptions options, UnityReferencesOptions unityReferencesOptions) { return(GetUnityReferences(scriptAssembly, null, unityAssemblies, options, unityReferencesOptions)); }
internal static void AddScriptAssemblyReferences(ref ScriptAssembly scriptAssembly, TargetAssembly targetAssembly, ScriptAssemblySettings settings, CompilationAssemblies assemblies, IDictionary <TargetAssembly, ScriptAssembly> targetToScriptAssembly, string filenameSuffix) { var scriptAssemblyReferences = new List <ScriptAssembly>(); var references = new List <string>(); bool buildingForEditor = settings.BuildingForEditor; // Add Unity assemblies (UnityEngine.dll, UnityEditor.dll) referencees. var unityReferences = GetUnityReferences(scriptAssembly, assemblies.UnityAssemblies, settings.CompilationOptions); references.AddRange(unityReferences); // Setup target assembly references foreach (var reference in targetAssembly.References) { ScriptAssembly scriptAssemblyReference; // Add ScriptAssembly references to other dirty script assemblies that also need to be rebuilt. if (targetToScriptAssembly.TryGetValue(reference, out scriptAssemblyReference)) { System.Diagnostics.Debug.Assert(scriptAssemblyReference != null); scriptAssemblyReferences.Add(scriptAssemblyReference); } else { // Add string references to other assemblies that do not need to be rebuilt. var assemblyPath = reference.FullPath(settings.OutputDirectory, filenameSuffix); if (File.Exists(assemblyPath)) { references.Add(assemblyPath); } } } // For predefined target assembly add references to custom target assemblies if (assemblies.CustomTargetAssemblies != null && (targetAssembly.Type & TargetAssemblyType.Predefined) == TargetAssemblyType.Predefined) { foreach (var customTargetAssembly in assemblies.PredefinedAssembliesCustomTargetReferences ?? Enumerable.Empty <TargetAssembly>()) { ScriptAssembly scriptAssemblyReference; // Only add ScriptAssembly reference if the custom target assembly is dirty, e.g. is in targetToScriptAssembly dictionary // Otherwise just add already compiled custom target assembly as precompiled reference. if (targetToScriptAssembly.TryGetValue(customTargetAssembly, out scriptAssemblyReference)) { scriptAssemblyReferences.Add(scriptAssemblyReference); } else { var customTargetAssemblyPath = customTargetAssembly.FullPath(settings.OutputDirectory, filenameSuffix); // File might not exist if there are no scripts in the custom target assembly folder. if (File.Exists(customTargetAssemblyPath)) { references.Add(customTargetAssemblyPath); } } } } // Add pre-compiled assemblies as references PrecompiledAssembly[] precompiledAssembliesForReferences = assemblies.PrecompiledAssemblies ?? new PrecompiledAssembly[] {}; if (settings.OptionalUnityReferences != OptionalUnityReferences.None) { precompiledAssembliesForReferences = precompiledAssembliesForReferences.Where(x => x.OptionalUnityReferences == OptionalUnityReferences.None || ((targetAssembly.OptionalUnityReferences & x.OptionalUnityReferences & settings.OptionalUnityReferences) != 0)).ToArray(); } var precompiledReferences = GetPrecompiledReferences(scriptAssembly, targetAssembly.Type, settings.CompilationOptions, targetAssembly.editorCompatibility, precompiledAssembliesForReferences); references.AddRange(precompiledReferences); if (buildingForEditor && assemblies.EditorAssemblyReferences != null) { references.AddRange(assemblies.EditorAssemblyReferences); } references.AddRange(MonoLibraryHelpers.GetSystemLibraryReferences(scriptAssembly.ApiCompatibilityLevel, scriptAssembly.BuildTarget, scriptAssembly.Language, buildingForEditor, scriptAssembly.Filename)); scriptAssembly.ScriptAssemblyReferences = scriptAssemblyReferences.ToArray(); scriptAssembly.References = references.ToArray(); }
internal static ScriptAssembly[] ToScriptAssemblies( IDictionary <TargetAssembly, DirtyTargetAssembly> targetAssemblies, ScriptAssemblySettings settings, CompilationAssemblies assemblies, ICompilationSetupWarningTracker warningSink, ISafeModeInfo safeModeInfo) { var scriptAssemblies = new ScriptAssembly[targetAssemblies.Count]; var targetToScriptAssembly = new Dictionary <TargetAssembly, ScriptAssembly>(); int index = 0; bool buildingForEditor = settings.BuildingForEditor; var safeModeWhiteList = new HashSet <string>(safeModeInfo.GetWhiteListAssemblyNames()); foreach (var entry in targetAssemblies) { var targetAssembly = entry.Key; var dirtyTargetAssembly = entry.Value; var scriptAssembly = new ScriptAssembly(); // Setup TargetAssembly -> ScriptAssembly mapping for converting references scriptAssemblies[index] = scriptAssembly; targetToScriptAssembly[targetAssembly] = scriptAssemblies[index++]; // Setup ScriptAssembly scriptAssembly.Flags = targetAssembly.Flags; scriptAssembly.BuildTarget = settings.BuildTarget; scriptAssembly.OriginPath = targetAssembly.PathPrefix; scriptAssembly.Filename = targetAssembly.Filename; scriptAssembly.SkipCodeGen = safeModeWhiteList.Contains(targetAssembly.Filename); scriptAssembly.RootNamespace = targetAssembly.Type == TargetAssemblyType.Predefined ? settings.ProjectRootNamespace : targetAssembly.RootNamespace; scriptAssembly.OutputDirectory = settings.OutputDirectory; scriptAssembly.Defines = targetAssembly.Defines == null ? s_CSharpVersionDefines : targetAssembly.Defines.Concat(s_CSharpVersionDefines).ToArray(); scriptAssembly.Files = dirtyTargetAssembly.SourceFiles.ToArray(); scriptAssembly.TargetAssemblyType = targetAssembly.Type; scriptAssembly.AsmDefPath = targetAssembly.AsmDefPath; if (scriptAssembly.TargetAssemblyType == TargetAssemblyType.Predefined) { scriptAssembly.CompilerOptions = new ScriptCompilerOptions(settings.PredefinedAssembliesCompilerOptions); } else { scriptAssembly.CompilerOptions = targetAssembly.CompilerOptions; } scriptAssembly.CompilerOptions.RoslynAnalyzerDllPaths = new string[0]; scriptAssembly.CompilerOptions.RoslynAnalyzerRulesetPath = string.Empty; scriptAssembly.CompilerOptions.AdditionalCompilerArguments = settings.AdditionalCompilerArguments; var editorOnlyTargetAssembly = (targetAssembly.Flags & AssemblyFlags.EditorOnly) == AssemblyFlags.EditorOnly; if (editorOnlyTargetAssembly) { scriptAssembly.CompilerOptions.ApiCompatibilityLevel = ApiCompatibilityLevel.NET_Unity_4_8; } else { scriptAssembly.CompilerOptions.ApiCompatibilityLevel = settings.PredefinedAssembliesCompilerOptions.ApiCompatibilityLevel; } if ((settings.CompilationOptions & EditorScriptCompilationOptions.BuildingUseDeterministicCompilation) == EditorScriptCompilationOptions.BuildingUseDeterministicCompilation) { scriptAssembly.CompilerOptions.UseDeterministicCompilation = true; } else { scriptAssembly.CompilerOptions.UseDeterministicCompilation = false; } scriptAssembly.CompilerOptions.CodeOptimization = settings.CodeOptimization; } // Don't add the auto-referenced engine assemblies if the assembly either has the flag set, or // is a codegen assembly AutoReferencedPackageAssemblies.AddReferences(assemblies.CustomTargetAssemblies, settings.CompilationOptions, t => { var hasNoEngineReferencesFlag = (t.Flags & AssemblyFlags.NoEngineReferences) == AssemblyFlags.NoEngineReferences; if (hasNoEngineReferencesFlag) { return(false); } return(!UnityCodeGenHelpers.IsCodeGen(t.Filename)); }); // Setup ScriptAssembly references index = 0; foreach (var entry in targetAssemblies) { var scriptAssembly = scriptAssemblies[index++]; AddScriptAssemblyReferences(ref scriptAssembly, entry.Key, settings, assemblies, targetToScriptAssembly, warningSink); if (UnityCodeGenHelpers.IsCodeGen(entry.Key.Filename) || UnityCodeGenHelpers.IsCodeGenTest(entry.Key.Filename) || CompilationPipelineCommonHelper.ShouldAdd(entry.Key.Filename)) { CompilationPipelineCommonHelper.UpdateScriptAssemblyReference(ref scriptAssembly); } if (!buildingForEditor) { PlatformSupportModuleHelpers.AddAdditionalPlatformSupportData(settings.CompilationExtension, ref scriptAssembly); } } if ((settings.CompilationOptions & EditorScriptCompilationOptions.BuildingWithRoslynAnalysis) != 0) { var analyzers = assemblies.RoslynAnalyzerDllPaths; foreach (var a in analyzers) { var targetAssemblyOwningAnalyzer = assemblies.CustomTargetAssemblies.Values .OrderBy(c => c.PathFilter(a)).LastOrDefault(); if (targetAssemblyOwningAnalyzer?.PathFilter(a) <= 0) { targetAssemblyOwningAnalyzer = null; } foreach (var scriptAssembly in scriptAssemblies) { if (ShouldUseAnalyzerForScriptAssembly(scriptAssembly, targetAssemblyOwningAnalyzer)) { scriptAssembly.CompilerOptions.RoslynAnalyzerDllPaths = scriptAssembly.CompilerOptions.RoslynAnalyzerDllPaths.Concat(new[] { a }).ToArray(); scriptAssembly.CompilerOptions.RoslynAnalyzerRulesetPath = scriptAssembly.TargetAssemblyType == TargetAssemblyType.Predefined ? RuleSetFileCache.GetRuleSetFilePathInRootFolder(Path.ChangeExtension(scriptAssembly.Filename, null)) : RuleSetFileCache.GetPathForAssembly(scriptAssembly.OriginPath); } } } } return(scriptAssemblies); }