Skip to content

Commit becdfd5

Browse files
committed
Move libb/uxn.b to the uxn codegen
1 parent 79842e5 commit becdfd5

4 files changed

Lines changed: 68 additions & 10 deletions

File tree

src/bgen.rs

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,63 @@ use nob::*;
1818
use crust::libc::*;
1919
use crust::*;
2020

21-
pub unsafe fn aggregate_libb(folder_path: *const c_char) -> Option<()> {
21+
const BUILD_LIBB_PATH: *const c_char = c!("./build/libb/");
22+
23+
pub unsafe fn aggregate_to_libb(folder_path: *const c_char) -> Option<()> {
2224
let mut children: File_Paths = zeroed();
2325

2426
if !read_entire_dir(folder_path, &mut children) { return None; }
2527

2628
for i in 0..children.count {
2729
let child = *children.items.add(i);
2830
if *child == '.' as c_char { continue; }
29-
if !copy_file(
30-
temp_sprintf(c!("%s/%s"), folder_path, child),
31-
temp_sprintf(c!("./build/libb/%s"), child),
32-
) { return None; }
31+
let child_path = temp_sprintf(c!("%s/%s"), folder_path, child);
32+
if temp_strip_suffix(child, c!(".b")).is_none() {
33+
log(Log_Level::INFO, c!("%s does not end with `.b`. Ignoring..."), child_path);
34+
continue;
35+
}
36+
if !matches!(get_file_type(child_path)?, File_Type::REGULAR) {
37+
log(Log_Level::INFO, c!("%s is not a regular file. Ignoring..."), child_path);
38+
continue;
39+
}
40+
let dest_path = temp_sprintf(c!("%s%s"), BUILD_LIBB_PATH, child);
41+
if file_exists(dest_path)? {
42+
// TODO: track which codegen provides which file and report the offender more precisely
43+
log(Log_Level::ERROR, c!("%s already exists. Several codegens provide a libb file with the same name."), dest_path);
44+
return None;
45+
}
46+
if !copy_file(child_path, dest_path,) { return None; }
47+
}
48+
Some(())
49+
}
50+
51+
pub unsafe fn reset_libb() -> Option<()> {
52+
if !mkdir_if_not_exists(BUILD_LIBB_PATH) { return None; }
53+
54+
let mut children: File_Paths = zeroed();
55+
56+
let folder_path = BUILD_LIBB_PATH;
57+
if !read_entire_dir(folder_path, &mut children) { return None; }
58+
59+
for i in 0..children.count {
60+
let child = *children.items.add(i);
61+
if strcmp(child, c!(".")) == 0 { continue; }
62+
if strcmp(child, c!("..")) == 0 { continue; }
63+
let child_path = temp_sprintf(c!("%s/%s"), folder_path, child);
64+
if !matches!(get_file_type(child_path)?, File_Type::REGULAR) {
65+
log(Log_Level::ERROR, c!("%s contains a non-regular file %s. This is not allowed. Please remove %s manually and trying building the project again."), folder_path, child_path, folder_path);
66+
return None;
67+
}
68+
if !delete_file(child_path) { return None; }
3369
}
3470
Some(())
3571
}
3672

3773
pub unsafe fn main(mut _argc: i32, mut _argv: *mut*mut c_char) -> Option<()> {
3874
if !mkdir_if_not_exists(c!("./build/")) { return None; }
39-
if !mkdir_if_not_exists(c!("./build/libb/")) { return None; }
4075

41-
aggregate_libb(c!("./libb/"))?;
76+
reset_libb()?;
77+
aggregate_to_libb(c!("./libb/"))?;
4278

4379
let parent = c!("./src/codegen");
4480
let mut children: File_Paths = zeroed();
@@ -61,7 +97,7 @@ pub unsafe fn main(mut _argc: i32, mut _argv: *mut*mut c_char) -> Option<()> {
6197
log(Log_Level::INFO, c!("--- CODEGEN %s ---"), child);
6298
let codegen_libb = temp_sprintf(c!("%s/%s/libb/"), parent, child);
6399
if file_exists(codegen_libb)? {
64-
aggregate_libb(codegen_libb)?;
100+
aggregate_to_libb(codegen_libb)?;
65101
}
66102
}
67103
}
File renamed without changes.

src/nob.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use core::ffi::*;
22
use core::slice;
3-
use crate::crust::libc;
3+
use crate::crust::*;
4+
use crate::enum_with_order;
45

56
#[repr(C)]
67
#[derive(Clone, Copy)]
@@ -119,6 +120,16 @@ pub enum Log_Level {
119120
NO_LOGS,
120121
}
121122

123+
enum_with_order! {
124+
#[derive(Clone, Copy)]
125+
enum File_Type in FILE_TYPE_ORDER {
126+
REGULAR,
127+
DIRECTORY,
128+
SYMLINK,
129+
OTHER,
130+
}
131+
}
132+
122133
extern "C" {
123134
#[link_name = "nob_temp_sprintf"]
124135
pub fn temp_sprintf(format: *const c_char, ...) -> *mut c_char;
@@ -154,6 +165,18 @@ extern "C" {
154165
pub static mut minimal_log_level: Log_Level;
155166
#[link_name = "nob_copy_file"]
156167
pub fn copy_file(src_path: *const c_char, dst_path: *const c_char) -> bool;
168+
#[link_name = "nob_delete_file"]
169+
pub fn delete_file(path: *const c_char) -> bool;
170+
}
171+
172+
pub unsafe fn get_file_type(path: *const c_char) -> Option<File_Type> {
173+
extern "C" {
174+
#[link_name = "nob_get_file_type"]
175+
pub fn get_file_type_raw(path: *const c_char) -> c_int;
176+
}
177+
let result = get_file_type_raw(path);
178+
if result < 0 { return None; }
179+
Some((*FILE_TYPE_ORDER)[result as usize])
157180
}
158181

159182
pub unsafe fn write_entire_file(path: *const c_char, data: *const c_void, size: usize) -> Option<()> {
@@ -193,7 +216,6 @@ pub unsafe fn file_exists(file_path: *const c_char) -> Option<bool> {
193216
}
194217
}
195218

196-
197219
// TODO: This is a generally useful function. Consider making it a part of nob.h
198220
pub unsafe fn temp_strip_suffix(s: *const c_char, suffix: *const c_char) -> Option<*const c_char> {
199221
let mut sv = sv_from_cstr(s);

0 commit comments

Comments
 (0)