|
| 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 os |
| 22 | +import sys |
| 23 | +import textwrap |
| 24 | +import argparse |
| 25 | + |
| 26 | +from pathlib import Path |
| 27 | + |
| 28 | +def generate(func_name, output_dir): |
| 29 | + global std |
| 30 | + |
| 31 | + # Sanity check |
| 32 | + func_name = func_name.lower() |
| 33 | + if not func_name in std.PROCEDURES: |
| 34 | + print(f"ERROR: Don't know {func_name}") |
| 35 | + return |
| 36 | + |
| 37 | + # Get the data for the bindings |
| 38 | + data = std.PROCEDURES[func_name] |
| 39 | + |
| 40 | + # Make an array of strings to emit into the output RST file. |
| 41 | + blank = '' |
| 42 | + out = [] |
| 43 | + |
| 44 | + out.append('SYNTAX') |
| 45 | + out.append('------') |
| 46 | + out.append(blank) |
| 47 | + |
| 48 | + # C bindings |
| 49 | + out.append('C Syntax') |
| 50 | + out.append('^^^^^^^^') |
| 51 | + out.append(blank) |
| 52 | + out.append('.. code-block:: c') |
| 53 | + out.append(blank) |
| 54 | + |
| 55 | + binding = data.express.iso_c |
| 56 | + line = textwrap.fill(str(binding), width=72, |
| 57 | + initial_indent=' ', |
| 58 | + subsequent_indent = ' ') |
| 59 | + out.append(line) |
| 60 | + out.append(blank) |
| 61 | + if data.has_embiggenment(): |
| 62 | + binding = data.express.embiggen.iso_c |
| 63 | + line = textwrap.fill(str(binding), width=72, |
| 64 | + initial_indent=' ', |
| 65 | + subsequent_indent = ' ') |
| 66 | + out.append(line) |
| 67 | + out.append(blank) |
| 68 | + |
| 69 | + # F90 bindings |
| 70 | + # Note: the f90 bindings were not embiggened |
| 71 | + out.append('Fortran Syntax') |
| 72 | + out.append('^^^^^^^^^^^^^^') |
| 73 | + out.append(blank) |
| 74 | + out.append('.. code-block:: fortran') |
| 75 | + out.append(blank) |
| 76 | + out.append(' USE MPI') |
| 77 | + out.append(" ! or the older form: INCLUDE 'mpif.h'") |
| 78 | + |
| 79 | + binding = data.express.f90 |
| 80 | + lines = str(binding).split('\n') |
| 81 | + for line in lines: |
| 82 | + out.append(f' {line}') |
| 83 | + out.append(blank) |
| 84 | + |
| 85 | + # F08 bindings |
| 86 | + out.append('Fortran 2008 Syntax') |
| 87 | + out.append('^^^^^^^^^^^^^^^^^^^') |
| 88 | + out.append(blank) |
| 89 | + out.append('.. code-block:: fortran') |
| 90 | + out.append(blank) |
| 91 | + out.append(' USE mpi_f08') |
| 92 | + |
| 93 | + binding = data.express.f08 |
| 94 | + lines = str(binding).split('\n') |
| 95 | + for line in lines: |
| 96 | + out.append(f' {line}') |
| 97 | + out.append(blank) |
| 98 | + |
| 99 | + if data.has_embiggenment(): |
| 100 | + binding = data.express.embiggen.f08 |
| 101 | + lines = str(binding).split('\n') |
| 102 | + for line in lines: |
| 103 | + out.append(f' {line}') |
| 104 | + out.append(blank) |
| 105 | + |
| 106 | + # Write the output file -- but only if it has changed |
| 107 | + old_content = None |
| 108 | + new_content = '\n'.join(out) |
| 109 | + output_file = os.path.join(output_dir, f'{func_name}.rst') |
| 110 | + if os.path.exists(output_file): |
| 111 | + with open(output_file) as fp: |
| 112 | + old_content = fp.read() |
| 113 | + |
| 114 | + if old_content != new_content: |
| 115 | + with open(output_file, 'w') as fp: |
| 116 | + fp.write('\n'.join(out)) |
| 117 | + print(f'Wrote {output_file}') |
| 118 | + |
| 119 | +#---------------- |
| 120 | + |
| 121 | +def setup_cli(): |
| 122 | + parser = argparse.ArgumentParser(description="Generate C/F90/F08 bindings for RST") |
| 123 | + |
| 124 | + parser.add_argument('--srcdir', |
| 125 | + required=True, |
| 126 | + help='Build source dir') |
| 127 | + parser.add_argument('--builddir', |
| 128 | + required=True, |
| 129 | + help='Build build dir') |
| 130 | + |
| 131 | + args = parser.parse_args() |
| 132 | + return args |
| 133 | + |
| 134 | +#---------------- |
| 135 | + |
| 136 | +def main(): |
| 137 | + args = setup_cli() |
| 138 | + |
| 139 | + src_dir = os.path.abspath(args.srcdir) |
| 140 | + build_dir = os.path.abspath(args.builddir) |
| 141 | + |
| 142 | + # A bit of a hack to load the pympistandard module, which is in |
| 143 | + # the Open MPI '3rd-party" tree. |
| 144 | + pympistandard_dir = Path(os.path.join(src_dir, '..', '3rd-party', |
| 145 | + 'pympistandard', 'src')).resolve() |
| 146 | + |
| 147 | + sys.path.insert(0, str(pympistandard_dir)) |
| 148 | + global std |
| 149 | + import pympistandard as std |
| 150 | + |
| 151 | + # This is the JSON file with all the MPI standard APIs. This is |
| 152 | + # not currently officially distributed by the MPI Forum, so it was |
| 153 | + # obtained by checking out the relevant branch from |
| 154 | + # https://github.com/mpi-forum/mpi-standard/ and doing a build. |
| 155 | + # This will create a file named apis.json. Copy that here to this |
| 156 | + # tree. |
| 157 | + mpi_standard_json = os.path.abspath(os.path.join(src_dir, |
| 158 | + 'mpi-standard-apis.json')) |
| 159 | + std.use_api_version(1, given_path=mpi_standard_json) |
| 160 | + |
| 161 | + # We need to write all of these into the build tree. See |
| 162 | + # docs/Makefile.am for a fuller explaination: all RST files are |
| 163 | + # copied to the build tree, and we build there. |
| 164 | + output_dir = os.path.join(build_dir, 'man-openmpi', 'man3', 'bindings') |
| 165 | + if not os.path.exists(output_dir): |
| 166 | + os.makedirs(output_dir) |
| 167 | + |
| 168 | + # Now we finally generate the files. Iterate over all the MPI |
| 169 | + # procedures and generate a binding file for each one of them. |
| 170 | + #for func_name in std.PROCEDURES: |
| 171 | + # generate(func_name, output_dir) |
| 172 | + |
| 173 | + # PROOF OF CONCEPT: Just generate 2 binding files for now -- to be |
| 174 | + # replaced with the above loop. |
| 175 | + generate("mpi_init", output_dir) |
| 176 | + generate("mpi_send", output_dir) |
| 177 | + |
| 178 | +if __name__ == "__main__": |
| 179 | + main() |
0 commit comments