Ejemplo n.º 1
0
        public void WalkTypeInfosInEmptyLibrary()
        {
            MockTypeLib typeLib = new MockTypeLib();

            ComDependencyWalker walker = new ComDependencyWalker(new MarshalReleaseComObject(MockReleaseComObject));
            walker.AnalyzeTypeLibrary(typeLib);
            Assert.Equal(0, walker.GetDependencies().GetLength(0));

            typeLib.AssertAllHandlesReleased();
        }
Ejemplo n.º 2
0
        private TYPELIBATTR[] RunDependencyWalker(MockTypeLib mainTypeLib, MockTypeLib dependencyTypeLib, bool dependencyShouldBePresent)
        {
            ComDependencyWalker walker = new ComDependencyWalker(new MarshalReleaseComObject(MockReleaseComObject));
            walker.AnalyzeTypeLibrary(mainTypeLib);

            TYPELIBATTR[] dependencies = walker.GetDependencies();

            // types from the main type library should be in the dependency list
            AssertDependenciesContainTypeLib(dependencies, mainTypeLib, true);

            AssertDependenciesContainTypeLib(dependencies, dependencyTypeLib, dependencyShouldBePresent);

            mainTypeLib.AssertAllHandlesReleased();
            dependencyTypeLib.AssertAllHandlesReleased();

            return dependencies;
        }
 private List<string> ScanAndResolveAllDependencies(ComDependencyWalker dependencyWalker, ComReferenceInfo reference)
 {
     dependencyWalker.ClearDependencyList();
     base.Log.LogMessageFromResources(MessageImportance.Low, "ResolveComReference.ScanningDependencies", new object[] { reference.SourceItemSpec });
     dependencyWalker.AnalyzeTypeLibrary(reference.typeLibPointer);
     foreach (Exception exception in dependencyWalker.EncounteredProblems)
     {
         base.Log.LogWarningWithCodeFromResources("ResolveComReference.FailedToScanDependencies", new object[] { reference.SourceItemSpec, exception.Message });
     }
     dependencyWalker.EncounteredProblems.Clear();
     HashSet<string> source = new HashSet<string>();
     foreach (System.Runtime.InteropServices.ComTypes.TYPELIBATTR typelibattr in dependencyWalker.GetDependencies())
     {
         if (!ComReference.AreTypeLibAttrEqual(typelibattr, reference.attr))
         {
             ComReferenceInfo info;
             if (this.IsExistingProjectReference(typelibattr, null, out info))
             {
                 ITaskItem item;
                 dependencyWalker.ClearAnalyzedTypeCache();
                 if (this.ResolveReference(dependencyWalker, info, this.WrapperOutputDirectory, out item))
                 {
                     source.Add(item.ItemSpec);
                     foreach (string str in info.dependentWrapperPaths)
                     {
                         source.Add(str);
                     }
                 }
             }
             else
             {
                 ComReferenceWrapperInfo info2;
                 base.Log.LogMessageFromResources(MessageImportance.Low, "ResolveComReference.ResolvingDependency", new object[] { typelibattr.guid, typelibattr.wMajorVerNum, typelibattr.wMinorVerNum });
                 ((IComReferenceResolver) this).ResolveComClassicReference(typelibattr, this.WrapperOutputDirectory, null, null, out info2);
                 base.Log.LogMessageFromResources(MessageImportance.Low, "ResolveComReference.ResolvedDependentComReference", new object[] { typelibattr.guid, typelibattr.wMajorVerNum, typelibattr.wMinorVerNum, info2.path });
                 source.Add(info2.path);
             }
         }
     }
     return source.ToList<string>();
 }
 private bool ResolveReferenceAndAddToList(ComDependencyWalker dependencyWalker, ComReferenceInfo projectRefInfo, ArrayList resolvedReferenceList, ArrayList moduleList)
 {
     ITaskItem item;
     if (!this.ResolveReference(dependencyWalker, projectRefInfo, this.WrapperOutputDirectory, out item))
     {
         return false;
     }
     resolvedReferenceList.Add(item);
     bool metadataFound = false;
     bool flag2 = MetadataConversionUtilities.TryConvertItemMetadataToBool(projectRefInfo.taskItem, "Isolated", out metadataFound);
     if (metadataFound && flag2)
     {
         string typeLibPath = projectRefInfo.typeLibPath;
         if (typeLibPath == null)
         {
             return false;
         }
         ITaskItem item2 = new TaskItem(typeLibPath);
         item2.SetMetadata("Name", projectRefInfo.taskItem.ItemSpec);
         moduleList.Add(item2);
     }
     return true;
 }
 internal bool ResolveReference(ComDependencyWalker dependencyWalker, ComReferenceInfo referenceInfo, string outputDirectory, out ITaskItem referencePathItem)
 {
     if (referenceInfo.referencePathItem == null)
     {
         ComReferenceWrapperInfo info;
         base.Log.LogMessageFromResources(MessageImportance.Low, "ResolveComReference.Resolving", new object[] { referenceInfo.taskItem.ItemSpec, referenceInfo.taskItem.GetMetadata("WrapperTool") });
         List<string> list = this.ScanAndResolveAllDependencies(dependencyWalker, referenceInfo);
         referenceInfo.dependentWrapperPaths = list;
         referencePathItem = new TaskItem();
         referenceInfo.referencePathItem = referencePathItem;
         if (this.ResolveComClassicReference(referenceInfo, outputDirectory, referenceInfo.taskItem.GetMetadata("WrapperTool"), referenceInfo.taskItem.ItemSpec, true, referenceInfo.dependentWrapperPaths, out info))
         {
             referencePathItem.ItemSpec = info.path;
             referenceInfo.taskItem.CopyMetadataTo(referencePathItem);
             string fullName = AssemblyName.GetAssemblyName(info.path).FullName;
             referencePathItem.SetMetadata("FusionName", fullName);
             base.Log.LogMessageFromResources(MessageImportance.Low, "ResolveComReference.ResolvedReference", new object[] { referenceInfo.taskItem.ItemSpec, info.path });
             return true;
         }
         base.Log.LogWarningWithCodeFromResources("ResolveComReference.CannotFindWrapperForTypeLib", new object[] { referenceInfo.taskItem.ItemSpec });
         return false;
     }
     bool flag = !string.IsNullOrEmpty(referenceInfo.referencePathItem.ItemSpec);
     referencePathItem = referenceInfo.referencePathItem;
     return flag;
 }
 public override bool Execute()
 {
     bool flag2;
     if (!this.VerifyAndInitializeInputs())
     {
         return false;
     }
     if (!this.ComputePathToAxImp() || !this.ComputePathToTlbImp())
     {
         return false;
     }
     this.allProjectRefs = new List<ComReferenceInfo>();
     this.allDependencyRefs = new List<ComReferenceInfo>();
     this.timestampCache = (ResolveComReferenceCache) StateFileBase.DeserializeCache(this.StateFile, base.Log, typeof(ResolveComReferenceCache));
     if ((this.timestampCache == null) || ((this.timestampCache != null) && !this.timestampCache.ToolPathsMatchCachePaths(this.tlbimpPath, this.aximpPath)))
     {
         base.Log.LogMessageFromResources(MessageImportance.Low, "ResolveComReference.NotUsingCacheFile", new object[] { (this.StateFile == null) ? string.Empty : this.StateFile });
         this.timestampCache = new ResolveComReferenceCache(this.tlbimpPath, this.aximpPath);
     }
     else
     {
         base.Log.LogMessageFromResources(MessageImportance.Low, "ResolveComReference.UsingCacheFile", new object[] { (this.StateFile == null) ? string.Empty : this.StateFile });
     }
     try
     {
         this.ConvertAttrReferencesToComReferenceInfo(this.allProjectRefs, this.TypeLibNames);
         this.ConvertFileReferencesToComReferenceInfo(this.allProjectRefs, this.TypeLibFiles);
         this.AddMissingTlbReferences();
         this.CheckForConflictingReferences();
         this.SetFrameworkVersionFromString(this.projectTargetFrameworkAsString);
         ArrayList moduleList = new ArrayList();
         ArrayList resolvedReferenceList = new ArrayList();
         ComDependencyWalker dependencyWalker = new ComDependencyWalker(new MarshalReleaseComObject(Marshal.ReleaseComObject));
         bool flag = true;
         for (int i = 0; i < 4; i++)
         {
             foreach (ComReferenceInfo info in this.allProjectRefs)
             {
                 string metadata = info.taskItem.GetMetadata("WrapperTool");
                 if ((((i == 0) && ComReferenceTypes.IsPia(metadata)) || ((i == 1) && ComReferenceTypes.IsTlbImp(metadata))) || (((i == 2) && ComReferenceTypes.IsPiaOrTlbImp(metadata)) || ((i == 3) && ComReferenceTypes.IsAxImp(metadata))))
                 {
                     try
                     {
                         if (!this.ResolveReferenceAndAddToList(dependencyWalker, info, resolvedReferenceList, moduleList))
                         {
                             flag = false;
                         }
                     }
                     catch (ComReferenceResolutionException)
                     {
                     }
                     catch (StrongNameException)
                     {
                         return false;
                     }
                     catch (FileLoadException exception)
                     {
                         if (!this.DelaySign)
                         {
                             throw;
                         }
                         base.Log.LogErrorWithCodeFromResources(null, info.SourceItemSpec, 0, 0, 0, 0, "ResolveComReference.LoadingDelaySignedAssemblyWithStrongNameVerificationEnabled", new object[] { exception.Message });
                         return false;
                     }
                     catch (ArgumentException exception2)
                     {
                         base.Log.LogErrorWithCodeFromResources("General.InvalidArgument", new object[] { exception2.Message });
                         return false;
                     }
                     catch (SystemException exception3)
                     {
                         base.Log.LogErrorWithCodeFromResources("ResolveComReference.FailedToResolveComReference", new object[] { info.attr.guid, info.attr.wMajorVerNum, info.attr.wMinorVerNum, exception3.Message });
                     }
                 }
             }
         }
         this.SetCopyLocalToFalseOnGacOrNoPIAAssemblies(resolvedReferenceList, GlobalAssemblyCache.GetGacPath());
         this.ResolvedModules = (ITaskItem[]) moduleList.ToArray(typeof(ITaskItem));
         this.ResolvedFiles = (ITaskItem[]) resolvedReferenceList.ToArray(typeof(ITaskItem));
         flag2 = flag && !base.Log.HasLoggedErrors;
     }
     finally
     {
         if ((this.timestampCache != null) && this.timestampCache.Dirty)
         {
             this.timestampCache.SerializeCache(this.StateFile, base.Log);
         }
         this.Cleanup();
     }
     return flag2;
 }
