Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace usage of rustfmt for generated code with prettyplease #215

Merged
merged 14 commits into from
Oct 25, 2024
Merged
117 changes: 67 additions & 50 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ members = [
"crates/sel4-root-task/default-test-harness",
"crates/sel4-root-task/macros",
"crates/sel4-runtime-common",
"crates/sel4-rustfmt-helper",
"crates/sel4-shared-ring-buffer",
"crates/sel4-shared-ring-buffer/block-io",
"crates/sel4-shared-ring-buffer/block-io/types",
Expand Down
2 changes: 1 addition & 1 deletion crates/examples/root-task/spawn-task/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ edition = "2021"
license = "BSD-2-Clause"

[dependencies]
object = { version = "0.36.1", default-features = false, features = ["read"] }
object = { version = "0.36.5", default-features = false, features = ["read"] }
sel4 = { path = "../../../sel4" }
sel4-root-task = { path = "../../../sel4-root-task" }
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ license = "BSD-2-Clause"
proc-macro = true

[dependencies]
proc-macro2 = "1.0.50"
quote = "1.0.23"
syn = { version = "1.0.107", features = ["full"] }
proc-macro2 = "1.0.89"
quote = "1.0.37"
syn = { version = "2.0.85", features = ["full"] }
6 changes: 3 additions & 3 deletions crates/sel4-backtrace/addr2line-context-helper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ edition = "2021"
license = "BSD-2-Clause"

[dependencies]
gimli = { version = "0.30.0", default-features = false, features = ["endian-reader"] }
object = { version = "0.36.1", default-features = false, features = ["read"] }
gimli = { version = "0.31.1", default-features = false, features = ["endian-reader"] }
object = { version = "0.36.5", default-features = false, features = ["read"] }
stable_deref_trait = { version = "1.1.0", default-features = false, features = ["alloc"] }

[dependencies.addr2line]
version = "0.23.0"
version = "0.24.2"
default-features = false
features = ["rustc-demangle", "cpp_demangle", "fallible-iterator", "smallvec"]
63 changes: 38 additions & 25 deletions crates/sel4-backtrace/addr2line-context-helper/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,31 @@

extern crate alloc;

use alloc::borrow::Cow;
use alloc::rc::Rc;

use addr2line::Context as AbstractContext;
use object::{Object, ObjectSection};

pub use addr2line::gimli::Error;

pub type Context = AbstractContext<gimli::EndianRcSlice<gimli::RunTimeEndian>>;

#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum Error {
ObjectError(object::Error),
GimliError(gimli::Error),
}

impl From<object::Error> for Error {
fn from(err: object::Error) -> Self {
Self::ObjectError(err)
}
}

impl From<gimli::Error> for Error {
fn from(err: gimli::Error) -> Self {
Self::GimliError(err)
}
}

pub fn new_context<'data: 'file, 'file, O: Object<'data>>(
file: &'file O,
) -> Result<Context, Error> {
Expand All @@ -28,31 +43,29 @@ pub fn new_context_with_sup<'data: 'file, 'file, O: Object<'data>>(
file: &'file O,
sup_file: Option<&'file O>,
) -> Result<Context, Error> {
let endian = if file.is_little_endian() {
gimli::RunTimeEndian::Little
} else {
gimli::RunTimeEndian::Big
let endian = match file.endianness() {
object::Endianness::Little => gimli::RunTimeEndian::Little,
object::Endianness::Big => gimli::RunTimeEndian::Big,
};

fn load_section<'data: 'file, 'file, O, Endian>(
id: gimli::SectionId,
file: &'file O,
endian: Endian,
) -> Result<gimli::EndianRcSlice<Endian>, Error>
where
O: Object<'data>,
Endian: gimli::Endianity,
{
let data = file
.section_by_name(id.name())
.and_then(|section| section.uncompressed_data().ok())
.unwrap_or(Cow::Borrowed(&[]));
Ok(gimli::EndianRcSlice::new(Rc::from(&*data), endian))
}

let mut dwarf = gimli::Dwarf::load(|id| load_section(id, file, endian))?;
if let Some(sup_file) = sup_file {
dwarf.load_sup(|id| load_section(id, sup_file, endian))?;
}
Context::from_dwarf(dwarf)
Ok(Context::from_dwarf(dwarf)?)
}

fn load_section<'data: 'file, 'file, O, Endian>(
id: gimli::SectionId,
file: &'file O,
endian: Endian,
) -> Result<gimli::EndianRcSlice<Endian>, Error>
where
O: Object<'data>,
Endian: gimli::Endianity,
{
let data = match file.section_by_name(id.name()) {
Some(section) => section.uncompressed_data()?,
None => Default::default(),
};
Ok(gimli::EndianRcSlice::new(Rc::from(data), endian))
}
2 changes: 1 addition & 1 deletion crates/sel4-backtrace/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ license = "BSD-2-Clause"
[dependencies]
clap = "4.4.6"
hex = "0.4.3"
object = "0.36.1"
object = "0.36.5"
sel4-backtrace-addr2line-context-helper = { path = "../addr2line-context-helper" }
sel4-backtrace-types = { path = "../types", features = ["full"] }
4 changes: 2 additions & 2 deletions crates/sel4-backtrace/embedded-debug-info/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ edition = "2021"
license = "BSD-2-Clause"

