|
9 | 9 | from __future__ import annotations |
10 | 10 |
|
11 | 11 | import os |
| 12 | +import subprocess |
| 13 | +import sys |
| 14 | +import tempfile |
| 15 | +from contextlib import contextmanager |
| 16 | +from typing import Iterator |
12 | 17 |
|
13 | 18 | project = "pyhwloc" |
14 | 19 | copyright = "2025, Jiaming Yuan" |
|
36 | 41 |
|
37 | 42 | intersphinx_mapping = {"python": ("https://docs.python.org/3.10", None)} |
38 | 43 |
|
39 | | -# -- Breathe |
40 | | -breathe_default_project = "pyhwloc" |
41 | | -breathe_domain_by_extension = {"h": "c"} |
42 | | - |
43 | | -CURR_PATH = os.path.dirname(os.path.abspath(os.path.expanduser(__file__))) # source |
44 | | -PROJECT_ROOT = os.path.normpath(os.path.join(CURR_PATH, os.path.pardir, os.path.pardir)) |
45 | | - |
46 | | -hwloc_xml_path = os.environ.get("PYHWLOC_XML_PATH", None) |
47 | | -if hwloc_xml_path is None: |
48 | | - hwloc_xml_path = os.path.join( |
49 | | - PROJECT_ROOT, os.path.pardir, "hwloc/doc/doxygen-doc/xml" |
50 | | - ) |
51 | | -breathe_projects = {"pyhwloc": hwloc_xml_path} |
52 | | -print("beathe projects", breathe_projects) |
53 | | - |
54 | 44 | # -- Build environment |
55 | 45 |
|
56 | 46 | os.environ["PYHWLOC_SPHINX"] = "1" |
|
62 | 52 | # path to where to save gallery generated output |
63 | 53 | "gallery_dirs": ["examples"], |
64 | 54 | } |
| 55 | + |
| 56 | + |
| 57 | +def is_readthedocs_build() -> bool: |
| 58 | + if os.environ.get("READTHEDOCS", None) == "True": |
| 59 | + return True |
| 60 | + return False |
| 61 | + |
| 62 | + |
| 63 | +def normpath(path: str) -> str: |
| 64 | + return os.path.normpath(os.path.abspath(path)) |
| 65 | + |
| 66 | + |
| 67 | +@contextmanager |
| 68 | +def _chdir(dirname: str) -> Iterator[None]: |
| 69 | + pwd = normpath(os.path.curdir) |
| 70 | + try: |
| 71 | + os.chdir(dirname) |
| 72 | + yield |
| 73 | + finally: |
| 74 | + os.chdir(pwd) |
| 75 | + |
| 76 | + |
| 77 | +def build_pyhwloc_xml() -> str: |
| 78 | + """Build and install pyhwloc, returns the path to the doxygen xml files.""" |
| 79 | + if sys.platform == "win32": |
| 80 | + raise NotImplementedError("Read the docs environment should be Linux.") |
| 81 | + |
| 82 | + hwloc_version_path = os.path.join( |
| 83 | + PROJECT_ROOT, |
| 84 | + "dev", |
| 85 | + "hwloc_version", |
| 86 | + ) |
| 87 | + with open(hwloc_version_path, "r") as fd: |
| 88 | + hwloc_version = fd.read().strip() |
| 89 | + |
| 90 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 91 | + pwd = normpath(os.path.curdir) |
| 92 | + script = f"""#!/usr/bin/env bash |
| 93 | +# Clone |
| 94 | +git clone https://github.com/open-mpi/hwloc.git |
| 95 | +cd hwloc |
| 96 | +git checkout {hwloc_version} |
| 97 | +
|
| 98 | +# Config |
| 99 | +./autogen.sh |
| 100 | +./configure --disable-nvml --enable-doxygen |
| 101 | +
|
| 102 | +# Build doc |
| 103 | +cd doc |
| 104 | +HWLOC_DOXYGEN_GENERATE_XML=YES doxygen ./doxygen.cfg |
| 105 | +# Result is in `hwloc/doc/doxygen-doc/xml` |
| 106 | +
|
| 107 | +# Copy and cleanup |
| 108 | +cp -r doxygen-doc/xml {pwd}/ |
| 109 | +cd .. |
| 110 | +git clean -xdf |
| 111 | +""" |
| 112 | + script_path = os.path.join(tmpdir, "build_xml.sh") |
| 113 | + with open(script_path, "w") as fd: |
| 114 | + fd.write(script) |
| 115 | + |
| 116 | + with _chdir(tmpdir): |
| 117 | + subprocess.check_call(["bash", script_path]) |
| 118 | + xml_path = os.path.join(pwd, "xml") |
| 119 | + |
| 120 | + # Install pyhwloc while we have the hwloc source |
| 121 | + hwloc_src_dir = os.path.join(tmpdir, "hwloc") |
| 122 | + subprocess.check_call( |
| 123 | + [ |
| 124 | + "pip", |
| 125 | + "install", |
| 126 | + PROJECT_ROOT, |
| 127 | + "--config-settings=fetch-hwloc=True", |
| 128 | + f"--config-settings=hwloc-src-dir={hwloc_src_dir}", |
| 129 | + "--config-settings=with-cuda=False", |
| 130 | + "--no-deps", |
| 131 | + "--no-build-isolation", |
| 132 | + ] |
| 133 | + ) |
| 134 | + |
| 135 | + return xml_path |
| 136 | + |
| 137 | + |
| 138 | +# -- Breathe |
| 139 | +breathe_default_project = "pyhwloc" |
| 140 | +breathe_domain_by_extension = {"h": "c"} |
| 141 | + |
| 142 | +CURR_PATH = os.path.dirname(os.path.abspath(os.path.expanduser(__file__))) # source |
| 143 | +PROJECT_ROOT = os.path.normpath(os.path.join(CURR_PATH, os.path.pardir, os.path.pardir)) |
| 144 | + |
| 145 | +if is_readthedocs_build(): |
| 146 | + hwloc_xml_path: str | None = build_pyhwloc_xml() |
| 147 | +else: |
| 148 | + hwloc_xml_path = os.environ.get("PYHWLOC_XML_PATH", None) |
| 149 | + if hwloc_xml_path is None: |
| 150 | + hwloc_xml_path = os.path.join( |
| 151 | + PROJECT_ROOT, os.path.pardir, "hwloc/doc/doxygen-doc/xml" |
| 152 | + ) |
| 153 | +breathe_projects = {"pyhwloc": hwloc_xml_path} |
| 154 | +print("breathe projects", breathe_projects) |
0 commit comments