Esempio n. 1
0
        public void LogVersionDefineError(TargetAssembly targetAssembly, ExpressionNotValidException validationError)
        {
            var asset      = AssetDatabase.LoadAssetAtPath <AssemblyDefinitionAsset>(EditorCompilationInterface.Instance.FindCustomTargetAssemblyFromTargetAssembly(targetAssembly).FilePath);
            var instanceID = asset.GetInstanceID();

            InternalLogVersionDefineError(validationError, instanceID);
        }
        internal static TargetAssembly GetPredefinedTargetAssembly(string scriptPath)
        {
            TargetAssembly resultAssembly = null;

            var lowerPath        = ScriptPathToLowerCase(scriptPath);
            int highestPathDepth = -1;

            foreach (var assembly in predefinedTargetAssemblies)
            {
                var pathFilter = assembly.PathFilter;
                int pathDepth  = -1;

                if (pathFilter == null)
                {
                    pathDepth = 0;
                }
                else
                {
                    pathDepth = pathFilter(lowerPath);
                }

                if (pathDepth > highestPathDepth)
                {
                    resultAssembly   = assembly;
                    highestPathDepth = pathDepth;
                }
            }

            return(resultAssembly);
        }
        internal static TargetAssembly GetPredefinedTargetAssembly(string scriptPath, string assemblyName = null)
        {
            if (assemblyName == null)
            {
                TargetAssembly resultAssembly   = null;
                var            lowerPath        = ScriptPathToLowerCase(scriptPath);
                int            highestPathDepth = -1;
                foreach (var assembly in predefinedTargetAssemblies.Values)
                {
                    var pathDepth = assembly.PathFilter?.Invoke(lowerPath) ?? 0;
                    if (pathDepth <= highestPathDepth)

                    {
                        continue;
                    }
                    resultAssembly   = assembly;
                    highestPathDepth = pathDepth;
                }
                return(resultAssembly);
            }

            if (predefinedTargetAssemblies.TryGetValue(assemblyName, out var predefined))
            {
                return(predefined);
            }
            return(null);
        }
Esempio n. 4
0
        public static bool ShouldAddTestRunnerReferences(TargetAssembly targetAssembly)
        {
            if (UnityCodeGenHelpers.IsCodeGen(targetAssembly.Filename))
            {
                return(false);
            }

            bool referencesTestRunnerAssembly = false;

            foreach (var reference in targetAssembly.References)
            {
                referencesTestRunnerAssembly =
                    reference.Filename.Equals(k_EditorTestRunnerAssemblyName, StringComparison.Ordinal) ||
                    reference.Filename.Equals(k_EngineTestRunnerAssemblyName, StringComparison.Ordinal);

                if (referencesTestRunnerAssembly)
                {
                    break;
                }
            }

            return(!referencesTestRunnerAssembly &&
                   !targetAssembly.Filename.Equals(k_EditorTestRunnerAssemblyName, StringComparison.Ordinal) &&
                   !targetAssembly.Filename.Equals(k_EngineTestRunnerAssemblyName, StringComparison.Ordinal) &&
                   (PlayerSettings.playModeTestRunnerEnabled || (targetAssembly.Flags & AssemblyFlags.EditorOnly) == AssemblyFlags.EditorOnly));
        }
 internal static void AddTestRunnerCustomReferences(ref TargetAssembly targetAssembly, Dictionary <string, TargetAssembly> assembliesCustomTargetAssemblies)
 {
     if (TestRunnerHelpers.ShouldAddTestRunnerReferences(targetAssembly) && assembliesCustomTargetAssemblies != null)
     {
         targetAssembly.References.AddRange(TestRunnerHelpers.GetReferences(assembliesCustomTargetAssemblies.Values));
     }
 }
 internal static void AddTestRunnerPrecompiledReferences(TargetAssembly targetAssembly, Dictionary <string, PrecompiledAssembly> nameToPrecompiledAssemblies, ref List <PrecompiledAssembly> precompiledReferences)
 {
     if (TestRunnerHelpers.ShouldAddNunitReferences(targetAssembly))
     {
         TestRunnerHelpers.AddNunitReferences(nameToPrecompiledAssemblies, ref precompiledReferences);
     }
 }
        internal static TargetAssembly GetCustomTargetAssembly(string scriptPath, string projectDirectory, TargetAssembly[] customTargetAssemblies)
        {
            if (customTargetAssemblies == null)
            {
                return(null);
            }

            int            highestPathDepth = -1;
            TargetAssembly resultAssembly   = null;

            // CustomScriptAssembly paths are absolute, so we convert the scriptPath to an absolute path, if necessary.
            bool isPathAbsolute = AssetPath.IsPathRooted(scriptPath);
            var  lowerFullPath  = isPathAbsolute ? AssetPath.GetFullPath(scriptPath).ToLower() : AssetPath.Combine(projectDirectory, scriptPath).ToLower();

            foreach (var assembly in customTargetAssemblies)
            {
                int pathDepth = assembly.PathFilter(lowerFullPath);

                if (pathDepth <= highestPathDepth)
                {
                    continue;
                }

                resultAssembly   = assembly;
                highestPathDepth = pathDepth;
            }

            return(resultAssembly);
        }
        public static bool IsCompatibleWithPlatformAndDefines(TargetAssembly assembly, BuildTarget buildTarget, EditorScriptCompilationOptions options)
        {
            var settings = new ScriptAssemblySettings
            {
                BuildTarget        = buildTarget,
                CompilationOptions = options
            };

            return(IsCompatibleWithPlatformAndDefines(assembly, settings));
        }