Ejemplo n.º 7
0
        private void RunDependencyWalkerFaultInjection(MockTypeLibrariesFailurePoints failurePoint, MockTypeLib mainTypeLib, MockTypeLib dependencyTypeLibGood1, MockTypeLib dependencyTypeLibBad1, MockTypeLib dependencyTypeLibGood2, MockTypeLib dependencyTypeLibBad2)
        {
            ComDependencyWalker walker = new ComDependencyWalker(new MarshalReleaseComObject(MockReleaseComObject));
            walker.AnalyzeTypeLibrary(mainTypeLib);

            // Did the current failure point get hit for this test? If not then no point in checking anything
            // The previous test (FaultInjectionMainLib) ensures that all defined failure points actually 
            // cause some sort of trouble
            if (walker.EncounteredProblems.Count > 0)
            {
                TYPELIBATTR[] dependencies = walker.GetDependencies();
                AssertDependenciesContainTypeLib("Test failed for failure point " + failurePoint.ToString(),
                    dependencies, mainTypeLib, true);
                AssertDependenciesContainTypeLib("Test failed for failure point " + failurePoint.ToString(),
                    dependencies, dependencyTypeLibGood1, true);
                AssertDependenciesContainTypeLib("Test failed for failure point " + failurePoint.ToString(),
                    dependencies, dependencyTypeLibGood2, true);
                AssertDependenciesContainTypeLib("Test failed for failure point " + failurePoint.ToString(),
                    dependencies, dependencyTypeLibBad1, false);
                AssertDependenciesContainTypeLib("Test failed for failure point " + failurePoint.ToString(),
                    dependencies, dependencyTypeLibBad2, false);
            }

            mainTypeLib.AssertAllHandlesReleased();
            dependencyTypeLibGood1.AssertAllHandlesReleased();
            dependencyTypeLibGood2.AssertAllHandlesReleased();
            dependencyTypeLibBad1.AssertAllHandlesReleased();
            dependencyTypeLibBad2.AssertAllHandlesReleased();
        }
