Skip to content

Commit

Permalink
codegen: upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
Nugine committed May 22, 2023
1 parent 5563812 commit b61bd08
Show file tree
Hide file tree
Showing 6 changed files with 3,594 additions and 166 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[dependencies]
libc = "0.2.139"
libc = "0.2.144"

[dev-dependencies]
once_cell = "1.16.0"
once_cell = "1.17.1"

[workspace]
members = ["codegen"]
15 changes: 4 additions & 11 deletions codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,7 @@ version = "0.0.0"
edition = "2021"
publish = false

[dependencies.libc-cfg]
git = "https://github.com/Nugine/codegen-tools"
rev = "c980bde491ce2f523cced539e93283e6ac0ce7ac"

[dependencies.codegen-writer]
git = "https://github.com/Nugine/codegen-tools"
rev = "c980bde491ce2f523cced539e93283e6ac0ce7ac"

[dependencies.target-cfg]
git = "https://github.com/Nugine/codegen-tools"
rev = "c980bde491ce2f523cced539e93283e6ac0ce7ac"
[dependencies]
codegen-writer = "0.1.0"
codegen-libc = "0.2.0"
codegen-cfg = "0.2.0"
34 changes: 20 additions & 14 deletions codegen/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use codegen_cfg::ast::*;
use codegen_libc::{search, simplified_expr, CfgItem, RegexSet};
use codegen_writer::g;
use codegen_writer::glines;
use libc_cfg::generate_item_cfg;

fn main() {
let path = "src/bindings.rs";
Expand All @@ -9,8 +10,10 @@ fn main() {
}

fn codegen() {
let libc_repo_path = "temp/libc";
let item_list = libc_cfg::generate_item_list(libc_repo_path).unwrap();
let libc_path = "temp/libc";
let re = RegexSet::new(["RLIM", "rlimit", "RLIMIT_"]).unwrap();

let item_list = search(libc_path, &re).unwrap();

glines!(
"#![allow(clippy::cast_possible_truncation)]",
Expand All @@ -23,29 +26,31 @@ fn codegen() {
codegen_resources(&item_list);
}

fn codegen_64(item_list: &[libc_cfg::Item]) {
fn codegen_64(item_list: &[CfgItem]) {
for name in ["rlimit", "getrlimit", "setrlimit"] {
let name64 = format!("{}64", name);
let item64 = item_list.iter().find(|item| item.name == name64).unwrap();
let cfg64 = generate_item_cfg(item64);
let cfg64 = item64.cfg.clone();

let item = item_list.iter().find(|item| item.name == name).unwrap();
let cfg = generate_item_cfg(item);
let cfg = item.cfg.clone();

g!("#[cfg({cfg64})]");
g!("pub use libc::{name64} as {name};");
g!();

g!("#[cfg(all(not({cfg64}), {cfg}))]");
let otherwise = simplified_expr(all((not(cfg64), cfg)));

g!("#[cfg({otherwise})]");
g!("pub use libc::{name};");
g!();
}
}

fn codegen_inf(item_list: &[libc_cfg::Item]) {
fn codegen_inf(item_list: &[CfgItem]) {
let name = "RLIM_INFINITY";
let item = item_list.iter().find(|item| item.name == name).unwrap();
let cfg = generate_item_cfg(item);
let cfg = &item.cfg;

g!("#[cfg({cfg})]");
g!("pub const {name}: u64 = libc::{name} as u64;");
Expand All @@ -56,7 +61,7 @@ fn codegen_inf(item_list: &[libc_cfg::Item]) {
g!();
}

fn codegen_resources(item_list: &[libc_cfg::Item]) {
fn codegen_resources(item_list: &[CfgItem]) {
let resources = {
let mut ans = Vec::new();
for item in item_list {
Expand All @@ -68,15 +73,15 @@ fn codegen_resources(item_list: &[libc_cfg::Item]) {
}

if name.starts_with("RLIMIT_") {
let cfg = generate_item_cfg(item);
ans.push((item, cfg));
ans.push(item);
}
}
ans
};

for (item, cfg) in &resources {
for item in &resources {
let name = item.name.as_str();
let cfg = &item.cfg;

g!("#[cfg({cfg})]");
g!("pub const {name}: u8 = libc::{name} as u8;");
Expand All @@ -94,8 +99,9 @@ fn codegen_resources(item_list: &[libc_cfg::Item]) {
g!("#[test]");
g!("fn resource_range() {{");

for (item, cfg) in &resources {
for item in &resources {
let name = item.name.as_str();
let cfg = &item.cfg;

g!("#[cfg({cfg})]");
g!("assert!((0..128).contains(&libc::{name}));");
Expand Down
2 changes: 1 addition & 1 deletion scripts/codegen.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash -e
./scripts/download-libc.sh
cargo run -p rlimit-codegen
cargo fmt
rustfmt src/bindings.rs
echo "done"
2 changes: 1 addition & 1 deletion scripts/download-libc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ if [ ! -d "temp/libc" ]; then
mkdir -p temp

pushd temp
git clone https://github.com/rust-lang/libc.git -b master --depth=1
git clone https://github.com/rust-lang/libc.git -b main --depth=1
popd
fi
Loading

0 comments on commit b61bd08

Please sign in to comment.