From 864d29bfe1475d52e7da6385ee8123b41184c833 Mon Sep 17 00:00:00 2001 From: Ulf Lilleengen Date: Fri, 15 Aug 2025 13:30:21 +0200 Subject: fix: update --- release/Cargo.toml | 7 ++ release/config.toml | 2 +- release/src/cargo.rs | 194 ++++++++++++++++++++++++++++++++++++++++++++ release/src/main.rs | 52 +++++------- release/src/semver_check.rs | 110 +++++++++++++++++++++++++ 5 files changed, 334 insertions(+), 31 deletions(-) create mode 100644 release/src/cargo.rs create mode 100644 release/src/semver_check.rs (limited to 'release') diff --git a/release/Cargo.toml b/release/Cargo.toml index 52ca01c2e..461021e0a 100644 --- a/release/Cargo.toml +++ b/release/Cargo.toml @@ -14,3 +14,10 @@ anyhow = "1" petgraph = "0.8.2" semver = "1.0.26" cargo-semver-checks = "0.43.0" +log = "0.4" +simple_logger = "5.0.0" +temp-file = "0.1.9" +flate2 = "1.1.1" + +[patch.crates-io] +cargo-semver-checks = { path = "../../cargo-semver-checks" } diff --git a/release/config.toml b/release/config.toml index 6b23217fa..f572b450c 100644 --- a/release/config.toml +++ b/release/config.toml @@ -27,7 +27,7 @@ embassy-net-ppp = { } embassy-net-esp-hosted = {} embassy-net-driver-channel = {} embassy-net-wiznet = {} -embassy-net-nrf91 = { features = ["defmt", "nrf9160"] } +embassy-net-nrf91 = { features = ["defmt", "nrf-pac/nrf9160"] } embassy-net-driver = {} embassy-net-tuntap = {} embassy-net-adin1110 = {} diff --git a/release/src/cargo.rs b/release/src/cargo.rs new file mode 100644 index 000000000..1a4f79f20 --- /dev/null +++ b/release/src/cargo.rs @@ -0,0 +1,194 @@ +//! Tools for working with Cargo. + +use std::{ + ffi::OsStr, + path::{Path, PathBuf}, + process::{Command, Stdio}, +}; + +use anyhow::{bail, Context as _, Result}; +use clap::ValueEnum as _; +use serde::{Deserialize, Serialize}; +use toml_edit::{DocumentMut, Formatted, Item, Value}; + +use crate::{windows_safe_path, Crate}; + +#[derive(Clone, Debug, PartialEq)] +pub enum CargoAction { + Build(PathBuf), + Run, +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct Artifact { + pub executable: PathBuf, +} + +/// Execute cargo with the given arguments and from the specified directory. +pub fn run(args: &[String], cwd: &Path) -> Result<()> { + run_with_env::<[(&str, &str); 0], _, _>(args, cwd, [], false)?; + Ok(()) +} + +/// Execute cargo with the given arguments and from the specified directory. +pub fn run_with_env(args: &[String], cwd: &Path, envs: I, capture: bool) -> Result +where + I: IntoIterator + core::fmt::Debug, + K: AsRef, + V: AsRef, +{ + if !cwd.is_dir() { + bail!("The `cwd` argument MUST be a directory"); + } + + // Make sure to not use a UNC as CWD! + // That would make `OUT_DIR` a UNC which will trigger things like the one fixed in https://github.com/dtolnay/rustversion/pull/51 + // While it's fixed in `rustversion` it's not fixed for other crates we are + // using now or in future! + let cwd = windows_safe_path(cwd); + + println!( + "Running `cargo {}` in {:?} - Environment {:?}", + args.join(" "), + cwd, + envs + ); + + let mut command = Command::new(get_cargo()); + + command + .args(args) + .current_dir(cwd) + .envs(envs) + .stdout(if capture { Stdio::piped() } else { Stdio::inherit() }) + .stderr(if capture { Stdio::piped() } else { Stdio::inherit() }); + + if args.iter().any(|a| a.starts_with('+')) { + // Make sure the right cargo runs + command.env_remove("CARGO"); + } + + let output = command.stdin(Stdio::inherit()).output()?; + + // Make sure that we return an appropriate exit code here, as Github Actions + // requires this in order to function correctly: + if output.status.success() { + Ok(String::from_utf8_lossy(&output.stdout).to_string()) + } else { + bail!("Failed to execute cargo subcommand `cargo {}`", args.join(" "),) + } +} + +fn get_cargo() -> String { + // On Windows when executed via `cargo run` (e.g. via the xtask alias) the + // `cargo` on the search path is NOT the cargo-wrapper but the `cargo` from the + // toolchain - that one doesn't understand `+toolchain` + #[cfg(target_os = "windows")] + let cargo = if let Ok(cargo) = std::env::var("CARGO_HOME") { + format!("{cargo}/bin/cargo") + } else { + String::from("cargo") + }; + + #[cfg(not(target_os = "windows"))] + let cargo = String::from("cargo"); + + cargo +} + +#[derive(Debug, Default)] +pub struct CargoArgsBuilder { + toolchain: Option, + subcommand: String, + target: Option, + features: Vec, + args: Vec, +} + +impl CargoArgsBuilder { + #[must_use] + pub fn toolchain(mut self, toolchain: S) -> Self + where + S: Into, + { + self.toolchain = Some(toolchain.into()); + self + } + + #[must_use] + pub fn subcommand(mut self, subcommand: S) -> Self + where + S: Into, + { + self.subcommand = subcommand.into(); + self + } + + #[must_use] + pub fn target(mut self, target: S) -> Self + where + S: Into, + { + self.target = Some(target.into()); + self + } + + #[must_use] + pub fn features(mut self, features: &[String]) -> Self { + self.features = features.to_vec(); + self + } + + #[must_use] + pub fn arg(mut self, arg: S) -> Self + where + S: Into, + { + self.args.push(arg.into()); + self + } + + #[must_use] + pub fn args(mut self, args: &[S]) -> Self + where + S: Clone + Into, + { + for arg in args { + self.args.push(arg.clone().into()); + } + self + } + + pub fn add_arg(&mut self, arg: S) -> &mut Self + where + S: Into, + { + self.args.push(arg.into()); + self + } + + #[must_use] + pub fn build(&self) -> Vec { + let mut args = vec![]; + + if let Some(ref toolchain) = self.toolchain { + args.push(format!("+{toolchain}")); + } + + args.push(self.subcommand.clone()); + + if let Some(ref target) = self.target { + args.push(format!("--target={target}")); + } + + if !self.features.is_empty() { + args.push(format!("--features={}", self.features.join(","))); + } + + for arg in self.args.iter() { + args.push(arg.clone()); + } + + args + } +} diff --git a/release/src/main.rs b/release/src/main.rs index 769611c78..0dbcc5d45 100644 --- a/release/src/main.rs +++ b/release/src/main.rs @@ -1,3 +1,4 @@ +use simple_logger::SimpleLogger; use std::collections::{BTreeMap, HashMap}; use std::fs; use std::path::{Path, PathBuf}; @@ -11,6 +12,8 @@ use petgraph::{Directed, Direction}; use toml_edit::{DocumentMut, Item, Value}; use types::*; +mod cargo; +mod semver_check; mod types; /// Tool to traverse and operate on intra-repo Rust crate dependencies @@ -70,7 +73,7 @@ fn load_release_config(repo: &Path) -> ReleaseConfig { } fn update_version(c: &mut Crate, new_version: &str) -> Result<()> { - let path = &c.path; + let path = c.path.join("Cargo.toml"); c.version = new_version.to_string(); let content = fs::read_to_string(&path)?; let mut doc: DocumentMut = content.parse()?; @@ -84,7 +87,7 @@ fn update_version(c: &mut Crate, new_version: &str) -> Result<()> { } fn update_versions(to_update: &Crate, dep: &CrateId, new_version: &str) -> Result<()> { - let path = &to_update.path; + let path = to_update.path.join("Cargo.toml"); let content = fs::read_to_string(&path)?; let mut doc: DocumentMut = content.parse()?; let mut changed = false; @@ -117,15 +120,16 @@ fn update_versions(to_update: &Crate, dep: &CrateId, new_version: &str) -> Resul Ok(()) } -fn list_crates(path: &PathBuf) -> Result> { - let d = std::fs::read_dir(path)?; - let release_config = load_release_config(path); +fn list_crates(root: &PathBuf) -> Result> { + let d = std::fs::read_dir(root)?; + let release_config = load_release_config(root); let mut crates = BTreeMap::new(); for c in d { let entry = c?; let name = entry.file_name().to_str().unwrap().to_string(); if entry.file_type()?.is_dir() && name.starts_with("embassy-") { - let entry = entry.path().join("Cargo.toml"); + let path = root.join(entry.path()); + let entry = path.join("Cargo.toml"); if entry.exists() { let content = fs::read_to_string(&entry)?; let parsed: ParsedCrate = toml::from_str(&content)?; @@ -138,7 +142,6 @@ fn list_crates(path: &PathBuf) -> Result> { } } - let path = path.join(entry); if let Some(config) = release_config.get(&id) { crates.insert( id.clone(), @@ -191,6 +194,7 @@ fn build_graph(crates: &BTreeMap) -> (Graph, HashMa } fn main() -> Result<()> { + SimpleLogger::new().init().unwrap(); let args = Args::parse(); let root = args.repo.canonicalize()?; @@ -306,7 +310,7 @@ fn main() -> Result<()> { let mut args: Vec = vec![ "publish".to_string(), "--manifest-path".to_string(), - c.path.display().to_string(), + c.path.join("Cargo.toml").display().to_string(), ]; if let Some(features) = &c.config.features { @@ -337,26 +341,9 @@ fn main() -> Result<()> { } fn check_semver(c: &Crate) -> Result<()> { - let mut args: Vec = vec![ - "semver-checks".to_string(), - "--manifest-path".to_string(), - c.path.display().to_string(), - "--default-features".to_string(), - ]; - if let Some(features) = &c.config.features { - args.push("--features".into()); - args.push(features.join(",")); - } - - let status = ProcessCommand::new("cargo").args(&args).output()?; - - println!("{}", core::str::from_utf8(&status.stdout).unwrap()); - eprintln!("{}", core::str::from_utf8(&status.stderr).unwrap()); - if !status.status.success() { - return Err(anyhow!("semver check failed")); - } else { - Ok(()) - } + let min_version = semver_check::minimum_update(c)?; + println!("Version should be bumped to {:?}", min_version); + Ok(()) } fn update_changelog(repo: &Path, c: &Crate) -> Result<()> { @@ -366,7 +353,7 @@ fn update_changelog(repo: &Path, c: &Crate) -> Result<()> { "--config".to_string(), repo.join("release").join("release.toml").display().to_string(), "--manifest-path".to_string(), - c.path.display().to_string(), + c.path.join("Cargo.toml").display().to_string(), "--execute".to_string(), "--no-confirm".to_string(), ]; @@ -386,7 +373,7 @@ fn publish_release(_repo: &Path, c: &Crate, push: bool) -> Result<()> { let mut args: Vec = vec![ "publish".to_string(), "--manifest-path".to_string(), - c.path.display().to_string(), + c.path.join("Cargo.toml").display().to_string(), ]; if let Some(features) = &c.config.features { @@ -415,3 +402,8 @@ fn publish_release(_repo: &Path, c: &Crate, push: bool) -> Result<()> { Ok(()) } } + +/// Make the path "Windows"-safe +pub fn windows_safe_path(path: &Path) -> PathBuf { + PathBuf::from(path.to_str().unwrap().to_string().replace("\\\\?\\", "")) +} diff --git a/release/src/semver_check.rs b/release/src/semver_check.rs new file mode 100644 index 000000000..b43164334 --- /dev/null +++ b/release/src/semver_check.rs @@ -0,0 +1,110 @@ +use std::fs; +use std::io::Write; +use std::path::{Path, PathBuf}; + +use cargo_semver_checks::{Check, GlobalConfig, ReleaseType, Rustdoc}; + +use crate::cargo::CargoArgsBuilder; +use crate::types::Crate; +use crate::windows_safe_path; + +/// Return the minimum required bump for the next release. +/// Even if nothing changed this will be [ReleaseType::Patch] +pub fn minimum_update(krate: &Crate) -> Result { + println!("Crate = {:?}", krate); + + let package_name = krate.name.clone(); + let package_path = krate.path.clone(); + let current_path = build_doc_json(krate)?; + + let baseline = Rustdoc::from_registry_latest_crate_version(); + let doc = Rustdoc::from_path(¤t_path); + let mut semver_check = Check::new(doc); + semver_check.with_default_features(); + semver_check.set_baseline(baseline); + semver_check.set_packages(vec![package_name]); + if let Some(features) = &krate.config.features { + let extra_current_features = features.clone(); + let extra_baseline_features = features.clone(); + semver_check.set_extra_features(extra_current_features, extra_baseline_features); + } + if let Some(target) = &krate.config.target { + semver_check.set_build_target(target.clone()); + } + let mut cfg = GlobalConfig::new(); + cfg.set_log_level(Some(log::Level::Trace)); + let result = semver_check.check_release(&mut cfg)?; + log::info!("Result {:?}", result); + + let mut min_required_update = ReleaseType::Patch; + for (_, report) in result.crate_reports() { + if let Some(required_bump) = report.required_bump() { + let required_is_stricter = + (min_required_update == ReleaseType::Patch) || (required_bump == ReleaseType::Major); + if required_is_stricter { + min_required_update = required_bump; + } + } + } + + Ok(min_required_update) +} + +pub(crate) fn build_doc_json(krate: &Crate) -> Result { + let target_dir = std::env::var("CARGO_TARGET_DIR"); + + let target_path = if let Ok(target) = target_dir { + PathBuf::from(target) + } else { + PathBuf::from(&krate.path).join("target") + }; + + let current_path = target_path; + let current_path = if let Some(target) = &krate.config.target { + current_path.join(target.clone()) + } else { + current_path + }; + let current_path = current_path + .join("doc") + .join(format!("{}.json", krate.name.to_string().replace("-", "_"))); + + std::fs::remove_file(¤t_path).ok(); + let features = if let Some(features) = &krate.config.features { + features.clone() + } else { + vec![] + }; + + log::info!("Building doc json for {} with features: {:?}", krate.name, features); + + let envs = vec![( + "RUSTDOCFLAGS", + "--cfg docsrs --cfg not_really_docsrs --cfg semver_checks", + )]; + + // always use `specific nightly` toolchain so we don't have to deal with potentially + // different versions of the doc-json + let cargo_builder = CargoArgsBuilder::default() + .toolchain("nightly-2025-06-29") + .subcommand("rustdoc") + .features(&features); + let cargo_builder = if let Some(target) = &krate.config.target { + cargo_builder.target(target.clone()) + } else { + cargo_builder + }; + + let cargo_builder = cargo_builder + .arg("-Zunstable-options") + .arg("-Zhost-config") + .arg("-Ztarget-applies-to-host") + .arg("--lib") + .arg("--output-format=json") + .arg("-Zbuild-std=alloc,core") + .arg("--config=host.rustflags=[\"--cfg=instability_disable_unstable_docs\"]"); + let cargo_args = cargo_builder.build(); + log::debug!("{cargo_args:#?}"); + crate::cargo::run_with_env(&cargo_args, &krate.path, envs, false)?; + Ok(current_path) +} -- cgit