Ejemplo n.º 8
0
        public void FullDependenciesWithIncrementalAnalysis()
        {
            MockTypeLib mainTypeLib1, mainTypeLib2, mainTypeLib3, dependencyTypeLib1, dependencyTypeLib2, dependencyTypeLib3;
            CreateTwoTypeLibs(out mainTypeLib1, out dependencyTypeLib1);
            CreateTwoTypeLibs(out mainTypeLib2, out dependencyTypeLib2);
            CreateTwoTypeLibs(out mainTypeLib3, out dependencyTypeLib3);

            mainTypeLib1.ContainedTypeInfos[0].DefinesVariable(dependencyTypeLib1.ContainedTypeInfos[0]);

            mainTypeLib2.ContainedTypeInfos[0].DefinesVariable(dependencyTypeLib1.ContainedTypeInfos[0]);
            mainTypeLib2.ContainedTypeInfos[0].DefinesVariable(dependencyTypeLib2.ContainedTypeInfos[0]);

            mainTypeLib3.ContainedTypeInfos[0].DefinesVariable(dependencyTypeLib1.ContainedTypeInfos[0]);
            mainTypeLib3.ContainedTypeInfos[0].DefinesVariable(dependencyTypeLib3.ContainedTypeInfos[0]);

            ComDependencyWalker walker = new ComDependencyWalker(MockReleaseComObject);

            walker.AnalyzeTypeLibrary(mainTypeLib1);
            TYPELIBATTR[] dependencies = walker.GetDependencies();
            ICollection<string> analyzedTypes = walker.GetAnalyzedTypeNames();

            AssertDependenciesContainTypeLib(dependencies, dependencyTypeLib1, true);
            AssertDependenciesContainTypeLib(dependencies, dependencyTypeLib2, false);
            AssertDependenciesContainTypeLib(dependencies, dependencyTypeLib3, false);
            Assert.Equal(2, analyzedTypes.Count);

            walker.ClearDependencyList();
            walker.AnalyzeTypeLibrary(mainTypeLib2);
            dependencies = walker.GetDependencies();
            analyzedTypes = walker.GetAnalyzedTypeNames();

            AssertDependenciesContainTypeLib(dependencies, dependencyTypeLib1, true);
            AssertDependenciesContainTypeLib(dependencies, dependencyTypeLib2, true);
            AssertDependenciesContainTypeLib(dependencies, dependencyTypeLib3, false);
            Assert.Equal(4, analyzedTypes.Count);

            walker.ClearDependencyList();
            walker.AnalyzeTypeLibrary(mainTypeLib3);
            dependencies = walker.GetDependencies();
            analyzedTypes = walker.GetAnalyzedTypeNames();

            AssertDependenciesContainTypeLib(dependencies, dependencyTypeLib1, true);
            AssertDependenciesContainTypeLib(dependencies, dependencyTypeLib2, false);
            AssertDependenciesContainTypeLib(dependencies, dependencyTypeLib3, true);
            Assert.Equal(6, analyzedTypes.Count);
        }
