Skip to contentSkip to conversation

doc: add rust docs

PATCH Superseded Bartosz Golaszewski 2 participants 10 messages Jun 4, 2025 – Jun 5, 2025 GPIO

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover doc: add rust docs

While full integration of Rust docs with sphinx is currently hard, if not impossible, we can still follow the pattern we used for GLib bindings and generate the docs using cargo, then linking to them from the sphinx page.

While at it: fix some minor issues in conf.py.

---
Bartosz Golaszewski (3):
doc: reformat conf.py with black
doc: improve the readability of the prefix variable
doc: integrate rust docs into the sphinx build

 .readthedocs.yaml |  3 +++
 docs/conf.py      | 43 ++++++++++++++++++++++++++++++++++---------
 docs/rust_api.rst | 22 ++++++++++++++--------
 3 files changed, 51 insertions(+), 17 deletions(-)

---
base-commit: 088c66ef20662b76eebf03e71f11196a5ae14b33
change-id: 20250604-rust-docs-502d6caf207b

Best regards,

Signature · 1 line
Bartosz Golaszewski <[email protected]>
#1 lore raw

PATCH 1/3 doc: reformat conf.py with black

From: Bartosz Golaszewski <[email protected]>

There's one missing newline according to black so add it.

---
 docs/conf.py | 1 +
 1 file changed, 1 insertion(+)
docs/conf.py +1 -0
index 6fb399dcf195bd01d1fa739de89f142672f3f16e..d89c13454baa8fbe9dab17b7c3fde6fc59b577f1 100644
@@ -54,6 +54,7 @@ except ImportError:
 
 html_theme = "sphinx_rtd_theme" if sphinx_rtd_theme else "default"
 
+
 # We need to know where to put docs generated by gi-docgen but app.outdir is
 # only set once the builder is initialized. Let's delay running gi-docgen
 # until we're notified.
Signature · 1 line
2.48.1
#2 lore raw

PATCH 2/3 doc: improve the readability of the prefix variable

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Currently the default value for prefix - indicating the current directory - is an empty string. This means that if we do "{prefix}/foo", we may end up pointing to the root directory which is not what we want. Make "./" be the default value so that the example becomes valid and add "/" where we couldn't do it before.

---
 docs/conf.py | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
docs/conf.py +3 -3
index d89c13454baa8fbe9dab17b7c3fde6fc59b577f1..04d1cea2a2175166555993c3e936e7cf1ebd0fe6 100644
@@ -61,15 +61,15 @@ html_theme = "sphinx_rtd_theme" if sphinx_rtd_theme else "default"
 def make_glib_docs(app):
     # For some reason on RTD we're in the docs/ directory while during a local
     # build, we're still in the top source directory.
