Skip to content

Commit

Permalink
Fix clippy
Browse files Browse the repository at this point in the history
Signed-off-by: Phani Sajja <[email protected]>
  • Loading branch information
sajjaphani committed Jul 17, 2024
1 parent 621247e commit 0f3c5f5
Showing 1 changed file with 41 additions and 37 deletions.
78 changes: 41 additions & 37 deletions src/bin/hab-ld64-wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,16 @@ use exec::Command;
use hab_pkg_wrappers::{env::CommonEnvironment, opts_parser, util::PrefixedArg};
use path_absolutize::Absolutize;

#[derive(Debug, PartialEq)]
#[derive(Default, Debug, PartialEq)]
pub enum LinkMode {
/// Ensures every path in the LD_RUN_PATH environment variable is added to the rpath of binaries
#[default]
Complete,
/// Adds paths in the LD_RUN_PATH environment variable to the rpath of binaries only
/// if the path actually contains a linked library
Minimal,
}

impl Default for LinkMode {
fn default() -> Self {
LinkMode::Complete
}
}

impl Display for LinkMode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Expand Down Expand Up @@ -63,7 +58,7 @@ impl Default for LDEnvironment {
allowed_impure_paths: std::env::var("HAB_ALLOWED_IMPURE_PATHS")
.map(|value| {
value
.split(":")
.split(':')
.map(PathBuf::from)
.collect::<Vec<PathBuf>>()
})
Expand All @@ -75,7 +70,7 @@ impl Default for LDEnvironment {
ld_run_path: std::env::var("HAB_LD_RUN_PATH")
.map(|value| {
value
.split(":")
.split(':')
.filter(|p| !p.is_empty())
.map(PathBuf::from)
.collect::<Vec<PathBuf>>()
Expand Down Expand Up @@ -136,22 +131,22 @@ where
|| path.starts_with(
env.common
.fs_root
.join(&env.tmp.components().skip(1).collect::<PathBuf>()),
.join(env.tmp.components().skip(1).collect::<PathBuf>()),
)
|| path.starts_with(
env.common
.fs_root
.join(&env.tmpdir.components().skip(1).collect::<PathBuf>()),
.join(env.tmpdir.components().skip(1).collect::<PathBuf>()),
)
|| path.starts_with(
env.common
.fs_root
.join(&env.temp.components().skip(1).collect::<PathBuf>()),
.join(env.temp.components().skip(1).collect::<PathBuf>()),
)
|| path.starts_with(
env.common
.fs_root
.join(&env.tempdir.components().skip(1).collect::<PathBuf>()),
.join(env.tempdir.components().skip(1).collect::<PathBuf>()),
))
}
fn install_name(&self, env: &LDEnvironment) -> PathBuf {
Expand All @@ -169,7 +164,7 @@ where
fn is_allowed_impure_path(&self, env: &LDEnvironment) -> bool {
let path = self.as_ref();
for dir in env.allowed_impure_paths.iter() {
if path.starts_with(&dir) {
if path.starts_with(dir) {
return true;
}
}
Expand Down Expand Up @@ -202,7 +197,7 @@ where
}

fn is_static_library(&self) -> bool {
self.as_ref().ends_with(".a")
self.as_ref().extension().is_some_and(|ext| ext == "a")
}
}

Expand All @@ -215,6 +210,7 @@ enum LibraryReference {

/// The type of output generated by the linker
#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy, Default)]
#[allow(clippy::enum_variant_names)]
enum LinkerOutputFormat {
#[default]
MachOExcecutable,
Expand Down Expand Up @@ -256,6 +252,7 @@ struct LibraryReferenceState {
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[allow(clippy::enum_variant_names)]
enum LibraryLinkType {
StaticLibraryLinked,
DynamicLibraryLinked,
Expand Down Expand Up @@ -327,7 +324,7 @@ impl<'a> LDArgument<'a> {
(_, "-search_dylibs_first") => Some(LDArgument::SearchDylibsFirst),
// File with linker arguments
(_, current_value) if current_value.is_prefixed_with("@") => Some(
LDArgument::LinkArgsFile(current_value.strip_prefix("@").unwrap()),
LDArgument::LinkArgsFile(current_value.strip_prefix('@').unwrap()),
),
// Output argument
(Some("-o"), current_value) => Some(LDArgument::Output(current_value)),
Expand All @@ -344,13 +341,13 @@ impl<'a> LDArgument<'a> {
),
// System Library Root arguments
(Some("-syslibroot"), current_value) => {
Some(LDArgument::SystemLibraryRoot(&current_value))
Some(LDArgument::SystemLibraryRoot(current_value))
}
// RPath arguments
(Some("-rpath"), current_value) => Some(LDArgument::RPath(current_value, false)),
// Library arguments
(Some("-dylib_file"), current_value) => {
let mut parts = current_value.split(":");
let mut parts = current_value.split(':');
let install_name = parts.next().unwrap();
let file_name = parts.next().unwrap();
Some(LDArgument::AlternateLibraryFilePath(
Expand All @@ -369,7 +366,7 @@ impl<'a> LDArgument<'a> {
current_value,
) => Some(LDArgument::LibraryFilePath(current_value, false)),
(Some("-force_load" | "-load_hidden"), current_value) => {
Some(LDArgument::LibraryFilePath(&current_value, false))
Some(LDArgument::LibraryFilePath(current_value, false))
}
// Ignore these if they are the current argument to prevent it from being mistaken for a -lx library argument
(_, "-load_hidden" | "-lazy_framework" | "-lazy_library" | "-lto_library") => None,
Expand Down Expand Up @@ -407,7 +404,7 @@ fn effective_library_name(library_reference: &LibraryReference) -> Option<String
.file_name()
.and_then(|file_name| file_name.to_str())
.and_then(|file_name| file_name.strip_prefix("lib"))
.and_then(|file_name| file_name.split_once("."))
.and_then(|file_name| file_name.split_once('.'))
.map(|(library_name, _)| library_name.to_string()),
}
}
Expand All @@ -429,7 +426,7 @@ fn parse_linker_arguments(
let mut skip_argument = false;
let mut skip_prev_argument = false;
if let Some(known_argument) =
LDArgument::parse(previous_argument.as_ref().map(|x| x.as_str()), &argument)
LDArgument::parse(previous_argument.as_deref(), &argument)
{
match known_argument {
LDArgument::Output(_) | LDArgument::InstallName(_) | LDArgument::LTOLibrary(_) => {
Expand Down Expand Up @@ -647,6 +644,7 @@ fn parse_linker_arguments(
});
}
} else if library_file_path.is_static_library() {
#[allow(clippy::collapsible_if)]
if library_file_path.is_file() {
library_state.results.push(LibraryLinkResult {
link_type: LibraryLinkType::StaticLibraryLinked,
Expand Down Expand Up @@ -695,6 +693,7 @@ fn parse_linker_arguments(
});
}
} else if library_file_path.is_static_library() {
#[allow(clippy::collapsible_if)]
if library_file_path.is_file() {
library_state.results.push(LibraryLinkResult {
link_type: LibraryLinkType::StaticLibraryLinked,
Expand Down Expand Up @@ -777,6 +776,7 @@ fn parse_linker_arguments(
.unwrap()
.to_path_buf();
if library_file_path.is_shared_library() {
#[allow(clippy::collapsible_if)]
if library_file_path.is_file() {
library_state.results.push(LibraryLinkResult {
link_type: if cfg!(target_os = "macos") {
Expand Down Expand Up @@ -812,6 +812,7 @@ fn parse_linker_arguments(
.unwrap()
.to_path_buf();
if library_file_path.is_shared_library() {
#[allow(clippy::collapsible_if)]
if library_file_path.is_file() {
library_state.results.push(LibraryLinkResult {
link_type: if cfg!(target_os = "macos") {
Expand Down Expand Up @@ -874,6 +875,7 @@ fn parse_linker_arguments(
.unwrap()
.to_path_buf();
if library_file_path.is_static_library() {
#[allow(clippy::collapsible_if)]
if library_file_path.is_file() {
library_state.results.push(LibraryLinkResult {
link_type: LibraryLinkType::StaticLibraryLinked,
Expand All @@ -899,6 +901,7 @@ fn parse_linker_arguments(
.unwrap()
.to_path_buf();
if library_file_path.is_static_library() {
#[allow(clippy::collapsible_if)]
if library_file_path.is_file() {
library_state.results.push(LibraryLinkResult {
link_type: LibraryLinkType::StaticLibraryLinked,
Expand Down Expand Up @@ -973,6 +976,7 @@ fn parse_linker_arguments(
}
}
if library_will_link {
#[allow(clippy::if_same_then_else)]
if final_link_result.is_none() {
final_link_result = Some(result.clone());
} else if library_is_available_at_runtime {
Expand Down Expand Up @@ -1027,7 +1031,8 @@ fn parse_linker_arguments(
if let Some(prefix) = env.prefix.as_ref() {
for run_path in env.ld_run_path.iter() {
if run_path.starts_with(prefix) {
if !rpaths.contains(&run_path) && !additional_rpaths.contains(&run_path) {
#[allow(clippy::collapsible_if)]
if !rpaths.contains(run_path) && !additional_rpaths.contains(run_path) {
additional_rpaths.push(run_path.clone())
}
}
Expand All @@ -1037,7 +1042,7 @@ fn parse_linker_arguments(
// Ensure all paths in the LD_RUN_PATH are added to the rpath
if env.ld_link_mode == LinkMode::Complete {
for search_dir in env.ld_run_path.iter() {
if !rpaths.contains(&search_dir) && !additional_rpaths.contains(&search_dir) {
if !rpaths.contains(search_dir) && !additional_rpaths.contains(search_dir) {
additional_rpaths.push(search_dir.clone())
}
}
Expand Down Expand Up @@ -1131,8 +1136,7 @@ fn main() {
} else {
vec![argument]
}
})
.into_iter(),
}),
&env,
);

Expand All @@ -1150,42 +1154,42 @@ fn main() {
.create(true)
.open(debug_log_file)
.expect("Failed to open debug output log file");
write!(
writeln!(
&mut file,
"work_dir: {}\n",
"work_dir: {}",
std::env::current_dir().unwrap().display()
)
.unwrap();
write!(
writeln!(
&mut file,
"original: {}\n",
"original: {}",
std::env::args().skip(1).collect::<Vec<String>>().join(" ")
)
.unwrap();
write!(
writeln!(
&mut file,
"wrapped: {} {}\n",
"wrapped: {} {}",
program,
parsed_arguments.join(" ")
)
.unwrap();
} else {
let mut file = std::io::stderr().lock();
write!(
writeln!(
&mut file,
"work_dir: {}\n",
"work_dir: {}",
std::env::current_dir().unwrap().display()
)
.unwrap();
write!(
writeln!(
&mut file,
"original: {}\n",
"original: {}",
std::env::args().skip(1).collect::<Vec<String>>().join(" ")
)
.unwrap();
write!(
writeln!(
&mut file,
"wrapped: {} {}\n",
"wrapped: {} {}",
program,
parsed_arguments.join(" ")
)
Expand Down

0 comments on commit 0f3c5f5

Please sign in to comment.