public static void GenerateCodeFiles(VulkanSpecification spec, TypeNameMappings tnm, string path) { if (!Directory.Exists(path)) { } GenerateAllTypes(spec, tnm, path); }
public static int Main(string[] args) { string outputPath = AppContext.BaseDirectory; ArgumentSyntax.Parse(args, s => { s.DefineOption("o|out", ref outputPath, "The folder into which code is generated. Defaults to the application directory."); }); Configuration.CodeOutputPath = outputPath; if (File.Exists(outputPath)) { Console.Error.WriteLine("The given path is a file, not a folder."); return(1); } else if (!Directory.Exists(outputPath)) { Directory.CreateDirectory(outputPath); } using (var fs = File.OpenRead(Path.Combine(AppContext.BaseDirectory, "vk.xml"))) { VulkanSpecification vs = VulkanSpecification.LoadFromXmlStream(fs); TypeNameMappings tnm = new TypeNameMappings(); foreach (var typedef in vs.Typedefs) { if (typedef.Requires != null) { tnm.AddMapping(typedef.Requires, typedef.Name); } else { tnm.AddMapping(typedef.Name, "uint"); } } HashSet <string> definedBaseTypes = new HashSet <string> { "VkBool32" }; if (Configuration.MapBaseTypes) { foreach (var baseType in vs.BaseTypes) { if (!definedBaseTypes.Contains(baseType.Key)) { tnm.AddMapping(baseType.Key, baseType.Value); } } } CodeGenerator.GenerateCodeFiles(vs, tnm, Configuration.CodeOutputPath); } return(0); }
private static void GenerateAllTypes(VulkanSpecification spec, TypeNameMappings tnm, string path) { using (StreamWriter sw = File.CreateText(Path.Combine(path, "Structures.gen.cs"))) { CsCodeWriter cw = new CsCodeWriter(sw); cw.WriteHeader(); cw.WriteLine(); using (cw.PushIfDef(GetActiveCalliCondition())) { cw.Using("System"); cw.WriteLine(); using (cw.PushBlock("namespace Vulkan")) { SpaceSeparatedList(cw, spec.Structures, structure => { StructureHelpers.WriteStructure(cw, structure, tnm, spec.Constants); }); } } } using (StreamWriter enumWriter = File.CreateText(Path.Combine(path, "Enums.gen.cs"))) { CsCodeWriter cw = new CsCodeWriter(enumWriter); cw.WriteHeader(); cw.WriteLine(); using (cw.PushIfDef(GetActiveCalliCondition())) { cw.Using("System"); cw.WriteLine(); using (cw.PushBlock("namespace Vulkan")) { SpaceSeparatedList(cw, spec.Enums, enumDef => { EnumHelpers.WriteEnum(cw, enumDef, tnm); }); } } } CommandDefinition[] allVariants = spec.Commands.SelectMany(cd => VariantGenerator.GenerateVariants(cd)).ToArray(); CommandDefinition[] allCommandsWithVariants = spec.Commands.Concat(allVariants).OrderBy(cd => cd.Name).ToArray(); using (StreamWriter commandWriter = File.CreateText(Path.Combine(path, "Commands.gen.cs"))) { CsCodeWriter cw = new CsCodeWriter(commandWriter); cw.WriteHeader(); cw.WriteLine(); using (cw.PushIfDef(GetActiveCalliCondition())) { cw.Using("System"); cw.Using("System.Runtime.InteropServices"); cw.WriteLine(); using (cw.PushBlock("namespace Vulkan")) using (cw.PushBlock("public static unsafe partial class VulkanNative")) { SpaceSeparatedList(cw, spec.Constants, constant => { ConstantHelpers.WriteConstant(cw, tnm, constant); }); cw.WriteLine(); SpaceSeparatedList(cw, allCommandsWithVariants, command => { CommandHelpers.WriteCommand(cw, tnm, command); }); cw.WriteLine(); using (cw.PushBlock("private static void LoadFunctionPointers()")) { foreach (CommandDefinition command in spec.Commands) { if (Configuration.GenerateCalliStubs) { cw.WriteLine($"{command.Name}_ptr = s_nativeLib.LoadFunctionPointer(\"{command.Name}\");"); } else { cw.WriteLine($"IntPtr {command.Name}_nativePtr = s_nativeLib.LoadFunctionPointer(\"{command.Name}\");"); using (cw.PushBlock($"if ({command.Name}_nativePtr != IntPtr.Zero)")) { cw.WriteLine($"{command.Name}_ptr = Marshal.GetDelegateForFunctionPointer<{command.Name}_delegate>({command.Name}_nativePtr);"); } using (cw.PushBlock("else")) { string invocation = string.Join(", ", command.Parameters.Select(pd => Util.NormalizeFieldName(pd.Name))); cw.WriteLine($"{command.Name}_ptr = ({invocation}) => {{ throw CreateMissingFunctionException(); }};"); } } } } } } } using (StreamWriter handleWriter = File.CreateText(Path.Combine(path, "Handles.gen.cs"))) { CsCodeWriter cw = new CsCodeWriter(handleWriter); cw.WriteHeader(); cw.WriteLine(); using (cw.PushIfDef(GetActiveCalliCondition())) { cw.Using("System"); cw.WriteLine(); using (cw.PushBlock("namespace Vulkan")) { SpaceSeparatedList(cw, spec.Handles, handle => { HandleHelpers.WriteHandle(cw, handle); }); } } } using (StreamWriter unionWriter = File.CreateText(Path.Combine(path, "Unions.gen.cs"))) { CsCodeWriter cw = new CsCodeWriter(unionWriter); cw.WriteHeader(); cw.WriteLine(); using (cw.PushIfDef(GetActiveCalliCondition())) { cw.Using("System.Runtime.InteropServices"); cw.WriteLine(); using (cw.PushBlock("namespace Vulkan")) { SpaceSeparatedList(cw, spec.Unions, union => { UnionHelpers.WriteUnion(cw, tnm, union); }); } } } }
private static void GenerateAllTypes(VulkanSpecification spec, TypeNameMappings tnm, string path) { using (StreamWriter sw = File.CreateText(Path.Combine(path, "Structures.gen.cs"))) { CsCodeWriter cw = new CsCodeWriter(sw); cw.WriteHeader(); cw.WriteLine(); cw.Using("System"); cw.WriteLine(); using (cw.PushBlock("namespace Vulkan")) { Util.SpaceSeparatedList(cw, spec.Structures, structure => { StructureHelpers.WriteStructure(cw, structure, tnm, spec.Constants); }); } } using (StreamWriter enumWriter = File.CreateText(Path.Combine(path, "Enums.gen.cs"))) { CsCodeWriter cw = new CsCodeWriter(enumWriter); cw.WriteHeader(); cw.WriteLine(); cw.Using("System"); cw.WriteLine(); using (cw.PushBlock("namespace Vulkan")) { Util.SpaceSeparatedList(cw, spec.Enums, enumDef => { EnumHelpers.WriteEnum(cw, enumDef, tnm); }); } } CommandDefinition[] allVariants = spec.Commands.SelectMany(cd => VariantGenerator.GenerateVariants(cd)).ToArray(); CommandDefinition[] allCommandsWithVariants = spec.Commands.Concat(allVariants).OrderBy(cd => cd.Name).ToArray(); using (StreamWriter commandWriter = File.CreateText(Path.Combine(path, "Commands.gen.cs"))) { CsCodeWriter cw = new CsCodeWriter(commandWriter); cw.WriteHeader(); cw.WriteLine(); cw.Using("System"); cw.Using("System.Runtime.InteropServices"); cw.WriteLine(); using (cw.PushBlock("namespace Vulkan")) using (cw.PushBlock("public static unsafe partial class VulkanNative")) { Util.SpaceSeparatedList(cw, allCommandsWithVariants, command => { CommandHelpers.WriteCommand(cw, tnm, command); }); cw.WriteLine(); using (cw.PushBlock("private static void LoadFunctionPointers()")) { foreach (CommandDefinition command in spec.Commands) { cw.WriteLine($"{command.Name}_ptr = s_nativeLib.LoadFunctionPointer(\"{command.Name}\");"); } } } } using (StreamWriter handleWriter = File.CreateText(Path.Combine(path, "Handles.gen.cs"))) { CsCodeWriter cw = new CsCodeWriter(handleWriter); cw.WriteHeader(); cw.WriteLine(); cw.Using("System"); cw.WriteLine(); using (cw.PushBlock("namespace Vulkan")) { Util.SpaceSeparatedList(cw, spec.Handles, handle => { HandleHelpers.WriteHandle(cw, handle); }); } } using (StreamWriter unionWriter = File.CreateText(Path.Combine(path, "Unions.gen.cs"))) { CsCodeWriter cw = new CsCodeWriter(unionWriter); cw.WriteHeader(); cw.WriteLine(); cw.Using("System.Runtime.InteropServices"); cw.WriteLine(); using (cw.PushBlock("namespace Vulkan")) { Util.SpaceSeparatedList(cw, spec.Unions, union => { UnionHelpers.WriteUnion(cw, tnm, union); }); } } using (StreamWriter unionWriter = File.CreateText(Path.Combine(path, "Constants.gen.cs"))) { CsCodeWriter cw = new CsCodeWriter(unionWriter); cw.WriteHeader(); cw.WriteLine(); cw.Using("System.Runtime.InteropServices"); cw.WriteLine(); using (cw.PushBlock("namespace Vulkan")) { ConstantHelpers.WriteAllConstants(cw, tnm, spec.Constants); } } }