删除了run指令以及多余的文件
This commit is contained in:
parent
aad0727e8a
commit
2df87f19c1
@ -1,46 +0,0 @@
|
|||||||
use std::path::PathBuf;
|
|
||||||
|
|
||||||
use super::config::BuildConfigTrait;
|
|
||||||
|
|
||||||
pub fn cmake_o(
|
|
||||||
config: &Box<dyn BuildConfigTrait>,
|
|
||||||
src_path: PathBuf,
|
|
||||||
file_o_set: &mut Vec<PathBuf>,
|
|
||||||
) -> 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(());
|
|
||||||
}
|
|
||||||
@ -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<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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -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<PathBuf>,
|
|
||||||
target_path: String,
|
|
||||||
extra_args: &Vec<String>,
|
|
||||||
) -> 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(())
|
|
||||||
}
|
|
||||||
@ -1,9 +1,5 @@
|
|||||||
mod cmake_o;
|
|
||||||
mod command;
|
mod command;
|
||||||
mod config;
|
|
||||||
mod files;
|
mod files;
|
||||||
mod gcc;
|
|
||||||
mod utils;
|
|
||||||
|
|
||||||
use crate::const_value::console_log;
|
use crate::const_value::console_log;
|
||||||
|
|
||||||
@ -32,19 +28,6 @@ pub struct CommandsBuild {
|
|||||||
c: bool,
|
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> {
|
pub fn run(config: CommandsBuild) -> Result<(), std::io::Error> {
|
||||||
// 获取项目路径
|
// 获取项目路径
|
||||||
let project_path = std::env::current_dir().unwrap();
|
let project_path = std::env::current_dir().unwrap();
|
||||||
|
|||||||
@ -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);
|
|
||||||
}
|
|
||||||
@ -24,8 +24,6 @@ enum Commands {
|
|||||||
New(cnew::CommandsNew),
|
New(cnew::CommandsNew),
|
||||||
/// 构建当前目录下的项目
|
/// 构建当前目录下的项目
|
||||||
Build(cbuild::CommandsBuild),
|
Build(cbuild::CommandsBuild),
|
||||||
/// `rcm build -q -r` 的简写
|
|
||||||
Run,
|
|
||||||
/// 保存项目为库项目并存储到仓库中
|
/// 保存项目为库项目并存储到仓库中
|
||||||
Store(cstore::CommandsStore),
|
Store(cstore::CommandsStore),
|
||||||
/// 在当前项目下添加库依赖
|
/// 在当前项目下添加库依赖
|
||||||
@ -41,7 +39,6 @@ fn main() -> std::io::Result<()> {
|
|||||||
match args.command {
|
match args.command {
|
||||||
Commands::New(config) => cnew::run(config),
|
Commands::New(config) => cnew::run(config),
|
||||||
Commands::Build(config) => cbuild::run(config),
|
Commands::Build(config) => cbuild::run(config),
|
||||||
Commands::Run => cbuild::run(cbuild::CommandsBuild::new_run()),
|
|
||||||
Commands::Store(config) => cstore::run(config),
|
Commands::Store(config) => cstore::run(config),
|
||||||
Commands::Add(config) => cadd::run(config),
|
Commands::Add(config) => cadd::run(config),
|
||||||
Commands::Test(config) => ctest::run(config),
|
Commands::Test(config) => ctest::run(config),
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user