Skip to content

Commit

Permalink
clean
Browse files Browse the repository at this point in the history
  • Loading branch information
Noratrieb committed Mar 16, 2024
1 parent a7cd3d4 commit 135c9bb
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 66 deletions.
83 changes: 65 additions & 18 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ use eyre::{bail, Context, OptionExt, Result};
use parse::ParsedTargetInfoFile;
use serde::Deserialize;

/// Information about a target obtained from the target_info markdown file.
struct TargetDocs {
/// Information about a target obtained from the markdown and rustc.
struct TargetInfo {
name: String,
maintainers: Vec<String>,
sections: Vec<(String, String)>,
footnotes: Vec<String>,
target_cfgs: Vec<(String, String)>,
metadata: RustcTargetMetadata,
}

/// All the sections that we want every doc page to have.
Expand Down Expand Up @@ -56,29 +58,48 @@ fn main() -> Result<()> {
.wrap_err("failed loading target_info")?
.into_iter()
.map(|info| {
let footnotes_used =
info.footnotes.iter().map(|(target, _)| (target.clone(), false)).collect();
TargetPatternEntry { info, used: false, footnotes_used }
let footnotes_used = info
.footnotes
.keys()
.map(|target| (target.clone(), false))
.collect();
TargetPatternEntry {
info,
used: false,
footnotes_used,
}
})
.collect::<Vec<_>>();

eprintln!("Collecting rustc information");
let rustc_infos =
targets.iter().map(|target| rustc_target_info(&rustc, target)).collect::<Vec<_>>();
let rustc_infos = targets
.iter()
.map(|target| rustc_target_info(&rustc, target))
.collect::<Vec<_>>();

let targets = targets
.into_iter()
.map(|target| target_doc_info(&mut info_patterns, target))
.zip(rustc_infos)
.map(|(md, rustc)| TargetInfo {
name: md.name,
maintainers: md.maintainers,
sections: md.sections,
footnotes: md.footnotes,
target_cfgs: rustc.target_cfgs,
metadata: rustc.metadata,
})
.collect::<Vec<_>>();

eprintln!("Rendering targets check_only={check_only}");
let targets_dir = Path::new(output_src).join("platform-support").join("targets");
let targets_dir = Path::new(output_src)
.join("platform-support")
.join("targets");
if !check_only {
std::fs::create_dir_all(&targets_dir).wrap_err("creating platform-support/targets dir")?;
}
for (info, rustc_info) in &targets {
let doc = render::render_target_md(info, rustc_info);
for info in &targets {
let doc = render::render_target_md(info);

if !check_only {
std::fs::write(targets_dir.join(format!("{}.md", info.name)), doc)
Expand All @@ -88,7 +109,10 @@ fn main() -> Result<()> {

for target_pattern in info_patterns {
if !target_pattern.used {
bail!("target pattern `{}` was never used", target_pattern.info.pattern);
bail!(
"target pattern `{}` was never used",
target_pattern.info.pattern
);
}

for footnote_target in target_pattern.info.footnotes.keys() {
Expand All @@ -115,7 +139,15 @@ struct TargetPatternEntry {
footnotes_used: HashMap<String, bool>,
}

fn target_doc_info(info_patterns: &mut [TargetPatternEntry], target: &str) -> TargetDocs {
/// Information about a target obtained from the target_info markdown file.
struct TargetInfoMd {
name: String,
maintainers: Vec<String>,
sections: Vec<(String, String)>,
footnotes: Vec<String>,
}

fn target_doc_info(info_patterns: &mut [TargetPatternEntry], target: &str) -> TargetInfoMd {
let mut maintainers = Vec::new();
let mut sections = Vec::new();

Expand All @@ -128,7 +160,6 @@ fn target_doc_info(info_patterns: &mut [TargetPatternEntry], target: &str) -> Ta

maintainers.extend_from_slice(&target_pattern.maintainers);


for (section_name, content) in &target_pattern.sections {
if sections.iter().any(|(name, _)| name == section_name) {
panic!(
Expand All @@ -139,7 +170,9 @@ fn target_doc_info(info_patterns: &mut [TargetPatternEntry], target: &str) -> Ta
}

if let Some(target_footnotes) = target_pattern.footnotes.get(target) {
target_pattern_entry.footnotes_used.insert(target.to_owned(), true);
target_pattern_entry
.footnotes_used
.insert(target.to_owned(), true);

if !footnotes.is_empty() {
panic!("target {target} is assigned metadata from more than one pattern");
Expand All @@ -149,7 +182,12 @@ fn target_doc_info(info_patterns: &mut [TargetPatternEntry], target: &str) -> Ta
}
}

TargetDocs { name: target.to_owned(), maintainers, sections, footnotes }
TargetInfoMd {
name: target.to_owned(),
maintainers,
sections,
footnotes,
}
}

/// Information about a target obtained from rustc.
Expand All @@ -173,7 +211,7 @@ fn rustc_target_info(rustc: &Path, target: &str) -> RustcTargetInfo {
.lines()
.filter_map(|line| {
if line.starts_with("target_") {
let Some((key, value)) = line.split_once("=") else {
let Some((key, value)) = line.split_once('=') else {
// For example `unix`
return None;
};
Expand All @@ -191,12 +229,21 @@ fn rustc_target_info(rustc: &Path, target: &str) -> RustcTargetInfo {

let json_spec = rustc_stdout(
rustc,
&["-Zunstable-options", "--print", "target-spec-json", "--target", target],
&[
"-Zunstable-options",
"--print",
"target-spec-json",
"--target",
target,
],
);
let spec = serde_json::from_str::<TargetJson>(&json_spec)
.expect("parsing --print target-spec-json for metadata");

RustcTargetInfo { target_cfgs, metadata: spec.metadata }
RustcTargetInfo {
target_cfgs,
metadata: spec.metadata,
}
}

fn rustc_stdout(rustc: &Path, args: &[&str]) -> String {
Expand Down
10 changes: 7 additions & 3 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ fn load_single_target_info(entry: &DirEntry) -> Result<ParsedTargetInfoFile> {
fn parse_file(name: &str, content: &str) -> Result<ParsedTargetInfoFile> {
let mut frontmatter_splitter = content.split("---\n");

let frontmatter = frontmatter_splitter.nth(1).ok_or_eyre("missing frontmatter")?;
let frontmatter = frontmatter_splitter
.nth(1)
.ok_or_eyre("missing frontmatter")?;

let frontmatter_line_count = frontmatter.lines().count() + 2; // 2 from ---

Expand All @@ -86,7 +88,7 @@ fn parse_file(name: &str, content: &str) -> Result<ParsedTargetInfoFile> {
let number = frontmatter_line_count + idx + 1; // 1 because "line numbers" are off by 1
if line.starts_with("```") {
in_codeblock ^= true; // toggle
} else if line.starts_with("#") {
} else if line.starts_with('#') {
if in_codeblock {
match sections.last_mut() {
Some((_, content)) => {
Expand Down Expand Up @@ -121,7 +123,9 @@ fn parse_file(name: &str, content: &str) -> Result<ParsedTargetInfoFile> {
}
}

sections.iter_mut().for_each(|section| section.1 = section.1.trim().to_owned());
sections
.iter_mut()
.for_each(|section| section.1 = section.1.trim().to_owned());

Ok(ParsedTargetInfoFile {
pattern: name.to_owned(),
Expand Down
4 changes: 0 additions & 4 deletions src/parse/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use crate::parse::Tier;

#[test]
fn no_frontmatter() {
let name = "archlinux-unknown-linux-gnu.md"; // arch linux is an arch, right?
Expand Down Expand Up @@ -38,7 +36,6 @@ fn parse_correctly() {
let name = "cat-unknown-linux-gnu.md";
let content = r#"
---
tier: "1" # first-class cats
maintainers: ["who maintains the cat?"]
---
## Requirements
Expand All @@ -59,7 +56,6 @@ But it should be possible.

assert_eq!(info.maintainers, vec!["who maintains the cat?"]);
assert_eq!(info.pattern, name);
assert_eq!(info.tier, Some(Tier::One));
assert_eq!(
info.sections,
vec![
Expand Down
Loading

0 comments on commit 135c9bb

Please sign in to comment.