Ejemplo n.º 9
0
        public void FaultInjectionMainLib()
        {
            // The primary test here is that we don't throw, which can't be explicitly expressed in NUnit...
            // other asserts are secondary
            foreach (MockTypeLibrariesFailurePoints failurePoint in Enum.GetValues(typeof(MockTypeLibrariesFailurePoints)))
            {
                MockTypeLib mainTypeLib = new MockTypeLib();
                mainTypeLib.AddTypeInfo(new MockTypeInfo());

                // Make it the StdOle lib to exercise the ITypeInfo.GetDocumentation failure point
                MockTypeLib dependencyTypeLib = new MockTypeLib(NativeMethods.IID_StdOle);
                dependencyTypeLib.AddTypeInfo(new MockTypeInfo());

                COMException failureException = new COMException("unhandled exception in " + failurePoint.ToString());
                mainTypeLib.InjectFailure(failurePoint, failureException);
                dependencyTypeLib.InjectFailure(failurePoint, failureException);

                mainTypeLib.ContainedTypeInfos[0].ImplementsInterface(dependencyTypeLib.ContainedTypeInfos[0]);
                mainTypeLib.ContainedTypeInfos[0].DefinesVariable(dependencyTypeLib.ContainedTypeInfos[0]);
                mainTypeLib.ContainedTypeInfos[0].DefinesFunction(
                    new MockTypeInfo[] { dependencyTypeLib.ContainedTypeInfos[0] }, dependencyTypeLib.ContainedTypeInfos[0]);

                ComDependencyWalker walker = new ComDependencyWalker(new MarshalReleaseComObject(MockReleaseComObject));
                walker.AnalyzeTypeLibrary(mainTypeLib);

                Assert.Equal(1, walker.EncounteredProblems.Count); // "Test failed for failure point " + failurePoint.ToString()
                Assert.Equal(failureException, walker.EncounteredProblems[0]); // "Test failed for failure point " + failurePoint.ToString()

                mainTypeLib.AssertAllHandlesReleased();
                dependencyTypeLib.AssertAllHandlesReleased();
            }
        }
