vkgen/lib.rs
1// MIT License
2//
3// Copyright (c) 2019-2021 Tobias Pfeiffer
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to deal
7// in the Software without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in all
13// copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21// SOFTWARE.
22
23//! The library version of this crate, to be used in build scripts.
24//!
25//! Example 1:
26//! ```rust
27//! use std::{fs, io};
28//!
29//! fn main() {
30//! vkgen::generate(
31//! fs::File::open("vk.xml")
32//! .map(io::BufReader::new)
33//! .expect("failed to open input file"),
34//! fs::OpenOptions::new()
35//! .write(true)
36//! .open("vk.rs")
37//! .map(io::BufWriter::new)
38//! .expect("failed to open output file"),
39//! fs::OpenOptions::new()
40//! .write(true)
41//! .open("vk.toml")
42//! .map(io::BufWriter::new)
43//! .expect("failed to open output file"),
44//! true
45//! ).unwrap();
46//!
47//! vkgen::fix_ptr_types(vkgen::Api::Vulkan, "vk.rs")
48//! .expect("failed to fix pointer types");
49//! }
50//! ```
51//!
52//! Example 2:
53//! ```rust
54//! use {vkgen::*, std::{fs, io}};
55//!
56//! fn main() {
57//! let Registry { api, elements, exts } = xml::deserialize(fs::File::open("vk.xml")
58//! .map(io::BufReader::new)
59//! .expect("failed to open input file"))
60//! .expect("failed to deserialize registry");
61//!
62//! gen(&mut fs::OpenOptions::new()
63//! .write(true)
64//! .open("vk.rs")
65//! .map(io::BufWriter::new)
66//! .expect("failed to open input file"), elements, api, true)
67//! .expect("failed to write the generated code");
68//!
69//! gen_cargo(&mut fs::OpenOptions::new()
70//! .write(true)
71//! .open("vk.toml")
72//! .map(io::BufWriter::new)
73//! .expect("failed to open output file"), exts)
74//! .expect("failed to write the generated code");
75//!
76//! fix_ptr_types(api, "vk.rs")
77//! .expect("failed to fix pointer types");
78//! }
79//! ```
80
81use std::{io::{self, *}, fs::OpenOptions, path::Path};
82
83pub use {parse::*, gen::*};
84
85pub mod xml;
86pub mod parse;
87pub mod gen;
88
89pub fn generate(
90 reader: impl BufRead + std::fmt::Debug,
91 mut writer: impl Write,
92 mut writer_cargo: impl Write,
93 mangle: bool
94) -> io::Result<()> {
95 let Registry { api, elements, exts } = xml::deserialize(reader)
96 .map_err(|e| Error::new(ErrorKind::InvalidData, e))?;
97
98 gen(&mut writer, elements, api, mangle)?;
99 gen_cargo(&mut writer_cargo, exts)?;
100 Ok(())
101}
102
103/// Fixes void pointers/unit references
104pub fn fix_ptr_types(api: Api, path: impl AsRef<Path>) -> io::Result<()> {
105 let mut buf = String::new();
106 let mut f = OpenOptions::new()
107 .read(true)
108 .write(true)
109 .open(path)?;
110
111 f.read_to_string(&mut buf)?;
112
113 let buf = buf.replace("&()", if api == Api::Vulkan { "VkAnyRef" } else { "XrAnyRef" })
114 .replace("&'a ()", if api == Api::Vulkan { "VkAnyRef<'a>" } else { "XrAnyRef<'a>" })
115 .replace("&mut ()", if api == Api::Vulkan { "VkAnyMut" } else { "XrAnyMut" })
116 .replace("&'a mut ()", if api == Api::Vulkan { "VkAnyMut<'a>" } else { "XrAnyMut<'a>" })
117 .replace("&[()]", "&[u8]")
118 .replace("&mut [()]", "&mut [u8]")
119 .replace("&'a [()]", "&'a [u8]")
120 .replace("&'a mut [()]", "&'a mut [u8]");
121
122 f.seek(SeekFrom::Start(0))?;
123 f.write_all(buf.as_bytes())?;
124 f.flush()
125}