Skip to content

Commit

Permalink
Functions to write the partial scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
AngheloAlf committed Apr 25, 2024
1 parent bcd4a3b commit 1a7deb6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
8 changes: 6 additions & 2 deletions slinky-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ fn main() {

writer.add_all_segment(&document.segments);

let output_path = cli.output.expect("output path is required for partial linking");
writer.save_linker_scripts(&output_path).expect("Error writing the linker scripts");
let output_path = cli
.output
.expect("output path is required for partial linking");
writer
.save_linker_scripts(&output_path)
.expect("Error writing the linker scripts");
writer
.write_other_files()
.expect("Error writing other files listed on the document");
Expand Down
34 changes: 29 additions & 5 deletions slinky/src/partial_linker_writer.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
/* SPDX-FileCopyrightText: © 2024 decompals */
/* SPDX-License-Identifier: MIT */

use std::{collections::HashMap, path::{Path, PathBuf}};
use std::{
collections::HashMap,
path::{Path, PathBuf},
};

use crate::{FileInfo, FileKind, LinkerWriter, Segment, Settings, SlinkyError};

pub struct PartialLinkerWriter<'a> {
main_writer: LinkerWriter<'a>,
partial_writers: Vec<LinkerWriter<'a>>,

partial_writers: Vec<(LinkerWriter<'a>, String)>,

settings: &'a Settings,
}
Expand All @@ -16,6 +20,7 @@ impl<'a> PartialLinkerWriter<'a> {
pub fn new(settings: &'a Settings) -> Self {
Self {
main_writer: LinkerWriter::new(settings),

partial_writers: Vec::new(),

settings,
Expand All @@ -25,12 +30,14 @@ impl<'a> PartialLinkerWriter<'a> {
pub fn add_all_segment(&mut self, segments: &[Segment]) {
self.main_writer.begin_sections();

self.partial_writers.reserve(segments.len());
for segment in segments {
let mut partial_writer = LinkerWriter::new(self.settings);

partial_writer.add_single_segment(segment);

self.partial_writers.push(partial_writer);
self.partial_writers
.push((partial_writer, segment.name.clone()));

let mut p = PathBuf::new();

Expand All @@ -55,11 +62,28 @@ impl<'a> PartialLinkerWriter<'a> {
self.main_writer.end_sections();
}

pub fn save_linker_scripts(&self, _path: &Path) -> Result<(), SlinkyError> {
pub fn save_linker_scripts(&self, path: &Path) -> Result<(), SlinkyError> {
self.main_writer.save_linker_script(path)?;

for (partial, name) in &self.partial_writers {
let mut p = PathBuf::new();

p.push(&self.settings.partial_scripts_path);
p.push(&format!("{}.ld", name));

partial.save_linker_script(&p)?;
}

Ok(())
}

pub fn write_other_files(&self) -> Result<(), SlinkyError> {
self.main_writer.write_other_files()?;

for (partial, _name) in &self.partial_writers {
partial.write_other_files()?;
}

Ok(())
}
}
Expand All @@ -72,7 +96,7 @@ impl PartialLinkerWriter<'_> {
}

#[must_use]
pub fn get_partial_writers(&self) -> &Vec<LinkerWriter> {
pub fn get_partial_writers(&self) -> &Vec<(LinkerWriter, String)> {
&self.partial_writers
}
}

0 comments on commit 1a7deb6

Please sign in to comment.