Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
841 changes: 701 additions & 140 deletions Cargo.lock

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions examples/tutorial/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,31 @@
mod generated;

#[derive(Debug)]
struct Item {
pub struct Item {
name: String,
size: u32,
}

#[derive(Debug)]
struct Inventory {
pub struct Inventory {
items: Vec<Item>,
remaining_space: u32,
}

impl Inventory {
fn new_empty(space: u32) -> Self {
pub fn new_empty(space: u32) -> Self {
Self {
items: vec![],
remaining_space: space,
}
}

fn add_item(&mut self, item: Item) {
pub fn add_item(&mut self, item: Item) {
self.remaining_space -= item.size;
self.items.push(item);
}

fn add_banana(&mut self, count: u32) {
pub fn add_banana(&mut self, count: u32) {
for _ in 0..count {
self.add_item(Item {
name: "banana".to_owned(),
Expand All @@ -37,7 +37,7 @@ impl Inventory {
}
}

fn into_items(self) -> Vec<Item> {
pub fn into_items(self) -> Vec<Item> {
self.items
}
}
5 changes: 5 additions & 0 deletions zngur-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ bench = false

[dependencies]
zngur = { version = "=0.7.0", path = "../zngur" }
zngur-def = {path = "../zngur-def/"}
clap = { version = "4.3.12", features = ["derive"] }
rustdoc-json = "0.9.7"
rustdoc-types = "0.55.0"
serde = { version = "1.0.204", features = ["derive"] }
serde_json = "1.0.122"
133 changes: 116 additions & 17 deletions zngur-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
use std::path::PathBuf;
use rustdoc_json::*;
use rustdoc_types::Crate;
use std::{
collections::HashMap,
fs::{self, read_to_string},
path::PathBuf,
str::from_utf8,
};
use zngur_def::LayoutPolicy;

use clap::Parser;
use zngur::Zngur;
use clap::{Args, Parser};
use zngur::{AutoZngur, SizeInfo, Zngur};

const ALLOC: &str = include_str!("../stdjson/alloc.json");
const CORE: &str = include_str!("../stdjson/core.json");
const PROC_MACRO: &str = include_str!("../stdjson/proc_macro.json");
const STD: &str = include_str!("../stdjson/std.json");
const STD_DETECT: &str = include_str!("../stdjson/std_detect.json");
const TEST: &str = include_str!("../stdjson/test.json");

#[derive(Parser)]
#[command(version)]
Expand All @@ -11,6 +26,10 @@ enum Command {
/// Path to the zng file
path: PathBuf,

/// Toggles the autogenerator
#[arg(short, long)]
auto: bool,

/// Path of the generated C++ file, if it is needed
///
/// Default is {ZNG_FILE_PARENT}/generated.cpp
Expand Down Expand Up @@ -52,27 +71,107 @@ fn main() {
match cmd {
Command::Generate {
path,
auto,
cpp_file,
h_file,
rs_file,
mangling_base,
cpp_namespace,
} => {
let pp = path.parent().unwrap();
let cpp_file = cpp_file.unwrap_or_else(|| pp.join("generated.cpp"));
let h_file = h_file.unwrap_or_else(|| pp.join("generated.h"));
let rs_file = rs_file.unwrap_or_else(|| pp.join("src/generated.rs"));
let mut zng = Zngur::from_zng_file(&path)
.with_cpp_file(cpp_file)
.with_h_file(h_file)
.with_rs_file(rs_file);
if let Some(mangling_base) = mangling_base {
zng = zng.with_mangling_base(&mangling_base);
}
if let Some(cpp_namespace) = cpp_namespace {
zng = zng.with_cpp_namespace(&cpp_namespace);
if auto {
let cpp_file = cpp_file.unwrap_or_else(|| path.join("generated.cpp"));
let h_file = h_file.unwrap_or_else(|| path.join("generated.h"));
let rs_file = rs_file.unwrap_or_else(|| path.join("src/generated.rs"));
dbg!(&cpp_file, &h_file, &rs_file);
let mut newpath = path.clone();
newpath.push("Cargo.toml");
let size_info = get_type_sizes(path);
let json_path = rustdoc_json::Builder::default()
.toolchain("nightly")
.manifest_path(newpath)
.build()
.unwrap();
let json = read_to_string(json_path).unwrap();
let primary: rustdoc_types::Crate = serde_json::from_str(&json).unwrap();
let mut crate_map = primary
.external_crates
.iter()
.filter_map(|(k, v)| match v.name.as_str() {
"alloc" => Some((*k, serde_json::from_str(ALLOC).unwrap())),
"core" => Some((*k, serde_json::from_str(CORE).unwrap())),
"proc_macro" => Some((*k, serde_json::from_str(PROC_MACRO).unwrap())),
"std" => Some((*k, serde_json::from_str(STD).unwrap())),
"st_detect" => Some((*k, serde_json::from_str(STD_DETECT).unwrap())),
"test" => Some((*k, serde_json::from_str(TEST).unwrap())),
a => {
println!("Unsupported crate dependency: {a}");
None
}
})
.collect::<HashMap<u32, Crate>>();
crate_map.insert(0, primary);

//TODO:NRB add mangling_base/cpp_namespace impl
AutoZngur::new()
.with_cpp_file(cpp_file)
.with_h_file(h_file)
.with_rs_file(rs_file)
.generate(crate_map, size_info);
} else {
let pp = path.parent().unwrap();
let cpp_file = cpp_file.unwrap_or_else(|| pp.join("generated.cpp"));
let h_file = h_file.unwrap_or_else(|| pp.join("generated.h"));
let rs_file = rs_file.unwrap_or_else(|| pp.join("src/generated.rs"));
let mut zng = Zngur::from_zng_file(&path)
.with_cpp_file(cpp_file)
.with_h_file(h_file)
.with_rs_file(rs_file);
if let Some(mangling_base) = mangling_base {
zng = zng.with_mangling_base(&mangling_base);
}
if let Some(cpp_namespace) = cpp_namespace {
zng = zng.with_cpp_namespace(&cpp_namespace);
}
zng.generate();
}
zng.generate();
}
}
}

fn get_type_sizes(path: PathBuf) -> HashMap<String, LayoutPolicy> {
std::process::Command::new("cargo")
.current_dir(&path)
.arg("clean")
.output()
.expect("close to godliness");
let raw_output = std::process::Command::new("cargo")
.current_dir(path)
.args(["+nightly", "rustc", "--", "-Z", "print-type-sizes"])
.output()
.expect("something");
let output = str::from_utf8(&raw_output.stdout).unwrap();
str_to_typesizes(output)
}

fn str_to_typesizes(input: &str) -> HashMap<String, LayoutPolicy> {
input
.split_terminator("\n")
.filter(|s| s.contains("type:"))
.map(|s| {
let mut parts = s.split_whitespace().skip(2).peekable();
let mut name = parts.next().unwrap().to_string().clone();
while let Some(p) = parts.peek()
&& p.parse::<u32>().is_err()
{
name.push(' ');
name.push_str(parts.next().unwrap());
}
let size = parts.next().unwrap().parse::<usize>().unwrap();
let align = parts.skip(2).next().unwrap().parse::<usize>().unwrap();
(
name[1..(name.len() - 2)].to_string(),
LayoutPolicy::StackAllocated { size, align },
)
})
.collect::<HashMap<_, _>>()
}
1 change: 1 addition & 0 deletions zngur-cli/stdjson/alloc.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions zngur-cli/stdjson/core.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions zngur-cli/stdjson/proc_macro.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions zngur-cli/stdjson/std.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions zngur-cli/stdjson/std_detect.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions zngur-cli/stdjson/test.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions zngur-def/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ license.workspace = true

[dependencies]
itertools = "0.11"
rustdoc-types = "0.55.0"
53 changes: 53 additions & 0 deletions zngur-def/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ use std::{collections::HashMap, fmt::Display};

use itertools::Itertools;

mod rdoc;
use rdoc::*;

mod merge;
pub use merge::{Merge, MergeFailure, MergeResult};
use rustdoc_types::{ItemEnum, Primitive};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Mutability {
Expand Down Expand Up @@ -184,6 +188,55 @@ pub enum PrimitiveRustType {
ZngurCppOpaqueOwnedObject,
}

// TODO:NRB isize and char
impl From<Primitive> for PrimitiveRustType {
fn from(value: Primitive) -> Self {
match value.name.as_str() {
"u8" => PrimitiveRustType::Uint(8),
"u16" => PrimitiveRustType::Uint(16),
"u32" => PrimitiveRustType::Uint(32),
"u64" => PrimitiveRustType::Uint(64),
"u128" => PrimitiveRustType::Uint(128),
"i8" => PrimitiveRustType::Int(8),
"i16" => PrimitiveRustType::Int(16),
"i32" => PrimitiveRustType::Int(32),
"i64" => PrimitiveRustType::Int(64),
"i128" => PrimitiveRustType::Int(128),
"f32" => PrimitiveRustType::Float(32),
"f64" => PrimitiveRustType::Float(64),
"usize" => PrimitiveRustType::Usize,
"bool" => PrimitiveRustType::Bool,
"str" => PrimitiveRustType::Str,
_ => panic!("Unknown primitive type: {}", value.name),
}
}
}

impl From<String> for PrimitiveRustType {
fn from(value: String) -> Self {
match value.as_str() {
"u8" => PrimitiveRustType::Uint(8),
"u16" => PrimitiveRustType::Uint(16),
"u32" => PrimitiveRustType::Uint(32),
"u64" => PrimitiveRustType::Uint(64),
"u128" => PrimitiveRustType::Uint(128),
"i8" => PrimitiveRustType::Int(8),
"i16" => PrimitiveRustType::Int(16),
"i32" => PrimitiveRustType::Int(32),
"i64" => PrimitiveRustType::Int(64),
"i128" => PrimitiveRustType::Int(128),
"f32" => PrimitiveRustType::Float(32),
"f64" => PrimitiveRustType::Float(64),
"usize" => PrimitiveRustType::Usize,
"bool" => PrimitiveRustType::Bool,
"str" => PrimitiveRustType::Str,
//TODO:NRB better impl
"isize" => PrimitiveRustType::Int(64),
_ => panic!("Unknown primitive type: {}", value),
}
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct RustPathAndGenerics {
pub path: Vec<String>,
Expand Down
Loading