Esempio n. 9
0
        internal static TargetAssembly GetTargetAssembly(string scriptPath, string projectDirectory, TargetAssembly[] customTargetAssemblies)
        {
            TargetAssembly resultAssembly = GetCustomTargetAssembly(scriptPath, projectDirectory, customTargetAssemblies);

            if (resultAssembly != null)
            {
                return(resultAssembly);
            }

            return(GetPredefinedTargetAssembly(scriptPath));
        }
Esempio n. 10
0
        internal static TargetAssembly GetTargetAssemblyLinearSearch(string scriptPath, string projectDirectory, IDictionary <string, TargetAssembly> customTargetAssemblies)
        {
            TargetAssembly resultAssembly = GetCustomTargetAssembly(scriptPath, projectDirectory, customTargetAssemblies);

            if (resultAssembly != null)
            {
                return(resultAssembly);
            }

            // Do not compile scripts outside the Assets/ folder into predefined assemblies.
            if (!Utility.IsAssetsPath(scriptPath))
            {
                return(null);
            }

            return(GetPredefinedTargetAssembly(scriptPath));
        }
Esempio n. 11
0
        public static bool ShouldAddNunitReferences(TargetAssembly targetAssembly)
        {
            bool referencesNUnit = false;

            if ((targetAssembly.Flags & AssemblyFlags.ExplicitReferences) == AssemblyFlags.ExplicitReferences)
            {
                foreach (var explicitPrecompiledReference in targetAssembly.ExplicitPrecompiledReferences)
                {
                    referencesNUnit = explicitPrecompiledReference.Equals(NunitAssemblyName, StringComparison.Ordinal);
                    if (referencesNUnit)
                    {
                        break;
                    }
                }
            }

            return(!referencesNUnit &&
                   (PlayerSettings.playModeTestRunnerEnabled || (targetAssembly.Flags & AssemblyFlags.EditorOnly) == AssemblyFlags.EditorOnly));
        }
Esempio n. 12
0
        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);
        }
Esempio n. 13
0
        internal static TargetAssembly GetPredefinedTargetAssembly(string scriptPath)
        {
            TargetAssembly resultAssembly = null;

            var extension        = AssetPath.GetExtension(scriptPath).Substring(1).ToLower();
            var lowerPath        = ("/" + scriptPath.ToLower()).ConvertSeparatorsToUnity();
            int highestPathDepth = -1;

            foreach (var assembly in predefinedTargetAssemblies)
            {
                if (extension != assembly.Language.GetExtensionICanCompile())
                {
                    continue;
                }

                var pathFilter = assembly.PathFilter;
                int pathDepth  = -1;

                if (pathFilter == null)
                {
                    pathDepth = 0;
                }
                else
                {
                    pathDepth = pathFilter(lowerPath);
                }

                if (pathDepth > highestPathDepth)
                {
                    resultAssembly   = assembly;
                    highestPathDepth = pathDepth;
                }
            }

            return(resultAssembly);
        }
