rust-cmaker/src/cbuild/config.rs

103 lines
2.6 KiB
Rust

use std::path::PathBuf;
pub trait BuildConfigTrait {
fn getpath_gcc(&self) -> String;
fn getpath_gpp(&self) -> String;
fn getpath_src(&self) -> String;
fn getpath_target_o(&self) -> String;
fn get_project_name(&self) -> &str;
fn getpath_target_bin(&self) -> String;
fn getpath_target_file(&self) -> String;
fn get_extra_final_args(&self) -> &Vec<String>;
fn need_run(&self) -> bool;
}
#[derive(Debug)]
pub struct BuildConfig {
compiler_path: Option<String>,
project_path: PathBuf,
project_name: String,
extra_final_args: Vec<String>,
need_run: bool,
}
impl BuildConfig {
pub fn new(
compiler_path: Option<String>,
project_name: String,
release: bool,
run: bool,
) -> Self {
let project_path = std::env::current_dir().unwrap();
let mut extra = Vec::new();
if release {
extra.push(String::from("-O3"));
}
Self {
compiler_path,
project_path,
project_name,
extra_final_args: extra,
need_run: run,
}
}
}
impl BuildConfigTrait for BuildConfig {
fn getpath_gcc(&self) -> String {
return self.getpath_gpp(); // 此处需要修改
}
fn getpath_gpp(&self) -> String {
let t = self.compiler_path.clone();
match t {
Some(str) => String::from(PathBuf::from(str).join("bin").join("g++").to_str().unwrap()),
None => String::from("g++"),
}
}
fn getpath_src(&self) -> String {
String::from(self.project_path.clone().join("src").to_str().unwrap())
}
fn getpath_target_o(&self) -> String {
String::from(
self.project_path
.clone()
.join("target")
.join("o")
.to_str()
.unwrap(),
)
}
fn get_project_name(&self) -> &str {
&self.project_name
}
fn getpath_target_bin(&self) -> String {
String::from(
self.project_path
.clone()
.join("target")
.join("bin")
.to_str()
.unwrap(),
)
}
fn getpath_target_file(&self) -> String {
let file_name = super::utils::make_app_file_name(self.get_project_name());
let ans = PathBuf::from(self.getpath_target_bin()).join(file_name);
String::from(ans.to_str().unwrap())
}
fn get_extra_final_args(&self) -> &Vec<String> {
return &self.extra_final_args;
}
fn need_run(&self) -> bool {
return self.need_run;
}
}