Skip to content

Commit

Permalink
format_output_filename: Use PathBuf instead of Strings
Browse files Browse the repository at this point in the history
Co-authored-by: flip1995 <hello@philkrones.com>
  • Loading branch information
CohenArthur and flip1995 committed Jun 11, 2021
1 parent fd5f694 commit 990a7d4
Showing 1 changed file with 36 additions and 24 deletions.
60 changes: 36 additions & 24 deletions src/gccrs/args.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! This module interprets arguments given to `rustc` and transforms them into valid
//! arguments for `gccrs`.

use std::path::PathBuf;

use getopts::{Matches, Options};

use super::{Error, Result};
Expand Down Expand Up @@ -34,36 +36,38 @@ impl CrateType {
fn format_output_filename(
matches: &Matches,
crate_type: CrateType,
) -> Result<(String, CrateType)> {
) -> Result<(PathBuf, CrateType)> {
// Return an [`Error::InvalidArg`] error if `--crate-name` or `out-dir` weren't
// given as arguments at this point of the translation
let crate_name = matches.opt_str("crate-name").ok_or(Error::InvalidArg)?;
let out_dir = matches.opt_str("out-dir").ok_or(Error::InvalidArg)?;

// FIXME: Figure out a way to return multiple output filenames. Just handle `bin`
// for now
let c_options = matches.opt_strs("C");

// FIXME: This is probably different on Windows, we should use Paths and PathStrs
// instead
let mut output_file = match crate_type {
CrateType::Bin => format!("{}/{}", out_dir, crate_name),
CrateType::DyLib => format!("{}/lib{}.so", out_dir, crate_name),
CrateType::StaticLib => format!("{}/lib{}.a", out_dir, crate_name),
let mut output_file = PathBuf::from(&out_dir);

// FIXME: Horrendous. We need to create a separate "C options" parser since we'll
// probably use more than just `extra-filename`. Issue #6 on Rust-GCC/cargo-gccrs
let extra_filename = c_options
.iter()
.filter_map(|c_opt| {
let mut split = c_opt.split('=');

if let Some("extra-filename") = split.next().as_deref() {
split.next()
} else {
None
}
})
.collect::<Vec<&str>>()[0];

match crate_type {
CrateType::Bin => output_file.push(&format!("{}{}", crate_name, extra_filename)),
CrateType::DyLib => output_file.push(&format!("lib{}.so{}", crate_name, extra_filename)),
CrateType::StaticLib => output_file.push(&format!("lib{}.a{}", crate_name, extra_filename)),
_ => unreachable!(
"gccrs cannot handle other crate types than bin, dylib or staticlib at the moment"
),
};

// FIXME: Horrendous. We need to create a separate "C options" parser since we'll
// probably use more than just `extra-filename`.
c_options.iter().for_each(|c_opt| {
let mut split = c_opt.split('=');

if let Some("extra-filename") = split.next().as_deref() {
output_file.push_str(&split.next().unwrap())
}
});
}

Ok((output_file, crate_type))
}
Expand All @@ -73,7 +77,7 @@ fn format_output_filename(
pub struct GccrsArgs {
source_files: Vec<String>,
crate_type: CrateType,
output_file: String,
output_file: PathBuf,
}

impl GccrsArgs {
Expand Down Expand Up @@ -128,12 +132,20 @@ impl GccrsArgs {
pub fn into_args(self) -> Vec<String> {
let mut args = self.source_files;

// FIXME: How does gccrs behave with non-unicode filenames? Is gcc[rs] available
// on the OSes that support non-unicode filenames?
let output_file = self
.output_file
.to_str()
.expect("Cannot handle non-unicode filenames yet")
.to_owned();

match self.crate_type {
CrateType::Bin => args.append(&mut vec![String::from("-o"), self.output_file]),
CrateType::Bin => args.append(&mut vec![String::from("-o"), output_file]),
CrateType::DyLib => args.append(&mut vec![
String::from("-shared"),
String::from("-o"),
self.output_file,
output_file,
]),
CrateType::StaticLib => unreachable!("Cannot generate static libraries yet"),
_ => {}
Expand Down

0 comments on commit 990a7d4

Please sign in to comment.