Esempio n. 14
0
        internal static TargetAssembly[] CreatePredefinedTargetAssemblies()
        {
            var runtimeFirstPassAssemblies = new List <TargetAssembly>();
            var runtimeAssemblies          = new List <TargetAssembly>();
            var editorFirstPassAssemblies  = new List <TargetAssembly>();
            var editorAssemblies           = new List <TargetAssembly>();

            var assemblies = new List <TargetAssembly>();

            var scriptCompilerOptions = new ScriptCompilerOptions();

            // Initialize predefined assembly targets
            {
                const string languageName = "CSharp";

                var runtimeFirstPass = new TargetAssembly("Assembly-" + languageName + "-firstpass" + ".dll",
                                                          AssemblyFlags.FirstPass | AssemblyFlags.UserAssembly,
                                                          TargetAssemblyType.Predefined,
                                                          null,
                                                          null,
                                                          FilterAssemblyInFirstpassFolder,
                                                          null,
                                                          scriptCompilerOptions);

                var runtime = new TargetAssembly("Assembly-" + languageName + ".dll",
                                                 AssemblyFlags.UserAssembly,
                                                 TargetAssemblyType.Predefined,
                                                 null,
                                                 null,
                                                 null,
                                                 null,
                                                 scriptCompilerOptions);

                var editorFirstPass = new TargetAssembly("Assembly-" + languageName + "-Editor-firstpass" + ".dll",
                                                         AssemblyFlags.EditorOnly | AssemblyFlags.FirstPass | AssemblyFlags.UserAssembly,
                                                         TargetAssemblyType.Predefined,
                                                         null,
                                                         null,
                                                         FilterAssemblyInFirstpassEditorFolder,
                                                         (settings, defines) => IsCompatibleWithEditor(settings),
                                                         scriptCompilerOptions);

                var editor = new TargetAssembly("Assembly-" + languageName + "-Editor" + ".dll",
                                                AssemblyFlags.EditorOnly | AssemblyFlags.UserAssembly,
                                                TargetAssemblyType.Predefined,
                                                null,
                                                null,
                                                FilterAssemblyInEditorFolder,
                                                (settings, defines) => IsCompatibleWithEditor(settings),
                                                scriptCompilerOptions);

                runtimeFirstPassAssemblies.Add(runtimeFirstPass);
                runtimeAssemblies.Add(runtime);
                editorFirstPassAssemblies.Add(editorFirstPass);
                editorAssemblies.Add(editor);

                assemblies.Add(runtimeFirstPass);
                assemblies.Add(runtime);
                assemblies.Add(editorFirstPass);
                assemblies.Add(editor);
            }

            // Setup dependencies

            // Runtime assemblies depend all first pass runtime assemblies
            foreach (var assembly in runtimeAssemblies)
            {
                assembly.References.AddRange(runtimeFirstPassAssemblies);
            }

            // First pass editor assemblies depend on all first pass runtime assemblies
            foreach (var assembly in editorFirstPassAssemblies)
            {
                assembly.References.AddRange(runtimeFirstPassAssemblies);
            }

            // Editor assemblies depend on all previous runtime and editor assemblies
            foreach (var assembly in editorAssemblies)
            {
                assembly.References.AddRange(runtimeFirstPassAssemblies);
                assembly.References.AddRange(runtimeAssemblies);
                assembly.References.AddRange(editorFirstPassAssemblies);
            }

            return(assemblies.ToArray());
        }
Esempio n. 15
0
 public static bool IsCompatibleWithPlatformAndDefines(TargetAssembly assembly, ScriptAssemblySettings settings)
 {
     return(assembly.IsCompatibleFunc == null || assembly.IsCompatibleFunc(settings, assembly.Defines));
 }
Esempio n. 16
0
        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);
        }
Esempio n. 17
0
 public static Dictionary <string, string> GetUnityReferences(ScriptAssembly scriptAssembly, TargetAssembly targetAssembly, PrecompiledAssembly[] unityAssemblies, EditorScriptCompilationOptions options, UnityReferencesOptions unityReferencesOptions)
 {
     return(GetUnityReferences(scriptAssembly, targetAssembly, unityAssemblies, null, options, unityReferencesOptions));
 }
