public List <ModuleDeclaration> ReflectToModules(IEnumerable <string> includeDirectories, IEnumerable <string> libraryDirectories, string extraArgs, params string [] moduleNames) { var moduleNameAggregate = moduleNames.Aggregate((s1, s2) => s1 + s2); var pathName = tempDirectory.UniquePath(moduleNameAggregate, null, "xml"); includeDirectories = includeDirectories ?? new string [] { tempDirectory.DirectoryPath }; libraryDirectories = libraryDirectories ?? new string [] { tempDirectory.DirectoryPath }; var modulesInLibraries = SwiftModuleFinder.FindModuleNames(libraryDirectories, CompilerInfo.Target); List <ISwiftModuleLocation> locations = SwiftModuleFinder.GatherAllReferencedModules(modulesInLibraries, includeDirectories, CompilerInfo.Target); string output = ""; try { output = Reflect(locations.Select(loc => loc.DirectoryPath), libraryDirectories, pathName, extraArgs, moduleNames); } finally { locations.DisposeAll(); } ThrowOnCompilerVersionMismatch(output, moduleNames); return(Reflector.FromXmlFile(pathName)); }
public Tuple <string, HashSet <string> > CompileWrappers(string [] inputLibraryDirectories, string [] inputModuleDirectories, IEnumerable <ModuleDeclaration> modulesToCompile, ModuleInventory modInventory, List <string> targets, string wrappingModuleName, bool outputIsFramework) { wrappingModuleName = wrappingModuleName ?? kXamWrapPrefix; string outputLibraryName = BuildLibraryName(wrappingModuleName, outputIsFramework); string outputLibraryPath = Path.Combine(outputDirectory, outputLibraryName); string outputFrameworkPath = Path.Combine(outputDirectory, Path.GetFileName(outputLibraryName) + ".framework"); string outputFrameworkLibPath = Path.Combine(outputFrameworkPath, outputLibraryName); if (File.Exists(outputLibraryPath)) { File.Delete(outputLibraryPath); } using (TempDirectorySwiftClassFileProvider fileProvider = new TempDirectorySwiftClassFileProvider(Ex.ThrowOnNull(wrappingModuleName, "wrappingModuleName"), true)) { var allReferencedModules = new HashSet <string> (); foreach (ModuleDeclaration module in modulesToCompile) { HashSet <string> referencedModules = null; var wrappedClasses = Wrap(module, modInventory, fileProvider, typeMapper, wrappingModuleName, out referencedModules, errors); this.wrappers.Add(module.Name, wrappedClasses); allReferencedModules.Merge(referencedModules); } var inModuleNamesList = modulesToCompile.Select(mod => mod.Name).ToList(); inModuleNamesList.Add("XamGlue"); if (fileProvider.IsEmpty) { return(new Tuple <string, HashSet <string> > (null, null)); } var targetOutDirs = new List <string> (); for (int i = 0; i < targets.Count; i++) { // each file goes to a unique output directory. // first compile into the fileProvider, then move to // fileProvider/tar-get-arch string targetoutdir = Path.Combine(fileProvider.DirectoryPath, targets [i]); targetOutDirs.Add(targetoutdir); Directory.CreateDirectory(targetoutdir); var locations = SwiftModuleFinder.GatherAllReferencedModules(allReferencedModules, inputModuleDirectories, targets [i]); try { string [] inputModDirs = locations.Select(loc => loc.DirectoryPath).ToArray(); CompileAllFiles(fileProvider, wrappingModuleName, outputLibraryName, outputLibraryPath, inputModDirs, inputLibraryDirectories, inModuleNamesList.ToArray(), targets [i], outputIsFramework); } catch (Exception e) { throw ErrorHelper.CreateError(ReflectorError.kCantHappenBase + 66, e, $"Failed to compile the generated swift wrapper code."); } finally { locations.DisposeAll(); } // move to arch directory File.Copy(Path.Combine(fileProvider.DirectoryPath, outputLibraryName), Path.Combine(targetoutdir, outputLibraryName), true); File.Delete(Path.Combine(fileProvider.DirectoryPath, outputLibraryName)); File.Copy(Path.Combine(fileProvider.DirectoryPath, wrappingModuleName + ".swiftmodule"), Path.Combine(targetoutdir, wrappingModuleName + ".swiftmodule")); File.Delete(Path.Combine(fileProvider.DirectoryPath, wrappingModuleName + ".swiftmodule")); File.Copy(Path.Combine(fileProvider.DirectoryPath, wrappingModuleName + ".swiftdoc"), Path.Combine(targetoutdir, wrappingModuleName + ".swiftdoc")); File.Delete(Path.Combine(fileProvider.DirectoryPath, wrappingModuleName + ".swiftdoc")); } if (targets.Count > 1) { // lipo all the outputs back into the fileProvider Lipo(targetOutDirs, fileProvider.DirectoryPath, outputLibraryName); File.Copy(Path.Combine(fileProvider.DirectoryPath, outputLibraryName), outputLibraryPath, true); if (!IsMacOSLib(outputLibraryPath)) { if (!Directory.Exists(outputFrameworkPath)) { Directory.CreateDirectory(outputFrameworkPath); } File.Copy(outputLibraryPath, outputFrameworkLibPath, true); InfoPList.MakeInfoPList(outputFrameworkLibPath, Path.Combine(outputFrameworkPath, "Info.plist")); } } else { File.Copy(Path.Combine(targetOutDirs [0], outputLibraryName), outputLibraryPath, true); if (!IsMacOSLib(outputLibraryPath)) { if (!Directory.Exists(outputFrameworkPath)) { Directory.CreateDirectory(outputFrameworkPath); } File.Copy(outputLibraryPath, outputFrameworkLibPath, true); InfoPList.MakeInfoPList(outputFrameworkLibPath, Path.Combine(outputFrameworkPath, "Info.plist")); } } for (int i = 0; i < targets.Count; i++) { string arch = targets [i].ClangTargetCpu(); string targetDir = Path.Combine(outputDirectory, arch); Directory.CreateDirectory(targetDir); File.Copy(Path.Combine(targetOutDirs [i], wrappingModuleName + ".swiftmodule"), Path.Combine(targetDir, wrappingModuleName + ".swiftmodule"), true); File.Copy(Path.Combine(targetOutDirs [i], wrappingModuleName + ".swiftdoc"), Path.Combine(targetDir, wrappingModuleName + ".swiftdoc"), true); } foreach (string dirname in targetOutDirs) { Directory.Delete(dirname, true); } if (retainSwiftFiles) { CopySwiftFiles(fileProvider, Path.Combine(outputDirectory, wrappingModuleName + "Source")); } return(new Tuple <string, HashSet <string> > (outputLibraryPath, allReferencedModules)); } }