public void Compile(SwiftCompilerOptions compilerOptions, bool outputIsFramework, Stream codeStream) { string pathName = tempDirectory.UniquePath(null, null, "swift"); using (FileStream stm = new FileStream(pathName, FileMode.Create)) { codeStream.CopyTo(stm); } Compile(compilerOptions, outputIsFramework, pathName); }
string BuildCompileArgs(SwiftCompilerOptions compilerOptions, bool outputIsFramework, string [] files) { StringBuilder sb = new StringBuilder(); sb.Append("-emit-library "); if (CompilerInfo.HasTarget) { sb.Append("-target ").Append(CompilerInfo.Target).Append(" "); } sb.Append("-sdk ").Append(GetSDKPath()).Append(" "); if (compilerOptions.IncludeDirectories != null) { foreach (string includeDirectory in compilerOptions.IncludeDirectories) { if (!String.IsNullOrEmpty(includeDirectory)) { sb.Append("-I ").Append(QuoteIfNeeded(includeDirectory)).Append(' '); } } } AppendLibraryAndFrameworks(sb, compilerOptions.LibraryDirectories ?? new string [] { }, compilerOptions.InputModules ?? new string [] { }, true); //if (Verbose) //sb.Append(" -v "); string moduleName = compilerOptions.ModuleName; if (!String.IsNullOrEmpty(moduleName)) { sb.Append("-emit-module -module-name ").Append(QuoteIfNeeded(moduleName)); } if (outputIsFramework) { sb.Append(" -Xlinker -rpath -Xlinker @executable_path/Frameworks -Xlinker -rpath -Xlinker @loader_path/Frameworks"); sb.Append(" -Xlinker -rpath -Xlinker @executable_path -Xlinker -rpath -Xlinker @rpath"); sb.Append(" -Xlinker -final_output -Xlinker ").Append(QuoteIfNeeded(moduleName)); sb.Append(" -Xlinker -install_name -Xlinker ").Append(QuoteIfNeeded($"@rpath/{moduleName}.framework/{moduleName}")); } else { sb.Append(" -Xlinker -install_name -Xlinker ").Append(QuoteIfNeeded($"@rpath/lib{moduleName}.dylib")); } foreach (string file in files) { sb.Append(" ").Append(file); } return(sb.ToString()); }
void CompileAllFiles(TempDirectorySwiftClassFileProvider fileProvider, string moduleName, string outputLibraryName, string outputLibraryPath, string [] inputModulePaths, string [] inputLibraryPaths, string [] inputModuleNames, string target, bool outputIsFramework) { SwiftTargetCompilerInfo compilerInfo = CompilerLocation.GetTargetInfo(target); using (CustomSwiftCompiler compiler = new CustomSwiftCompiler(compilerInfo, fileProvider, false)) { compiler.Verbose = verbose; string [] sourceFiles = fileProvider.CompletedFileNames.ToArray(); SwiftCompilerOptions options = new SwiftCompilerOptions(moduleName, inputModulePaths, inputLibraryPaths, inputModuleNames); compiler.Compile(options, outputIsFramework, sourceFiles); } }
List <ClassDeclaration> ReflectClassDeclarations(string code) { using (TempDirectoryFilenameProvider fileProvider = new TempDirectoryFilenameProvider(null, false)) { CustomSwiftCompiler compiler = Utils.DefaultSwiftCompiler(fileProvider); SwiftCompilerOptions options = new SwiftCompilerOptions("NameNotImportant", null, null, null); compiler.CompileString(options, code); List <ModuleDeclaration> modules = compiler.ReflectToModules(new string [] { compiler.DirectoryPath }, new string [] { compiler.DirectoryPath }, "", "NameNotImportant"); Assert.AreEqual(1, modules.Count); return(modules [0].AllClasses); } }
public void CompileString(SwiftCompilerOptions compilerOptions, string codeString) { if (String.IsNullOrEmpty(codeString)) { throw new ArgumentNullException(nameof(codeString)); } using (MemoryStream stream = new MemoryStream()) { StreamWriter writer = new StreamWriter(stream); writer.Write(codeString); writer.Flush(); stream.Position = 0; Compile(compilerOptions, false, stream); } }
public static CustomSwiftCompiler CompileSwift(string swiftCode, DisposableTempDirectory provider = null, string moduleName = "Xython", string target = "x86_64-apple-macosx10.9") { CustomSwiftCompiler compiler = DefaultSwiftCompiler(provider, target: target); string [] includeDirectories = null; List <string> libraryDirectories = new List <string> { compiler.DirectoryPath, Compiler.kSwiftRuntimeGlueDirectory }; if (provider != null) { includeDirectories = new string [] { provider.DirectoryPath }; libraryDirectories.Add(provider.DirectoryPath); } SwiftCompilerOptions options = new SwiftCompilerOptions(moduleName, includeDirectories, libraryDirectories.ToArray(), new string [] { "XamGlue" }); compiler.CompileString(options, swiftCode); return(compiler); }
public void Compile(SwiftCompilerOptions compilerOptions, bool outputIsFramework, params string [] files) { string args = BuildCompileArgs(compilerOptions, outputIsFramework, files); if (Verbose) { Console.WriteLine("Compiling swift files: " + files.InterleaveCommas()); } Launch(CompilerInfo.CustomSwiftc, args); var outputLib = Path.Combine(DirectoryPath, $"lib{compilerOptions.ModuleName}.dylib"); RemoveBadRPath(outputLib); if (outputIsFramework) { string srcLib = outputLib; File.Copy(srcLib, Path.Combine(DirectoryPath, compilerOptions.ModuleName), true); File.Delete(srcLib); } }