Ejemplo n.º 10
0
        /*
         * Method:  ResolveReference
         * 
         * Helper COM resolution method. Creates an appropriate helper class for the given tool and calls 
         * the Resolve method on it.
         */
        internal bool ResolveReference(ComDependencyWalker dependencyWalker, ComReferenceInfo referenceInfo, string outputDirectory, out ITaskItem referencePathItem)
        {
            if (referenceInfo.referencePathItem == null)
            {
                if (!Silent)
                {
                    Log.LogMessageFromResources(MessageImportance.Low, "ResolveComReference.Resolving", referenceInfo.taskItem.ItemSpec, referenceInfo.taskItem.GetMetadata(ComReferenceItemMetadataNames.wrapperTool));
                }

                List<string> dependencyPaths = ScanAndResolveAllDependencies(dependencyWalker, referenceInfo);

                referenceInfo.dependentWrapperPaths = dependencyPaths;
                referencePathItem = new TaskItem();
                referenceInfo.referencePathItem = referencePathItem;

                ComReferenceWrapperInfo wrapperInfo;

                if (ResolveComClassicReference(referenceInfo, outputDirectory,
                    referenceInfo.taskItem.GetMetadata(ComReferenceItemMetadataNames.wrapperTool),
                    referenceInfo.taskItem.ItemSpec, true, referenceInfo.dependentWrapperPaths, out wrapperInfo))
                {
                    referencePathItem.ItemSpec = wrapperInfo.path;
                    referenceInfo.taskItem.CopyMetadataTo(referencePathItem);

                    string fusionName = AssemblyName.GetAssemblyName(wrapperInfo.path).FullName;
                    referencePathItem.SetMetadata(ItemMetadataNames.fusionName, fusionName);

                    if (!Silent)
                    {
                        Log.LogMessageFromResources(MessageImportance.Low, "ResolveComReference.ResolvedReference", referenceInfo.taskItem.ItemSpec, wrapperInfo.path);
                    }

                    return true;
                }

                if (!Silent)
                {
                    Log.LogWarningWithCodeFromResources("ResolveComReference.CannotFindWrapperForTypeLib", referenceInfo.taskItem.ItemSpec);
                }

                return false;
            }
            else
            {
                bool successfullyResolved = !String.IsNullOrEmpty(referenceInfo.referencePathItem.ItemSpec);
                referencePathItem = referenceInfo.referencePathItem;

                return successfullyResolved;
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Resolves the COM reference, and adds it to the appropriate item list.
        /// </summary>
        /// <param name="projectRefInfo"></param>
        /// <param name="resolvedReferenceList"></param>
        /// <param name="moduleList"></param>
        /// <returns></returns>
        private bool ResolveReferenceAndAddToList
            (
            ComDependencyWalker dependencyWalker,
            ComReferenceInfo projectRefInfo,
            ArrayList resolvedReferenceList,
            ArrayList moduleList
            )
        {
            ITaskItem referencePath;

            if (ResolveReference(dependencyWalker, projectRefInfo, WrapperOutputDirectory, out referencePath))
            {
                resolvedReferenceList.Add(referencePath);

                bool metadataFound = false;
                bool isolated = MetadataConversionUtilities.TryConvertItemMetadataToBool(projectRefInfo.taskItem, "Isolated", out metadataFound);

                if (metadataFound && isolated)
                {
                    string modulePath = projectRefInfo.strippedTypeLibPath;
                    if (modulePath != null)
                    {
                        ITaskItem moduleItem = new TaskItem(modulePath);
                        moduleItem.SetMetadata("Name", projectRefInfo.taskItem.ItemSpec);
                        moduleList.Add(moduleItem);
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            else
            {
                return false;
            }

            return true;
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Task entry point.
        /// </summary>
        /// <returns></returns>
        public override bool Execute()
        {
            if (!VerifyAndInitializeInputs())
            {
                return false;
            }

            if (!ComputePathToAxImp() || !ComputePathToTlbImp())
            {
                // unable to compute the path to tlbimp.exe, aximp.exe, or both and that is necessary to 
                // continue forward, so return now.
                return false;
            }

            allProjectRefs = new List<ComReferenceInfo>();
            allDependencyRefs = new List<ComReferenceInfo>();

            _timestampCache = (ResolveComReferenceCache)StateFileBase.DeserializeCache(StateFile, Log, typeof(ResolveComReferenceCache));

            if (_timestampCache == null || (_timestampCache != null && !_timestampCache.ToolPathsMatchCachePaths(_tlbimpPath, _aximpPath)))
            {
                if (!Silent)
                {
                    Log.LogMessageFromResources(MessageImportance.Low, "ResolveComReference.NotUsingCacheFile", StateFile == null ? String.Empty : StateFile);
                }

                _timestampCache = new ResolveComReferenceCache(_tlbimpPath, _aximpPath);
            }
            else if (!Silent)
            {
                Log.LogMessageFromResources(MessageImportance.Low, "ResolveComReference.UsingCacheFile", StateFile == null ? String.Empty : StateFile);
            }

            try
            {
                ConvertAttrReferencesToComReferenceInfo(allProjectRefs, TypeLibNames);
                ConvertFileReferencesToComReferenceInfo(allProjectRefs, TypeLibFiles);

                // add missing tlbimp references for aximp ones
                AddMissingTlbReferences();

                // see if we have any typelib name clashes. Ignore the return value - we now remove the conflicting refs
                // and continue (first one wins)
                CheckForConflictingReferences();

                SetFrameworkVersionFromString(_projectTargetFrameworkAsString);

                // Process each task item. If one of them fails we still process the rest of them, but
                // remember that the task should return failure.
                // DESIGN CHANGE: we no longer fail the task when one or more references fail to resolve. 
                // Unless we experience a catastrophic failure, we'll log warnings for those refs and proceed
                // (and return success)
                ArrayList moduleList = new ArrayList();
                ArrayList resolvedReferenceList = new ArrayList();

                ComDependencyWalker dependencyWalker = new ComDependencyWalker(Marshal.ReleaseComObject);
                bool allReferencesResolvedSuccessfully = true;
                for (int pass = 0; pass < 4; pass++)
                {
                    foreach (ComReferenceInfo projectRefInfo in allProjectRefs)
                    {
                        string wrapperType = projectRefInfo.taskItem.GetMetadata(ComReferenceItemMetadataNames.wrapperTool);

                        // first resolve all PIA refs, then regular tlb refs and finally ActiveX refs
                        if ((pass == 0 && ComReferenceTypes.IsPia(wrapperType)) ||
                            (pass == 1 && ComReferenceTypes.IsTlbImp(wrapperType)) ||
                            (pass == 2 && ComReferenceTypes.IsPiaOrTlbImp(wrapperType)) ||
                            (pass == 3 && ComReferenceTypes.IsAxImp(wrapperType)))
                        {
                            try
                            {
                                if (!this.ResolveReferenceAndAddToList(dependencyWalker, projectRefInfo, resolvedReferenceList, moduleList))
                                {
                                    allReferencesResolvedSuccessfully = false;
                                }
                            }
                            catch (ComReferenceResolutionException)
                            {
                                // problem resolving this reference? continue so that we can display all error messages
                            }
                            catch (StrongNameException)
                            {
                                // key extraction problem? No point in continuing, since all wrappers will hit the same problem.
                                // error message has already been logged
                                return false;
                            }
                            catch (FileLoadException ex)
                            {
                                // This exception is thrown when we try to load a delay signed assembly without disabling
                                // strong name verification first. So print a nice information if we're generating 
                                // delay signed wrappers, otherwise rethrow, since it's an unexpected exception.
                                if (DelaySign)
                                {
                                    Log.LogErrorWithCodeFromResources(null, projectRefInfo.SourceItemSpec, 0, 0, 0, 0, "ResolveComReference.LoadingDelaySignedAssemblyWithStrongNameVerificationEnabled", ex.Message);

                                    // no point in printing the same thing multiple times...
                                    return false;
                                }
                                else
                                {
                                    Debug.Assert(false, "Unexpected exception in ResolveComReference.Execute. " +
                                        "Please log a MSBuild bug specifying the steps to reproduce the problem.");
                                    throw;
                                }
                            }
                            catch (ArgumentException ex)
                            {
                                // This exception is thrown when we try to convert some of the Metadata from the project
                                // file and the conversion fails.  Most likely, the user needs to correct a type in the 
                                // project file.
                                Log.LogErrorWithCodeFromResources("General.InvalidArgument", ex.Message);
                                return false;
                            }
                            catch (SystemException ex)
                            {
                                Log.LogErrorWithCodeFromResources("ResolveComReference.FailedToResolveComReference",
                                    projectRefInfo.attr.guid, projectRefInfo.attr.wMajorVerNum, projectRefInfo.attr.wMinorVerNum,
                                    ex.Message);
                            }
                        }
                    }
                }

                SetCopyLocalToFalseOnGacOrNoPIAAssemblies(resolvedReferenceList, GlobalAssemblyCache.GetGacPath());

                ResolvedModules = (ITaskItem[])moduleList.ToArray(typeof(ITaskItem));
                ResolvedFiles = (ITaskItem[])resolvedReferenceList.ToArray(typeof(ITaskItem));

                // The Logs from AxImp and TlbImp aren't part of our log, but if the task failed, it will return false from 
                // GenerateWrapper, which should get passed all the way back up here.  
                return allReferencesResolvedSuccessfully && !Log.HasLoggedErrors;
            }
            finally
            {
                if ((_timestampCache != null) && _timestampCache.Dirty)
                {
                    _timestampCache.SerializeCache(StateFile, Log);
                }

                Cleanup();
            }
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Scan all the dependencies of the main project references and preresolve them
        /// so that when we get asked about a previously unknown dependency in the form of a .NET assembly 
        /// we know what to do with it.
        /// </summary>
        private List<string> ScanAndResolveAllDependencies(ComDependencyWalker dependencyWalker, ComReferenceInfo reference)
        {
            dependencyWalker.ClearDependencyList();

            if (!Silent)
            {
                Log.LogMessageFromResources(MessageImportance.Low, "ResolveComReference.ScanningDependencies", reference.SourceItemSpec);
            }

            dependencyWalker.AnalyzeTypeLibrary(reference.typeLibPointer);

            if (!Silent)
            {
                foreach (Exception ex in dependencyWalker.EncounteredProblems)
                {
                    // A failure to resolve a reference due to something possibly being missing from disk is not
                    // an error; the user may not be actually consuming types from it
                    Log.LogWarningWithCodeFromResources("ResolveComReference.FailedToScanDependencies",
                        reference.SourceItemSpec, ex.Message);
                }
            }

            dependencyWalker.EncounteredProblems.Clear();

            HashSet<string> dependentPaths = new HashSet<string>();
            TYPELIBATTR[] dependentAttrs = dependencyWalker.GetDependencies();

            foreach (TYPELIBATTR dependencyTypeLibAttr in dependentAttrs)
            {
                // We don't need to even try to resolve if the dependency reference is ourselves. 
                if (!ComReference.AreTypeLibAttrEqual(dependencyTypeLibAttr, reference.attr))
                {
                    ComReferenceInfo existingReference;

                    if (IsExistingProjectReference(dependencyTypeLibAttr, null, out existingReference))
                    {
                        ITaskItem resolvedItem;

                        // If we're resolving another project reference, empty out the type cache -- if the dependencies are buried,
                        // caching the analyzed types can make it so that we don't recognize our dependencies' dependencies. 
                        dependencyWalker.ClearAnalyzedTypeCache();

                        if (ResolveReference(dependencyWalker, existingReference, WrapperOutputDirectory, out resolvedItem))
                        {
                            // Add the resolved dependency
                            dependentPaths.Add(resolvedItem.ItemSpec);

                            // and anything it depends on 
                            foreach (string dependentPath in existingReference.dependentWrapperPaths)
                            {
                                dependentPaths.Add(dependentPath);
                            }
                        }
                    }
                    else
                    {
                        if (!Silent)
                        {
                            Log.LogMessageFromResources(MessageImportance.Low, "ResolveComReference.ResolvingDependency",
                                dependencyTypeLibAttr.guid, dependencyTypeLibAttr.wMajorVerNum, dependencyTypeLibAttr.wMinorVerNum);
                        }

                        ComReferenceWrapperInfo wrapperInfo;

                        ((IComReferenceResolver)this).ResolveComClassicReference(dependencyTypeLibAttr, WrapperOutputDirectory,
                            null /* unknown wrapper type */, null /* unknown name */, out wrapperInfo);

                        if (!Silent)
                        {
                            Log.LogMessageFromResources(MessageImportance.Low, "ResolveComReference.ResolvedDependentComReference",
                                dependencyTypeLibAttr.guid, dependencyTypeLibAttr.wMajorVerNum, dependencyTypeLibAttr.wMinorVerNum,
                                wrapperInfo.path);
                        }

                        dependentPaths.Add(wrapperInfo.path);
                    }
                }
            }

            return dependentPaths.ToList<string>();
        }