-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Compilation fix under Ubuntu #109
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,9 +6,13 @@ use std::{ | |
process::Command, | ||
}; | ||
|
||
use semver::Version; | ||
use walkdir::WalkDir; | ||
|
||
use crate::constants::{GenerationMode, DEFAULT_TENDERDASH_COMMITISH, DEP_PROTOC_VERSION}; | ||
use crate::constants::{ | ||
GenerationMode, DEFAULT_TENDERDASH_COMMITISH, DEP_PROTOC_VERSION_OTHER, | ||
DEP_PROTOC_VERSION_UBUNTU, | ||
}; | ||
|
||
/// Check out a specific commitish of the tenderdash repository. | ||
/// | ||
|
@@ -360,53 +364,97 @@ pub(crate) fn check_state(dir: &Path, commitish: &str) -> bool { | |
} | ||
} | ||
|
||
fn get_required_protoc_version() -> &'static str { | ||
#[cfg(target_os = "linux")] | ||
{ | ||
// Further refine detection if needed | ||
// For example, detect if it's Ubuntu | ||
DEP_PROTOC_VERSION_UBUNTU | ||
} | ||
|
||
#[cfg(not(target_os = "linux"))] | ||
{ | ||
DEP_PROTOC_VERSION_OTHER | ||
} | ||
} | ||
|
||
/// Check if all dependencies are met | ||
pub(crate) fn check_deps() -> Result<(), String> { | ||
dep_protoc(DEP_PROTOC_VERSION).map(|_| ()) | ||
dep_protoc(get_required_protoc_version()).map(|_| ()) | ||
} | ||
|
||
/// Check if protoc is installed and has the required version | ||
fn dep_protoc(expected_version: f32) -> Result<f32, String> { | ||
let protoc = prost_build::protoc_from_env(); | ||
|
||
// Run `protoc --version` and capture the output | ||
let output = Command::new(protoc) | ||
fn dep_protoc(required_version_str: &str) -> Result<Version, String> { | ||
// Get the installed protoc version | ||
let output = std::process::Command::new("protoc") | ||
.arg("--version") | ||
.output() | ||
.map_err(|e| format!("failed to run: {}", e))?; | ||
.map_err(|e| format!("Failed to execute protoc: {}", e))?; | ||
|
||
// Convert the output to a string | ||
let out = output.stdout; | ||
let version_output = String::from_utf8(out.clone()) | ||
.map_err(|e| format!("output {:?} is not utf8 string: {}", out, e))?; | ||
let version_output = String::from_utf8(output.stdout) | ||
.map_err(|e| format!("Invalid UTF-8 output from protoc: {}", e))?; | ||
|
||
// Extract the version number from string like `libprotoc 25.1` | ||
let version: f32 = version_output | ||
// Extract the version number from the output | ||
// Assuming the output is like "libprotoc 3.12.4" | ||
let installed_version_str = version_output | ||
.trim() | ||
.split_whitespace() | ||
.last() | ||
.unwrap() | ||
.parse() | ||
.map_err(|e| format!("failed to parse protoc version {}: {}", version_output, e))?; | ||
.nth(1) | ||
.ok_or_else(|| "Failed to parse protoc version output".to_string())?; | ||
|
||
// Parse the versions | ||
let installed_version = | ||
Version::parse(&normalize_version(installed_version_str)).map_err(|e| { | ||
format!( | ||
"Failed to parse installed protoc version '{}': {}", | ||
installed_version_str, e | ||
) | ||
})?; | ||
|
||
if version < expected_version { | ||
let required_version = Version::parse(required_version_str).map_err(|e| { | ||
format!( | ||
"Failed to parse required protoc version '{}': {}", | ||
required_version_str, e | ||
) | ||
})?; | ||
|
||
// Compare versions | ||
if installed_version >= required_version { | ||
Ok(installed_version) | ||
} else { | ||
Err(format!( | ||
"protoc version must be {} or higher, but found {}; please upgrade: https://github.com/protocolbuffers/protobuf/releases/", | ||
expected_version, version | ||
"Installed protoc version {} is less than required version {}", | ||
installed_version, required_version | ||
)) | ||
} else { | ||
Ok(version) | ||
} | ||
} | ||
|
||
fn normalize_version(version_str: &str) -> String { | ||
let mut parts: Vec<&str> = version_str.split('.').collect(); | ||
while parts.len() < 3 { | ||
parts.push("0"); | ||
} | ||
parts.join(".") | ||
} | ||
Comment on lines
+431
to
+437
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle pre-release and build metadata in version strings The Apply this diff to remove the normalization and rely on -fn normalize_version(version_str: &str) -> String {
- let mut parts: Vec<&str> = version_str.split('.').collect();
- while parts.len() < 3 {
- parts.push("0");
- }
- parts.join(".")
-} Update the version parsing in let installed_version = Version::parse(installed_version_str)
.map_err(|e| format!("Failed to parse installed protoc version '{}': {}", installed_version_str, e))?;
-let required_version = Version::parse(required_version_str)
+let required_version = Version::parse(&normalize_version(required_version_str))
.map_err(|e| format!("Failed to parse required protoc version '{}': {}", required_version_str, e))?;
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_protoc_dep() { | ||
let expected_versions = vec![(10.1, true), (DEP_PROTOC_VERSION, true), (90.5, false)]; | ||
for expect in expected_versions { | ||
assert_eq!(dep_protoc(expect.0).is_ok(), expect.1); | ||
let expected_versions = vec![ | ||
("10.1.0", true), | ||
(DEP_PROTOC_VERSION_OTHER, true), | ||
("90.5.0", false), | ||
]; | ||
for &(required_version, expected_result) in &expected_versions { | ||
let result = dep_protoc(required_version); | ||
assert_eq!( | ||
result.is_ok(), | ||
expected_result, | ||
"Test case failed for required_version='{}', error='{:?}'", | ||
required_version, | ||
result.err() | ||
); | ||
Comment on lines
+444
to
+457
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Unit tests depend on the system's installed The For example, you can refactor -fn dep_protoc(required_version_str: &str) -> Result<Version, String> {
+fn dep_protoc<F>(required_version_str: &str, get_protoc_version_output: F) -> Result<Version, String>
+ where F: Fn() -> Result<String, String>
{
- let output = std::process::Command::new("protoc")
- .arg("--version")
- .output()
- .map_err(|e| format!("Failed to execute protoc: {}", e))?;
-
- let version_output = String::from_utf8(output.stdout)
- .map_err(|e| format!("Invalid UTF-8 output from protoc: {}", e))?;
+ let version_output = get_protoc_version_output()?;
// Parsing logic remains the same
} Then, in your tests, you can provide a mock function: #[test]
fn test_protoc_dep() {
let expected_versions = vec![
("10.1.0", true),
(DEP_PROTOC_VERSION_OTHER, true),
("90.5.0", false),
];
for &(required_version, expected_result) in &expected_versions {
let mock_output = Ok("libprotoc 28.0.0".to_string());
let result = dep_protoc(required_version, |_| mock_output.clone());
assert_eq!(
result.is_ok(),
expected_result,
"Test case failed for required_version='{}', error='{:?}'",
required_version,
result.err()
);
}
} |
||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Consider refining OS detection to specifically identify Ubuntu
Currently, the
get_required_protoc_version
function uses#[cfg(target_os = "linux")]
to determine if the operating system is Linux and then assumes it's Ubuntu by returningDEP_PROTOC_VERSION_UBUNTU
. This may not be accurate for all Linux distributions, as different distributions may have differentprotoc
versions. Consider implementing a more precise detection mechanism to specifically identify Ubuntu. This can help ensure that the correct requiredprotoc
version is used for the appropriate operating system.