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

Add cargo-zigbuild doc support #235

Merged
merged 1 commit into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/bin/cargo-zigbuild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::ffi::OsString;
use std::path::PathBuf;
use std::process::Command;

use cargo_zigbuild::{Build, Check, Clippy, Install, Run, Rustc, Test, Zig};
use cargo_zigbuild::{Build, Check, Clippy, Doc, Install, Run, Rustc, Test, Zig};
use clap::Parser;

#[allow(clippy::large_enum_variant)]
Expand All @@ -21,6 +21,8 @@ pub enum Opt {
Clippy(Clippy),
#[command(name = "check", aliases = &["c"])]
Check(Check),
#[command(name = "doc")]
Doc(Doc),
#[command(name = "install")]
Install(Install),
#[command(name = "rustc")]
Expand Down Expand Up @@ -59,6 +61,10 @@ fn main() -> anyhow::Result<()> {
check.enable_zig_ar = true;
check.execute()?
}
Opt::Doc(mut doc) => {
doc.enable_zig_ar = true;
doc.execute()?
}
Opt::Install(mut install) => {
install.enable_zig_ar = true;
install.execute()?
Expand Down
87 changes: 87 additions & 0 deletions src/doc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use std::ops::{Deref, DerefMut};
use std::path::PathBuf;
use std::process::{self, Command};

use anyhow::{Context, Result};
use clap::Parser;

use crate::Zig;

#[derive(Clone, Debug, Default, Parser)]
#[command(
display_order = 1,
after_help = "Run `cargo help doc` for more detailed information."
)]
pub struct Doc {
#[command(flatten)]
pub cargo: cargo_options::Doc,

/// Disable zig linker
#[arg(skip)]
pub disable_zig_linker: bool,

/// Enable zig ar
#[arg(skip)]
pub enable_zig_ar: bool,
}

impl Doc {
/// Create a new doc from manifest path
#[allow(clippy::field_reassign_with_default)]
pub fn new(manifest_path: Option<PathBuf>) -> Self {
let mut build = Self::default();
build.manifest_path = manifest_path;
build
}

/// Execute `cargo doc` command
pub fn execute(&self) -> Result<()> {
let mut run = self.build_command()?;

let mut child = run.spawn().context("Failed to run cargo doc")?;
let status = child.wait().expect("Failed to wait on cargo doc process");
if !status.success() {
process::exit(status.code().unwrap_or(1));
}
Ok(())
}

/// Generate cargo subcommand
pub fn build_command(&self) -> Result<Command> {
let mut build = self.cargo.command();
if !self.disable_zig_linker {
Zig::apply_command_env(
self.manifest_path.as_deref(),
self.release,
&self.cargo.common,
&mut build,
self.enable_zig_ar,
)?;
}

Ok(build)
}
}

impl Deref for Doc {
type Target = cargo_options::Doc;

fn deref(&self) -> &Self::Target {
&self.cargo
}
}

impl DerefMut for Doc {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.cargo
}
}

impl From<cargo_options::Doc> for Doc {
fn from(cargo: cargo_options::Doc) -> Self {
Self {
cargo,
..Default::default()
}
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod build;
mod check;
mod clippy;
mod doc;
mod install;
pub mod linux;
pub mod macos;
Expand All @@ -12,6 +13,7 @@ pub mod zig;
pub use crate::clippy::Clippy;
pub use build::Build;
pub use check::Check;
pub use doc::Doc;
pub use install::Install;
pub use run::Run;
pub use rustc::Rustc;
Expand Down
Loading