From 6df525f9785325e501e34322eb682b0cb483d273 Mon Sep 17 00:00:00 2001 From: Guilleag01 Date: Sat, 26 Jul 2025 21:23:44 +0200 Subject: [PATCH] first version of init done --- .gitignore | 1 + c_config.toml | 9 +++++++++ src/compiler.rs | 20 +++++++++++--------- src/init.rs | 37 +++++++++++++++++++++++++++++++++++-- src/main.rs | 11 +++++++++-- test/build/dir/test1.o | Bin 1560 -> 0 bytes test/build/dir/test2.o | Bin 1496 -> 0 bytes test/build/main.o | Bin 1552 -> 0 bytes test/c_config.toml | 4 ++-- test/dir/test1.c | 8 -------- test/dir/test1.h | 6 ------ test/dir/test2.c | 6 ------ test/dir/test2.h | 6 ------ test/main | Bin 15560 -> 0 bytes test/main.c | 8 -------- 15 files changed, 67 insertions(+), 49 deletions(-) create mode 100644 c_config.toml delete mode 100644 test/build/dir/test1.o delete mode 100644 test/build/dir/test2.o delete mode 100644 test/build/main.o delete mode 100644 test/dir/test1.c delete mode 100644 test/dir/test1.h delete mode 100644 test/dir/test2.c delete mode 100644 test/dir/test2.h delete mode 100755 test/main delete mode 100644 test/main.c diff --git a/.gitignore b/.gitignore index ea8c4bf..84c4bf5 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +/test \ No newline at end of file diff --git a/c_config.toml b/c_config.toml new file mode 100644 index 0000000..27cd54f --- /dev/null +++ b/c_config.toml @@ -0,0 +1,9 @@ + +[general] +target = "test" +main = "main.c" + +[build] +build_dir = "build" +cc = "gcc" +cflags = "-Wall" diff --git a/src/compiler.rs b/src/compiler.rs index d01f30a..56fadc0 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -56,7 +56,7 @@ impl Compiler { let o_path_strip = o_path_dir.to_str().unwrap(); - println!("Compiling {}", file); + println!("Compiling {file}"); let mut c_path = path::Path::new(&file).parent().unwrap().components(); c_path.next(); @@ -85,7 +85,7 @@ impl Compiler { ); if verbose { - println!("{}", command); + println!("{command}"); } o_s.push(o_path_strip.to_string()); @@ -96,10 +96,10 @@ impl Compiler { let stderr = std::str::from_utf8(&out.stderr).unwrap(); if !stdout.is_empty() { - println!("{}", stdout); + println!("{stdout}"); } if !stderr.is_empty() { - println!("{}", stderr); + println!("{stderr}"); } } @@ -110,7 +110,7 @@ impl Compiler { ); for o_file in o_s { - link_command = format!("{} {}", link_command, o_file); + link_command = format!("{link_command} {o_file}"); } let target_path = path::Path::new(&self.path).join(&self.config.general.target); @@ -125,7 +125,7 @@ impl Compiler { println!("Building {}", target_path.to_str().unwrap()); if verbose { - println!("{}", link_command); + println!("{link_command}"); } let out = Command::new("sh") @@ -138,10 +138,10 @@ impl Compiler { let stderr = std::str::from_utf8(&out.stderr).unwrap(); if !stdout.is_empty() { - println!("{}", stdout); + println!("{stdout}"); } if !stderr.is_empty() { - println!("{}", stderr); + println!("{stderr}"); } } @@ -164,6 +164,8 @@ impl Compiler { .to_string(), ); + println!("{scanned_files:?}"); + (src_file, header_files) } @@ -301,7 +303,7 @@ impl Compiler { let mut inc_string = "".to_string(); for inc_dir in self.inc_dirs.clone() { - inc_string = format!("{} -I{}", inc_string, inc_dir); + inc_string = format!("{inc_string} -I{inc_dir}"); } inc_string diff --git a/src/init.rs b/src/init.rs index e30ade0..21adb49 100644 --- a/src/init.rs +++ b/src/init.rs @@ -1,7 +1,40 @@ -use std::{fs, path}; +use std::{fs, io::Write, path}; pub fn init(path: String) { - println!("Creating {}", path); + println!("Creating {path}"); fs::create_dir(path.clone()).unwrap(); fs::create_dir(path::Path::new(&path).join("src")).unwrap(); + let mut conf_file = fs::File::create(path::Path::new(&path).join("c_config.toml")).unwrap(); + conf_file + .write_all( + format!( + r#"[general] +target = "{path}" +main = "src/main.c" + +[build] +build_dir = "build" +cc = "gcc" +cflags = "-Wall" +"# + ) + .as_bytes(), + ) + .unwrap(); + conf_file.flush().unwrap(); + + let mut main_file = + fs::File::create(path::Path::new(&path).join("src").join("main.c")).unwrap(); + main_file + .write_all( + r#"#include + +int main(int argc, char **argv) { + printf("Hello world!\n"); +} +"# + .as_bytes(), + ) + .unwrap(); + main_file.flush().unwrap(); } diff --git a/src/main.rs b/src/main.rs index b1616fa..7a3bfb3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -61,8 +61,15 @@ fn main() { } else { let conf_path = path::Path::new(args.path.as_str()).join("c_config.toml"); - let contents = fs::read_to_string(conf_path.clone()) - .unwrap_or_else(|_| panic!("Couldn't read {}", conf_path.to_str().unwrap())); + // let contents = fs::read_to_string(conf_path.clone()) + // .unwrap_or_else(|_| panic!("Couldn't read {}", conf_path.to_str().unwrap())); + + let contents = if let Ok(s) = fs::read_to_string(conf_path.clone()) { + s + } else { + eprintln!("Error: Couldn't read c_config.toml"); + return; + }; let config: Config = toml::from_str(&contents).unwrap(); diff --git a/test/build/dir/test1.o b/test/build/dir/test1.o deleted file mode 100644 index 3a483a2b2674e9d4c919388c056511bc32fb7172..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1560 zcmbtTJ#Q015S_C_5=0UVq5wj%6#@xX_6fvCfeaSp3Pcep6++}3NWV@;AnWATuHHB>vt#vy0)wQ)}8`o9q zmX38x#m)G3Gj85N-W)H*3l%wrY7~vZNl#H=Utk|3dbdu|ej{34T38%$@D=8;APU>V zmBxo!XLa1I1_hr1O15=a*C^Sp#ttV2-88#R$G4*E9TVzhaMJEG-g zH`UhcThhJMrlhxuf^?o$MmOvC&47J_%o@$Jyjj?^`^MSZgFQXWv!TiD0q3BS*?IXY zPy43q<9|2B1u#o|qc3e|D4%2NXVlXbfXDMv{|^3 ztEpI}aT=qI`pJ3y+dUxllV2C$-FK(;XgBnmI%#U(=)?p>tF_va|9Ew6W6rNUQ-P}Z zK{^#dD4rm(J%kk3J zLTzmD4Ld-;g-~aJ{$BPo7q7eMwHDKEd$IehPUig69vFMTr_}43Y3Y1agR(hw_DquE zL)I5#FY@_!nGy1UFdNKDaGzxm{Bx@!QprKfgj>u_oberzV!azGqxTJ|W@tiETS-DH zjxwbYwOYE(wsvH+;+;HAOxXBj_3iea>cmk;$L4@zkV)^n{}6{Qo%Zp+m(K~XlR+m@ z)#@@=G0QsqZW>Lbhn$=i5n@kbWN?DOI?if)WiY zW4m3LZJMJRtw%?HqVG3=L`T2>LLcKZwm$xf(!DtB>Aa`IB;M{5t#&%E!$+$h)*pw> zXR4)|VXM)4-e@&m&=hk+KB&kERH5JmnA|Dw{CgaO1Yg!DIH(7Uvokk`oV>&s4&(to zZ|CaYDxKQ&1}DJ3M9^o7PA|&B)&IAcj8===PGaaW@8lPEV64WcKABp0^OxX+iK%X( zXs~Z`GCDU+nz1#omMWj~i;>*1IA(?!3Vz?gryV@BIO;enCQ|ufV&YBKCL8H3u`<0E zE29q#sa|YiQk!{BDoaOBH%$`V=U6{ATJa8_<|gj$TK{u@Uk$Q!pfhvG98}U<8=tZ` z(e@_SQz6r_L;OZp+SZXhNKD?9_={{{c5MNyKDLRG^>4EVByVZK;tws3ow(9-9mn?j zKj9c^UX|ZuE^1Efl6~p#zdgVIE6Z=NcEwmn{! - -void test1() { - printf("Hola desde test1\n"); - test2(); -} \ No newline at end of file diff --git a/test/dir/test1.h b/test/dir/test1.h deleted file mode 100644 index 503c897..0000000 --- a/test/dir/test1.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef TEST1_HEADER -#define TEST1_HEADER - -void test1(); - -#endif diff --git a/test/dir/test2.c b/test/dir/test2.c deleted file mode 100644 index 9f6eedf..0000000 --- a/test/dir/test2.c +++ /dev/null @@ -1,6 +0,0 @@ -#include "test2.h" -#include - -void test2() { - printf("Hola desde test2\n"); -} \ No newline at end of file diff --git a/test/dir/test2.h b/test/dir/test2.h deleted file mode 100644 index f2ba837..0000000 --- a/test/dir/test2.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef TEST2_HEADER -#define TEST2_HEADER - -void test2(); - -#endif diff --git a/test/main b/test/main deleted file mode 100755 index 79bcc7e712e965bcbd3024a7d17624743fc8eb28..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 15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uiArQdL6CNyS&^gavV^gnX4XJn`k zN#U6E&VcO**aHK)Se@o1T*t^na6fPMPhY zEv2hFZXxgG6=k&-1OL!DiGtMhoAT71LbdEn=Sv03VIMjLt3t8l%vaq4x+-Dkj9077 z)vxR5n6nTg``sBh+3&npuKU&m`>lS$KToMl^}?)|9|wAs0H>u^Dtm5bsx+UOtCZ*5 zig!U}Lecnq5&u{y7F4EKLMHy4(uULd>a@xfE|kz|fLH zezrIPAIj(%yNn(#=o&#*nThi3tXo1<{>MW1S-LmL{Y?H3BbYP#m;088k;1fve~E86 zf^yx7yh+WXCwMG7CC}{4<__B@*{|CwEU*@OqWFEW; zE|~~l{wK9^6hf3E0fc+u3%&zQlrQVT4dz=RGpGnpFbQ&$FYB^YYhW*aN;kp_+aQxn z{Fn7lGxJS*8Y@~FcziYwRl+ytukfQNb2lWTGRhr(hn4g@PQHjg^1DLoc<~+8uxw_7 YYOf5$8l#CdXnlT`FPu9Kg3(m}0-i6fu>b%7 diff --git a/test/main.c b/test/main.c deleted file mode 100644 index 3dd2cb6..0000000 --- a/test/main.c +++ /dev/null @@ -1,8 +0,0 @@ -#include - -#include "test1.h" - -int main() { - printf("Hola desde main\n"); - test1(); -} \ No newline at end of file