Ejemplo n.º 1
0
        public static AssemblyDefinition Build(IWeaverLogger logger)
        {
            AssemblyDefinition assembly = null;

            var assemblyBuilder = new AssemblyBuilder(Path.Combine(OutputDirectory, OutputFile), SourceFiles.ToArray())
            {
                referencesOptions = ReferencesOptions.UseEngineModules
            };

            if (AllowUnsafe)
            {
                assemblyBuilder.compilerOptions.AllowUnsafeCode = true;
            }

            assemblyBuilder.buildFinished += delegate(string assemblyPath, CompilerMessage[] compilerMessages)
            {
                CompilerMessages.AddRange(compilerMessages);
                foreach (CompilerMessage cm in compilerMessages)
                {
                    if (cm.type == CompilerMessageType.Error)
                    {
                        Debug.Log($"Error At: {cm.file}:{ cm.line} -- {cm.message}");
                        CompilerErrors = true;
                    }
                }

                Debug.LogWarning($"Test Assembler errors: {CompilerErrors} for {OutputFile}");

                // assembly builder does not call ILPostProcessor (WTF Unity?),  so we must invoke it ourselves.
                var compiledAssembly = new CompiledAssembly(assemblyPath)
                {
                    Defines    = assemblyBuilder.defaultDefines,
                    References = assemblyBuilder.defaultReferences
                };

                var weaver = new Weaver(logger);

                assembly = weaver.Weave(compiledAssembly);
            };

            // Start build of assembly
            if (!assemblyBuilder.Build())
            {
                Debug.LogErrorFormat("Failed to start build of assembly {0}", assemblyBuilder.assemblyPath);
                return(assembly);
            }

            while (assemblyBuilder.status != AssemblyBuilderStatus.Finished)
            {
                System.Threading.Thread.Sleep(10);
            }

            return(assembly);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Builds and Weaves an Assembly with references to unity engine and other asmdefs.
        /// <para>
        ///     NOTE: Does not write the weaved assemble to disk
        /// </para>
        /// </summary>
        public AssemblyDefinition Build(IWeaverLogger logger)
        {
            AssemblyDefinition assembly = null;

            // This will compile scripts with the same references as files in the asset folder.
            // This means that the dll will get references to all asmdef just as if it was the default "Assembly-CSharp.dll"
            var assemblyBuilder = new AssemblyBuilder(ProjectPathFile, sourceFiles.ToArray())
            {
                referencesOptions = ReferencesOptions.UseEngineModules
            };

            assemblyBuilder.buildFinished += delegate(string assemblyPath, CompilerMessage[] compilerMessages)
            {
#if !UNITY_2020_2_OR_NEWER
                CompilerMessages.AddRange(compilerMessages);
                foreach (CompilerMessage cm in compilerMessages)
                {
                    if (cm.type == CompilerMessageType.Error)
                    {
                        Debug.LogErrorFormat("{0}:{1} -- {2}", cm.file, cm.line, cm.message);
                        CompilerErrors = true;
                    }
                }
#endif

                // assembly builder does not call ILPostProcessor (WTF Unity?),  so we must invoke it ourselves.
                var compiledAssembly = new CompiledAssembly(assemblyPath)
                {
                    Defines    = assemblyBuilder.defaultDefines,
                    References = assemblyBuilder.defaultReferences
                };

                var weaver = new Weaver(logger);

                assembly = weaver.Weave(compiledAssembly);
            };

            // Start build of assembly
            if (!assemblyBuilder.Build())
            {
                Debug.LogErrorFormat("Failed to start build of assembly {0}", assemblyBuilder.assemblyPath);
                return(assembly);
            }

            while (assemblyBuilder.status != AssemblyBuilderStatus.Finished)
            {
                System.Threading.Thread.Sleep(10);
            }

            return(assembly);
        }
Ejemplo n.º 3
0
        static void BuildAssembly(bool wait)
        {
            AssemblyBuilder assemblyBuilder = new AssemblyBuilder(OutputDirectory + OutputFile, SourceFiles.ToArray());

            assemblyBuilder.additionalReferences = ReferenceAssemblies.ToArray();
            if (AllowUnsafe)
            {
                assemblyBuilder.compilerOptions.AllowUnsafeCode = true;
            }

            assemblyBuilder.buildStarted += delegate(string assemblyPath)
            {
                //Debug.LogFormat("Assembly build started for {0}", assemblyPath);
            };

            assemblyBuilder.buildFinished += delegate(string assemblyPath, CompilerMessage[] compilerMessages)
            {
                CompilerMessages.AddRange(compilerMessages);
                foreach (CompilerMessage cm in compilerMessages)
                {
                    if (cm.type == CompilerMessageType.Warning)
                    {
                        //Debug.LogWarningFormat("{0}:{1} -- {2}", cm.file, cm.line, cm.message);
                    }
                    else if (cm.type == CompilerMessageType.Error)
                    {
                        Debug.LogErrorFormat("{0}:{1} -- {2}", cm.file, cm.line, cm.message);
                        CompilerErrors = true;
                    }
                }
            };

            // Start build of assembly
            if (!assemblyBuilder.Build())
            {
                Debug.LogErrorFormat("Failed to start build of assembly {0}", assemblyBuilder.assemblyPath);
                return;
            }

            if (wait)
            {
                while (assemblyBuilder.status != AssemblyBuilderStatus.Finished)
                {
                    System.Threading.Thread.Sleep(10);
                }
            }
        }
Ejemplo n.º 4
0
        private void buildFinished(string assemblyPath, CompilerMessage[] compilerMessages)
        {
            Log($"buildFinished for {OutputFile}");

            // in unity2020, ILPP runs automatically. but in unity2021 it does not
#if !UNITY_2020_2_OR_NEWER || UNITY_2021_3_OR_NEWER
            CompilerMessages.AddRange(compilerMessages);
            foreach (CompilerMessage m in compilerMessages)
            {
                if (m.type == CompilerMessageType.Error)
                {
                    // first error
                    if (!CompilerErrors)
                    {
                        LogWarning($"Batch failed!!!");
                    }

                    CompilerErrors = true;
                    LogWarning($"{m.file}:{m.line} -- {m.message}");
                }
            }

            // we can't run weaver if there are compile errors
            if (CompilerErrors)
            {
                return;
            }
#endif

            // call weaver on result
            var compiledAssembly = new CompiledAssembly(assemblyPath, builder);

            Log($"Starting weaver on {OutputFile}");
            var weaver = new Mirage.Weaver.Weaver(logger);
            builtAssembly = weaver.Weave(compiledAssembly);
            Log($"Finished weaver on {OutputFile}");

            // NOTE: we need to write to check for ArgumentException from writing
            TryWriteAssembly(builtAssembly);
        }
Ejemplo n.º 5
0
        public static void Build()
        {
            var assemblyBuilder = new AssemblyBuilder(Path.Combine(OutputDirectory, OutputFile), SourceFiles.ToArray())
            {
                referencesOptions = ReferencesOptions.UseEngineModules
            };

            if (AllowUnsafe)
            {
                assemblyBuilder.compilerOptions.AllowUnsafeCode = true;
            }

            assemblyBuilder.buildFinished += delegate(string assemblyPath, CompilerMessage[] compilerMessages)
            {
                CompilerMessages.AddRange(compilerMessages);
                foreach (CompilerMessage cm in compilerMessages)
                {
                    if (cm.type == CompilerMessageType.Error)
                    {
                        Debug.LogErrorFormat("{0}:{1} -- {2}", cm.file, cm.line, cm.message);
                        CompilerErrors = true;
                    }
                }
            };

            // Start build of assembly
            if (!assemblyBuilder.Build())
            {
                Debug.LogErrorFormat("Failed to start build of assembly {0}", assemblyBuilder.assemblyPath);
                return;
            }

            while (assemblyBuilder.status != AssemblyBuilderStatus.Finished)
            {
                System.Threading.Thread.Sleep(10);
            }
        }
Ejemplo n.º 6
0
        public static void Build(Action <string> OnWarning, Action <string> OnError)
        {
            AssemblyBuilder assemblyBuilder = new AssemblyBuilder(Path.Combine(OutputDirectory, OutputFile), SourceFiles.ToArray())
            {
                // "The type 'MonoBehaviour' is defined in an assembly that is not referenced"
                referencesOptions = ReferencesOptions.UseEngineModules
            };

            if (AllowUnsafe)
            {
                assemblyBuilder.compilerOptions.AllowUnsafeCode = true;
            }

#if UNITY_2020_3_OR_NEWER
            // Unity automatically invokes ILPostProcessor after
            // AssemblyBuilder.Build() (on windows at least. not on mac).
            // => .buildFinished() below CompilerMessages would already contain
            //    the weaver messages, failing tests.
            // => SyncVarTests->SyncVarSyncList fails too if ILPP was
            //    already applied by Unity, and we apply it again.
            //
            // we need to not run ILPP for WeaverTests assemblies here.
            // -> we can't set member variables because Unity creates a new
            //    ILPP instance internally and invokes it
            // -> define is passed through ILPP though, and avoids static state.
            assemblyBuilder.additionalDefines = new [] { ILPostProcessorHook.IgnoreDefine };
#endif

            assemblyBuilder.buildFinished += delegate(string assemblyPath, CompilerMessage[] compilerMessages)
            {
                // CompilerMessages from compiling the original test assembly.
                // note that we can see weaver messages here if Unity runs
                // ILPostProcessor after AssemblyBuilder.Build().
                // => that's why we pass the ignore define above.
                CompilerMessages.AddRange(compilerMessages);
                foreach (CompilerMessage cm in compilerMessages)
                {
                    if (cm.type == CompilerMessageType.Error)
                    {
                        Debug.LogError($"{cm.file}:{cm.line} -- {cm.message}");
                        CompilerErrors = true;
                    }
                }

#if UNITY_2020_3_OR_NEWER
                // on 2018/2019, CompilationFinishedHook weaves after building.
                // on 2020, ILPostProcessor weaves after building.
                //   on windows, it runs after AssemblyBuilder.Build()
                //   on mac, it does not run after AssemblyBuidler.Build()
                // => run it manually in all cases
                // => this way we can feed result.Logs to test results too
                // NOTE: we could simply call Weaver.Weave() here.
                //       but let's make all tests run through ILPP.
                //       just like regular projects would.
                //       helps catch issues early.

                // copy references from assemblyBuilder's references
                List <string> references = new List <string>();
                if (assemblyBuilder.defaultReferences != null)
                {
                    references.AddRange(assemblyBuilder.defaultReferences);
                }
                if (assemblyBuilder.additionalReferences != null)
                {
                    references.AddRange(assemblyBuilder.additionalReferences);
                }

                // invoke ILPostProcessor with an assembly from file.
                // NOTE: code for creating and invoking the ILPostProcessor has
                //       to be in Weaver.dll where 'CompilationPipeline' is
                //       available due to name being of form 'Unity.*.CodeGen'.
                //       => we can't change tests to that Unity.*.CodeGen
                //          because some tests need to be weaved, but ILPP isn't
                //          ran on Unity.*.CodeGen assemblies itself.
                ILPostProcessorFromFile.ILPostProcessFile(assemblyPath, references.ToArray(), OnWarning, OnError);
#endif
            };

            // Start build of assembly
            if (!assemblyBuilder.Build())
            {
                Debug.LogError($"Failed to start build of assembly {assemblyBuilder.assemblyPath}");
                return;
            }

            while (assemblyBuilder.status != AssemblyBuilderStatus.Finished)
            {
                Thread.Sleep(10);
            }
        }