-
-
Notifications
You must be signed in to change notification settings - Fork 14.2k
Automate rustc(1) man page generation using help2man script
#150066
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
Open
jkelley129
wants to merge
3
commits into
rust-lang:main
Choose a base branch
from
jkelley129:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+389
−214
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| # Generating the `rustc(1)` Man Page | ||
|
|
||
| This document describes how the `rustc(1)` man page is generated, where the logic lives, and how it robustly targets the correct `rustc` binary regardless of execution context. | ||
|
|
||
| ## Goal | ||
|
|
||
| Ensure the `rustc(1)` man page is generated **entirely from `rustc --help -v` output** so that documentation reflects the real compiler and does not drift. | ||
|
|
||
| ## Source of Truth | ||
|
|
||
| The man page is generated from: | ||
|
|
||
| ``` | ||
| rustc --help -v | ||
| ``` | ||
|
|
||
| This output is treated as authoritative. No manual sections are maintained. | ||
|
|
||
| ## Tooling | ||
|
|
||
| - [`help2man`](https://www.gnu.org/software/help2man/) to generate the man page: | ||
| - Parses CLI help output (`rustc --help -v`) | ||
| - Provides a formatted groff man page | ||
|
|
||
| ## Script Location | ||
|
|
||
| - All logic and artifacts are in `src/doc/man`. | ||
| - This avoids coupling to the main build system. | ||
|
|
||
| ## Generation Script | ||
|
|
||
| `generate-rustc-man.sh` is the reproducible way to regenerate the man page. | ||
|
|
||
| - The script resolves its own location and output directory. | ||
| - The path to the *actual* `rustc` binary is passed as an argument. | ||
| - No assumptions made about the working directory. | ||
|
|
||
| ### Script Behavior | ||
|
|
||
| - Resolves absolute path to provided `rustc` binary. | ||
| - Runs `help2man` with `--help -v` on that binary. | ||
| - Writes output to `src/doc/man/rustc.1`. | ||
|
|
||
| The build or release tooling invoking this script **must pass the correct `rustc` binary** (e.g., a freshly built compiler). | ||
|
|
||
| ## Regenerating the Man Page | ||
|
|
||
| From the repository root *after building rustc*: | ||
|
|
||
| ```bash | ||
| ./src/doc/man/generate-rustc-man.sh path/to/rustc | ||
| ``` | ||
|
|
||
| This guarantees: | ||
| - The correct (built) compiler is documented | ||
| - Output always lands in the right place | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - `help2man` must be installed and in your `$PATH`. | ||
|
|
||
| On most Linux distros: `sudo apt install help2man` (Debian/Ubuntu) or `sudo dnf install help2man` (Fedora). | ||
|
|
||
| ## Rationale | ||
|
|
||
| - Keeps the man page in sync with real compiler behavior | ||
| - Avoids manual duplication of CLI documentation | ||
| - Regeneration is explicit and reproducible | ||
|
|
||
| ## Notes | ||
|
|
||
| - The generated `rustc.1` can be `.gitignore`d as desired, depending on release/build process. | ||
| - If you change how `rustc` outputs help, rerun this process to update the man page |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| #!/bin/bash | ||
| set -euo pipefail | ||
|
|
||
| RUSTC="${1:?Usage: $0 <path-to-rustc>}" | ||
| SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" | ||
| OUTPUT="$SCRIPT_DIR/rustc.1" | ||
| INCLUDE_FILE="$(mktemp)" | ||
| trap "rm -f '$INCLUDE_FILE'" EXIT | ||
|
|
||
| # Parse -C help output into man format | ||
| generate_codegen_section() { | ||
| "$RUSTC" -C help 2>&1 | awk ' | ||
| /^ -C / { | ||
| sub(/^ -C +/, "") | ||
| split($0, parts, / -- /) | ||
| option = parts[1] | ||
| desc = parts[2] | ||
| gsub(/=val$/, "", option) | ||
| printf ".TP\n\\fB-C %s\\fR=\\fIval\\fR\n%s\n", option, desc | ||
| }' | ||
| } | ||
|
|
||
| # Build the include file with proper section ordering | ||
| generate_include_file() { | ||
| cat <<'EOF' | ||
| [name] | ||
| rustc \- The Rust compiler | ||
|
|
||
| [description] | ||
| This program is a compiler for the Rust language, available at https://www.rust-lang.org. | ||
|
|
||
| [>options] | ||
| .SH "CODEGEN OPTIONS" | ||
| These options affect code generation. Run \fBrustc -C help\fR for the full list. | ||
|
|
||
| EOF | ||
|
|
||
| # Dynamic codegen options | ||
| generate_codegen_section | ||
|
|
||
| cat <<'EOF' | ||
|
|
||
| .SH ENVIRONMENT | ||
| Some of these affect only test harness programs (generated via rustc --test); | ||
| others affect all programs which link to the Rust standard library. | ||
| .TP | ||
| \fBRUST_BACKTRACE\fR | ||
| If set to a value different than "0", produces a backtrace in the output of a program which panics. | ||
| .TP | ||
| \fBRUST_TEST_THREADS\fR | ||
| The test framework Rust provides executes tests in parallel. This variable sets the maximum number of threads used for this purpose. This setting is overridden by the --test-threads option. | ||
| .TP | ||
| \fBRUST_TEST_NOCAPTURE\fR | ||
| If set to a value other than "0", a synonym for the --nocapture flag. | ||
| .TP | ||
| \fBRUST_MIN_STACK\fR | ||
| Sets the minimum stack size for new threads. | ||
|
|
||
| .SH EXAMPLES | ||
| To build an executable from a source file with a main function: | ||
| $ rustc -o hello hello.rs | ||
|
|
||
| To build a library from a source file: | ||
| $ rustc --crate-type=lib hello-lib.rs | ||
|
|
||
| To build either with a crate (.rs) file: | ||
| $ rustc hello.rs | ||
|
|
||
| To build an executable with debug info: | ||
| $ rustc -g -o hello hello.rs | ||
|
|
||
| [see also] | ||
| .BR rustdoc (1) | ||
|
|
||
| [bugs] | ||
| See https://github.com/rust-lang/rust/issues for issues. | ||
|
|
||
| [author] | ||
| See https://github.com/rust-lang/rust/graphs/contributors or use `git log --all --format='%cN <%cE>' | sort -u` in the rust source distribution. | ||
|
|
||
| [copyright] | ||
| This work is dual-licensed under Apache 2.0 and MIT terms. | ||
| See COPYRIGHT file in the rust source distribution. | ||
| EOF | ||
| } | ||
|
|
||
| # Generate the include file | ||
| generate_include_file >"$INCLUDE_FILE" | ||
|
|
||
| # Generate man page | ||
| help2man \ | ||
| --include="$INCLUDE_FILE" \ | ||
| --no-info \ | ||
| --no-discard-stderr \ | ||
| "$RUSTC" >"$OUTPUT" | ||
|
|
||
| echo "Generated $OUTPUT from $RUSTC help output" | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a way to get help2man to pass a custom flag here rather than open-coding something in bash? if we're doing it for codegen flags i'd rather do it consistently in rust throughout so we don't need an external dependency.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I understand what you are saying, are you suggesting to add a flag to the
rustc --helpcommand(eg.--format-man) that will output all of the necessary information forhelp2manwithout needing to parse it from external sources like therust -C help?If so, I think that makes sense and I'm happy to do it, but correct me if I'm wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no, i'm asking:
a) does help2man support running
rustc -C helpinstead of--helpwhen it generates this output? if so, you could delete this bash and replace it with a call to help2man.b) otherwise, if it doesn't support that, then i would like to avoid using help2man altogether and rewrite this bash logic into rust. we'd get rid of the bash script all together and put the rust code in a run-make test. we wouldn't modify the compiler.
either way, i don't want to have a bunch of awk no one knows how to maintain.
does that makes sense?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I understand what you are saying. I looked into it and it doesn't look like
help2mansupports two separate help options. You can specify a custom help option(which I use to specify verbose help output) but you can't have two and concatenate without using some separate logic to string them together, and it sounds like that's what you are trying to avoid, which makes sense.I'm happy to rewrite this in Rust and use some custom logic to parse the help outputs rather than
help2manif that's what you think the best next step.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering now if it makes sense to change rustc to accept both
rustc --help -C helpat once ... but that will be a big hassle since it has to go through an MCP. I think open-coding it in rust is not so bad, that will give us more flexibility in case help2man doesn't recognize some syntax we start using in the future.yeah ok, let's do the rewrite into rust.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've opened rust-lang/compiler-team#954 since I think that's a good change regardless of what we do here, I don't mind if you wait until we get a resolution there to work more on this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I will wait until we have a decision there to start working more on the rust implementation.