| 1 |
#!/usr/bin/env python3 |
| 2 |
# ex: set filetype=python: |
| 3 |
|
| 4 |
"""Translate an XDR specification into executable code that |
| 5 |
can be compiled for the Linux kernel.""" |
| 6 |
|
| 7 |
__author__ = "Chuck Lever" |
| 8 |
__copyright__ = "Copyright (c) 2024 Oracle and/or its affiliates." |
| 9 |
__license__ = "GPL-2.0 only" |
| 10 |
__version__ = "0.2" |
| 11 |
|
| 12 |
import sys |
| 13 |
from pathlib import Path |
| 14 |
import argparse |
| 15 |
|
| 16 |
_XDRGEN_DIR = Path(__file__).resolve().parent |
| 17 |
if str(_XDRGEN_DIR) not in sys.path: |
| 18 |
sys.path.insert(0, str(_XDRGEN_DIR)) |
| 19 |
|
| 20 |
from subcmds import definitions |
| 21 |
from subcmds import declarations |
| 22 |
from subcmds import lint |
| 23 |
from subcmds import source |
| 24 |
|
| 25 |
|
| 26 |
sys.path.insert(1, "@pythondir@") |
| 27 |
|
| 28 |
|
| 29 |
def main() -> int: |
| 30 |
"""Parse command-line options""" |
| 31 |
parser = argparse.ArgumentParser( |
| 32 |
formatter_class=argparse.RawDescriptionHelpFormatter, |
| 33 |
description="Convert an XDR specification to Linux kernel source code", |
| 34 |
epilog="""\ |
| 35 |
Copyright (c) 2024 Oracle and/or its affiliates. |
| 36 |
|
| 37 |
License GPLv2: <http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt> |
| 38 |
This is free software. You are free to change and redistribute it. |
| 39 |
There is NO WARRANTY, to the extent permitted by law.""", |
| 40 |
) |
| 41 |
parser.add_argument( |
| 42 |
"--version", |
| 43 |
help="Display the version of this tool", |
| 44 |
action="version", |
| 45 |
version=__version__, |
| 46 |
) |
| 47 |
|
| 48 |
subcommands = parser.add_subparsers(title="Subcommands", required=True) |
| 49 |
|
| 50 |
definitions_parser = subcommands.add_parser( |
| 51 |
"definitions", help="Generate XDR definitions" |
| 52 |
) |
| 53 |
definitions_parser.add_argument( |
| 54 |
"--annotate", |
| 55 |
action="store_true", |
| 56 |
default=False, |
| 57 |
help="Add annotation comments", |
| 58 |
) |
| 59 |
definitions_parser.add_argument( |
| 60 |
"--language", |
| 61 |
action="store_true", |
| 62 |
default="C", |
| 63 |
help="Output language", |
| 64 |
) |
| 65 |
definitions_parser.add_argument( |
| 66 |
"--peer", |
| 67 |
choices=["server", "client",], |
| 68 |
default="server", |
| 69 |
help="Generate header code for client or server side", |
| 70 |
type=str, |
| 71 |
) |
| 72 |
definitions_parser.add_argument("filename", help="File containing an XDR specification") |
| 73 |
definitions_parser.set_defaults(func=definitions.subcmd) |
| 74 |
|
| 75 |
declarations_parser = subcommands.add_parser( |
| 76 |
"declarations", help="Generate function declarations" |
| 77 |
) |
| 78 |
declarations_parser.add_argument( |
| 79 |
"--annotate", |
| 80 |
action="store_true", |
| 81 |
default=False, |
| 82 |
help="Add annotation comments", |
| 83 |
) |
| 84 |
declarations_parser.add_argument( |
| 85 |
"--language", |
| 86 |
action="store_true", |
| 87 |
default="C", |
| 88 |
help="Output language", |
| 89 |
) |
| 90 |
declarations_parser.add_argument( |
| 91 |
"--peer", |
| 92 |
choices=["server", "client",], |
| 93 |
default="server", |
| 94 |
help="Generate code for client or server side", |
| 95 |
type=str, |
| 96 |
) |
| 97 |
declarations_parser.add_argument("filename", help="File containing an XDR specification") |
| 98 |
declarations_parser.set_defaults(func=declarations.subcmd) |
| 99 |
|
| 100 |
linter_parser = subcommands.add_parser("lint", help="Check an XDR specification") |
| 101 |
linter_parser.add_argument("filename", help="File containing an XDR specification") |
| 102 |
linter_parser.set_defaults(func=lint.subcmd) |
| 103 |
|
| 104 |
source_parser = subcommands.add_parser( |
| 105 |
"source", help="Generate XDR encoder and decoder source code" |
| 106 |
) |
| 107 |
source_parser.add_argument( |
| 108 |
"--annotate", |
| 109 |
action="store_true", |
| 110 |
default=False, |
| 111 |
help="Add annotation comments", |
| 112 |
) |
| 113 |
source_parser.add_argument( |
| 114 |
"--language", |
| 115 |
action="store_true", |
| 116 |
default="C", |
| 117 |
help="Output language", |
| 118 |
) |
| 119 |
source_parser.add_argument( |
| 120 |
"--peer", |
| 121 |
choices=["server", "client",], |
| 122 |
default="server", |
| 123 |
help="Generate code for client or server side", |
| 124 |
type=str, |
| 125 |
) |
| 126 |
source_parser.add_argument( |
| 127 |
"--no-enum-validation", |
| 128 |
action="store_true", |
| 129 |
default=False, |
| 130 |
help="Disable enum value validation in decoders", |
| 131 |
) |
| 132 |
source_parser.add_argument("filename", help="File containing an XDR specification") |
| 133 |
source_parser.set_defaults(func=source.subcmd) |
| 134 |
|
| 135 |
args = parser.parse_args() |
| 136 |
return args.func(args) |
| 137 |
|
| 138 |
|
| 139 |
try: |
| 140 |
if __name__ == "__main__": |
| 141 |
sys.exit(main()) |
| 142 |
except (KeyboardInterrupt, BrokenPipeError): |
| 143 |
sys.exit(1) |