[dependencies]
addr2line = { version = "0.23.0", default-features = false }
object = { version = "0.36.1", default-features = false, features = ["read"] }
addr2line = { version = "0.24.2", default-features = false }
object = { version = "0.36.5", default-features = false, features = ["read"] }
sel4-backtrace-addr2line-context-helper = { path = "../addr2line-context-helper" }
2 changes: 1 addition & 1 deletion crates/sel4-backtrace/types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ postcard = { version = "1.0.2", default-features = false, optional = true }
serde = { version = "1.0.147", default-features = false, features = ["derive"], optional = true }

[dependencies.addr2line]
version = "0.23.0"
version = "0.24.2"
default-features = false
features = ["rustc-demangle", "cpp_demangle", "fallible-iterator", "smallvec"]
optional = true
2 changes: 1 addition & 1 deletion crates/sel4-capdl-initializer/add-spec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ anyhow = "1.0.66"
clap = "4.4.6"
fallible-iterator = "0.2.0"
num = "0.4.1"
object = { version = "0.36.1", features = ["all"] }
object = { version = "0.36.5", features = ["all"] }
postcard = { version = "1.0.2", default-features = false, features = ["alloc"] }
sel4-capdl-initializer-types = { path = "../types", features = ["std", "serde", "deflate"] }
sel4-synthetic-elf = { path = "../../sel4-synthetic-elf" }
Expand Down
2 changes: 1 addition & 1 deletion crates/sel4-capdl-initializer/embed-spec/Cargo.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ mk {
inherit (versions)
proc-macro2
quote
syn
serde
serde_json
;
hex = "0.4.3";
syn = { version = versions.syn; features = [ "full" ]; };
sel4-capdl-initializer-types = localCrates.sel4-capdl-initializer-types // { features = [ "serde" "std" "deflate" ]; };
};
}
6 changes: 3 additions & 3 deletions crates/sel4-capdl-initializer/embed-spec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ license = "BSD-2-Clause"

[dependencies]
hex = "0.4.3"
proc-macro2 = "1.0.50"
quote = "1.0.23"
proc-macro2 = "1.0.89"
quote = "1.0.37"
sel4-capdl-initializer-types = { path = "../types", features = ["serde", "std", "deflate"] }
serde = "1.0.147"
serde_json = "1.0.87"
syn = { version = "1.0.107", features = ["full"] }
syn = "2.0.85"
1 change: 0 additions & 1 deletion crates/sel4-capdl-initializer/types/derive/Cargo.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ mk {
proc-macro2
quote
syn
synstructure
;
};
}
7 changes: 3 additions & 4 deletions crates/sel4-capdl-initializer/types/derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ license = "BSD-2-Clause"
proc-macro = true

