|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Copyright (c) 2025 Jeffrey M. Squyres. All rights reserved. |
| 4 | +# |
| 5 | +# $COPYRIGHT$ |
| 6 | +# |
| 7 | +# Additional copyrights may follow |
| 8 | +# |
| 9 | +# $HEADER$ |
| 10 | +# |
| 11 | + |
| 12 | +# Script to create RST files containing the C, F90, and F80 bindings |
| 13 | +# that will be included in each of the MPI API man pages. We generate |
| 14 | +# the bindings using the official MPI Forum python library to read the |
| 15 | +# API JSON that was generated when building the MPI Forum standard |
| 16 | +# LaTeX document and then emit the bindings. |
| 17 | +# |
| 18 | +# Using this method, we can emit both "regular" and "embiggened" |
| 19 | +# versions of each API (if an "embiggened" version exists). |
| 20 | + |
| 21 | +import re |
| 22 | +import os |
| 23 | +import sys |
| 24 | +import textwrap |
| 25 | +import argparse |
| 26 | + |
| 27 | +from pathlib import Path |
| 28 | +from collections import defaultdict |
| 29 | +from pprint import pprint |
| 30 | + |
| 31 | +#---------------- |
| 32 | + |
| 33 | +def setup_cli(): |
| 34 | + parser = argparse.ArgumentParser(description="Generate C/F90/F08 bindings for RST") |
| 35 | + |
| 36 | + parser.add_argument('--srcdir', |
| 37 | + required=True, |
| 38 | + help='Build source dir') |
| 39 | + parser.add_argument('--builddir', |
| 40 | + required=True, |
| 41 | + help='Build build dir') |
| 42 | + |
| 43 | + args = parser.parse_args() |
| 44 | + return args |
| 45 | + |
| 46 | +#---------------- |
| 47 | + |
| 48 | +def generate(func_name_arg, output_dir, directives): |
| 49 | + global std |
| 50 | + |
| 51 | + # Sanity check |
| 52 | + func_name = func_name_arg.lower() |
| 53 | + if not func_name in std.PROCEDURES: |
| 54 | + print(f"ERROR: Don't know {func_name}") |
| 55 | + return |
| 56 | + |
| 57 | + # Do not generate this file if the binding for this function is |
| 58 | + # going to be included in another file |
| 59 | + for key, data in directives.items(): |
| 60 | + if key == func_name: |
| 61 | + continue |
| 62 | + |
| 63 | + # If this function is in the list of MPI bindings included in |
| 64 | + # another page, then we don't need to generate this page. |
| 65 | + if func_name in data: |
| 66 | + return |
| 67 | + |
| 68 | + # Also do not generate if we have no man page for this MPI API |
| 69 | + # function. |
| 70 | + if func_name not in directives: |
| 71 | + return |
| 72 | + |
| 73 | + #---------------- |
| 74 | + |
| 75 | + # If we get here, we're going to generate the bindings for one or |
| 76 | + # more API functions. |
| 77 | + func_names_data = [] |
| 78 | + for name in directives[func_name_arg]: |
| 79 | + func_names_data.append(std.PROCEDURES[name]) |
| 80 | + data = std.PROCEDURES[func_name_arg] |
| 81 | + if data not in func_names_data: |
| 82 | + func_names_data.insert(0, data) |
| 83 | + |
| 84 | + # Make an array of strings to emit into the output RST file. |
| 85 | + blank = '' |
| 86 | + out = [] |
| 87 | + |
| 88 | + out.append('SYNTAX') |
| 89 | + out.append('------') |
| 90 | + out.append(blank) |
| 91 | + |
| 92 | + have_binding = False |
| 93 | + |
| 94 | + # C bindings |
| 95 | + emitted_header = False |
| 96 | + for data in func_names_data: |
| 97 | + binding = str(data.express.iso_c) |
| 98 | + if binding and len(binding) > 0 and binding != 'None': |
| 99 | + have_binding = True |
| 100 | + if not emitted_header: |
| 101 | + out.append('C Syntax') |
| 102 | + out.append('^^^^^^^^') |
| 103 | + out.append(blank) |
| 104 | + out.append('.. code-block:: c') |
| 105 | + out.append(blank) |
| 106 | + emitted_header = True |
| 107 | + |
| 108 | + # Note that the binding string is technically in LaTeX. |
| 109 | + # One thing we'll need to adjust is to convert "\ldots" to |
| 110 | + # "..." (e.g., for MPI_Pcontrol). |
| 111 | + binding = binding.replace(r'\ldots', '...') |
| 112 | + |
| 113 | + line = textwrap.fill(binding, width=72, |
| 114 | + initial_indent=' ', |
| 115 | + subsequent_indent = ' ') |
| 116 | + out.append(line) |
| 117 | + out.append(blank) |
| 118 | + |
| 119 | + if data.has_embiggenment(): |
| 120 | + binding = str(data.express.embiggen.iso_c) |
| 121 | + binding = binding.replace(r'\ldots', '...') |
| 122 | + line = textwrap.fill(binding, width=72, |
| 123 | + initial_indent=' ', |
| 124 | + subsequent_indent = ' ') |
| 125 | + out.append(line) |
| 126 | + out.append(blank) |
| 127 | + |
| 128 | + # F90 bindings |
| 129 | + # Note: the f90 bindings were not embiggened |
| 130 | + emitted_header = False |
| 131 | + for data in func_names_data: |
| 132 | + binding = str(data.express.f90) |
| 133 | + if binding and len(binding) > 0 and binding != 'None': |
| 134 | + have_binding = True |
| 135 | + if not emitted_header: |
| 136 | + out.append('Fortran Syntax') |
| 137 | + out.append('^^^^^^^^^^^^^^') |
| 138 | + out.append(blank) |
| 139 | + out.append('.. code-block:: fortran') |
| 140 | + out.append(blank) |
| 141 | + out.append(' USE MPI') |
| 142 | + out.append(" ! or the older form: INCLUDE 'mpif.h'") |
| 143 | + emitted_header = True |
| 144 | + |
| 145 | + lines = binding.split('\n') |
| 146 | + for line in lines: |
| 147 | + out.append(f' {line}') |
| 148 | + out.append(blank) |
| 149 | + |
| 150 | + # F08 bindings |
| 151 | + emitted_header = False |
| 152 | + for data in func_names_data: |
| 153 | + binding = str(data.express.f08) |
| 154 | + if binding and len(binding) > 0 and binding != 'None': |
| 155 | + have_binding = True |
| 156 | + if not emitted_header: |
| 157 | + out.append('Fortran 2008 Syntax') |
| 158 | + out.append('^^^^^^^^^^^^^^^^^^^') |
| 159 | + out.append(blank) |
| 160 | + out.append('.. code-block:: fortran') |
| 161 | + out.append(blank) |
| 162 | + out.append(' USE mpi_f08') |
| 163 | + emitted_header = True |
| 164 | + |
| 165 | + lines = binding.split('\n') |
| 166 | + for line in lines: |
| 167 | + out.append(f' {line}') |
| 168 | + out.append(blank) |
| 169 | + |
| 170 | + if data.has_embiggenment(): |
| 171 | + binding = str(data.express.embiggen.f08) |
| 172 | + lines = binding.split('\n') |
| 173 | + for line in lines: |
| 174 | + out.append(f' {line}') |
| 175 | + out.append(blank) |
| 176 | + |
| 177 | + # Sanity check |
| 178 | + if not have_binding: |
| 179 | + print(f"NO BINDINGS for {func_name_arg}") |
| 180 | + return |
| 181 | + |
| 182 | + # Write the output file -- but only if it has changed |
| 183 | + old_content = None |
| 184 | + new_content = '\n'.join(out) |
| 185 | + output_file = os.path.join(output_dir, f'{func_name}.rst') |
| 186 | + if os.path.exists(output_file): |
| 187 | + with open(output_file) as fp: |
| 188 | + old_content = fp.read() |
| 189 | + |
| 190 | + if old_content != new_content: |
| 191 | + with open(output_file, 'w') as fp: |
| 192 | + fp.write('\n'.join(out)) |
| 193 | + |
| 194 | + # This script is run during "make". So emit a make-style |
| 195 | + # message. |
| 196 | + print(f' GENERATE RST {func_name_arg} binding') |
| 197 | + |
| 198 | +#---------------- |
| 199 | + |
| 200 | +# Some existing .3.rst man pages actually contain the docs for |
| 201 | +# multiple MPI_* API functions. Read the .3.rst files and look for |
| 202 | +# directives that mean "this file contains documentation for all these |
| 203 | +# MPI API functions". |
| 204 | +def read_rst_man_pages(src_dir): |
| 205 | + directives = {} |
| 206 | + prog = re.compile(r'^MPI_.*\.3\.rst$') |
| 207 | + |
| 208 | + man3_dir = Path(os.path.join(src_dir, 'man-openmpi', 'man3')).resolve() |
| 209 | + for file in os.listdir(man3_dir): |
| 210 | + # Only want MPI man pages |
| 211 | + if not prog.match(file): |
| 212 | + continue |
| 213 | + |
| 214 | + with open(os.path.join(man3_dir, file)) as fp: |
| 215 | + lines = fp.readlines() |
| 216 | + |
| 217 | + file_api_name = file.replace('.3.rst', '').lower() |
| 218 | + |
| 219 | + # Make an initial/empty list for every MPI API man page that |
| 220 | + # we find |
| 221 | + directives[file_api_name] = list() |
| 222 | + |
| 223 | + prefix = '.. mpi-bindings:' |
| 224 | + for line in lines: |
| 225 | + line = line.strip() |
| 226 | + if not line.startswith(prefix): |
| 227 | + continue |
| 228 | + |
| 229 | + bindings = line[len(prefix):].split(',') |
| 230 | + for binding in bindings: |
| 231 | + binding = binding.strip() |
| 232 | + directives[file_api_name].append(binding.lower()) |
| 233 | + |
| 234 | + return directives |
| 235 | + |
| 236 | +#---------------- |
| 237 | + |
| 238 | +def main(): |
| 239 | + args = setup_cli() |
| 240 | + |
| 241 | + src_dir = os.path.abspath(args.srcdir) |
| 242 | + build_dir = os.path.abspath(args.builddir) |
| 243 | + |
| 244 | + # Read existing srcdir/man-openmpi/man3/MPI_*.3.rst files and look |
| 245 | + # for directives to guide this generation process. |
| 246 | + directives = read_rst_man_pages(src_dir) |
| 247 | + |
| 248 | + # A bit of a hack to load the pympistandard module, which is in |
| 249 | + # the Open MPI '3rd-party" tree. |
| 250 | + pympistandard_dir = Path(os.path.join(src_dir, '..', '3rd-party', |
| 251 | + 'pympistandard', 'src')).resolve() |
| 252 | + |
| 253 | + sys.path.insert(0, str(pympistandard_dir)) |
| 254 | + global std |
| 255 | + import pympistandard as std |
| 256 | + |
| 257 | + # This is the JSON file with all the MPI standard APIs. This is |
| 258 | + # not currently officially distributed by the MPI Forum, so it was |
| 259 | + # obtained by checking out the relevant branch from |
| 260 | + # https://github.com/mpi-forum/mpi-standard/ and doing a build. |
| 261 | + # This will create a file named apis.json. Copy that here to this |
| 262 | + # tree. |
| 263 | + mpi_standard_json = os.path.abspath(os.path.join(src_dir, |
| 264 | + 'mpi-standard-apis.json')) |
| 265 | + std.use_api_version(1, given_path=mpi_standard_json) |
| 266 | + |
| 267 | + # We need to write all of these into the build tree. See |
| 268 | + # docs/Makefile.am for a fuller explaination: all RST files are |
| 269 | + # copied to the build tree, and we build there. |
| 270 | + output_dir = os.path.join(build_dir, 'man-openmpi', 'man3', 'bindings') |
| 271 | + if not os.path.exists(output_dir): |
| 272 | + os.makedirs(output_dir) |
| 273 | + |
| 274 | + # Now we finally generate the files. Iterate over all the MPI |
| 275 | + # procedures and generate a binding file for each one of them. |
| 276 | + for func_name in std.PROCEDURES: |
| 277 | + generate(func_name, output_dir, directives) |
| 278 | + |
| 279 | +if __name__ == "__main__": |
| 280 | + main() |
0 commit comments