From 2df87f19c1dccd4325e084ce307c7967cec12720 Mon Sep 17 00:00:00 2001 From: Cloyir Date: Fri, 12 May 2023 09:54:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E4=BA=86run=E6=8C=87?= =?UTF-8?q?=E4=BB=A4=E4=BB=A5=E5=8F=8A=E5=A4=9A=E4=BD=99=E7=9A=84=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cbuild/cmake_o.rs | 46 ------------------- src/cbuild/config.rs | 102 ------------------------------------------ src/cbuild/gcc.rs | 75 ------------------------------- src/cbuild/mod.rs | 17 ------- src/cbuild/utils.rs | 43 ------------------ src/main.rs | 3 -- 6 files changed, 286 deletions(-) delete mode 100644 src/cbuild/cmake_o.rs delete mode 100644 src/cbuild/config.rs delete mode 100644 src/cbuild/gcc.rs delete mode 100644 src/cbuild/utils.rs diff --git a/src/cbuild/cmake_o.rs b/src/cbuild/cmake_o.rs deleted file mode 100644 index 168f39d..0000000 --- a/src/cbuild/cmake_o.rs +++ /dev/null @@ -1,46 +0,0 @@ -use std::path::PathBuf; - -use super::config::BuildConfigTrait; - -pub fn cmake_o( - config: &Box, - src_path: PathBuf, - file_o_set: &mut Vec, -) -> Result<(), std::io::Error> { - // 遍历文件夹内每个子项 - let dir = std::fs::read_dir(src_path)?; - for path in dir { - let path = path?; - // 如果子项为文件夹, 递归查找内部项 - if path.file_type()?.is_dir() { - cmake_o(config, path.path(), file_o_set)?; - continue; - } - // 判断是否为需要编译的文件, 即.c/.cpp文件 - let file_path = path; - let file_name = String::from(file_path.file_name().to_str().unwrap()); - let file_type = super::utils::file_is_c_or_cpp(&file_name); - if file_type.is_else() { - continue; - } - // 找到需要编译的文件, 将其输出到target/o目录下 - let new_file_path = - PathBuf::from(config.getpath_target_o()).join((*file_o_set).len().to_string() + ".o"); - - use super::gcc::make_file_to_target_o; - match file_type { - super::utils::FileType::C => { - make_file_to_target_o(config.getpath_gcc(), &file_path.path(), &new_file_path)?; - } - super::utils::FileType::Cpp => { - make_file_to_target_o(config.getpath_gpp(), &file_path.path(), &new_file_path)?; - } - super::utils::FileType::Else => { - continue; - } - } - - file_o_set.push(new_file_path); - } - return Ok(()); -} diff --git a/src/cbuild/config.rs b/src/cbuild/config.rs deleted file mode 100644 index 402e4c9..0000000 --- a/src/cbuild/config.rs +++ /dev/null @@ -1,102 +0,0 @@ -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; - fn need_run(&self) -> bool; -} - -#[derive(Debug)] -pub struct BuildConfig { - compiler_path: Option, - project_path: PathBuf, - project_name: String, - extra_final_args: Vec, - need_run: bool, -} - -impl BuildConfig { - pub fn new( - compiler_path: Option, - 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 { - return &self.extra_final_args; - } - - fn need_run(&self) -> bool { - return self.need_run; - } -} diff --git a/src/cbuild/gcc.rs b/src/cbuild/gcc.rs deleted file mode 100644 index 8537597..0000000 --- a/src/cbuild/gcc.rs +++ /dev/null @@ -1,75 +0,0 @@ -use std::path::{Path, PathBuf}; - -// 根据单个.c/.cpp文件的路径创建.o文件并输出到new_file_path -pub fn make_file_to_target_o( - command: String, - source_file_path: &PathBuf, - new_file_path: &PathBuf, -) -> Result<(), std::io::Error> { - // 获取路径 - let source_file_path = source_file_path.to_str().unwrap(); - let new_file_path = new_file_path.to_str().unwrap(); - - // 运行指令 - let mut output = std::process::Command::new(command); - output.args(["-o", new_file_path]); - output.args(["-c", source_file_path]); - let output = output.output()?; - - // 输出调试信息 - let state = if output.status.success() { - format!( - "{}: success to compile 编译成功", - crate::const_value::THIS_PROJECT_NAME - ) - } else { - format!( - "{}: failed to compile 编译失败 \n错误信息: {:?}", - crate::const_value::THIS_PROJECT_NAME, - String::from_utf8(output.stderr).unwrap() - ) - }; - println!( - "{}: file : {:?} ---> new file : {:?} ---> {}", - crate::const_value::THIS_PROJECT_NAME, - Path::new(source_file_path).file_name().unwrap(), - Path::new(new_file_path).file_name().unwrap(), - state - ); - Ok(()) -} - -// 根据.o文件的集合构建可执行程序 -pub fn make_o_files_to_bin( - command: String, - source_paths: &mut Vec, - target_path: String, - extra_args: &Vec, -) -> Result<(), std::io::Error> { - // 创建指令 - let mut output = std::process::Command::new(command); - while !source_paths.is_empty() { - let item = source_paths.pop().unwrap(); - output.arg(item.to_str().unwrap()); - } - for item in extra_args { - output.arg(item); - } - output.args(["-o", &target_path]); - - // 运行指令 - let output = output.output()?; - if output.status.success() { - println!( - "{}: success to compile 编译成功", - crate::const_value::THIS_PROJECT_NAME - ); - } else { - println!( - "{}: failed to compile 编译失败\n{:?}", - crate::const_value::THIS_PROJECT_NAME, - output - ); - } - Ok(()) -} diff --git a/src/cbuild/mod.rs b/src/cbuild/mod.rs index a7b2619..9ad5112 100644 --- a/src/cbuild/mod.rs +++ b/src/cbuild/mod.rs @@ -1,9 +1,5 @@ -mod cmake_o; mod command; -mod config; mod files; -mod gcc; -mod utils; use crate::const_value::console_log; @@ -32,19 +28,6 @@ pub struct CommandsBuild { c: bool, } -impl CommandsBuild { - /// rcm run指令 - pub fn new_run() -> CommandsBuild { - CommandsBuild { - quiet: true, - release: false, - run: true, - mingw: None, - c: false, - } - } -} - pub fn run(config: CommandsBuild) -> Result<(), std::io::Error> { // 获取项目路径 let project_path = std::env::current_dir().unwrap(); diff --git a/src/cbuild/utils.rs b/src/cbuild/utils.rs deleted file mode 100644 index 8d7fbf6..0000000 --- a/src/cbuild/utils.rs +++ /dev/null @@ -1,43 +0,0 @@ -pub enum FileType { - C, - Cpp, - Else, -} - -impl FileType { - pub fn is_else(&self) -> bool { - match self { - Self::Else => { - return true; - } - _ => { - return false; - } - } - } -} - -// 判断文件名是否为.c或.cpp结尾 -pub fn file_is_c_or_cpp(file_name: &String) -> FileType { - let t: Vec<&u8> = file_name.as_bytes().iter().collect(); - if t.len() >= 2 && *t[t.len() - 2] == b'.' && *t[t.len() - 1] == b'c' { - return FileType::C; - } - if t.len() >= 4 - && *t[t.len() - 4] == b'.' - && *t[t.len() - 3] == b'c' - && *t[t.len() - 2] == b'p' - && *t[t.len() - 1] == b'p' - { - return FileType::Cpp; - } - return FileType::Else; -} - -// 生成可执行程序的文件名(仅windows上有.exe结尾) -pub fn make_app_file_name(file_name: &str) -> String { - if cfg!(target_os = "windows") { - return String::from(file_name) + ".exe"; - } - return String::from(file_name); -} diff --git a/src/main.rs b/src/main.rs index 878111f..f9c82cf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,8 +24,6 @@ enum Commands { New(cnew::CommandsNew), /// 构建当前目录下的项目 Build(cbuild::CommandsBuild), - /// `rcm build -q -r` 的简写 - Run, /// 保存项目为库项目并存储到仓库中 Store(cstore::CommandsStore), /// 在当前项目下添加库依赖 @@ -41,7 +39,6 @@ fn main() -> std::io::Result<()> { match args.command { Commands::New(config) => cnew::run(config), Commands::Build(config) => cbuild::run(config), - Commands::Run => cbuild::run(cbuild::CommandsBuild::new_run()), Commands::Store(config) => cstore::run(config), Commands::Add(config) => cadd::run(config), Commands::Test(config) => ctest::run(config),