first version of init done

This commit is contained in:
Guilleag01
2025-07-26 21:23:44 +02:00
parent 1a2bd851fe
commit 6df525f978
15 changed files with 67 additions and 49 deletions

1
.gitignore vendored
View File

@@ -1 +1,2 @@
/target /target
/test

9
c_config.toml Normal file
View File

@@ -0,0 +1,9 @@
[general]
target = "test"
main = "main.c"
[build]
build_dir = "build"
cc = "gcc"
cflags = "-Wall"

View File

@@ -56,7 +56,7 @@ impl Compiler {
let o_path_strip = o_path_dir.to_str().unwrap(); 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(); let mut c_path = path::Path::new(&file).parent().unwrap().components();
c_path.next(); c_path.next();
@@ -85,7 +85,7 @@ impl Compiler {
); );
if verbose { if verbose {
println!("{}", command); println!("{command}");
} }
o_s.push(o_path_strip.to_string()); o_s.push(o_path_strip.to_string());
@@ -96,10 +96,10 @@ impl Compiler {
let stderr = std::str::from_utf8(&out.stderr).unwrap(); let stderr = std::str::from_utf8(&out.stderr).unwrap();
if !stdout.is_empty() { if !stdout.is_empty() {
println!("{}", stdout); println!("{stdout}");
} }
if !stderr.is_empty() { if !stderr.is_empty() {
println!("{}", stderr); println!("{stderr}");
} }
} }
@@ -110,7 +110,7 @@ impl Compiler {
); );
for o_file in o_s { 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); 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()); println!("Building {}", target_path.to_str().unwrap());
if verbose { if verbose {
println!("{}", link_command); println!("{link_command}");
} }
let out = Command::new("sh") let out = Command::new("sh")
@@ -138,10 +138,10 @@ impl Compiler {
let stderr = std::str::from_utf8(&out.stderr).unwrap(); let stderr = std::str::from_utf8(&out.stderr).unwrap();
if !stdout.is_empty() { if !stdout.is_empty() {
println!("{}", stdout); println!("{stdout}");
} }
if !stderr.is_empty() { if !stderr.is_empty() {
println!("{}", stderr); println!("{stderr}");
} }
} }
@@ -164,6 +164,8 @@ impl Compiler {
.to_string(), .to_string(),
); );
println!("{scanned_files:?}");
(src_file, header_files) (src_file, header_files)
} }
@@ -301,7 +303,7 @@ impl Compiler {
let mut inc_string = "".to_string(); let mut inc_string = "".to_string();
for inc_dir in self.inc_dirs.clone() { 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 inc_string

View File

@@ -1,7 +1,40 @@
use std::{fs, path}; use std::{fs, io::Write, path};
pub fn init(path: String) { pub fn init(path: String) {
println!("Creating {}", path); println!("Creating {path}");
fs::create_dir(path.clone()).unwrap(); fs::create_dir(path.clone()).unwrap();
fs::create_dir(path::Path::new(&path).join("src")).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 <stdio.h>
int main(int argc, char **argv) {
printf("Hello world!\n");
}
"#
.as_bytes(),
)
.unwrap();
main_file.flush().unwrap();
} }

View File

@@ -61,8 +61,15 @@ fn main() {
} else { } else {
let conf_path = path::Path::new(args.path.as_str()).join("c_config.toml"); let conf_path = path::Path::new(args.path.as_str()).join("c_config.toml");
let contents = fs::read_to_string(conf_path.clone()) // let contents = fs::read_to_string(conf_path.clone())
.unwrap_or_else(|_| panic!("Couldn't read {}", conf_path.to_str().unwrap())); // .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(); let config: Config = toml::from_str(&contents).unwrap();

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -1,6 +1,6 @@
[general] [general]
target = "main" target = "test"
main = "main.c" main = "src/main.c"
[build] [build]
build_dir = "build" build_dir = "build"

View File

@@ -1,8 +0,0 @@
#include "test1.h"
#include "test2.h"
#include <stdio.h>
void test1() {
printf("Hola desde test1\n");
test2();
}

View File

@@ -1,6 +0,0 @@
#ifndef TEST1_HEADER
#define TEST1_HEADER
void test1();
#endif

View File

@@ -1,6 +0,0 @@
#include "test2.h"
#include <stdio.h>
void test2() {
printf("Hola desde test2\n");
}

View File

@@ -1,6 +0,0 @@
#ifndef TEST2_HEADER
#define TEST2_HEADER
void test2();
#endif

BIN
test/main

Binary file not shown.

View File

@@ -1,8 +0,0 @@
#include <stdio.h>
#include "test1.h"
int main() {
printf("Hola desde main\n");
test1();
}