-    prefix = "../" if os.getenv("READTHEDOCS") == "True" else ""
+    prefix = "../" if os.getenv("READTHEDOCS") == "True" else "./"
 
     subprocess.run(
         [
             "gi-docgen",
             "generate",
             "--config",
-            f"{prefix}bindings/glib/gi-docgen.toml",
-            f"{prefix}bindings/glib/Gpiodglib-1.0.gir",
+            f"{prefix}/bindings/glib/gi-docgen.toml",
+            f"{prefix}/bindings/glib/Gpiodglib-1.0.gir",
             "--output-dir",
             f"{app.outdir}",
         ],
Signature · 1 line
2.48.1
#3 lore raw

PATCH 3/3 doc: integrate rust docs into the sphinx build

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Follow the pattern we established with GLib bindings: generate a separate set of docs for the rust bindings, make them part of the generated doc bundle and point to the rust index.html from the dedicated sphinx page.

---
 .readthedocs.yaml |  3 +++
 docs/conf.py      | 38 +++++++++++++++++++++++++++++++-------
 docs/rust_api.rst | 22 ++++++++++++++--------
 3 files changed, 48 insertions(+), 15 deletions(-)
.readthedocs.yaml +3 -0
index 5f4f5ac4954de70e060f1a7b2eafe3a731620c16..d81900c9ad9a60d8530b58e38add3a3a353da718 100644
@@ -14,6 +14,7 @@ build:
   os: ubuntu-24.04
   tools:
     python: "3.12"
+    rust: "1.86"
   apt_packages:
       - autoconf
       - autoconf-archive
@@ -24,6 +25,8 @@ build:
       - gir1.2-glib-2.0-dev
       - gobject-introspection
       - graphviz
+      # Needed for stdbool.h in rust build
+      - libclang-dev
       - libglib2.0-dev-bin
       - libgudev-1.0-dev
       - libtool
docs/conf.py +31 -7
index 04d1cea2a2175166555993c3e936e7cf1ebd0fe6..8c7bed234c98990f01adaec6ca3d6db2c3171c65 100644
@@ -54,15 +54,16 @@ except ImportError:
 
 html_theme = "sphinx_rtd_theme" if sphinx_rtd_theme else "default"
 
+# We need to know where to put docs generated by gi-docgen and cargo but
+# app.outdir is only set once the builder is initialized. Let's delay running
+# gi-docgen until we're notified.
+#
+# For some reason on RTD we're in the docs/ directory while during a local
+# build, we're still in the top source directory.
+prefix = "../" if os.getenv("READTHEDOCS") == "True" else "./"
+
 
-# We need to know where to put docs generated by gi-docgen but app.outdir is
-# only set once the builder is initialized. Let's delay running gi-docgen
-# until we're notified.
 def make_glib_docs(app):
-    # For some reason on RTD we're in the docs/ directory while during a local
-    # build, we're still in the top source directory.
-    prefix = "../" if os.getenv("READTHEDOCS") == "True" else "./"
-
     subprocess.run(
         [
             "gi-docgen",
@@ -77,8 +78,31 @@ def make_glib_docs(app):
     )
 
 
+def make_rust_docs(app):
+    subprocess.run(
+        [
+            "cargo",
+            "doc",
+            "--manifest-path",
+            f"{prefix}/bindings/rust/libgpiod/Cargo.toml",
+            "--target-dir",
+            f"{app.outdir}/rust/",
+        ],
+        check=True,
+        env=dict(
+            os.environ,
+            LD_LIBRARY_PATH="../../../lib/.libs/",
+            SYSTEM_DEPS_LIBGPIOD_LIB="gpiod",
+            SYSTEM_DEPS_LIBGPIOD_NO_PKG_CONFIG="1",
+            SYSTEM_DEPS_LIBGPIOD_SEARCH_NATIVE="../../../lib/.libs/",
+            SYSTEM_DEPS_LIBGPIOD_INCLUDE="../../../include/",
+        ),
+    )
+
+
 def setup(app):
     app.connect("builder-inited", make_glib_docs)
+    app.connect("builder-inited", make_rust_docs)
 
 
 subprocess.run(["doxygen", "Doxyfile"])
docs/rust_api.rst +14 -8
index 1408b5c2457309938e314d1c289b81d583d9cc09..dda20d0baadcf8d97832cd60a60a5d00f99affc9 100644
@@ -5,19 +5,25 @@
 ..
    This file is part of libgpiod.
 
-Where are the Rust bindings?
-=============================
+   libgpiod Rust bindings documentation
 
-.. warning::
-   There's currently no good way of integrating rust documentation with sphinx.
-   Rust bindings should be documented on https://docs.rs/ but due to a yet
-   unsolved build problem, this is currently not the case. Please refer to the
-   in-source comments for now.
+libgpiod Rust bindings API
+==========================
 
-Rust bindings are available on https://crates.io/ as the ``libgpiod`` package.
+Rust bindings for libgpiod aim to provide a memory-safe interface to the
+low-level C API. They are available on https://crates.io/ as the ``libgpiod``
+package.
 
 .. note::
    When building the Rust bindings along the C library using make, they will
    be automatically configured to build against the build results of the
    C library. Building rust bindings requires cargo to be available on the
    system.
+
+.. warning::
+   The documentation for Rust bindings is generated using ``cargo doc`` and
+   cannot be easily integrated with sphinx documentation. Please navigate to
+   a separate section dedicated exclusively to the Rust part of the API.
+
+
+`Navigate to Rust bindings documentation <rust/doc/libgpiod/index.html>`_
Signature · 1 line
2.48.1
#4 lore raw

Re: [PATCH libgpiod 3/3] doc: integrate rust docs into the sphinx build

Quoted 26 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Follow the pattern we established with GLib bindings: generate a separate set of docs for the rust bindings, make them part of the generated doc bundle and point to the rust index.html from the dedicated sphinx page.

---
 .readthedocs.yaml |  3 +++
 docs/conf.py      | 38 +++++++++++++++++++++++++++++++-------
 docs/rust_api.rst | 22 ++++++++++++++--------
 3 files changed, 48 insertions(+), 15 deletions(-)
.readthedocs.yaml +1 -0
index 5f4f5ac4954de70e060f1a7b2eafe3a731620c16..d81900c9ad9a60d8530b58e38add3a3a353da718 100644
@@ -14,6 +14,7 @@ build:
   os: ubuntu-24.04
   tools:
     python: "3.12"
+    rust: "1.86"
   apt_packages:
       - autoconf
       - autoconf-archive

.readthedocs.yaml build: · line 29

Quoted 5 lines
.readthedocs.yaml +2 -0
@@ -24,6 +25,8 @@ build:
       - gir1.2-glib-2.0-dev
       - gobject-introspection
       - graphviz
+      # Needed for stdbool.h in rust build
+      - libclang-dev

Mostly it is needed for bindgen, right?

Quoted 35 lines
.readthedocs.yaml +0 -0
       - libglib2.0-dev-bin
       - libgudev-1.0-dev
       - libtool
docs/conf.py +8 -7
index 04d1cea2a2175166555993c3e936e7cf1ebd0fe6..8c7bed234c98990f01adaec6ca3d6db2c3171c65 100644
@@ -54,15 +54,16 @@ except ImportError:
 
 html_theme = "sphinx_rtd_theme" if sphinx_rtd_theme else "default"
 
+# We need to know where to put docs generated by gi-docgen and cargo but
+# app.outdir is only set once the builder is initialized. Let's delay running
+# gi-docgen until we're notified.
+#
+# For some reason on RTD we're in the docs/ directory while during a local
+# build, we're still in the top source directory.
+prefix = "../" if os.getenv("READTHEDOCS") == "True" else "./"
+
 
-# We need to know where to put docs generated by gi-docgen but app.outdir is
-# only set once the builder is initialized. Let's delay running gi-docgen
-# until we're notified.
 def make_glib_docs(app):
-    # For some reason on RTD we're in the docs/ directory while during a local
-    # build, we're still in the top source directory.
-    prefix = "../" if os.getenv("READTHEDOCS") == "True" else "./"
-
     subprocess.run(
         [
             "gi-docgen",
@@ -77,8 +78,31 @@ def make_glib_docs(app):
     )
 
 

docs/conf.py def make_glib_docs(app): · line 90

Quoted 10 lines
docs/conf.py +10 -0
+def make_rust_docs(app):
+    subprocess.run(
+        [
+            "cargo",
+            "doc",
+            "--manifest-path",
+            f"{prefix}/bindings/rust/libgpiod/Cargo.toml",
+            "--target-dir",
+            f"{app.outdir}/rust/",
+        ],

This would narrow it down to only the libraries that actually matter:

docs/conf.py +3 -0
index 8c7bed2..e5baeb2 100644
@@ -82,8 +82,11 @@ def make_rust_docs(app):
     subprocess.run(
         [
             "cargo",
             "doc",
+            "--no-deps",
+            "--package=libgpiod",
+            "--package=libgpiod-sys",
             "--manifest-path",
             f"{prefix}/bindings/rust/libgpiod/Cargo.toml",
             "--target-dir",
             f"{app.outdir}/rust/",

As the other stuff is just build dependencies, we probably do not need to include it in the output? But not having a strong opinion here.

- Erik

#5 lore raw

Re: [PATCH libgpiod 3/3] doc: integrate rust docs into the sphinx build

Quoted 27 lines · Erik Schilling
Quoted 25 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Follow the pattern we established with GLib bindings: generate a separate set of docs for the rust bindings, make them part of the generated doc bundle and point to the rust index.html from the dedicated sphinx page.

---
 .readthedocs.yaml |  3 +++
 docs/conf.py      | 38 +++++++++++++++++++++++++++++++-------
 docs/rust_api.rst | 22 ++++++++++++++--------
 3 files changed, 48 insertions(+), 15 deletions(-)
.readthedocs.yaml +1 -0
index 5f4f5ac4954de70e060f1a7b2eafe3a731620c16..d81900c9ad9a60d8530b58e38add3a3a353da718 100644
@@ -14,6 +14,7 @@ build:
   os: ubuntu-24.04
   tools:
     python: "3.12"
+    rust: "1.86"
   apt_packages:
       - autoconf
       - autoconf-archive
Quoted 9 lines

.readthedocs.yaml build: · line 29

Quoted 6 lines
.readthedocs.yaml +2 -0
@@ -24,6 +25,8 @@ build:
       - gir1.2-glib-2.0-dev
       - gobject-introspection
       - graphviz
+      # Needed for stdbool.h in rust build
+      - libclang-dev

Mostly it is needed for bindgen, right?

Yes, I'll change the comment here.

Quoted 65 lines
Quoted 35 lines
.readthedocs.yaml +0 -0
       - libglib2.0-dev-bin
       - libgudev-1.0-dev
       - libtool
docs/conf.py +8 -7
index 04d1cea2a2175166555993c3e936e7cf1ebd0fe6..8c7bed234c98990f01adaec6ca3d6db2c3171c65 100644
@@ -54,15 +54,16 @@ except ImportError:
 
 html_theme = "sphinx_rtd_theme" if sphinx_rtd_theme else "default"
 
+# We need to know where to put docs generated by gi-docgen and cargo but
+# app.outdir is only set once the builder is initialized. Let's delay running
+# gi-docgen until we're notified.
+#
+# For some reason on RTD we're in the docs/ directory while during a local
+# build, we're still in the top source directory.
+prefix = "../" if os.getenv("READTHEDOCS") == "True" else "./"
+
 
-# We need to know where to put docs generated by gi-docgen but app.outdir is
-# only set once the builder is initialized. Let's delay running gi-docgen
-# until we're notified.
 def make_glib_docs(app):
-    # For some reason on RTD we're in the docs/ directory while during a local
-    # build, we're still in the top source directory.
-    prefix = "../" if os.getenv("READTHEDOCS") == "True" else "./"
-
     subprocess.run(
         [
             "gi-docgen",
@@ -77,8 +78,31 @@ def make_glib_docs(app):
     )
 
 

docs/conf.py def make_glib_docs(app): · line 90

Quoted 10 lines
docs/conf.py +10 -0
+def make_rust_docs(app):
+    subprocess.run(
+        [
+            "cargo",
+            "doc",
+            "--manifest-path",
+            f"{prefix}/bindings/rust/libgpiod/Cargo.toml",
+            "--target-dir",
+            f"{app.outdir}/rust/",
+        ],

This would narrow it down to only the libraries that actually matter:

docs/conf.py +3 -0
index 8c7bed2..e5baeb2 100644
@@ -82,8 +82,11 @@ def make_rust_docs(app):
     subprocess.run(
         [
             "cargo",
             "doc",
+            "--no-deps",
+            "--package=libgpiod",
+            "--package=libgpiod-sys",
             "--manifest-path",
             f"{prefix}/bindings/rust/libgpiod/Cargo.toml",
             "--target-dir",
             f"{app.outdir}/rust/",
Quoted 3 lines

As the other stuff is just build dependencies, we probably do not need to include it in the output? But not having a strong opinion here.

Thanks, it looks much cleaner with this. In fact: I only want to include libgpiod and not the -sys create as the former is the only official user-facing API.

I also dropped some environment variables which turned out to not be needed at build-time - LD_LIBRARY_PATH and SYSTEM_DEPS_LIBGPIOD_SEARCH_NATIVE.

I'll send a v2 shortly.

Bartosz

#6 lore raw
In reply to Bartosz Golaszewski
Quoted 19 lines · Bartosz Golaszewski

While full integration of Rust docs with sphinx is currently hard, if not impossible, we can still follow the pattern we used for GLib bindings and generate the docs using cargo, then linking to them from the sphinx page.

While at it: fix some minor issues in conf.py.

---
Bartosz Golaszewski (3):
doc: reformat conf.py with black
doc: improve the readability of the prefix variable
doc: integrate rust docs into the sphinx build

 .readthedocs.yaml |  3 +++
 docs/conf.py      | 43 ++++++++++++++++++++++++++++++++++---------
 docs/rust_api.rst | 22 ++++++++++++++--------
 3 files changed, 51 insertions(+), 17 deletions(-)
Quoted 8 lines

---
base-commit: 088c66ef20662b76eebf03e71f11196a5ae14b33
change-id: 20250604-rust-docs-502d6caf207b

Best regards,
--
Bartosz Golaszewski <[email protected]>

Erik, Cc'ing your current address here, I forgot you're no longer with linaro. Hope you can get this from lore.

Bart

#7 lore raw
In reply to Bartosz Golaszewski
Quoted 14 lines · Bartosz Golaszewski

While full integration of Rust docs with sphinx is currently hard, if not impossible, we can still follow the pattern we used for GLib bindings and generate the docs using cargo, then linking to them from the sphinx page.

While at it: fix some minor issues in conf.py.

---
Bartosz Golaszewski (3):
doc: reformat conf.py with black
doc: improve the readability of the prefix variable
doc: integrate rust docs into the sphinx build

Quoted 4 lines
 .readthedocs.yaml |  3 +++
 docs/conf.py      | 43 ++++++++++++++++++++++++++++++++++---------
 docs/rust_api.rst | 22 ++++++++++++++--------
 3 files changed, 51 insertions(+), 17 deletions(-)

I tried to do a build of it to look at how it looks, but I run into:

Quoted 1 line

make docs

[...]
usage: gi-docgen generate [-h] [-q] [--fatal-warnings] [--add-include-path INCLUDE_PATHS] [-C FILE] [--dry-run] [--templates-dir TEMPLATES_DIR] [--content-dir CONTENT_DIRS] [--theme-name THEME_NAME] [--output-dir OUTPUT_DIR] [--no-namespace-dir]
[--section SECTIONS]
GIRFILE
gi-docgen generate: error: argument GIRFILE: can't open './/bindings/glib/Gpiodglib-1.0.gir': [Errno 2] No such file or directory: './/bindings/glib/Gpiodglib-1.0.gir'

Extension error:
Handler <function make_glib_docs at 0x7f186e7b5ee0> for event 'builder-inited' threw an exception (exception: Command '['gi-docgen', 'generate', '--config', './/bindings/glib/gi-docgen.toml', './/bindings/glib/Gpiodglib-1.0.gir', '--output-dir', '/home/ablu/projects/libgpiod/docs/sphinx-output']' returned non-zero exit status 2.) ~/projects/libgpiod/docs
make[1]: Leaving directory '/home/ablu/projects/libgpiod/docs'

Not entirely sure what should create that .gir file. I have a build with bindings enabled, but my bindings/glib/ does not have that file.

Am I missing something?

- Erik

#8 lore raw
Quoted 35 lines · Erik Schilling
Quoted 14 lines · Bartosz Golaszewski

While full integration of Rust docs with sphinx is currently hard, if not impossible, we can still follow the pattern we used for GLib bindings and generate the docs using cargo, then linking to them from the sphinx page.

While at it: fix some minor issues in conf.py.

---
Bartosz Golaszewski (3):
doc: reformat conf.py with black
doc: improve the readability of the prefix variable
doc: integrate rust docs into the sphinx build

Quoted 4 lines
 .readthedocs.yaml |  3 +++
 docs/conf.py      | 43 ++++++++++++++++++++++++++++++++++---------
 docs/rust_api.rst | 22 ++++++++++++++--------
 3 files changed, 51 insertions(+), 17 deletions(-)

I tried to do a build of it to look at how it looks, but I run into:

Quoted 1 line

make docs

[...]
usage: gi-docgen generate [-h] [-q] [--fatal-warnings] [--add-include-path INCLUDE_PATHS] [-C FILE] [--dry-run] [--templates-dir TEMPLATES_DIR] [--content-dir CONTENT_DIRS] [--theme-name THEME_NAME] [--output-dir OUTPUT_DIR] [--no-namespace-dir]
[--section SECTIONS]
GIRFILE
gi-docgen generate: error: argument GIRFILE: can't open './/bindings/glib/Gpiodglib-1.0.gir': [Errno 2] No such file or directory: './/bindings/glib/Gpiodglib-1.0.gir'

Extension error:
Handler <function make_glib_docs at 0x7f186e7b5ee0> for event 'builder-inited' threw an exception (exception: Command '['gi-docgen', 'generate', '--config', './/bindings/glib/gi-docgen.toml', './/bindings/glib/Gpiodglib-1.0.gir', '--output-dir', '/home/ablu/projects/libgpiod/docs/sphinx-output']' returned non-zero exit status 2.) ~/projects/libgpiod/docs
make[1]: Leaving directory '/home/ablu/projects/libgpiod/docs'

Quoted 6 lines

Not entirely sure what should create that .gir file. I have a build with bindings enabled, but my bindings/glib/ does not have that file.

Am I missing something?

- Erik

I ran the build both locally and over at RTD[1] so it works fine. You probably just need to install the following GLib packages: gi-docgen, gir1.2-glib-2.0-dev and gobject-introspection. In general: make sure you have all the packages listed in .readthedocs.yaml in the libgpiod git tree.

Bart

[1] https://libgpiod.readthedocs.io/en/b4-rust-docs/

#9 lore raw
Quoted 41 lines · Bartosz Golaszewski
Quoted 40 lines · Erik Schilling
Quoted 14 lines · Bartosz Golaszewski

While full integration of Rust docs with sphinx is currently hard, if not impossible, we can still follow the pattern we used for GLib bindings and generate the docs using cargo, then linking to them from the sphinx page.

While at it: fix some minor issues in conf.py.

---
Bartosz Golaszewski (3):
doc: reformat conf.py with black
doc: improve the readability of the prefix variable
doc: integrate rust docs into the sphinx build

Quoted 4 lines
 .readthedocs.yaml |  3 +++
 docs/conf.py      | 43 ++++++++++++++++++++++++++++++++++---------
 docs/rust_api.rst | 22 ++++++++++++++--------
 3 files changed, 51 insertions(+), 17 deletions(-)

I tried to do a build of it to look at how it looks, but I run into:

Quoted 1 line

make docs

[...]
usage: gi-docgen generate [-h] [-q] [--fatal-warnings] [--add-include-path INCLUDE_PATHS] [-C FILE] [--dry-run] [--templates-dir TEMPLATES_DIR] [--content-dir CONTENT_DIRS] [--theme-name THEME_NAME] [--output-dir OUTPUT_DIR] [--no-namespace-dir]
[--section SECTIONS]
GIRFILE
gi-docgen generate: error: argument GIRFILE: can't open './/bindings/glib/Gpiodglib-1.0.gir': [Errno 2] No such file or directory: './/bindings/glib/Gpiodglib-1.0.gir'

Extension error:
Handler <function make_glib_docs at 0x7f186e7b5ee0> for event 'builder-inited' threw an exception (exception: Command '['gi-docgen', 'generate', '--config', './/bindings/glib/gi-docgen.toml', './/bindings/glib/Gpiodglib-1.0.gir', '--output-dir', '/home/ablu/projects/libgpiod/docs/sphinx-output']' returned non-zero exit status 2.) ~/projects/libgpiod/docs
make[1]: Leaving directory '/home/ablu/projects/libgpiod/docs'

Not entirely sure what should create that .gir file. I have a build with bindings enabled, but my bindings/glib/ does not have that file.

Am I missing something?

Quoted 7 lines
Quoted 1 line

- Erik

I ran the build both locally and over at RTD[1] so it works fine. You probably just need to install the following GLib packages: gi-docgen, gir1.2-glib-2.0-dev and gobject-introspection. In general: make sure you have all the packages listed in .readthedocs.yaml in the libgpiod git tree.

Was missing: gobject-introspection-devel on Fedora ✅