Ejemplo n.º 1
0
        public override bool Execute()
        {
            var allNative = new Dictionary <string, ITaskItem> ();

            foreach (var n in NativeDepsPaths)
            {
                var fileName = Path.GetFileName(n.ItemSpec);
                if (!allNative.ContainsKey(fileName))
                {
                    allNative.Add(fileName, n);
                }
            }
            var keptNative        = new List <ITaskItem> ();
            var managedAssemblies = ManagedAssemblyPaths.Select(i => i.ItemSpec).ToArray();

            foreach (string managedAssembly in managedAssemblies)
            {
                using (var peReader = new PEReader(new FileStream(managedAssembly, FileMode.Open, FileAccess.Read, FileShare.Read)))
                {
                    if (peReader.HasMetadata)
                    {
                        var reader = peReader.GetMetadataReader();
                        for (int i = 1, count = reader.GetTableRowCount(TableIndex.ModuleRef); i <= count; i++)
                        {
                            var moduleRef  = reader.GetModuleReference(MetadataTokens.ModuleReferenceHandle(i));
                            var moduleName = reader.GetString(moduleRef.Name);

                            var moduleRefCandidates = new[] { moduleName, moduleName + ".dll", moduleName + ".so", moduleName + ".dylib" };

                            ITaskItem referencedNativeFile = null;
                            foreach (string moduleRefCandidate in moduleRefCandidates)
                            {
                                if (allNative.TryGetValue(moduleRefCandidate, out referencedNativeFile))
                                {
                                    break;
                                }
                            }

                            if (referencedNativeFile != null)
                            {
                                keptNative.Add(referencedNativeFile);
                            }
                            else
                            {
                                // DLLImport that wasn't satisfied
                                Log.LogMessage(MessageImportance.High, "unsatisfied DLLImport: " + managedAssembly + " -> " + moduleName);
                            }
                        }
                    }
                }
            }

            foreach (var n in NativeDepsToKeep)
            {
                ITaskItem nativeFile = null;
                if (allNative.TryGetValue(n.ItemSpec, out nativeFile))
                {
                    keptNative.Add(nativeFile);
                }
            }
            KeptNativeDepsPaths = keptNative.ToArray();
            return(true);
        }
Ejemplo n.º 2
0
        public override bool Execute()
        {
            var allNativeNames = new HashSet <string> ();

            foreach (var nativeDep in NativeDepsPaths)
            {
                allNativeNames.Add(Path.GetFileName(nativeDep.ItemSpec));
            }
            var keptNativeNames = new HashSet <string> ();

            foreach (var nativeDep in NativeDepsToKeep)
            {
                keptNativeNames.Add(Path.GetFileName(nativeDep.ItemSpec));
            }

            var managedAssemblies = ManagedAssemblyPaths.Select(i => i.ItemSpec).ToArray();

            foreach (string managedAssembly in managedAssemblies)
            {
                using (var peReader = new PEReader(new FileStream(managedAssembly, FileMode.Open, FileAccess.Read, FileShare.Read))) {
                    if (peReader.HasMetadata)
                    {
                        var reader = peReader.GetMetadataReader();
                        for (int i = 1, count = reader.GetTableRowCount(TableIndex.ModuleRef); i <= count; i++)
                        {
                            var moduleRef  = reader.GetModuleReference(MetadataTokens.ModuleReferenceHandle(i));
                            var moduleName = reader.GetString(moduleRef.Name);

                            var moduleRefCandidates = new [] { moduleName, moduleName + ".dll", moduleName + ".so", moduleName + ".dylib" };

                            bool foundModuleRef = false;
                            foreach (string moduleRefCandidate in moduleRefCandidates)
                            {
                                if (allNativeNames.Contains(moduleRefCandidate))
                                {
                                    keptNativeNames.Add(moduleRefCandidate);
                                    foundModuleRef = true;
                                }
                            }

                            if (!foundModuleRef)
                            {
                                Log.LogMessage("unsatisfied DLLImport: " + managedAssembly + " -> " + moduleName);
                            }
                        }
                    }
                }
            }

            var keptNativeDeps = new List <ITaskItem> ();

            foreach (var nativeDep in NativeDepsPaths)
            {
                var fileName = Path.GetFileName(nativeDep.ItemSpec);
                if (keptNativeNames.Contains(fileName))
                {
                    keptNativeDeps.Add(nativeDep);
                }
            }

            KeptNativeDepsPaths = keptNativeDeps.ToArray();
            return(true);
        }