Skip to content

Commit

Permalink
citnames: simplify compilation module
Browse files Browse the repository at this point in the history
  • Loading branch information
rizsotto committed Jan 26, 2024
1 parent 1f05b31 commit 3fcaf32
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions source/citnames_rs/src/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,15 @@ impl TryFrom<Semantic> for Vec<Entry> {
match pass {
CompilerPass::Preprocess =>
Err(anyhow!("preprocess pass should not show up in results")),
CompilerPass::Compile { source, output, flags } => {
let mut arguments: Vec<String> = vec![];
// Assemble the arguments as it would be for a single source file.
arguments.push(into_string(&compiler)?);
for flag in flags {
arguments.push(flag.clone());
}
if let Some(file) = &output {
arguments.push(String::from("-o"));
arguments.push(into_string(file)?)
}
arguments.push(into_string(source)?);

CompilerPass::Compile { source, output, flags } =>
Ok(
Entry {
file: into_abspath(source.clone(), working_dir.as_path())?,
directory: working_dir.clone(),
output: into_abspath_opt(output.clone(), working_dir.as_path())?,
arguments: arguments.clone(),
arguments: into_arguments(&compiler, source, output, flags)?,
}
)
}
}
})
.collect();
Expand All @@ -70,6 +57,26 @@ impl TryFrom<Semantic> for Vec<Entry> {
}
}

fn into_arguments(
compiler: &PathBuf,
source: &PathBuf,
output: &Option<PathBuf>,
flags: &Vec<String>,
) -> Result<Vec<String>, anyhow::Error> {
let mut arguments: Vec<String> = vec![];
// Assemble the arguments as it would be for a single source file.
arguments.push(into_string(&compiler)?);
for flag in flags {
arguments.push(flag.clone());
}
if let Some(file) = output {
arguments.push(String::from("-o"));
arguments.push(into_string(file)?)
}
arguments.push(into_string(source)?);
Ok(arguments)
}

fn into_abspath(path: PathBuf, root: &Path) -> Result<PathBuf, std::io::Error> {
let candidate = if path.is_absolute() {
path.absolutize()
Expand All @@ -81,7 +88,7 @@ fn into_abspath(path: PathBuf, root: &Path) -> Result<PathBuf, std::io::Error> {

fn into_abspath_opt(path: Option<PathBuf>, root: &Path) -> Result<Option<PathBuf>, std::io::Error> {
path.map(|v| into_abspath(v, root))
.map_or(Ok(None), |v| v.map(Some))
.transpose()
}

fn into_string(path: &Path) -> Result<String> {
Expand Down

0 comments on commit 3fcaf32

Please sign in to comment.