Skip to content

Commit c3b824f

Browse files
committed
Group foreign items by feature and refactor
1 parent 171bcb9 commit c3b824f

File tree

8 files changed

+6947
-16370
lines changed

8 files changed

+6947
-16370
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ env_logger = { version = "0.10.1", optional = true }
2929

3030
[build-dependencies.vitasdk-sys-build-util]
3131
path = "build-util"
32-
version = "0.1.0"
32+
version = "0.2.0"
3333
optional = true
3434

3535
[package.metadata.docs.rs]

build-util/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "vitasdk-sys-build-util"
3-
version = "0.1.0"
3+
version = "0.2.0"
44
edition = "2021"
55
authors = ["Daria Sukhonina <[email protected]>"]
66
license = "MIT OR Apache-2.0"

build-util/src/bindgen.rs

Lines changed: 6 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
use std::{env, fs, io, path::Path, process};
22

33
use quote::ToTokens;
4-
use syn::{ForeignItem, Item, Type};
4+
use syn::{self, visit_mut::VisitMut};
55

6-
use crate::link_visitor::{
7-
syn::{self, visit_mut::VisitMut},
8-
Link,
9-
};
6+
use crate::visitors::{Link, Sort};
107

118
pub fn generate(
129
vita_headers_include: &Path,
@@ -25,11 +22,12 @@ pub fn generate(
2522
"Loading vita-headers metadata yaml files from \"{}\"",
2623
db.to_string_lossy()
2724
);
28-
let mut link = Link::load(db, vitasdk_sys_manifest);
29-
link.visit_file_mut(&mut bindings);
25+
let link = Link::load(db, vitasdk_sys_manifest);
26+
27+
link.visit(&mut bindings);
3028

3129
// We sort items here so generated bindings don't depend on the included order.
32-
sort_items(&mut bindings.items);
30+
Sort.visit_file_mut(&mut bindings);
3331

3432
let bindings = bindings.into_token_stream();
3533

@@ -97,97 +95,3 @@ fn generate_preprocessed_bindings(
9795

9896
builder.generate().expect("bindgen failed").to_string()
9997
}
100-
101-
/// Sorts items on alphabetical order based on normalized identifier.
102-
/// Bindgen items will be moved to the start.
103-
fn sort_items(items: &mut [Item]) {
104-
items.sort_by_cached_key(|item| {
105-
let (precedence, ident) =
106-
match item {
107-
Item::ExternCrate(i) => (0, i.ident.to_string()),
108-
Item::Use(_i) => (1, String::new()),
109-
Item::Mod(i) => (2, i.ident.to_string()),
110-
Item::Macro(i) => (
111-
3,
112-
i.ident
113-
.as_ref()
114-
.map(|i| i.to_string())
115-
.unwrap_or_else(String::new),
116-
),
117-
Item::Static(i) => (4, i.ident.to_string()),
118-
Item::Const(i) => (4, i.ident.to_string()),
119-
Item::Trait(i) => (4, i.ident.to_string()),
120-
Item::TraitAlias(i) => (4, i.ident.to_string()),
121-
Item::Type(i) => (4, i.ident.to_string()),
122-
Item::Enum(i) => (4, i.ident.to_string()),
123-
Item::Struct(i) => (4, i.ident.to_string()),
124-
Item::Impl(i) => {
125-
let ident =
126-
match &*i.self_ty {
127-
Type::Path(path_type) => path_type.path.segments.iter().fold(
128-
String::new(),
129-
|acc, segment| {
130-
let ident_string = segment.ident.to_string();
131-
if acc.is_empty() {
132-
ident_string
133-
} else {
134-
acc + "::" + &ident_string
135-
}
136-
},
137-
),
138-
ty => {
139-
log::warn!("impl on unexpected type {ty:?}");
140-
String::new()
141-
}
142-
};
143-
(4, ident)
144-
}
145-
Item::Fn(i) => (4, i.sig.ident.to_string()),
146-
Item::ForeignMod(i) => {
147-
// For `extern` blocks, we use the first item's identifier.
148-
let ident = match i.items.len() {
149-
// Could use link name here. But it's not strictly necessary as they're
150-
// already sorted.
151-
0 => String::new(),
152-
len => {
153-
if len > 1 {
154-
log::warn!("Unexpected ForeignMod with multiple items: {i:?}");
155-
}
156-
157-
match &i.items[0] {
158-
ForeignItem::Fn(i) => i.sig.ident.to_string(),
159-
ForeignItem::Static(i) => i.ident.to_string(),
160-
ForeignItem::Type(i) => i.ident.to_string(),
161-
i => {
162-
log::warn!("Unexpected item in foreign mod: {i:?}");
163-
String::new()
164-
}
165-
}
166-
}
167-
};
168-
(4, ident)
169-
}
170-
Item::Union(i) => (4, i.ident.to_string()),
171-
i => {
172-
log::warn!("Unexpected item: {i:?}");
173-
(10, String::new())
174-
}
175-
};
176-
consider_bindgen((precedence, normalize_str(&ident)))
177-
});
178-
}
179-
180-
fn normalize_str(input: &str) -> String {
181-
input.to_lowercase().replace('_', "")
182-
}
183-
184-
fn consider_bindgen(keys: (i32, String)) -> (i32, String) {
185-
let (precedence, ident) = keys;
186-
let new_precedence = if ident.starts_with("bindgen") {
187-
// Move bindgen items to the start of the file
188-
precedence - 16
189-
} else {
190-
precedence
191-
};
192-
(new_precedence, ident)
193-
}

build-util/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pub mod bindgen;
2-
pub mod link_visitor;
32
pub mod vita_headers_db;
3+
4+
mod visitors;

build-util/src/link_visitor.rs

Lines changed: 0 additions & 162 deletions
This file was deleted.

0 commit comments

Comments
 (0)