Skip to content

Commit

Permalink
yeet dependencies_writer and integrate the logic into the main linker…
Browse files Browse the repository at this point in the history
…_writer
  • Loading branch information
AngheloAlf committed Mar 12, 2024
1 parent d63d3d3 commit c6daff1
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 126 deletions.
4 changes: 1 addition & 3 deletions slinky-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ fn main() {
// println!("settings {:#?}", document.settings);

let mut writer = slinky::LinkerWriter::new(&document.settings);
let mut dependencies_writer = slinky::DependenciesWriter::new(&document.settings);
writer.begin_sections();
for segment in &document.segments {
writer.add_segment(segment);
dependencies_writer.add_segment(segment);
}
writer.end_sections();

Expand All @@ -45,7 +43,7 @@ fn main() {

if let Some(d_path) = &document.settings.d_path {
if let Some(target_path) = &document.settings.target_path {
dependencies_writer
writer
.save_dependencies_file(d_path, target_path)
.expect("Error writing dependencies file");
}
Expand Down
118 changes: 0 additions & 118 deletions slinky/src/dependencies_writer.rs

This file was deleted.

2 changes: 0 additions & 2 deletions slinky/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ mod segment;

mod document;

mod dependencies_writer;
mod linker_writer;

pub use error::SlinkyError;
Expand All @@ -28,5 +27,4 @@ pub use segment::Segment;

pub use document::Document;

pub use dependencies_writer::DependenciesWriter;
pub use linker_writer::LinkerWriter;
76 changes: 76 additions & 0 deletions slinky/src/linker_writer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* SPDX-FileCopyrightText: © 2024 decompals */
/* SPDX-License-Identifier: MIT */

use std::path::PathBuf;
use std::{collections::HashSet, io::Write, path::Path};

use crate::{file_kind::FileKind, SlinkyError};
Expand All @@ -10,6 +11,9 @@ use crate::{FileInfo, Segment};
pub struct LinkerWriter<'a> {
pub linker_symbols: HashSet<String>,

// Used for dependency generation
files_paths: Vec<PathBuf>,

indent_level: i32,
buffer: Vec<String>,

Expand All @@ -20,6 +24,7 @@ impl<'a> LinkerWriter<'a> {
pub fn new(settings: &'a Settings) -> Self {
Self {
linker_symbols: HashSet::new(),
files_paths: Vec::new(),
indent_level: 0,
buffer: Vec::new(),
settings,
Expand Down Expand Up @@ -174,6 +179,71 @@ impl<'a> LinkerWriter<'a> {
ret
}

pub fn save_dependencies_file(
&mut self,
path: &Path,
target_path: &Path,
) -> Result<(), SlinkyError> {
let mut f = utils::create_file_and_parents(path)?;

if let Err(e) = write!(f, "{}:", target_path.display()) {
return Err(SlinkyError::FailedFileWrite {
path: path.to_path_buf(),
description: e.to_string(),
contents: target_path.display().to_string(),
});
}

for p in &self.files_paths {
if let Err(e) = write!(f, " \\\n {}", p.display()) {
return Err(SlinkyError::FailedFileWrite {
path: path.to_path_buf(),
description: e.to_string(),
contents: p.display().to_string(),
});
}
}

if let Err(e) = write!(f, "\n\n") {
return Err(SlinkyError::FailedFileWrite {
path: path.to_path_buf(),
description: e.to_string(),
contents: "".to_string(),
});
}

for p in &self.files_paths {
if let Err(e) = writeln!(f, "{}:", p.display()) {
return Err(SlinkyError::FailedFileWrite {
path: path.to_path_buf(),
description: e.to_string(),
contents: p.display().to_string(),
});
}
}

Ok(())
}

#[must_use]
pub fn export_dependencies_as_string(&self, target_path: &Path) -> String {
let mut ret = String::new();

ret += &format!("{}:", target_path.display());

for p in &self.files_paths {
ret += &format!(" \\\n {}", p.display());
}

ret += "\n\n";

for p in &self.files_paths {
ret += &format!("{}:\n", p.display());
}

ret
}

pub fn save_symbol_header(&self, path: &Path) -> Result<(), SlinkyError> {
let mut f = utils::create_file_and_parents(path)?;

Expand Down Expand Up @@ -356,6 +426,9 @@ impl LinkerWriter<'_> {
path.extend(&file.path);

self.writeln(&format!("{}({}{});", path.display(), section, wildcard));
if !self.files_paths.contains(&path) {
self.files_paths.push(path);
}
}
FileKind::Archive => {
let mut path = base_path.to_path_buf();
Expand All @@ -368,6 +441,9 @@ impl LinkerWriter<'_> {
section,
wildcard
));
if !self.files_paths.contains(&path) {
self.files_paths.push(path);
}
}
FileKind::Pad => {
if file.section == section {
Expand Down
8 changes: 5 additions & 3 deletions slinky/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,19 @@ fn check_ld_generation(yaml_path: &Path, ld_path: &Path) {
fn check_d_generation(yaml_path: &Path, ld_path: &Path) {
let document = slinky::Document::read_file(yaml_path).expect("unable to read original file");

let mut dependencies_writer = slinky::DependenciesWriter::new(&document.settings);
let mut writer = slinky::LinkerWriter::new(&document.settings);
writer.begin_sections();
for segment in &document.segments {
dependencies_writer.add_segment(segment);
writer.add_segment(segment);
}
writer.end_sections();

let expected_d_contents = fs::read_to_string(ld_path).expect("unable to read expected d file");

let target_path = document.settings.target_path.as_ref().unwrap();
assert_eq!(
expected_d_contents,
dependencies_writer.export_as_string(target_path)
writer.export_dependencies_as_string(target_path)
);
}

Expand Down

0 comments on commit c6daff1

Please sign in to comment.