[dependencies]
proc-macro2 = "1.0.50"
quote = "1.0.23"
syn = "1.0.107"
synstructure = "0.12.6"
proc-macro2 = "1.0.89"
quote = "1.0.37"
syn = "2.0.85"
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ mk {
sel4-capdl-initializer-types = localCrates.sel4-capdl-initializer-types // { features = [ "borrowed-indirect" ]; };
};
build-dependencies = {
inherit (versions) serde_json;
inherit (versions) serde_json prettyplease;
syn = { version = versions.syn; features = [ "parsing" ]; };
inherit (localCrates)
sel4-capdl-initializer-embed-spec
sel4-capdl-initializer-with-embedded-spec-build-env
sel4-capdl-initializer-types
sel4-rustfmt-helper
;
};
features = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ deflate = ["sel4-capdl-initializer-types/deflate"]
sel4-capdl-initializer-types = { path = "../../types", features = ["borrowed-indirect"] }

[build-dependencies]
prettyplease = "0.2.25"
sel4-capdl-initializer-embed-spec = { path = "../../embed-spec" }
sel4-capdl-initializer-types = { path = "../../types" }
sel4-capdl-initializer-with-embedded-spec-build-env = { path = "../build-env" }
sel4-rustfmt-helper = { path = "../../../sel4-rustfmt-helper" }
serde_json = "1.0.87"
syn = { version = "2.0.85", features = ["parsing"] }
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use std::fs;
use std::path::PathBuf;

use sel4_capdl_initializer_with_embedded_spec_build_env::get_embedding;
use sel4_rustfmt_helper::Rustfmt;