Esempio n. 18
0
        public static TargetAssembly[] CreateTargetAssemblies(IEnumerable <CustomScriptAssembly> customScriptAssemblies)
        {
            if (customScriptAssemblies == null)
            {
                return(null);
            }


            foreach (var customAssembly in customScriptAssemblies)
            {
                if (predefinedTargetAssemblies.Any(p => AssetPath.GetAssemblyNameWithoutExtension(p.Filename) == customAssembly.Name))
                {
                    throw new Exception(string.Format("Assembly cannot be have reserved name '{0}'. Defined in '{1}'", customAssembly.Name, customAssembly.FilePath));
                }
            }

            var targetAssemblies     = new List <TargetAssembly>();
            var nameToTargetAssembly = new Dictionary <string, TargetAssembly>();


            // Create TargetAssemblies
            foreach (var customAssembly in customScriptAssemblies)
            {
                var lowerPathPrefix = customAssembly.PathPrefix.ToLower(CultureInfo.InvariantCulture);

                var targetAssembly = new TargetAssembly(customAssembly.Name + ".dll",
                                                        null,
                                                        customAssembly.AssemblyFlags,
                                                        TargetAssemblyType.Custom,
                                                        customAssembly.PathPrefix,
                                                        path => FastStartsWith(path, customAssembly.PathPrefix, lowerPathPrefix) ? customAssembly.PathPrefix.Length : -1,
                                                        (BuildTarget target, EditorScriptCompilationOptions options) => customAssembly.IsCompatibleWith(target, options),
                                                        customAssembly.CompilerOptions)
                {
                    OptionalUnityReferences = customAssembly.OptionalUnityReferences,
                };

                targetAssemblies.Add(targetAssembly);
                nameToTargetAssembly[customAssembly.Name] = targetAssembly;
            }

            var targetAssembliesEnumerator = targetAssemblies.GetEnumerator();

            // Setup references for TargetAssemblies
            foreach (var customAssembly in customScriptAssemblies)
            {
                targetAssembliesEnumerator.MoveNext();
                var targetAssembly = targetAssembliesEnumerator.Current;

                if (customAssembly.References == null)
                {
                    continue;
                }

                foreach (var reference in customAssembly.References)
                {
                    TargetAssembly referenceAssembly = null;

                    if (!nameToTargetAssembly.TryGetValue(reference, out referenceAssembly))
                    {
                        UnityEngine.Debug.LogWarning(string.Format("Could not find reference '{0}' for assembly '{1}'", reference, customAssembly.Name));
                        continue;
                    }

                    targetAssembly.References.Add(referenceAssembly);
                }
            }

            return(targetAssemblies.ToArray());
        }
Esempio n. 19
0
        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();
        }
Esempio n. 20
0
 static bool IsCompatibleWithPlatform(TargetAssembly assembly, ScriptAssemblySettings settings)
 {
     return(assembly.IsCompatibleFunc == null || assembly.IsCompatibleFunc(settings.BuildTarget, settings.CompilationOptions));
 }
Esempio n. 21
0
        public static Dictionary <string, TargetAssembly> CreateTargetAssemblies(IEnumerable <CustomScriptAssembly> customScriptAssemblies)
        {
            if (customScriptAssemblies == null)
            {
                return(null);
            }

            var targetAssemblies     = new List <TargetAssembly>();
            var nameToTargetAssembly = new Dictionary <string, TargetAssembly>();

            // Create TargetAssemblies
            foreach (var customAssembly in customScriptAssemblies)
            {
                var lowerPathPrefix             = Utility.FastToLower(customAssembly.PathPrefix);
                var lowerAdditionalPathPrefixes = customAssembly.AdditionalPrefixes?.Select(Utility.FastToLower).ToArray();

                var targetAssembly = new TargetAssembly(customAssembly.Name + ".dll",
                                                        customAssembly.AssemblyFlags,
                                                        TargetAssemblyType.Custom,
                                                        customAssembly.PathPrefix,
                                                        customAssembly.AdditionalPrefixes,
                                                        path => PathFilter(path, customAssembly.PathPrefix, lowerPathPrefix, customAssembly.AdditionalPrefixes, lowerAdditionalPathPrefixes),
                                                        (settings, defines) => customAssembly.IsCompatibleWith(settings.BuildTarget, settings.CompilationOptions, defines),
                                                        customAssembly.CompilerOptions)
                {
                    ExplicitPrecompiledReferences = customAssembly.PrecompiledReferences?.ToList() ?? new List <string>(),
                    VersionDefines = customAssembly.VersionDefines != null
                        ? customAssembly.VersionDefines.ToList() : new List <VersionDefine>(),
                    RootNamespace       = customAssembly.RootNamespace,
                    ResponseFileDefines = customAssembly.ResponseFileDefines,
                    AsmDefPath          = customAssembly.FilePath
                };

                targetAssemblies.Add(targetAssembly);
                nameToTargetAssembly[customAssembly.Name] = targetAssembly;
            }

            var targetAssembliesEnumerator = targetAssemblies.GetEnumerator();

            // Setup references for TargetAssemblies
            foreach (var customAssembly in customScriptAssemblies)
            {
                targetAssembliesEnumerator.MoveNext();
                var targetAssembly = targetAssembliesEnumerator.Current;

                if (customAssembly.References == null)
                {
                    continue;
                }

                foreach (var reference in customAssembly.References)
                {
                    TargetAssembly referenceAssembly = null;

                    if (!nameToTargetAssembly.TryGetValue(reference, out referenceAssembly))
                    {
                        continue;
                    }

                    targetAssembly.References.Add(referenceAssembly);
                }
            }

            var customTargetAssembliesDict = new Dictionary <string, TargetAssembly>();

            foreach (var targetAssembly in targetAssemblies)
            {
                customTargetAssembliesDict[targetAssembly.Filename] = targetAssembly;
            }

            return(customTargetAssembliesDict);
        }
