Skip to content

Commit

Permalink
Emit version comment on generated files
Browse files Browse the repository at this point in the history
Closes #72
  • Loading branch information
AngheloAlf committed Jul 14, 2024
1 parent 5d65823 commit f494edc
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 11 deletions.
6 changes: 6 additions & 0 deletions slinky-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ struct Cli {

#[arg(short = 'c', long, value_parser = parse_key_val::<String, String>, value_delimiter = ',')]
custom_options: Vec<(String, String)>,

/// Disables the version comment emitted on linker scripts
#[arg(long)]
omit_version_comment: bool,
}

// Taken from https://github.com/clap-rs/clap/blob/f5965e586292d31b2a2cbd83f19d145180471012/examples/typed-derive.rs#L48
Expand Down Expand Up @@ -57,6 +61,8 @@ fn create_runtime_settings(cli: &Cli) -> RuntimeSettings {

rs.add_custom_options(cli.custom_options.iter().cloned());

rs.set_emit_version_comment(!cli.omit_version_comment);

rs
}

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

mod runtime_settings;

pub mod version;

pub use error::SlinkyError;

pub use linker_symbols_style::LinkerSymbolsStyle;
Expand Down
48 changes: 44 additions & 4 deletions slinky/src/linker_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::path::PathBuf;
use std::{io::Write, path::Path};

use crate::{file_kind::FileKind, SlinkyError};
use crate::{utils, Document, RuntimeSettings, VramClass};
use crate::{utils, version, Document, RuntimeSettings, VramClass};
use crate::{FileInfo, Segment};

use crate::script_buffer::ScriptBuffer;
Expand Down Expand Up @@ -36,8 +36,20 @@ impl<'a> LinkerWriter<'a> {
vram_classes.insert(vram_class.name.clone(), vram_class.clone());
}

let mut buffer = ScriptBuffer::new();

if rs.emit_version_comment() {
buffer.writeln(&format!(
"/* Generated by slinky {}.{}.{} */",
version::VERSION_MAJOR,
version::VERSION_MINOR,
version::VERSION_PATCH
));
buffer.write_empty_line();
}

Self {
buffer: ScriptBuffer::new(),
buffer,

files_paths: indexmap::IndexSet::new(),

Expand Down Expand Up @@ -278,8 +290,6 @@ impl<'a> LinkerWriter<'a> {
}

pub fn add_single_segment(&mut self, segment: &Segment) -> Result<(), SlinkyError> {
assert!(self.buffer.is_empty());

// Make sure this function is called only once
assert!(!self.single_segment);
self.single_segment = true;
Expand Down Expand Up @@ -348,6 +358,21 @@ impl LinkerWriter<'_> {
dst: &mut impl Write,
target_path: &Path,
) -> Result<(), SlinkyError> {
if self.rs.emit_version_comment() {
if let Err(e) = write!(
dst,
"# Generated by slinky {}.{}.{}\n\n",
version::VERSION_MAJOR,
version::VERSION_MINOR,
version::VERSION_PATCH
) {
return Err(SlinkyError::FailedWrite {
description: e.to_string(),
contents: "Version comment".to_string(),
});
}
}

let escaped_target_path = self.rs.escape_path(target_path)?;
if let Err(e) = write!(dst, "{}:", escaped_target_path.display()) {
return Err(SlinkyError::FailedWrite {
Expand Down Expand Up @@ -415,6 +440,21 @@ impl LinkerWriter<'_> {

impl LinkerWriter<'_> {
pub fn export_symbol_header(&self, dst: &mut impl Write) -> Result<(), SlinkyError> {
if self.rs.emit_version_comment() {
if let Err(e) = write!(
dst,
"/* Generated by slinky {}.{}.{} */\n\n",
version::VERSION_MAJOR,
version::VERSION_MINOR,
version::VERSION_PATCH
) {
return Err(SlinkyError::FailedWrite {
description: e.to_string(),
contents: "Version comment".to_string(),
});
}
}

if let Err(e) = write!(
dst,
"#ifndef HEADER_SYMBOLS_H\n#define HEADER_SYMBOLS_H\n\n"
Expand Down
12 changes: 12 additions & 0 deletions slinky/src/runtime_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use crate::SlinkyError;
#[derive(PartialEq, Debug)]
pub struct RuntimeSettings {
custom_options: HashMap<String, String>,

emit_version_comment: bool,
}

impl Default for RuntimeSettings {
Expand All @@ -23,6 +25,8 @@ impl RuntimeSettings {
pub fn new() -> Self {
Self {
custom_options: HashMap::new(),

emit_version_comment: true,
}
}

Expand All @@ -36,6 +40,14 @@ impl RuntimeSettings {
{
self.custom_options.extend(others);
}

pub fn emit_version_comment(&self) -> bool {
self.emit_version_comment
}

pub fn set_emit_version_comment(&mut self, emit: bool) {
self.emit_version_comment = emit;
}
}

impl RuntimeSettings {
Expand Down
5 changes: 0 additions & 5 deletions slinky/src/script_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,6 @@ impl ScriptBuffer {
}

impl ScriptBuffer {
#[must_use]
pub fn is_empty(&self) -> bool {
self.buffer.is_empty()
}

#[must_use]
pub fn get_buffer(&self) -> &[String] {
&self.buffer
Expand Down
8 changes: 8 additions & 0 deletions slinky/src/version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* SPDX-FileCopyrightText: © 2024 decompals */
/* SPDX-License-Identifier: MIT */

pub static VERSION_MAJOR: u32 = 0;
pub static VERSION_MINOR: u32 = 1;
pub static VERSION_PATCH: u32 = 0;

pub static VERSION_TUPLE: (u32, u32, u32) = (VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH);
2 changes: 2 additions & 0 deletions slinky/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ fn create_runtime_settings() -> RuntimeSettings {
("compiler".into(), "modern_gcc".into()),
]);

rs.set_emit_version_comment(false);

rs
}

Expand Down
4 changes: 2 additions & 2 deletions tests/regen_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ for filepath in tests/test_cases/*.yaml; do
stem="${filename%.*}"
output=tests/test_cases/$stem.ld
echo Generating $output
cargo run --release -- $filepath -o $output --custom-options version=us -c compiler=modern_gcc
cargo run --release -- $filepath -o $output --omit-version-comment --custom-options version=us -c compiler=modern_gcc
done

for filepath in tests/partial_linking/*.yaml; do
filename=$(basename -- "$filepath")
stem="${filename%.*}"
output=tests/partial_linking/$stem.ld
echo Generating $output
cargo run --release -- $filepath -o $output --partial-linking -c version=us -c compiler=modern_gcc
cargo run --release -- $filepath -o $output --omit-version-comment --partial-linking -c version=us -c compiler=modern_gcc
done

0 comments on commit f494edc

Please sign in to comment.