Skip to content

Commit

Permalink
Move manpages generation to an xtask (#645)
Browse files Browse the repository at this point in the history
PR #596 brought forward automatic generation of Linux manual pages for
Oxipng, which is executed every time Oxipng is built. However, while
building manpages on every build is convenient for Oxipng development
and doing so didn't catch my attention initially, it introduces
noticeable inefficiencies for crates using Oxipng as a library: during
their build, Oxipng manpages are also built, even though most dependent
crates won't use such artifacts, as they are not considered part of the
public Oxipng crate API or even appropriate for non-human consumption.

Moreover, generating manpages depends on `clap`, which is a heavyweight
dependency: according to a fresh `cargo build --timings --release` on my
development workstation, its `clap_builder` dependency is the third most
time consuming unit to build, totalling 1.5 s (out of 11.7 s, or 12.8%).
And there is no way for dependent crates to turn this off:
[`build-dependencies` cannot be conditional on crate
features](https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#platform-specific-dependencies).
Potentially using other `cfg` hacks to either enable or disable manpage
generation is unergonomic, if not outright disallowed. Besides reducing
their compilation time cost, dependent crates may also want to trim the
size of their dependency tree, avoiding unnecessary dependency downloads
in the process.

Therefore, a better solution to conditionally build manpages in a way
convenient for both Oxipng maintainers and downstream consumers is
needed. My proposal implemented in this PR is to leverage the
[`cargo-xtask`](https://github.com/matklad/cargo-xtask) convention to
define an auxiliary crate to move the manpage generation logic and
dependencies to, which is not part of the `oxipng` crate published on
`crates.io`. That way Oxipng maintainers and packagers can still
generate manpages at request with ease, without any automation being
noticeable to uninterested crate consumers. And as a side benefit,
Oxipng maintainers can also benefit from slightly faster iteration times
due to the lack of a build script for the main crate.

The new `mangen` xtask can be run at any time with `cargo xtask mangen`.
The generated manpages are now available at
`target/xtask/mangen/manpages`. Existing deployment scripts were updated
accordingly.
  • Loading branch information
AlexTMjugador authored Nov 24, 2024
1 parent e7f1c04 commit c81a863
Show file tree
Hide file tree
Showing 10 changed files with 249 additions and 64 deletions.
3 changes: 3 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[alias]
xtask = "run --manifest-path xtask/Cargo.toml --"

[target.'cfg(all(target_os = "linux", target_arch = "aarch64"))']
runner = "qemu-aarch64" # May need to remove this if targeting AArch64 from an AArch64 Linux box

Expand Down
1 change: 0 additions & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ jobs:
run: |
mkdir -p "target/${{ matrix.target }}/release"
mv target/oxipng "target/${{ matrix.target }}/release"
mv target/debug/assets "target/${{ matrix.target }}/release"
cargo install --locked cargo-deb
cargo deb --target "${{ matrix.target }}" --no-build --no-strip
Expand Down
17 changes: 0 additions & 17 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ exclude = [
"Dockerfile",
"scripts/*",
"tests/*",
"xtask/*",
]
homepage = "https://github.com/shssoichiro/oxipng"
license = "MIT"
Expand Down Expand Up @@ -77,10 +78,6 @@ default-features = false
features = ["png"]
version = "0.25.5"

[build-dependencies]
clap = "4.5.21"
clap_mangen = "0.2.24"

[features]
binary = ["dep:clap", "dep:glob", "dep:env_logger"]
default = ["binary", "parallel", "zopfli", "filetime"]
Expand All @@ -105,7 +102,7 @@ panic = "abort"
[package.metadata.deb]
assets = [
["target/release/oxipng", "usr/bin/", "755"],
["target/release/assets/oxipng.1", "usr/share/man/man1/", "644"],
["target/xtask/mangen/manpages/oxipng.1", "usr/share/man/man1/", "644"],
["README.md", "usr/share/doc/oxipng/", "644"],
["CHANGELOG.md", "usr/share/doc/oxipng/", "644"],
]
6 changes: 6 additions & 0 deletions MANUAL.txt
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ Options:
Use the much slower but stronger Zopfli compressor for main compression trials.
Recommended use is with '-o max' and '--fast'.

--zi <iterations>
Set the number of iterations to use for Zopfli compression. Using fewer iterations may
speed up compression for large files. This option requires '--zopfli' to be set.

[default: 15]

--timeout <secs>
Maximum amount of time, in seconds, to spend on optimizations. Oxipng will check the
timeout before each transformation or compression trial, and will stop trying to optimize
Expand Down
40 changes: 0 additions & 40 deletions build.rs

This file was deleted.

3 changes: 2 additions & 1 deletion scripts/manual.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/bash
#!/bin/sh -eu
cargo build
cargo xtask mangen

./target/debug/oxipng -V > MANUAL.txt
#Redirect all streams to prevent detection of the terminal width and force an internal default of 100
Expand Down
200 changes: 200 additions & 0 deletions xtask/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "xtask"
description = "xtasks for the Oxipng project: https://github.com/matklad/cargo-xtask"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
clap = "4.5.21"
clap_mangen = "0.2.24"
26 changes: 26 additions & 0 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use std::{env, error::Error, fs, fs::File, io::BufWriter};

use clap_mangen::Man;

include!("../../src/cli.rs");

fn main() -> Result<(), Box<dyn Error>> {
match &*env::args().nth(1).ok_or("No xtask to run provided")? {
"mangen" => build_manpages(),
_ => Err("Unknown xtask".into()),
}
}

fn build_manpages() -> Result<(), Box<dyn Error>> {
// Put manpages in <working directory>/target/xtask/mangen/manpages. Our working directory is
// expected to be the root of the repository due to the xtask invocation alias
let manpages_dir = env::current_dir()?.join("target/xtask/mangen/manpages");
fs::create_dir_all(&manpages_dir)?;

let mut man_file = BufWriter::new(File::create(manpages_dir.join("oxipng.1"))?);
Man::new(build_command()).render(&mut man_file)?;

println!("Manpages generated in {}", manpages_dir.display());

Ok(())
}

0 comments on commit c81a863

Please sign in to comment.