Skip to content

Commit

Permalink
Better get_srcs and arrange_targets
Browse files Browse the repository at this point in the history
  • Loading branch information
BeichenY1 committed Mar 8, 2024
1 parent 5fb3fdb commit d444645
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 191 deletions.
2 changes: 1 addition & 1 deletion doc/ruxgo_book/src/installation.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Ruxgo 安装

要从源代码构建`ruxgo`可执行文件,你首先需要安装 Rust 和 Cargo。按照[Rust安装页面](https://www.rust-lang.org/tools/install)上的说明操作。Ruxgo 目前至少需要 Rust 1.70 版本。
要从源代码构建`ruxgo`可执行文件,你首先需要安装 Rust 和 Cargo。按照[Rust安装页面](https://www.rust-lang.org/tools/install)上的说明操作。Ruxgo 目前至少需要 Rust 1.74 版本。

一旦你安装了 Rust,就可以使用以下命令来构建和安装 Ruxgo:

Expand Down
115 changes: 44 additions & 71 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,23 +565,27 @@ impl<'a> Target<'a> {
cmd.push(' ');
cmd.push_str(RUXLIBC_BIN);
cmd.push(' ');
let mode = if !self.os_config.platform.mode.is_empty() {
&self.os_config.platform.mode
} else {
"debug"
};
cmd.push_str(&format!(
"{}/target/{}/{}/{}",
BUILD_DIR,
&self.os_config.platform.target,
&self.os_config.platform.mode,
RUXLIBC_RUST_LIB
BUILD_DIR, &self.os_config.platform.target, mode, RUXLIBC_RUST_LIB
));
} else if self.os_config.ulib == "ruxmusl" {
cmd.push(' ');
cmd.push_str(RUXMUSL_BIN);
cmd.push(' ');
let mode = if !self.os_config.platform.mode.is_empty() {
&self.os_config.platform.mode
} else {
"debug"
};
cmd.push_str(&format!(
"{}/target/{}/{}/{}",
BUILD_DIR,
&self.os_config.platform.target,
&self.os_config.platform.mode,
RUXMUSL_RUST_LIB
BUILD_DIR, &self.os_config.platform.target, mode, RUXMUSL_RUST_LIB
));
}

Expand Down Expand Up @@ -776,73 +780,42 @@ impl<'a> Target<'a> {
/// Recursively gets all the source files in the given root path
/// # Notes
/// The source is first filtered through the `src_only` and `src_exclude` fields
fn get_srcs(&mut self, root_path: &str) -> Vec<Src> {
if root_path.is_empty() {
return Vec::new();
}
let root_dir = PathBuf::from(root_path);
let mut srcs: Vec<Src> = Vec::new();
let root_entries = std::fs::read_dir(root_dir).unwrap_or_else(|_| {
log(
LogLevel::Error,
&format!("Could not read directory: {}", root_path),
);
std::process::exit(1);
});
let src_only: Vec<&str> = self
.target_config
.src_only
.iter()
.map(AsRef::as_ref)
.collect();
let src_exclude: Vec<&str> = self
.target_config
.src_exclude
.iter()
.map(AsRef::as_ref)
.collect();

// Iterate over all entrys
for entry in root_entries {
let entry = entry.unwrap();
let path = entry
.path()
.to_str()
.unwrap()
.to_string()
.replace('\\', "/"); // if windows's path
// Exclusion logic: Check if the path is in src_exclude
let exclude = src_exclude.iter().any(|&excluded| path.contains(excluded));
if exclude {
log(
LogLevel::Debug,
&format!("Excluding (in src_exclude): {}", path),
);
fn get_srcs(&mut self, root_path: &str) {
for entry in WalkDir::new(root_path).into_iter().filter_map(|e| e.ok()) {
let path = entry.path();
let path_str = path.to_str().unwrap_or_default();
#[cfg(target_os = "windows")]
let path_str = path_str.replace('\\', "/");
if self.should_exclude(path_str) {
continue;
}
if entry.path().is_dir() {
srcs.append(&mut self.get_srcs(&path));
} else {
// Inclusion logic: Apply src_only logic only to files
let include = if !src_only.is_empty() {
src_only.iter().any(|&included| path.contains(included))
} else {
true // If src_only is empty, include all
};
if !include {
log(
LogLevel::Debug,
&format!("Excluding (not in src_only): {}", path),
);
continue;
}
if path.ends_with(".cpp") || path.ends_with(".c") {
self.add_src(path);
if path.is_file() {
if let Some(ext) = path.extension() {
if (ext == "cpp" || ext == "c") && self.should_include(path_str) {
self.add_src(path_str.to_owned());
}
}
}
}
}

srcs
/// Exclusion logic: Check if the path is in src_exclude
fn should_exclude(&self, path: &str) -> bool {
self.target_config
.src_exclude
.iter()
.any(|excluded| path.contains(excluded))
}

/// Inclusion logic: Apply src_only logic only to files
fn should_include(&self, path: &str) -> bool {
if self.target_config.src_only.is_empty() {
return true;
}
self.target_config
.src_only
.iter()
.any(|included| path.contains(included))
}

/// Adds a source file to the target's srcs field
Expand All @@ -867,7 +840,7 @@ impl<'a> Target<'a> {
fn get_src_obj_name(&self, src_name: &str) -> String {
let mut obj_name = String::new();
obj_name.push_str(OBJ_DIR);
obj_name.push('-');
obj_name.push('/');
obj_name.push_str(&self.target_config.name);
obj_name.push('-');
obj_name.push_str(src_name);
Expand All @@ -878,7 +851,7 @@ impl<'a> Target<'a> {
/// Returns a vector of .h or .hpp files the given C/C++ depends on
fn get_dependant_includes(&mut self, path: &str) -> Vec<String> {
let mut result = HashSet::new();
//Use the stack to handle recursive paths
// Use the stack to handle recursive paths
let mut to_process = vec![path.to_string()];
let include_substrings: HashSet<String> = self
.get_include_substrings(path)
Expand Down
12 changes: 6 additions & 6 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,10 @@ fn build_os(os_config: &OSConfig, ulib: &str, rux_feats: &[String], lib_feats: &
}

let target = format!("--target {}", os_config.platform.target);
let mode = format!("--{}", os_config.platform.mode);
let mut mode = String::new();
if !os_config.platform.mode.is_empty() {
mode = format!("--{}", os_config.platform.mode);
}
let os_ulib = format!("-p {}", ulib);
let verbose = match os_config.platform.v.as_str() {
"1" => "-v",
Expand Down Expand Up @@ -1024,16 +1027,13 @@ pub fn init_project(project_name: &str, is_c: Option<bool>, config: &GlobalConfi
/// Parses the config file of local project
pub fn parse_config() -> (BuildConfig, OSConfig, Vec<TargetConfig>) {
#[cfg(target_os = "linux")]
let (build_config, os_config, targets) = parser::parse_config("./config_linux.toml", true);
let (build_config, os_config, targets) = parser::parse_config("./config_linux.toml", false);
#[cfg(target_os = "windows")]
let (build_config, os_config, targets) = utils::parse_config("./config_win32.toml", true);

let mut num_exe = 0;
let mut exe_target: Option<&TargetConfig> = None;
if targets.is_empty() {
log(LogLevel::Error, "No targets in config");
std::process::exit(1);
}

for target in &targets {
if target.typ == "exe" {
num_exe += 1;
Expand Down
Loading

0 comments on commit d444645

Please sign in to comment.