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 3a483a2..0000000 Binary files a/test/build/dir/test1.o and /dev/null differ diff --git a/test/build/dir/test2.o b/test/build/dir/test2.o deleted file mode 100644 index e64fb33..0000000 Binary files a/test/build/dir/test2.o and /dev/null differ diff --git a/test/build/main.o b/test/build/main.o deleted file mode 100644 index b706fd1..0000000 Binary files a/test/build/main.o and /dev/null differ diff --git a/test/c_config.toml b/test/c_config.toml index 8f9ac59..5d8eb98 100644 --- a/test/c_config.toml +++ b/test/c_config.toml @@ -1,6 +1,6 @@ [general] -target = "main" -main = "main.c" +target = "test" +main = "src/main.c" [build] build_dir = "build" diff --git a/test/dir/test1.c b/test/dir/test1.c deleted file mode 100644 index 23b7a47..0000000 --- a/test/dir/test1.c +++ /dev/null @@ -1,8 +0,0 @@ -#include "test1.h" -#include "test2.h" -#include - -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 79bcc7e..0000000 Binary files a/test/main and /dev/null differ 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