Skip to content

zig init: Add step to generate documentation#22690

Open
sorairolake wants to merge 1 commit intoziglang:masterfrom
sorairolake:add-doc-step
Open

zig init: Add step to generate documentation#22690
sorairolake wants to merge 1 commit intoziglang:masterfrom
sorairolake:add-doc-step

Conversation

@sorairolake
Copy link
Copy Markdown
Contributor

@sorairolake sorairolake commented Jan 31, 2025

Add a docs step to build.zig which zig init generates. This step will generate the package documentation from the doc comments.

The generated package documentation can be viewed by running the following in the directory where it was generated:

python -m http.server

Also, add doc comments and doctests to add function to provide an example of these.

Closes #22644

Comment thread lib/init/src/root.zig Outdated
Comment thread lib/init/build.zig Outdated
Comment thread lib/init/build.zig Outdated
Copy link
Copy Markdown
Collaborator

@linusg linusg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with one more change.

Comment thread lib/init/build.zig Outdated
Comment thread lib/init/build.zig
const install_docs = b.addInstallDirectory(.{
.source_dir = docs_obj.getEmittedDocs(),
.install_dir = .prefix,
.install_subdir = "docs",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit of an odd choice of directory. Since this is zig init, I think we should try to encourage best practice. I'd expect something more like $prefix/share/doc/$name/html (the norm on Debian/Ubuntu at least).

Suggested change
.install_subdir = "docs",
.install_subdir = b.pathJoin(&.{ "share", "doc", ".NAME", "html" }),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Autodocs output is hardly something distros would want to install so I don't think it makes sense to follow distro conventions here. Much more likely that someone will view the files from a local zig-out/ or a CI system will copy them onto a static file server.

See also:

zig/build.zig

Lines 60 to 64 in cb8d7a8

const install_std_docs = b.addInstallDirectory(.{
.source_dir = autodoc_test.getEmittedDocs(),
.install_dir = .prefix,
.install_subdir = "doc/std",
});

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Autodocs output is hardly something distros would want to install

Hm, what makes you say that? I see an ocean of API docs in /usr/share/doc on my system, many of them in HTML form, and many of them generated from source code.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discoverability mostly - if it was a man page there is a tool that knows where to look for those. Windows back in the day had at least a chm viewer (which would be lovely for this now that I think about it!). If I have to manually dig around in some system directory where I may or may not find what I'm looking for I'll directly go to the nearest search engine instead.

Maybe I'm an outlier though 🙂

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't say I'm a frequent browser of /usr/share/doc myself... but my inclination is to follow established practice in distros when it comes to stuff like this.

@andrewrk any thoughts on this since you've done Debian packaging work?

Comment thread lib/init/build.zig
// This creates a `docs` step. It will be visible in the `zig build --help`
// menu, and can be selected like this: `zig build docs`
// This will generate the package documentation from the doc comments.
const docs_step = b.step("docs", "Build the package documentation");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there actually a good reason why we want this to be its own named step, as opposed to just being part of b.getInstallStep()?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a `docs` step to `build.zig` which `zig init` generates. This step
will generate the package documentation from the doc comments.

The generated package documentation can be viewed by running the
following in the directory where it was generated:

```sh
python -m http.server
```

Also, add doc comments and doctests to `add` function to provide an
example of these.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a step to generate documentation to build.zig which zig init generates

4 participants