Esempio n. 22
0
        internal static TargetAssembly[] CreatePredefinedTargetAssemblies()
        {
            var runtimeFirstPassAssemblies = new List <TargetAssembly>();
            var runtimeAssemblies          = new List <TargetAssembly>();
            var editorFirstPassAssemblies  = new List <TargetAssembly>();
            var editorAssemblies           = new List <TargetAssembly>();

            var supportedLanguages = ScriptCompilers.SupportedLanguages;
            var assemblies         = new List <TargetAssembly>();

            var scriptCompilerOptions = new ScriptCompilerOptions();

            // Initialize predefined assembly targets
            foreach (var language in supportedLanguages)
            {
                var languageName = language.GetLanguageName();

                var runtimeFirstPass = new TargetAssembly("Assembly-" + languageName + "-firstpass" + ".dll",
                                                          language,
                                                          AssemblyFlags.FirstPass,
                                                          TargetAssemblyType.Predefined,
                                                          null,
                                                          FilterAssemblyInFirstpassFolder,
                                                          null,
                                                          scriptCompilerOptions);

                var runtime = new TargetAssembly("Assembly-" + languageName + ".dll",
                                                 language,
                                                 AssemblyFlags.None,
                                                 TargetAssemblyType.Predefined,
                                                 null,
                                                 null,
                                                 null,
                                                 scriptCompilerOptions);

                var editorFirstPass = new TargetAssembly("Assembly-" + languageName + "-Editor-firstpass" + ".dll",
                                                         language,
                                                         AssemblyFlags.EditorOnly | AssemblyFlags.FirstPass,
                                                         TargetAssemblyType.Predefined,
                                                         null,
                                                         FilterAssemblyInFirstpassEditorFolder,
                                                         IsCompatibleWithEditor,
                                                         scriptCompilerOptions)
                {
                    OptionalUnityReferences = OptionalUnityReferences.TestAssemblies,
                };

                var editor = new TargetAssembly("Assembly-" + languageName + "-Editor" + ".dll",
                                                language,
                                                AssemblyFlags.EditorOnly,
                                                TargetAssemblyType.Predefined,
                                                null,
                                                FilterAssemblyInEditorFolder,
                                                IsCompatibleWithEditor,
                                                scriptCompilerOptions)
                {
                    OptionalUnityReferences = OptionalUnityReferences.TestAssemblies,
                };

                runtimeFirstPassAssemblies.Add(runtimeFirstPass);
                runtimeAssemblies.Add(runtime);
                editorFirstPassAssemblies.Add(editorFirstPass);
                editorAssemblies.Add(editor);

                assemblies.Add(runtimeFirstPass);
                assemblies.Add(runtime);
                assemblies.Add(editorFirstPass);
                assemblies.Add(editor);
            }

            // Setup dependencies

            // Runtime assemblies depend all first pass runtime assemblies
            foreach (var assembly in runtimeAssemblies)
            {
                assembly.References.AddRange(runtimeFirstPassAssemblies);
            }

            // First pass editor assemblies depend on all first pass runtime assemblies
            foreach (var assembly in editorFirstPassAssemblies)
            {
                assembly.References.AddRange(runtimeFirstPassAssemblies);
            }

            // Editor assemblies depend on all previous runtime and editor assemblies
            foreach (var assembly in editorAssemblies)
            {
                assembly.References.AddRange(runtimeFirstPassAssemblies);
                assembly.References.AddRange(runtimeAssemblies);
                assembly.References.AddRange(editorFirstPassAssemblies);
            }

            return(assemblies.ToArray());
        }
Esempio n. 23
0
        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();
        }
Esempio n. 24
0
 protected bool Equals(TargetAssembly other)
 {
     return(string.Equals(Filename, other.Filename) && Flags == other.Flags && Type == other.Type);
 }