|
| 1 | +# SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import json |
| 5 | +import os |
| 6 | +import shutil |
| 7 | +import subprocess |
| 8 | + |
| 9 | +from esp_docs.idf_extensions.build_system import project_path as dummy_project_path |
| 10 | + |
| 11 | +from at_extensions.idf_env import get_idf_python, prepare_idf_env |
| 12 | + |
| 13 | + |
| 14 | +class AtIdfBuilder: |
| 15 | + def __init__(self) -> None: |
| 16 | + self.project_description = {} |
| 17 | + |
| 18 | + def _run_idf_py(self, idf_py, args, cmake_build_dir): |
| 19 | + result = subprocess.run(idf_py + args, env=os.environ.copy(), text=True, capture_output=True) |
| 20 | + if result.returncode == 0: |
| 21 | + return |
| 22 | + |
| 23 | + details = '\n'.join(part for part in [result.stdout, result.stderr] if part and part.strip()).strip() |
| 24 | + target = args[-1] if 'set-target' in args else 'unknown' |
| 25 | + raise RuntimeError( |
| 26 | + 'Dummy ESP-IDF project setup failed for target {} (exit code {}).\n' |
| 27 | + 'Install tools with: python $IDF_PATH/tools/idf_tools.py install\n' |
| 28 | + '{}' |
| 29 | + .format(target, result.returncode, details) |
| 30 | + ) |
| 31 | + |
| 32 | + def generate_idf_info(self, app, config): |
| 33 | + os.environ['IDF_DOC_BUILD'] = 'y' |
| 34 | + |
| 35 | + if not app.config.idf_target: |
| 36 | + raise RuntimeError( |
| 37 | + 'A valid target is needed to build ESP-AT docs. ' |
| 38 | + 'Please re-run build-docs with a target specified, e.g: build-docs -t esp32' |
| 39 | + ) |
| 40 | + |
| 41 | + build_dir = os.path.dirname(app.doctreedir.rstrip(os.sep)) |
| 42 | + cmake_build_dir = os.path.join(build_dir, 'build_dummy_project') |
| 43 | + idf_path = prepare_idf_env() |
| 44 | + idf_py = [ |
| 45 | + get_idf_python(), |
| 46 | + os.path.join(idf_path, 'tools', 'idf.py'), |
| 47 | + '-B', |
| 48 | + cmake_build_dir, |
| 49 | + '-C', |
| 50 | + dummy_project_path, |
| 51 | + '-D', |
| 52 | + 'SDKCONFIG={}'.format(os.path.join(build_dir, 'dummy_project_sdkconfig')), |
| 53 | + ] |
| 54 | + |
| 55 | + shutil.rmtree(cmake_build_dir, ignore_errors=True) |
| 56 | + print('Starting dummy IDF project for ESP-AT docs (IDF_PATH={})...'.format(idf_path)) |
| 57 | + self._run_idf_py(idf_py, ['--preview', 'set-target', app.config.idf_target], cmake_build_dir) |
| 58 | + self._run_idf_py(idf_py, ['reconfigure'], cmake_build_dir) |
| 59 | + |
| 60 | + with open(os.path.join(cmake_build_dir, 'project_description.json')) as f: |
| 61 | + self.project_description = json.load(f) |
| 62 | + |
| 63 | + if self.project_description['target'] != app.config.idf_target: |
| 64 | + raise RuntimeError( |
| 65 | + 'Dummy IDF project target mismatch: expected {}, got {}.' |
| 66 | + .format(app.config.idf_target, self.project_description['target']) |
| 67 | + ) |
| 68 | + |
| 69 | + app.emit('project-build-info', self.project_description) |
| 70 | + return [] |
| 71 | + |
| 72 | + |
| 73 | +at_idf_builder = AtIdfBuilder() |
| 74 | + |
| 75 | + |
| 76 | +def setup(app): |
| 77 | + try: |
| 78 | + build_dir = os.environ['BUILDDIR'] |
| 79 | + except KeyError: |
| 80 | + build_dir = os.path.dirname(app.doctreedir.rstrip(os.sep)) |
| 81 | + |
| 82 | + for directory in (build_dir, os.path.join(build_dir, 'inc')): |
| 83 | + try: |
| 84 | + os.mkdir(directory) |
| 85 | + except OSError: |
| 86 | + pass |
| 87 | + |
| 88 | + app.add_event('project-build-info') |
| 89 | + app.connect('config-inited', at_idf_builder.generate_idf_info) |
| 90 | + |
| 91 | + return {'parallel_read_safe': True, 'parallel_write_safe': True, 'version': '0.1'} |
0 commit comments