Skip to contentSkip to conversation

xdrgen: Generalize/harden pathname construction

PATCH New Chuck Lever 1 participant 3 messages Oct 27, 2025

Subsystems (3)
lore
Share & tools
lore

Chuck Lever

author maintainer
#0 lore raw

PATCH 1/3 xdrgen: Generalize/harden pathname construction

From: Chuck Lever <[email protected]>

Use Python's built-in Path constructor to find the Jinja templates. This provides better error checking, proper use of path component separators, and more reliable location of the template files.

---
 tools/net/sunrpc/xdrgen/generators/__init__.py | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
tools/net/sunrpc/xdrgen/generators/__init__.py +6 -5
index b98574a36a4a..e22632cf38fb 100644
@@ -2,7 +2,7 @@
 
 """Define a base code generator class"""
 
-import sys
+from pathlib import Path
 from jinja2 import Environment, FileSystemLoader, Template
 
 from xdr_ast import _XdrAst, Specification, _RpcProgram, _XdrTypeSpecifier
@@ -14,8 +14,11 @@ def create_jinja2_environment(language: str, xdr_type: str) -> Environment:
     """Open a set of templates based on output language"""
     match language:
         case "C":
+            templates_dir = (
+                Path(__file__).parent.parent / "templates" / language / xdr_type
+            )
             environment = Environment(
-                loader=FileSystemLoader(sys.path[0] + "/templates/C/" + xdr_type + "/"),
+                loader=FileSystemLoader(templates_dir),
                 trim_blocks=True,
                 lstrip_blocks=True,
             )
@@ -48,9 +51,7 @@ def find_xdr_program_name(root: Specification) -> str:
 
 def header_guard_infix(filename: str) -> str:
     """Extract the header guard infix from the specification filename"""
-    basename = filename.split("/")[-1]
-    program = basename.replace(".x", "")
-    return program.upper()
+    return Path(filename).stem.upper()
 
 
 def kernel_c_type(spec: _XdrTypeSpecifier) -> str:
Signature · 1 line
2.51.0
author maintainer
#1 lore raw

PATCH 2/3 xdrgen: Make the xdrgen script location-independent

From: Chuck Lever <[email protected]>

The @pythondir@ placeholder is meant for build-time substitution, such as with autoconf. autoconf is not used in the kernel. Let's replace that mechanism with one that better enables the xdrgen script to be run from any directory.

---
 tools/net/sunrpc/xdrgen/xdrgen | 5 +++++
 1 file changed, 5 insertions(+)
tools/net/sunrpc/xdrgen/xdrgen +5 -0
index 43762be39252..3afd0547d67c 100755
@@ -10,8 +10,13 @@ __license__ = "GPL-2.0 only"
 __version__ = "0.2"
 
 import sys
+from pathlib import Path
 import argparse
 
+_XDRGEN_DIR = Path(__file__).resolve().parent
+if str(_XDRGEN_DIR) not in sys.path:
+    sys.path.insert(0, str(_XDRGEN_DIR))
+
 from subcmds import definitions
 from subcmds import declarations
 from subcmds import lint
Signature · 1 line
2.51.0
author maintainer
#2 lore raw

PATCH 3/3 xdrgen: Fix the variable-length opaque field decoder template

In reply to Chuck Lever

From: Chuck Lever <[email protected]>

Ensure that variable-length opaques are decoded into the named field, and do not overwrite the structure itself.

---
 .../xdrgen/templates/C/struct/decoder/variable_length_opaque.j2 | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/net/sunrpc/xdrgen/templates/C/struct/decoder/variable_length_opaque.j2 +1 -1
index 9a814de54ae8..65698e20d8cd 100644
@@ -2,5 +2,5 @@
 {% if annotate %}
 	/* member {{ name }} (variable-length opaque) */
 {% endif %}
-	if (!xdrgen_decode_opaque(xdr, (opaque *)ptr, {{ maxsize }}))
+	if (!xdrgen_decode_opaque(xdr, &ptr->{{ name }}, {{ maxsize }}))
 		return false;
Signature · 1 line
2.51.0