fn main() {
// TODO
Expand All @@ -24,9 +23,9 @@ fn main() {
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());

{
let formatted = prettyplease::unparse(&syn::parse2(embedded_spec).unwrap());
let spec_out_path = out_dir.join("spec.rs");
fs::write(&spec_out_path, format!("{}", embedded_spec)).unwrap();
Rustfmt::detect().format(&spec_out_path);
fs::write(&spec_out_path, formatted).unwrap();
}

for (fname, content) in &aux_files {
Expand Down
4 changes: 3 additions & 1 deletion crates/sel4-kernel-loader/Cargo.nix
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,17 @@ mk {
};
build-dependencies = {
inherit (versions)
proc-macro2
quote
object
serde
prettyplease
;
postcard = postcardWith [ "alloc" ];
cc = "1.0.76";
glob = "0.3.0";
syn = { version = versions.syn; features = [ "parsing" ]; };
inherit (localCrates)
sel4-rustfmt-helper
sel4-platform-info
sel4-config
sel4-kernel-loader-config-types
Expand Down
8 changes: 5 additions & 3 deletions crates/sel4-kernel-loader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,19 @@ spin = { version = "0.9.4", features = ["lock_api"] }
[build-dependencies]
cc = "1.0.76"
glob = "0.3.0"
object = "0.36.1"
object = "0.36.5"
postcard = { version = "1.0.2", default-features = false, features = ["alloc"] }
quote = "1.0.23"
prettyplease = "0.2.25"
proc-macro2 = "1.0.89"
quote = "1.0.37"
sel4-build-env = { path = "../sel4/build-env" }
sel4-config = { path = "../sel4/config" }
sel4-kernel-loader-config-types = { path = "config-types" }
sel4-kernel-loader-embed-page-tables = { path = "embed-page-tables" }
sel4-kernel-loader-payload-types = { path = "payload-types" }
sel4-platform-info = { path = "../sel4-platform-info" }
sel4-rustfmt-helper = { path = "../sel4-rustfmt-helper" }
serde = "1.0.147"
syn = { version = "2.0.85", features = ["parsing"] }

[target."cfg(any(target_arch = \"arm\", target_arch = \"aarch64\"))".dependencies]
sel4-bcm2835-aux-uart-driver = { path = "../drivers/bcm2835-aux-uart" }
Expand Down
2 changes: 1 addition & 1 deletion crates/sel4-kernel-loader/add-payload/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ clap = "4.4.6"
fallible-iterator = "0.2.0"
heapless = "0.7.16"
num = "0.4.1"
object = { version = "0.36.1", features = ["all"] }
object = { version = "0.36.5", features = ["all"] }
postcard = { version = "1.0.2", default-features = false, features = ["alloc"] }
sel4-config-generic-types = { path = "../../sel4/config/generic/types", features = ["serde"] }
sel4-kernel-loader-config-types = { path = "../config-types" }
Expand Down
35 changes: 16 additions & 19 deletions crates/sel4-kernel-loader/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use object::{
read::elf::{ElfFile, ProgramHeader},
ReadRef,
};
use proc_macro2::TokenStream;
use quote::format_ident;

use sel4_build_env::{get_libsel4_include_dirs, get_with_sel4_prefix_relative_fallback};
Expand All @@ -25,7 +26,6 @@ use sel4_kernel_loader_embed_page_tables::{
schemes, LeafLocation, Region, RegionsBuilder, Scheme, SchemeHelpers,
};
use sel4_platform_info::PLATFORM_INFO;
use sel4_rustfmt_helper::Rustfmt;

pub const SEL4_KERNEL_ENV: &str = "SEL4_KERNEL";

Expand Down Expand Up @@ -81,9 +81,10 @@ fn main() {
}

if let "aarch64" | "aarch32" = sel4_cfg_str!(SEL4_ARCH) {
let toks = mk_loader_map();
let formatted = prettyplease::unparse(&syn::parse2(toks).unwrap());
let out_path = PathBuf::from(&out_dir).join("loader_page_tables.rs");
fs::write(&out_path, mk_loader_map()).unwrap();
Rustfmt::detect().format(&out_path);
fs::write(&out_path, formatted).unwrap();
}

let kernel_path = get_with_sel4_prefix_relative_fallback(SEL4_KERNEL_ENV, "bin/kernel.elf");
Expand All @@ -96,13 +97,10 @@ fn main() {
(kernel_phys_addr_range.end + KERNEL_HEADROOM).next_multiple_of(GRANULE_SIZE);

{
let toks = mk_kernel_map(kernel_phys_addr_range, kernel_phys_to_virt_offset);
let formatted = prettyplease::unparse(&syn::parse2(toks).unwrap());
let out_path = PathBuf::from(&out_dir).join("kernel_page_tables.rs");
fs::write(
&out_path,
mk_kernel_map(kernel_phys_addr_range, kernel_phys_to_virt_offset),
)
.unwrap();
Rustfmt::detect().format(&out_path);
fs::write(&out_path, formatted).unwrap();
}

// Note that -Ttext={} is incompatible with --no-rosegment (no error),
Expand All @@ -123,7 +121,7 @@ fn main() {

// // //

fn mk_loader_map() -> String {
fn mk_loader_map() -> TokenStream {
let device_range_end = match sel4_cfg_str!(SEL4_ARCH) {
"aarch64" => 1 << 39,
"aarch32" => SchemeHelpers::<SchemeImpl>::virt_bounds().end,
Expand All @@ -143,15 +141,16 @@ fn mk_loader_map() -> String {
));
}

let toks = regions.build().construct_table().embed(
regions.build().construct_table().embed(
format_ident!("loader_level_0_table"),
format_ident!("sel4_kernel_loader_embed_page_tables_runtime"),
);

format!("{toks}")
)
}

fn mk_kernel_map(kernel_phys_addr_range: Range<u64>, kernel_phys_to_virt_offset: u64) -> String {
fn mk_kernel_map(
kernel_phys_addr_range: Range<u64>,
kernel_phys_to_virt_offset: u64,
) -> TokenStream {
let virt_start = kernel_phys_addr_range
.start
.wrapping_add(kernel_phys_to_virt_offset);
Expand All @@ -170,12 +169,10 @@ fn mk_kernel_map(kernel_phys_addr_range: Range<u64>, kernel_phys_to_virt_offset:
SchemeImpl::mk_kernel_leaf_for_kernel_map(kernel_phys_to_virt_offset, loc)
}));

let toks = regions.build().construct_table().embed(
regions.build().construct_table().embed(
format_ident!("kernel_boot_level_0_table"),
format_ident!("sel4_kernel_loader_embed_page_tables_runtime"),
);

format!("{}", toks)
)
}

trait SchemeExt: Scheme {
Expand Down
Loading
Loading